[0dd3a2f] | 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 | // |
---|
[40e636a] | 7 | // FixFunction.cc -- |
---|
[0dd3a2f] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Sun May 17 16:19:49 2015 |
---|
[1931bb01] | 11 | // Last Modified By : Andrew Beach |
---|
| 12 | // Last Modified On : Tue Jul 12 14:28:00 2022 |
---|
| 13 | // Update Count : 7 |
---|
[0dd3a2f] | 14 | // |
---|
| 15 | |
---|
[51b7345] | 16 | #include "FixFunction.h" |
---|
[30f9072] | 17 | |
---|
| 18 | #include <list> // for list |
---|
| 19 | |
---|
[c1ed2ee] | 20 | #include "AST/Decl.hpp" |
---|
| 21 | #include "AST/Pass.hpp" |
---|
| 22 | #include "AST/Type.hpp" |
---|
| 23 | #include "Common/utility.h" // for maybeClone, copy |
---|
[30f9072] | 24 | #include "SynTree/Declaration.h" // for FunctionDecl, ObjectDecl, Declarati... |
---|
[c3acf0aa] | 25 | #include "SynTree/Expression.h" // for Expression |
---|
[30f9072] | 26 | #include "SynTree/Type.h" // for ArrayType, PointerType, Type, Basic... |
---|
[51b7345] | 27 | |
---|
| 28 | namespace SymTab { |
---|
[c1ed2ee] | 29 | class FixFunction_old : public WithShortCircuiting { |
---|
| 30 | typedef Mutator Parent; |
---|
| 31 | public: |
---|
| 32 | FixFunction_old() : isVoid( false ) {} |
---|
[51b7345] | 33 | |
---|
[c1ed2ee] | 34 | void premutate(FunctionDecl *functionDecl); |
---|
| 35 | DeclarationWithType* postmutate(FunctionDecl *functionDecl); |
---|
[21b7161] | 36 | |
---|
[c1ed2ee] | 37 | Type * postmutate(ArrayType * arrayType); |
---|
| 38 | |
---|
| 39 | void premutate(ArrayType * arrayType); |
---|
| 40 | void premutate(VoidType * voidType); |
---|
| 41 | void premutate(BasicType * basicType); |
---|
| 42 | void premutate(PointerType * pointerType); |
---|
| 43 | void premutate(StructInstType * aggregateUseType); |
---|
| 44 | void premutate(UnionInstType * aggregateUseType); |
---|
| 45 | void premutate(EnumInstType * aggregateUseType); |
---|
| 46 | void premutate(TraitInstType * aggregateUseType); |
---|
| 47 | void premutate(TypeInstType * aggregateUseType); |
---|
| 48 | void premutate(TupleType * tupleType); |
---|
| 49 | void premutate(VarArgsType * varArgsType); |
---|
| 50 | void premutate(ZeroType * zeroType); |
---|
| 51 | void premutate(OneType * oneType); |
---|
| 52 | |
---|
| 53 | bool isVoid; |
---|
| 54 | }; |
---|
| 55 | |
---|
| 56 | DeclarationWithType * FixFunction_old::postmutate(FunctionDecl *functionDecl) { |
---|
[8b11840] | 57 | // can't delete function type because it may contain assertions, so transfer ownership to new object |
---|
[21b7161] | 58 | ObjectDecl *pointer = new ObjectDecl( functionDecl->name, functionDecl->get_storageClasses(), functionDecl->linkage, nullptr, new PointerType( Type::Qualifiers(), functionDecl->type ), nullptr, functionDecl->attributes ); |
---|
[d53772d] | 59 | pointer->location = functionDecl->location; |
---|
[21b7161] | 60 | functionDecl->attributes.clear(); |
---|
[8b11840] | 61 | functionDecl->type = nullptr; |
---|
[0dd3a2f] | 62 | delete functionDecl; |
---|
| 63 | return pointer; |
---|
| 64 | } |
---|
[51b7345] | 65 | |
---|
[4bda2cf] | 66 | // xxx - this passes on void[], e.g. |
---|
| 67 | // void foo(void [10]); |
---|
| 68 | // does not cause an error |
---|
| 69 | |
---|
[c1ed2ee] | 70 | Type * FixFunction_old::postmutate(ArrayType *arrayType) { |
---|
[40e636a] | 71 | // need to recursively mutate the base type in order for multi-dimensional arrays to work. |
---|
[21b7161] | 72 | PointerType *pointerType = new PointerType( arrayType->get_qualifiers(), arrayType->base, arrayType->dimension, arrayType->isVarLen, arrayType->isStatic ); |
---|
[d53772d] | 73 | pointerType->location = arrayType->location; |
---|
[21b7161] | 74 | arrayType->base = nullptr; |
---|
| 75 | arrayType->dimension = nullptr; |
---|
[0dd3a2f] | 76 | delete arrayType; |
---|
| 77 | return pointerType; |
---|
| 78 | } |
---|
[51b7345] | 79 | |
---|
[c1ed2ee] | 80 | void FixFunction_old::premutate(VoidType *) { |
---|
[21b7161] | 81 | isVoid = true; |
---|
[89e6ffc] | 82 | } |
---|
| 83 | |
---|
[c1ed2ee] | 84 | void FixFunction_old::premutate(FunctionDecl *) { visit_children = false; } |
---|
| 85 | void FixFunction_old::premutate(ArrayType *) { visit_children = false; } |
---|
| 86 | void FixFunction_old::premutate(BasicType *) { visit_children = false; } |
---|
| 87 | void FixFunction_old::premutate(PointerType *) { visit_children = false; } |
---|
| 88 | void FixFunction_old::premutate(StructInstType *) { visit_children = false; } |
---|
| 89 | void FixFunction_old::premutate(UnionInstType *) { visit_children = false; } |
---|
| 90 | void FixFunction_old::premutate(EnumInstType *) { visit_children = false; } |
---|
| 91 | void FixFunction_old::premutate(TraitInstType *) { visit_children = false; } |
---|
| 92 | void FixFunction_old::premutate(TypeInstType *) { visit_children = false; } |
---|
| 93 | void FixFunction_old::premutate(TupleType *) { visit_children = false; } |
---|
| 94 | void FixFunction_old::premutate(VarArgsType *) { visit_children = false; } |
---|
| 95 | void FixFunction_old::premutate(ZeroType *) { visit_children = false; } |
---|
| 96 | void FixFunction_old::premutate(OneType *) { visit_children = false; } |
---|
[4bda2cf] | 97 | |
---|
| 98 | bool fixFunction( DeclarationWithType *& dwt ) { |
---|
[c1ed2ee] | 99 | PassVisitor<FixFunction_old> fixer; |
---|
[4bda2cf] | 100 | dwt = dwt->acceptMutator( fixer ); |
---|
| 101 | return fixer.pass.isVoid; |
---|
| 102 | } |
---|
[c1ed2ee] | 103 | |
---|
| 104 | namespace { |
---|
| 105 | struct FixFunction_new final : public ast::WithShortCircuiting { |
---|
| 106 | bool isVoid = false; |
---|
| 107 | |
---|
[c408483] | 108 | void previsit( const ast::FunctionDecl * ) { visit_children = false; } |
---|
[c1ed2ee] | 109 | |
---|
[c408483] | 110 | const ast::DeclWithType * postvisit( const ast::FunctionDecl * func ) { |
---|
[c1ed2ee] | 111 | return new ast::ObjectDecl{ |
---|
| 112 | func->location, func->name, new ast::PointerType{ func->type }, nullptr, |
---|
| 113 | func->storage, func->linkage, nullptr, copy( func->attributes ) }; |
---|
| 114 | } |
---|
| 115 | |
---|
[c408483] | 116 | void previsit( const ast::ArrayType * ) { visit_children = false; } |
---|
[c1ed2ee] | 117 | |
---|
[c408483] | 118 | const ast::Type * postvisit( const ast::ArrayType * array ) { |
---|
[c1ed2ee] | 119 | return new ast::PointerType{ |
---|
| 120 | array->base, array->dimension, array->isVarLen, array->isStatic, |
---|
| 121 | array->qualifiers }; |
---|
| 122 | } |
---|
| 123 | |
---|
[1931bb01] | 124 | void previsit( const ast::FunctionType * ) { visit_children = false; } |
---|
| 125 | |
---|
| 126 | const ast::Type * postvisit( const ast::FunctionType * type ) { |
---|
| 127 | return new ast::PointerType( type ); |
---|
| 128 | } |
---|
| 129 | |
---|
[c408483] | 130 | void previsit( const ast::VoidType * ) { isVoid = true; } |
---|
| 131 | |
---|
| 132 | void previsit( const ast::BasicType * ) { visit_children = false; } |
---|
| 133 | void previsit( const ast::PointerType * ) { visit_children = false; } |
---|
| 134 | void previsit( const ast::StructInstType * ) { visit_children = false; } |
---|
| 135 | void previsit( const ast::UnionInstType * ) { visit_children = false; } |
---|
| 136 | void previsit( const ast::EnumInstType * ) { visit_children = false; } |
---|
| 137 | void previsit( const ast::TraitInstType * ) { visit_children = false; } |
---|
| 138 | void previsit( const ast::TypeInstType * ) { visit_children = false; } |
---|
| 139 | void previsit( const ast::TupleType * ) { visit_children = false; } |
---|
| 140 | void previsit( const ast::VarArgsType * ) { visit_children = false; } |
---|
| 141 | void previsit( const ast::ZeroType * ) { visit_children = false; } |
---|
| 142 | void previsit( const ast::OneType * ) { visit_children = false; } |
---|
[c1ed2ee] | 143 | }; |
---|
| 144 | } // anonymous namespace |
---|
| 145 | |
---|
| 146 | const ast::DeclWithType * fixFunction( const ast::DeclWithType * dwt, bool & isVoid ) { |
---|
| 147 | ast::Pass< FixFunction_new > fixer; |
---|
| 148 | dwt = dwt->accept( fixer ); |
---|
[7ff3e522] | 149 | isVoid |= fixer.core.isVoid; |
---|
[c1ed2ee] | 150 | return dwt; |
---|
| 151 | } |
---|
| 152 | |
---|
[1931bb01] | 153 | const ast::Type * fixFunction( const ast::Type * type, bool & isVoid ) { |
---|
| 154 | ast::Pass< FixFunction_new > fixer; |
---|
| 155 | type = type->accept( fixer ); |
---|
| 156 | isVoid |= fixer.core.isVoid; |
---|
| 157 | return type; |
---|
| 158 | } |
---|
| 159 | |
---|
[51b7345] | 160 | } // namespace SymTab |
---|
[0dd3a2f] | 161 | |
---|
| 162 | // Local Variables: // |
---|
| 163 | // tab-width: 4 // |
---|
| 164 | // mode: c++ // |
---|
| 165 | // compile-command: "make install" // |
---|
| 166 | // End: // |
---|