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