declarable.cc 6.02 KB
Newer Older
1 2 3 4
// 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.

5 6
#include "src/torque/declarable.h"

7 8 9
#include <fstream>
#include <iostream>

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

namespace v8 {
namespace internal {
namespace torque {

19
DEFINE_CONTEXTUAL_VARIABLE(CurrentScope)
20

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

28
std::ostream& operator<<(std::ostream& os, const Callable& m) {
29
  os << "callable " << m.ReadableName() << "(";
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
  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;
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 52
     << b.signature().parameter_types;
  return os;
53 54
}

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

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

  return os;
}
71 72 73 74 75 76 77 78 79 80

SpecializationRequester::SpecializationRequester(SourcePosition position,
                                                 Scope* scope, std::string name)
    : position(position), name(std::move(name)) {
  // Skip scopes that are not related to template specializations, they might be
  // stack-allocated and not live for long enough.
  while (scope && scope->GetSpecializationRequester().IsNone())
    scope = scope->ParentScope();
  this->scope = scope;
}
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

std::vector<Declarable*> Scope::Lookup(const QualifiedName& name) {
  if (name.namespace_qualification.size() >= 1 &&
      name.namespace_qualification[0] == "") {
    return GlobalContext::GetDefaultNamespace()->Lookup(
        name.DropFirstNamespaceQualification());
  }
  std::vector<Declarable*> result;
  if (ParentScope()) {
    result = ParentScope()->Lookup(name);
  }
  for (Declarable* declarable : LookupShallow(name)) {
    result.push_back(declarable);
  }
  return result;
}
97

98 99
base::Optional<std::string> TypeConstraint::IsViolated(const Type* type) const {
  if (upper_bound && !type->IsSubtypeOf(*upper_bound)) {
100 101 102 103 104 105
    if (type->IsTopType()) {
      return TopType::cast(type)->reason();
    } else {
      return {
          ToString("expected ", *type, " to be a subtype of ", **upper_bound)};
    }
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
  }
  return base::nullopt;
}

base::Optional<std::string> FindConstraintViolation(
    const std::vector<const Type*>& types,
    const std::vector<TypeConstraint>& constraints) {
  DCHECK_EQ(constraints.size(), types.size());
  for (size_t i = 0; i < types.size(); ++i) {
    if (auto violation = constraints[i].IsViolated(types[i])) {
      return {"Could not instantiate generic, " + *violation + "."};
    }
  }
  return base::nullopt;
}

std::vector<TypeConstraint> ComputeConstraints(
    Scope* scope, const GenericParameters& parameters) {
  CurrentScope::Scope scope_scope(scope);
  std::vector<TypeConstraint> result;
  for (const GenericParameter& parameter : parameters) {
    if (parameter.constraint) {
      result.push_back(TypeConstraint::SubtypeConstraint(
          TypeVisitor::ComputeType(*parameter.constraint)));
    } else {
      result.push_back(TypeConstraint::Unconstrained());
    }
  }
  return result;
}

137
TypeArgumentInference GenericCallable::InferSpecializationTypes(
138
    const TypeVector& explicit_specialization_types,
139
    const std::vector<base::Optional<const Type*>>& arguments) {
140
  const std::vector<TypeExpression*>& parameters =
141
      declaration()->parameters.types;
142
  CurrentScope::Scope generic_scope(ParentScope());
143
  TypeArgumentInference inference(generic_parameters(),
144 145
                                  explicit_specialization_types, parameters,
                                  arguments);
146 147 148 149 150 151
  if (!inference.HasFailed()) {
    if (auto violation =
            FindConstraintViolation(inference.GetResult(), Constraints())) {
      inference.Fail(*violation);
    }
  }
152
  return inference;
153 154
}

155
base::Optional<Statement*> GenericCallable::CallableBody() {
156 157 158 159 160 161 162 163 164 165
  if (auto* decl = TorqueMacroDeclaration::DynamicCast(declaration())) {
    return decl->body;
  } else if (auto* decl =
                 TorqueBuiltinDeclaration::DynamicCast(declaration())) {
    return decl->body;
  } else {
    return base::nullopt;
  }
}

166 167 168 169 170 171
bool Namespace::IsDefaultNamespace() const {
  return this == GlobalContext::GetDefaultNamespace();
}

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

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
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_;
}

188 189 190
}  // namespace torque
}  // namespace internal
}  // namespace v8