v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
declarable.cc
Go to the documentation of this file.
1// Copyright 2018 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
6
7#include <fstream>
8#include <iostream>
9#include <optional>
10
11#include "src/torque/ast.h"
15
16namespace v8::internal::torque {
17
18QualifiedName QualifiedName::Parse(std::string qualified_name) {
19 std::vector<std::string> qualifications;
20 while (true) {
21 size_t namespace_delimiter_index = qualified_name.find("::");
22 if (namespace_delimiter_index == std::string::npos) break;
23 qualifications.push_back(
24 qualified_name.substr(0, namespace_delimiter_index));
25 qualified_name = qualified_name.substr(namespace_delimiter_index + 2);
26 }
27 return QualifiedName(std::move(qualifications), qualified_name);
28}
29
30std::ostream& operator<<(std::ostream& os, const QualifiedName& name) {
31 for (const std::string& qualifier : name.namespace_qualification) {
32 os << qualifier << "::";
33 }
34 return os << name.name;
35}
36
37std::ostream& operator<<(std::ostream& os, const Callable& m) {
38 os << "callable " << m.ReadableName() << "(";
39 if (m.signature().implicit_count != 0) {
40 os << "implicit ";
41 TypeVector implicit_parameter_types(
42 m.signature().parameter_types.types.begin(),
43 m.signature().parameter_types.types.begin() +
44 m.signature().implicit_count);
45 os << implicit_parameter_types << ")(";
46 TypeVector explicit_parameter_types(
47 m.signature().parameter_types.types.begin() +
48 m.signature().implicit_count,
49 m.signature().parameter_types.types.end());
50 os << explicit_parameter_types;
51 } else {
52 os << m.signature().parameter_types;
53 }
54 os << "): " << *m.signature().return_type;
55 return os;
56}
57
58std::ostream& operator<<(std::ostream& os, const Builtin& b) {
59 os << "builtin " << *b.signature().return_type << " " << b.ReadableName()
60 << b.signature().parameter_types;
61 return os;
62}
63
64std::ostream& operator<<(std::ostream& os, const RuntimeFunction& b) {
65 os << "runtime function " << *b.signature().return_type << " "
66 << b.ReadableName() << b.signature().parameter_types;
67 return os;
68}
69
70std::ostream& operator<<(std::ostream& os, const GenericCallable& g) {
71 os << "generic " << g.name() << "<";
73 [](const GenericParameter& identifier) {
74 return identifier.name->value;
75 });
76 os << ">";
77
78 return os;
79}
80
82 Scope* s, std::string name)
83 : position(position), name(std::move(name)) {
84 // Skip scopes that are not related to template specializations, they might be
85 // stack-allocated and not live for long enough.
86 while (s && s->GetSpecializationRequester().IsNone()) s = s->ParentScope();
87 this->scope = s;
88}
89
90std::vector<Declarable*> Scope::Lookup(const QualifiedName& name) {
91 if (!name.namespace_qualification.empty() &&
92 name.namespace_qualification[0].empty()) {
94 name.DropFirstNamespaceQualification());
95 }
96 std::vector<Declarable*> result;
97 if (ParentScope()) {
98 result = ParentScope()->Lookup(name);
99 }
100 for (Declarable* declarable : LookupShallow(name)) {
101 result.push_back(declarable);
102 }
103 return result;
104}
105
106std::optional<std::string> TypeConstraint::IsViolated(const Type* type) const {
107 if (upper_bound && !type->IsSubtypeOf(*upper_bound)) {
108 if (type->IsTopType()) {
109 return TopType::cast(type)->reason();
110 } else {
111 return {
112 ToString("expected ", *type, " to be a subtype of ", **upper_bound)};
113 }
114 }
115 return std::nullopt;
116}
117
118std::optional<std::string> FindConstraintViolation(
119 const std::vector<const Type*>& types,
120 const std::vector<TypeConstraint>& constraints) {
121 DCHECK_EQ(constraints.size(), types.size());
122 for (size_t i = 0; i < types.size(); ++i) {
123 if (auto violation = constraints[i].IsViolated(types[i])) {
124 return {"Could not instantiate generic, " + *violation + "."};
125 }
126 }
127 return std::nullopt;
128}
129
130std::vector<TypeConstraint> ComputeConstraints(
131 Scope* scope, const GenericParameters& parameters) {
132 CurrentScope::Scope scope_scope(scope);
133 std::vector<TypeConstraint> result;
134 for (const GenericParameter& parameter : parameters) {
135 if (parameter.constraint) {
137 TypeVisitor::ComputeType(*parameter.constraint)));
138 } else {
140 }
141 }
142 return result;
143}
144
146 const TypeVector& explicit_specialization_types,
147 const std::vector<std::optional<const Type*>>& arguments) {
148 const std::vector<TypeExpression*>& parameters =
149 declaration()->parameters.types;
150 CurrentScope::Scope generic_scope(ParentScope());
152 explicit_specialization_types, parameters,
153 arguments);
154 if (!inference.HasFailed()) {
155 if (auto violation =
157 inference.Fail(*violation);
158 }
159 }
160 return inference;
161}
162
163std::optional<Statement*> GenericCallable::CallableBody() {
164 if (auto* macro_decl = TorqueMacroDeclaration::DynamicCast(declaration())) {
165 return macro_decl->body;
166 } else if (auto* builtin_decl =
167 TorqueBuiltinDeclaration::DynamicCast(declaration())) {
168 return builtin_decl->body;
169 } else {
170 return std::nullopt;
171 }
172}
173
177
179
180const Type* TypeAlias::Resolve() const {
181 if (!type_) {
182 CurrentScope::Scope scope_activator(ParentScope());
183 CurrentSourcePosition::Scope position_activator(Position());
184 TypeDeclaration* decl = *delayed_;
185 if (being_resolved_) {
186 std::stringstream s;
187 s << "Cannot create type " << decl->name->value
188 << " due to circular dependencies.";
189 ReportError(s.str());
190 }
192 }
193 return *type_;
194}
195
196} // namespace v8::internal::torque
const Signature & signature() const
Definition declarable.h:306
const std::string & ReadableName() const
Definition declarable.h:305
SourcePosition Position() const
Definition declarable.h:96
TypeArgumentInference InferSpecializationTypes(const TypeVector &explicit_specialization_types, const std::vector< std::optional< const Type * > > &arguments)
std::optional< Statement * > CallableBody()
const std::string & name() const
Definition declarable.h:609
const GenericParameters & generic_parameters() const
Definition declarable.h:611
static Namespace * GetDefaultNamespace()
const std::string & name() const
Definition declarable.h:222
std::vector< Declarable * > LookupShallow(const QualifiedName &name)
Definition declarable.h:177
std::vector< Declarable * > Lookup(const QualifiedName &name)
Definition declarable.cc:90
std::optional< TypeDeclaration * > delayed_
Definition declarable.h:703
const Type * Resolve() const
std::optional< const Type * > type_
Definition declarable.h:704
static TypeConstraint SubtypeConstraint(const Type *upper_bound)
Definition declarable.h:565
std::optional< const Type * > upper_bound
Definition declarable.h:572
static TypeConstraint Unconstrained()
Definition declarable.h:564
std::optional< std::string > IsViolated(const Type *) const
static const Type * ComputeType(TypeExpression *type_expression)
ZoneVector< RpoNumber > & result
int position
Definition liveedit.cc:290
int s
Definition mul-fft.cc:297
int m
Definition mul-fft.cc:294
STL namespace.
std::ostream & operator<<(std::ostream &os, Identifier *id)
Definition ast.h:263
void ReportError(Args &&... args)
Definition utils.h:96
std::string ToString(Args &&... args)
Definition utils.h:41
std::vector< GenericParameter > GenericParameters
Definition ast.h:1129
std::vector< const Type * > TypeVector
Definition types.h:85
std::optional< std::string > FindConstraintViolation(const std::vector< const Type * > &types, const std::vector< TypeConstraint > &constraints)
std::vector< TypeConstraint > ComputeConstraints(Scope *scope, const GenericParameters &parameters)
void PrintCommaSeparatedList(std::ostream &os, const T &list, C &&transform)
Definition utils.h:163
static const char *const kTestNamespaceName
Definition utils.h:322
#define DCHECK_EQ(v1, v2)
Definition logging.h:485
QualifiedName(std::vector< std::string > namespace_qualification, std::string name)
Definition declarable.h:31
static QualifiedName Parse(std::string qualified_name)
Definition declarable.cc:18
SpecializationRequester(SourcePosition position, Scope *scope, std::string name)
Definition declarable.cc:81
Symbol identifier