Skip to content

Commit 5c6aa40

Browse files
committed
Validate that we understand types
1 parent 7ce0ace commit 5c6aa40

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

src/mbit/emitter.ts

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,15 @@ namespace ts {
1414
console.log(stringKind(n))
1515
}
1616

17+
function userError(msg: string) {
18+
debugger;
19+
var e = new Error(msg);
20+
(<any>e).bitvmUserError = true;
21+
throw e;
22+
}
23+
1724
function isRefType(t: Type) {
25+
checkType(t);
1826
return !(t.flags & (TypeFlags.Number | TypeFlags.Boolean | TypeFlags.Enum))
1927
}
2028

@@ -34,6 +42,17 @@ namespace ts {
3442
let lf = thumb.lf;
3543
let checker: TypeChecker;
3644

45+
function checkType(t: Type) {
46+
let ok = TypeFlags.String | TypeFlags.Number | TypeFlags.Boolean | TypeFlags.Void | TypeFlags.Enum
47+
if ((t.flags & ok) == 0) {
48+
userError(lf("unsupported type: {0}", checker.typeToString(t)))
49+
}
50+
return t
51+
}
52+
53+
function typeOf(node: Node) {
54+
return checkType(checker.getTypeAtLocation(node))
55+
}
3756

3857
export function emitMBit(program: Program): EmitResult {
3958

@@ -69,13 +88,6 @@ namespace ts {
6988
}, arg0, arg1, arg2));
7089
}
7190

72-
function userError(msg: string) {
73-
debugger;
74-
var e = new Error(msg);
75-
(<any>e).bitvmUserError = true;
76-
throw e;
77-
}
78-
7991
function unhandled(n: Node, addInfo = "") {
8092
if (addInfo)
8193
addInfo = " (" + addInfo + ")"
@@ -209,8 +221,7 @@ namespace ts {
209221
return res
210222
}
211223
function isRefExpr(e: Expression) {
212-
let tp = checker.getTypeAtLocation(e)
213-
return isRefType(tp)
224+
return isRefType(typeOf(e))
214225
}
215226
function getMask(args: Expression[]) {
216227
Debug.assert(args.length <= 8)
@@ -225,7 +236,7 @@ namespace ts {
225236
function emitCallExpression(node: CallExpression) {
226237
let decl = getDecl(node.expression)
227238
let attrs = parseComments(decl)
228-
let hasRet = !(checker.getTypeAtLocation(node).flags & TypeFlags.Void)
239+
let hasRet = !(typeOf(node).flags & TypeFlags.Void)
229240
let args = node.arguments
230241

231242
if (decl && decl.kind == SyntaxKind.FunctionDeclaration) {
@@ -331,7 +342,7 @@ namespace ts {
331342
function emitVoidExpression(node: VoidExpression) { }
332343
function emitAwaitExpression(node: AwaitExpression) { }
333344
function emitPrefixUnaryExpression(node: PrefixUnaryExpression) {
334-
let tp = checker.getTypeAtLocation(node.operand)
345+
let tp = typeOf(node.operand)
335346
if (tp.flags & TypeFlags.Boolean) {
336347
if (node.operator == SyntaxKind.ExclamationToken) {
337348
emit(node.operand)
@@ -367,7 +378,7 @@ namespace ts {
367378
}
368379

369380
function emitPostfixUnaryExpression(node: PostfixUnaryExpression) {
370-
let tp = checker.getTypeAtLocation(node.operand)
381+
let tp = typeOf(node.operand)
371382

372383
if (tp.flags & TypeFlags.Number) {
373384
switch (node.operator) {
@@ -384,8 +395,8 @@ namespace ts {
384395
function isBogusReturn(node: Expression) {
385396
let par = node.parent
386397
if (!(par.kind == SyntaxKind.ExpressionStatement ||
387-
(par.kind == SyntaxKind.ForStatement &&
388-
(<ForStatement>par).incrementor == node || (<ForStatement>par).initializer == node)))
398+
(par.kind == SyntaxKind.ForStatement &&
399+
(<ForStatement>par).incrementor == node || (<ForStatement>par).initializer == node)))
389400
return false
390401

391402
if (node.kind == SyntaxKind.PrefixUnaryExpression || node.kind == SyntaxKind.PostfixUnaryExpression) {
@@ -432,8 +443,8 @@ namespace ts {
432443
return
433444
}
434445

435-
let lt = checker.getTypeAtLocation(node.left)
436-
let rt = checker.getTypeAtLocation(node.right)
446+
let lt = typeOf(node.left)
447+
let rt = typeOf(node.right)
437448

438449
let shim = (n: string) => {
439450
emit(node.left)
@@ -505,7 +516,7 @@ namespace ts {
505516
}
506517
function emitAsString(e: Expression) {
507518
emit(e)
508-
let tp = checker.getTypeAtLocation(e)
519+
let tp = typeOf(e)
509520
if (tp.flags & TypeFlags.Number)
510521
proc.emitCall("number::to_string", 0)
511522
else if (tp.flags & TypeFlags.Boolean)
@@ -575,7 +586,7 @@ namespace ts {
575586
function emitExprAsStmt(node: Expression) {
576587
if (!node) return;
577588
emit(node);
578-
let a = checker.getTypeAtLocation(node)
589+
let a = typeOf(node)
579590
if (!(a.flags & TypeFlags.Void) && !isBogusReturn(node)) {
580591
if (isRefType(a)) {
581592
// will pop
@@ -1030,7 +1041,7 @@ namespace ts {
10301041

10311042
function isRefDecl(def: Declaration) {
10321043
//let tp = checker.getDeclaredTypeOfSymbol(def.symbol)
1033-
let tp = checker.getTypeAtLocation(def)
1044+
let tp = typeOf(def)
10341045
return isRefType(tp)
10351046
}
10361047

0 commit comments

Comments
 (0)