]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/parser.cpp
Check the flat qualifier from the correct member
[libs/gl.git] / source / glsl / parser.cpp
index 4e8ff97aa894864632fcb47e044b3102d04b9f70..615d75d426a38e1e2e83dabf09fee6edd23b78e7 100644 (file)
@@ -66,7 +66,14 @@ void Parser::parse_source(const string &name, int index)
        allow_stage_change = true;
        while(!tokenizer.peek_token().empty())
                if(RefPtr<Statement> statement = parse_with_recovery(&Parser::parse_global_declaration))
+               {
                        cur_stage->content.body.push_back(statement);
+                       if(next_global_declaration)
+                       {
+                               cur_stage->content.body.push_back(next_global_declaration);
+                               next_global_declaration = 0;
+                       }
+               }
 
        if(!errors.empty())
                throw invalid_shader_source(join(errors.begin(), errors.end(), "\n"));
@@ -99,6 +106,11 @@ void Parser::stage_change(Stage::Type stage)
        cur_stage = &module->stages.back();
 
        stage_types.clear();
+       if(const Stage *builtin = get_builtins(stage))
+       {
+               for(const auto &kvp: builtin->types)
+                       stage_types.insert(kvp.first);
+       }
        for(const Module *m: imported_modules)
        {
                auto j = find_member(m->stages, stage, &Stage::type);
@@ -164,7 +176,7 @@ bool Parser::is_interface_qualifier(const string &token)
 
 bool Parser::is_sampling_qualifier(const string &token)
 {
-       return (token=="centroid" || token=="sample");
+       return (token=="centroid" || token=="sample" || token=="patch");
 }
 
 bool Parser::is_interpolation_qualifier(const string &token)
@@ -275,9 +287,11 @@ RefPtr<Statement> Parser::parse_global_declaration()
                }
                else if(is_interface_qualifier(token) && tokenizer.peek_token(2)=="{")
                {
-                       RefPtr<InterfaceBlock> iface = parse_interface_block();
-                       iface->layout = layout;
-                       return iface;
+                       RefPtr<StructDeclaration> iface_strct = parse_interface_block();
+                       VariableDeclaration *iface_var = iface_strct->block_declaration;
+                       iface_var->layout = layout;
+                       next_global_declaration = iface_var;
+                       return iface_strct;
                }
                else
                {
@@ -296,7 +310,11 @@ RefPtr<Statement> Parser::parse_global_declaration()
                if(is_type(next) || is_qualifier(next))
                        return parse_variable_declaration();
                else
-                       return parse_interface_block();
+               {
+                       RefPtr<StructDeclaration> iface_strct = parse_interface_block();
+                       next_global_declaration = iface_strct->block_declaration;
+                       return iface_strct;
+               }
        }
        else if(is_qualifier(token))
                return parse_variable_declaration();
@@ -605,6 +623,15 @@ RefPtr<FunctionCall> Parser::parse_function_call(const VariableReference &var)
        return call;
 }
 
+void Parser::add_type(TypeDeclaration &type)
+{
+       cur_stage->types[type.name] = &type;
+       if(cur_stage->type==Stage::SHARED)
+               global_types.insert(type.name);
+       else
+               stage_types.insert(type.name);
+}
+
 RefPtr<TypeDeclaration> Parser::parse_type_declaration()
 {
        tokenizer.expect("typedef");
@@ -616,11 +643,7 @@ RefPtr<TypeDeclaration> Parser::parse_type_declaration()
                type = parse_basic_type_declaration();
 
        tokenizer.expect(";");
-       cur_stage->types[type->name] = type.get();
-       if(cur_stage->type==Stage::SHARED)
-               global_types.insert(type->name);
-       else
-               stage_types.insert(type->name);
+       add_type(*type);
        return type;
 }
 
@@ -684,6 +707,8 @@ RefPtr<ImageTypeDeclaration> Parser::parse_image_type_declaration()
                        type->sampled = true;
                else if(token=="shadow")
                        type->shadow = true;
+               else if(token=="multisample")
+                       type->multisample = true;
                else
                        throw parse_error(tokenizer.get_location(), token, "image type attribute");
 
@@ -710,11 +735,7 @@ RefPtr<StructDeclaration> Parser::parse_struct_declaration()
        parse_block(strct->members, true, &Parser::parse_variable_declaration);
        tokenizer.expect(";");
 
-       cur_stage->types[strct->name] = strct.get();
-       if(cur_stage->type==Stage::SHARED)
-               global_types.insert(strct->name);
-       else
-               stage_types.insert(strct->name);
+       add_type(*strct);
        return strct;
 }
 
@@ -810,29 +831,43 @@ RefPtr<FunctionDeclaration> Parser::parse_function_declaration()
        return func;
 }
 
-RefPtr<InterfaceBlock> Parser::parse_interface_block()
+RefPtr<StructDeclaration> Parser::parse_interface_block()
 {
-       RefPtr<InterfaceBlock> iface = create_node<InterfaceBlock>();
-
-       iface->interface = tokenizer.parse_token();
-       if(!is_interface_qualifier(iface->interface))
-               throw parse_error(tokenizer.get_location(), iface->interface, "an interface qualifier");
+       RefPtr<StructDeclaration> strct = create_node<StructDeclaration>();
+       RefPtr<VariableDeclaration> var = create_node<VariableDeclaration>();
 
-       iface->block_name = expect_identifier();
-       iface->members = new Block;
-       parse_block(*iface->members, true, &Parser::parse_variable_declaration_with_layout);
+       var->interface = tokenizer.parse_token();
+       if(!is_interface_qualifier(var->interface))
+               throw parse_error(tokenizer.get_location(), var->interface, "an interface qualifier");
+
+       strct->block_name = expect_identifier();
+       string name_base = format("_%s_%s", var->interface, strct->block_name);
+       strct->name = name_base;
+       for(unsigned i=1; (stage_types.count(strct->name) || global_types.count(strct->name)); ++i)
+               strct->name = format("%s_%d", name_base, i);
+       var->type = strct->name;
+       parse_block(strct->members, true, &Parser::parse_variable_declaration_with_layout);
        if(!check(";"))
        {
-               iface->instance_name = expect_identifier();
+               var->name = expect_identifier();
                if(check("["))
                {
-                       iface->array = true;
-                       tokenizer.expect("]");
+                       var->array = true;
+                       if(!check("]"))
+                       {
+                               var->array_size = parse_expression();
+                               tokenizer.expect("]");
+                       }
                }
                tokenizer.expect(";");
        }
+       else
+               var->name = format("%s %s", var->interface, strct->block_name);
+
+       strct->block_declaration = var.release();
+       add_type(*strct);
 
-       return iface;
+       return strct;
 }
 
 RefPtr<Conditional> Parser::parse_conditional()