source: src/ResolvExpr/PtrsAssignable.cc @ f07c1e6

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since f07c1e6 was f07c1e6, checked in by Rob Schluntz <rschlunt@…>, 6 years ago

Convert PtrsAssignable? to PassVisitor?

  • Property mode set to 100644
File size: 4.3 KB
RevLine 
[a32b204]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//
[5ccb10d]7// PtrsAssignable.cc --
[a32b204]8//
9// Author           : Richard C. Bilson
10// Created On       : Sun May 17 11:44:11 2015
[4040425]11// Last Modified By : Peter A. Buhr
12// Last Modified On : Wed Mar  2 17:36:05 2016
13// Update Count     : 8
[a32b204]14//
15
[f07c1e6]16#include "Common/PassVisitor.h"
[ea6332d]17#include "ResolvExpr/TypeEnvironment.h"  // for EqvClass, TypeEnvironment
18#include "SynTree/Type.h"                // for TypeInstType, Type, BasicType
19#include "SynTree/Visitor.h"             // for Visitor
[51b7345]20
21
22namespace ResolvExpr {
[f07c1e6]23        struct PtrsAssignable : public WithShortCircuiting {
[a32b204]24                PtrsAssignable( Type *dest, const TypeEnvironment &env );
25
26                int get_result() const { return result; }
27
[f07c1e6]28                void previsit( Type * ) { visit_children = false; }
29
30                void postvisit( VoidType * voidType );
31                void postvisit( BasicType * basicType );
32                void postvisit( PointerType * pointerType );
33                void postvisit( ArrayType * arrayType );
34                void postvisit( FunctionType * functionType );
35                void postvisit( StructInstType * inst );
36                void postvisit( UnionInstType * inst );
37                void postvisit( EnumInstType * inst );
38                void postvisit( TraitInstType * inst );
39                void postvisit( TypeInstType * inst );
40                void postvisit( TupleType * tupleType );
41                void postvisit( VarArgsType * varArgsType );
42                void postvisit( ZeroType * zeroType );
43                void postvisit( OneType * oneType );
[a32b204]44          private:
45                Type *dest;
46                int result;
47                const TypeEnvironment &env;
48        };
49
50        int ptrsAssignable( Type *src, Type *dest, const TypeEnvironment &env ) {
[b0837e4]51                // std::cerr << "assignable: " << src << " | " << dest << std::endl;
[a32b204]52                if ( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) {
53                        EqvClass eqvClass;
54                        if ( env.lookup( destAsTypeInst->get_name(), eqvClass ) ) {
55                                return ptrsAssignable( src, eqvClass.type, env );
56                        } // if
57                } // if
58                if ( dynamic_cast< VoidType* >( dest ) ) {
[b0837e4]59                        // void * = T * for any T is unsafe
60                        // xxx - this should be safe, but that currently breaks the build
61                        return -1;
[a32b204]62                } else {
[f07c1e6]63                        PassVisitor<PtrsAssignable> ptrs( dest, env );
[a32b204]64                        src->accept( ptrs );
[f07c1e6]65                        return ptrs.pass.get_result();
[a32b204]66                } // if
[c11e31c]67        }
[a32b204]68
[7e003011]69        PtrsAssignable::PtrsAssignable( Type *dest, const TypeEnvironment &env ) : dest( dest ), result( 0 ), env( env ) {}
[c11e31c]70
[f07c1e6]71        void PtrsAssignable::postvisit( VoidType * ) {
[4d5e57b]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.
[a32b204]74        }
[c11e31c]75
[f07c1e6]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 ) {}
[c11e31c]80
[f07c1e6]81        void PtrsAssignable::postvisit(  __attribute__((unused)) StructInstType *inst ) {}
82        void PtrsAssignable::postvisit(  __attribute__((unused)) UnionInstType *inst ) {}
[c11e31c]83
[f07c1e6]84        void PtrsAssignable::postvisit( EnumInstType * ) {
[b0837e4]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.
[a32b204]91                        result = 1;
92                }
93        }
[c11e31c]94
[f07c1e6]95        void PtrsAssignable::postvisit(  __attribute__((unused)) TraitInstType *inst ) {}
96        void PtrsAssignable::postvisit( TypeInstType *inst ) {
[a32b204]97                EqvClass eqvClass;
[1521de2]98                if ( env.lookup( inst->get_name(), eqvClass ) && eqvClass.type ) {
[b0837e4]99                        // T * = S * for any S depends on the type bound to T
[a32b204]100                        result = ptrsAssignable( eqvClass.type, dest, env );
101                } // if
[c11e31c]102        }
103
[f07c1e6]104        void PtrsAssignable::postvisit(  __attribute__((unused)) TupleType *tupleType ) {}
105        void PtrsAssignable::postvisit(  __attribute__((unused)) VarArgsType *varArgsType ) {}
106        void PtrsAssignable::postvisit(  __attribute__((unused)) ZeroType *zeroType ) {}
107        void PtrsAssignable::postvisit(  __attribute__((unused)) OneType *oneType ) {}
[5ccb10d]108
[51b7345]109} // namespace ResolvExpr
[a32b204]110
111// Local Variables: //
112// tab-width: 4 //
113// mode: c++ //
114// compile-command: "make install" //
115// End: //
Note: See TracBrowser for help on using the repository browser.