declaration-visitor.h 5.9 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 24 25
#include "src/torque/file-visitor.h"
#include "src/torque/global-context.h"
#include "src/torque/scope.h"
#include "src/torque/types.h"
#include "src/torque/utils.h"

namespace v8 {
namespace internal {
namespace torque {

class DeclarationVisitor : public FileVisitor {
 public:
  explicit DeclarationVisitor(GlobalContext& global_context)
26
      : FileVisitor(global_context),
27
        scope_(declarations(), global_context.GetDefaultModule()) {}
28

29 30 31 32
  void Visit(Ast* ast) {
    Visit(ast->default_module());
    DrainSpecializationQueue();
  }
33 34 35 36 37 38

  void Visit(Expression* expr);
  void Visit(Statement* stmt);
  void Visit(Declaration* decl);

  void Visit(ModuleDeclaration* decl) {
39
    ScopedModuleActivator activator(this, decl->GetModule());
40
    Declarations::ModuleScopeActivator scope(declarations(), decl->GetModule());
41 42 43 44
    for (Declaration* child : decl->declarations) Visit(child);
  }
  void Visit(DefaultModuleDeclaration* decl) {
    decl->SetModule(global_context_.GetDefaultModule());
45
    Visit(implicit_cast<ModuleDeclaration*>(decl));
46 47 48
  }
  void Visit(ExplicitModuleDeclaration* decl) {
    decl->SetModule(global_context_.GetModule(decl->name));
49
    Visit(implicit_cast<ModuleDeclaration*>(decl));
50 51
  }

52
  void Visit(IdentifierExpression* expr);
53 54
  void Visit(NumberLiteralExpression* expr) {}
  void Visit(StringLiteralExpression* expr) {}
55
  void Visit(CallExpression* expr);
56 57 58 59 60 61
  void Visit(ElementAccessExpression* expr) {
    Visit(expr->array);
    Visit(expr->index);
  }
  void Visit(FieldAccessExpression* expr) { Visit(expr->object); }
  void Visit(BlockStatement* expr) {
62
    Declarations::NodeScopeActivator scope(declarations(), expr);
63 64 65 66
    for (Statement* stmt : expr->statements) Visit(stmt);
  }
  void Visit(ExpressionStatement* stmt) { Visit(stmt->expression); }
  void Visit(TailCallStatement* stmt) { Visit(stmt->call); }
67
  void Visit(TypeDeclaration* decl);
68

69
  void Visit(TypeAliasDeclaration* decl) {
70 71 72
    const Type* type = declarations()->GetType(decl->type);
    type->AddAlias(decl->name);
    declarations()->DeclareType(decl->name, type);
73 74
  }

75
  Builtin* BuiltinDeclarationCommon(BuiltinDeclaration* decl, bool external,
76
                                    const Signature& signature);
77

78 79
  void Visit(ExternalBuiltinDeclaration* decl, const Signature& signature,
             Statement* body) {
80
    BuiltinDeclarationCommon(decl, true, signature);
81 82
  }

83 84 85 86 87 88 89 90
  void Visit(ExternalRuntimeDeclaration* decl, const Signature& sig,
             Statement* body);
  void Visit(ExternalMacroDeclaration* decl, const Signature& sig,
             Statement* body);
  void Visit(TorqueBuiltinDeclaration* decl, const Signature& signature,
             Statement* body);
  void Visit(TorqueMacroDeclaration* decl, const Signature& signature,
             Statement* body);
91

92
  void Visit(CallableNode* decl, const Signature& signature, Statement* body);
93

94
  void Visit(ConstDeclaration* decl);
95 96 97
  void Visit(StandardDeclaration* decl);
  void Visit(GenericDeclaration* decl);
  void Visit(SpecializationDeclaration* decl);
98 99
  void Visit(ReturnStatement* stmt);

100 101
  void Visit(DebugStatement* stmt) {}
  void Visit(AssertStatement* stmt) {
102
    bool do_check = !stmt->debug_only;
103
#if defined(DEBUG)
104
    do_check = true;
105
#endif
106
    if (do_check) DeclareExpressionForBranch(stmt->expression);
107 108
  }

109
  void Visit(VarDeclarationStatement* stmt);
110
  void Visit(ExternConstDeclaration* decl);
111 112 113 114

  void Visit(StructDeclaration* decl);
  void Visit(StructExpression* decl) {}

115 116 117
  void Visit(LogicalOrExpression* expr);
  void Visit(LogicalAndExpression* expr);
  void DeclareExpressionForBranch(Expression* node);
118

119 120 121
  void Visit(ConditionalExpression* expr);
  void Visit(IfStatement* stmt);
  void Visit(WhileStatement* stmt);
122 123 124 125 126 127 128 129 130 131 132
  void Visit(ForOfLoopStatement* stmt);

  void Visit(AssignmentExpression* expr) {
    MarkLocationModified(expr->location);
    Visit(expr->location);
    Visit(expr->value);
  }

  void Visit(BreakStatement* stmt) {}
  void Visit(ContinueStatement* stmt) {}
  void Visit(GotoStatement* expr) {}
133
  void Visit(ForLoopStatement* stmt);
134 135 136 137 138 139

  void Visit(IncrementDecrementExpression* expr) {
    MarkLocationModified(expr->location);
    Visit(expr->location);
  }

140 141
  void Visit(AssumeTypeImpossibleExpression* expr) { Visit(expr->expression); }

142
  void Visit(TryLabelStatement* stmt);
143
  void GenerateHeader(std::string& file_name);
144 145 146 147 148 149 150 151 152

 private:
  struct LiveAndChanged {
    std::set<const Variable*> live;
    std::set<const Variable*> changed;
  };

  void PushControlSplit() {
    LiveAndChanged live_and_changed;
153
    live_and_changed.live = declarations()->GetLiveVariables();
154 155 156
    live_and_changed_variables_.push_back(live_and_changed);
  }

157 158 159 160
  Variable* DeclareVariable(const std::string& name, const Type* type,
                            bool is_const);
  Parameter* DeclareParameter(const std::string& name, const Type* type);

161 162 163 164 165 166
  std::set<const Variable*> PopControlSplit() {
    auto result = live_and_changed_variables_.back().changed;
    live_and_changed_variables_.pop_back();
    return result;
  }

167 168 169
  void MarkLocationModified(Expression* location);
  bool MarkVariableModified(const Variable* variable);
  void DeclareSignature(const Signature& signature);
170 171
  void DeclareSpecializedTypes(const SpecializationKey& key);

172 173 174 175
  void Specialize(const SpecializationKey& key, CallableNode* callable,
                  const CallableNodeSignature* signature,
                  Statement* body) override;

176
  Declarations::ModuleScopeActivator scope_;
177
  std::vector<Builtin*> torque_builtins_;
178 179 180 181 182 183 184 185
  std::vector<LiveAndChanged> live_and_changed_variables_;
};

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

#endif  // V8_TORQUE_DECLARATION_VISITOR_H_