source: src/ResolvExpr/PtrsAssignable.cc@ 10dc6908

ADT arm-eh ast-experimental cleanup-dtors enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 10dc6908 was b0837e4, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Minor cleanup in conversion code

  • Property mode set to 100644
File size: 4.2 KB
Line 
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
21namespace 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 if ( ! dynamic_cast< FunctionType* >( dest ) ) {
71 // T * = void * is safe for any T that is not a function type.
72 // xxx - this should be unsafe...
73 result = 1;
74 } // if
75 }
76
77 void PtrsAssignable::visit( __attribute__((unused)) BasicType *basicType ) {}
78 void PtrsAssignable::visit( __attribute__((unused)) PointerType *pointerType ) {}
79 void PtrsAssignable::visit( __attribute__((unused)) ArrayType *arrayType ) {}
80 void PtrsAssignable::visit( __attribute__((unused)) FunctionType *functionType ) {}
81
82 void PtrsAssignable::visit( __attribute__((unused)) StructInstType *inst ) {}
83 void PtrsAssignable::visit( __attribute__((unused)) UnionInstType *inst ) {}
84
85 void PtrsAssignable::visit( EnumInstType * ) {
86 if ( dynamic_cast< BasicType* >( dest ) ) {
87 // int * = E *, etc. is safe. This isn't technically correct, as each
88 // enum has one basic type that it is compatible with, an that type can
89 // differ from enum to enum. Without replicating GCC's internal logic,
90 // there is no way to know which type this particular enum is compatible
91 // with, so punt on this for now.
92 result = 1;
93 }
94 }
95
96 void PtrsAssignable::visit( __attribute__((unused)) TraitInstType *inst ) {}
97 void PtrsAssignable::visit( TypeInstType *inst ) {
98 EqvClass eqvClass;
99 if ( env.lookup( inst->get_name(), eqvClass ) && eqvClass.type ) {
100 // T * = S * for any S depends on the type bound to T
101 result = ptrsAssignable( eqvClass.type, dest, env );
102 } // if
103 }
104
105 void PtrsAssignable::visit( __attribute__((unused)) TupleType *tupleType ) {}
106 void PtrsAssignable::visit( __attribute__((unused)) VarArgsType *varArgsType ) {}
107 void PtrsAssignable::visit( __attribute__((unused)) ZeroType *zeroType ) {}
108 void PtrsAssignable::visit( __attribute__((unused)) OneType *oneType ) {}
109
110} // namespace ResolvExpr
111
112// Local Variables: //
113// tab-width: 4 //
114// mode: c++ //
115// compile-command: "make install" //
116// End: //
Note: See TracBrowser for help on using the repository browser.