print_char('\t');
}
-static void print_stringrep(const string_t *string)
-{
- for (size_t i = 0; i < string->size; ++i) {
- print_char(string->begin[i]);
- }
-}
-
/**
* Returns 1 if a given precedence level has right-to-left
* associativity, else 0.
*/
static void print_quoted_string(const string_t *const string, char border)
{
+ print_string(get_string_encoding_prefix(string->encoding));
+
print_char(border);
const char *end = string->begin + string->size;
for (const char *c = string->begin; c != end; ++c) {
static void print_string_literal(string_literal_expression_t const *const literal, char const delimiter)
{
- print_string(get_string_encoding_prefix(literal->encoding));
print_quoted_string(&literal->value, delimiter);
}
case EXPR_LITERAL_BOOLEAN:
case EXPR_LITERAL_FLOATINGPOINT:
case EXPR_LITERAL_INTEGER:
- print_stringrep(&literal->value);
- print_stringrep(&literal->suffix);
+ print_string(literal->value.begin);
return;
default:
*/
static void print_typeprop_expression(const typeprop_expression_t *expression)
{
- if (expression->base.kind == EXPR_SIZEOF) {
- print_string("sizeof");
- } else {
- assert(expression->base.kind == EXPR_ALIGNOF);
- print_string("__alignof__");
+ switch (expression->base.kind) {
+ case EXPR_SIZEOF: print_string("sizeof"); break;
+ case EXPR_ALIGNOF: print_string(c_mode & _C11 ? "_Alignof" : "__alignof__"); break;
+ default: panic("invalid typeprop kind");
}
if (expression->tp_expression != NULL) {
/* PREC_TOP: always print the '()' here, sizeof x is right but unusual */
}
}
-/**
- * Print a while statement.
- *
- * @param statement the statement
- */
-static void print_while_statement(const while_statement_t *statement)
-{
- print_string("while (");
- print_expression(statement->condition);
- print_char(')');
- print_inner_statement(statement->body);
-}
-
/**
* Print a do-while statement.
*
*/
static void print_for_statement(const for_statement_t *statement)
{
- print_string("for (");
- if (statement->initialisation != NULL) {
- print_expression(statement->initialisation);
+ if (statement->initialisation || statement->scope.entities || !statement->condition || statement->step) {
+ print_string("for (");
+ if (statement->initialisation != NULL) {
+ print_expression(statement->initialisation);
+ print_char(';');
+ } else {
+ entity_t const *entity = statement->scope.entities;
+ for (; entity != NULL; entity = entity->base.next) {
+ if (is_generated_entity(entity))
+ continue;
+ /* FIXME display of multiple declarations is wrong */
+ print_declaration(entity);
+ }
+ }
+ if (statement->condition != NULL) {
+ print_char(' ');
+ print_expression(statement->condition);
+ }
print_char(';');
- } else {
- entity_t const *entity = statement->scope.entities;
- for (; entity != NULL; entity = entity->base.next) {
- if (is_generated_entity(entity))
- continue;
- /* FIXME display of multiple declarations is wrong */
- print_declaration(entity);
+ if (statement->step != NULL) {
+ print_char(' ');
+ print_expression(statement->step);
}
- }
- if (statement->condition != NULL) {
- print_char(' ');
+ } else {
+ print_string("while (");
print_expression(statement->condition);
}
- print_char(';');
- if (statement->step != NULL) {
- print_char(' ');
- print_expression(statement->step);
- }
print_char(')');
print_inner_statement(statement->body);
}
case STATEMENT_MS_TRY: print_ms_try_statement( &stmt->ms_try); break;
case STATEMENT_RETURN: print_return_statement( &stmt->returns); break;
case STATEMENT_SWITCH: print_switch_statement( &stmt->switchs); break;
- case STATEMENT_WHILE: print_while_statement( &stmt->whiles); break;
}
}
}
switch (initializer->kind) {
- case INITIALIZER_VALUE: {
- const initializer_value_t *value = &initializer->value;
- print_assignment_expression(value->value);
+ case INITIALIZER_STRING:
+ case INITIALIZER_VALUE:
+ print_assignment_expression(initializer->value.value);
return;
- }
+
case INITIALIZER_LIST: {
- assert(initializer->kind == INITIALIZER_LIST);
print_string("{ ");
const initializer_list_t *list = &initializer->list;
print_string(" }");
return;
}
- case INITIALIZER_STRING:
- print_quoted_string(&initializer->string.string, '"');
- return;
- case INITIALIZER_WIDE_STRING:
- print_quoted_string(&initializer->string.string, '"');
- return;
+
case INITIALIZER_DESIGNATOR:
print_designator(initializer->designator.designator);
print_string(" = ");
print_type_ext(entity->declaration.type, entity->base.symbol,
&entity->function.parameters);
- if (entity->function.statement != NULL) {
+ if (entity->function.body != NULL) {
print_char('\n');
- print_indented_statement(entity->function.statement);
+ print_indented_statement(entity->function.body);
print_char('\n');
return;
}
case ENTITY_VARIABLE:
if (entity->variable.thread_local)
- print_string("__thread ");
+ print_string(c_mode & _C11 ? "_Thread_local " : "__thread ");
print_type_ext(declaration->type, declaration->base.symbol, NULL);
if (entity->variable.initializer != NULL) {
print_string(" = ");
{
switch (initializer->kind) {
case INITIALIZER_STRING:
- case INITIALIZER_WIDE_STRING:
case INITIALIZER_DESIGNATOR:
return EXPR_CLASS_CONSTANT;
expression_classification_t is_constant_expression(const expression_t *expression)
{
switch (expression->kind) {
- case EXPR_LITERAL_CASES:
case EXPR_LITERAL_CHARACTER:
- case EXPR_CLASSIFY_TYPE:
- case EXPR_OFFSETOF:
- case EXPR_ALIGNOF:
- case EXPR_BUILTIN_CONSTANT_P:
case EXPR_BUILTIN_TYPES_COMPATIBLE_P:
case EXPR_ENUM_CONSTANT:
+ case EXPR_LITERAL_BOOLEAN:
+ case EXPR_LITERAL_MS_NOOP:
return EXPR_CLASS_CONSTANT;
- case EXPR_SIZEOF: {
- type_t *const type = skip_typeref(expression->typeprop.type);
- return
- !is_type_array(type) || !type->array.is_vla ? EXPR_CLASS_CONSTANT :
- EXPR_CLASS_VARIABLE;
+ {
+ type_t *type;
+ case EXPR_ALIGNOF:
+ type = skip_typeref(expression->typeprop.type);
+ goto check_type;
+
+ case EXPR_CLASSIFY_TYPE:
+ type = skip_typeref(expression->classify_type.type_expression->base.type);
+ goto check_type;
+
+ case EXPR_LITERAL_INTEGER:
+ case EXPR_LITERAL_FLOATINGPOINT:
+ type = skip_typeref(expression->base.type);
+ goto check_type;
+
+ case EXPR_OFFSETOF:
+ type = skip_typeref(expression->offsetofe.type);
+ goto check_type;
+
+ case EXPR_SIZEOF:
+ type = skip_typeref(expression->typeprop.type);
+ if (is_type_array(type) && type->array.is_vla)
+ return EXPR_CLASS_VARIABLE;
+ goto check_type;
+
+check_type:
+ return is_type_valid(type) ? EXPR_CLASS_CONSTANT : EXPR_CLASS_ERROR;
+ }
+
+ case EXPR_BUILTIN_CONSTANT_P: {
+ expression_classification_t const c = is_constant_expression(expression->builtin_constant.value);
+ return c != EXPR_CLASS_ERROR ? EXPR_CLASS_CONSTANT : EXPR_CLASS_ERROR;
}
case EXPR_STRING_LITERAL: