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_FILE_VISITOR_H_
6 #define V8_TORQUE_FILE_VISITOR_H_
7 
8 #include <deque>
9 #include <string>
10 
11 #include "src/torque/ast.h"
12 #include "src/torque/global-context.h"
13 #include "src/torque/types.h"
14 #include "src/torque/utils.h"
15 
16 namespace v8 {
17 namespace internal {
18 namespace torque {
19 
20 class FileVisitor {
21  public:
FileVisitor(GlobalContext & global_context)22   explicit FileVisitor(GlobalContext& global_context)
23       : global_context_(global_context),
24         declarations_(global_context.declarations()),
25         module_(global_context.GetDefaultModule()) {}
26 
GetTypeVector(const std::vector<TypeExpression * > & v)27   TypeVector GetTypeVector(const std::vector<TypeExpression*>& v) {
28     TypeVector result;
29     for (TypeExpression* t : v) {
30       result.push_back(declarations()->GetType(t));
31     }
32     return result;
33   }
34 
ast()35   Ast* ast() { return global_context_.ast(); }
declarations()36   Declarations* declarations() { return global_context_.declarations(); }
37 
38   void DrainSpecializationQueue();
39 
40   class ScopedModuleActivator {
41    public:
ScopedModuleActivator(FileVisitor * visitor,Module * module)42     ScopedModuleActivator(FileVisitor* visitor, Module* module)
43         : visitor_(visitor), saved_module_(visitor->CurrentModule()) {
44       visitor->module_ = module;
45     }
~ScopedModuleActivator()46     ~ScopedModuleActivator() { visitor_->module_ = saved_module_; }
47 
48    private:
49     FileVisitor* visitor_;
50     Module* saved_module_;
51   };
52 
53  protected:
54   static constexpr const char* kReturnValueVariable = "_return";
55   static constexpr const char* kDoneLabelName = "_done";
56   static constexpr const char* kForIndexValueVariable = "_for_index";
57 
CurrentModule()58   Module* CurrentModule() const { return module_; }
59 
60   friend class ScopedModuleActivator;
61 
GetParameterVariableFromName(const std::string & name)62   std::string GetParameterVariableFromName(const std::string& name) {
63     return std::string("p_") + name;
64   }
65 
66   Signature MakeSignature(const CallableNodeSignature* signature);
67   Signature MakeSignatureFromReturnType(TypeExpression* return_type);
68 
69   struct PendingSpecialization {
70     SpecializationKey key;
71     CallableNode* callable;
72     const CallableNodeSignature* signature;
73     base::Optional<Statement*> body;
74     SourcePosition request_position;
75   };
76 
77   void QueueGenericSpecialization(const SpecializationKey& key,
78                                   CallableNode* callable,
79                                   const CallableNodeSignature* signature,
80                                   base::Optional<Statement*> body);
81 
82   void SpecializeGeneric(const PendingSpecialization& specialization);
83 
84   virtual void Specialize(const SpecializationKey&, CallableNode* callable,
85                           const CallableNodeSignature* signature,
86                           Statement* body) = 0;
87 
88   GlobalContext& global_context_;
89   Declarations* declarations_;
90   std::deque<PendingSpecialization> pending_specializations_;
91   std::set<SpecializationKey> completed_specializations_;
92   Callable* current_callable_;
93   Module* module_;
94 };
95 
96 }  // namespace torque
97 }  // namespace internal
98 }  // namespace v8
99 
100 #endif  // V8_TORQUE_FILE_VISITOR_H_
101