1 | #include "typeops.h" |
---|
2 | #include "SynTree/Type.h" |
---|
3 | #include "SynTree/Declaration.h" |
---|
4 | #include "SynTree/Visitor.h" |
---|
5 | |
---|
6 | |
---|
7 | namespace ResolvExpr { |
---|
8 | class PtrsAssignable : public Visitor { |
---|
9 | public: |
---|
10 | PtrsAssignable( Type *dest, const TypeEnvironment &env ); |
---|
11 | |
---|
12 | int get_result() const { return result; } |
---|
13 | |
---|
14 | virtual void visit(VoidType *voidType); |
---|
15 | virtual void visit(BasicType *basicType); |
---|
16 | virtual void visit(PointerType *pointerType); |
---|
17 | virtual void visit(ArrayType *arrayType); |
---|
18 | virtual void visit(FunctionType *functionType); |
---|
19 | virtual void visit(StructInstType *inst); |
---|
20 | virtual void visit(UnionInstType *inst); |
---|
21 | virtual void visit(EnumInstType *inst); |
---|
22 | virtual void visit(ContextInstType *inst); |
---|
23 | virtual void visit(TypeInstType *inst); |
---|
24 | virtual void visit(TupleType *tupleType); |
---|
25 | private: |
---|
26 | Type *dest; |
---|
27 | int result; |
---|
28 | const TypeEnvironment &env; |
---|
29 | }; |
---|
30 | |
---|
31 | int ptrsAssignable( Type *src, Type *dest, const TypeEnvironment &env ) { |
---|
32 | if ( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) { |
---|
33 | EqvClass eqvClass; |
---|
34 | if ( env.lookup( destAsTypeInst->get_name(), eqvClass ) ) { |
---|
35 | return ptrsAssignable( src, eqvClass.type, env ); |
---|
36 | } |
---|
37 | } |
---|
38 | if ( dynamic_cast< VoidType* >( dest ) ) { |
---|
39 | return 1; |
---|
40 | } else { |
---|
41 | PtrsAssignable ptrs( dest, env ); |
---|
42 | src->accept( ptrs ); |
---|
43 | return ptrs.get_result(); |
---|
44 | } |
---|
45 | } |
---|
46 | |
---|
47 | PtrsAssignable::PtrsAssignable( Type *dest, const TypeEnvironment &env ) : dest( dest ), result( 0 ), env( env ) { |
---|
48 | } |
---|
49 | |
---|
50 | void PtrsAssignable::visit(VoidType *voidType) { |
---|
51 | if ( dynamic_cast< FunctionType* >( dest ) ) { |
---|
52 | result = 0; |
---|
53 | } else { |
---|
54 | result = -1; |
---|
55 | } |
---|
56 | } |
---|
57 | |
---|
58 | void PtrsAssignable::visit( BasicType *basicType ) { |
---|
59 | } |
---|
60 | |
---|
61 | void PtrsAssignable::visit( PointerType *pointerType ) { |
---|
62 | } |
---|
63 | |
---|
64 | void PtrsAssignable::visit( ArrayType *arrayType ) { |
---|
65 | } |
---|
66 | |
---|
67 | void PtrsAssignable::visit( FunctionType *functionType ) { |
---|
68 | result = -1; |
---|
69 | } |
---|
70 | |
---|
71 | void PtrsAssignable::visit( StructInstType *inst ) { |
---|
72 | // I don't think we should be doing anything here, but I'm willing to admit that I might be wrong |
---|
73 | } |
---|
74 | |
---|
75 | void PtrsAssignable::visit( UnionInstType *inst ) { |
---|
76 | // I don't think we should be doing anything here, but I'm willing to admit that I might be wrong |
---|
77 | } |
---|
78 | |
---|
79 | void PtrsAssignable::visit( EnumInstType *inst ) { |
---|
80 | if ( dynamic_cast< EnumInstType* >( inst ) ) { |
---|
81 | result = 1; |
---|
82 | } else if ( BasicType *bt = dynamic_cast< BasicType* >( inst ) ) { |
---|
83 | result = bt->get_kind() == BasicType::SignedInt; |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | void PtrsAssignable::visit( ContextInstType *inst ) { |
---|
88 | // I definitely don't think we should be doing anything here |
---|
89 | } |
---|
90 | |
---|
91 | void PtrsAssignable::visit( TypeInstType *inst ) { |
---|
92 | EqvClass eqvClass; |
---|
93 | if ( env.lookup( inst->get_name(), eqvClass ) ) { |
---|
94 | result = ptrsAssignable( eqvClass.type, dest, env ); |
---|
95 | } else { |
---|
96 | result = 0; |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | void PtrsAssignable::visit( TupleType *tupleType ) { |
---|
101 | /// // This code doesn't belong here, but it might be useful somewhere else |
---|
102 | /// if ( TupleType *destAsTuple = dynamic_cast< TupleType* >( dest ) ) { |
---|
103 | /// int ret = 0; |
---|
104 | /// std::list< Type* >::const_iterator srcIt = tupleType->get_types().begin(); |
---|
105 | /// std::list< Type* >::const_iterator destIt = destAsTuple->get_types().begin(); |
---|
106 | /// while( srcIt != tupleType->get_types().end() && destIt != destAsTuple->get_types().end() ) { |
---|
107 | /// int assignResult = ptrsAssignable( *srcIt++, *destIt++ ); |
---|
108 | /// if ( assignResult == 0 ) { |
---|
109 | /// result = assignResult; |
---|
110 | /// return; |
---|
111 | /// } else if ( assignResult < 0 ) { |
---|
112 | /// ret = -1; |
---|
113 | /// } else if ( ret > 0 ) { |
---|
114 | /// ret += assignResult; |
---|
115 | /// } |
---|
116 | /// } |
---|
117 | /// if ( srcIt == tupleType->get_types().end() && destIt == destAsTuple->get_types().end() ) { |
---|
118 | /// result = ret; |
---|
119 | /// } else { |
---|
120 | /// result = 0; |
---|
121 | /// } |
---|
122 | /// } |
---|
123 | } |
---|
124 | } // namespace ResolvExpr |
---|