1group valid "Valid scoping and name redeclaration cases"
2
3	case local_variable_hides_global_variable
4		version 100 es
5		values
6		{
7			input int in0 = [ 1 | 2 | 3 ];
8			output int out0 = [ 1 | 2 | 3 ];
9		}
10
11		both ""
12			#version 100
13			precision mediump float;
14			${DECLARATIONS}
15
16			int a = -1;
17
18			void main()
19			{
20				${SETUP}
21				int a = in0;
22
23				out0 = a;
24				${OUTPUT}
25			}
26		""
27	end
28
29	case block_variable_hides_local_variable
30		version 100 es
31		values
32		{
33			input int in0 = [ 1 | 2 | 3 ];
34			output int out0 = [ 1 | 2 | 3 ];
35		}
36
37		both ""
38			#version 100
39			precision mediump float;
40			${DECLARATIONS}
41			void main()
42			{
43				${SETUP}
44				int a = in0;
45				{
46					int a = -1;
47				}
48				out0 = a;
49				${OUTPUT}
50			}
51		""
52	end
53
54	case block_variable_hides_global_variable
55		version 100 es
56		values
57		{
58			input int in0 = [ 1 | 2 | 3 ];
59			output int out0 = [ 1 | 2 | 3 ];
60		}
61
62		both ""
63			#version 100
64			precision mediump float;
65			${DECLARATIONS}
66
67			int a = -1;
68
69			void main()
70			{
71				${SETUP}
72				{
73					int a = in0;
74
75					out0 = a;
76				}
77				${OUTPUT}
78			}
79		""
80	end
81
82	case for_init_statement_variable_hides_local_variable
83		version 100 es
84		values
85		{
86			input int in0 = [ 1 | 2 | 3 ];
87			output int out0 = [ 1 | 2 | 3 ];
88		}
89
90		both ""
91			#version 100
92			precision mediump float;
93			${DECLARATIONS}
94			void main()
95			{
96				${SETUP}
97				int a = in0;
98				for (int a = 0; a < 10; a++)
99				{
100				}
101				out0 = a;
102				${OUTPUT}
103			}
104		""
105	end
106
107	case for_init_statement_variable_hides_global_variable
108		version 100 es
109		values
110		{
111			input int in0 = [ 1 | 2 | 3 ];
112			output int out0 = [ 1 | 2 | 3 ];
113		}
114
115		both ""
116			#version 100
117			precision mediump float;
118			${DECLARATIONS}
119
120			int a = 5;
121
122			void main()
123			{
124				${SETUP}
125				for (int a = 0; a < 10; a++)
126				{
127				}
128				out0 = in0 + a - 5;
129				${OUTPUT}
130			}
131		""
132	end
133
134	case variable_in_if_hides_global_variable
135		version 100 es
136		values
137		{
138			input int in0 = [ 1 | 2 | 3 ];
139			output int out0 = [ 1 | 2 | 3 ];
140		}
141
142		both ""
143			#version 100
144			precision mediump float;
145			${DECLARATIONS}
146
147			int a = 1;
148
149			void main()
150			{
151				${SETUP}
152				if (true)
153					int a = 42;
154				out0 = a*in0;
155				${OUTPUT}
156			}
157		""
158	end
159
160	case variable_from_outer_scope_visible_in_initializer
161		version 100 es
162		values
163		{
164			input int in0 = [ 1 | 2 | 3 ];
165			output int out0 = [ 1 | 2 | 3 ];
166		}
167
168		both ""
169			#version 100
170			precision mediump float;
171			${DECLARATIONS}
172			void main()
173			{
174				${SETUP}
175				int a = in0;
176				{
177					int a = a+5, b = a-5;
178					out0 = b;
179					a = 42;
180				}
181				out0 = out0 + a - in0;
182				${OUTPUT}
183			}
184		""
185	end
186
187	case local_int_variable_hides_struct_type
188		version 100 es
189		values
190		{
191			input int in0 = [ 1 | 2 | 3 ];
192			output int out0 = [ 1 | 2 | 3 ];
193		}
194
195		both ""
196			#version 100
197			precision mediump float;
198			${DECLARATIONS}
199
200			struct S { int val; };
201
202			void main()
203			{
204				${SETUP}
205				int S = S(in0).val;
206				out0 = S;
207				${OUTPUT}
208			}
209		""
210	end
211
212	case local_struct_variable_hides_struct_type
213		version 100 es
214		values
215		{
216			input int in0 = [ 1 | 2 | 3 ];
217			output int out0 = [ 1 | 2 | 3 ];
218		}
219
220		both ""
221			#version 100
222			precision mediump float;
223			${DECLARATIONS}
224
225			struct S { int val; };
226
227			void main()
228			{
229				${SETUP}
230				S S = S(in0);
231				out0 = S.val;
232				${OUTPUT}
233			}
234		""
235	end
236
237	case local_variable_hides_function
238		version 100 es
239		values
240		{
241			input int in0 = [ 1 | 2 | 3 ];
242			output int out0 = [ 1 | 2 | 3 ];
243		}
244
245		both ""
246			#version 100
247			precision mediump float;
248			${DECLARATIONS}
249
250			int foo (int x) { return x; }
251
252			void main()
253			{
254				${SETUP}
255				int foo = in0;
256				out0 = foo;
257				${OUTPUT}
258			}
259		""
260	end
261
262	case function_parameter_hides_global_variable
263		version 100 es
264		values
265		{
266			input int in0 = [ 1 | 2 | 3 ];
267			output int out0 = [ 1 | 2 | 3 ];
268		}
269
270		both ""
271			#version 100
272			precision mediump float;
273			${DECLARATIONS}
274
275			int a = -1;
276
277			int func (int a) { return a; }
278
279			void main()
280			{
281				${SETUP}
282				out0 = func(in0);
283				${OUTPUT}
284			}
285		""
286	end
287
288	case function_parameter_hides_struct_type
289		version 100 es
290		values
291		{
292			input int in0 = [ 1 | 2 | 3 ];
293			output int out0 = [ 1 | 2 | 3 ];
294		}
295
296		both ""
297			#version 100
298			precision mediump float;
299			${DECLARATIONS}
300
301			struct S { int x; };
302
303			int func (int S) { return S; }
304
305			void main()
306			{
307				${SETUP}
308				out0 = func(in0);
309				${OUTPUT}
310			}
311		""
312	end
313
314	case function_parameter_hides_function
315		version 100 es
316		values
317		{
318			input int in0 = [ 1 | 2 | 3 ];
319			output int out0 = [ 1 | 2 | 3 ];
320		}
321
322		both ""
323			#version 100
324			precision mediump float;
325			${DECLARATIONS}
326
327			int func (int func) { return func; }
328
329			void main()
330			{
331				${SETUP}
332				out0 = func(in0);
333				${OUTPUT}
334			}
335		""
336	end
337
338	case local_variable_in_inner_scope_hides_function_parameter
339		version 100 es
340		values
341		{
342			input int in0 = [ 1 | 2 | 3 ];
343			output int out0 = [ 1 | 2 | 3 ];
344		}
345
346		both ""
347			#version 100
348			precision mediump float;
349			${DECLARATIONS}
350			int func (int inp, int x) { { int x = 5; return inp + x - 5; } }
351
352			void main()
353			{
354				${SETUP}
355				out0 = func(in0, 42);
356				${OUTPUT}
357			}
358		""
359	end
360
361end
362
363group invalid "Invalid scoping behavior"
364
365	case redeclare_global_variable
366		version 100 es
367		expect compile_fail
368		both ""
369			#version 100
370			precision mediump float;
371			${DECLARATIONS}
372
373			int a;
374			float a;
375
376			void main()
377			{
378				a = 1.0;
379				${POSITION_FRAG_COLOR} = vec4(a);
380			}
381		""
382	end
383
384	case redeclare_local_variable
385		version 100 es
386		expect compile_fail
387		both ""
388			#version 100
389			precision mediump float;
390			${DECLARATIONS}
391
392			void main()
393			{
394				int a;
395				float a;
396				a = 1.0;
397				${POSITION_FRAG_COLOR} = vec4(a);
398			}
399		""
400	end
401
402	case redeclare_for_init_statement_variable
403		version 100 es
404		expect compile_fail
405		both ""
406			#version 100
407			precision mediump float;
408			${DECLARATIONS}
409
410			void main()
411			{
412				for (int i = 0; i < 10; i++)
413				{
414					int i = 11;
415				}
416				${POSITION_FRAG_COLOR} = vec4(0.0);
417			}
418		""
419	end
420
421	case redeclare_for_condition_variable
422		version 100 es
423		expect compile_fail
424		both ""
425			#version 100
426			precision mediump float;
427			${DECLARATIONS}
428
429			void main()
430			{
431				for (int i = 0; int a = (i < 10); i++)
432				{
433					int a = 0;
434				}
435				${POSITION_FRAG_COLOR} = vec4(0.0);
436			}
437		""
438	end
439
440	case redeclare_for_init_statement_variable_in_for_condition
441		version 100 es
442		expect compile_fail
443		both ""
444			#version 100
445			precision mediump float;
446			${DECLARATIONS}
447
448			void main()
449			{
450				float a;
451				for (int i = 0; int i = (i < 10); i++)
452				{
453					a = sin(i);
454				}
455				${POSITION_FRAG_COLOR} = vec4(a);
456			}
457		""
458	end
459
460	case redeclare_while_condition_variable
461		version 100 es
462		expect compile_fail
463		both ""
464			#version 100
465			precision mediump float;
466			${DECLARATIONS}
467
468			void main()
469			{
470				int a = 0;
471				while (int i = (a < 5))
472				{
473					int i = 11;
474					a += i;
475				}
476				${POSITION_FRAG_COLOR} = vec4(0.0);
477			}
478		""
479	end
480
481	case redeclare_function
482		version 100 es
483		expect compile_fail
484		both ""
485			#version 100
486			precision mediump float;
487			${DECLARATIONS}
488
489			float func(float x);
490			float func(float x);
491
492			float func(float x) { return x + 1.0; }
493
494			void main()
495			{
496				${POSITION_FRAG_COLOR} = vec4(func(1.0));
497			}
498		""
499	end
500
501	case redefine_function
502		version 100 es
503		expect compile_fail
504		both ""
505			#version 100
506			precision mediump float;
507			${DECLARATIONS}
508
509			float func(float x);
510
511			float func(float x) { return x + 1.0; }
512			float func(float x) { return x + 2.0; }
513
514			void main()
515			{
516				${POSITION_FRAG_COLOR} = vec4(func(1.0));
517			}
518		""
519	end
520
521	case redeclare_builtin
522		version 100 es
523		expect compile_fail
524		both ""
525			#version 100
526			precision mediump float;
527			${DECLARATIONS}
528
529			float sin(float x);
530
531			void main()
532			{
533				${POSITION_FRAG_COLOR} = vec4(sin(1.0));
534			}
535		""
536	end
537
538	case redefine_builtin
539		version 100 es
540		expect compile_fail
541		both ""
542			#version 100
543			precision mediump float;
544			${DECLARATIONS}
545
546			float sin(float x) { return x + 1.0; }
547
548			void main()
549			{
550				${POSITION_FRAG_COLOR} = vec4(sin(1.0));
551			}
552		""
553	end
554
555	case conflict_function_struct
556		version 100 es
557		expect compile_fail
558		both ""
559			#version 100
560			precision mediump float;
561			${DECLARATIONS}
562
563			void f(int x);
564			struct f { int x; };
565
566			void main()
567			{
568				${POSITION_FRAG_COLOR} = vec4(1);
569			}
570		""
571	end
572
573	case conflict_function_variable
574		version 100 es
575		expect compile_fail
576		both ""
577			#version 100
578			precision mediump float;
579			${DECLARATIONS}
580
581			void f(int x);
582			float f;
583
584			void main()
585			{
586				f = 1.0;
587				${POSITION_FRAG_COLOR} = vec4(f);
588			}
589		""
590	end
591
592	case use_global_variable_before_declaration
593		version 100 es
594		expect compile_fail
595		both ""
596			#version 100
597			precision mediump float;
598			${DECLARATIONS}
599
600			void func()
601			{
602				a = 2.0;
603			}
604
605			float a;
606
607			void main()
608			{
609				func();
610				${POSITION_FRAG_COLOR} = vec4(a);
611			}
612		""
613	end
614
615	case use_local_variable_before_declaration
616		version 100 es
617		expect compile_fail
618		both ""
619			#version 100
620			precision mediump float;
621			${DECLARATIONS}
622
623			void main()
624			{
625				float a = 1.0;
626				a = b;
627				float b = 2.0;
628
629				${POSITION_FRAG_COLOR} = vec4(a);
630			}
631		""
632	end
633
634	case use_struct_type_before_declaration
635		version 100 es
636		expect compile_fail
637		both ""
638			#version 100
639			precision mediump float;
640			${DECLARATIONS}
641
642			float func (float x) { return S(x).val; }
643			struct S { float val; };
644
645			void main()
646			{
647				${POSITION_FRAG_COLOR} = vec4(func(1.0));
648			}
649		""
650	end
651
652	case use_function_before_declaration
653		version 100 es
654		expect compile_fail
655		both ""
656			#version 100
657			precision mediump float;
658			${DECLARATIONS}
659
660			float func (float x) { return bar(x); }
661			float bar (float x) { return x; }
662
663			void main()
664			{
665				${POSITION_FRAG_COLOR} = vec4(func(1.0));
666			}
667		""
668	end
669
670	case use_variable_from_block_in_outer_scope
671		version 100 es
672		expect compile_fail
673		both ""
674			#version 100
675			precision mediump float;
676			${DECLARATIONS}
677
678			void main()
679			{
680				{
681					float a = 1.0;
682				}
683				${POSITION_FRAG_COLOR} = vec4(a);
684			}
685		""
686	end
687
688	case use_variable_from_if_in_outer_scope
689		version 100 es
690		expect compile_fail
691		both ""
692			#version 100
693			precision mediump float;
694			${DECLARATIONS}
695
696			void main()
697			{
698				if (true)
699					float a = 1.0;
700				${POSITION_FRAG_COLOR} = vec4(a);
701			}
702		""
703	end
704
705	case use_variable_from_else_in_outer_scope
706		version 100 es
707		expect compile_fail
708		both ""
709			#version 100
710			precision mediump float;
711			${DECLARATIONS}
712
713			void main()
714			{
715				if (false)
716					float a = 1.0;
717				else
718					float b = 2.0;
719				${POSITION_FRAG_COLOR} = vec4(b);
720			}
721		""
722	end
723
724	case use_variable_from_if_in_else
725		version 100 es
726		expect compile_fail
727		both ""
728			#version 100
729			precision mediump float;
730			${DECLARATIONS}
731
732			void main()
733			{
734				float a = 1.0;
735				if (true)
736				{
737					float b = 2.0;
738				}
739				else
740				{
741					a = b;
742				}
743				${POSITION_FRAG_COLOR} = vec4(a);
744			}
745		""
746	end
747
748	case use_variable_from_for_init_statement_in_outer_scope
749		version 100 es
750		expect compile_fail
751		both ""
752			#version 100
753			precision mediump float;
754			${DECLARATIONS}
755
756			void main()
757			{
758				float x = 0.0;
759				for (int i = 0; i < 10; i++)
760				{
761					x += sin(i);
762				}
763				${POSITION_FRAG_COLOR} = vec4(float(i));
764			}
765		""
766	end
767
768	case use_variable_from_while_condition_in_outer_scope
769		version 100 es
770		expect compile_fail
771		both ""
772			#version 100
773			precision mediump float;
774			${DECLARATIONS}
775
776			void main()
777			{
778				int a = 1;
779				while (bool b = (a == 1))
780				{
781					a++;
782				}
783				${POSITION_FRAG_COLOR} = vec4(float(b));
784			}
785		""
786	end
787
788	case use_parameter_names_from_function_declaration
789		version 100 es
790		expect compile_fail
791		both ""
792			#version 100
793			precision mediump float;
794			${DECLARATIONS}
795
796			float func(float a, float b);
797
798			float func(float x, float y) { return a+b; }
799
800			void main()
801			{
802				${POSITION_FRAG_COLOR} = vec4(func(1.0, 2.0));
803			}
804		""
805	end
806
807	case variable_not_visible_in_own_initializer
808		version 100 es
809		expect compile_fail
810		both ""
811			#version 100
812			precision mediump float;
813			${DECLARATIONS}
814
815			void main()
816			{
817				float x = x;
818				${POSITION_FRAG_COLOR} = vec4(x);
819			}
820		""
821	end
822
823end # invalid
824