]> 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 72f8ebfa6279982e675b98b9c5364044e90f3150..615d75d426a38e1e2e83dabf09fee6edd23b78e7 100644 (file)
@@ -1,9 +1,11 @@
+#include <msp/core/algorithm.h>
 #include <msp/core/raii.h>
 #include <msp/strings/format.h>
 #include <msp/strings/regex.h>
 #include <msp/strings/utils.h>
 #include "builtin.h"
 #include "glsl_error.h"
+#include "modulecache.h"
 #include "parser.h"
 
 #undef interface
@@ -14,7 +16,8 @@ namespace Msp {
 namespace GL {
 namespace SL {
 
-Parser::Parser():
+Parser::Parser(ModuleCache *s):
+       mod_cache(s),
        preprocessor(tokenizer),
        module(0)
 {
@@ -25,20 +28,16 @@ Parser::Parser():
        preprocessor.signal_line.connect(sigc::mem_fun(this, &Parser::line_change));
 }
 
-Parser::~Parser()
-{
-       delete module;
-}
-
-Module &Parser::parse(const string &s, const string &n, int i)
+void Parser::parse(Module &m, const string &s, const string &n, int i)
 {
+       SetForScope<Module *> set_module(module, &m);
        source = s;
        parse_source(n, i);
-       return *module;
 }
 
-Module &Parser::parse(IO::Base &io, const string &n, int i)
+void Parser::parse(Module &m, IO::Base &io, const string &n, int i)
 {
+       SetForScope<Module *> set_module(module, &m);
        source = string();
        while(!io.eof())
        {
@@ -47,39 +46,34 @@ Module &Parser::parse(IO::Base &io, const string &n, int i)
                source.append(buffer, len);
        }
        parse_source(n, i);
-       return *module;
 }
 
 void Parser::parse_source(const string &name, int index)
 {
-       delete module;
-       module = new Module;
-
        cur_stage = &module->shared;
        base_index = index;
        source_index = index;
        if(index>=0)
                source_reference(1, name);
 
-       // TODO Need to somehow get type names from imports
        if(const Stage *builtin = get_builtins(Stage::SHARED))
        {
-               for(map<string, TypeDeclaration *>::const_iterator i=builtin->types.begin(); i!=builtin->types.end(); ++i)
-                       declared_types.insert(i->first);
-       }
-       else
-       {
-               declared_types.insert("void");
-               declared_types.insert("bool");
-               declared_types.insert("int");
-               declared_types.insert("float");
+               for(const auto &kvp: builtin->types)
+                       global_types.insert(kvp.first);
        }
 
        tokenizer.begin(source, name);
        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"));
@@ -110,6 +104,22 @@ void Parser::stage_change(Stage::Type stage)
        if(cur_stage->type!=Stage::SHARED)
                module->stages.back().previous = cur_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);
+               if(j!=m->stages.end())
+               {
+                       for(const auto &kvp: j->types)
+                               stage_types.insert(kvp.first);
+               }
+       }
 }
 
 void Parser::line_change(int index, unsigned line)
@@ -166,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)
@@ -190,13 +200,13 @@ bool Parser::is_qualifier(const string &token)
 
 bool Parser::is_type(const string &token)
 {
-       return declared_types.count(token);
+       return global_types.count(token) || stage_types.count(token);
 }
 
 bool Parser::is_identifier(const string &token)
 {
        static Regex re("^[a-zA-Z_][a-zA-Z0-9_]*$");
-       return re.match(token);
+       return static_cast<bool>(re.match(token));
 }
 
 template<typename T>
@@ -277,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
                {
@@ -298,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();
@@ -366,6 +382,15 @@ RefPtr<Import> Parser::parse_import()
        RefPtr<Import> import = create_node<Import>();
        import->module = expect_identifier();
        tokenizer.expect(";");
+
+       if(mod_cache)
+       {
+               const Module &imported_mod = mod_cache->get_module(import->module);
+               imported_modules.push_back(&imported_mod);
+               for(const auto &kvp: imported_mod.shared.types)
+                       global_types.insert(kvp.first);
+       }
+
        return import;
 }
 
@@ -536,7 +561,9 @@ RefPtr<Literal> Parser::parse_literal()
        if(isdigit(literal->token[0]))
        {
                // TODO have the tokenizer return the type of the token
-               if(literal->token.back()=='f')
+               if(literal->token.back()=='u')
+                       literal->value = lexical_cast<unsigned>(literal->token.substr(0, literal->token.size()-1));
+               else if(literal->token.back()=='f')
                        literal->value = lexical_cast<float>(literal->token.substr(0, literal->token.size()-1));
                else if(literal->token.find('.')!=string::npos)
                        literal->value = lexical_cast<float>(literal->token);
@@ -596,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");
@@ -607,7 +643,7 @@ RefPtr<TypeDeclaration> Parser::parse_type_declaration()
                type = parse_basic_type_declaration();
 
        tokenizer.expect(";");
-       declared_types.insert(type->name);
+       add_type(*type);
        return type;
 }
 
@@ -671,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");
 
@@ -697,7 +735,7 @@ RefPtr<StructDeclaration> Parser::parse_struct_declaration()
        parse_block(strct->members, true, &Parser::parse_variable_declaration);
        tokenizer.expect(";");
 
-       declared_types.insert(strct->name);
+       add_type(*strct);
        return strct;
 }
 
@@ -793,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);
 
-       return iface;
+       strct->block_declaration = var.release();
+       add_type(*strct);
+
+       return strct;
 }
 
 RefPtr<Conditional> Parser::parse_conditional()