1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // PtrsAssignable.cc -- |
---|
8 | // |
---|
9 | // Author : Richard C. Bilson |
---|
10 | // Created On : Sun May 17 11:44:11 2015 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Wed Mar 2 17:36:05 2016 |
---|
13 | // Update Count : 8 |
---|
14 | // |
---|
15 | |
---|
16 | #include "typeops.h" |
---|
17 | #include "SynTree/Type.h" |
---|
18 | #include "SynTree/Declaration.h" |
---|
19 | #include "SynTree/Visitor.h" |
---|
20 | |
---|
21 | |
---|
22 | namespace ResolvExpr { |
---|
23 | class PtrsAssignable : public Visitor { |
---|
24 | public: |
---|
25 | PtrsAssignable( Type *dest, const TypeEnvironment &env ); |
---|
26 | |
---|
27 | int get_result() const { return result; } |
---|
28 | |
---|
29 | virtual void visit( VoidType *voidType ); |
---|
30 | virtual void visit( BasicType *basicType ); |
---|
31 | virtual void visit( PointerType *pointerType ); |
---|
32 | virtual void visit( ArrayType *arrayType ); |
---|
33 | virtual void visit( FunctionType *functionType ); |
---|
34 | virtual void visit( StructInstType *inst ); |
---|
35 | virtual void visit( UnionInstType *inst ); |
---|
36 | virtual void visit( EnumInstType *inst ); |
---|
37 | virtual void visit( TraitInstType *inst ); |
---|
38 | virtual void visit( TypeInstType *inst ); |
---|
39 | virtual void visit( TupleType *tupleType ); |
---|
40 | virtual void visit( VarArgsType *varArgsType ); |
---|
41 | virtual void visit( ZeroType *zeroType ); |
---|
42 | virtual void visit( OneType *oneType ); |
---|
43 | private: |
---|
44 | Type *dest; |
---|
45 | int result; |
---|
46 | const TypeEnvironment &env; |
---|
47 | }; |
---|
48 | |
---|
49 | int ptrsAssignable( Type *src, Type *dest, const TypeEnvironment &env ) { |
---|
50 | if ( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) { |
---|
51 | EqvClass eqvClass; |
---|
52 | if ( env.lookup( destAsTypeInst->get_name(), eqvClass ) ) { |
---|
53 | return ptrsAssignable( src, eqvClass.type, env ); |
---|
54 | } // if |
---|
55 | } // if |
---|
56 | if ( dynamic_cast< VoidType* >( dest ) ) { |
---|
57 | return 1; |
---|
58 | } else { |
---|
59 | PtrsAssignable ptrs( dest, env ); |
---|
60 | src->accept( ptrs ); |
---|
61 | return ptrs.get_result(); |
---|
62 | } // if |
---|
63 | } |
---|
64 | |
---|
65 | PtrsAssignable::PtrsAssignable( Type *dest, const TypeEnvironment &env ) : dest( dest ), result( 0 ), env( env ) { |
---|
66 | } |
---|
67 | |
---|
68 | void PtrsAssignable::visit( VoidType *voidType ) { |
---|
69 | if ( dynamic_cast< FunctionType* >( dest ) ) { |
---|
70 | result = 0; |
---|
71 | } else { |
---|
72 | result = -1; |
---|
73 | } // if |
---|
74 | } |
---|
75 | |
---|
76 | void PtrsAssignable::visit( BasicType *basicType ) { |
---|
77 | } |
---|
78 | |
---|
79 | void PtrsAssignable::visit( PointerType *pointerType ) { |
---|
80 | } |
---|
81 | |
---|
82 | void PtrsAssignable::visit( ArrayType *arrayType ) { |
---|
83 | } |
---|
84 | |
---|
85 | void PtrsAssignable::visit( FunctionType *functionType ) { |
---|
86 | result = -1; |
---|
87 | } |
---|
88 | |
---|
89 | void PtrsAssignable::visit( StructInstType *inst ) { |
---|
90 | // I don't think we should be doing anything here, but I'm willing to admit that I might be wrong |
---|
91 | } |
---|
92 | |
---|
93 | void PtrsAssignable::visit( UnionInstType *inst ) { |
---|
94 | // I don't think we should be doing anything here, but I'm willing to admit that I might be wrong |
---|
95 | } |
---|
96 | |
---|
97 | void PtrsAssignable::visit( EnumInstType *inst ) { |
---|
98 | if ( dynamic_cast< EnumInstType* >( inst ) ) { |
---|
99 | result = 1; |
---|
100 | } else if ( BasicType *bt = dynamic_cast< BasicType* >( inst ) ) { |
---|
101 | result = bt->get_kind() == BasicType::SignedInt; |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | void PtrsAssignable::visit( TraitInstType *inst ) { |
---|
106 | // I definitely don't think we should be doing anything here |
---|
107 | } |
---|
108 | |
---|
109 | void PtrsAssignable::visit( TypeInstType *inst ) { |
---|
110 | EqvClass eqvClass; |
---|
111 | if ( env.lookup( inst->get_name(), eqvClass ) && eqvClass.type ) { |
---|
112 | result = ptrsAssignable( eqvClass.type, dest, env ); |
---|
113 | } else { |
---|
114 | result = 0; |
---|
115 | } // if |
---|
116 | } |
---|
117 | |
---|
118 | void PtrsAssignable::visit( TupleType *tupleType ) { |
---|
119 | /// // This code doesn't belong here, but it might be useful somewhere else |
---|
120 | /// if ( TupleType *destAsTuple = dynamic_cast< TupleType* >( dest ) ) { |
---|
121 | /// int ret = 0; |
---|
122 | /// std::list< Type* >::const_iterator srcIt = tupleType->get_types().begin(); |
---|
123 | /// std::list< Type* >::const_iterator destIt = destAsTuple->get_types().begin(); |
---|
124 | /// while ( srcIt != tupleType->get_types().end() && destIt != destAsTuple->get_types().end() ) { |
---|
125 | /// int assignResult = ptrsAssignable( *srcIt++, *destIt++ ); |
---|
126 | /// if ( assignResult == 0 ) { |
---|
127 | /// result = assignResult; |
---|
128 | /// return; |
---|
129 | /// } else if ( assignResult < 0 ) { |
---|
130 | /// ret = -1; |
---|
131 | /// } else if ( ret > 0 ) { |
---|
132 | /// ret += assignResult; |
---|
133 | /// } |
---|
134 | /// } |
---|
135 | /// if ( srcIt == tupleType->get_types().end() && destIt == destAsTuple->get_types().end() ) { |
---|
136 | /// result = ret; |
---|
137 | /// } else { |
---|
138 | /// result = 0; |
---|
139 | /// } |
---|
140 | /// } |
---|
141 | } |
---|
142 | |
---|
143 | void PtrsAssignable::visit( VarArgsType *varArgsType ) { |
---|
144 | } |
---|
145 | |
---|
146 | void PtrsAssignable::visit( ZeroType *zeroType ) { |
---|
147 | } |
---|
148 | |
---|
149 | void PtrsAssignable::visit( OneType *oneType ) { |
---|
150 | } |
---|
151 | |
---|
152 | } // namespace ResolvExpr |
---|
153 | |
---|
154 | // Local Variables: // |
---|
155 | // tab-width: 4 // |
---|
156 | // mode: c++ // |
---|
157 | // compile-command: "make install" // |
---|
158 | // End: // |
---|