void MemberAccessOperator::doPrint(PrintContext &context) const {
    bool braces = compound()->is<UnaryOperator>() ||
                  compound()->is<BinaryOperator>() ||
                  compound()->is<Typecast>();

    if (braces) {
        context.out() << "(";
    }
    compound_->print(context);
    if (braces) {
        context.out() << ")";
    }

    switch (accessKind()) {
        case ARROW:
            context.out() << "->";
            break;
        case DOT:
            context.out() << '.';
            break;
        default:
            unreachable();
            break;
    }
    
    context.out() << member_->identifier();
}
Beispiel #2
0
void IntegerConstant::doPrint(PrintContext &context) const {
    SignedConstantValue val = value().size() > 1 ? value().signedValue() : value().value();

    if ((0 <= val && val <= 100) || (-100 <= val && val < 0 && !type()->isUnsigned())) {
        context.out() << val;
    } else {
        context.out() << hex << "0x" << value().value() << dec;
    }
}
Beispiel #3
0
void Typecast::doPrint(PrintContext &context) const {
    context.out() << '(' << *type() << ')';
    bool braces = operand()->is<BinaryOperator>();
    if (braces) {
        context.out() << '(';
    }
    operand()->print(context);
    if (braces) {
        context.out() << ')';
    }
}
Beispiel #4
0
void UnaryOperator::doPrint(PrintContext &context) const {
    switch (operatorKind()) {
        case DEREFERENCE:
            context.out() << '*';
            break;
        case REFERENCE:
            context.out() << '&';
            break;
        case BITWISE_NOT:
            context.out() << '~';
            break;
        case LOGICAL_NOT:
            context.out() << '!';
            break;
        case NEGATION:
            context.out() << '-';
            break;
        case PREFIX_INCREMENT:
            context.out() << "++";
            break;
        case PREFIX_DECREMENT:
            context.out() << "--";
            break;
        default:
            unreachable();
            break;
    }

    int precedence = this->precedence();
    int operandPrecedence = operand()->precedence();

    int absPrecedence = abs(precedence);
    int absOperandPrecedence = abs(operandPrecedence);

    bool operandInBraces = absOperandPrecedence > absPrecedence;

    /* Avoid too many minuses in a row. */
    if (operatorKind() == NEGATION || operatorKind() == PREFIX_DECREMENT) {
        if (auto unary = operand()->as<UnaryOperator>()) {
            if (unary->operatorKind() == NEGATION || unary->operatorKind() == PREFIX_DECREMENT) {
                operandInBraces = true;
            }
        }
    }

    if (operandInBraces) {
        context.out() << '(';
    }
    operand()->print(context);
    if (operandInBraces) {
        context.out() << ')';
    }
}
void FunctionDeclaration::printSignature(PrintContext &context) const {
    context.out() << *type()->returnType() << ' ';
    functionIdentifier()->print(context);
    context.out() << '(';

    bool comma = false;
    foreach (const auto &argument, arguments_) {
        if (comma) {
            context.out() << ", ";
        } else {
            comma = true;
        }
        argument->print(context);
    }

    if (type()->variadic()) {
        if (comma) {
            context.out() << ", ";
        }
        context.out() << "...";
    }
    
    context.out() << ')';
}
Beispiel #6
0
void While::doPrint(PrintContext &context) const {
    context.out() << "while (";
    condition()->print(context);
    context.out() << ") ";
    printNestedStatement(body(), context);
}
Beispiel #7
0
void Switch::doPrint(PrintContext &context) const {
    context.out() << "switch (";
    expression()->print(context);
    context.out() << ") ";
    printNestedStatement(body(), context);
}
Beispiel #8
0
void InlineAssembly::doPrint(PrintContext &context) const {
    context.out() << "__asm__(\"" << code() << "\")";
}
Beispiel #9
0
void ExpressionStatement::doPrint(PrintContext &context) const {
    expression_->print(context);
    context.out() << ';';
}
void FunctionDeclaration::doPrint(PrintContext &context) const {
    printComment(context);
    printSignature(context);
    context.out() << ';';
}
Beispiel #11
0
void LabelIdentifier::doPrint(PrintContext &context) const {
    context.out() << declaration_->identifier();
}
Beispiel #12
0
void Goto::doPrint(PrintContext &context) const {
    context.out() << "goto ";
    destination_->print(context);
    context.out() << ';';
}