source: src/ResolvExpr/PtrsAssignable.cc @ 3ca540f

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 3ca540f was 4d5e57b, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Prevent implicit conversion of void * to other pointer type for parameter passing

  • Property mode set to 100644
File size: 4.2 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
[ea6332d]16#include "ResolvExpr/TypeEnvironment.h"  // for EqvClass, TypeEnvironment
17#include "SynTree/Type.h"                // for TypeInstType, Type, BasicType
18#include "SynTree/Visitor.h"             // for Visitor
[51b7345]19
20
21namespace ResolvExpr {
[a32b204]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 );
[4040425]36                virtual void visit( TraitInstType *inst );
[a32b204]37                virtual void visit( TypeInstType *inst );
38                virtual void visit( TupleType *tupleType );
[44b7088]39                virtual void visit( VarArgsType *varArgsType );
[89e6ffc]40                virtual void visit( ZeroType *zeroType );
41                virtual void visit( OneType *oneType );
[a32b204]42          private:
43                Type *dest;
44                int result;
45                const TypeEnvironment &env;
46        };
47
48        int ptrsAssignable( Type *src, Type *dest, const TypeEnvironment &env ) {
[b0837e4]49                // std::cerr << "assignable: " << src << " | " << dest << std::endl;
[a32b204]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 ) ) {
[b0837e4]57                        // void * = T * for any T is unsafe
58                        // xxx - this should be safe, but that currently breaks the build
59                        return -1;
[a32b204]60                } else {
61                        PtrsAssignable ptrs( dest, env );
62                        src->accept( ptrs );
63                        return ptrs.get_result();
64                } // if
[c11e31c]65        }
[a32b204]66
[7e003011]67        PtrsAssignable::PtrsAssignable( Type *dest, const TypeEnvironment &env ) : dest( dest ), result( 0 ), env( env ) {}
[c11e31c]68
[7e003011]69        void PtrsAssignable::visit( __attribute((unused)) VoidType *voidType ) {
[4d5e57b]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.
[a32b204]72        }
[c11e31c]73
[7e003011]74        void PtrsAssignable::visit( __attribute__((unused)) BasicType *basicType ) {}
75        void PtrsAssignable::visit( __attribute__((unused)) PointerType *pointerType ) {}
76        void PtrsAssignable::visit( __attribute__((unused)) ArrayType *arrayType ) {}
[b0837e4]77        void PtrsAssignable::visit( __attribute__((unused)) FunctionType *functionType ) {}
[c11e31c]78
[7e003011]79        void PtrsAssignable::visit(  __attribute__((unused)) StructInstType *inst ) {}
80        void PtrsAssignable::visit(  __attribute__((unused)) UnionInstType *inst ) {}
[c11e31c]81
[5ccb10d]82        void PtrsAssignable::visit( EnumInstType * ) {
[b0837e4]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.
[a32b204]89                        result = 1;
90                }
91        }
[c11e31c]92
[7e003011]93        void PtrsAssignable::visit(  __attribute__((unused)) TraitInstType *inst ) {}
[a32b204]94        void PtrsAssignable::visit( TypeInstType *inst ) {
95                EqvClass eqvClass;
[1521de20]96                if ( env.lookup( inst->get_name(), eqvClass ) && eqvClass.type ) {
[b0837e4]97                        // T * = S * for any S depends on the type bound to T
[a32b204]98                        result = ptrsAssignable( eqvClass.type, dest, env );
99                } // if
[c11e31c]100        }
101
[7e003011]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 ) {}
[5ccb10d]106
[51b7345]107} // namespace ResolvExpr
[a32b204]108
109// Local Variables: //
110// tab-width: 4 //
111// mode: c++ //
112// compile-command: "make install" //
113// End: //
Note: See TracBrowser for help on using the repository browser.