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 "ResolvExpr/TypeEnvironment.h" // for EqvClass, TypeEnvironment |
---|
17 | #include "SynTree/Type.h" // for TypeInstType, Type, BasicType |
---|
18 | #include "SynTree/Visitor.h" // for Visitor |
---|
19 | |
---|
20 | |
---|
21 | namespace ResolvExpr { |
---|
22 | class PtrsAssignable : public Visitor { |
---|
23 | public: |
---|
24 | PtrsAssignable( Type *dest, const TypeEnvironment &env ); |
---|
25 | |
---|
26 | int get_result() const { return result; } |
---|
27 | |
---|
28 | virtual void visit( VoidType *voidType ); |
---|
29 | virtual void visit( BasicType *basicType ); |
---|
30 | virtual void visit( PointerType *pointerType ); |
---|
31 | virtual void visit( ArrayType *arrayType ); |
---|
32 | virtual void visit( FunctionType *functionType ); |
---|
33 | virtual void visit( StructInstType *inst ); |
---|
34 | virtual void visit( UnionInstType *inst ); |
---|
35 | virtual void visit( EnumInstType *inst ); |
---|
36 | virtual void visit( TraitInstType *inst ); |
---|
37 | virtual void visit( TypeInstType *inst ); |
---|
38 | virtual void visit( TupleType *tupleType ); |
---|
39 | virtual void visit( VarArgsType *varArgsType ); |
---|
40 | virtual void visit( ZeroType *zeroType ); |
---|
41 | virtual void visit( OneType *oneType ); |
---|
42 | private: |
---|
43 | Type *dest; |
---|
44 | int result; |
---|
45 | const TypeEnvironment &env; |
---|
46 | }; |
---|
47 | |
---|
48 | int ptrsAssignable( Type *src, Type *dest, const TypeEnvironment &env ) { |
---|
49 | // std::cerr << "assignable: " << src << " | " << dest << std::endl; |
---|
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 | // void * = T * for any T is unsafe |
---|
58 | // xxx - this should be safe, but that currently breaks the build |
---|
59 | return -1; |
---|
60 | } else { |
---|
61 | PtrsAssignable ptrs( dest, env ); |
---|
62 | src->accept( ptrs ); |
---|
63 | return ptrs.get_result(); |
---|
64 | } // if |
---|
65 | } |
---|
66 | |
---|
67 | PtrsAssignable::PtrsAssignable( Type *dest, const TypeEnvironment &env ) : dest( dest ), result( 0 ), env( env ) {} |
---|
68 | |
---|
69 | void PtrsAssignable::visit( __attribute((unused)) VoidType *voidType ) { |
---|
70 | // T * = void * is disallowed - this is a change from C, where any |
---|
71 | // void * can be assigned or passed to a non-void pointer without a cast. |
---|
72 | } |
---|
73 | |
---|
74 | void PtrsAssignable::visit( __attribute__((unused)) BasicType *basicType ) {} |
---|
75 | void PtrsAssignable::visit( __attribute__((unused)) PointerType *pointerType ) {} |
---|
76 | void PtrsAssignable::visit( __attribute__((unused)) ArrayType *arrayType ) {} |
---|
77 | void PtrsAssignable::visit( __attribute__((unused)) FunctionType *functionType ) {} |
---|
78 | |
---|
79 | void PtrsAssignable::visit( __attribute__((unused)) StructInstType *inst ) {} |
---|
80 | void PtrsAssignable::visit( __attribute__((unused)) UnionInstType *inst ) {} |
---|
81 | |
---|
82 | void PtrsAssignable::visit( EnumInstType * ) { |
---|
83 | if ( dynamic_cast< BasicType* >( dest ) ) { |
---|
84 | // int * = E *, etc. is safe. This isn't technically correct, as each |
---|
85 | // enum has one basic type that it is compatible with, an that type can |
---|
86 | // differ from enum to enum. Without replicating GCC's internal logic, |
---|
87 | // there is no way to know which type this particular enum is compatible |
---|
88 | // with, so punt on this for now. |
---|
89 | result = 1; |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | void PtrsAssignable::visit( __attribute__((unused)) TraitInstType *inst ) {} |
---|
94 | void PtrsAssignable::visit( TypeInstType *inst ) { |
---|
95 | EqvClass eqvClass; |
---|
96 | if ( env.lookup( inst->get_name(), eqvClass ) && eqvClass.type ) { |
---|
97 | // T * = S * for any S depends on the type bound to T |
---|
98 | result = ptrsAssignable( eqvClass.type, dest, env ); |
---|
99 | } // if |
---|
100 | } |
---|
101 | |
---|
102 | void PtrsAssignable::visit( __attribute__((unused)) TupleType *tupleType ) {} |
---|
103 | void PtrsAssignable::visit( __attribute__((unused)) VarArgsType *varArgsType ) {} |
---|
104 | void PtrsAssignable::visit( __attribute__((unused)) ZeroType *zeroType ) {} |
---|
105 | void PtrsAssignable::visit( __attribute__((unused)) OneType *oneType ) {} |
---|
106 | |
---|
107 | } // namespace ResolvExpr |
---|
108 | |
---|
109 | // Local Variables: // |
---|
110 | // tab-width: 4 // |
---|
111 | // mode: c++ // |
---|
112 | // compile-command: "make install" // |
---|
113 | // End: // |
---|