150
|
1 // RUN: llvm-tblgen %s | FileCheck %s
|
|
2 // RUN: not llvm-tblgen -DERROR1 %s 2>&1 | FileCheck --check-prefix=ERROR1 %s
|
|
3 // RUN: not llvm-tblgen -DERROR2 %s 2>&1 | FileCheck --check-prefix=ERROR2 %s
|
|
4 // RUN: not llvm-tblgen -DERROR3 %s 2>&1 | FileCheck --check-prefix=ERROR3 %s
|
252
|
5 // RUN: not llvm-tblgen -DERROR4 %s 2>&1 | FileCheck --check-prefix=ERROR4 %s
|
|
6 // RUN: not llvm-tblgen -DERROR5 %s 2>&1 | FileCheck --check-prefix=ERROR5 %s
|
150
|
7
|
|
8 #ifdef ERROR1
|
|
9 // Refer to a variable we haven't defined *yet*, expecting an error.
|
|
10 // ERROR1: [[@LINE+1]]:22: error: Variable not defined: 'myvar'
|
|
11 def bad { dag x = (? myvar); }
|
|
12 #endif
|
|
13
|
|
14 // Define a global variable.
|
|
15 defvar myvar = "foo";
|
|
16
|
|
17 #ifdef ERROR2
|
|
18 // Demonstrate an error when a global variable is redefined.
|
|
19 // ERROR2: [[@LINE+1]]:8: error: def or global variable of this name already exists
|
|
20 defvar myvar = "another value";
|
|
21 #endif
|
|
22
|
|
23 multiclass Test<int x> {
|
|
24 // Refer to a global variable, while inside a local scope like a multiclass.
|
|
25 def _with_global_string { string s = myvar; }
|
|
26
|
|
27 // Define some variables local to this multiclass, and prove we can refer to
|
|
28 // those too.
|
|
29 defvar myvar = !add(x, 100);
|
|
30 defvar myvar2 = "string of " # myvar;
|
|
31 def _with_local_int { int i = myvar; string s = myvar2; }
|
|
32
|
|
33 #ifdef ERROR3
|
|
34 // Demonstrate an error when a local variable is redefined.
|
|
35 // ERROR3: [[@LINE+1]]:10: error: local variable of this name already exists
|
|
36 defvar myvar = "another value";
|
|
37 #endif
|
|
38 }
|
|
39
|
|
40 // Instantiate the above multiclass, and expect all the right outputs.
|
|
41
|
|
42 // CHECK: def aaa_with_global_string {
|
|
43 // CHECK-NEXT: string s = "foo";
|
|
44 // CHECK: def aaa_with_local_int {
|
|
45 // CHECK-NEXT: int i = 101;
|
|
46 // CHECK-NEXT: string s = "string of 101";
|
|
47 // CHECK: def bbb_with_global_string {
|
|
48 // CHECK-NEXT: string s = "foo";
|
|
49 // CHECK: def bbb_with_local_int {
|
|
50 // CHECK-NEXT: int i = 102;
|
|
51 // CHECK-NEXT: string s = "string of 102";
|
|
52 defm aaa: Test<1>;
|
|
53 defm bbb: Test<2>;
|
|
54
|
|
55 // Test that local variables can be defined inside a foreach block, and inside
|
|
56 // an object body.
|
|
57 //
|
|
58 // The scopes nest (you can refer to variables in an outer block from an inner
|
|
59 // one), and the variables go out of scope again at the end of the block (in
|
|
60 // particular, you don't get a redefinition error the next time round the
|
|
61 // loop).
|
|
62
|
|
63 // CHECK: def nest_f1_s3 {
|
|
64 // CHECK-NEXT: int member = 113;
|
|
65 // CHECK-NEXT: }
|
|
66 // CHECK: def nest_f1_s4 {
|
|
67 // CHECK-NEXT: int member = 114;
|
|
68 // CHECK-NEXT: }
|
|
69 // CHECK: def nest_f2_s3 {
|
|
70 // CHECK-NEXT: int member = 123;
|
|
71 // CHECK-NEXT: }
|
|
72 // CHECK: def nest_f2_s4 {
|
|
73 // CHECK-NEXT: int member = 124;
|
|
74 // CHECK-NEXT: }
|
|
75 foreach first = [ 1, 2 ] in {
|
|
76 defvar firstStr = "f" # first;
|
|
77 foreach second = [ 3, 4 ] in {
|
|
78 defvar secondStr = "s" # second;
|
|
79 def "nest_" # firstStr # "_" # secondStr {
|
|
80 defvar defLocalVariable = !add(!mul(first, 10), second);
|
|
81 int member = !add(100, defLocalVariable);
|
|
82 }
|
|
83 }
|
|
84 }
|
|
85 defvar firstStr = "now define this at the top level and still expect no error";
|
|
86
|
|
87 // Test that you can shadow an outer declaration with an inner one. Here, we
|
|
88 // expect all the shadowOuter records (both above and below the inner foreach)
|
|
89 // to get the value 1 from the outer definition of shadowedVariable, and the
|
|
90 // shadowInner ones to get 2 from the inner definition.
|
|
91
|
|
92 // CHECK: def shadowInner11 {
|
|
93 // CHECK-NEXT: int var = 2;
|
|
94 // CHECK: def shadowInner12 {
|
|
95 // CHECK-NEXT: int var = 2;
|
|
96 // CHECK: def shadowInner21 {
|
|
97 // CHECK-NEXT: int var = 2;
|
|
98 // CHECK: def shadowInner22 {
|
|
99 // CHECK-NEXT: int var = 2;
|
|
100 // CHECK: def shadowInnerIf1 {
|
|
101 // CHECK-NEXT: int var = 3;
|
|
102 // CHECK: def shadowOuterAbove1 {
|
|
103 // CHECK-NEXT: int var = 1;
|
|
104 // CHECK: def shadowOuterAbove2 {
|
|
105 // CHECK-NEXT: int var = 1;
|
|
106 // CHECK: def shadowOuterBelowForeach1 {
|
|
107 // CHECK-NEXT: int var = 1;
|
|
108 // CHECK: def shadowOuterBelowForeach2 {
|
|
109 // CHECK-NEXT: int var = 1;
|
|
110 // CHECK: def shadowOuterBelowIf1 {
|
|
111 // CHECK-NEXT: int var = 1;
|
|
112 // CHECK: def shadowOuterBelowIf2 {
|
|
113 // CHECK-NEXT: int var = 1;
|
|
114
|
|
115 foreach first = [ 1, 2 ] in {
|
|
116 defvar shadowedVariable = 1;
|
|
117 def shadowOuterAbove # first { int var = shadowedVariable; }
|
|
118
|
|
119 // The foreach statement opens a new scope, in which a new variable of the
|
|
120 // same name can be defined without clashing with the outer one.
|
|
121 foreach second = [ 1, 2 ] in {
|
|
122 defvar shadowedVariable = 2;
|
|
123 def shadowInner # first # second { int var = shadowedVariable; }
|
|
124 }
|
|
125
|
|
126 // Now the outer variable is back in scope.
|
|
127 def shadowOuterBelowForeach # first { int var = shadowedVariable; }
|
|
128
|
|
129 // An if statement also opens a new scope.
|
|
130 if !eq(first, 1) then {
|
|
131 defvar shadowedVariable = 3;
|
|
132 def shadowInnerIf # first { int var = shadowedVariable; }
|
|
133 }
|
|
134
|
|
135 // Now the outer variable is back in scope again.
|
|
136 def shadowOuterBelowIf # first { int var = shadowedVariable; }
|
|
137 }
|
|
138
|
252
|
139 class RedefinitionTest<int a, int b> {
|
|
140 #ifdef ERROR4
|
|
141 defvar value = !add(a, b);
|
|
142 #endif
|
|
143 // ERROR4: [[@LINE+1]]:7: error: local variable of this name already exists
|
|
144 int value = !add(a, b);
|
|
145 #ifdef ERROR5
|
|
146 // ERROR5: [[@LINE+1]]:10: error: field of this name already exists
|
|
147 defvar value = !add(a, b);
|
|
148 #endif
|
|
149 }
|
|
150
|
|
151 // These variables should be shadowed by class/multiclass template arguments.
|
|
152 defvar a = 2333;
|
|
153 defvar b = 2333;
|
|
154 defvar c = 2333;
|
|
155 class ShadowGlobalsTest<int a, int b, int c> {
|
|
156 // Template arguments have higher priorities than global variables.
|
|
157 int value = !add(a, b, c);
|
|
158 }
|
|
159
|
|
160 class ShadowClassArgumentTest<int a, int b, int c> {
|
|
161 // Local variable 'c' has higher priority than class template argument 'c'.
|
|
162 defvar c = !add(c, c);
|
|
163 int value = !add(a, b, c);
|
|
164 }
|
|
165
|
|
166 multiclass ShadowMulticlassArgumentTest<int a, int b, int c> {
|
|
167 // Local variable 'c' has higher priority than multiclass template argument 'c'.
|
|
168 defvar c = !add(c, c);
|
|
169 def "" {
|
|
170 int value = !add(a, b, c);
|
|
171 }
|
|
172 }
|
|
173
|
|
174 // CHECK: def shadowTestOfClassArgument {
|
|
175 // CHECK-NEXT: int value = 11;
|
|
176 // CHECK: def shadowTestOfGlobals {
|
|
177 // CHECK-NEXT: int value = 8;
|
|
178 // CHECK: def shadowTestOfLoopIterator0 {
|
|
179 // CHECK-NEXT: int value = 10;
|
|
180 // CHECK: def shadowTestOfLoopIterator1 {
|
|
181 // CHECK-NEXT: int value = 11;
|
|
182 // CHECK: def shadowTestOfMulticlassArgument {
|
|
183 // CHECK-NEXT: int value = 11;
|
|
184 def shadowTestOfClassArgument: ShadowClassArgumentTest<2, 3, 3>;
|
|
185 def shadowTestOfGlobals: ShadowGlobalsTest<2, 3, 3>;
|
|
186 foreach i = 0...1 in {
|
|
187 // Local variable 'i' has higher priority than loop iterator 'i'.
|
|
188 def shadowTestOfLoopIterator # i {
|
|
189 defvar i = !add(10, i);
|
|
190 int value = i;
|
|
191 }
|
|
192 }
|
|
193 defm shadowTestOfMulticlassArgument: ShadowMulticlassArgumentTest<2, 3, 3>;
|
|
194
|
150
|
195 // Test that a top-level let statement also makes a variable scope (on the
|
|
196 // general principle of consistency, because it defines a braced sub-block).
|
|
197
|
|
198 let someVariable = "some value" in {
|
|
199 defvar myvar = "override the definition from above and expect no error";
|
|
200 }
|
|
201 // CHECK: def topLevelLetTest {
|
|
202 // CHECK-NEXT: string val = "foo";
|
|
203 def topLevelLetTest { string val = myvar; }
|