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