-
Notifications
You must be signed in to change notification settings - Fork 146
/
type_check.py
1388 lines (1172 loc) · 54.9 KB
/
type_check.py
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
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# Copyright 2014 Stanford University and Los Alamos National Security, LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
###
### Type Checker
###
# Backport of singledispatch to Python 2.x.
try:
from functools import singledispatch
except ImportError:
from singledispatch import singledispatch
# Work around for OrderedDict missing in Python 2.6.
try:
from collections import OrderedDict
except ImportError:
from ordereddict import OrderedDict
from . import ast, types
from .clang import types as ctypes
def is_eq(t): return types.is_POD(t) or types.is_pointer(t)
def returns_same_type(*ts): return ts[0]
def returns_bool(*_ignored): return types.Bool()
unary_operator_table = {
'-': (types.is_numeric, returns_same_type),
'!': (types.is_bool, returns_bool),
'~': (types.is_integral, returns_same_type),
}
binary_operator_table = {
'*': (types.is_numeric, returns_same_type),
'/': (types.is_numeric, returns_same_type),
'%': (types.is_integral, returns_same_type),
'+': (types.is_numeric, returns_same_type),
'-': (types.is_numeric, returns_same_type),
'>>': (types.is_integral, returns_same_type),
'<<': (types.is_integral, returns_same_type),
'<': (types.is_numeric, returns_bool),
'<=': (types.is_numeric, returns_bool),
'>': (types.is_numeric, returns_bool),
'>=': (types.is_numeric, returns_bool),
'==': (is_eq, returns_bool),
'!=': (is_eq, returns_bool),
'&': (types.is_integral, returns_same_type),
'^': (types.is_integral, returns_same_type),
'|': (types.is_integral, returns_same_type),
'&&': (types.is_bool, returns_bool),
'||': (types.is_bool, returns_bool),
}
reduce_operator_table = {
'*': types.is_numeric,
'/': types.is_numeric,
'%': types.is_integral,
'+': types.is_numeric,
'-': types.is_numeric,
'>>': types.is_integral,
'<<': types.is_integral,
'&': types.is_integral,
'^': types.is_integral,
'|': types.is_integral,
}
# A method combination around wrapper a la Common Lisp.
class DispatchAround:
def __init__(self, inner_fn, outer_fn):
self.inner_fn = inner_fn
self.outer_fn = outer_fn
def __call__(self, *args, **kwargs):
return self.outer_fn(self.inner_fn, *args, **kwargs)
def __getattr__(self, name):
return getattr(self.inner_fn, name)
def store_result_in_type_map(fn):
def helper(fn, node, cx):
node_type = fn(node, cx)
cx.type_map[node] = node_type
return node_type
return DispatchAround(fn, helper)
@store_result_in_type_map
@singledispatch
def type_check_node(node, cx):
raise Exception('Type checking failed at %s' % node)
@type_check_node.register(ast.Program)
def _(node, cx):
cx = cx.new_global_scope()
def_types = type_check_node(node.definitions, cx)
return types.Program(def_types)
@type_check_node.register(ast.Definitions)
def _(node, cx):
def_types = []
for definition in node.definitions:
def_types.append(type_check_node(definition, cx))
return def_types
@type_check_node.register(ast.Import)
def _(node, cx):
module_type = ctypes.foreign_type(node.ast, cx.opts)
for foreign_name, foreign_type in module_type.def_types.iteritems():
cx.insert(node, foreign_name, foreign_type)
cx.foreign_types.append(foreign_type)
return module_type
@type_check_node.register(ast.Struct)
def _(node, cx):
original_cx = cx
cx = cx.new_struct_scope()
# Initially create empty struct type.
struct_name = type_check_node(node.name, cx)
param_types = [
cx.region_forest.add(
types.Region(param.name, types.RegionKind(None, None)))
for param in node.params.params]
region_types = [
cx.region_forest.add(
types.Region(region.name, types.RegionKind(None, None)))
for region in node.regions.regions]
struct_constraints = []
empty_field_map = OrderedDict()
struct_type = types.Struct(struct_name, param_types, region_types, struct_constraints, empty_field_map)
def_struct_type = types.Kind(type = struct_type)
# Insert the struct name into global scope.
original_cx.insert(node, struct_name, def_struct_type)
# Figure out the actual types for params and regions and
# insert them into struct scope.
for param, param_type in zip(node.params.params, param_types):
cx.insert(node, param.name, param_type)
param_type.kind = type_check_node(param.type, cx)
if not param_type.validate_regions():
raise types.TypeError(node, 'Region type is inconsistent with itself: %s' % param_type.pretty_kind())
for region, region_type in zip(node.regions.regions, region_types):
cx.insert(node, region.name, region_type)
region_type.kind = type_check_node(region.type, cx)
if not region_type.validate_regions():
raise types.TypeError(node, 'Region type is inconsistent with itself: %s' % region_type.pretty_kind())
struct_constraints = type_check_node(node.constraints, cx)
struct_type.constraints = struct_constraints
field_map = type_check_node(node.field_decls, cx)
struct_type.field_map = field_map
# Note: This simple check only works as long as mutual
# recursion is disallowed on structs.
for field_type in field_map.itervalues():
if field_type == struct_type:
raise types.TypeError(node, 'Struct may not contain itself')
return def_struct_type
@type_check_node.register(ast.StructName)
def _(node, cx):
return node.name
@type_check_node.register(ast.StructConstraints)
def _(node, cx):
return [type_check_node(constraint, cx) for constraint in node.constraints]
@type_check_node.register(ast.StructConstraint)
def _(node, cx):
lhs = type_check_node(node.lhs, cx)
rhs = type_check_node(node.rhs, cx)
if lhs.kind.contains_type != rhs.kind.contains_type:
raise types.TypeError(node, 'Type mismatch in region element types for constraint: %s and %s' % (
lhs.kind.contains_type, rhs.kind.contains_type))
constraint = types.Constraint(node.op, lhs, rhs)
return constraint
@type_check_node.register(ast.StructConstraintRegion)
def _(node, cx):
region_type = cx.lookup(node, node.name)
assert types.is_region(region_type)
return region_type
@type_check_node.register(ast.FieldDecls)
def _(node, cx):
return OrderedDict([
type_check_node(field_decl, cx)
for field_decl in node.field_decls])
@type_check_node.register(ast.FieldDecl)
def _(node, cx):
field_kind = type_check_node(node.field_type, cx)
return (node.name, field_kind.type)
@type_check_node.register(ast.Function)
def _(node, cx):
original_cx = cx
cx = cx.new_function_scope()
fn_name = type_check_node(node.name, cx)
param_types = type_check_node(node.params, cx)
cx.privileges = type_check_node(node.privileges, cx)
return_kind = type_check_node(node.return_type, cx)
assert types.is_kind(return_kind)
return_type = return_kind.type
fn_type = types.Function(param_types, cx.privileges, return_type)
# Insert function name into global scope. Second insert
# prevents parameters from shadowing function name.
original_cx.insert(node, fn_name, fn_type)
cx.insert(node, fn_name, fn_type)
type_check_node(node.block, cx.with_return_type(return_type))
return fn_type
@type_check_node.register(ast.FunctionName)
def _(node, cx):
return node.name
@type_check_node.register(ast.FunctionParams)
def _(node, cx):
return [type_check_node(param, cx)
for param in node.params]
@type_check_node.register(ast.FunctionParam)
def _(node, cx):
if isinstance(node.declared_type, ast.TypeRegionKind):
# Region types may be self-referential. Insert regions
# into scope early to handle recursive types.
region_type = types.Region(node.name, types.RegionKind(None, None))
cx.region_forest.add(region_type)
cx.insert(node, node.name, region_type)
region_kind = type_check_node(node.declared_type, cx)
region_type.kind = region_kind
if not region_type.validate_regions():
raise types.TypeError(node, 'Region type is inconsistent with itself: %s' % region_type.pretty_kind())
return region_type
if isinstance(node.declared_type, ast.TypeArrayKind):
# Region types may be self-referential. Insert regions
# into scope early to handle recursive types.
region_type = types.Region(node.name, types.RegionKind(None, None))
cx.region_forest.add(region_type)
cx.insert(node, node.name, region_type)
region_kind = type_check_node(node.declared_type, cx)
region_type.kind = region_kind
return region_type
if isinstance(node.declared_type, ast.TypeIspaceKind):
ispace_kind = type_check_node(node.declared_type, cx)
ispace_type = types.Ispace(node.name, ispace_kind)
cx.insert(node, node.name, ispace_type)
return ispace_type
# Handle non-region types:
declared_kind = type_check_node(node.declared_type, cx)
assert types.is_kind(declared_kind)
declared_type = declared_kind.type
if types.is_void(declared_type):
raise types.TypeError(node, 'Task parameters are not allowed to be void')
if not types.is_concrete(declared_type):
raise types.TypeError(node, 'Task parameters are not allowed to contain wildcards')
assert types.allows_var_binding(declared_type)
reference_type = types.StackReference(declared_type)
cx.insert(node, node.name, reference_type)
return declared_type
@type_check_node.register(ast.FunctionReturnType)
def _(node, cx):
return type_check_node(node.declared_type, cx)
@type_check_node.register(ast.FunctionPrivileges)
def _(node, cx):
return cx.privileges | set(
privilege
for privilege_node in node.privileges
for privilege in type_check_node(privilege_node, cx))
@type_check_node.register(ast.FunctionPrivilege)
def _(node, cx):
return type_check_node(node.privilege, cx)
@type_check_node.register(ast.TypeVoid)
def _(node, cx):
return types.Kind(types.Void())
@type_check_node.register(ast.TypeBool)
def _(node, cx):
return types.Kind(types.Bool())
@type_check_node.register(ast.TypeDouble)
def _(node, cx):
return types.Kind(types.Double())
@type_check_node.register(ast.TypeFloat)
def _(node, cx):
return types.Kind(types.Float())
@type_check_node.register(ast.TypeInt)
def _(node, cx):
return types.Kind(types.Int())
@type_check_node.register(ast.TypeUInt)
def _(node, cx):
return types.Kind(types.UInt())
@type_check_node.register(ast.TypeInt8)
def _(node, cx):
return types.Kind(types.Int8())
@type_check_node.register(ast.TypeInt16)
def _(node, cx):
return types.Kind(types.Int16())
@type_check_node.register(ast.TypeInt32)
def _(node, cx):
return types.Kind(types.Int32())
@type_check_node.register(ast.TypeInt64)
def _(node, cx):
return types.Kind(types.Int64())
@type_check_node.register(ast.TypeUInt8)
def _(node, cx):
return types.Kind(types.UInt8())
@type_check_node.register(ast.TypeUInt16)
def _(node, cx):
return types.Kind(types.UInt16())
@type_check_node.register(ast.TypeUInt32)
def _(node, cx):
return types.Kind(types.UInt32())
@type_check_node.register(ast.TypeUInt64)
def _(node, cx):
return types.Kind(types.UInt64())
@type_check_node.register(ast.TypeColoring)
def _(node, cx):
region = type_check_node(node.region, cx)
if not (types.is_region(region) or types.is_ispace(region)):
raise types.TypeError(node, 'Type mismatch in type %s: expected %s but got %s' % (
'coloring', 'a region or ispace', region))
return types.Kind(types.Coloring(region))
@type_check_node.register(ast.TypeColoringRegion)
def _(node, cx):
return cx.lookup(node, node.name)
@type_check_node.register(ast.TypeID)
def _(node, cx):
kind = cx.lookup(node, node.name)
args = type_check_node(node.args, cx)
if not types.is_kind(kind):
raise types.TypeError(node, 'Type mismatch in type %s: expected a type but got %s' % (
node.name, kind))
if len(args) != len(kind.type.params):
raise types.TypeError(node, 'Incorrect number of arguments for struct %s: expected %s but got %s' % (
node.name, len(kind.type.params), len(args)))
region_map = dict([
(old_region, new_region)
for old_region, new_region in zip(kind.type.params, args)])
for param, arg in zip(kind.type.params, args):
assert types.is_region(param)
if types.is_region(arg):
if param.kind.contains_type is not None and arg.kind.contains_type is not None:
param_kind = param.kind.substitute_regions(region_map)
arg_kind = arg.kind
if param_kind != arg_kind:
raise types.TypeError(node, 'Type mismatch in type parameter to %s: expected %s but got %s' % (
node.name, param_kind, arg_kind))
elif types.is_region_wild(arg):
pass
else:
assert False
return kind.instantiate_params(region_map)
@type_check_node.register(ast.TypeArgs)
def _(node, cx):
return [type_check_node(arg, cx) for arg in node.args]
@type_check_node.register(ast.TypeArg)
def _(node, cx):
arg = cx.lookup(node, node.name)
if not types.is_region(arg):
raise types.TypeError(node, 'Type mismatch in type %s: expected a region but got %s' % (
node.name, arg))
return arg
@type_check_node.register(ast.TypeArgWild)
def _(node, cx):
return types.RegionWild()
@type_check_node.register(ast.TypePointer)
def _(node, cx):
points_to_kind = type_check_node(node.points_to_type, cx)
regions = type_check_node(node.regions, cx)
assert types.is_kind(points_to_kind)
points_to_type = points_to_kind.type
for region in regions:
if types.is_region(region):
contains_type = region.kind.contains_type
if contains_type is not None and contains_type != points_to_type:
raise types.TypeError(node, 'Type mismatch in pointer type: expected %s but got %s' % (
contains_type, points_to_type))
elif types.is_region_wild(region):
pass
else:
if not types.is_kind(region):
raise types.TypeError(node, 'Type mismatch in pointer type: expected a region but got %s' % (
region))
raise types.TypeError(node, 'Type mismatch in pointer type: expected a region but got %s' % (
region.type))
return types.Kind(types.Pointer(points_to_type, regions))
@type_check_node.register(ast.TypePointerRegions)
def _(node, cx):
return [type_check_node(region, cx)
for region in node.regions]
@type_check_node.register(ast.TypeRegion)
def _(node, cx):
region_type = cx.lookup(node, node.name)
return region_type
@type_check_node.register(ast.TypeRegionWild)
def _(node, cx):
return types.RegionWild()
@type_check_node.register(ast.TypeRegionKind)
def _(node, cx):
contains_type = None
if node.contains_type is not None:
contains_type = type_check_node(node.contains_type, cx).type
return types.RegionKind(None, contains_type)
@type_check_node.register(ast.TypeArrayKind)
def _(node, cx):
ispace = type_check_node(node.ispace, cx)
contains_type = type_check_node(node.contains_type, cx).type
return types.RegionKind(ispace, contains_type)
@type_check_node.register(ast.TypeIspace)
def _(node, cx):
ispace_type = cx.lookup(node, node.name)
return ispace_type
@type_check_node.register(ast.TypeIspaceKind)
def _(node, cx):
index_type = type_check_node(node.index_type, cx).type
return types.IspaceKind(index_type)
@type_check_node.register(ast.Privilege)
def _(node, cx):
if node.privilege == 'reads':
privilege = types.Privilege.READ
elif node.privilege == 'writes':
privilege = types.Privilege.WRITE
elif node.privilege == 'reduces':
privilege = types.Privilege.REDUCE
else:
assert False
regions = type_check_node(node.regions, cx)
return [
types.Privilege(node, privilege, node.op, region, field_path)
for region, field_path in regions]
@type_check_node.register(ast.PrivilegeRegions)
def _(node, cx):
return [
region
for region_node in node.regions
for region in type_check_node(region_node, cx)]
@type_check_node.register(ast.PrivilegeRegion)
def _(node, cx):
region = cx.lookup(node, node.name)
field_paths = type_check_node(node.fields, cx)
return [(region, field_path) for field_path in field_paths]
@type_check_node.register(ast.PrivilegeRegionFields)
def _(node, cx):
if len(node.fields) == 0:
return [()]
return [
field_path
for field_node in node.fields
for field_path in type_check_node(field_node, cx)]
@type_check_node.register(ast.PrivilegeRegionField)
def _(node, cx):
prefix = (node.name,)
field_paths = type_check_node(node.fields, cx)
return [prefix + field_path for field_path in field_paths]
@type_check_node.register(ast.Block)
def _(node, cx):
cx = cx.new_block_scope()
for expr in node.block:
type_check_node(expr, cx)
return types.Void()
@type_check_node.register(ast.StatementAssert)
def _(node, cx):
expr_type = type_check_node(node.expr, cx).check_read(node.expr, cx)
if not types.is_bool(expr_type):
raise types.TypeError(node, 'Type mismatch in assert statement: expected %s but got %s' % (
types.Bool(), expr_type))
return types.Void()
@type_check_node.register(ast.StatementExpr)
def _(node, cx):
type_check_node(node.expr, cx).check_read(node.expr, cx)
return types.Void()
@type_check_node.register(ast.StatementIf)
def _(node, cx):
condition_type = type_check_node(node.condition, cx).check_read(node.condition, cx)
type_check_node(node.then_block, cx)
if node.else_block is not None:
type_check_node(node.else_block, cx)
if not types.is_bool(condition_type):
raise types.TypeError(node, 'If condition expression is not type bool')
return types.Void()
@type_check_node.register(ast.StatementFor)
def _(node, cx):
cx = cx.new_block_scope()
index_types = type_check_node(node.indices, cx)
region_types = type_check_node(node.regions, cx)
if len(index_types) != len(region_types):
raise types.TypeError(node, 'Incorrect number of indices in for statement: expected %s but got %s' % (
len(region_types), len(index_types)))
# Two forms of iteration are supported, over a single index
# space, or over any number of regions. In the case where
# multiple regions are being iterated, it is assumed the
# regions have the same index space. At the moment this has to
# be checked dynamically to be sound.
if len(region_types) == 1 and types.is_ispace(region_types[0]):
index_node = node.indices.indices[0]
index_type = index_types[0]
ispace_type = region_types[0]
# We can infer the index type if unspecified.
if index_type is None:
index_type = ispace_type.kind.index_type
if index_type != ispace_type.kind.index_type:
raise types.TypeError(node, 'Type mismatch in for statement: expected %s but got %s' % (
index_type, ispace_type.kind.index_type))
# Patch environment and type map to know about the inferred index type.
cx.insert(node, index_node.name, index_type)
cx.type_map[index_node] = index_type
else:
for index_node, index_type, region_type, index \
in zip(node.indices.indices, index_types, region_types, xrange(len(index_types))):
if not types.is_region(region_type):
raise types.TypeError(node, 'Type mismatch on index %s of for statement: expected a region but got %s' % (
index, region_type))
# We can infer the index type as long as the region is explicitly typed.
if index_type is None:
if region_type.kind.contains_type is None:
raise types.TypeError(node, 'Unable to infer type of index %s of for statement: region %s has no element type' % (
index, region_type))
index_type = types.Pointer(region_type.kind.contains_type, [region_type])
if not types.is_pointer(index_type):
raise types.TypeError(node, 'Type mismatch on index %s of for statement: expected a pointer but got %s' % (
index, index_type))
if len(index_type.regions) != 1 or index_type.regions[0] != region_type:
raise types.TypeError(node, 'Type mismatch on index %s of for statement: expected %s but got %s' % (
index, index_type,
types.Pointer(region_type.kind.contains_type, [region_type])))
# Patch environment and type map to know about the inferred index type.
cx.insert(node, index_node.name, index_type)
cx.type_map[index_node] = index_type
type_check_node(node.block, cx)
return types.Void()
@type_check_node.register(ast.ForIndices)
def _(node, cx):
return [type_check_node(index, cx)
for index in node.indices]
@type_check_node.register(ast.ForIndex)
def _(node, cx):
if node.type is not None:
declared_kind = type_check_node(node.type, cx)
assert types.is_kind(declared_kind)
return declared_kind.type
return None
@type_check_node.register(ast.ForRegions)
def _(node, cx):
return [type_check_node(region, cx)
for region in node.regions]
@type_check_node.register(ast.ForRegion)
def _(node, cx):
region_type = cx.lookup(node, node.name)
return region_type
@type_check_node.register(ast.StatementLet)
def _(node, cx):
declared_type = None
if node.type is not None:
declared_kind = type_check_node(node.type, cx)
if types.is_region_kind(declared_kind):
declared_type = types.Region(node.name, declared_kind)
cx.region_forest.add(declared_type)
if types.is_kind(declared_kind):
declared_type = declared_kind.type
else:
assert False
expr_type = type_check_node(node.expr, cx).check_read(node.expr, cx)
# Hack: Rather full type inference, which gets ugly fast, just
# implement "auto-style" inference by using the expression
# type if no type declaration is provided.
if declared_type is None:
if types.is_region(expr_type):
declared_type = types.Region(node.name, expr_type.kind)
cx.region_forest.add(declared_type)
else:
declared_type = expr_type
if not types.is_concrete(declared_type):
raise types.TypeError(node, 'Let bound expressions are not allowed to contain wildcards')
if types.is_void(declared_type):
raise types.TypeError(node, 'Let bound expressions are not allowed to be void')
if types.is_region(expr_type) and types.is_region(declared_type):
if expr_type.kind != declared_type.kind:
raise types.TypeError(node, 'Let bound expression of type %s does not match declared type %s' % (
expr_type.kind, declared_type.kind))
else:
if expr_type != declared_type:
raise types.TypeError(node, 'Let bound expression of type %s does not match declared type %s' % (
expr_type, declared_type))
cx.insert(node, node.name, declared_type, shadow = True)
if types.is_region(expr_type):
cx.region_forest.union(declared_type, expr_type)
cx.constraints.add(
types.Constraint(lhs = expr_type, op = types.Constraint.SUBREGION, rhs = declared_type))
cx.constraints.add(
types.Constraint(lhs = declared_type, op = types.Constraint.SUBREGION, rhs = expr_type))
return declared_type
@type_check_node.register(ast.StatementLetRegion)
def _(node, cx):
region_type = types.Region(node.name, types.RegionKind(None, None))
cx.region_forest.add(region_type)
# Insert region name into scope so that element type can refer to it.
cx.insert(node, node.name, region_type)
declared_region_kind = None
if node.region_kind is not None:
declared_region_kind = type_check_node(node.region_kind, cx)
element_kind = type_check_node(node.element_type, cx)
size_type = type_check_node(node.size_expr, cx).check_read(node.size_expr, cx)
assert types.is_kind(element_kind) and not types.is_void(element_kind.type)
if not types.is_int(size_type):
raise types.TypeError(node, 'Type mismatch in region: expected %s but got %s' % (
types.Int(), size_type))
# Now patch region type so that it refers to the contained type.
region_kind = types.RegionKind(None, element_kind.type)
region_type.kind = region_kind
if not region_type.validate_regions():
raise types.TypeError(node, 'Region type is inconsistent with itself: %s' % region_type.pretty_kind())
if declared_region_kind is None:
declared_region_kind = region_kind
if declared_region_kind != region_kind:
raise types.TypeError(node, 'Let bound expression of type %s does not match declared type %s' % (
region_kind, declared_region_kind))
cx.privileges.add(types.Privilege(node, types.Privilege.READ, None, region_type, ()))
cx.privileges.add(types.Privilege(node, types.Privilege.WRITE, None, region_type, ()))
return region_type
@type_check_node.register(ast.StatementLetArray)
def _(node, cx):
ispace_type = type_check_node(node.ispace_type, cx)
region_type = types.Region(node.name, types.RegionKind(ispace_type, None))
cx.region_forest.add(region_type)
# insert region name into scope so that element type can refer to it
cx.insert(node, node.name, region_type)
declared_region_kind = None
if node.region_kind is not None:
declared_region_kind = type_check_node(node.region_kind, cx)
element_kind = type_check_node(node.element_type, cx)
assert types.is_kind(element_kind) and not types.is_void(element_kind.type)
# now patch region type so that it refers to the contained type
region_kind = types.RegionKind(ispace_type, element_kind.type)
region_type.kind = region_kind
if declared_region_kind is None:
declared_region_kind = region_kind
if declared_region_kind != region_kind:
raise types.TypeError(node, 'Let bound expression of type %s does not match declared type %s' % (
region_kind, declared_region_kind))
cx.privileges.add(types.Privilege(node, types.Privilege.READ, None, region_type, ()))
cx.privileges.add(types.Privilege(node, types.Privilege.WRITE, None, region_type, ()))
return region_type
@type_check_node.register(ast.StatementLetIspace)
def _(node, cx):
declared_ispace_kind = None
if node.ispace_kind is not None:
declared_ispace_kind = type_check_node(node.ispace_kind, cx)
index_kind = type_check_node(node.index_type, cx)
size_type = type_check_node(node.size_expr, cx).check_read(node.size_expr, cx)
assert types.is_kind(index_kind) and types.is_int(index_kind.type)
if not types.is_int(size_type):
raise types.TypeError(node, 'Type mismatch in ispace: expected %s but got %s' % (
types.Int(), size_type))
ispace_kind = types.IspaceKind(index_kind.type)
if declared_ispace_kind is None:
declared_ispace_kind = ispace_kind
if declared_ispace_kind != ispace_kind:
raise types.TypeError(node, 'Let bound expression of type %s does not match declared type %s' % (
ispace_kind, declared_ispace_kind))
ispace_type = types.Ispace(node.name, ispace_kind)
cx.insert(node, node.name, ispace_type)
return types.Void()
@type_check_node.register(ast.StatementLetPartition)
def _(node, cx):
region_type = type_check_node(node.region_type, cx).check_read(node.region_type, cx)
mode = type_check_node(node.mode, cx)
coloring_type = type_check_node(node.coloring_expr, cx).check_read(node.coloring_expr, cx)
if not (types.is_region(region_type) or types.is_ispace(region_type)):
raise types.TypeError(node, 'Type mismatch in partition: expected a region or ispace but got %s' % (
region_type))
expected_coloring_type = types.Coloring(region_type)
if coloring_type != expected_coloring_type:
raise types.TypeError(node, 'Type mismatch in partition: expected %s but got %s' % (
expected_coloring_type, coloring_type))
partition_kind = types.PartitionKind(region_type, mode)
partition_type = types.Partition(node.name, partition_kind)
cx.insert(node, node.name, partition_type)
return partition_type
@type_check_node.register(ast.PartitionMode)
def _(node, cx):
if node.mode == 'disjoint':
return types.Partition.DISJOINT
elif node.mode == 'aliased':
return types.Partition.ALIASED
assert False
@type_check_node.register(ast.StatementReturn)
def _(node, cx):
expr_type = type_check_node(node.expr, cx).check_read(node.expr, cx)
if expr_type != cx.return_type:
raise types.TypeError(node, 'Returned expression of type %s does not match declared return type %s' % (
expr_type, cx.return_type))
return types.Void()
@type_check_node.register(ast.StatementUnpack)
def _(node, cx):
expr_type = type_check_node(node.expr, cx).check_read(node.expr, cx)
declared_kind = type_check_node(node.type, cx)
assert types.is_kind(declared_kind)
declared_type = declared_kind.type
if not types.is_struct(expr_type):
raise types.TypeError(node, 'Type mismatch in unpack: expected %s but got %s' % (
'a struct', expr_type))
region_types = type_check_node(node.regions, cx)
for region, region_type in zip(node.regions.regions, region_types):
cx.insert(node, region.name, region_type) # FIXME: handle shadowing
region_map = dict(zip(declared_type.regions, region_types))
actual_type = declared_type.instantiate_regions(region_map)
# Patch regions so that they contain the correct type.
for region_type, declared_region_type in zip(region_types, declared_type.regions):
region_type.kind = declared_region_type.kind.substitute_regions(region_map)
if expr_type != declared_type:
raise types.TypeError(node, 'Type mismatch in unpack: expected %s but got %s' % (
declared_type, expr_type))
cx.insert(node, node.name, actual_type) # FIXME: handle shadowing
cx.constraints.update(actual_type.constraints)
return region_types
@type_check_node.register(ast.UnpackRegions)
def _(node, cx):
return [type_check_node(region, cx) for region in node.regions]
@type_check_node.register(ast.UnpackRegion)
def _(node, cx):
# Create regions with empty region_types initially, patch later.
region_type = types.Region(node.name, types.RegionKind(None, None))
cx.region_forest.add(region_type)
return region_type
@type_check_node.register(ast.StatementVar)
def _(node, cx):
declared_type = None
if node.type is not None:
declared_kind = type_check_node(node.type, cx)
assert types.is_kind(declared_kind)
declared_type = declared_kind.type
expr_type = type_check_node(node.expr, cx).check_read(node.expr, cx)
# Hack: Rather full type inference, which gets ugly fast, just
# implement "auto-style" inference by using the expression
# type if no type declaration is provided.
if declared_type is None:
declared_type = expr_type
if not types.is_concrete(declared_type):
raise types.TypeError(node, 'Variables are not allowed to contain wildcards')
if expr_type != declared_type:
raise types.TypeError(node, 'Variable initializer of type %s does not match declared type %s' % (
expr_type, declared_type))
assert types.allows_var_binding(declared_type)
reference_type = types.StackReference(declared_type)
cx.insert(node, node.name, reference_type, shadow = True)
return types.Void()
@type_check_node.register(ast.StatementWhile)
def _(node, cx):
condition_type = type_check_node(node.condition, cx).check_read(node.condition, cx)
type_check_node(node.block, cx)
if not types.is_bool(condition_type):
raise types.TypeError(node, 'While condition expression is not type bool')
return types.Void()
@type_check_node.register(ast.ExprID)
def _(node, cx):
id_type = cx.lookup(node, node.name)
return id_type
@type_check_node.register(ast.ExprAssignment)
def _(node, cx):
lval_type = type_check_node(node.lval, cx).check_write(node.lval, cx)
rval_type = type_check_node(node.rval, cx).check_read(node.rval, cx)
if lval_type != rval_type:
raise types.TypeError(node, 'Type mismatch in assignment: %s and %s' % (
lval_type, rval_type))
return rval_type
@type_check_node.register(ast.ExprUnaryOp)
def _(node, cx):
arg_type = type_check_node(node.arg, cx).check_read(node.arg, cx)
if not unary_operator_table[node.op][0](arg_type):
raise types.TypeError(node, 'Type mismatch in operand to unary operator: %s' % (
arg_type))
return unary_operator_table[node.op][1](arg_type)
@type_check_node.register(ast.ExprBinaryOp)
def _(node, cx):
lhs_type = type_check_node(node.lhs, cx).check_read(node.lhs, cx)
rhs_type = type_check_node(node.rhs, cx).check_read(node.rhs, cx)
if lhs_type != rhs_type:
raise types.TypeError(node, 'Type mismatch in operands to binary operator: %s and %s' % (
lhs_type, rhs_type))
if not binary_operator_table[node.op][0](lhs_type):
raise types.TypeError(node, 'Type mismatch in operand to binary operator: %s' % (
lhs_type))
if not binary_operator_table[node.op][0](rhs_type):
raise types.TypeError(node, 'Type mismatch in operand to binary operator: %s' % (
rhs_type))
return binary_operator_table[node.op][1](lhs_type, rhs_type)
@type_check_node.register(ast.ExprReduceOp)
def _(node, cx):
lhs_type = type_check_node(node.lhs, cx).check_reduce(node.lhs, node.op, cx)
rhs_type = type_check_node(node.rhs, cx).check_read(node.rhs, cx)
if lhs_type != rhs_type:
raise types.TypeError(node, 'Type mismatch in operands to binary operator: %s and %s' % (
lhs_type, rhs_type))
if not reduce_operator_table[node.op](lhs_type):
raise types.TypeError(node, 'Type mismatch in operand to binary operator: %s' % (
lhs_type))
if not reduce_operator_table[node.op](rhs_type):
raise types.TypeError(node, 'Type mismatch in operand to binary operator: %s' % (
rhs_type))
return types.Void()
@type_check_node.register(ast.ExprCast)
def _(node, cx):
cast_to_kind = type_check_node(node.cast_to_type, cx)
assert types.is_kind(cast_to_kind) and types.is_numeric(cast_to_kind.type)
expr_type = type_check_node(node.expr, cx).check_read(node.expr, cx)
if not types.is_numeric(expr_type):
raise types.TypeError(node, 'Type mismatch in cast: expected a number but got %s' % (
expr_type))
return cast_to_kind.type
@type_check_node.register(ast.ExprNull)
def _(node, cx):
pointer_kind = type_check_node(node.pointer_type, cx)
assert types.is_kind(pointer_kind) and types.is_pointer(pointer_kind.type)
return pointer_kind.type
@type_check_node.register(ast.ExprIsnull)
def _(node, cx):
pointer_type = type_check_node(node.pointer_expr, cx).check_read(node.pointer_expr, cx)
if not types.is_pointer(pointer_type):
raise types.TypeError(node, 'Type mismatch for argument %s in call to task %s: expected %s but got %s' % (
0, 'isnull', 'a pointer', pointer_type))
return types.Bool()
@type_check_node.register(ast.ExprNew)
def _(node, cx):
pointer_kind = type_check_node(node.pointer_type, cx)
assert types.is_kind(pointer_kind) and types.is_pointer(pointer_kind.type)
pointer_type = pointer_kind.type
if len(pointer_type.regions) != 1:
raise types.TypeError(node, 'Type mismatch in new: cannot allocate pointer with more than one region %s' % (
pointer_type))
region_type = pointer_type.regions[0]
if region_type.kind.ispace is not None:
raise types.TypeError(node, 'Type mismatch in new: cannot allocate into array %s' %
region_type)
return pointer_type
@type_check_node.register(ast.ExprRead)
def _(node, cx):
pointer_type = type_check_node(node.pointer_expr, cx).check_read(node.pointer_expr, cx)
if not types.is_pointer(pointer_type):
raise types.TypeError(node, 'Type mismatch in read: expected a pointer but got %s' % (
pointer_type))
privileges_requested = [
types.Privilege(node, types.Privilege.READ, None, region, ())
for region in pointer_type.regions]
success, failed_request = types.check_privileges(privileges_requested, cx)
if not success:
raise types.TypeError(node, 'Invalid privilege %s requested in read' % failed_request)
value_type = pointer_type.points_to_type
return value_type
@type_check_node.register(ast.ExprWrite)
def _(node, cx):
pointer_type = type_check_node(node.pointer_expr, cx).check_read(node.pointer_expr, cx)
value_type = type_check_node(node.value_expr, cx).check_read(node.value_expr, cx)
if not types.is_pointer(pointer_type):
raise types.TypeError(node, 'Type mismatch in write: expected a pointer but got %s' % (
pointer_type))
if pointer_type.points_to_type != value_type:
raise types.TypeError(node, 'Type mismatch in write: expected %s but got %s' % (
value_type, pointer_type.points_to_type))
privileges_requested = [
types.Privilege(node, types.Privilege.WRITE, None, region, ())
for region in pointer_type.regions]
success, failed_request = types.check_privileges(privileges_requested, cx)
if not success:
raise types.TypeError(node, 'Invalid privilege %s requested in write' % failed_request)
return types.Void()