]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/builtin.cpp
Check the flat qualifier from the correct member
[libs/gl.git] / source / glsl / builtin.cpp
index f60d2a8a1d0736ff9d2d171b1a583fd39bedc879..217509a20b74c61e83091feda6dc856dd9a3cfcc 100644 (file)
@@ -1,3 +1,4 @@
+#include <msp/core/algorithm.h>
 #include <msp/gl/resources.h>
 #include <msp/io/seekable.h>
 #include "builtin.h"
@@ -9,11 +10,16 @@ namespace Msp {
 namespace GL {
 namespace SL {
 
-void populate_types(Stage &stage)
+void add_builtin_type(Stage &stage, const 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,44 +34,23 @@ Module *get_builtins_module()
                if(!io)
                        return 0;
 
-               Parser parser;
-               Module *module = new Module(parser.parse(*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 = "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;
+               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();
 }
@@ -78,10 +63,8 @@ const Stage *get_builtins(Stage::Type type)
 
        if(type==Stage::SHARED)
                return &module->shared;
-       for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
-               if(i->type==type)
-                       return &*i;
-       return 0;
+       auto i = find_member(module->stages, type, &Stage::type);
+       return (i!=module->stages.end() ? &*i : 0);
 }
 
 } // namespace SL