declarations.h 6.61 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_TORQUE_DECLARATIONS_H_
#define V8_TORQUE_DECLARATIONS_H_

8
#include <memory>
9 10 11
#include <string>

#include "src/torque/declarable.h"
12
#include "src/torque/utils.h"
13 14 15 16 17

namespace v8 {
namespace internal {
namespace torque {

18
static constexpr const char* const kFromConstexprMacroName = "FromConstexpr";
19 20 21 22 23
static constexpr const char* kMacroEndLabelName = "__macro_end";
static constexpr const char* kBreakLabelName = "__break";
static constexpr const char* kContinueLabelName = "__continue";
static constexpr const char* kCatchLabelName = "__catch";
static constexpr const char* kNextCaseLabelName = "__NextCase";
24

25 26 27 28 29 30 31 32 33 34 35
template <class T>
std::vector<T*> FilterDeclarables(const std::vector<Declarable*> list) {
  std::vector<T*> result;
  for (Declarable* declarable : list) {
    if (T* t = T::DynamicCast(declarable)) {
      result.push_back(t);
    }
  }
  return result;
}

36 37 38 39 40 41 42 43 44
inline std::string UnwrapTNodeTypeName(const std::string& generates) {
  if (generates.length() < 7 || generates.substr(0, 6) != "TNode<" ||
      generates.substr(generates.length() - 1, 1) != ">") {
    ReportError("generated type \"", generates,
                "\" should be of the form \"TNode<...>\"");
  }
  return generates.substr(6, generates.length() - 7);
}

45 46
class Declarations {
 public:
47
  static std::vector<Declarable*> TryLookup(const QualifiedName& name) {
48 49
    return CurrentScope::Get()->Lookup(name);
  }
50

51
  static std::vector<Declarable*> TryLookupShallow(const QualifiedName& name) {
52 53
    return CurrentScope::Get()->LookupShallow(name);
  }
54

55
  template <class T>
56
  static std::vector<T*> TryLookup(const QualifiedName& name) {
57
    return FilterDeclarables<T>(TryLookup(name));
58 59
  }

60
  static std::vector<Declarable*> Lookup(const QualifiedName& name) {
61 62
    std::vector<Declarable*> d = TryLookup(name);
    if (d.empty()) {
63
      ReportError("cannot find \"", name, "\"");
64 65 66 67
    }
    return d;
  }

68
  static std::vector<Declarable*> LookupGlobalScope(const QualifiedName& name);
69

70
  static const TypeAlias* LookupTypeAlias(const QualifiedName& name);
71
  static const Type* LookupType(const QualifiedName& name);
72
  static const Type* LookupType(const Identifier* identifier);
73
  static base::Optional<const Type*> TryLookupType(const QualifiedName& name);
74
  static const Type* LookupGlobalType(const QualifiedName& name);
75

76
  static Builtin* FindSomeInternalBuiltinWithType(
77
      const BuiltinPointerType* type);
78

79
  static Value* LookupValue(const QualifiedName& name);
80

81 82
  static Macro* TryLookupMacro(const std::string& name,
                               const TypeVector& types);
83
  static base::Optional<Builtin*> TryLookupBuiltin(const QualifiedName& name);
84

85 86
  static std::vector<GenericCallable*> LookupGeneric(const std::string& name);
  static GenericCallable* LookupUniqueGeneric(const QualifiedName& name);
87

88
  static GenericType* LookupUniqueGenericType(const QualifiedName& name);
89
  static GenericType* LookupGlobalUniqueGenericType(const std::string& name);
90
  static base::Optional<GenericType*> TryLookupGenericType(
91
      const QualifiedName& name);
92

93
  static Namespace* DeclareNamespace(const std::string& name);
94
  static TypeAlias* DeclareType(const Identifier* name, const Type* type);
95

96 97 98
  static const TypeAlias* PredeclareTypeAlias(const Identifier* name,
                                              TypeDeclaration* type,
                                              bool redeclaration);
99 100 101
  static TorqueMacro* CreateTorqueMacro(std::string external_name,
                                        std::string readable_name,
                                        bool exported_to_csa,
102
                                        Signature signature,
103 104 105 106
                                        base::Optional<Statement*> body,
                                        bool is_user_defined);
  static ExternMacro* CreateExternMacro(std::string name,
                                        std::string external_assembler_name,
107
                                        Signature signature);
108
  static Macro* DeclareMacro(
109
      const std::string& name, bool accessible_from_csa,
110
      base::Optional<std::string> external_assembler_name,
111 112
      const Signature& signature, base::Optional<Statement*> body,
      base::Optional<std::string> op = {}, bool is_user_defined = true);
113

114 115
  static Method* CreateMethod(AggregateType* class_type,
                              const std::string& name, Signature signature,
116
                              Statement* body);
117

118 119 120 121 122 123
  static Intrinsic* CreateIntrinsic(const std::string& name,
                                    const Signature& signature);

  static Intrinsic* DeclareIntrinsic(const std::string& name,
                                     const Signature& signature);

124 125
  static Builtin* CreateBuiltin(std::string external_name,
                                std::string readable_name, Builtin::Kind kind,
126
                                Signature signature,
127 128
                                base::Optional<Statement*> body);
  static Builtin* DeclareBuiltin(const std::string& name, Builtin::Kind kind,
129
                                 const Signature& signature,
130
                                 base::Optional<Statement*> body);
131

132
  static RuntimeFunction* DeclareRuntimeFunction(const std::string& name,
133
                                                 const Signature& signature);
134

135
  static void DeclareExternConstant(Identifier* name, const Type* type,
136
                                    std::string value);
137
  static NamespaceConstant* DeclareNamespaceConstant(Identifier* name,
138 139
                                                     const Type* type,
                                                     Expression* body);
140

141 142 143 144
  static GenericCallable* DeclareGenericCallable(
      const std::string& name, GenericCallableDeclaration* ast_node);
  static GenericType* DeclareGenericType(const std::string& name,
                                         GenericTypeDeclaration* ast_node);
145

146
  template <class T>
147 148 149
  static T* Declare(const std::string& name, T* d) {
    CurrentScope::Get()->AddDeclarable(name, d);
    return d;
150
  }
151 152 153 154
  template <class T>
  static T* Declare(const std::string& name, std::unique_ptr<T> d) {
    return CurrentScope::Get()->AddDeclarable(name,
                                              RegisterDeclarable(std::move(d)));
155
  }
156
  static Macro* DeclareOperator(const std::string& name, Macro* m);
157

158 159
  static std::string GetGeneratedCallableName(
      const std::string& name, const TypeVector& specialized_types);
160 161
};

162 163 164 165 166
}  // namespace torque
}  // namespace internal
}  // namespace v8

#endif  // V8_TORQUE_DECLARATIONS_H_