declaration-visitor.cc 6.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
// Copyright 2017 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 "src/torque/declaration-visitor.h"

namespace v8 {
namespace internal {
namespace torque {

void DeclarationVisitor::Visit(Expression* expr) {
  switch (expr->kind) {
#define ENUM_ITEM(name)        \
  case AstNode::Kind::k##name: \
    return Visit(name::cast(expr));
    AST_EXPRESSION_NODE_KIND_LIST(ENUM_ITEM)
#undef ENUM_ITEM
    default:
      UNIMPLEMENTED();
  }
}

void DeclarationVisitor::Visit(Statement* stmt) {
  switch (stmt->kind) {
#define ENUM_ITEM(name)        \
  case AstNode::Kind::k##name: \
    return Visit(name::cast(stmt));
    AST_STATEMENT_NODE_KIND_LIST(ENUM_ITEM)
#undef ENUM_ITEM
    default:
      UNIMPLEMENTED();
  }
}

void DeclarationVisitor::Visit(Declaration* decl) {
  switch (decl->kind) {
#define ENUM_ITEM(name)        \
  case AstNode::Kind::k##name: \
    return Visit(name::cast(decl));
    AST_DECLARATION_NODE_KIND_LIST(ENUM_ITEM)
#undef ENUM_ITEM
    default:
      UNIMPLEMENTED();
  }
}

void DeclarationVisitor::Visit(BuiltinDeclaration* decl) {
  if (global_context_.verbose()) {
    std::cout << "found declaration of builtin " << decl->name;
  }
  Scope* enclosing_scope = TopScope();

  Signature signature = MakeSignature(decl->parameters, decl->return_type, {});

  Scope* new_scope = new Scope(global_context_);
  Scope::Activator s(new_scope);
  if (signature.types().size() == 0 ||
      !signature.types()[0].Is(CONTEXT_TYPE_STRING)) {
    std::stringstream stream;
    stream << "first parameter to builtin " << decl->name
           << " is not a context but should be at "
           << PositionAsString(decl->pos);
    ReportError(stream.str());
  }
  if (global_context_.verbose()) {
    std::cout << decl->name << " with signature " << signature << std::endl;
  }

69 70 71 72 73 74 75 76
  const bool javascript = decl->javascript_linkage;
  const bool varargs = decl->parameters.has_varargs;
  if (varargs && !javascript) {
    std::stringstream stream;
    stream << "builtin " << decl->name
           << " with rest parameters must be a JavaScript builtin at "
           << PositionAsString(decl->pos);
    ReportError(stream.str());
77
  }
78
  if (javascript) {
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    if (signature.types().size() < 2 ||
        !signature.types()[1].Is(OBJECT_TYPE_STRING)) {
      std::stringstream stream;
      stream << "second parameter to javascript builtin " << decl->name
             << " is not a receiver type but should be at "
             << PositionAsString(decl->pos);
      ReportError(stream.str());
    }
  }

  if (varargs) {
    TopScope()->DeclareConstant(decl->pos, decl->parameters.arguments_variable,
                                GetTypeOracle().GetArgumentsType(),
                                "arguments");
  }

95 96 97
  Builtin::Kind kind = !javascript ? Builtin::kStub
                                   : varargs ? Builtin::kVarArgsJavaScript
                                             : Builtin::kFixedArgsJavaScript;
98
  Builtin* builtin = enclosing_scope->DeclareBuiltin(
99
      decl->pos, decl->name, kind, new_scope, signature);
100
  defined_builtins_.push_back(builtin);
101
  DeclareParameterList(decl->pos, signature, {});
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
  CurrentCallActivator activator(global_context_, builtin);
  Visit(decl->body);
}

void DeclarationVisitor::Visit(MacroDeclaration* decl) {
  if (global_context_.verbose()) {
    std::cout << "found declaration of macro " << decl->name;
  }
  Scope* enclosing_scope = TopScope();
  Scope* new_scope = new Scope(global_context_);
  Scope::Activator s(new_scope);

  PushControlSplit();

  Signature signature =
      MakeSignature(decl->parameters, decl->return_type, decl->labels);

  if (!signature.return_type.IsVoidOrNever()) {
    TopScope()->DeclareVariable(decl->pos, kReturnValueVariable,
                                signature.return_type);
  }

  if (global_context_.verbose()) {
    std::cout << " resulting in signature " << signature << "\n";
  }

  Macro* macro = enclosing_scope->DeclareMacro(decl->pos, decl->name, new_scope,
                                               signature);
130
  DeclareParameterList(decl->pos, signature, decl->labels);
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
  CurrentCallActivator activator(global_context_, macro);
  Visit(decl->body);

  auto changed_vars = PopControlSplit();
  global_context_.AddControlSplitChangedVariables(decl, changed_vars);
}

void DeclarationVisitor::Visit(ReturnStatement* stmt) {
  const Callable* callable = global_context_.GetCurrentCallable();
  if (callable->IsMacro() && callable->HasReturnValue()) {
    MarkVariableModified(
        Variable::cast(LookupValue(stmt->pos, kReturnValueVariable)));
  }
}

void DeclarationVisitor::Visit(ForOfLoopStatement* stmt) {
  // Scope for for iteration variable
  Scope::Activator s(global_context_, stmt);
  Visit(stmt->var_declaration);
  Visit(stmt->iterable);
  if (stmt->begin) Visit(*stmt->begin);
  if (stmt->end) Visit(*stmt->end);
  PushControlSplit();
  Visit(stmt->body);
  auto changed_vars = PopControlSplit();
  global_context_.AddControlSplitChangedVariables(stmt, changed_vars);
}

void DeclarationVisitor::Visit(TryCatchStatement* stmt) {
  // Activate a new scope to declare catch handler labels, they should not be
  // visible outside the catch.
  {
    Scope::Activator s(global_context_, stmt);

    // Declare catch labels
    for (LabelBlock* block : stmt->label_blocks) {
167
      Label* shared_label = TopScope()->DeclareLabel(stmt->pos, block->label);
168 169 170 171 172 173 174 175 176 177 178
      {
        Scope::Activator s(global_context_, block->body);
        if (block->parameters.has_varargs) {
          std::stringstream stream;
          stream << "cannot use ... for label parameters  at "
                 << PositionAsString(stmt->pos);
          ReportError(stream.str());
        }

        size_t i = 0;
        for (auto p : block->parameters.names) {
179
          shared_label->AddVariable(TopScope()->DeclareVariable(
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
              stmt->pos, p,
              GetTypeOracle().GetType(block->parameters.types[i])));
          ++i;
        }
      }
      if (global_context_.verbose()) {
        std::cout << " declaring catch for exception " << block->label << "\n";
      }
    }

    // Try catch not supported yet
    DCHECK_EQ(stmt->catch_blocks.size(), 0);

    Visit(stmt->try_block);
  }

  for (CatchBlock* block : stmt->catch_blocks) {
    Visit(block->body);
  }
199 200 201 202

  for (LabelBlock* block : stmt->label_blocks) {
    Visit(block->body);
  }
203 204 205 206 207
}

}  // namespace torque
}  // namespace internal
}  // namespace v8