namespace GL {
namespace SL {
-Module *create_builtins_module()
+Module *get_builtins_module()
{
- Parser parser;
- Module *module = new Module(parser.parse(builtins_src, "<builtin>"));
- for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
+ static RefPtr<Module> builtins_module;
+ static bool initialized = false;
+ if(!initialized)
{
- VariableResolver().visit(i->content);
- for(map<string, VariableDeclaration *>::iterator j=i->content.variables.begin(); j!=i->content.variables.end(); ++j)
- j->second->linked_declaration = j->second;
- }
- return module;
-}
+ initialized = true;
-Module &get_builtins_module()
-{
- static RefPtr<Module> builtins_module = create_builtins_module();
- return *builtins_module;
+ Parser parser;
+ Module *module = new Module(parser.parse(builtins_src, "<builtin>"));
+ for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
+ {
+ VariableResolver().apply(*i);
+ for(map<string, VariableDeclaration *>::iterator j=i->content.variables.begin(); j!=i->content.variables.end(); ++j)
+ j->second->linked_declaration = j->second;
+ }
+
+ builtins_module = module;
+ }
+ return builtins_module.get();
}
Stage *get_builtins(Stage::Type type)
{
- Module &module = get_builtins_module();
- for(list<Stage>::iterator i=module.stages.begin(); i!=module.stages.end(); ++i)
+ Module *module = get_builtins_module();
+ if(!module)
+ return 0;
+
+ for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
if(i->type==type)
return &*i;
return 0;