]> git.tdb.fi Git - libs/gl.git/commitdiff
Refactor creation of builtin GLSL types
authorMikko Rasa <tdb@tdb.fi>
Fri, 23 Apr 2021 22:40:30 +0000 (01:40 +0300)
committerMikko Rasa <tdb@tdb.fi>
Fri, 23 Apr 2021 22:40:30 +0000 (01:40 +0300)
source/glsl/builtin.cpp
source/glsl/parser.cpp

index b9fe487c7bb2f810360918167df628d05a5acbfb..83e480e5cf417b5943abb7841ebcb32256f8136e 100644 (file)
@@ -9,11 +9,16 @@ namespace Msp {
 namespace GL {
 namespace SL {
 
-void populate_types(Stage &stage)
+void add_builtin_type(Stage &stage, const std::string &name, BasicTypeDeclaration::Kind kind, unsigned size, unsigned sign)
 {
-       for(NodeList<Statement>::const_iterator i=stage.content.body.begin(); i!=stage.content.body.end(); ++i)
-               if(TypeDeclaration *type = dynamic_cast<TypeDeclaration *>(i->get()))
-                       stage.types[type->name] = type;
+       RefPtr<BasicTypeDeclaration> type = new BasicTypeDeclaration;
+       type->source = BUILTIN_SOURCE;
+       type->name = name;
+       type->kind = kind;
+       type->size = size;
+       type->sign = sign;
+       stage.content.body.push_back(type);
+       stage.types[name] = type.get();
 }
 
 Module *get_builtins_module()
@@ -28,53 +33,23 @@ Module *get_builtins_module()
                if(!io)
                        return 0;
 
-               RefPtr<Module> module = new Module;
-               Parser parser(0);
-               parser.parse(*module, *io, "<builtin>", BUILTIN_SOURCE);
-
-               NodeList<Statement> &shared_body = module->shared.content.body;
-               NodeList<Statement>::iterator insert_point = shared_body.begin();
-
-               RefPtr<BasicTypeDeclaration> type = new BasicTypeDeclaration;
-               type->source = BUILTIN_SOURCE;
-               type->name = "void";
-               type->kind = BasicTypeDeclaration::VOID;
-               shared_body.insert(insert_point, type);
-
-               type = new BasicTypeDeclaration;
-               type->source = BUILTIN_SOURCE;
-               type->name = "bool";
-               type->size = 1;
-               type->kind = BasicTypeDeclaration::BOOL;
-               shared_body.insert(insert_point, type);
-
-               type = new BasicTypeDeclaration;
-               type->source = BUILTIN_SOURCE;
-               type->name = "int";
-               type->size = 32;
-               type->kind = BasicTypeDeclaration::INT;
-               shared_body.insert(insert_point, type);
-
-               type = new BasicTypeDeclaration;
-               type->source = BUILTIN_SOURCE;
-               type->name = "uint";
-               type->size = 32;
-               type->sign = false;
-               type->kind = BasicTypeDeclaration::INT;
-               shared_body.insert(insert_point, type);
-
-               type = new BasicTypeDeclaration;
-               type->source = BUILTIN_SOURCE;
-               type->name = "float";
-               type->size = 32;
-               type->kind = BasicTypeDeclaration::FLOAT;
-               shared_body.insert(insert_point, type);
-
-               populate_types(module->shared);
-               for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
-                       populate_types(*i);
-
-               builtins_module = module.release();
+               builtins_module = new Module;
+               add_builtin_type(builtins_module->shared, "void", BasicTypeDeclaration::VOID, 0, true);
+               add_builtin_type(builtins_module->shared, "bool", BasicTypeDeclaration::BOOL, 1, true);
+               add_builtin_type(builtins_module->shared, "int", BasicTypeDeclaration::INT, 32, true);
+               add_builtin_type(builtins_module->shared, "uint", BasicTypeDeclaration::INT, 32, false);
+               add_builtin_type(builtins_module->shared, "float", BasicTypeDeclaration::FLOAT, 32, true);
+
+               try
+               {
+                       Parser parser(0);
+                       parser.parse(*builtins_module, *io, "<builtin>", BUILTIN_SOURCE);
+               }
+               catch(...)
+               {
+                       builtins_module = 0;
+                       throw;
+               }
        }
        return builtins_module.get();
 }
index e2a6e1fb6c8dc65664cecd819e527995c7fb119a..626a65424532e189aa30e6b376f5ca041425ef24 100644 (file)
@@ -61,14 +61,6 @@ void Parser::parse_source(const string &name, int index)
                for(map<string, TypeDeclaration *>::const_iterator i=builtin->types.begin(); i!=builtin->types.end(); ++i)
                        global_types.insert(i->first);
        }
-       else
-       {
-               global_types.insert("void");
-               global_types.insert("bool");
-               global_types.insert("int");
-               global_types.insert("uint");
-               global_types.insert("float");
-       }
 
        tokenizer.begin(source, name);
        allow_stage_change = true;