From: Mikko Rasa Date: Sun, 21 Feb 2021 23:52:29 +0000 (+0200) Subject: Slightly refactor layout qualifiers in the GLSL parser X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=commitdiff_plain;h=ffdb126a55467245da08a9e6f5669f86e1899bb0 Slightly refactor layout qualifiers in the GLSL parser Their values are now stored as integers, since no qualifier takes a value of any other type. --- diff --git a/source/glsl/compatibility.cpp b/source/glsl/compatibility.cpp index 07698394..a65f6de7 100644 --- a/source/glsl/compatibility.cpp +++ b/source/glsl/compatibility.cpp @@ -264,20 +264,19 @@ void LegacyConverter::visit(VariableDeclaration &var) if(var.layout && !supports_interface_layouts()) { vector::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(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); } diff --git a/source/glsl/debug.cpp b/source/glsl/debug.cpp index 0a5b6ef3..2bc3a893 100644 --- a/source/glsl/debug.cpp +++ b/source/glsl/debug.cpp @@ -196,9 +196,9 @@ void DumpTree::visit(Layout &layout) for(vector::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); ) { vector::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(); diff --git a/source/glsl/generate.cpp b/source/glsl/generate.cpp index 064d217e..d435d29a 100644 --- a/source/glsl/generate.cpp +++ b/source/glsl/generate.cpp @@ -52,8 +52,9 @@ void DeclarationCombiner::visit(VariableDeclaration &var) { bool found = false; for(vector::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; } diff --git a/source/glsl/output.cpp b/source/glsl/output.cpp index 3fc703c1..c1da9fec 100644 --- a/source/glsl/output.cpp +++ b/source/glsl/output.cpp @@ -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(')'); } diff --git a/source/glsl/parser.cpp b/source/glsl/parser.cpp index ec6ce6eb..4b6bd7ed 100644 --- a/source/glsl/parser.cpp +++ b/source/glsl/parser.cpp @@ -1,5 +1,6 @@ #include #include +#include #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(token); +} + bool Parser::check(const string &token) { bool result = (tokenizer.peek_token()==token); @@ -315,10 +324,10 @@ RefPtr 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; diff --git a/source/glsl/parser.h b/source/glsl/parser.h index cdb57ebc..1572128f 100644 --- a/source/glsl/parser.h +++ b/source/glsl/parser.h @@ -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 &); diff --git a/source/glsl/syntax.h b/source/glsl/syntax.h index 3fb848d2..70b1bb39 100644 --- a/source/glsl/syntax.h +++ b/source/glsl/syntax.h @@ -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 qualifiers;