summaryrefslogtreecommitdiff
path: root/src/mesa/shader/slang/slang_typeinfo.c
blob: a9e1261746dbeb18c541bc98782ca0b36112ee3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
/*
 * Mesa 3-D graphics library
 * Version:  6.5
 *
 * Copyright (C) 2005-2006  Brian Paul   All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/**
 * \file slang_assemble_typeinfo.c
 * slang type info
 * \author Michal Krol
 */

#include "imports.h"
#include "slang_typeinfo.h"
#include "slang_compile.h"
#include "slang_log.h"
#include "prog_instruction.h"


/**
 * Checks if a field selector is a general swizzle (an r-value swizzle
 * with replicated components or an l-value swizzle mask) for a
 * vector.  Returns GL_TRUE if this is the case, <swz> is filled with
 * swizzle information.  Returns GL_FALSE otherwise.
 */
GLboolean
_slang_is_swizzle(const char *field, GLuint rows, slang_swizzle * swz)
{
   GLuint i;
   GLboolean xyzw = GL_FALSE, rgba = GL_FALSE, stpq = GL_FALSE;

   /* init to undefined.
    * We rely on undefined/nil values to distinguish between
    * regular swizzles and writemasks.
    * For example, the swizzle ".xNNN" is the writemask ".x".
    * That's different than the swizzle ".xxxx".
    */
   for (i = 0; i < 4; i++)
      swz->swizzle[i] = SWIZZLE_NIL;

   /* the swizzle can be at most 4-component long */
   swz->num_components = slang_string_length(field);
   if (swz->num_components > 4)
      return GL_FALSE;

   for (i = 0; i < swz->num_components; i++) {
      /* mark which swizzle group is used */
      switch (field[i]) {
      case 'x':
      case 'y':
      case 'z':
      case 'w':
         xyzw = GL_TRUE;
         break;
      case 'r':
      case 'g':
      case 'b':
      case 'a':
         rgba = GL_TRUE;
         break;
      case 's':
      case 't':
      case 'p':
      case 'q':
         stpq = GL_TRUE;
         break;
      default:
         return GL_FALSE;
      }

      /* collect swizzle component */
      switch (field[i]) {
      case 'x':
      case 'r':
      case 's':
         swz->swizzle[i] = 0;
         break;
      case 'y':
      case 'g':
      case 't':
         swz->swizzle[i] = 1;
         break;
      case 'z':
      case 'b':
      case 'p':
         swz->swizzle[i] = 2;
         break;
      case 'w':
      case 'a':
      case 'q':
         swz->swizzle[i] = 3;
         break;
      }

      /* check if the component is valid for given vector's row count */
      if (rows <= swz->swizzle[i])
         return GL_FALSE;
   }

   /* only one swizzle group can be used */
   if ((xyzw && rgba) || (xyzw && stpq) || (rgba && stpq))
      return GL_FALSE;

   return GL_TRUE;
}



/**
 * Checks if a general swizzle is an l-value swizzle - these swizzles
 * do not have duplicated fields.  Returns GL_TRUE if this is a
 * swizzle mask.  Returns GL_FALSE otherwise
 */
GLboolean
_slang_is_swizzle_mask(const slang_swizzle * swz, GLuint rows)
{
   GLuint i, c = 0;

   /* the swizzle may not be longer than the vector dim */
   if (swz->num_components > rows)
      return GL_FALSE;

   /* the swizzle components cannot be duplicated */
   for (i = 0; i < swz->num_components; i++) {
      if ((c & (1 << swz->swizzle[i])) != 0)
         return GL_FALSE;
      c |= 1 << swz->swizzle[i];
   }

   return GL_TRUE;
}


/**
 * Combines (multiplies) two swizzles to form single swizzle.
 * Example: "vec.wzyx.yx" --> "vec.zw".
 */
GLvoid
_slang_multiply_swizzles(slang_swizzle * dst, const slang_swizzle * left,
                         const slang_swizzle * right)
{
   GLuint i;

   dst->num_components = right->num_components;
   for (i = 0; i < right->num_components; i++)
      dst->swizzle[i] = left->swizzle[right->swizzle[i]];
}


GLvoid
slang_type_specifier_ctr(slang_type_specifier * self)
{
   self->type = SLANG_SPEC_VOID;
   self->_struct = NULL;
   self->_array = NULL;
}

GLvoid
slang_type_specifier_dtr(slang_type_specifier * self)
{
   if (self->_struct != NULL) {
      slang_struct_destruct(self->_struct);
      slang_alloc_free(self->_struct);
   }
   if (self->_array != NULL) {
      slang_type_specifier_dtr(self->_array);
      slang_alloc_free(self->_array);
   }
}

GLboolean
slang_type_specifier_copy(slang_type_specifier * x,
                          const slang_type_specifier * y)
{
   slang_type_specifier z;

   slang_type_specifier_ctr(&z);
   z.type = y->type;
   if (z.type == SLANG_SPEC_STRUCT) {
      z._struct = (slang_struct *) slang_alloc_malloc(sizeof(slang_struct));
      if (z._struct == NULL) {
         slang_type_specifier_dtr(&z);
         return GL_FALSE;
      }
      if (!slang_struct_construct(z._struct)) {
         slang_alloc_free(z._struct);
         slang_type_specifier_dtr(&z);
         return GL_FALSE;
      }
      if (!slang_struct_copy(z._struct, y->_struct)) {
         slang_type_specifier_dtr(&z);
         return GL_FALSE;
      }
   }
   else if (z.type == SLANG_SPEC_ARRAY) {
      z._array =
         (slang_type_specifier *)
         slang_alloc_malloc(sizeof(slang_type_specifier));
      if (z._array == NULL) {
         slang_type_specifier_dtr(&z);
         return GL_FALSE;
      }
      slang_type_specifier_ctr(z._array);
      if (!slang_type_specifier_copy(z._array, y->_array)) {
         slang_type_specifier_dtr(&z);
         return GL_FALSE;
      }
   }
   slang_type_specifier_dtr(x);
   *x = z;
   return GL_TRUE;
}


/**
 * Test if two types are equal.
 */
GLboolean
slang_type_specifier_equal(const slang_type_specifier * x,
                           const slang_type_specifier * y)
{
   if (x->type != y->type)
      return GL_FALSE;
   if (x->type == SLANG_SPEC_STRUCT)
      return slang_struct_equal(x->_struct, y->_struct);
   if (x->type == SLANG_SPEC_ARRAY)
      return slang_type_specifier_equal(x->_array, y->_array);
   return GL_TRUE;
}


/**
 * As above, but allow float/int casting.
 */
static GLboolean
slang_type_specifier_compatible(const slang_type_specifier * x,
                                const slang_type_specifier * y)
{
   /* special case: float == int */
   if (x->type == SLANG_SPEC_INT && y->type == SLANG_SPEC_FLOAT) {
      return GL_TRUE;
   }
   /* XXX may need to add bool/int compatibility, etc */

   if (x->type != y->type)
      return GL_FALSE;
   if (x->type == SLANG_SPEC_STRUCT)
      return slang_struct_equal(x->_struct, y->_struct);
   if (x->type == SLANG_SPEC_ARRAY)
      return slang_type_specifier_compatible(x->_array, y->_array);
   return GL_TRUE;
}


GLboolean
slang_typeinfo_construct(slang_typeinfo * ti)
{
   slang_type_specifier_ctr(&ti->spec);
   ti->array_len = 0;
   return GL_TRUE;
}

GLvoid
slang_typeinfo_destruct(slang_typeinfo * ti)
{
   slang_type_specifier_dtr(&ti->spec);
}



/**
 * Determine the return type of a function.
 * \param a_name  the function name
 * \param param  function parameters (overloading)
 * \param num_params  number of parameters to function
 * \param space  namespace to search
 * \param spec  returns the type
 * \param funFound  returns pointer to the function, or NULL if not found.
 * \return GL_TRUE for success, GL_FALSE if failure (bad function name)
 */
static GLboolean
_slang_typeof_function(slang_atom a_name,
                       slang_operation * params, GLuint num_params,
                       const slang_name_space * space,
                       slang_type_specifier * spec,
                       slang_function **funFound,
                       slang_atom_pool *atoms, slang_info_log *log)
{
   *funFound = _slang_locate_function(space->funcs, a_name, params,
                                      num_params, space, atoms, log);
   if (!*funFound)
      return GL_TRUE;  /* yes, not false */
   return slang_type_specifier_copy(spec, &(*funFound)->header.type.specifier);
}


/**
 * Determine the type of a math function.
 * \param name  name of the operator, one of +,-,*,/ or unary -
 * \param params  array of function parameters
 * \param num_params  number of parameters
 * \param space  namespace to use
 * \param spec  returns the function's type
 * \param atoms  atom pool
 * \return GL_TRUE for success, GL_FALSE if failure
 */
static GLboolean
typeof_math_call(const char *name, slang_operation *call,
                 const slang_name_space * space,
                 slang_type_specifier * spec,
                 slang_atom_pool * atoms,
                 slang_info_log *log)
{
   if (call->fun) {
      /* we've previously resolved this function call */
      slang_type_specifier_copy(spec, &call->fun->header.type.specifier);
      return GL_TRUE;
   }
   else {
      slang_atom atom;
      slang_function *fun;

      /* number of params: */
      assert(call->num_children == 1 || call->num_children == 2);

      atom = slang_atom_pool_atom(atoms, name);
      if (!_slang_typeof_function(atom, call->children, call->num_children,
                                  space, spec, &fun, atoms, log))
         return GL_FALSE;

      if (fun) {
         /* Save pointer to save time in future */
         call->fun = fun;
         return GL_TRUE;
      }
      return GL_FALSE;
   }
}

GLboolean
_slang_typeof_operation(const slang_assemble_ctx * A,
                        slang_operation * op,
                        slang_typeinfo * ti)
{
   return _slang_typeof_operation_(op, &A->space, ti, A->atoms, A->log);
}


/**
 * Determine the return type of an operation.
 * \param op  the operation node
 * \param space  the namespace to use
 * \param ti  the returned type
 * \param atoms  atom pool
 * \return GL_TRUE for success, GL_FALSE if failure
 */
GLboolean
_slang_typeof_operation_(slang_operation * op,
                         const slang_name_space * space,
                         slang_typeinfo * ti,
                         slang_atom_pool * atoms,
                         slang_info_log *log)
{
   ti->can_be_referenced = GL_FALSE;
   ti->is_swizzled = GL_FALSE;

   switch (op->type) {
   case SLANG_OPER_BLOCK_NO_NEW_SCOPE:
   case SLANG_OPER_BLOCK_NEW_SCOPE:
   case SLANG_OPER_VARIABLE_DECL:
   case SLANG_OPER_ASM:
   case SLANG_OPER_BREAK:
   case SLANG_OPER_CONTINUE:
   case SLANG_OPER_DISCARD:
   case SLANG_OPER_RETURN:
   case SLANG_OPER_IF:
   case SLANG_OPER_WHILE:
   case SLANG_OPER_DO:
   case SLANG_OPER_FOR:
   case SLANG_OPER_VOID:
      ti->spec.type = SLANG_SPEC_VOID;
      break;
   case SLANG_OPER_EXPRESSION:
   case SLANG_OPER_ASSIGN:
   case SLANG_OPER_ADDASSIGN:
   case SLANG_OPER_SUBASSIGN:
   case SLANG_OPER_MULASSIGN:
   case SLANG_OPER_DIVASSIGN:
   case SLANG_OPER_PREINCREMENT:
   case SLANG_OPER_PREDECREMENT:
      if (!_slang_typeof_operation_(op->children, space, ti, atoms, log))
         return GL_FALSE;
      break;
   case SLANG_OPER_LITERAL_BOOL:
      if (op->literal_size == 1)
         ti->spec.type = SLANG_SPEC_BOOL;
      else if (op->literal_size == 2)
         ti->spec.type = SLANG_SPEC_BVEC2;
      else if (op->literal_size == 3)
         ti->spec.type = SLANG_SPEC_BVEC3;
      else if (op->literal_size == 4)
         ti->spec.type = SLANG_SPEC_BVEC4;
      else {
         _mesa_problem(NULL,
               "Unexpected bool literal_size %d in _slang_typeof_operation()",
               op->literal_size);
         ti->spec.type = SLANG_SPEC_BOOL;
      }
      break;
   case SLANG_OPER_LOGICALOR:
   case SLANG_OPER_LOGICALXOR:
   case SLANG_OPER_LOGICALAND:
   case SLANG_OPER_EQUAL:
   case SLANG_OPER_NOTEQUAL:
   case SLANG_OPER_LESS:
   case SLANG_OPER_GREATER:
   case SLANG_OPER_LESSEQUAL:
   case SLANG_OPER_GREATEREQUAL:
   case SLANG_OPER_NOT:
      ti->spec.type = SLANG_SPEC_BOOL;
      break;
   case SLANG_OPER_LITERAL_INT:
      if (op->literal_size == 1)
         ti->spec.type = SLANG_SPEC_INT;
      else if (op->literal_size == 2)
         ti->spec.type = SLANG_SPEC_IVEC2;
      else if (op->literal_size == 3)
         ti->spec.type = SLANG_SPEC_IVEC3;
      else if (op->literal_size == 4)
         ti->spec.type = SLANG_SPEC_IVEC4;
      else {
         _mesa_problem(NULL,
               "Unexpected int literal_size %d in _slang_typeof_operation()",
               op->literal_size);
         ti->spec.type = SLANG_SPEC_INT;
      }
      break;
   case SLANG_OPER_LITERAL_FLOAT:
      if (op->literal_size == 1)
         ti->spec.type = SLANG_SPEC_FLOAT;
      else if (op->literal_size == 2)
         ti->spec.type = SLANG_SPEC_VEC2;
      else if (op->literal_size == 3)
         ti->spec.type = SLANG_SPEC_VEC3;
      else if (op->literal_size == 4)
         ti->spec.type = SLANG_SPEC_VEC4;
      else {
         _mesa_problem(NULL,
               "Unexpected float literal_size %d in _slang_typeof_operation()",
               op->literal_size);
         ti->spec.type = SLANG_SPEC_FLOAT;
      }
      break;
   case SLANG_OPER_IDENTIFIER:
      {
         slang_variable *var;
         var = _slang_locate_variable(op->locals, op->a_id, GL_TRUE);
         if (!var) {
            slang_info_log_error(log, "undefined variable '%s'",
                                 (char *) op->a_id);
            return GL_FALSE;
         }
         if (!slang_type_specifier_copy(&ti->spec, &var->type.specifier)) {
            slang_info_log_memory(log);
            return GL_FALSE;
         }
         ti->can_be_referenced = GL_TRUE;
         ti->array_len = var->array_len;
      }
      break;
   case SLANG_OPER_SEQUENCE:
      /* TODO: check [0] and [1] if they match */
      if (!_slang_typeof_operation_(&op->children[1], space, ti, atoms, log)) {
         return GL_FALSE;
      }
      ti->can_be_referenced = GL_FALSE;
      ti->is_swizzled = GL_FALSE;
      break;
      /*case SLANG_OPER_MODASSIGN: */
      /*case SLANG_OPER_LSHASSIGN: */
      /*case SLANG_OPER_RSHASSIGN: */
      /*case SLANG_OPER_ORASSIGN: */
      /*case SLANG_OPER_XORASSIGN: */
      /*case SLANG_OPER_ANDASSIGN: */
   case SLANG_OPER_SELECT:
      /* TODO: check [1] and [2] if they match */
      if (!_slang_typeof_operation_(&op->children[1], space, ti, atoms, log)) {
         return GL_FALSE;
      }
      ti->can_be_referenced = GL_FALSE;
      ti->is_swizzled = GL_FALSE;
      break;
      /*case SLANG_OPER_BITOR: */
      /*case SLANG_OPER_BITXOR: */
      /*case SLANG_OPER_BITAND: */
      /*case SLANG_OPER_LSHIFT: */
      /*case SLANG_OPER_RSHIFT: */
   case SLANG_OPER_ADD:
      assert(op->num_children == 2);
      if (!typeof_math_call("+", op, space, &ti->spec, atoms, log))
         return GL_FALSE;
      break;
   case SLANG_OPER_SUBTRACT:
      assert(op->num_children == 2);
      if (!typeof_math_call("-", op, space, &ti->spec, atoms, log))
         return GL_FALSE;
      break;
   case SLANG_OPER_MULTIPLY:
      assert(op->num_children == 2);
      if (!typeof_math_call("*", op, space, &ti->spec, atoms, log))
         return GL_FALSE;
      break;
   case SLANG_OPER_DIVIDE:
      assert(op->num_children == 2);
      if (!typeof_math_call("/", op, space, &ti->spec, atoms, log))
         return GL_FALSE;
      break;
   /*case SLANG_OPER_MODULUS: */
   case SLANG_OPER_PLUS:
      if (!_slang_typeof_operation_(op->children, space, ti, atoms, log))
         return GL_FALSE;
      ti->can_be_referenced = GL_FALSE;
      ti->is_swizzled = GL_FALSE;
      break;
   case SLANG_OPER_MINUS:
      assert(op->num_children == 1);
      if (!typeof_math_call("-", op, space, &ti->spec, atoms, log))
         return GL_FALSE;
      break;
      /*case SLANG_OPER_COMPLEMENT: */
   case SLANG_OPER_SUBSCRIPT:
      {
         slang_typeinfo _ti;

         if (!slang_typeinfo_construct(&_ti))
            return GL_FALSE;
         if (!_slang_typeof_operation_(op->children, space, &_ti, atoms, log)) {
            slang_typeinfo_destruct(&_ti);
            return GL_FALSE;
         }
         ti->can_be_referenced = _ti.can_be_referenced;
         if (_ti.spec.type == SLANG_SPEC_ARRAY) {
            if (!slang_type_specifier_copy(&ti->spec, _ti.spec._array)) {
               slang_typeinfo_destruct(&_ti);
               return GL_FALSE;
            }
         }
         else {
            if (!_slang_type_is_vector(_ti.spec.type)
                && !_slang_type_is_matrix(_ti.spec.type)) {
               slang_typeinfo_destruct(&_ti);
               slang_info_log_error(log, "cannot index a non-array type");
               return GL_FALSE;
            }
            ti->spec.type = _slang_type_base(_ti.spec.type);
         }
         slang_typeinfo_destruct(&_ti);
      }
      break;
   case SLANG_OPER_CALL:
      {
         slang_function *fun;

         if (!_slang_typeof_function(op->a_id, op->children, op->num_children,
                                     space, &ti->spec, &fun, atoms, log))
            return GL_FALSE;
         if (!fun) {
            /* Look for struct initializer? */
            slang_struct *s =
               slang_struct_scope_find(space->structs, op->a_id, GL_TRUE);
            if (s != NULL) {
               ti->spec.type = SLANG_SPEC_STRUCT;
               ti->spec._struct =
                  (slang_struct *) slang_alloc_malloc(sizeof(slang_struct));
               if (ti->spec._struct == NULL)
                  return GL_FALSE;
               if (!slang_struct_construct(ti->spec._struct)) {
                  slang_alloc_free(ti->spec._struct);
                  ti->spec._struct = NULL;
                  return GL_FALSE;
               }
               if (!slang_struct_copy(ti->spec._struct, s))
                  return GL_FALSE;
            }
            else {
               const char *name;
               slang_type_specifier_type type;

               name = slang_atom_pool_id(atoms, op->a_id);
               type = slang_type_specifier_type_from_string(name);
               if (type == SLANG_SPEC_VOID) {
                  slang_info_log_error(log, "undefined function '%s'", name);
                  return GL_FALSE;
               }
               ti->spec.type = type;
            }
         }
      }
      break;
   case SLANG_OPER_FIELD:
      {
         slang_typeinfo _ti;

         if (!slang_typeinfo_construct(&_ti))
            return GL_FALSE;
         if (!_slang_typeof_operation_(op->children, space, &_ti, atoms, log)) {
            slang_typeinfo_destruct(&_ti);
            return GL_FALSE;
         }
         if (_ti.spec.type == SLANG_SPEC_STRUCT) {
            slang_variable *field;

            field = _slang_locate_variable(_ti.spec._struct->fields, op->a_id,
                                           GL_FALSE);
            if (field == NULL) {
               slang_typeinfo_destruct(&_ti);
               return GL_FALSE;
            }
            if (!slang_type_specifier_copy(&ti->spec, &field->type.specifier)) {
               slang_typeinfo_destruct(&_ti);
               return GL_FALSE;
            }
            ti->can_be_referenced = _ti.can_be_referenced;
         }
         else {
            GLuint rows;
            const char *swizzle;
            slang_type_specifier_type base;

            /* determine the swizzle of the field expression */
            if (!_slang_type_is_vector(_ti.spec.type)) {
               slang_typeinfo_destruct(&_ti);
               slang_info_log_error(log, "Can't swizzle scalar expression");
               return GL_FALSE;
            }
            rows = _slang_type_dim(_ti.spec.type);
            swizzle = slang_atom_pool_id(atoms, op->a_id);
            if (!_slang_is_swizzle(swizzle, rows, &ti->swz)) {
               slang_typeinfo_destruct(&_ti);
               slang_info_log_error(log, "bad swizzle '%s'", swizzle);
               return GL_FALSE;
            }
            ti->is_swizzled = GL_TRUE;
            ti->can_be_referenced = _ti.can_be_referenced
               && _slang_is_swizzle_mask(&ti->swz, rows);
            if (_ti.is_swizzled) {
               slang_swizzle swz;

               /* swizzle the swizzle */
               _slang_multiply_swizzles(&swz, &_ti.swz, &ti->swz);
               ti->swz = swz;
            }
            base = _slang_type_base(_ti.spec.type);
            switch (ti->swz.num_components) {
            case 1:
               ti->spec.type = base;
               break;
            case 2:
               switch (base) {
               case SLANG_SPEC_FLOAT:
                  ti->spec.type = SLANG_SPEC_VEC2;
                  break;
               case SLANG_SPEC_INT:
                  ti->spec.type = SLANG_SPEC_IVEC2;
                  break;
               case SLANG_SPEC_BOOL:
                  ti->spec.type = SLANG_SPEC_BVEC2;
                  break;
               default:
                  break;
               }
               break;
            case 3:
               switch (base) {
               case SLANG_SPEC_FLOAT:
                  ti->spec.type = SLANG_SPEC_VEC3;
                  break;
               case SLANG_SPEC_INT:
                  ti->spec.type = SLANG_SPEC_IVEC3;
                  break;
               case SLANG_SPEC_BOOL:
                  ti->spec.type = SLANG_SPEC_BVEC3;
                  break;
               default:
                  break;
               }
               break;
            case 4:
               switch (base) {
               case SLANG_SPEC_FLOAT:
                  ti->spec.type = SLANG_SPEC_VEC4;
                  break;
               case SLANG_SPEC_INT:
                  ti->spec.type = SLANG_SPEC_IVEC4;
                  break;
               case SLANG_SPEC_BOOL:
                  ti->spec.type = SLANG_SPEC_BVEC4;
                  break;
               default:
                  break;
               }
               break;
            default:
               break;
            }
         }
         slang_typeinfo_destruct(&_ti);
      }
      break;
   case SLANG_OPER_POSTINCREMENT:
   case SLANG_OPER_POSTDECREMENT:
      if (!_slang_typeof_operation_(op->children, space, ti, atoms, log))
         return GL_FALSE;
      ti->can_be_referenced = GL_FALSE;
      ti->is_swizzled = GL_FALSE;
      break;
   default:
      return GL_FALSE;
   }

   return GL_TRUE;
}


/**
 * Lookup a function according to name and parameter count/types.
 */
slang_function *
_slang_locate_function(const slang_function_scope * funcs, slang_atom a_name,
                       slang_operation * args, GLuint num_args,
                       const slang_name_space * space, slang_atom_pool * atoms,
                       slang_info_log *log)
{
   GLuint i;

   for (i = 0; i < funcs->num_functions; i++) {
      slang_function *f = &funcs->functions[i];
      const GLuint haveRetValue = _slang_function_has_return_value(f);
      GLuint j;

      if (a_name != f->header.a_name)
         continue;
      if (f->param_count - haveRetValue != num_args)
         continue;

      /* compare parameter / argument types */
      for (j = 0; j < num_args; j++) {
         slang_typeinfo ti;

         if (!slang_typeinfo_construct(&ti))
            return NULL;
         if (!_slang_typeof_operation_(&args[j], space, &ti, atoms, log)) {
            slang_typeinfo_destruct(&ti);
            return NULL;
         }
         if (!slang_type_specifier_compatible(&ti.spec,
             &f->parameters->variables[j]->type.specifier)) {
            slang_typeinfo_destruct(&ti);
            break;
         }
         slang_typeinfo_destruct(&ti);

         /* "out" and "inout" formal parameter requires the actual
          * parameter to be l-value.
          */
         if (!ti.can_be_referenced &&
             (f->parameters->variables[j]->type.qualifier == SLANG_QUAL_OUT ||
              f->parameters->variables[j]->type.qualifier == SLANG_QUAL_INOUT))
            break;
      }
      if (j == num_args)
         return f;
   }
   if (funcs->outer_scope != NULL)
      return _slang_locate_function(funcs->outer_scope, a_name, args,
                                    num_args, space, atoms, log);
   return NULL;
}


/**
 * Determine if a type is a matrix.
 * \return GL_TRUE if is a matrix, GL_FALSE otherwise.
 */
GLboolean
_slang_type_is_matrix(slang_type_specifier_type ty)
{
   switch (ty) {
   case SLANG_SPEC_MAT2:
   case SLANG_SPEC_MAT3:
   case SLANG_SPEC_MAT4:
      return GL_TRUE;
   default:
      return GL_FALSE;
   }
}


/**
 * Determine if a type is a vector.
 * \return GL_TRUE if is a vector, GL_FALSE otherwise.
 */
GLboolean
_slang_type_is_vector(slang_type_specifier_type ty)
{
   switch (ty) {
   case SLANG_SPEC_VEC2:
   case SLANG_SPEC_VEC3:
   case SLANG_SPEC_VEC4:
   case SLANG_SPEC_IVEC2:
   case SLANG_SPEC_IVEC3:
   case SLANG_SPEC_IVEC4:
   case SLANG_SPEC_BVEC2:
   case SLANG_SPEC_BVEC3:
   case SLANG_SPEC_BVEC4:
      return GL_TRUE;
   default:
      return GL_FALSE;
   }
}


/**
 * Given a vector type, return the type of the vector's elements
 */
slang_type_specifier_type
_slang_type_base(slang_type_specifier_type ty)
{
   switch (ty) {
   case SLANG_SPEC_FLOAT:
   case SLANG_SPEC_VEC2:
   case SLANG_SPEC_VEC3:
   case SLANG_SPEC_VEC4:
      return SLANG_SPEC_FLOAT;
   case SLANG_SPEC_INT:
   case SLANG_SPEC_IVEC2:
   case SLANG_SPEC_IVEC3:
   case SLANG_SPEC_IVEC4:
      return SLANG_SPEC_INT;
   case SLANG_SPEC_BOOL:
   case SLANG_SPEC_BVEC2:
   case SLANG_SPEC_BVEC3:
   case SLANG_SPEC_BVEC4:
      return SLANG_SPEC_BOOL;
   case SLANG_SPEC_MAT2:
      return SLANG_SPEC_VEC2;
   case SLANG_SPEC_MAT3:
      return SLANG_SPEC_VEC3;
   case SLANG_SPEC_MAT4:
      return SLANG_SPEC_VEC4;
   default:
      return SLANG_SPEC_VOID;
   }
}


/**
 * Return the dimensionality of a vector or matrix type.
 */
GLuint
_slang_type_dim(slang_type_specifier_type ty)
{
   switch (ty) {
   case SLANG_SPEC_FLOAT:
   case SLANG_SPEC_INT:
   case SLANG_SPEC_BOOL:
      return 1;
   case SLANG_SPEC_VEC2:
   case SLANG_SPEC_IVEC2:
   case SLANG_SPEC_BVEC2:
   case SLANG_SPEC_MAT2:
      return 2;
   case SLANG_SPEC_VEC3:
   case SLANG_SPEC_IVEC3:
   case SLANG_SPEC_BVEC3:
   case SLANG_SPEC_MAT3:
      return 3;
   case SLANG_SPEC_VEC4:
   case SLANG_SPEC_IVEC4:
   case SLANG_SPEC_BVEC4:
   case SLANG_SPEC_MAT4:
      return 4;
   default:
      return 0;
   }
}