declaration-visitor.h 4.11 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_DECLARATION_VISITOR_H_
#define V8_TORQUE_DECLARATION_VISITOR_H_

#include <set>
#include <string>

11
#include "src/base/macros.h"
12
#include "src/torque/declarations.h"
13 14 15 16 17 18 19 20
#include "src/torque/global-context.h"
#include "src/torque/types.h"
#include "src/torque/utils.h"

namespace v8 {
namespace internal {
namespace torque {

21 22
Namespace* GetOrCreateNamespace(const std::string& name);

23
class PredeclarationVisitor {
24
 public:
25
  static void Predeclare(Ast* ast) {
26
    CurrentScope::Scope current_namespace(GlobalContext::GetDefaultNamespace());
27
    for (Declaration* child : ast->declarations()) Predeclare(child);
28
  }
29
  static void ResolvePredeclarations();
30

31 32 33 34 35 36 37 38
 private:
  static void Predeclare(Declaration* decl);
  static void Predeclare(NamespaceDeclaration* decl) {
    CurrentScope::Scope current_scope(GetOrCreateNamespace(decl->name));
    for (Declaration* child : decl->declarations) Predeclare(child);
  }
  static void Predeclare(TypeDeclaration* decl) {
    Declarations::PredeclareTypeAlias(decl->name, decl, false);
39
  }
40
  static void Predeclare(StructDeclaration* decl) {
41 42 43 44 45
    Declarations::PredeclareTypeAlias(decl->name, decl, false);
  }
  static void Predeclare(GenericTypeDeclaration* generic_decl) {
    Declarations::DeclareGenericType(generic_decl->declaration->name->value,
                                     generic_decl);
46
  }
47 48 49
  static void Predeclare(GenericCallableDeclaration* generic_decl) {
    Declarations::DeclareGenericCallable(generic_decl->declaration->name->value,
                                         generic_decl);
50
  }
51
};
52

53 54 55 56 57 58 59 60
class DeclarationVisitor {
 public:
  static void Visit(Ast* ast) {
    CurrentScope::Scope current_namespace(GlobalContext::GetDefaultNamespace());
    for (Declaration* child : ast->declarations()) Visit(child);
  }
  static void Visit(Declaration* decl);
  static void Visit(NamespaceDeclaration* decl) {
61
    CurrentScope::Scope current_scope(GetOrCreateNamespace(decl->name));
62 63 64
    for (Declaration* child : decl->declarations) Visit(child);
  }

65 66 67 68
  static void Visit(TypeDeclaration* decl) {
    // Looking up the type will trigger type computation; this ensures errors
    // are reported even if the type is unused.
    Declarations::LookupType(decl->name);
69
  }
70
  static void Visit(StructDeclaration* decl) {
71
    Declarations::LookupType(decl->name);
72
  }
73

74 75 76 77 78
  static Builtin* CreateBuiltin(BuiltinDeclaration* decl,
                                std::string external_name,
                                std::string readable_name, Signature signature,
                                base::Optional<Statement*> body);

79 80 81 82 83 84
  static void Visit(ExternalBuiltinDeclaration* decl);
  static void Visit(ExternalRuntimeDeclaration* decl);
  static void Visit(ExternalMacroDeclaration* decl);
  static void Visit(TorqueBuiltinDeclaration* decl);
  static void Visit(TorqueMacroDeclaration* decl);
  static void Visit(IntrinsicDeclaration* decl);
85 86

  static void Visit(ConstDeclaration* decl);
87 88 89 90
  static void Visit(GenericCallableDeclaration* decl) {
    // The PredeclarationVisitor already handled this case.
  }
  static void Visit(GenericTypeDeclaration* decl) {
91 92
    // The PredeclarationVisitor already handled this case.
  }
93 94 95 96
  static void Visit(SpecializationDeclaration* decl);
  static void Visit(ExternConstDeclaration* decl);
  static void Visit(CppIncludeDeclaration* decl);

97
  static Signature MakeSpecializedSignature(
98 99 100
      const SpecializationKey<GenericCallable>& key);
  static Callable* SpecializeImplicit(
      const SpecializationKey<GenericCallable>& key);
101
  static Callable* Specialize(
102 103
      const SpecializationKey<GenericCallable>& key,
      CallableDeclaration* declaration,
104
      base::Optional<const SpecializationDeclaration*> explicit_specialization,
105
      base::Optional<Statement*> body, SourcePosition position);
106

107
 private:
108 109
  static void DeclareSpecializedTypes(
      const SpecializationKey<GenericCallable>& key);
110 111 112 113 114 115 116
};

}  // namespace torque
}  // namespace internal
}  // namespace v8

#endif  // V8_TORQUE_DECLARATION_VISITOR_H_