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 | int objectCast( Type *src, const TypeEnvironment &env, const SymTab::Indexer &indexer ) {
|
---|
53 | if ( dynamic_cast< FunctionType* >( src ) ) {
|
---|
54 | return -1;
|
---|
55 | } else if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( src ) ) {
|
---|
56 | EqvClass eqvClass;
|
---|
57 | if ( NamedTypeDecl *ntDecl = indexer.lookupType( typeInst->get_name() ) ) {
|
---|
58 | if ( TypeDecl *tyDecl = dynamic_cast< TypeDecl* >( ntDecl ) ) {
|
---|
59 | if ( tyDecl->get_kind() == TypeDecl::Ftype ) {
|
---|
60 | return -1;
|
---|
61 | } // if
|
---|
62 | } //if
|
---|
63 | } else if ( env.lookup( typeInst->get_name(), eqvClass ) ) {
|
---|
64 | if ( eqvClass.data.kind == TypeDecl::Ftype ) {
|
---|
65 | return -1;
|
---|
66 | } // if
|
---|
67 | } // if
|
---|
68 | } //if
|
---|
69 | return 1;
|
---|
70 | }
|
---|
71 | int functionCast( Type *src, const TypeEnvironment &env, const SymTab::Indexer &indexer ) {
|
---|
72 | return -1 * objectCast( src, env, indexer ); // reverse the sense of objectCast
|
---|
73 | }
|
---|
74 |
|
---|
75 | int ptrsCastable( Type *src, Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer ) {
|
---|
76 | if ( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) {
|
---|
77 | EqvClass eqvClass;
|
---|
78 | if ( env.lookup( destAsTypeInst->get_name(), eqvClass ) ) {
|
---|
79 | return ptrsAssignable( src, eqvClass.type, env );
|
---|
80 | } // if
|
---|
81 | } // if
|
---|
82 | if ( dynamic_cast< VoidType* >( dest ) ) {
|
---|
83 | return objectCast( src, env, indexer );
|
---|
84 | } else {
|
---|
85 | PtrsCastable ptrs( dest, env, indexer );
|
---|
86 | src->accept( ptrs );
|
---|
87 | return ptrs.get_result();
|
---|
88 | } // if
|
---|
89 | }
|
---|
90 |
|
---|
91 | PtrsCastable::PtrsCastable( Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer )
|
---|
92 | : dest( dest ), result( 0 ), env( env ), indexer( indexer ) {
|
---|
93 | }
|
---|
94 |
|
---|
95 | void PtrsCastable::visit( __attribute__((unused)) VoidType *voidType) {
|
---|
96 | result = objectCast( dest, env, indexer );
|
---|
97 | }
|
---|
98 |
|
---|
99 | void PtrsCastable::visit( __attribute__((unused)) BasicType *basicType) {
|
---|
100 | result = objectCast( dest, env, indexer );
|
---|
101 | }
|
---|
102 |
|
---|
103 | void PtrsCastable::visit( __attribute__((unused)) PointerType *pointerType) {
|
---|
104 | result = objectCast( dest, env, indexer );
|
---|
105 | }
|
---|
106 |
|
---|
107 | void PtrsCastable::visit( __attribute__((unused)) ArrayType *arrayType) {
|
---|
108 | result = objectCast( dest, env, indexer );
|
---|
109 | }
|
---|
110 |
|
---|
111 | void PtrsCastable::visit( __attribute__((unused)) FunctionType *functionType) {
|
---|
112 | // result = -1;
|
---|
113 | result = functionCast( dest, env, indexer );
|
---|
114 | }
|
---|
115 |
|
---|
116 | void PtrsCastable::visit( __attribute__((unused)) StructInstType *inst) {
|
---|
117 | result = objectCast( dest, env, indexer );
|
---|
118 | }
|
---|
119 |
|
---|
120 | void PtrsCastable::visit( __attribute__((unused)) UnionInstType *inst) {
|
---|
121 | result = objectCast( dest, env, indexer );
|
---|
122 | }
|
---|
123 |
|
---|
124 | void PtrsCastable::visit( __attribute__((unused)) EnumInstType *inst) {
|
---|
125 | if ( dynamic_cast< EnumInstType* >( dest ) ) {
|
---|
126 | result = 1;
|
---|
127 | } else if ( BasicType *bt = dynamic_cast< BasicType* >( dest ) ) {
|
---|
128 | if ( bt->get_kind() == BasicType::SignedInt ) {
|
---|
129 | result = 0;
|
---|
130 | } else {
|
---|
131 | result = 1;
|
---|
132 | }
|
---|
133 | } else {
|
---|
134 | result = objectCast( dest, env, indexer );
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | void PtrsCastable::visit( __attribute__((unused)) TraitInstType *inst ) {}
|
---|
139 |
|
---|
140 | void PtrsCastable::visit(TypeInstType *inst) {
|
---|
141 | //result = objectCast( inst, env, indexer ) > 0 && objectCast( dest, env, indexer ) > 0 ? 1 : -1;
|
---|
142 | result = objectCast( inst, env, indexer ) == objectCast( dest, env, indexer ) ? 1 : -1;
|
---|
143 | }
|
---|
144 |
|
---|
145 | void PtrsCastable::visit( __attribute__((unused)) TupleType *tupleType) {
|
---|
146 | result = objectCast( dest, env, indexer );
|
---|
147 | }
|
---|
148 |
|
---|
149 | void PtrsCastable::visit( __attribute__((unused)) VarArgsType *varArgsType) {
|
---|
150 | result = objectCast( dest, env, indexer );
|
---|
151 | }
|
---|
152 |
|
---|
153 | void PtrsCastable::visit( __attribute__((unused)) ZeroType *zeroType) {
|
---|
154 | result = objectCast( dest, env, indexer );
|
---|
155 | }
|
---|
156 |
|
---|
157 | void PtrsCastable::visit( __attribute__((unused)) OneType *oneType) {
|
---|
158 | result = objectCast( dest, env, indexer );
|
---|
159 | }
|
---|
160 | } // namespace ResolvExpr
|
---|
161 |
|
---|
162 | // Local Variables: //
|
---|
163 | // tab-width: 4 //
|
---|
164 | // mode: c++ //
|
---|
165 | // compile-command: "make install" //
|
---|
166 | // End: //
|
---|