Line data Source code
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 :
5 : #include <fstream>
6 : #include <iostream>
7 :
8 : #include "src/torque/declarable.h"
9 : #include "src/torque/global-context.h"
10 :
11 : namespace v8 {
12 : namespace internal {
13 : namespace torque {
14 :
15 106089 : DEFINE_CONTEXTUAL_VARIABLE(CurrentScope)
16 :
17 0 : std::ostream& operator<<(std::ostream& os, const QualifiedName& name) {
18 0 : for (const std::string& qualifier : name.namespace_qualification) {
19 0 : os << qualifier << "::";
20 : }
21 0 : return os << name.name;
22 : }
23 :
24 0 : std::ostream& operator<<(std::ostream& os, const Callable& m) {
25 0 : os << "callable " << m.ReadableName() << "(";
26 0 : if (m.signature().implicit_count != 0) {
27 0 : os << "implicit ";
28 : TypeVector implicit_parameter_types(
29 : m.signature().parameter_types.types.begin(),
30 0 : m.signature().parameter_types.types.begin() +
31 0 : m.signature().implicit_count);
32 0 : os << implicit_parameter_types << ")(";
33 : TypeVector explicit_parameter_types(
34 0 : m.signature().parameter_types.types.begin() +
35 : m.signature().implicit_count,
36 0 : m.signature().parameter_types.types.end());
37 0 : os << explicit_parameter_types;
38 : } else {
39 0 : os << m.signature().parameter_types;
40 : }
41 0 : os << "): " << *m.signature().return_type;
42 0 : return os;
43 : }
44 :
45 0 : std::ostream& operator<<(std::ostream& os, const Builtin& b) {
46 0 : os << "builtin " << *b.signature().return_type << " " << b.ReadableName()
47 0 : << b.signature().parameter_types;
48 0 : return os;
49 : }
50 :
51 0 : std::ostream& operator<<(std::ostream& os, const RuntimeFunction& b) {
52 0 : os << "runtime function " << *b.signature().return_type << " "
53 0 : << b.ReadableName() << b.signature().parameter_types;
54 0 : return os;
55 : }
56 :
57 0 : std::ostream& operator<<(std::ostream& os, const Generic& g) {
58 0 : os << "generic " << g.name() << "<";
59 0 : PrintCommaSeparatedList(os, g.declaration()->generic_parameters);
60 0 : os << ">";
61 :
62 0 : return os;
63 : }
64 :
65 436 : base::Optional<const Type*> Generic::InferTypeArgument(
66 1372 : size_t i, const TypeVector& arguments) {
67 872 : const std::string type_name = declaration()->generic_parameters[i];
68 1000 : const std::vector<TypeExpression*>& parameters =
69 436 : declaration()->callable->signature->parameters.types;
70 436 : size_t j = declaration()->callable->signature->parameters.implicit_count;
71 1500 : for (size_t i = 0; i < arguments.size() && j < parameters.size(); ++i, ++j) {
72 : BasicTypeExpression* basic =
73 500 : BasicTypeExpression::DynamicCast(parameters[j]);
74 1500 : if (basic && basic->namespace_qualification.empty() &&
75 1500 : !basic->is_constexpr && basic->name == type_name) {
76 : return arguments[i];
77 : }
78 : }
79 : return base::nullopt;
80 : }
81 :
82 4320 : base::Optional<TypeVector> Generic::InferSpecializationTypes(
83 4320 : const TypeVector& explicit_specialization_types,
84 4320 : const TypeVector& arguments) {
85 4320 : TypeVector result = explicit_specialization_types;
86 4320 : size_t type_parameter_count = declaration()->generic_parameters.size();
87 4320 : if (explicit_specialization_types.size() > type_parameter_count) {
88 : return base::nullopt;
89 : }
90 436 : for (size_t i = explicit_specialization_types.size();
91 : i < type_parameter_count; ++i) {
92 436 : base::Optional<const Type*> inferred = InferTypeArgument(i, arguments);
93 436 : if (!inferred) return base::nullopt;
94 436 : result.push_back(*inferred);
95 : }
96 : return result;
97 : }
98 :
99 41045 : bool Namespace::IsDefaultNamespace() const {
100 41045 : return this == GlobalContext::GetDefaultNamespace();
101 : }
102 :
103 34 : bool Namespace::IsTestNamespace() const { return name() == kTestNamespaceName; }
104 :
105 : } // namespace torque
106 : } // namespace internal
107 9114 : } // namespace v8
|