]> git.tdb.fi Git - libs/gl.git/commitdiff
Don't allow arbitrary statements in structs or interface blocks
authorMikko Rasa <tdb@tdb.fi>
Thu, 25 Feb 2021 23:09:21 +0000 (01:09 +0200)
committerMikko Rasa <tdb@tdb.fi>
Thu, 25 Feb 2021 23:21:11 +0000 (01:21 +0200)
source/glsl/parser.cpp
source/glsl/parser.h

index 4b6bd7ed08992271f5ba73318a253b8a4ec2d6ab..b493c677d3d4007a4c9d8c285ba6d8c4b99fc9f3 100644 (file)
@@ -339,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)
@@ -348,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);
 
@@ -486,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);
@@ -536,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;
@@ -564,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();
@@ -585,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();
@@ -610,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;
@@ -649,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;
 }
@@ -664,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;
 }
index 1572128ffc6bb2378e0e465433a6e01240d6bbe4..9ab1232aaa0d9ad7aec82b24ee873b8b4e998e16 100644 (file)
@@ -64,12 +64,14 @@ private:
        RefPtr<Import> parse_import();
        RefPtr<Precision> parse_precision();
        RefPtr<Layout> parse_layout();
-       void parse_block(Block &, bool);
+       template<typename T>
+       void parse_block(Block &, bool, RefPtr<T> (Parser::*)());
        RefPtr<Expression> parse_expression(unsigned = 0);
        RefPtr<BinaryExpression> parse_binary(const RefPtr<Expression> &, const Operator *);
        RefPtr<FunctionCall> parse_function_call(const VariableReference &);
        RefPtr<StructDeclaration> parse_struct_declaration();
        RefPtr<VariableDeclaration> parse_variable_declaration();
+       RefPtr<VariableDeclaration> parse_variable_declaration_with_layout();
        RefPtr<FunctionDeclaration> parse_function_declaration();
        RefPtr<InterfaceBlock> parse_interface_block();
        RefPtr<Conditional> parse_conditional();