declarable.cc 3.72 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <fstream>
#include <iostream>

#include "src/torque/declarable.h"
9
#include "src/torque/global-context.h"
10
#include "src/torque/type-inference.h"
11
#include "src/torque/type-visitor.h"
12 13 14 15 16

namespace v8 {
namespace internal {
namespace torque {

17
DEFINE_CONTEXTUAL_VARIABLE(CurrentScope)
18

19 20
std::ostream& operator<<(std::ostream& os, const QualifiedName& name) {
  for (const std::string& qualifier : name.namespace_qualification) {
21
    os << qualifier << "::";
22 23 24 25
  }
  return os << name.name;
}

26
std::ostream& operator<<(std::ostream& os, const Callable& m) {
27
  os << "callable " << m.ReadableName() << "(";
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
  if (m.signature().implicit_count != 0) {
    os << "implicit ";
    TypeVector implicit_parameter_types(
        m.signature().parameter_types.types.begin(),
        m.signature().parameter_types.types.begin() +
            m.signature().implicit_count);
    os << implicit_parameter_types << ")(";
    TypeVector explicit_parameter_types(
        m.signature().parameter_types.types.begin() +
            m.signature().implicit_count,
        m.signature().parameter_types.types.end());
    os << explicit_parameter_types;
  } else {
    os << m.signature().parameter_types;
  }
  os << "): " << *m.signature().return_type;
44
  return os;
45 46
}

47
std::ostream& operator<<(std::ostream& os, const Builtin& b) {
48
  os << "builtin " << *b.signature().return_type << " " << b.ReadableName()
49 50
     << b.signature().parameter_types;
  return os;
51 52
}

53
std::ostream& operator<<(std::ostream& os, const RuntimeFunction& b) {
54 55
  os << "runtime function " << *b.signature().return_type << " "
     << b.ReadableName() << b.signature().parameter_types;
56
  return os;
57 58
}

59 60
std::ostream& operator<<(std::ostream& os, const Generic& g) {
  os << "generic " << g.name() << "<";
61
  PrintCommaSeparatedList(
62
      os, g.generic_parameters(),
63
      [](const Identifier* identifier) { return identifier->value; });
64 65 66 67 68
  os << ">";

  return os;
}

69
TypeArgumentInference Generic::InferSpecializationTypes(
70 71
    const TypeVector& explicit_specialization_types,
    const TypeVector& arguments) {
72
  size_t implicit_count = declaration()->parameters.implicit_count;
73
  const std::vector<TypeExpression*>& parameters =
74
      declaration()->parameters.types;
75 76 77
  std::vector<TypeExpression*> explicit_parameters(
      parameters.begin() + implicit_count, parameters.end());

78
  CurrentScope::Scope generic_scope(ParentScope());
79
  TypeArgumentInference inference(generic_parameters(),
80 81 82
                                  explicit_specialization_types,
                                  explicit_parameters, arguments);
  return inference;
83 84
}

85 86 87 88 89 90 91 92 93 94 95
base::Optional<Statement*> Generic::CallableBody() {
  if (auto* decl = TorqueMacroDeclaration::DynamicCast(declaration())) {
    return decl->body;
  } else if (auto* decl =
                 TorqueBuiltinDeclaration::DynamicCast(declaration())) {
    return decl->body;
  } else {
    return base::nullopt;
  }
}

96 97 98 99 100 101
bool Namespace::IsDefaultNamespace() const {
  return this == GlobalContext::GetDefaultNamespace();
}

bool Namespace::IsTestNamespace() const { return name() == kTestNamespaceName; }

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
const Type* TypeAlias::Resolve() const {
  if (!type_) {
    CurrentScope::Scope scope_activator(ParentScope());
    CurrentSourcePosition::Scope position_activator(Position());
    TypeDeclaration* decl = *delayed_;
    if (being_resolved_) {
      std::stringstream s;
      s << "Cannot create type " << decl->name->value
        << " due to circular dependencies.";
      ReportError(s.str());
    }
    type_ = TypeVisitor::ComputeType(decl);
  }
  return *type_;
}

118 119 120
}  // namespace torque
}  // namespace internal
}  // namespace v8