summaryrefslogtreecommitdiff
path: root/deps/v8/src/torque/declaration-visitor.h
blob: dbd28f4b873afadb267b9e786602c587e5fd9d98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// 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>

#include "src/base/macros.h"
#include "src/torque/declarations.h"
#include "src/torque/global-context.h"
#include "src/torque/types.h"
#include "src/torque/utils.h"

namespace v8 {
namespace internal {
namespace torque {

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

class PredeclarationVisitor {
 public:
  static void Predeclare(Ast* ast) {
    CurrentScope::Scope current_namespace(GlobalContext::GetDefaultNamespace());
    for (Declaration* child : ast->declarations()) Predeclare(child);
  }
  static void ResolvePredeclarations();

 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);
  }
  static void Predeclare(StructDeclaration* decl) {
    if (decl->IsGeneric()) {
      Declarations::DeclareGenericStructType(decl->name->value, decl);
    } else {
      Declarations::PredeclareTypeAlias(decl->name, decl, false);
    }
  }
  static void Predeclare(GenericDeclaration* decl) {
    Declarations::DeclareGeneric(decl->callable->name, decl);
  }
};

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) {
    CurrentScope::Scope current_scope(GetOrCreateNamespace(decl->name));
    for (Declaration* child : decl->declarations) Visit(child);
  }

  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);
  }
  static void Visit(StructDeclaration* decl) {
    if (!decl->IsGeneric()) {
      Declarations::LookupType(decl->name);
    }
  }

  static Builtin* CreateBuiltin(BuiltinDeclaration* decl,
                                std::string external_name,
                                std::string readable_name, Signature signature,
                                base::Optional<Statement*> body);
  static void Visit(ExternalBuiltinDeclaration* decl,
                    const Signature& signature,
                    base::Optional<Statement*> body) {
    Declarations::Declare(
        decl->name,
        CreateBuiltin(decl, decl->name, decl->name, signature, base::nullopt));
  }

  static void Visit(ExternalRuntimeDeclaration* decl, const Signature& sig,
                    base::Optional<Statement*> body);
  static void Visit(ExternalMacroDeclaration* decl, const Signature& sig,
                    base::Optional<Statement*> body);
  static void Visit(TorqueBuiltinDeclaration* decl, const Signature& signature,
                    base::Optional<Statement*> body);
  static void Visit(TorqueMacroDeclaration* decl, const Signature& signature,
                    base::Optional<Statement*> body);
  static void Visit(IntrinsicDeclaration* decl, const Signature& signature,
                    base::Optional<Statement*> body);

  static void Visit(CallableNode* decl, const Signature& signature,
                    base::Optional<Statement*> body);

  static void Visit(ConstDeclaration* decl);
  static void Visit(StandardDeclaration* decl);
  static void Visit(GenericDeclaration* decl) {
    // The PredeclarationVisitor already handled this case.
  }
  static void Visit(SpecializationDeclaration* decl);
  static void Visit(ExternConstDeclaration* decl);
  static void Visit(CppIncludeDeclaration* decl);

  static Signature MakeSpecializedSignature(const SpecializationKey& key);
  static Callable* SpecializeImplicit(const SpecializationKey& key);
  static Callable* Specialize(
      const SpecializationKey& key, CallableNode* declaration,
      base::Optional<const CallableNodeSignature*> signature,
      base::Optional<Statement*> body, SourcePosition position);

 private:
  static void DeclareSpecializedTypes(const SpecializationKey& key);
};

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

#endif  // V8_TORQUE_DECLARATION_VISITOR_H_