X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ast.c;h=7284a5b5788487571ae9c13782aa8df38154d6f5;hb=0a499089dd2383e73ae8364c8eae82c068ba980b;hp=d15ece769ce8bdbeb9f5873f92355b76a39baeb5;hpb=e98818d1ef6f4a0cc91e869adfa2180c4deeddf9;p=cparser diff --git a/ast.c b/ast.c index d15ece7..7284a5b 100644 --- a/ast.c +++ b/ast.c @@ -22,6 +22,8 @@ #include "ast_t.h" #include "symbol_t.h" #include "type_t.h" +#include "parser.h" +#include "lang_features.h" #include #include @@ -113,8 +115,7 @@ static unsigned get_expression_precedence(expression_kind_t kind) [EXPR_CLASSIFY_TYPE] = PREC_UNARY, [EXPR_ALIGNOF] = PREC_UNARY, - [EXPR_FUNCTION] = PREC_PRIM, - [EXPR_PRETTY_FUNCTION] = PREC_PRIM, + [EXPR_FUNCNAME] = PREC_PRIM, [EXPR_BUILTIN_SYMBOL] = PREC_PRIM, [EXPR_BUILTIN_CONSTANT_P] = PREC_PRIM, [EXPR_BUILTIN_PREFETCH] = PREC_PRIM, @@ -317,6 +318,24 @@ static void print_string_literal( print_quoted_string(&string_literal->value, '"'); } +/** + * Prints a predefined symbol. + */ +static void print_funcname( + const funcname_expression_t *funcname) +{ + const char *s = ""; + switch(funcname->kind) { + case FUNCNAME_FUNCTION: s = (c_mode & _C99) ? "__func__" : "__FUNCTION__"; break; + case FUNCNAME_PRETTY_FUNCTION: s = "__PRETTY_FUNCTION__"; break; + case FUNCNAME_FUNCSIG: s = "__FUNCSIG__"; break; + case FUNCNAME_FUNCDNAME: s = "__FUNCDNAME__"; break; + } + fputc('"', out); + fputs(s, out); + fputc('"', out); +} + static void print_wide_string_literal( const wide_string_literal_expression_t *const wstr) { @@ -716,8 +735,9 @@ static void print_expression_prec(const expression_t *expression, unsigned top_p case EXPR_CONST: print_const(&expression->conste); break; - case EXPR_FUNCTION: - case EXPR_PRETTY_FUNCTION: + case EXPR_FUNCNAME: + print_funcname(&expression->funcname); + break; case EXPR_STRING_LITERAL: print_string_literal(&expression->string); break; @@ -869,9 +889,7 @@ static void print_if_statement(const if_statement_t *statement) fputs("if (", out); print_expression(statement->condition); fputs(") ", out); - if(statement->true_statement != NULL) { - print_statement(statement->true_statement); - } + print_statement(statement->true_statement); if(statement->false_statement != NULL) { print_indent(); @@ -933,6 +951,9 @@ static void print_declaration_statement( declaration_t *declaration = statement->declarations_begin; for( ; declaration != statement->declarations_end->next; declaration = declaration->next) { + if(declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY) + continue; + if(!first) { print_indent(); } else { @@ -1075,6 +1096,37 @@ end_of_print_asm_statement: fputs(");\n", out); } +/** + * Print a microsoft __try statement. + * + * @param statement the statement + */ +static void print_ms_try_statement(const ms_try_statement_t *statement) +{ + fputs("__try ", out); + print_statement(statement->try_statement); + print_indent(); + if(statement->except_expression != NULL) { + fputs("__except(", out); + print_expression(statement->except_expression); + fputs(") ", out); + } else { + fputs("__finally ", out); + } + print_statement(statement->final_statement); +} + +/** + * Print a microsoft __leave statement. + * + * @param statement the statement + */ +static void print_leave_statement(const leave_statement_t *statement) +{ + (void) statement; + fputs("__leave;\n", out); +} + /** * Print a statement. * @@ -1131,6 +1183,12 @@ void print_statement(const statement_t *statement) case STATEMENT_ASM: print_asm_statement(&statement->asms); break; + case STATEMENT_MS_TRY: + print_ms_try_statement(&statement->ms_try); + break; + case STATEMENT_LEAVE: + print_leave_statement(&statement->leave); + break; case STATEMENT_INVALID: fprintf(out, "$invalid statement$"); break; @@ -1167,7 +1225,7 @@ static void print_storage_class(storage_class_tag_t storage_class) void print_initializer(const initializer_t *initializer) { if(initializer == NULL) { - fputs("{ NIL-INITIALIZER }", out); + fputs("{}", out); return; } @@ -1185,8 +1243,9 @@ void print_initializer(const initializer_t *initializer) for(size_t i = 0 ; i < list->len; ++i) { const initializer_t *sub_init = list->initializers[i]; print_initializer(list->initializers[i]); - if(i < list->len-1 && sub_init->kind != INITIALIZER_DESIGNATOR) { - fputs(", ", out); + if(i < list->len-1) { + if(sub_init == NULL || sub_init->kind != INITIALIZER_DESIGNATOR) + fputs(", ", out); } } fputs(" }", out); @@ -1211,10 +1270,14 @@ void print_initializer(const initializer_t *initializer) * Print microsoft extended declaration modifiers. */ static void print_ms_modifiers(const declaration_t *declaration) { - decl_modifiers_t modifiers = declaration->modifiers; + if((c_mode & _MS) == 0) + return; + + decl_modifiers_t modifiers = declaration->decl_modifiers; /* DM_FORCEINLINE handled outside. */ - if((modifiers & ~DM_FORCEINLINE) != 0 || declaration->alignment != 0 || + if((modifiers & ~DM_FORCEINLINE) != 0 || + declaration->alignment != 0 || declaration->deprecated != 0 || declaration->get_property_sym != NULL || declaration->put_property_sym != NULL) { char *next = "("; @@ -1246,7 +1309,7 @@ static void print_ms_modifiers(const declaration_t *declaration) { if(modifiers & DM_NOINLINE) { fputs(next, out); next = ", "; fputs("noinline", out); } - if(modifiers & DM_DEPRECATED) { + if(declaration->deprecated != 0) { fputs(next, out); next = ", "; fputs("deprecated", out); if(declaration->deprecated_string != NULL) fprintf(out, "(\"%s\")", declaration->deprecated_string); @@ -1254,6 +1317,12 @@ static void print_ms_modifiers(const declaration_t *declaration) { if(declaration->alignment != 0) { fputs(next, out); next = ", "; fprintf(out, "align(%u)", declaration->alignment); } + if(modifiers & DM_RESTRICT) { + fputs(next, out); next = ", "; fputs("restrict", out); + } + if(modifiers & DM_NOALIAS) { + fputs(next, out); next = ", "; fputs("noalias", out); + } if(declaration->get_property_sym != NULL || declaration->put_property_sym != NULL) { char *comma = ""; fputs(next, out); next = ", "; fprintf(out, "property("); @@ -1278,10 +1347,10 @@ static void print_normal_declaration(const declaration_t *declaration) { print_storage_class((storage_class_tag_t) declaration->declared_storage_class); if(declaration->is_inline) { - if(declaration->modifiers & DM_FORCEINLINE) + if(declaration->decl_modifiers & DM_FORCEINLINE) fputs("__forceinline ", out); else { - if(declaration->modifiers & DM_MICROSOFT_INLINE) + if(declaration->decl_modifiers & DM_MICROSOFT_INLINE) fputs("__inline ", out); else fputs("inline ", out); @@ -1386,7 +1455,7 @@ bool is_constant_initializer(const initializer_t *initializer) case INITIALIZER_VALUE: return is_constant_expression(initializer->value.value); - case INITIALIZER_LIST: { + case INITIALIZER_LIST: for(size_t i = 0; i < initializer->list.len; ++i) { initializer_t *sub_initializer = initializer->list.initializers[i]; if(!is_constant_initializer(sub_initializer)) @@ -1394,7 +1463,6 @@ bool is_constant_initializer(const initializer_t *initializer) } return true; } - } panic("invalid initializer kind found"); } @@ -1440,6 +1508,14 @@ bool is_address_constant(const expression_t *expression) case EXPR_UNARY_TAKE_ADDRESS: return is_object_with_constant_address(expression->unary.value); + case EXPR_UNARY_DEREFERENCE: { + type_t *real_type = revert_automatic_type_conversion(expression->unary.value); + /* dereferencing a function is a NOP */ + if(is_type_function(real_type)) { + return is_address_constant(expression->unary.value); + } + } + case EXPR_UNARY_CAST: return is_type_pointer(skip_typeref(expression->base.type)) && (is_constant_expression(expression->unary.value) @@ -1486,8 +1562,7 @@ bool is_constant_expression(const expression_t *expression) case EXPR_WIDE_STRING_LITERAL: case EXPR_SIZEOF: case EXPR_CLASSIFY_TYPE: - case EXPR_FUNCTION: - case EXPR_PRETTY_FUNCTION: + case EXPR_FUNCNAME: case EXPR_OFFSETOF: case EXPR_ALIGNOF: case EXPR_BUILTIN_CONSTANT_P: @@ -1506,8 +1581,8 @@ bool is_constant_expression(const expression_t *expression) case EXPR_UNARY_PREFIX_DECREMENT: case EXPR_UNARY_BITFIELD_EXTRACT: case EXPR_UNARY_ASSUME: /* has VOID type */ - case EXPR_UNARY_DEREFERENCE: case EXPR_UNARY_TAKE_ADDRESS: + case EXPR_UNARY_DEREFERENCE: case EXPR_BINARY_ASSIGN: case EXPR_BINARY_MUL_ASSIGN: case EXPR_BINARY_DIV_ASSIGN: