+#include <msp/core/algorithm.h>
#include <msp/core/raii.h>
#include <msp/strings/format.h>
#include <msp/strings/regex.h>
#include <msp/strings/utils.h>
#include "builtin.h"
#include "glsl_error.h"
+#include "modulecache.h"
#include "parser.h"
#undef interface
namespace GL {
namespace SL {
-Parser::Parser():
+Parser::Parser(ModuleCache *s):
+ mod_cache(s),
preprocessor(tokenizer),
module(0)
{
preprocessor.signal_line.connect(sigc::mem_fun(this, &Parser::line_change));
}
-Parser::~Parser()
-{
- delete module;
-}
-
-Module &Parser::parse(const string &s, const string &n, int i)
+void Parser::parse(Module &m, const string &s, const string &n, int i)
{
+ SetForScope<Module *> set_module(module, &m);
source = s;
parse_source(n, i);
- return *module;
}
-Module &Parser::parse(IO::Base &io, const string &n, int i)
+void Parser::parse(Module &m, IO::Base &io, const string &n, int i)
{
+ SetForScope<Module *> set_module(module, &m);
source = string();
while(!io.eof())
{
source.append(buffer, len);
}
parse_source(n, i);
- return *module;
}
void Parser::parse_source(const string &name, int index)
{
- delete module;
- module = new Module;
-
cur_stage = &module->shared;
base_index = index;
source_index = index;
if(index>=0)
source_reference(1, name);
- // TODO Need to somehow get type names from imports
if(const Stage *builtin = get_builtins(Stage::SHARED))
{
- for(map<string, TypeDeclaration *>::const_iterator i=builtin->types.begin(); i!=builtin->types.end(); ++i)
- declared_types.insert(i->first);
- }
- else
- {
- declared_types.insert("void");
- declared_types.insert("bool");
- declared_types.insert("int");
- declared_types.insert("float");
+ 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"));
if(cur_stage->type!=Stage::SHARED)
module->stages.back().previous = cur_stage;
cur_stage = &module->stages.back();
+
+ stage_types.clear();
+ if(const Stage *builtin = get_builtins(stage))
+ {
+ 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(const auto &kvp: j->types)
+ stage_types.insert(kvp.first);
+ }
+ }
}
void Parser::line_change(int index, unsigned line)
bool Parser::is_sampling_qualifier(const string &token)
{
- return (token=="centroid" || token=="sample");
+ return (token=="centroid" || token=="sample" || token=="patch");
}
bool Parser::is_interpolation_qualifier(const string &token)
bool Parser::is_type(const string &token)
{
- return declared_types.count(token);
+ return global_types.count(token) || stage_types.count(token);
}
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>
if(is_interface_qualifier(token) && tokenizer.peek_token(1)==";")
{
RefPtr<InterfaceLayout> iface_lo = create_node<InterfaceLayout>();
+ iface_lo->layout.source = layout->source;
+ iface_lo->layout.line = layout->line;
iface_lo->layout.qualifiers = layout->qualifiers;
iface_lo->interface = tokenizer.parse_token();
tokenizer.expect(";");
return iface_lo;
}
+ else if(is_interface_qualifier(token) && tokenizer.peek_token(2)=="{")
+ {
+ 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
{
RefPtr<VariableDeclaration> var = parse_variable_declaration();
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();
RefPtr<Import> import = create_node<Import>();
import->module = expect_identifier();
tokenizer.expect(";");
+
+ if(mod_cache)
+ {
+ const Module &imported_mod = mod_cache->get_module(import->module);
+ imported_modules.push_back(&imported_mod);
+ for(const auto &kvp: imported_mod.shared.types)
+ global_types.insert(kvp.first);
+ }
+
return import;
}
if(token==")")
throw parse_error(tokenizer.get_location(), token, "a layout qualifier name");
- layout->qualifiers.push_back(Layout::Qualifier());
+ layout->qualifiers.push_back(token);
Layout::Qualifier &qual = layout->qualifiers.back();
- qual.name = token;
if((qual.has_value = check("=")))
{
if(isdigit(literal->token[0]))
{
// TODO have the tokenizer return the type of the token
- if(isnumrc(literal->token))
- literal->value = lexical_cast<int>(literal->token);
- else
+ if(literal->token.back()=='u')
+ literal->value = lexical_cast<unsigned>(literal->token.substr(0, literal->token.size()-1));
+ else if(literal->token.back()=='f')
+ literal->value = lexical_cast<float>(literal->token.substr(0, literal->token.size()-1));
+ else if(literal->token.find('.')!=string::npos)
literal->value = lexical_cast<float>(literal->token);
+ else
+ literal->value = lexical_cast<int>(literal->token);
}
else if(literal->token=="true" || literal->token=="false")
literal->value = (literal->token=="true");
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(";");
- declared_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(";");
- declared_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->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);
- return iface;
+ strct->block_declaration = var.release();
+ add_type(*strct);
+
+ return strct;
}
RefPtr<Conditional> Parser::parse_conditional()