]> git.tdb.fi Git - libs/gl.git/commitdiff
Create array type declarations as necessary
authorMikko Rasa <tdb@tdb.fi>
Fri, 5 Mar 2021 21:08:41 +0000 (23:08 +0200)
committerMikko Rasa <tdb@tdb.fi>
Fri, 5 Mar 2021 23:00:31 +0000 (01:00 +0200)
source/glsl/generate.cpp
source/glsl/generate.h
source/glsl/output.cpp

index 07181d3583e69fbc1067292bbaddc7855c58a5ac..c9803acc3b2afd5698b29f66c8d69a110ddfc18c 100644 (file)
@@ -143,6 +143,16 @@ TypeDeclaration *TypeResolver::resolve_type(const string &name)
                return 0;
 }
 
                return 0;
 }
 
+void TypeResolver::visit(Block &block)
+{
+       for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
+       {
+               if(!block.parent)
+                       type_insert_point = i;
+               (*i)->visit(*this);
+       }
+}
+
 void TypeResolver::visit(BasicTypeDeclaration &type)
 {
        type.base_type = resolve_type(type.base);
 void TypeResolver::visit(BasicTypeDeclaration &type)
 {
        type.base_type = resolve_type(type.base);
@@ -157,6 +167,8 @@ void TypeResolver::visit(BasicTypeDeclaration &type)
 
        if(type.kind==BasicTypeDeclaration::ALIAS && type.base_type)
                alias_map[&type] = type.base_type;
 
        if(type.kind==BasicTypeDeclaration::ALIAS && type.base_type)
                alias_map[&type] = type.base_type;
+       else if(type.kind==BasicTypeDeclaration::ARRAY && type.base_type)
+               array_types[type.base_type] = &type;
 
        stage->types.insert(make_pair(type.name, &type));
 }
 
        stage->types.insert(make_pair(type.name, &type));
 }
@@ -175,7 +187,26 @@ void TypeResolver::visit(StructDeclaration &strct)
 
 void TypeResolver::visit(VariableDeclaration &var)
 {
 
 void TypeResolver::visit(VariableDeclaration &var)
 {
-       var.type_declaration = resolve_type(var.type);
+       TypeDeclaration *type = resolve_type(var.type);
+       if(var.array && type)
+       {
+               map<TypeDeclaration *, TypeDeclaration *>::iterator i = array_types.find(type);
+               if(i==array_types.end())
+               {
+                       BasicTypeDeclaration *array = new BasicTypeDeclaration;
+                       array->source = BUILTIN_SOURCE;
+                       array->name = type->name+"[]";
+                       array->kind = BasicTypeDeclaration::ARRAY;
+                       array->base = type->name;
+                       array->base_type = type;
+                       stage->content.body.insert(type_insert_point, array);
+                       array->visit(*this);
+                       type = array;
+               }
+               else
+                       type = i->second;
+       }
+       var.type_declaration = type;
 }
 
 void TypeResolver::visit(FunctionDeclaration &func)
 }
 
 void TypeResolver::visit(FunctionDeclaration &func)
index b660a0957447f1507fb9aa1b0a8ed5e5dedf28f5..63f1b0770ba435b287157eef975514b7e41cab9d 100644 (file)
@@ -61,6 +61,8 @@ class TypeResolver: private TraversingVisitor
 private:
        Stage *stage;
        std::map<TypeDeclaration *, TypeDeclaration *> alias_map;
 private:
        Stage *stage;
        std::map<TypeDeclaration *, TypeDeclaration *> alias_map;
+       std::map<TypeDeclaration *, TypeDeclaration *> array_types;
+       NodeList<Statement>::iterator type_insert_point;
 
 public:
        TypeResolver();
 
 public:
        TypeResolver();
@@ -69,6 +71,7 @@ public:
 
 private:
        TypeDeclaration *resolve_type(const std::string &);
 
 private:
        TypeDeclaration *resolve_type(const std::string &);
+       virtual void visit(Block &);
        virtual void visit(BasicTypeDeclaration &);
        virtual void visit(ImageTypeDeclaration &);
        virtual void visit(StructDeclaration &);
        virtual void visit(BasicTypeDeclaration &);
        virtual void visit(ImageTypeDeclaration &);
        virtual void visit(StructDeclaration &);
index f440fa8c01cb34ea31cbd9742020165f4b61546e..d44dbff99de3f1c0e6b59f244169ff18072f61d9 100644 (file)
@@ -243,7 +243,10 @@ void Formatter::visit(VariableDeclaration &var)
        }
        if(!var.precision.empty())
                append(format("%s ", var.precision));
        }
        if(!var.precision.empty())
                append(format("%s ", var.precision));
-       append(format("%s %s", var.type_declaration->name, var.name));
+       string type_name = var.type_declaration->name;
+       if(var.array)
+               type_name = type_name.substr(0, type_name.find('['));
+       append(format("%s %s", type_name, var.name));
        if(var.array)
        {
                append('[');
        if(var.array)
        {
                append('[');