also support extern variable declarations in local scope
[cparser] / ast2firm.c
index 714e013..bca405d 100644 (file)
@@ -3225,6 +3225,60 @@ static ir_node *compound_statement_to_firm(compound_statement_t *compound)
        return result;
 }
 
+static void create_global_variable(declaration_t *declaration)
+{
+       ir_visibility  vis;
+       ir_type       *var_type;
+       switch ((storage_class_tag_t)declaration->storage_class) {
+               case STORAGE_CLASS_STATIC:
+                       vis = visibility_local;
+                       goto global_var;
+
+               case STORAGE_CLASS_EXTERN:
+                       vis = visibility_external_allocated;
+                       goto global_var;
+
+               case STORAGE_CLASS_NONE:
+                       vis = visibility_external_visible;
+                       goto global_var;
+
+               case STORAGE_CLASS_THREAD:
+                       vis = visibility_external_visible;
+                       goto tls_var;
+
+               case STORAGE_CLASS_THREAD_EXTERN:
+                       vis = visibility_external_allocated;
+                       goto tls_var;
+
+               case STORAGE_CLASS_THREAD_STATIC:
+                       vis = visibility_local;
+                       goto tls_var;
+
+tls_var:
+                       var_type = get_tls_type();
+                       goto create_var;
+
+global_var:
+                       var_type = get_glob_type();
+                       goto create_var;
+
+create_var:
+                       create_declaration_entity(declaration,
+                                                 DECLARATION_KIND_GLOBAL_VARIABLE,
+                                                 var_type);
+                       set_entity_visibility(declaration->v.entity, vis);
+
+                       return;
+
+               case STORAGE_CLASS_TYPEDEF:
+               case STORAGE_CLASS_AUTO:
+               case STORAGE_CLASS_REGISTER:
+               case STORAGE_CLASS_ENUM_ENTRY:
+                       break;
+       }
+       panic("Invalid storage class for global variable");
+}
+
 static void create_local_declaration(declaration_t *declaration)
 {
        if(declaration->symbol == NULL)
@@ -3239,12 +3293,18 @@ static void create_local_declaration(declaration_t *declaration)
        case STORAGE_CLASS_ENUM_ENTRY:
                panic("enum entry declaration in local block found");
        case STORAGE_CLASS_EXTERN:
-               panic("extern declaration in local block found");
+               create_global_variable(declaration);
+               create_initializer(declaration);
+               return;
        case STORAGE_CLASS_NONE:
        case STORAGE_CLASS_AUTO:
        case STORAGE_CLASS_REGISTER:
                if(is_type_function(type)) {
-                       panic("nested functions not supported yet");
+                       if(declaration->init.statement != NULL) {
+                               panic("nested functions not supported yet");
+                       } else {
+                               get_function_entity(declaration);
+                       }
                } else {
                        create_local_variable(declaration);
                }
@@ -3841,6 +3901,14 @@ static int count_decls_in_expression(const expression_t *expression) {
        }
        EXPR_UNARY_CASES
                return count_decls_in_expression(expression->unary.value);
+       case EXPR_CALL: {
+               int count = 0;
+               call_argument_t *argument = expression->call.arguments;
+               for( ; argument != NULL; argument = argument->next) {
+                       count += count_decls_in_expression(argument->expression);
+               }
+               return count;
+       }
 
        default:
                break;
@@ -4145,62 +4213,6 @@ static void create_function(declaration_t *declaration)
        irg_vrfy(irg);
 }
 
-static void create_global_variable(declaration_t *declaration)
-{
-       ir_visibility  vis;
-       ir_type       *var_type;
-       switch ((storage_class_tag_t)declaration->storage_class) {
-               case STORAGE_CLASS_STATIC:
-                       vis = visibility_local;
-                       goto global_var;
-
-               case STORAGE_CLASS_EXTERN:
-                       vis = visibility_external_allocated;
-                       goto global_var;
-
-               case STORAGE_CLASS_NONE:
-                       vis = visibility_external_visible;
-                       goto global_var;
-
-               case STORAGE_CLASS_THREAD:
-                       vis = visibility_external_visible;
-                       goto tls_var;
-
-               case STORAGE_CLASS_THREAD_EXTERN:
-                       vis = visibility_external_allocated;
-                       goto tls_var;
-
-               case STORAGE_CLASS_THREAD_STATIC:
-                       vis = visibility_local;
-                       goto tls_var;
-
-tls_var:
-                       var_type = get_tls_type();
-                       goto create_var;
-
-global_var:
-                       var_type = get_glob_type();
-                       goto create_var;
-
-create_var:
-                       create_declaration_entity(declaration,
-                                                 DECLARATION_KIND_GLOBAL_VARIABLE,
-                                                 var_type);
-                       set_entity_visibility(declaration->v.entity, vis);
-
-                       current_ir_graph = get_const_code_irg();
-                       create_initializer(declaration);
-                       return;
-
-               case STORAGE_CLASS_TYPEDEF:
-               case STORAGE_CLASS_AUTO:
-               case STORAGE_CLASS_REGISTER:
-               case STORAGE_CLASS_ENUM_ENTRY:
-                       break;
-       }
-       panic("Invalid storage class for global variable");
-}
-
 static void scope_to_firm(scope_t *scope)
 {
        /* first pass: create declarations */
@@ -4222,7 +4234,7 @@ static void scope_to_firm(scope_t *scope)
                }
        }
 
-       /* second pass: create code */
+       /* second pass: create code/initializers */
        declaration = scope->declarations;
        for( ; declaration != NULL; declaration = declaration->next) {
                if(declaration->namespc != NAMESPACE_NORMAL)
@@ -4234,10 +4246,14 @@ static void scope_to_firm(scope_t *scope)
                        continue;
 
                type_t *type = declaration->type;
-               if(type->kind != TYPE_FUNCTION)
-                       continue;
-
-               create_function(declaration);
+               if(type->kind == TYPE_FUNCTION) {
+                       create_function(declaration);
+               } else {
+                       assert(declaration->declaration_kind
+                                       == DECLARATION_KIND_GLOBAL_VARIABLE);
+                       current_ir_graph = get_const_code_irg();
+                       create_initializer(declaration);
+               }
        }
 }