void synthesizeConstructors(Program& program)
{
    FindAllTypes findAllTypes;
    findAllTypes.checkErrorAndVisit(program);
    auto m_unnamedTypes = findAllTypes.takeUnnamedTypes();
    auto m_namedTypes = findAllTypes.takeNamedTypes();

    bool isOperator = true;

    for (auto& unnamedType : m_unnamedTypes) {
        AST::VariableDeclaration variableDeclaration(Lexer::Token(unnamedType.get().origin()), AST::Qualifiers(), { unnamedType.get().clone() }, String(), WTF::nullopt, WTF::nullopt);
        AST::VariableDeclarations parameters;
        parameters.append(WTFMove(variableDeclaration));
        AST::NativeFunctionDeclaration copyConstructor(AST::FunctionDeclaration(Lexer::Token(unnamedType.get().origin()), AST::AttributeBlock(), WTF::nullopt, unnamedType.get().clone(), "operator cast"_str, WTFMove(parameters), WTF::nullopt, isOperator));
        program.append(WTFMove(copyConstructor));

        AST::NativeFunctionDeclaration defaultConstructor(AST::FunctionDeclaration(Lexer::Token(unnamedType.get().origin()), AST::AttributeBlock(), WTF::nullopt, unnamedType.get().clone(), "operator cast"_str, AST::VariableDeclarations(), WTF::nullopt, isOperator));
        program.append(WTFMove(defaultConstructor));
    }

    for (auto& namedType : m_namedTypes) {
        AST::VariableDeclaration variableDeclaration(Lexer::Token(namedType.get().origin()), AST::Qualifiers(), { AST::TypeReference::wrap(Lexer::Token(namedType.get().origin()), namedType.get()) }, String(), WTF::nullopt, WTF::nullopt);
        AST::VariableDeclarations parameters;
        parameters.append(WTFMove(variableDeclaration));
        AST::NativeFunctionDeclaration copyConstructor(AST::FunctionDeclaration(Lexer::Token(namedType.get().origin()), AST::AttributeBlock(), WTF::nullopt, AST::TypeReference::wrap(Lexer::Token(namedType.get().origin()), namedType.get()), "operator cast"_str, WTFMove(parameters), WTF::nullopt, isOperator));
        program.append(WTFMove(copyConstructor));

        AST::NativeFunctionDeclaration defaultConstructor(AST::FunctionDeclaration(Lexer::Token(namedType.get().origin()), AST::AttributeBlock(), WTF::nullopt, AST::TypeReference::wrap(Lexer::Token(namedType.get().origin()), namedType.get()), "operator cast"_str, AST::VariableDeclarations(), WTF::nullopt, isOperator));
        program.append(WTFMove(defaultConstructor));
    }
}
Beispiel #2
0
Model::AnnotationItem *annotation(
    const QString& name, Model::Node *item, bool isSequence,
    Model::VariableDeclarationItem::StorageType storageType)
{
  Model::AnnotationItem *node = createNode<Model::AnnotationItem>();
  node->mItem = item;

  Model::VariableDeclarationItem::VariableType variableType;
  QString type;
  
  if (Model::TerminalItem *t = nodeCast<Model::TerminalItem*>(item))
    {
      variableType = Model::VariableDeclarationItem::TypeToken;
      type = t->mName;
    }
  else if (Model::NonTerminalItem *nt = nodeCast<Model::NonTerminalItem*>(item))
    {
      variableType = Model::VariableDeclarationItem::TypeNode;
      type = nt->mSymbol->mName;      
    }
  else
    {
      variableType = Model::VariableDeclarationItem::TypeVariable; // has to be set to something, or compiler gives a warning
      qFatal("Item must either be a terminal or a non-terminal");
      Q_ASSERT(0); // ### item must either be a terminal or a nonTerminal
    }
      
  node->mDeclaration =
     variableDeclaration(Model::VariableDeclarationItem::DeclarationLocal,
                              storageType, variableType, isSequence, name, type);
  return node;
}
void synthesizeArrayOperatorLength(Program& program)
{
    FindArrayTypes findArrayTypes;
    findArrayTypes.checkErrorAndVisit(program);
    auto arrayTypes = findArrayTypes.takeArrayTypes();

    bool isOperator = true;

    for (auto& arrayType : arrayTypes) {
        AST::VariableDeclaration variableDeclaration(Lexer::Token(arrayType.get().origin()), AST::Qualifiers(), { arrayType.get().clone() }, String(), WTF::nullopt, WTF::nullopt);
        AST::VariableDeclarations parameters;
        parameters.append(WTFMove(variableDeclaration));
        AST::NativeFunctionDeclaration nativeFunctionDeclaration(AST::FunctionDeclaration(Lexer::Token(arrayType.get().origin()), AST::AttributeBlock(), WTF::nullopt, AST::TypeReference::wrap(Lexer::Token(arrayType.get().origin()), program.intrinsics().uintType()), "operator.length"_str, WTFMove(parameters), WTF::nullopt, isOperator));
        program.append(WTFMove(nativeFunctionDeclaration));
    }
}
Beispiel #4
0
  void insideBlock(BlockNode* node) {
    variableDeclaration(node->scope());
    functionDeclaration(node->scope());

    for (uint32_t i = 0; i < node->nodes(); i++) {
      indent();
      AstNode* current = node->nodeAt(i);
      current->visit(this);
      if (current->isCallNode() || current->isBinaryOpNode() ||
          current->isUnaryOpNode() || current->isStringLiteralNode() ||
          current->isDoubleLiteralNode() || current->isIntLiteralNode() ||
          current->isLoadNode())
        _output << ";";
      _output << endl;
    }
  }