]> git.tdb.fi Git - libs/gl.git/commitdiff
Slightly refactor layout qualifiers in the GLSL parser
authorMikko Rasa <tdb@tdb.fi>
Sun, 21 Feb 2021 23:52:29 +0000 (01:52 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sun, 21 Feb 2021 23:53:20 +0000 (01:53 +0200)
Their values are now stored as integers, since no qualifier takes a
value of any other type.

source/glsl/compatibility.cpp
source/glsl/debug.cpp
source/glsl/generate.cpp
source/glsl/output.cpp
source/glsl/parser.cpp
source/glsl/parser.h
source/glsl/syntax.h

index 0769839430f371c646374f8150cfb0f245e17607..a65f6de72fe1a3ddfe29be27edd8d563c9dde55a 100644 (file)
@@ -264,20 +264,19 @@ void LegacyConverter::visit(VariableDeclaration &var)
        if(var.layout && !supports_interface_layouts())
        {
                vector<Layout::Qualifier>::iterator i;
-               for(i=var.layout->qualifiers.begin(); (i!=var.layout->qualifiers.end() && i->identifier!="location"); ++i) ;
+               for(i=var.layout->qualifiers.begin(); (i!=var.layout->qualifiers.end() && i->name!="location"); ++i) ;
                if(i!=var.layout->qualifiers.end())
                {
-                       unsigned location = lexical_cast<unsigned>(i->value);
                        if(stage->type==Stage::VERTEX && var.interface=="in")
                        {
-                               stage->locations[var.name] = location;
+                               stage->locations[var.name] = i->value;
                                var.layout->qualifiers.erase(i);
                        }
                        else if(stage->type==Stage::FRAGMENT && var.interface=="out")
                        {
-                               if(location!=0 && !check_extension(&Features::ext_gpu_shader4))
+                               if(i->value!=0 && !check_extension(&Features::ext_gpu_shader4))
                                        throw unsupported_shader("EXT_gpu_shader4 is required");
-                               stage->locations[var.name] = location;
+                               stage->locations[var.name] = i->value;
                                var.layout->qualifiers.erase(i);
                        }
 
index 0a5b6ef375be2efccb77261fdf6c9e124c4f2b05..2bc3a893e099384e16c663cfdfd601e3a945d85d 100644 (file)
@@ -196,9 +196,9 @@ void DumpTree::visit(Layout &layout)
        for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); )
        {
                vector<Layout::Qualifier>::const_iterator j = increment(i, layout.qualifiers);
-               string qualifier = j->identifier;
-               if(!j->value.empty())
-                       qualifier += format("=%s", j->value);
+               string qualifier = j->name;
+               if(j->has_value)
+                       qualifier += format("=%d", j->value);
                append(qualifier);
        }
        end_sub();
index 064d217e7da7b9b47f2c90dbab59a8f5006f9a77..d435d29a37e87b2c38f3d7e09ef5ebc6243f0989 100644 (file)
@@ -52,8 +52,9 @@ void DeclarationCombiner::visit(VariableDeclaration &var)
                                {
                                        bool found = false;
                                        for(vector<Layout::Qualifier>::iterator j=ptr->layout->qualifiers.begin(); (!found && j!=ptr->layout->qualifiers.end()); ++j)
-                                               if(j->identifier==i->identifier)
+                                               if(j->name==i->name)
                                                {
+                                                       j->has_value = i->value;
                                                        j->value = i->value;
                                                        found = true;
                                                }
index 3fc703c1966d9b4a9c745f7f3ee8e49aa55c5af8..c1da9feccb2bd3f8e6c835bf91d57a21ad7b9913 100644 (file)
@@ -186,9 +186,9 @@ void Formatter::visit(Layout &layout)
        {
                if(i!=layout.qualifiers.begin())
                        append(", ");
-               append(i->identifier);
-               if(!i->value.empty())
-                       append(format("=%s", i->value));
+               append(i->name);
+               if(i->has_value)
+                       append(format("=%d", i->value));
        }
        append(')');
 }
index ec6ce6eb5e28bb060050e25121196e3e262a8f4d..4b6bd7ed08992271f5ba73318a253b8a4ec2d6ab 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"
 
@@ -118,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);
@@ -315,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;
index cdb57ebc5b48850bae81f4af251e5a841b669504..1572128ffc6bb2378e0e465433a6e01240d6bbe4 100644 (file)
@@ -41,6 +41,7 @@ private:
 
        std::string expect_type();
        std::string expect_identifier();
+       int expect_integer();
        bool check(const std::string &);
 
        static bool is_interface_qualifier(const std::string &);
index 3fb848d2ceb81a91fdc07ab19f0e072b0b71010c..70b1bb39f3547d2f3fb838fe8359d5aa979b5e1c 100644 (file)
@@ -242,9 +242,9 @@ struct Layout: Node
 {
        struct Qualifier
        {
-               // TODO the standard calls this name, not identifier
-               std::string identifier;
-               std::string value;
+               std::string name;
+               bool has_value;
+               int value;
        };
 
        std::vector<Qualifier> qualifiers;