if(const Stage *builtin = get_builtins(Stage::SHARED))
{
- for(map<string, TypeDeclaration *>::const_iterator i=builtin->types.begin(); i!=builtin->types.end(); ++i)
- global_types.insert(i->first);
+ for(const auto &kvp: builtin->types)
+ global_types.insert(kvp.first);
}
tokenizer.begin(source, name);
allow_stage_change = true;
while(!tokenizer.peek_token().empty())
if(RefPtr<Statement> statement = parse_with_recovery(&Parser::parse_global_declaration))
+ {
cur_stage->content.body.push_back(statement);
+ if(next_global_declaration)
+ {
+ cur_stage->content.body.push_back(next_global_declaration);
+ next_global_declaration = 0;
+ }
+ }
if(!errors.empty())
throw invalid_shader_source(join(errors.begin(), errors.end(), "\n"));
cur_stage = &module->stages.back();
stage_types.clear();
- for(vector<const Module *>::const_iterator i=imported_modules.begin(); i!=imported_modules.end(); ++i)
+ if(const Stage *builtin = get_builtins(stage))
{
- list<Stage>::const_iterator j = find_member((*i)->stages, stage, &Stage::type);
- if(j!=(*i)->stages.end())
+ for(const auto &kvp: builtin->types)
+ stage_types.insert(kvp.first);
+ }
+ for(const Module *m: imported_modules)
+ {
+ auto j = find_member(m->stages, stage, &Stage::type);
+ if(j!=m->stages.end())
{
- for(map<string, TypeDeclaration *>::const_iterator k=j->types.begin(); k!=j->types.end(); ++k)
- stage_types.insert(k->first);
+ for(const auto &kvp: j->types)
+ stage_types.insert(kvp.first);
}
}
}
bool Parser::is_identifier(const string &token)
{
static Regex re("^[a-zA-Z_][a-zA-Z0-9_]*$");
- return re.match(token);
+ return static_cast<bool>(re.match(token));
}
template<typename T>
}
else if(is_interface_qualifier(token) && tokenizer.peek_token(2)=="{")
{
- RefPtr<InterfaceBlock> iface = parse_interface_block();
- iface->layout = layout;
- return iface;
+ RefPtr<StructDeclaration> iface_strct = parse_interface_block();
+ VariableDeclaration *iface_var = iface_strct->block_declaration;
+ iface_var->layout = layout;
+ next_global_declaration = iface_var;
+ return iface_strct;
}
else
{
if(is_type(next) || is_qualifier(next))
return parse_variable_declaration();
else
- return parse_interface_block();
+ {
+ RefPtr<StructDeclaration> iface_strct = parse_interface_block();
+ next_global_declaration = iface_strct->block_declaration;
+ return iface_strct;
+ }
}
else if(is_qualifier(token))
return parse_variable_declaration();
{
const Module &imported_mod = mod_cache->get_module(import->module);
imported_modules.push_back(&imported_mod);
- for(map<string, TypeDeclaration *>::const_iterator i=imported_mod.shared.types.begin(); i!=imported_mod.shared.types.end(); ++i)
- global_types.insert(i->first);
+ for(const auto &kvp: imported_mod.shared.types)
+ global_types.insert(kvp.first);
}
return import;
return call;
}
+void Parser::add_type(TypeDeclaration &type)
+{
+ cur_stage->types[type.name] = &type;
+ if(cur_stage->type==Stage::SHARED)
+ global_types.insert(type.name);
+ else
+ stage_types.insert(type.name);
+}
+
RefPtr<TypeDeclaration> Parser::parse_type_declaration()
{
tokenizer.expect("typedef");
type = parse_basic_type_declaration();
tokenizer.expect(";");
- cur_stage->types[type->name] = type.get();
- if(cur_stage->type==Stage::SHARED)
- global_types.insert(type->name);
- else
- stage_types.insert(type->name);
+ add_type(*type);
return type;
}
type->sampled = true;
else if(token=="shadow")
type->shadow = true;
+ else if(token=="multisample")
+ type->multisample = true;
else
throw parse_error(tokenizer.get_location(), token, "image type attribute");
parse_block(strct->members, true, &Parser::parse_variable_declaration);
tokenizer.expect(";");
- cur_stage->types[strct->name] = strct.get();
- if(cur_stage->type==Stage::SHARED)
- global_types.insert(strct->name);
- else
- stage_types.insert(strct->name);
+ add_type(*strct);
return strct;
}
return func;
}
-RefPtr<InterfaceBlock> Parser::parse_interface_block()
+RefPtr<StructDeclaration> Parser::parse_interface_block()
{
- RefPtr<InterfaceBlock> iface = create_node<InterfaceBlock>();
-
- iface->interface = tokenizer.parse_token();
- if(!is_interface_qualifier(iface->interface))
- throw parse_error(tokenizer.get_location(), iface->interface, "an interface qualifier");
+ RefPtr<StructDeclaration> strct = create_node<StructDeclaration>();
+ RefPtr<VariableDeclaration> var = create_node<VariableDeclaration>();
- iface->block_name = expect_identifier();
- iface->members = new Block;
- parse_block(*iface->members, true, &Parser::parse_variable_declaration_with_layout);
+ var->interface = tokenizer.parse_token();
+ if(!is_interface_qualifier(var->interface))
+ throw parse_error(tokenizer.get_location(), var->interface, "an interface qualifier");
+
+ strct->block_name = expect_identifier();
+ string name_base = format("_%s_%s", var->interface, strct->block_name);
+ strct->name = name_base;
+ for(unsigned i=1; (stage_types.count(strct->name) || global_types.count(strct->name)); ++i)
+ strct->name = format("%s_%d", name_base, i);
+ var->type = strct->name;
+ parse_block(strct->members, true, &Parser::parse_variable_declaration_with_layout);
if(!check(";"))
{
- iface->instance_name = expect_identifier();
+ var->name = expect_identifier();
if(check("["))
{
- iface->array = true;
- tokenizer.expect("]");
+ var->array = true;
+ if(!check("]"))
+ {
+ var->array_size = parse_expression();
+ tokenizer.expect("]");
+ }
}
tokenizer.expect(";");
}
+ else
+ var->name = format("%s %s", var->interface, strct->block_name);
+
+ strct->block_declaration = var.release();
+ add_type(*strct);
- return iface;
+ return strct;
}
RefPtr<Conditional> Parser::parse_conditional()