declarations.h 5.87 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// 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_

#include <string>

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

namespace v8 {
namespace internal {
namespace torque {

17
static constexpr const char* const kFromConstexprMacroName = "FromConstexpr";
18 19
static constexpr const char* kTrueLabelName = "_True";
static constexpr const char* kFalseLabelName = "_False";
20

21 22 23 24 25 26 27 28 29 30 31
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;
}

32 33
class Declarations {
 public:
34
  static std::vector<Declarable*> TryLookup(const QualifiedName& name) {
35 36
    return CurrentScope::Get()->Lookup(name);
  }
37

38
  static std::vector<Declarable*> TryLookupShallow(const QualifiedName& name) {
39 40
    return CurrentScope::Get()->LookupShallow(name);
  }
41

42
  template <class T>
43
  static std::vector<T*> TryLookup(const QualifiedName& name) {
44
    return FilterDeclarables<T>(TryLookup(name));
45 46
  }

47
  static std::vector<Declarable*> Lookup(const QualifiedName& name) {
48 49
    std::vector<Declarable*> d = TryLookup(name);
    if (d.empty()) {
50
      ReportError("cannot find \"", name, "\"");
51 52 53 54
    }
    return d;
  }

55 56
  static std::vector<Declarable*> LookupGlobalScope(const std::string& name);

57 58
  static const Type* LookupType(const QualifiedName& name);
  static const Type* LookupType(std::string name);
59 60
  static const Type* LookupGlobalType(const std::string& name);
  static const Type* GetType(TypeExpression* type_expression);
61

62
  static Builtin* FindSomeInternalBuiltinWithType(
63
      const BuiltinPointerType* type);
64

65
  static Value* LookupValue(const QualifiedName& name);
66

67 68
  static Macro* TryLookupMacro(const std::string& name,
                               const TypeVector& types);
69
  static base::Optional<Builtin*> TryLookupBuiltin(const QualifiedName& name);
70

71
  static std::vector<Generic*> LookupGeneric(const std::string& name);
72
  static Generic* LookupUniqueGeneric(const QualifiedName& name);
73

74
  static Namespace* DeclareNamespace(const std::string& name);
75

76
  static const AbstractType* DeclareAbstractType(
77
      const std::string& name, bool transient, const std::string& generated,
78 79
      base::Optional<const AbstractType*> non_constexpr_version,
      const base::Optional<std::string>& parent = {});
80

81 82
  static void DeclareType(const std::string& name, const Type* type,
                          bool redeclaration);
83

84 85
  static StructType* DeclareStruct(const std::string& name,
                                   const std::vector<Field>& fields);
86

87 88 89 90
  static ClassType* DeclareClass(base::Optional<std::string> parent,
                                 const std::string& name, bool transient,
                                 const std::string& generates,
                                 std::vector<Field> fields, size_t size);
91

92 93 94 95
  static Macro* CreateMacro(std::string external_name,
                            std::string readable_name,
                            base::Optional<std::string> external_assembler_name,
                            Signature signature, bool transitioning,
96
                            base::Optional<Statement*> body);
97 98 99 100 101 102
  static Macro* DeclareMacro(
      const std::string& name,
      base::Optional<std::string> external_assembler_name,
      const Signature& signature, bool transitioning,
      base::Optional<Statement*> body, base::Optional<std::string> op = {});

103 104 105 106
  static Method* CreateMethod(AggregateType* class_type,
                              const std::string& name, Signature signature,
                              bool transitioning, Statement* body);

107 108 109 110 111 112
  static Intrinsic* CreateIntrinsic(const std::string& name,
                                    const Signature& signature);

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

113 114 115
  static Builtin* CreateBuiltin(std::string external_name,
                                std::string readable_name, Builtin::Kind kind,
                                Signature signature, bool transitioning,
116 117 118 119
                                base::Optional<Statement*> body);
  static Builtin* DeclareBuiltin(const std::string& name, Builtin::Kind kind,
                                 const Signature& signature, bool transitioning,
                                 base::Optional<Statement*> body);
120

121 122 123
  static RuntimeFunction* DeclareRuntimeFunction(const std::string& name,
                                                 const Signature& signature,
                                                 bool transitioning);
124

125 126
  static void DeclareExternConstant(const std::string& name, const Type* type,
                                    std::string value);
127 128 129
  static NamespaceConstant* DeclareNamespaceConstant(const std::string& name,
                                                     const Type* type,
                                                     Expression* body);
130

131 132
  static Generic* DeclareGeneric(const std::string& name,
                                 GenericDeclaration* generic);
133

134
  template <class T>
135 136 137
  static T* Declare(const std::string& name, T* d) {
    CurrentScope::Get()->AddDeclarable(name, d);
    return d;
138
  }
139 140 141 142
  template <class T>
  static T* Declare(const std::string& name, std::unique_ptr<T> d) {
    return CurrentScope::Get()->AddDeclarable(name,
                                              RegisterDeclarable(std::move(d)));
143
  }
144
  static Macro* DeclareOperator(const std::string& name, Macro* m);
145

146 147
  static std::string GetGeneratedCallableName(
      const std::string& name, const TypeVector& specialized_types);
148 149
};

150 151 152 153 154
}  // namespace torque
}  // namespace internal
}  // namespace v8

#endif  // V8_TORQUE_DECLARATIONS_H_