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 : Sun May 17 11:51:17 2015 |
---|
13 | // Update Count : 2 |
---|
14 | // |
---|
15 | |
---|
16 | #include "typeops.h" |
---|
17 | #include "SynTree/Type.h" |
---|
18 | #include "SynTree/Declaration.h" |
---|
19 | #include "SynTree/Visitor.h" |
---|
20 | #include "SymTab/Indexer.h" |
---|
21 | |
---|
22 | |
---|
23 | namespace ResolvExpr { |
---|
24 | class PtrsCastable : public Visitor { |
---|
25 | public: |
---|
26 | PtrsCastable( Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer ); |
---|
27 | |
---|
28 | int get_result() const { return result; } |
---|
29 | |
---|
30 | virtual void visit(VoidType *voidType); |
---|
31 | virtual void visit(BasicType *basicType); |
---|
32 | virtual void visit(PointerType *pointerType); |
---|
33 | virtual void visit(ArrayType *arrayType); |
---|
34 | virtual void visit(FunctionType *functionType); |
---|
35 | virtual void visit(StructInstType *inst); |
---|
36 | virtual void visit(UnionInstType *inst); |
---|
37 | virtual void visit(EnumInstType *inst); |
---|
38 | virtual void visit(ContextInstType *inst); |
---|
39 | virtual void visit(TypeInstType *inst); |
---|
40 | virtual void visit(TupleType *tupleType); |
---|
41 | private: |
---|
42 | Type *dest; |
---|
43 | int result; |
---|
44 | const TypeEnvironment &env; |
---|
45 | const SymTab::Indexer &indexer; |
---|
46 | }; |
---|
47 | |
---|
48 | int objectCast( Type *src, const TypeEnvironment &env, const SymTab::Indexer &indexer ) { |
---|
49 | if ( dynamic_cast< FunctionType* >( src ) ) { |
---|
50 | return -1; |
---|
51 | } else if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( src ) ) { |
---|
52 | EqvClass eqvClass; |
---|
53 | if ( NamedTypeDecl *ntDecl = indexer.lookupType( typeInst->get_name() ) ) { |
---|
54 | if ( TypeDecl *tyDecl = dynamic_cast< TypeDecl* >( ntDecl ) ) { |
---|
55 | if ( tyDecl->get_kind() == TypeDecl::Ftype ) { |
---|
56 | return -1; |
---|
57 | } // if |
---|
58 | } //if |
---|
59 | } else if ( env.lookup( typeInst->get_name(), eqvClass ) ) { |
---|
60 | if ( eqvClass.kind == TypeDecl::Ftype ) { |
---|
61 | return -1; |
---|
62 | } // if |
---|
63 | } // if |
---|
64 | } //if |
---|
65 | return 1; |
---|
66 | } |
---|
67 | |
---|
68 | int ptrsCastable( Type *src, Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer ) { |
---|
69 | if ( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) { |
---|
70 | EqvClass eqvClass; |
---|
71 | if ( env.lookup( destAsTypeInst->get_name(), eqvClass ) ) { |
---|
72 | return ptrsAssignable( src, eqvClass.type, env ); |
---|
73 | } // if |
---|
74 | } // if |
---|
75 | if ( dynamic_cast< VoidType* >( dest ) ) { |
---|
76 | return objectCast( src, env, indexer ); |
---|
77 | } else { |
---|
78 | PtrsCastable ptrs( dest, env, indexer ); |
---|
79 | src->accept( ptrs ); |
---|
80 | return ptrs.get_result(); |
---|
81 | } // if |
---|
82 | } |
---|
83 | |
---|
84 | PtrsCastable::PtrsCastable( Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer ) |
---|
85 | : dest( dest ), result( 0 ), env( env ), indexer( indexer ) { |
---|
86 | } |
---|
87 | |
---|
88 | void PtrsCastable::visit(VoidType *voidType) { |
---|
89 | result = objectCast( dest, env, indexer ); |
---|
90 | } |
---|
91 | |
---|
92 | void PtrsCastable::visit(BasicType *basicType) { |
---|
93 | result = objectCast( dest, env, indexer ); |
---|
94 | } |
---|
95 | |
---|
96 | void PtrsCastable::visit(PointerType *pointerType) { |
---|
97 | result = objectCast( dest, env, indexer ); |
---|
98 | } |
---|
99 | |
---|
100 | void PtrsCastable::visit(ArrayType *arrayType) { |
---|
101 | result = objectCast( dest, env, indexer ); |
---|
102 | } |
---|
103 | |
---|
104 | void PtrsCastable::visit(FunctionType *functionType) { |
---|
105 | result = -1; |
---|
106 | } |
---|
107 | |
---|
108 | void PtrsCastable::visit(StructInstType *inst) { |
---|
109 | result = objectCast( dest, env, indexer ); |
---|
110 | } |
---|
111 | |
---|
112 | void PtrsCastable::visit(UnionInstType *inst) { |
---|
113 | result = objectCast( dest, env, indexer ); |
---|
114 | } |
---|
115 | |
---|
116 | void PtrsCastable::visit(EnumInstType *inst) { |
---|
117 | if ( dynamic_cast< EnumInstType* >( inst ) ) { |
---|
118 | result = 1; |
---|
119 | } else if ( BasicType *bt = dynamic_cast< BasicType* >( inst ) ) { |
---|
120 | if ( bt->get_kind() == BasicType::SignedInt ) { |
---|
121 | result = 0; |
---|
122 | } else { |
---|
123 | result = 1; |
---|
124 | } |
---|
125 | } else { |
---|
126 | result = objectCast( dest, env, indexer ); |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | void PtrsCastable::visit(ContextInstType *inst) { |
---|
131 | // I definitely don't think we should be doing anything here |
---|
132 | } |
---|
133 | |
---|
134 | void PtrsCastable::visit(TypeInstType *inst) { |
---|
135 | result = objectCast( inst, env, indexer ) && objectCast( dest, env, indexer ) ? 1 : -1; |
---|
136 | } |
---|
137 | |
---|
138 | void PtrsCastable::visit(TupleType *tupleType) { |
---|
139 | result = objectCast( dest, env, indexer ); |
---|
140 | } |
---|
141 | } // namespace ResolvExpr |
---|
142 | |
---|
143 | // Local Variables: // |
---|
144 | // tab-width: 4 // |
---|
145 | // mode: c++ // |
---|
146 | // compile-command: "make install" // |
---|
147 | // End: // |
---|