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
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
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); }
14 // Define a global variable.
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";
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; }
27 // Define some variables local to this multiclass, and prove we can refer to
29 defvar myvar = !add(x, 100);
30 defvar myvar2 = "string of " # myvar;
31 def _with_local_int { int i = myvar; string s = myvar2; }
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";
40 // Instantiate the above multiclass, and expect all the right outputs.
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";
55 // Test that local variables can be defined inside a foreach block, and inside
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
63 // CHECK: def nest_f1_s3 {
64 // CHECK-NEXT: int member = 113;
66 // CHECK: def nest_f1_s4 {
67 // CHECK-NEXT: int member = 114;
69 // CHECK: def nest_f2_s3 {
70 // CHECK-NEXT: int member = 123;
72 // CHECK: def nest_f2_s4 {
73 // CHECK-NEXT: int member = 124;
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);
85 defvar firstStr = "now define this at the top level and still expect no error";
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.
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;
115 foreach first = [ 1, 2 ] in {
116 defvar shadowedVariable = 1;
117 def shadowOuterAbove # first { int var = shadowedVariable; }
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; }
126 // Now the outer variable is back in scope.
127 def shadowOuterBelowForeach # first { int var = shadowedVariable; }
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; }
135 // Now the outer variable is back in scope again.
136 def shadowOuterBelowIf # first { int var = shadowedVariable; }
139 class RedefinitionTest<int a, int b> {
141 defvar value = !add(a, b);
143 // ERROR4: [[@LINE+1]]:7: error: local variable of this name already exists
144 int value = !add(a, b);
146 // ERROR5: [[@LINE+1]]:10: error: field of this name already exists
147 defvar value = !add(a, b);
151 // These variables should be shadowed by class/multiclass template arguments.
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);
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);
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);
170 int value = !add(a, b, c);
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);
193 defm shadowTestOfMulticlassArgument: ShadowMulticlassArgumentTest<2, 3, 3>;
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).
198 let someVariable = "some value" in {
199 defvar myvar = "override the definition from above and expect no error";
201 // CHECK: def topLevelLetTest {
202 // CHECK-NEXT: string val = "foo";
203 def topLevelLetTest { string val = myvar; }