]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/parser.cpp
Don't allow arbitrary statements in structs or interface blocks
[libs/gl.git] / source / glsl / parser.cpp
index eee6212930f5dccabdac77b4a32467f91fb89727..b493c677d3d4007a4c9d8c285ba6d8c4b99fc9f3 100644 (file)
@@ -1,5 +1,6 @@
 #include <msp/strings/format.h>
 #include <msp/strings/regex.h>
+#include <msp/strings/utils.h>
 #include "glsl_error.h"
 #include "parser.h"
 
@@ -17,7 +18,9 @@ Parser::Parser():
 {
        tokenizer.signal_preprocess.connect(sigc::mem_fun(&preprocessor, &Preprocessor::preprocess));
        preprocessor.signal_version.connect(sigc::mem_fun(this, &Parser::set_required_version));
+       preprocessor.signal_source.connect(sigc::mem_fun(this, &Parser::source_reference));
        preprocessor.signal_stage_change.connect(sigc::mem_fun(this, &Parser::stage_change));
+       preprocessor.signal_line.connect(sigc::mem_fun(this, &Parser::line_change));
 }
 
 Parser::~Parser()
@@ -50,8 +53,9 @@ void Parser::parse_source(const string &name, unsigned index)
        delete module;
        module = new Module;
        cur_stage = &module->shared;
+       base_index = index;
        source_index = index;
-       module->source_map.set_name(source_index, name);
+       source_reference(1, name);
        tokenizer.begin(name, source);
        while(RefPtr<Statement> statement = parse_global_declaration())
                cur_stage->content.body.push_back(statement);
@@ -59,7 +63,15 @@ void Parser::parse_source(const string &name, unsigned index)
 
 void Parser::set_required_version(const Version &ver)
 {
-       cur_stage->required_version = ver;
+       cur_stage->required_features.glsl_version = ver;
+}
+
+void Parser::source_reference(unsigned index, const string &name)
+{
+       if(index<1)
+               throw invalid_shader_source(tokenizer.get_location(), "Invalid source reference");
+
+       module->source_map.set_name(base_index+index-1, name);
 }
 
 void Parser::stage_change(Stage::Type stage)
@@ -76,6 +88,21 @@ void Parser::stage_change(Stage::Type stage)
        cur_stage = &module->stages.back();
 }
 
+void Parser::line_change(int index, unsigned line)
+{
+       if(index>0)
+               source_index = base_index+index-1;
+       else if(index==0)
+               source_index = 0;
+       else
+               index = source_index;
+
+       string name = module->source_map.get_name(index);
+       if(name.empty())
+               name = format("<%d>", index);
+       tokenizer.set_location(Location(name, line));
+}
+
 string Parser::expect_type()
 {
        string token = tokenizer.parse_token();
@@ -92,6 +119,14 @@ string Parser::expect_identifier()
        return token;
 }
 
+int Parser::expect_integer()
+{
+       string token = tokenizer.parse_token();
+       if(!isnumrc(token))
+               throw parse_error(tokenizer.get_location(), token, "an integer literal");
+       return lexical_cast<int>(token);
+}
+
 bool Parser::check(const string &token)
 {
        bool result = (tokenizer.peek_token()==token);
@@ -289,10 +324,10 @@ RefPtr<Layout> Parser::parse_layout()
 
                layout->qualifiers.push_back(Layout::Qualifier());
                Layout::Qualifier &qual = layout->qualifiers.back();
-               qual.identifier = token;
+               qual.name = token;
 
-               if(check("="))
-                       qual.value = tokenizer.parse_token();
+               if((qual.has_value = check("=")))
+                       qual.value = expect_integer();
 
                if(tokenizer.peek_token()==")")
                        break;
@@ -304,7 +339,8 @@ RefPtr<Layout> Parser::parse_layout()
        return layout;
 }
 
-void Parser::parse_block(Block &block, bool require_braces)
+template<typename T>
+void Parser::parse_block(Block &block, bool require_braces, RefPtr<T> (Parser::*parse_content)())
 {
        bool have_braces = (require_braces || tokenizer.peek_token()=="{");
        if(have_braces)
@@ -313,10 +349,10 @@ void Parser::parse_block(Block &block, bool require_braces)
        if(have_braces)
        {
                while(tokenizer.peek_token()!="}")
-                       block.body.push_back(parse_statement());
+                       block.body.push_back((this->*parse_content)());
        }
        else
-               block.body.push_back(parse_statement());
+               block.body.push_back((this->*parse_content)());
 
        block.use_braces = (require_braces || block.body.size()!=1);
 
@@ -451,7 +487,7 @@ RefPtr<StructDeclaration> Parser::parse_struct_declaration()
        strct->line = tokenizer.get_location().line;
 
        strct->name = expect_identifier();
-       parse_block(strct->members, true);
+       parse_block(strct->members, true, &Parser::parse_variable_declaration);
        tokenizer.expect(";");
 
        declared_types.insert(strct->name);
@@ -501,6 +537,18 @@ RefPtr<VariableDeclaration> Parser::parse_variable_declaration()
        return var;
 }
 
+RefPtr<VariableDeclaration> Parser::parse_variable_declaration_with_layout()
+{
+       RefPtr<Layout> layout;
+       if(tokenizer.peek_token()=="layout")
+               layout = parse_layout();
+
+       RefPtr<VariableDeclaration> var = parse_variable_declaration();
+       var->layout = layout;
+
+       return var;
+}
+
 RefPtr<FunctionDeclaration> Parser::parse_function_declaration()
 {
        RefPtr<FunctionDeclaration> func = new FunctionDeclaration;
@@ -529,7 +577,7 @@ RefPtr<FunctionDeclaration> Parser::parse_function_declaration()
        if(token=="{")
        {
                func->definition = func.get();
-               parse_block(func->body, true);
+               parse_block(func->body, true, &Parser::parse_statement);
        }
        else if(token==";")
                tokenizer.parse_token();
@@ -550,7 +598,7 @@ RefPtr<InterfaceBlock> Parser::parse_interface_block()
                throw parse_error(tokenizer.get_location(), iface->interface, "an interface qualifier");
 
        iface->name = expect_identifier();
-       parse_block(iface->members, true);
+       parse_block(iface->members, true, &Parser::parse_variable_declaration_with_layout);
        if(!check(";"))
        {
                iface->instance_name = expect_identifier();
@@ -575,13 +623,13 @@ RefPtr<Conditional> Parser::parse_conditional()
        cond->condition = parse_expression();
        tokenizer.expect(")");
 
-       parse_block(cond->body, false);
+       parse_block(cond->body, false, &Parser::parse_statement);
 
        string token = tokenizer.peek_token();
        if(token=="else")
        {
                tokenizer.parse_token();
-               parse_block(cond->else_body, false);
+               parse_block(cond->else_body, false, &Parser::parse_statement);
        }
 
        return cond;
@@ -614,7 +662,7 @@ RefPtr<Iteration> Parser::parse_for()
                loop->loop_expression = parse_expression();
        tokenizer.expect(")");
 
-       parse_block(loop->body, false);
+       parse_block(loop->body, false, &Parser::parse_statement);
 
        return loop;
 }
@@ -629,7 +677,7 @@ RefPtr<Iteration> Parser::parse_while()
        loop->condition = parse_expression();
        tokenizer.expect(")");
 
-       parse_block(loop->body, false);
+       parse_block(loop->body, false, &Parser::parse_statement);
 
        return loop;
 }