-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecker.ts
326 lines (311 loc) · 9.82 KB
/
checker.ts
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
import {
TranslationUnit,
Node,
FunctionDefinition,
DeclaratorNode,
AssignmentExpressionNode,
Typename,
ExpressionNode,
IdentifierNode,
} from "./core/parser.definitions";
import { TokenLocation } from "./core/error";
import { assertNever } from "./core/assertNever";
import { CheckerError } from "./core/error";
export interface CheckerWarning extends TokenLocation {
msg: string;
}
export function checkTranslationUnit(unit: TranslationUnit): CheckerWarning[] {
const warnings: CheckerWarning[] = [];
const locator = unit.locationMap();
const declaratorMap = unit.declaratorMap();
function getDeclaration(identifier: IdentifierNode) {
const declaration = declaratorMap.get(identifier.declaratorNodeId);
if (!declaration) {
error(
identifier,
`Internal error: unable to find declaration ${identifier.declaratorNodeId}`
);
}
return declaration;
}
function warn(node: Node, msg: string) {
const location = locator.get(node);
if (!location) {
console.warn(`Unable to find location for node type=${node.type}`);
warnings.push({
msg,
length: 0,
pos: 0,
line: 0,
});
} else {
warnings.push({
msg,
...location,
});
}
}
function error(node: Node, msg: string): never {
// TODO: Add error into errors list
// Now we just throw
const location = locator.get(node);
if (!location) {
console.warn(`Unable to find location for node type=${node.type}`);
throw new CheckerError(`${msg}`, { pos: 0, line: 0, length: 0 });
} else {
throw new CheckerError(`${msg}`, location);
}
}
function haveLvalue(expression: ExpressionNode) {
if (expression.type === "identifier") {
const type = getDeclaration(expression);
if (type.typename.const) {
return false;
}
if (type.typename.type === "arithmetic") {
return true;
} else if (type.typename.type === "pointer") {
return true;
} else if (
type.typename.type === "enum" ||
type.typename.type === "struct"
) {
error(expression, "TODO: Not supported yet");
} else if (type.typename.type === "void") {
return false;
}
} else if (expression.type === "subscript operator") {
const target = expression.target;
const index = expression.index;
const targetType = getExpressionType(target);
if (targetType.type !== "array") {
error(target, "Not an array");
}
if (targetType.elementsTypename.const) {
error(target, "Elements are const");
}
const indexType = getExpressionType(index);
if (indexType.type !== "arithmetic") {
error(index, "Index should be arithmetic");
}
if (
indexType.arithmeticType !== "char" &&
indexType.arithmeticType !== "int"
) {
error(index, "Wrong type for index. Only char and ints. No float ");
}
} else if (expression.type === "const") {
return false;
} else {
// TODO
return false;
}
}
// TODO: Caching
function getExpressionType(expression: ExpressionNode): Typename {
if (expression.type === "const") {
if (expression.subtype === "int") {
// TODO: All sizes check
return {
type: "arithmetic",
arithmeticType: "int",
const: true,
signedUnsigned: "signed",
};
} else if (expression.subtype === "float") {
return {
type: "arithmetic",
arithmeticType: "double",
const: true,
signedUnsigned: "signed",
};
} else if (expression.subtype === "char") {
return {
type: "arithmetic",
arithmeticType: "char",
const: true,
signedUnsigned: "unsigned",
};
} else {
assertNever(expression.subtype);
}
} else if (expression.type === "binary operator") {
const left = getExpressionType(expression.left);
const right = getExpressionType(expression.right);
if (left.type === "arithmetic" && right.type === "arithmetic") {
// TODO: Proper way
if (left.arithmeticType === "int" && right.arithmeticType === "int") {
return left;
} else if (
left.arithmeticType === "float" ||
right.arithmeticType === "float"
) {
error(expression, "Float type is not supported yet");
} else if (
left.arithmeticType === "char" &&
right.arithmeticType === "int"
) {
return right;
} else if (
left.arithmeticType === "int" &&
right.arithmeticType === "char"
) {
return left;
} else if (
left.arithmeticType === "char" &&
right.arithmeticType === "char"
) {
// Lol, what if multiplication between two chars?
return left;
} else {
error(expression, "Not supported yet");
}
} else if (left.type === "pointer" && right.type === "arithmetic") {
if (right.arithmeticType !== "int" && right.arithmeticType !== "char") {
error(expression, "not supported yet");
}
if (expression.operator !== "+" && expression.operator !== "-") {
error(expression, "Wrong operator for pointer and arithmetic type");
}
return left;
} else if (left.type === "arithmetic" && right.type === "pointer") {
error(expression, "Not supported yet. Change left and right");
} else {
error(expression, "Not supported yet");
}
} else if (expression.type === "assignment") {
if (!haveLvalue(expression.lvalue)) {
error(expression.lvalue, "Not an lvalue");
}
throw new Error("todo");
}
// TODO
throw new Error("TODO");
}
function checkExpression(expression: ExpressionNode) {
getExpressionType(expression);
return;
}
function checkIsAssignmentPossible(lvalue: Typename, rvalue: Typename) {
if (lvalue.type === "arithmetic" && rvalue.type === "arithmetic") {
return;
}
if (lvalue.type === "pointer" && rvalue.type === "arithmetic") {
if (rvalue.arithmeticType === "char" || rvalue.arithmeticType === "int") {
warn(lvalue, `Assignnment of int to pointer`);
} else {
error(lvalue, "TODO: Other types or error on float");
}
return true;
}
throw new Error("TODO");
}
function isExpessionKnownAtCompilationTime(
expression: ExpressionNode
): boolean {
checkExpression(expression);
if (expression.type === "const") {
return true;
} else if (expression.type === "binary operator") {
return (
isExpessionKnownAtCompilationTime(expression.left) &&
isExpessionKnownAtCompilationTime(expression.right)
);
} else {
return false;
// @TODO
}
}
function checkDeclarator(declarator: DeclaratorNode, isFileScope: boolean) {
if (declarator.typename.type === "function") {
if (declarator.initializer !== null) {
error(
declarator,
"Internal error: should be no initializer for function"
);
}
} else if (declarator.typename.type === "function-knr") {
error(
declarator,
"Internal error: K&R notations should be already transformed"
);
} else if (declarator.typename.type === "void") {
error(declarator, "void type for variable, this is weird");
} else if (declarator.typename.type === "arithmetic") {
if (
declarator.initializer &&
declarator.initializer.type !== "assigmnent-expression"
) {
error(
declarator.initializer,
"Only assigmnent-expression is allowed for arthmetic type"
);
}
} else if (
declarator.typename.type === "enum" ||
declarator.typename.type === "struct"
) {
error(declarator, "TODO: Not implemented");
} else if (declarator.typename.type === "pointer") {
if (declarator.initializer) {
if (declarator.initializer.type !== "assigmnent-expression") {
error(
declarator.initializer,
"Only assigmnent-expression is allowed for pointer type"
);
} else {
const initializerType = getExpressionType(
declarator.initializer.expression
);
checkIsAssignmentPossible(declarator.typename, initializerType);
}
}
} else if (declarator.typename.type === "array") {
if (declarator.initializer) {
if (declarator.initializer.type === "assigmnent-expression") {
error(declarator, "Wrong initializer for array");
}
error(declarator, "Initializers for array are not supported yet");
}
if (declarator.typename.size === "*") {
error(declarator, "star modifier used outside of function prototype");
}
if (declarator.typename.size === null) {
error(declarator, "Incomplete type is not allowed");
}
const isFixedSize = isExpessionKnownAtCompilationTime(
declarator.typename.size
);
if (isFileScope) {
if (!isFixedSize) {
error(
declarator,
"variable length array declaration not allowed at file scope"
);
}
} else {
if (!isFixedSize) {
error(
declarator,
"TODO: variable length array declaration is not implemented yet"
);
}
}
} else {
assertNever(declarator.typename);
}
}
function checkFunction(func: FunctionDefinition) {
// todo
}
for (const statement of unit.body) {
if (statement.type === "function-declaration") {
checkFunction(statement);
} else if (statement.type === "declarator") {
checkDeclarator(statement, true);
} else {
assertNever(statement);
}
}
return warnings;
}