X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ast.c;h=7bac0da7b7283154950242160ef12966b63bd545;hb=4108d31ed34aee3c0841ae01b844420da8a3cdf1;hp=3184cf65958d0d691bb681c56aff3a43e028354a;hpb=8f2fe47e7c42914a261f6481ce20664ff7249378;p=cparser diff --git a/ast.c b/ast.c index 3184cf6..7bac0da 100644 --- a/ast.c +++ b/ast.c @@ -23,6 +23,7 @@ #include "symbol_t.h" #include "type_t.h" #include "parser.h" +#include "lang_features.h" #include #include @@ -114,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, @@ -137,7 +137,6 @@ static unsigned get_expression_precedence(expression_kind_t kind) [EXPR_UNARY_CAST] = PREC_UNARY, [EXPR_UNARY_CAST_IMPLICIT] = PREC_UNARY, [EXPR_UNARY_ASSUME] = PREC_PRIM, - [EXPR_UNARY_BITFIELD_EXTRACT] = PREC_ACCESS, [EXPR_BINARY_ADD] = PREC_PLUS, [EXPR_BINARY_SUB] = PREC_PLUS, @@ -318,6 +317,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) { @@ -437,10 +454,6 @@ static void print_unary_expression(const unary_expression_t *unexpr) case EXPR_UNARY_DEREFERENCE: fputs("*", out); break; case EXPR_UNARY_TAKE_ADDRESS: fputs("&", out); break; - case EXPR_UNARY_BITFIELD_EXTRACT: - print_expression_prec(unexpr->value, prec); - return; - case EXPR_UNARY_POSTFIX_INCREMENT: print_expression_prec(unexpr->value, prec); fputs("++", out); @@ -623,7 +636,7 @@ static void print_select(const select_expression_t *expression) { unsigned prec = get_expression_precedence(expression->base.kind); print_expression_prec(expression->compound, prec); - if(is_type_pointer(expression->compound->base.type)) { + if(is_type_pointer(skip_typeref(expression->compound->base.type))) { fputs("->", out); } else { fputc('.', out); @@ -717,8 +730,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; @@ -1006,23 +1020,23 @@ static void print_for_statement(const for_statement_t *statement) } /** - * Print assembler constraints. + * Print assembler arguments. * - * @param constraints the constraints + * @param arguments the arguments */ -static void print_asm_constraints(asm_constraint_t *constraints) +static void print_asm_arguments(asm_argument_t *arguments) { - asm_constraint_t *constraint = constraints; - for( ; constraint != NULL; constraint = constraint->next) { - if(constraint != constraints) + asm_argument_t *argument = arguments; + for( ; argument != NULL; argument = argument->next) { + if(argument != arguments) fputs(", ", out); - if(constraint->symbol) { - fprintf(out, "[%s] ", constraint->symbol->string); + if(argument->symbol) { + fprintf(out, "[%s] ", argument->symbol->string); } - print_quoted_string(&constraint->constraints, '"'); + print_quoted_string(&argument->constraints, '"'); fputs(" (", out); - print_expression(constraint->expression); + print_expression(argument->expression); fputs(")", out); } } @@ -1061,12 +1075,12 @@ static void print_asm_statement(const asm_statement_t *statement) goto end_of_print_asm_statement; fputs(" : ", out); - print_asm_constraints(statement->inputs); + print_asm_arguments(statement->inputs); if(statement->outputs == NULL && statement->clobbers == NULL) goto end_of_print_asm_statement; fputs(" : ", out); - print_asm_constraints(statement->outputs); + print_asm_arguments(statement->outputs); if(statement->clobbers == NULL) goto end_of_print_asm_statement; @@ -1077,6 +1091,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. * @@ -1133,6 +1178,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; @@ -1214,10 +1265,14 @@ void print_initializer(const initializer_t *initializer) * Print microsoft extended declaration modifiers. */ static void print_ms_modifiers(const declaration_t *declaration) { + if((c_mode & _MS) == 0) + return; + decl_modifiers_t modifiers = declaration->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 = "("; @@ -1249,7 +1304,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); @@ -1406,18 +1461,19 @@ bool is_constant_initializer(const initializer_t *initializer) panic("invalid initializer kind found"); } -static bool is_object_with_constant_address(const expression_t *expression) +static bool is_object_with_linker_constant_address(const expression_t *expression) { switch(expression->kind) { case EXPR_UNARY_DEREFERENCE: return is_address_constant(expression->unary.value); case EXPR_SELECT: { - if(is_type_pointer(expression->select.compound->base.type)) { + type_t *base_type = skip_typeref(expression->select.compound->base.type); + if(is_type_pointer(base_type)) { /* it's a -> */ return is_address_constant(expression->select.compound); } else { - return is_object_with_constant_address(expression->select.compound); + return is_object_with_linker_constant_address(expression->select.compound); } } @@ -1446,20 +1502,30 @@ bool is_address_constant(const expression_t *expression) { switch(expression->kind) { case EXPR_UNARY_TAKE_ADDRESS: - return is_object_with_constant_address(expression->unary.value); + return is_object_with_linker_constant_address(expression->unary.value); case EXPR_UNARY_DEREFERENCE: { - type_t *real_type = revert_automatic_type_conversion(expression->unary.value); + 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); } + + /* fallthrough */ } - case EXPR_UNARY_CAST: - return is_type_pointer(skip_typeref(expression->base.type)) - && (is_constant_expression(expression->unary.value) + case EXPR_UNARY_CAST: { + type_t *dest = skip_typeref(expression->base.type); + if (!is_type_pointer(dest) && + ! (dest->kind == TYPE_ATOMIC + && (get_atomic_type_flags(dest->atomic.akind) & ATOMIC_TYPE_FLAG_INTEGER) + && (get_atomic_type_size(dest->atomic.akind) >= get_atomic_type_size(get_intptr_kind())))) + return false; + + return (is_constant_expression(expression->unary.value) || is_address_constant(expression->unary.value)); + } case EXPR_BINARY_ADD: case EXPR_BINARY_SUB: { @@ -1481,7 +1547,7 @@ bool is_address_constant(const expression_t *expression) if(is_type_function(type)) return true; if(is_type_array(type)) { - return is_object_with_constant_address(expression); + return is_object_with_linker_constant_address(expression); } return false; } @@ -1491,6 +1557,64 @@ bool is_address_constant(const expression_t *expression) } } +static bool is_builtin_const_call(const expression_t *expression) +{ + expression_t *function = expression->call.function; + if (function->kind != EXPR_BUILTIN_SYMBOL) { + return false; + } + + symbol_t *symbol = function->builtin_symbol.symbol; + + switch (symbol->ID) { + case T___builtin_huge_val: + case T___builtin_nan: + case T___builtin_nanf: + case T___builtin_nand: + return true; + } + + return false; +} + +static bool is_constant_pointer(const expression_t *expression) +{ + if (is_constant_expression(expression)) + return true; + + switch (expression->kind) { + case EXPR_SELECT: + return is_constant_pointer(expression->select.compound); + case EXPR_UNARY_CAST: + return is_constant_pointer(expression->unary.value); + default: + return false; + } +} + +static bool is_object_with_constant_address(const expression_t *expression) +{ + switch(expression->kind) { + case EXPR_SELECT: { + expression_t *compound = expression->select.compound; + type_t *compound_type = compound->base.type; + compound_type = skip_typeref(compound_type); + if(is_type_pointer(compound_type)) { + return is_constant_pointer(compound); + } else { + return is_object_with_constant_address(compound); + } + } + case EXPR_ARRAY_ACCESS: + return is_constant_pointer(expression->array_access.array_ref) + && is_constant_expression(expression->array_access.index); + case EXPR_UNARY_DEREFERENCE: + return is_constant_pointer(expression->unary.value); + default: + return false; + } +} + bool is_constant_expression(const expression_t *expression) { switch(expression->kind) { @@ -1502,8 +1626,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: @@ -1511,7 +1634,6 @@ bool is_constant_expression(const expression_t *expression) case EXPR_BUILTIN_SYMBOL: case EXPR_BUILTIN_PREFETCH: - case EXPR_CALL: case EXPR_SELECT: case EXPR_VA_START: case EXPR_VA_ARG: @@ -1520,9 +1642,7 @@ bool is_constant_expression(const expression_t *expression) case EXPR_UNARY_POSTFIX_DECREMENT: case EXPR_UNARY_PREFIX_INCREMENT: case EXPR_UNARY_PREFIX_DECREMENT: - case EXPR_UNARY_BITFIELD_EXTRACT: case EXPR_UNARY_ASSUME: /* has VOID type */ - case EXPR_UNARY_TAKE_ADDRESS: case EXPR_UNARY_DEREFERENCE: case EXPR_BINARY_ASSIGN: case EXPR_BINARY_MUL_ASSIGN: @@ -1538,6 +1658,12 @@ bool is_constant_expression(const expression_t *expression) case EXPR_BINARY_COMMA: return false; + case EXPR_UNARY_TAKE_ADDRESS: + return is_object_with_constant_address(expression->unary.value); + + case EXPR_CALL: + return is_builtin_const_call(expression); + case EXPR_UNARY_NEGATE: case EXPR_UNARY_PLUS: case EXPR_UNARY_BITWISE_NEGATE: