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 #include "src/torque/file-visitor.h"
6 
7 #include "src/torque/declarable.h"
8 #include "src/torque/parameter-difference.h"
9 
10 namespace v8 {
11 namespace internal {
12 namespace torque {
13 
MakeSignature(const CallableNodeSignature * signature)14 Signature FileVisitor::MakeSignature(const CallableNodeSignature* signature) {
15   LabelDeclarationVector definition_vector;
16   for (auto label : signature->labels) {
17     LabelDeclaration def = {label.name, GetTypeVector(label.types)};
18     definition_vector.push_back(def);
19   }
20   Signature result{signature->parameters.names,
21                    {GetTypeVector(signature->parameters.types),
22                     signature->parameters.has_varargs},
23                    declarations()->GetType(signature->return_type),
24                    definition_vector};
25   return result;
26 }
27 
MakeSignatureFromReturnType(TypeExpression * return_type)28 Signature FileVisitor::MakeSignatureFromReturnType(
29     TypeExpression* return_type) {
30   Signature result{{}, {{}, false}, declarations()->GetType(return_type), {}};
31   return result;
32 }
33 
QueueGenericSpecialization(const SpecializationKey & key,CallableNode * callable,const CallableNodeSignature * signature,base::Optional<Statement * > body)34 void FileVisitor::QueueGenericSpecialization(
35     const SpecializationKey& key, CallableNode* callable,
36     const CallableNodeSignature* signature, base::Optional<Statement*> body) {
37   pending_specializations_.push_back(
38       {key, callable, signature, body, CurrentSourcePosition::Get()});
39 }
40 
SpecializeGeneric(const PendingSpecialization & specialization)41 void FileVisitor::SpecializeGeneric(
42     const PendingSpecialization& specialization) {
43   CurrentSourcePosition::Scope scope(specialization.request_position);
44   if (completed_specializations_.find(specialization.key) !=
45       completed_specializations_.end()) {
46     std::stringstream stream;
47     stream << "cannot redeclare specialization of "
48            << specialization.key.first->name() << " with types <"
49            << specialization.key.second << ">";
50     ReportError(stream.str());
51   }
52   if (!specialization.body) {
53     std::stringstream stream;
54     stream << "missing specialization of " << specialization.key.first->name()
55            << " with types <" << specialization.key.second << ">";
56     ReportError(stream.str());
57   }
58   Declarations::ScopedGenericSpecializationKey instantiation(
59       declarations(), specialization.key);
60   FileVisitor::ScopedModuleActivator activator(
61       this, specialization.key.first->module());
62   Specialize(specialization.key, specialization.callable,
63              specialization.signature, *specialization.body);
64   completed_specializations_.insert(specialization.key);
65 }
66 
DrainSpecializationQueue()67 void FileVisitor::DrainSpecializationQueue() {
68   while (pending_specializations_.size() != 0) {
69     PendingSpecialization specialization(pending_specializations_.front());
70     pending_specializations_.pop_front();
71     if (completed_specializations_.find(specialization.key) ==
72         completed_specializations_.end()) {
73       Declarations::ScopedGenericScopeChainSnapshot scope(declarations(),
74                                                           specialization.key);
75       SpecializeGeneric(specialization);
76     }
77   }
78 }
79 
80 }  // namespace torque
81 }  // namespace internal
82 }  // namespace v8
83