• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef V8_TORQUE_DECLARATION_VISITOR_H_
6 #define V8_TORQUE_DECLARATION_VISITOR_H_
7 
8 #include <set>
9 #include <string>
10 
11 #include "src/base/macros.h"
12 #include "src/torque/declarations.h"
13 #include "src/torque/file-visitor.h"
14 #include "src/torque/global-context.h"
15 #include "src/torque/scope.h"
16 #include "src/torque/types.h"
17 #include "src/torque/utils.h"
18 
19 namespace v8 {
20 namespace internal {
21 namespace torque {
22 
23 class DeclarationVisitor : public FileVisitor {
24  public:
DeclarationVisitor(GlobalContext & global_context)25   explicit DeclarationVisitor(GlobalContext& global_context)
26       : FileVisitor(global_context),
27         scope_(declarations(), global_context.GetDefaultModule()) {}
28 
Visit(Ast * ast)29   void Visit(Ast* ast) {
30     Visit(ast->default_module());
31     DrainSpecializationQueue();
32   }
33 
34   void Visit(Expression* expr);
35   void Visit(Statement* stmt);
36   void Visit(Declaration* decl);
37 
Visit(ModuleDeclaration * decl)38   void Visit(ModuleDeclaration* decl) {
39     ScopedModuleActivator activator(this, decl->GetModule());
40     Declarations::ModuleScopeActivator scope(declarations(), decl->GetModule());
41     for (Declaration* child : decl->declarations) Visit(child);
42   }
Visit(DefaultModuleDeclaration * decl)43   void Visit(DefaultModuleDeclaration* decl) {
44     decl->SetModule(global_context_.GetDefaultModule());
45     Visit(implicit_cast<ModuleDeclaration*>(decl));
46   }
Visit(ExplicitModuleDeclaration * decl)47   void Visit(ExplicitModuleDeclaration* decl) {
48     decl->SetModule(global_context_.GetModule(decl->name));
49     Visit(implicit_cast<ModuleDeclaration*>(decl));
50   }
51 
52   void Visit(IdentifierExpression* expr);
Visit(NumberLiteralExpression * expr)53   void Visit(NumberLiteralExpression* expr) {}
Visit(StringLiteralExpression * expr)54   void Visit(StringLiteralExpression* expr) {}
55   void Visit(CallExpression* expr);
Visit(ElementAccessExpression * expr)56   void Visit(ElementAccessExpression* expr) {
57     Visit(expr->array);
58     Visit(expr->index);
59   }
Visit(FieldAccessExpression * expr)60   void Visit(FieldAccessExpression* expr) { Visit(expr->object); }
Visit(BlockStatement * expr)61   void Visit(BlockStatement* expr) {
62     Declarations::NodeScopeActivator scope(declarations(), expr);
63     for (Statement* stmt : expr->statements) Visit(stmt);
64   }
Visit(ExpressionStatement * stmt)65   void Visit(ExpressionStatement* stmt) { Visit(stmt->expression); }
Visit(TailCallStatement * stmt)66   void Visit(TailCallStatement* stmt) { Visit(stmt->call); }
67   void Visit(TypeDeclaration* decl);
68 
Visit(TypeAliasDeclaration * decl)69   void Visit(TypeAliasDeclaration* decl) {
70     const Type* type = declarations()->GetType(decl->type);
71     type->AddAlias(decl->name);
72     declarations()->DeclareType(decl->name, type);
73   }
74 
75   Builtin* BuiltinDeclarationCommon(BuiltinDeclaration* decl, bool external,
76                                     const Signature& signature);
77 
Visit(ExternalBuiltinDeclaration * decl,const Signature & signature,Statement * body)78   void Visit(ExternalBuiltinDeclaration* decl, const Signature& signature,
79              Statement* body) {
80     BuiltinDeclarationCommon(decl, true, signature);
81   }
82 
83   void Visit(ExternalRuntimeDeclaration* decl, const Signature& sig,
84              Statement* body);
85   void Visit(ExternalMacroDeclaration* decl, const Signature& sig,
86              Statement* body);
87   void Visit(TorqueBuiltinDeclaration* decl, const Signature& signature,
88              Statement* body);
89   void Visit(TorqueMacroDeclaration* decl, const Signature& signature,
90              Statement* body);
91 
92   void Visit(CallableNode* decl, const Signature& signature, Statement* body);
93 
94   void Visit(ConstDeclaration* decl);
95   void Visit(StandardDeclaration* decl);
96   void Visit(GenericDeclaration* decl);
97   void Visit(SpecializationDeclaration* decl);
98   void Visit(ReturnStatement* stmt);
99 
Visit(DebugStatement * stmt)100   void Visit(DebugStatement* stmt) {}
Visit(AssertStatement * stmt)101   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   void Visit(StructDeclaration* decl);
Visit(StructExpression * decl)113   void Visit(StructExpression* decl) {}
114 
115   void Visit(LogicalOrExpression* expr);
116   void Visit(LogicalAndExpression* expr);
117   void DeclareExpressionForBranch(Expression* node);
118 
119   void Visit(ConditionalExpression* expr);
120   void Visit(IfStatement* stmt);
121   void Visit(WhileStatement* stmt);
122   void Visit(ForOfLoopStatement* stmt);
123 
Visit(AssignmentExpression * expr)124   void Visit(AssignmentExpression* expr) {
125     MarkLocationModified(expr->location);
126     Visit(expr->location);
127     Visit(expr->value);
128   }
129 
Visit(BreakStatement * stmt)130   void Visit(BreakStatement* stmt) {}
Visit(ContinueStatement * stmt)131   void Visit(ContinueStatement* stmt) {}
Visit(GotoStatement * expr)132   void Visit(GotoStatement* expr) {}
133   void Visit(ForLoopStatement* stmt);
134 
Visit(IncrementDecrementExpression * expr)135   void Visit(IncrementDecrementExpression* expr) {
136     MarkLocationModified(expr->location);
137     Visit(expr->location);
138   }
139 
Visit(AssumeTypeImpossibleExpression * expr)140   void Visit(AssumeTypeImpossibleExpression* expr) { Visit(expr->expression); }
141 
142   void Visit(TryLabelStatement* stmt);
143   void GenerateHeader(std::string& file_name);
144 
145  private:
146   struct LiveAndChanged {
147     std::set<const Variable*> live;
148     std::set<const Variable*> changed;
149   };
150 
PushControlSplit()151   void PushControlSplit() {
152     LiveAndChanged live_and_changed;
153     live_and_changed.live = declarations()->GetLiveVariables();
154     live_and_changed_variables_.push_back(live_and_changed);
155   }
156 
157   Variable* DeclareVariable(const std::string& name, const Type* type,
158                             bool is_const);
159   Parameter* DeclareParameter(const std::string& name, const Type* type);
160 
PopControlSplit()161   std::set<const Variable*> PopControlSplit() {
162     auto result = live_and_changed_variables_.back().changed;
163     live_and_changed_variables_.pop_back();
164     return result;
165   }
166 
167   void MarkLocationModified(Expression* location);
168   bool MarkVariableModified(const Variable* variable);
169   void DeclareSignature(const Signature& signature);
170   void DeclareSpecializedTypes(const SpecializationKey& key);
171 
172   void Specialize(const SpecializationKey& key, CallableNode* callable,
173                   const CallableNodeSignature* signature,
174                   Statement* body) override;
175 
176   Declarations::ModuleScopeActivator scope_;
177   std::vector<Builtin*> torque_builtins_;
178   std::vector<LiveAndChanged> live_and_changed_variables_;
179 };
180 
181 }  // namespace torque
182 }  // namespace internal
183 }  // namespace v8
184 
185 #endif  // V8_TORQUE_DECLARATION_VISITOR_H_
186