]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/parser.cpp
Replace some local pointers with references in VulkanCommands
[libs/gl.git] / source / glsl / parser.cpp
1 #include <msp/core/algorithm.h>
2 #include <msp/core/raii.h>
3 #include <msp/strings/format.h>
4 #include <msp/strings/regex.h>
5 #include <msp/strings/utils.h>
6 #include "builtin.h"
7 #include "glsl_error.h"
8 #include "modulecache.h"
9 #include "parser.h"
10
11 #undef interface
12
13 using namespace std;
14
15 namespace Msp {
16 namespace GL {
17 namespace SL {
18
19 Parser::Parser(ModuleCache *s):
20         mod_cache(s),
21         preprocessor(tokenizer),
22         module(0)
23 {
24         tokenizer.signal_preprocess.connect(sigc::mem_fun(&preprocessor, &Preprocessor::preprocess));
25         preprocessor.signal_version.connect(sigc::mem_fun(this, &Parser::set_required_version));
26         preprocessor.signal_source.connect(sigc::mem_fun(this, &Parser::source_reference));
27         preprocessor.signal_stage_change.connect(sigc::mem_fun(this, &Parser::stage_change));
28         preprocessor.signal_line.connect(sigc::mem_fun(this, &Parser::line_change));
29 }
30
31 void Parser::parse(Module &m, const string &s, const string &n, int i)
32 {
33         SetForScope<Module *> set_module(module, &m);
34         source = s;
35         parse_source(n, i);
36 }
37
38 void Parser::parse(Module &m, IO::Base &io, const string &n, int i)
39 {
40         SetForScope<Module *> set_module(module, &m);
41         source = string();
42         while(!io.eof())
43         {
44                 char buffer[4096];
45                 unsigned len = io.read(buffer, sizeof(buffer));
46                 source.append(buffer, len);
47         }
48         parse_source(n, i);
49 }
50
51 void Parser::parse_source(const string &name, int index)
52 {
53         cur_stage = &module->shared;
54         base_index = index;
55         source_index = index;
56         if(index>=0)
57                 source_reference(1, name);
58
59         if(const Stage *builtin = get_builtins(Stage::SHARED))
60         {
61                 for(const auto &kvp: builtin->types)
62                         global_types.insert(kvp.first);
63         }
64
65         tokenizer.begin(source, name);
66         allow_stage_change = true;
67         while(!tokenizer.peek_token().empty())
68                 if(RefPtr<Statement> statement = parse_with_recovery(&Parser::parse_global_declaration))
69                 {
70                         cur_stage->content.body.push_back(statement);
71                         if(next_global_declaration)
72                         {
73                                 cur_stage->content.body.push_back(next_global_declaration);
74                                 next_global_declaration = 0;
75                         }
76                 }
77
78         if(!errors.empty())
79                 throw invalid_shader_source(join(errors.begin(), errors.end(), "\n"));
80 }
81
82 void Parser::set_required_version(const Version &ver)
83 {
84         cur_stage->required_features.glsl_version = ver;
85 }
86
87 void Parser::source_reference(unsigned index, const string &name)
88 {
89         if(index<1 || base_index<0)
90                 throw invalid_shader_source(tokenizer.get_location(), "Invalid source reference");
91
92         module->source_map.set_name(base_index+index-1, name);
93 }
94
95 void Parser::stage_change(Stage::Type stage)
96 {
97         if(!allow_stage_change)
98                 throw invalid_shader_source(tokenizer.get_location(), "Changing stage not allowed here");
99         else if(stage<=cur_stage->type)
100                 throw invalid_shader_source(tokenizer.get_location(), "Stage '%s' not allowed here", Stage::get_stage_name(stage));
101
102         module->stages.push_back(stage);
103
104         if(cur_stage->type!=Stage::SHARED)
105                 module->stages.back().previous = cur_stage;
106         cur_stage = &module->stages.back();
107
108         stage_types.clear();
109         if(const Stage *builtin = get_builtins(stage))
110         {
111                 for(const auto &kvp: builtin->types)
112                         stage_types.insert(kvp.first);
113         }
114         for(const Module *m: imported_modules)
115         {
116                 auto j = find_member(m->stages, stage, &Stage::type);
117                 if(j!=m->stages.end())
118                 {
119                         for(const auto &kvp: j->types)
120                                 stage_types.insert(kvp.first);
121                 }
122         }
123 }
124
125 void Parser::line_change(int index, unsigned line)
126 {
127         if(index>0)
128                 source_index = base_index+index-1;
129         else if(index==0)
130                 source_index = 0;
131         else
132                 index = source_index;
133
134         string name = module->source_map.get_name(index);
135         if(name.empty())
136                 name = format("<%d>", index);
137         tokenizer.set_location(Location(name, line));
138 }
139
140 string Parser::expect_type()
141 {
142         string token = tokenizer.parse_token();
143         if(!is_type(token))
144                 throw parse_error(tokenizer.get_location(), token, "a type");
145         return token;
146 }
147
148 string Parser::expect_identifier()
149 {
150         string token = tokenizer.parse_token();
151         if(!is_identifier(token))
152                 throw parse_error(tokenizer.get_location(), token, "an identifier");
153         return token;
154 }
155
156 int Parser::expect_integer()
157 {
158         string token = tokenizer.parse_token();
159         if(!isnumrc(token))
160                 throw parse_error(tokenizer.get_location(), token, "an integer literal");
161         return lexical_cast<int>(token);
162 }
163
164 bool Parser::check(const string &token)
165 {
166         bool result = (tokenizer.peek_token()==token);
167         if(result)
168                 tokenizer.parse_token();
169         return result;
170 }
171
172 bool Parser::is_interface_qualifier(const string &token)
173 {
174         return (token=="uniform" || token=="in" || token=="out");
175 }
176
177 bool Parser::is_sampling_qualifier(const string &token)
178 {
179         return (token=="centroid" || token=="sample");
180 }
181
182 bool Parser::is_interpolation_qualifier(const string &token)
183 {
184         return (token=="smooth" || token=="flat" || token=="noperspective");
185 }
186
187 bool Parser::is_precision_qualifier(const string &token)
188 {
189         return (token=="highp" || token=="mediump" || token=="lowp");
190 }
191
192 bool Parser::is_qualifier(const string &token)
193 {
194         return (token=="const" ||
195                 is_interface_qualifier(token) ||
196                 is_sampling_qualifier(token) ||
197                 is_interpolation_qualifier(token) ||
198                 is_precision_qualifier(token));
199 }
200
201 bool Parser::is_type(const string &token)
202 {
203         return global_types.count(token) || stage_types.count(token);
204 }
205
206 bool Parser::is_identifier(const string &token)
207 {
208         static Regex re("^[a-zA-Z_][a-zA-Z0-9_]*$");
209         return static_cast<bool>(re.match(token));
210 }
211
212 template<typename T>
213 RefPtr<T> Parser::create_node()
214 {
215         RefPtr<T> node = new T;
216         node->source = source_index;
217         node->line = tokenizer.get_location().line;
218         return node;
219 }
220
221 template<typename T>
222 RefPtr<T> Parser::parse_with_recovery(RefPtr<T> (Parser::*parse_func)())
223 {
224         tokenizer.clear_progress_mark();
225         try
226         {
227                 return (this->*parse_func)();
228         }
229         catch(const invalid_shader_source &exc)
230         {
231                 errors.push_back(exc.what());
232         }
233
234         if(tokenizer.get_last_token()!=";" || !tokenizer.get_progress_mark())
235         {
236                 unsigned scope_level = 0;
237                 while(1)
238                 {
239                         if(tokenizer.peek_token()=="}" && scope_level==0)
240                         {
241                                 if(!tokenizer.get_progress_mark())
242                                         tokenizer.parse_token();
243                                 break;
244                         }
245
246                         string token = tokenizer.parse_token();
247                         if(token=="}")
248                         {
249                                 --scope_level;
250                                 if(scope_level==0)
251                                         break;
252                         }
253                         else if(token=="{")
254                                 ++scope_level;
255                         else if(token==";" && scope_level==0)
256                                 break;
257                         else if(token.empty())
258                                 break;
259                 }
260         }
261
262         return RefPtr<T>();
263 }
264
265 RefPtr<Statement> Parser::parse_global_declaration()
266 {
267         string token = tokenizer.peek_token();
268         SetFlag disallow(allow_stage_change, false);
269
270         if(token=="import")
271                 return parse_import();
272         else if(token=="precision")
273                 return parse_precision();
274         else if(token=="layout")
275         {
276                 RefPtr<Layout> layout = parse_layout();
277                 token = tokenizer.peek_token();
278                 if(is_interface_qualifier(token) && tokenizer.peek_token(1)==";")
279                 {
280                         RefPtr<InterfaceLayout> iface_lo = create_node<InterfaceLayout>();
281                         iface_lo->layout.source = layout->source;
282                         iface_lo->layout.line = layout->line;
283                         iface_lo->layout.qualifiers = layout->qualifiers;
284                         iface_lo->interface = tokenizer.parse_token();
285                         tokenizer.expect(";");
286                         return iface_lo;
287                 }
288                 else if(is_interface_qualifier(token) && tokenizer.peek_token(2)=="{")
289                 {
290                         RefPtr<StructDeclaration> iface_strct = parse_interface_block();
291                         VariableDeclaration *iface_var = iface_strct->block_declaration;
292                         iface_var->layout = layout;
293                         next_global_declaration = iface_var;
294                         return iface_strct;
295                 }
296                 else
297                 {
298                         RefPtr<VariableDeclaration> var = parse_variable_declaration();
299                         var->layout = layout;
300                         return var;
301                 }
302         }
303         else if(token=="typedef")
304                 return parse_type_declaration();
305         else if(token=="struct")
306                 return parse_struct_declaration();
307         else if(is_interface_qualifier(token))
308         {
309                 string next = tokenizer.peek_token(1);
310                 if(is_type(next) || is_qualifier(next))
311                         return parse_variable_declaration();
312                 else
313                 {
314                         RefPtr<StructDeclaration> iface_strct = parse_interface_block();
315                         next_global_declaration = iface_strct->block_declaration;
316                         return iface_strct;
317                 }
318         }
319         else if(is_qualifier(token))
320                 return parse_variable_declaration();
321         else if(token=="virtual")
322                 return parse_function_declaration();
323         else if(is_type(token))
324         {
325                 if(tokenizer.peek_token(2)=="(")
326                         return parse_function_declaration();
327                 else
328                         return parse_variable_declaration();
329         }
330         else if(token.empty())
331                 return 0;
332         else
333                 throw parse_error(tokenizer.get_location(), token, "a global declaration");
334 }
335
336 RefPtr<Statement> Parser::parse_statement()
337 {
338         string token = tokenizer.peek_token();
339         if(token=="if")
340                 return parse_conditional();
341         else if(token=="for")
342                 return parse_for();
343         else if(token=="while")
344                 return parse_while();
345         else if(token=="passthrough")
346                 return parse_passthrough();
347         else if(token=="return")
348                 return parse_return();
349         else if(token=="break" || token=="continue" || token=="discard")
350         {
351                 RefPtr<Jump> jump = create_node<Jump>();
352                 jump->keyword = tokenizer.parse_token();
353                 tokenizer.expect(";");
354
355                 return jump;
356         }
357         else if(is_qualifier(token) || is_type(token))
358                 return parse_variable_declaration();
359         else if(token==";")
360         {
361                 tokenizer.parse_token();
362                 throw invalid_shader_source(tokenizer.get_location(), "Empty statement not allowed");
363         }
364         else if(!token.empty())
365         {
366                 RefPtr<ExpressionStatement> expr = create_node<ExpressionStatement>();
367                 expr->expression = parse_expression();
368                 tokenizer.expect(";");
369
370                 return expr;
371         }
372         else
373                 throw parse_error(tokenizer.get_location(), token, "a statement");
374 }
375
376 RefPtr<Import> Parser::parse_import()
377 {
378         if(cur_stage->type!=Stage::SHARED)
379                 throw invalid_shader_source(tokenizer.get_location(), "Imports are only allowed in the shared section");
380
381         tokenizer.expect("import");
382         RefPtr<Import> import = create_node<Import>();
383         import->module = expect_identifier();
384         tokenizer.expect(";");
385
386         if(mod_cache)
387         {
388                 const Module &imported_mod = mod_cache->get_module(import->module);
389                 imported_modules.push_back(&imported_mod);
390                 for(const auto &kvp: imported_mod.shared.types)
391                         global_types.insert(kvp.first);
392         }
393
394         return import;
395 }
396
397 RefPtr<Precision> Parser::parse_precision()
398 {
399         tokenizer.expect("precision");
400         RefPtr<Precision> precision = create_node<Precision>();
401
402         precision->precision = tokenizer.parse_token();
403         if(!is_precision_qualifier(precision->precision))
404                 throw parse_error(tokenizer.get_location(), precision->precision, "a precision qualifier");
405
406         // TODO Add validation for this
407         precision->type = expect_type();
408
409         tokenizer.expect(";");
410
411         return precision;
412 }
413
414 RefPtr<Layout> Parser::parse_layout()
415 {
416         tokenizer.expect("layout");
417         tokenizer.expect("(");
418         RefPtr<Layout> layout = create_node<Layout>();
419         while(1)
420         {
421                 string token = tokenizer.parse_token();
422                 if(token==")")
423                         throw parse_error(tokenizer.get_location(), token, "a layout qualifier name");
424
425                 layout->qualifiers.push_back(token);
426                 Layout::Qualifier &qual = layout->qualifiers.back();
427
428                 if((qual.has_value = check("=")))
429                 {
430                         if(qual.name=="constant_id" && tokenizer.peek_token()=="auto")
431                         {
432                                 qual.value = -1;
433                                 tokenizer.parse_token();
434                         }
435                         else
436                                 qual.value = expect_integer();
437                 }
438
439                 if(tokenizer.peek_token()==")")
440                         break;
441
442                 tokenizer.expect(",");
443         }
444         tokenizer.expect(")");
445
446         return layout;
447 }
448
449 template<typename T>
450 void Parser::parse_block(Block &block, bool require_braces, RefPtr<T> (Parser::*parse_content)())
451 {
452         bool have_braces = (require_braces || tokenizer.peek_token()=="{");
453         if(have_braces)
454                 tokenizer.expect("{");
455
456         if(have_braces)
457         {
458                 while(tokenizer.peek_token()!="}")
459                         if(RefPtr<Statement> node = parse_with_recovery(parse_content))
460                                 block.body.push_back(node);
461         }
462         else
463                 block.body.push_back((this->*parse_content)());
464
465         block.use_braces = (require_braces || block.body.size()!=1);
466
467         if(have_braces)
468                 tokenizer.expect("}");
469 }
470
471 RefPtr<Expression> Parser::parse_expression(const Operator *outer_oper)
472 {
473         unsigned outer_precedence = (outer_oper ? outer_oper->precedence+(outer_oper->assoc==Operator::RIGHT_TO_LEFT) : 20);
474         RefPtr<Expression> left;
475         VariableReference *left_var = 0;
476         while(1)
477         {
478                 string token = tokenizer.peek_token();
479
480                 const Operator *oper = 0;
481                 for(const Operator *i=Operator::operators; (!oper && i->type); ++i)
482                         if(token==i->token && (!left || i->type!=Operator::PREFIX) && (left || i->type!=Operator::POSTFIX))
483                                 oper = i;
484
485                 bool lower_precedence = (oper && oper->type!=Operator::PREFIX && oper->precedence>=outer_precedence);
486                 if(token==";" || token==")" || token=="]" || token=="," || token==":" || lower_precedence)
487                 {
488                         if(left)
489                                 return left;
490                         else
491                                 throw parse_error(tokenizer.get_location(), token, "an expression");
492                 }
493                 else if(left)
494                 {
495                         if(token=="(")
496                         {
497                                 if(!left_var)
498                                         throw invalid_shader_source(tokenizer.get_location(), "Syntax error before '(': function name must be an identifier");
499                                 left = parse_function_call(*left_var);
500                         }
501                         else if(token==".")
502                         {
503                                 RefPtr<MemberAccess> memacc = create_node<MemberAccess>();
504                                 memacc->left = left;
505                                 memacc->oper = oper;
506                                 tokenizer.parse_token();
507                                 memacc->member = expect_identifier();
508                                 left = memacc;
509                         }
510                         else if(oper && oper->type==Operator::POSTFIX)
511                         {
512                                 RefPtr<UnaryExpression> unary = create_node<UnaryExpression>();
513                                 unary->oper = oper;
514                                 tokenizer.parse_token();
515                                 unary->expression = left;
516                                 left = unary;
517                         }
518                         else if(oper && oper->type==Operator::BINARY)
519                                 left = parse_binary(left, *oper);
520                         else if(oper && oper->type==Operator::TERNARY)
521                                 left = parse_ternary(left, *oper);
522                         else
523                                 throw parse_error(tokenizer.get_location(), token, "an operator");
524                         left_var = 0;
525                 }
526                 else
527                 {
528                         if(token=="(")
529                         {
530                                 tokenizer.parse_token();
531                                 left = parse_expression();
532                                 tokenizer.expect(")");
533                         }
534                         else if(isdigit(token[0]) || token=="true" || token=="false")
535                                 left = parse_literal();
536                         else if(is_identifier(token))
537                         {
538                                 RefPtr<VariableReference> var = create_node<VariableReference>();
539                                 var->name = expect_identifier();
540                                 left = var;
541                                 left_var = var.get();
542                         }
543                         else if(oper && oper->type==Operator::PREFIX)
544                         {
545                                 RefPtr<UnaryExpression> unary = create_node<UnaryExpression>();
546                                 unary->oper = oper;
547                                 tokenizer.parse_token();
548                                 unary->expression = parse_expression(oper);
549                                 left = unary;
550                         }
551                         else
552                                 throw parse_error(tokenizer.get_location(), token, "an expression");
553                 }
554         }
555 }
556
557 RefPtr<Literal> Parser::parse_literal()
558 {
559         RefPtr<Literal> literal = create_node<Literal>();
560         literal->token = tokenizer.parse_token();
561         if(isdigit(literal->token[0]))
562         {
563                 // TODO have the tokenizer return the type of the token
564                 if(literal->token.back()=='u')
565                         literal->value = lexical_cast<unsigned>(literal->token.substr(0, literal->token.size()-1));
566                 else if(literal->token.back()=='f')
567                         literal->value = lexical_cast<float>(literal->token.substr(0, literal->token.size()-1));
568                 else if(literal->token.find('.')!=string::npos)
569                         literal->value = lexical_cast<float>(literal->token);
570                 else
571                         literal->value = lexical_cast<int>(literal->token);
572         }
573         else if(literal->token=="true" || literal->token=="false")
574                 literal->value = (literal->token=="true");
575         else
576                 throw parse_error(tokenizer.get_location(), literal->token, "a literal");
577
578         return literal;
579 }
580
581 RefPtr<BinaryExpression> Parser::parse_binary(const RefPtr<Expression> &left, const Operator &oper)
582 {
583         RefPtr<BinaryExpression> binary = (oper.precedence==16 ?
584                 static_cast<RefPtr<BinaryExpression> >(create_node<Assignment>()) : create_node<BinaryExpression>());
585         binary->left = left;
586         binary->oper = &oper;
587         tokenizer.expect(oper.token);
588         if(oper.token2[0])
589         {
590                 binary->right = parse_expression();
591                 tokenizer.expect(oper.token2);
592         }
593         else
594                 binary->right = parse_expression(&oper);
595         return binary;
596 }
597
598 RefPtr<TernaryExpression> Parser::parse_ternary(const RefPtr<Expression> &cond, const Operator &oper)
599 {
600         RefPtr<TernaryExpression> ternary = create_node<TernaryExpression>();
601         ternary->condition = cond;
602         ternary->oper = &oper;
603         tokenizer.expect("?");
604         ternary->true_expr = parse_expression(&oper);
605         tokenizer.expect(":");
606         ternary->false_expr = parse_expression(&oper);
607         return ternary;
608 }
609
610 RefPtr<FunctionCall> Parser::parse_function_call(const VariableReference &var)
611 {
612         RefPtr<FunctionCall> call = create_node<FunctionCall>();
613         call->name = var.name;
614         call->oper = &Operator::get_operator("(", Operator::POSTFIX);
615         tokenizer.expect("(");
616         while(tokenizer.peek_token()!=")")
617         {
618                 if(!call->arguments.empty())
619                         tokenizer.expect(",");
620                 call->arguments.push_back(parse_expression());
621         }
622         tokenizer.expect(")");
623         return call;
624 }
625
626 void Parser::add_type(TypeDeclaration &type)
627 {
628         cur_stage->types[type.name] = &type;
629         if(cur_stage->type==Stage::SHARED)
630                 global_types.insert(type.name);
631         else
632                 stage_types.insert(type.name);
633 }
634
635 RefPtr<TypeDeclaration> Parser::parse_type_declaration()
636 {
637         tokenizer.expect("typedef");
638
639         RefPtr<TypeDeclaration> type;
640         if(tokenizer.peek_token()=="image")
641                 type = parse_image_type_declaration();
642         else
643                 type = parse_basic_type_declaration();
644
645         tokenizer.expect(";");
646         add_type(*type);
647         return type;
648 }
649
650 RefPtr<BasicTypeDeclaration> Parser::parse_basic_type_declaration()
651 {
652         RefPtr<BasicTypeDeclaration> type = create_node<BasicTypeDeclaration>();
653
654         if(tokenizer.peek_token()=="vector")
655         {
656                 type->kind = BasicTypeDeclaration::VECTOR;
657
658                 tokenizer.parse_token();
659                 tokenizer.expect("(");
660                 type->size = expect_integer();
661                 tokenizer.expect(")");
662         }
663
664         type->base = expect_type();
665         type->name = expect_identifier();
666
667         if(type->kind==BasicTypeDeclaration::ALIAS && check("["))
668         {
669                 type->kind = BasicTypeDeclaration::ARRAY;
670                 tokenizer.expect("]");
671         }
672
673         return type;
674 }
675
676 RefPtr<ImageTypeDeclaration> Parser::parse_image_type_declaration()
677 {
678         tokenizer.expect("image");
679         tokenizer.expect("(");
680
681         RefPtr<ImageTypeDeclaration> type = create_node<ImageTypeDeclaration>();
682         while(1)
683         {
684                 string token = tokenizer.parse_token();
685                 if(token=="dimensions")
686                 {
687                         tokenizer.expect("=");
688                         token = tokenizer.parse_token();
689                         if(token=="1")
690                                 type->dimensions = ImageTypeDeclaration::ONE;
691                         else if(token=="2")
692                                 type->dimensions = ImageTypeDeclaration::TWO;
693                         else if(token=="3")
694                                 type->dimensions = ImageTypeDeclaration::THREE;
695                         else if(token=="cube")
696                                 type->dimensions = ImageTypeDeclaration::CUBE;
697                         else
698                                 throw parse_error(tokenizer.get_location(), token, "dimensions");
699
700                         if(check("["))
701                         {
702                                 type->array = true;
703                                 tokenizer.expect("]");
704                         }
705                 }
706                 else if(token=="sampled")
707                         type->sampled = true;
708                 else if(token=="shadow")
709                         type->shadow = true;
710                 else
711                         throw parse_error(tokenizer.get_location(), token, "image type attribute");
712
713                 token = tokenizer.peek_token();
714                 if(token==")")
715                         break;
716
717                 tokenizer.expect(",");
718         }
719         tokenizer.expect(")");
720
721         type->base = expect_type();
722         type->name = expect_identifier();
723
724         return type;
725 }
726
727 RefPtr<StructDeclaration> Parser::parse_struct_declaration()
728 {
729         tokenizer.expect("struct");
730         RefPtr<StructDeclaration> strct = create_node<StructDeclaration>();
731
732         strct->name = expect_identifier();
733         parse_block(strct->members, true, &Parser::parse_variable_declaration);
734         tokenizer.expect(";");
735
736         add_type(*strct);
737         return strct;
738 }
739
740 RefPtr<VariableDeclaration> Parser::parse_variable_declaration()
741 {
742         RefPtr<VariableDeclaration> var = create_node<VariableDeclaration>();
743
744         string token = tokenizer.peek_token();
745         while(is_qualifier(token))
746         {
747                 tokenizer.parse_token();
748                 if(is_interface_qualifier(token))
749                         var->interface = token;
750                 else if(is_sampling_qualifier(token))
751                         var->sampling = token;
752                 else if(is_interpolation_qualifier(token))
753                         var->interpolation = token;
754                 else if(is_precision_qualifier(token))
755                         var->precision = token;
756                 else if(token=="const")
757                         var->constant = true;
758                 token = tokenizer.peek_token();
759         }
760
761         var->type = expect_type();
762         var->name = expect_identifier();
763
764         if(check("["))
765         {
766                 var->array = true;
767                 if(!check("]"))
768                 {
769                         var->array_size = parse_expression();
770                         tokenizer.expect("]");
771                 }
772         }
773
774         if(check("="))
775                 var->init_expression = parse_expression();
776
777         tokenizer.expect(";");
778         return var;
779 }
780
781 RefPtr<VariableDeclaration> Parser::parse_variable_declaration_with_layout()
782 {
783         RefPtr<Layout> layout;
784         if(tokenizer.peek_token()=="layout")
785                 layout = parse_layout();
786
787         RefPtr<VariableDeclaration> var = parse_variable_declaration();
788         var->layout = layout;
789
790         return var;
791 }
792
793 RefPtr<FunctionDeclaration> Parser::parse_function_declaration()
794 {
795         RefPtr<FunctionDeclaration> func = create_node<FunctionDeclaration>();
796
797         func->virtua = check("virtual");
798         func->return_type = expect_type();
799         func->name = expect_identifier();
800         tokenizer.expect("(");
801         while(tokenizer.peek_token()!=")")
802         {
803                 if(!func->parameters.empty())
804                         tokenizer.expect(",");
805
806                 RefPtr<VariableDeclaration> var = create_node<VariableDeclaration>();
807                 string token = tokenizer.peek_token();
808                 if(token=="in" || token=="out" || token=="inout")
809                         var->interface = tokenizer.parse_token();
810                 var->type = expect_type();
811                 var->name = expect_identifier();
812                 func->parameters.push_back(var);
813         }
814         tokenizer.expect(")");
815
816         func->overrd = check("override");
817
818         string token = tokenizer.peek_token();
819         if(token=="{")
820         {
821                 func->definition = func.get();
822                 parse_block(func->body, true, &Parser::parse_statement);
823         }
824         else if(token==";")
825                 tokenizer.parse_token();
826         else
827                 throw parse_error(tokenizer.get_location(), token, "'{' or ';'");
828
829         return func;
830 }
831
832 RefPtr<StructDeclaration> Parser::parse_interface_block()
833 {
834         RefPtr<StructDeclaration> strct = create_node<StructDeclaration>();
835         RefPtr<VariableDeclaration> var = create_node<VariableDeclaration>();
836
837         var->interface = tokenizer.parse_token();
838         if(!is_interface_qualifier(var->interface))
839                 throw parse_error(tokenizer.get_location(), var->interface, "an interface qualifier");
840
841         strct->block_name = expect_identifier();
842         string name_base = format("_%s_%s", var->interface, strct->block_name);
843         strct->name = name_base;
844         for(unsigned i=1; (stage_types.count(strct->name) || global_types.count(strct->name)); ++i)
845                 strct->name = format("%s_%d", name_base, i);
846         var->type = strct->name;
847         parse_block(strct->members, true, &Parser::parse_variable_declaration_with_layout);
848         if(!check(";"))
849         {
850                 var->name = expect_identifier();
851                 if(check("["))
852                 {
853                         var->array = true;
854                         tokenizer.expect("]");
855                 }
856                 tokenizer.expect(";");
857         }
858         else
859                 var->name = format("%s %s", var->interface, strct->block_name);
860
861         strct->block_declaration = var.release();
862         add_type(*strct);
863
864         return strct;
865 }
866
867 RefPtr<Conditional> Parser::parse_conditional()
868 {
869         tokenizer.expect("if");
870         RefPtr<Conditional> cond = create_node<Conditional>();
871         tokenizer.expect("(");
872         cond->condition = parse_expression();
873         tokenizer.expect(")");
874
875         parse_block(cond->body, false, &Parser::parse_statement);
876
877         string token = tokenizer.peek_token();
878         if(token=="else")
879         {
880                 tokenizer.parse_token();
881                 parse_block(cond->else_body, false, &Parser::parse_statement);
882         }
883
884         return cond;
885 }
886
887 RefPtr<Iteration> Parser::parse_for()
888 {
889         tokenizer.expect("for");
890         RefPtr<Iteration> loop = create_node<Iteration>();
891         tokenizer.expect("(");
892         string token = tokenizer.peek_token();
893         if(is_type(token))
894                 loop->init_statement = parse_statement();
895         else
896         {
897                 if(token!=";")
898                 {
899                         RefPtr<ExpressionStatement> expr = create_node<ExpressionStatement>();
900                         expr->expression = parse_expression();
901                         loop->init_statement = expr;
902                 }
903                 tokenizer.expect(";");
904         }
905         if(tokenizer.peek_token()!=";")
906                 loop->condition = parse_expression();
907         tokenizer.expect(";");
908         if(tokenizer.peek_token()!=")")
909                 loop->loop_expression = parse_expression();
910         tokenizer.expect(")");
911
912         parse_block(loop->body, false, &Parser::parse_statement);
913
914         return loop;
915 }
916
917 RefPtr<Iteration> Parser::parse_while()
918 {
919         tokenizer.expect("while");
920         RefPtr<Iteration> loop = create_node<Iteration>();
921         tokenizer.expect("(");
922         loop->condition = parse_expression();
923         tokenizer.expect(")");
924
925         parse_block(loop->body, false, &Parser::parse_statement);
926
927         return loop;
928 }
929
930 RefPtr<Passthrough> Parser::parse_passthrough()
931 {
932         tokenizer.expect("passthrough");
933         RefPtr<Passthrough> pass = create_node<Passthrough>();
934         if(cur_stage->type==Stage::GEOMETRY)
935         {
936                 tokenizer.expect("[");
937                 pass->subscript = parse_expression();
938                 tokenizer.expect("]");
939         }
940         tokenizer.expect(";");
941         return pass;
942 }
943
944 RefPtr<Return> Parser::parse_return()
945 {
946         tokenizer.expect("return");
947         RefPtr<Return> ret = create_node<Return>();
948         if(tokenizer.peek_token()!=";")
949                 ret->expression = parse_expression();
950         tokenizer.expect(";");
951         return ret;
952 }
953
954 } // namespace SL
955 } // namespace GL
956 } // namespace Msp