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
|
//-----------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation. All Rights Reserved.
//
//-----------------------------------------------------------------------------
using System;
using System.Diagnostics.Contracts;
using System.Collections.Generic;
// Visitor to search for types proxies that could not completely be
// determined by type inference. If this happens, a warning is
// generated and the proxies are instantiated in a more or less arbitrary
// fashion.
namespace Microsoft.Boogie {
public class TypeAmbiguitySeeker : ReadOnlyVisitor {
private readonly InTypeSeeker/*!*/ inTypeSeeker = new InTypeSeeker();
private readonly TypecheckingContext/*!*/ TC;
[ContractInvariantMethod]
void ObjectInvariant() {
Contract.Invariant(inTypeSeeker != null);
Contract.Invariant(TC != null);
}
public TypeAmbiguitySeeker(TypecheckingContext tc) {
Contract.Requires(tc != null);
TC = tc;
}
private void CheckTypeParams(Absy node, TypeParamInstantiation insts) {
Contract.Requires(insts != null);
Contract.Requires(node != null);
foreach (TypeVariable/*!*/ var in insts.FormalTypeParams) {
Contract.Assert(var != null);
Type/*!*/ inst = insts[var];
Contract.Assert(inst != null);
inTypeSeeker.FoundAmbiguity = false;
inTypeSeeker.Visit(inst);
if (inTypeSeeker.FoundAmbiguity)
TC.Warning(node,
"type parameter {0} is ambiguous, instantiating to {1}",
var, inst);
}
}
public override Expr VisitNAryExpr(NAryExpr node) {
//Contract.Requires(node != null);
Contract.Ensures(Contract.Result<Expr>() != null);
CheckTypeParams(node, cce.NonNull(node.TypeParameters));
return base.VisitNAryExpr(node);
}
public override AssignLhs VisitMapAssignLhs(MapAssignLhs node) {
//Contract.Requires(node != null);
Contract.Ensures(Contract.Result<AssignLhs>() != null);
CheckTypeParams(node, cce.NonNull(node.TypeParameters));
return base.VisitMapAssignLhs(node);
}
}
internal class InTypeSeeker : ReadOnlyVisitor {
internal bool FoundAmbiguity = false;
// called when an uninstantiated proxy was found
private Type Instantiate(Type node, Type inst) {
Contract.Requires(inst != null);
Contract.Requires(node != null);
Contract.Ensures(Contract.Result<Type>() == node);
FoundAmbiguity = true;
bool success = node.Unify(inst);
Contract.Assert(success);
return node;
}
public override Type VisitTypeProxy(TypeProxy node) {
//Contract.Requires(node != null);
Contract.Ensures(Contract.Result<Type>() != null);
if (node.ProxyFor != null)
return base.VisitTypeProxy(node);
return Instantiate(node, Type.Int);
}
public override Type VisitMapTypeProxy(MapTypeProxy node) {
//Contract.Requires(node != null);
Contract.Ensures(Contract.Result<Type>() != null);
if (node.ProxyFor != null)
return base.VisitMapTypeProxy(node);
List<TypeVariable>/*!*/ typeParams = new List<TypeVariable>();
List<Type>/*!*/ arguments = new List<Type>();
for (int i = 0; i < node.Arity; ++i) {
TypeVariable/*!*/ param = new TypeVariable(Token.NoToken, "arg" + i);
Contract.Assert(param != null);
typeParams.Add(param);
arguments.Add(param);
}
TypeVariable/*!*/ result = new TypeVariable(Token.NoToken, "res");
Contract.Assert(result != null);
typeParams.Add(result);
Type/*!*/ instantiation = new MapType(Token.NoToken, typeParams, arguments, result);
Contract.Assert(instantiation != null);
return Instantiate(node, instantiation);
}
public override Type VisitBvTypeProxy(BvTypeProxy node) {
//Contract.Requires(node != null);
Contract.Ensures(Contract.Result<Type>() != null);
if (node.ProxyFor != null)
return base.VisitBvTypeProxy(node);
return Instantiate(node, new BvType(node.MinBits));
}
}
}
|