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 | // PtrsCastable.cc -- |
---|
8 | // |
---|
9 | // Author : Richard C. Bilson |
---|
10 | // Created On : Sun May 17 11:48:00 2015 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Wed Mar 2 17:36:18 2016 |
---|
13 | // Update Count : 8 |
---|
14 | // |
---|
15 | |
---|
16 | #include "ResolvExpr/TypeEnvironment.h" // for EqvClass, TypeEnvironment |
---|
17 | #include "SymTab/Indexer.h" // for Indexer |
---|
18 | #include "SynTree/Declaration.h" // for TypeDecl, TypeDecl::Kind::Ftype |
---|
19 | #include "SynTree/Type.h" // for TypeInstType, Type, BasicType |
---|
20 | #include "SynTree/Visitor.h" // for Visitor |
---|
21 | #include "typeops.h" // for ptrsAssignable |
---|
22 | |
---|
23 | |
---|
24 | namespace ResolvExpr { |
---|
25 | class PtrsCastable : public Visitor { |
---|
26 | public: |
---|
27 | PtrsCastable( Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer ); |
---|
28 | |
---|
29 | int get_result() const { return result; } |
---|
30 | |
---|
31 | virtual void visit(VoidType *voidType); |
---|
32 | virtual void visit(BasicType *basicType); |
---|
33 | virtual void visit(PointerType *pointerType); |
---|
34 | virtual void visit(ArrayType *arrayType); |
---|
35 | virtual void visit(FunctionType *functionType); |
---|
36 | virtual void visit(StructInstType *inst); |
---|
37 | virtual void visit(UnionInstType *inst); |
---|
38 | virtual void visit(EnumInstType *inst); |
---|
39 | virtual void visit(TraitInstType *inst); |
---|
40 | virtual void visit(TypeInstType *inst); |
---|
41 | virtual void visit(TupleType *tupleType); |
---|
42 | virtual void visit(VarArgsType *varArgsType); |
---|
43 | virtual void visit(ZeroType *zeroType); |
---|
44 | virtual void visit(OneType *oneType); |
---|
45 | private: |
---|
46 | Type *dest; |
---|
47 | int result; |
---|
48 | const TypeEnvironment &env; |
---|
49 | const SymTab::Indexer &indexer; |
---|
50 | }; |
---|
51 | |
---|
52 | namespace { |
---|
53 | int objectCast( Type *src, const TypeEnvironment &env, const SymTab::Indexer &indexer ) { |
---|
54 | if ( dynamic_cast< FunctionType* >( src ) ) { |
---|
55 | return -1; |
---|
56 | } else if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( src ) ) { |
---|
57 | EqvClass eqvClass; |
---|
58 | if ( NamedTypeDecl *ntDecl = indexer.lookupType( typeInst->get_name() ) ) { |
---|
59 | if ( TypeDecl *tyDecl = dynamic_cast< TypeDecl* >( ntDecl ) ) { |
---|
60 | if ( tyDecl->get_kind() == TypeDecl::Ftype ) { |
---|
61 | return -1; |
---|
62 | } // if |
---|
63 | } //if |
---|
64 | } else if ( env.lookup( typeInst->get_name(), eqvClass ) ) { |
---|
65 | if ( eqvClass.data.kind == TypeDecl::Ftype ) { |
---|
66 | return -1; |
---|
67 | } // if |
---|
68 | } // if |
---|
69 | } //if |
---|
70 | return 1; |
---|
71 | } |
---|
72 | int functionCast( Type *src, const TypeEnvironment &env, const SymTab::Indexer &indexer ) { |
---|
73 | return -1 * objectCast( src, env, indexer ); // reverse the sense of objectCast |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | int ptrsCastable( Type *src, Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer ) { |
---|
78 | if ( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) { |
---|
79 | EqvClass eqvClass; |
---|
80 | if ( env.lookup( destAsTypeInst->get_name(), eqvClass ) ) { |
---|
81 | return ptrsAssignable( src, eqvClass.type, env ); |
---|
82 | } // if |
---|
83 | } // if |
---|
84 | if ( dynamic_cast< VoidType* >( dest ) ) { |
---|
85 | return objectCast( src, env, indexer ); |
---|
86 | } else { |
---|
87 | PtrsCastable ptrs( dest, env, indexer ); |
---|
88 | src->accept( ptrs ); |
---|
89 | return ptrs.get_result(); |
---|
90 | } // if |
---|
91 | } |
---|
92 | |
---|
93 | PtrsCastable::PtrsCastable( Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer ) |
---|
94 | : dest( dest ), result( 0 ), env( env ), indexer( indexer ) { |
---|
95 | } |
---|
96 | |
---|
97 | void PtrsCastable::visit( VoidType * ) { |
---|
98 | result = objectCast( dest, env, indexer ); |
---|
99 | } |
---|
100 | |
---|
101 | void PtrsCastable::visit( BasicType * ) { |
---|
102 | result = objectCast( dest, env, indexer ); |
---|
103 | } |
---|
104 | |
---|
105 | void PtrsCastable::visit( PointerType * ) { |
---|
106 | result = objectCast( dest, env, indexer ); |
---|
107 | } |
---|
108 | |
---|
109 | void PtrsCastable::visit( ArrayType * ) { |
---|
110 | result = objectCast( dest, env, indexer ); |
---|
111 | } |
---|
112 | |
---|
113 | void PtrsCastable::visit( FunctionType * ) { |
---|
114 | // result = -1; |
---|
115 | result = functionCast( dest, env, indexer ); |
---|
116 | } |
---|
117 | |
---|
118 | void PtrsCastable::visit( StructInstType * ) { |
---|
119 | result = objectCast( dest, env, indexer ); |
---|
120 | } |
---|
121 | |
---|
122 | void PtrsCastable::visit( UnionInstType * ) { |
---|
123 | result = objectCast( dest, env, indexer ); |
---|
124 | } |
---|
125 | |
---|
126 | void PtrsCastable::visit( EnumInstType * ) { |
---|
127 | if ( dynamic_cast< EnumInstType* >( dest ) ) { |
---|
128 | result = 1; |
---|
129 | } else if ( BasicType *bt = dynamic_cast< BasicType* >( dest ) ) { |
---|
130 | if ( bt->get_kind() == BasicType::SignedInt ) { |
---|
131 | result = 0; |
---|
132 | } else { |
---|
133 | result = 1; |
---|
134 | } |
---|
135 | } else { |
---|
136 | result = objectCast( dest, env, indexer ); |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | void PtrsCastable::visit( TraitInstType * ) {} |
---|
141 | |
---|
142 | void PtrsCastable::visit(TypeInstType *inst) { |
---|
143 | //result = objectCast( inst, env, indexer ) > 0 && objectCast( dest, env, indexer ) > 0 ? 1 : -1; |
---|
144 | result = objectCast( inst, env, indexer ) == objectCast( dest, env, indexer ) ? 1 : -1; |
---|
145 | } |
---|
146 | |
---|
147 | void PtrsCastable::visit( TupleType * ) { |
---|
148 | result = objectCast( dest, env, indexer ); |
---|
149 | } |
---|
150 | |
---|
151 | void PtrsCastable::visit( VarArgsType * ) { |
---|
152 | result = objectCast( dest, env, indexer ); |
---|
153 | } |
---|
154 | |
---|
155 | void PtrsCastable::visit( ZeroType * ) { |
---|
156 | result = objectCast( dest, env, indexer ); |
---|
157 | } |
---|
158 | |
---|
159 | void PtrsCastable::visit( OneType * ) { |
---|
160 | result = objectCast( dest, env, indexer ); |
---|
161 | } |
---|
162 | } // namespace ResolvExpr |
---|
163 | |
---|
164 | // Local Variables: // |
---|
165 | // tab-width: 4 // |
---|
166 | // mode: c++ // |
---|
167 | // compile-command: "make install" // |
---|
168 | // End: // |
---|