-
Notifications
You must be signed in to change notification settings - Fork 1
/
constraints.c
288 lines (263 loc) · 8.9 KB
/
constraints.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "data.h"
#include "auxiliary.h"
#include "phases.h"
static int errors = 0;
static void checkTreeHasSetup(AstNode *tree);
static void checkSwitchHasDefault(AstNode *tree);
static void checkReceiveHasDefault(AstNode *tree);
static void checkNotGlobalVar(AstNode *tree);
static void checkHasMaxOneMessageBlock(AstNode *tree);
static void checkVarInitialized(AstNode *tree);
static void checkAllSpawnsInSetup(AstNode *prog, AstNode *stmts);
static int countSpawns(AstNode *nodes, int recurse);
static void checkAllAdvancedInputInSetup(AstNode *prog, AstNode *stmts);
static int countFunctioncall(char *name, AstNode *nodes, int recurse);
static int checkHasReturn(AstNode *tree);
/* The following list of contextual constraints are checked by
this phase:
1) Each tree has one setup function with type setup(void)->void
2) Each switch has exactly one default case
3) Each receive has exactly one default case
4) No modification of global variables
5) Each tree has at most one messages block
6) All variables are initialized before use, or have a default value
7) All spawns appear as a statement directly in the setup function
8) All calls to advancedInputPin appear as a statement directly in the setup function
9) Check that all functions with return type != void actually returns.
*/
int contextualConstraintsCheck(AstNode *tree){
AstNode *children;
switch(tree->tag){
case Prog:
checkTreeHasSetup(tree);
checkHasMaxOneMessageBlock(tree);
break;
case Switch:
checkSwitchHasDefault(tree);
break;
case Receive:
checkReceiveHasDefault(tree);
break;
case Assignment:
checkNotGlobalVar(tree->node.Assignment.location);
break;
case UnaryOperation:
if(tree->node.UnaryOperation.operator == OpDecrement
|| tree->node.UnaryOperation.operator == OpIncrement)
checkNotGlobalVar(tree->node.UnaryOperation.expression);
break;
case VariableLocation:
case StructLocation:
case ArrayLocation:
checkVarInitialized(tree);
return errors;
case DefineFunction:
if(tree->node.DefineFunction.type->tag == BuiltinType &&
tree->node.DefineFunction.type->node.BuiltinType.type == BuiltinTypeVoid)
break;
else{
int hasRet = checkHasReturn(tree->node.DefineFunction.statements);
if(!hasRet){
errors++;
eprintf(tree->linenum, "Function '%s' doesn't have a return statement for all code paths.\n", prettyprint(tree->node.DefineFunction.identifier));
}
}
}
children = tree->children;
for(; children != NULL; children = children->chain)
contextualConstraintsCheck(children);
return errors;
}
static int checkHasReturn(AstNode *tree){
AstNode *child;
for(child = tree; child != NULL; child = child->chain){
switch(child->tag){
case Return:
return 1;
case If: /* we can't be sure with loops. In if switch and receive, we could check that all cases has a return, but we don't right now.*/
case Switch:
case Receive:
case For:
case While:
break;
default:
if(checkHasReturn(child->children))
return 1;
break;
}
}
return 0;
}
/* Look at all the toplevel declarations and see if a function
with the signature 'setup() -> void' exists */
static void checkTreeHasSetup(AstNode *tree){
AstNode *toplevels = tree->node.Prog.toplevels;
AstNode *node;
for(node = toplevels; node != NULL; node = node->next){
Symbol *s;
FunctionTypeDescriptor t;
if(node->tag != DefineFunction)
continue;
s = node->node.DefineFunction.identifier->node.Identifier.symbol;
if(strcmp("setup", s->name) != 0)
continue;
if(s->type->tag != FunctionTypeTag)
continue;
t = s->type->tags.typeFunction;
if(t.arity == 0
&& t.returnType->tag == BuiltinTypeTag
&& t.returnType->tags.typeBuiltin.builtinType == BuiltinTypeVoid){
checkAllSpawnsInSetup(tree, node->node.DefineFunction.statements);
checkAllAdvancedInputInSetup(tree, node->node.DefineFunction.statements);
return;
}
}
eprintf(1, "No setup function with type 'setup() -> void' found in program\n");
errors++;
}
/* go through all cases and check if there is exactly one default case */
static void checkSwitchHasDefault(AstNode *tree){
AstNode *cases = tree->node.Switch.cases;
AstNode *node;
int defaultCount = 0;
for(node = cases; node != NULL; node = node->next){
if(node->node.SwitchCase.literal == NULL)
defaultCount++;
}
if(defaultCount != 1){
errors++;
eprintf(tree->linenum, "Switch statements must have exactly one default case. This one has %d\n", defaultCount);
}
}
/* go through all cases and check if there is exactly one default case */
static void checkReceiveHasDefault(AstNode *tree){
AstNode *cases = tree->node.Receive.cases;
AstNode *node;
int defaultCount = 0;
for(node = cases; node != NULL; node = node->next){
if(node->node.ReceiveCase.messageName == NULL)
defaultCount++;
}
if(defaultCount != 1){
errors++;
eprintf(tree->linenum, "Receive statements must have exactly one default case. This one has %d\n", defaultCount);
}
}
/* Check if the tree is a location, and if so, check that it isn't global */
static void checkNotGlobalVar(AstNode *tree){
AstNode *id;
switch(tree->tag){
case VariableLocation:
id = tree->node.VariableLocation.identifier;
break;
case StructLocation:
id = tree->node.StructLocation.identifier;
break;
case ArrayLocation:
id = tree->node.ArrayLocation.identifier;
break;
default:
return;
}
if(id->node.Identifier.symbol->globalvar){
eprintf(tree->linenum, "Cannot modify the global variable '%s'\n", id->node.Identifier.symbol->name);
errors++;
}
}
/* Check that there is at most one messages block in the program */
static void checkHasMaxOneMessageBlock(AstNode *tree){
int msgblockCount = 0;
AstNode *toplevels = tree->node.Prog.toplevels;
AstNode *node;
for(node = toplevels; node != NULL; node = node->next){
if(node->tag == DefineMessage)
msgblockCount++;
}
if(msgblockCount > 1){
eprintf(1, "At most one messages block is allowed per program. This program has %d\n", msgblockCount);
errors++;
}
}
/* check that the variable is either initialized before use, or have a default value */
static void checkVarInitialized(AstNode *tree){
AstNode *id;
Symbol *sym;
Type *type;
/* make sure tree is not the left side of an assignment. */
if(tree->parent->tag == Assignment && tree->parent->node.Assignment.location == tree)
return;
switch(tree->tag){
case VariableLocation:
id = tree->node.VariableLocation.identifier;
break;
case StructLocation:
id = tree->node.StructLocation.identifier;
break;
case ArrayLocation:
id = tree->node.ArrayLocation.identifier;
break;
default:
return;
}
sym = id->node.Identifier.symbol;
type = sym->type;
if(!isInitialized(sym->initInfo, type) && !canGetDefaultValue(sym->initInfo, type)){
if(type->tag == BuiltinTypeTag){
errors++;
eprintf(tree->linenum, "Variable '%s' is not initialized when used here\n", sym->name);
}else if(type->tag == ArrayTypeTag){
eprintf(tree->linenum, "Warning: array '%s' might not be fully initialized here\n", sym->name);
}else if(type->tag == StructTypeTag){
StructInitializeInfo *sinfo;
errors++;
eprintf(tree->linenum, "Struct instance '%s' have some uninitialized fields when used here: ", sym->name);
for(sinfo = sym->initInfo->structInitialized; sinfo != NULL; sinfo = sinfo->next){
if(!isInitialized(sinfo->info, sinfo->symbol->type) && !canGetDefaultValue(sinfo->info, sinfo->symbol->type))
printf("%s ", sinfo->symbol->name);
}
printf("\n");
}
}
}
static int countSpawns(AstNode *nodes, int recurse){
AstNode *n;
int count = 0;
for(n = nodes; n != NULL; n = n->chain){
if(n->tag == Spawn)
count++;
if(recurse || n->tag == ExprStmt || n->tag == VarDecl || n->tag == Assignment)
count += countSpawns(n->children, recurse);
}
return count;
}
static void checkAllSpawnsInSetup(AstNode *prog, AstNode *stmts){
int spawncountall = countSpawns(prog->node.Prog.toplevels, 1);
int spawncountstmts = countSpawns(stmts, 0);
prog->node.Prog.spawnCount = spawncountstmts;
if(spawncountall > spawncountstmts){
errors++;
eprintf(1, "It looks like you have spawns located somewhere else than directly in the setup function, which is not allowed.\n");
}
}
static int countFunctioncall(char *name, AstNode *nodes, int recurse){
AstNode *n;
int count = 0;
for(n = nodes; n != NULL; n = n->chain){
if(n->tag == FunctionCall && strcmp(n->node.FunctionCall.identifier->node.Identifier.symbol->name, name) == 0)
count++;
if(recurse || n->tag == ExprStmt)
count += countFunctioncall(name, n->children, recurse);
}
return count;
}
static void checkAllAdvancedInputInSetup(AstNode *prog, AstNode *stmts){
int countall = countFunctioncall("advancedInputPin", prog->node.Prog.toplevels, 1);
int countstmts = countFunctioncall("advancedInputPin", stmts, 0);
if(countall > countstmts){
errors++;
eprintf(1, "It looks like you have calls to advancedInputPin located somewhere else than directly in the setup function, which is not allowed.\n");
}
}