declaration-visitor.h 3.56 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 21 22 23
#include "src/torque/file-visitor.h"
#include "src/torque/global-context.h"
#include "src/torque/types.h"
#include "src/torque/utils.h"

namespace v8 {
namespace internal {
namespace torque {

class DeclarationVisitor : public FileVisitor {
 public:
24
  void Visit(Ast* ast) {
25
    CurrentScope::Scope current_namespace(GlobalContext::GetDefaultNamespace());
26
    for (Declaration* child : ast->declarations()) Visit(child);
27
  }
28 29 30

  void Visit(Declaration* decl);

31 32
  Namespace* GetOrCreateNamespace(const std::string& name) {
    std::vector<Namespace*> existing_namespaces = FilterDeclarables<Namespace>(
33
        Declarations::TryLookupShallow(QualifiedName(name)));
34 35
    if (existing_namespaces.empty()) {
      return Declarations::DeclareNamespace(name);
36
    }
37 38
    DCHECK_EQ(1, existing_namespaces.size());
    return existing_namespaces.front();
39 40
  }

41 42
  void Visit(NamespaceDeclaration* decl) {
    CurrentScope::Scope current_scope(GetOrCreateNamespace(decl->name));
43 44 45
    for (Declaration* child : decl->declarations) Visit(child);
  }

46
  void Visit(TypeDeclaration* decl);
47
  void Visit(ClassDeclaration* decl);
48

49
  void Visit(TypeAliasDeclaration* decl) {
50
    const Type* type = Declarations::GetType(decl->type);
51
    type->AddAlias(decl->name);
52
    Declarations::DeclareType(decl->name, type, true);
53 54
  }

55 56
  Builtin* CreateBuiltin(BuiltinDeclaration* decl, std::string external_name,
                         std::string readable_name, Signature signature,
57
                         base::Optional<Statement*> body);
58
  void Visit(ExternalBuiltinDeclaration* decl, const Signature& signature,
59 60
             base::Optional<Statement*> body) {
    Declarations::Declare(
61 62
        decl->name,
        CreateBuiltin(decl, decl->name, decl->name, signature, base::nullopt));
63 64
  }

65
  void Visit(ExternalRuntimeDeclaration* decl, const Signature& sig,
66
             base::Optional<Statement*> body);
67
  void Visit(ExternalMacroDeclaration* decl, const Signature& sig,
68
             base::Optional<Statement*> body);
69
  void Visit(TorqueBuiltinDeclaration* decl, const Signature& signature,
70
             base::Optional<Statement*> body);
71
  void Visit(TorqueMacroDeclaration* decl, const Signature& signature,
72
             base::Optional<Statement*> body);
73 74
  void Visit(IntrinsicDeclaration* decl, const Signature& signature,
             base::Optional<Statement*> body);
75

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

79
  void Visit(ConstDeclaration* decl);
80 81 82
  void Visit(StandardDeclaration* decl);
  void Visit(GenericDeclaration* decl);
  void Visit(SpecializationDeclaration* decl);
83
  void Visit(ExternConstDeclaration* decl);
84
  void Visit(StructDeclaration* decl);
85
  void Visit(CppIncludeDeclaration* decl);
86

87 88 89 90
  Signature MakeSpecializedSignature(const SpecializationKey& key);
  Callable* SpecializeImplicit(const SpecializationKey& key);
  Callable* Specialize(const SpecializationKey& key, CallableNode* declaration,
                       base::Optional<const CallableNodeSignature*> signature,
91
                       base::Optional<Statement*> body);
92 93

 private:
94
  void DeclareSpecializedTypes(const SpecializationKey& key);
95 96 97 98 99 100 101
};

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

#endif  // V8_TORQUE_DECLARATION_VISITOR_H_