| [1931bb01] | 1 | // | 
|---|
|  | 2 | // Cforall Version 1.0.0 Copyright (C) 2018 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 | // | 
|---|
| [11df881] | 7 | // EnumAndPointerDecay.cpp -- Normalizes enumerations and types in functions. | 
|---|
| [1931bb01] | 8 | // | 
|---|
|  | 9 | // Author           : Andrew Beach | 
|---|
|  | 10 | // Created On       : Tue Jun 28 15:50:00 2022 | 
|---|
|  | 11 | // Last Modified By : Andrew Beach | 
|---|
| [e9e9f56] | 12 | // Last Modified On : Tue Sep 20 16:14:00 2022 | 
|---|
|  | 13 | // Update Count     : 1 | 
|---|
| [1931bb01] | 14 | // | 
|---|
|  | 15 |  | 
|---|
|  | 16 | #include "EnumAndPointerDecay.hpp" | 
|---|
|  | 17 |  | 
|---|
|  | 18 | #include "AST/CVQualifiers.hpp" | 
|---|
|  | 19 | #include "AST/Decl.hpp" | 
|---|
|  | 20 | #include "AST/Pass.hpp" | 
|---|
|  | 21 | #include "AST/Type.hpp" | 
|---|
|  | 22 | #include "SymTab/FixFunction.h" | 
|---|
| [e874605] | 23 | #include "Validate/NoIdSymbolTable.hpp" | 
|---|
| [1931bb01] | 24 |  | 
|---|
|  | 25 | namespace Validate { | 
|---|
|  | 26 |  | 
|---|
|  | 27 | namespace { | 
|---|
|  | 28 |  | 
|---|
| [e874605] | 29 | struct EnumAndPointerDecayCore final : public WithNoIdSymbolTable, public ast::WithCodeLocation { | 
|---|
| [1931bb01] | 30 | ast::EnumDecl const * previsit( ast::EnumDecl const * decl ); | 
|---|
|  | 31 | ast::FunctionDecl const * previsit( ast::FunctionDecl const * decl ); | 
|---|
|  | 32 | ast::FunctionType const * previsit( ast::FunctionType const * type ); | 
|---|
|  | 33 | }; | 
|---|
|  | 34 |  | 
|---|
|  | 35 | ast::EnumDecl const * EnumAndPointerDecayCore::previsit( | 
|---|
|  | 36 | ast::EnumDecl const * decl ) { | 
|---|
|  | 37 | if ( decl->members.empty() ) { | 
|---|
|  | 38 | return decl; | 
|---|
|  | 39 | } | 
|---|
|  | 40 | // Set the type of each member of the enumeration to be EnumContant. | 
|---|
|  | 41 | auto mut = ast::mutate( decl ); | 
|---|
| [e874605] | 42 | std::vector<ast::ptr<ast::Decl>> buffer; | 
|---|
| [19a8c40] | 43 | for ( auto member : decl->members ) { | 
|---|
|  | 44 | if ( ast::ObjectDecl const * object = member.as<ast::ObjectDecl>() ) { | 
|---|
|  | 45 | buffer.push_back( ast::mutate_field( object, | 
|---|
|  | 46 | &ast::ObjectDecl::type, | 
|---|
|  | 47 | new ast::EnumInstType( decl, ast::CV::Const ) ) ); | 
|---|
|  | 48 | } else if ( auto value = member.as<ast::InlineMemberDecl>() ) { | 
|---|
| [e874605] | 49 | if ( auto targetEnum = symtab.lookupEnum( value->name ) ) { | 
|---|
| [19a8c40] | 50 | for ( auto enumMember : targetEnum->members ) { | 
|---|
|  | 51 | auto enumObject = enumMember.strict_as<ast::ObjectDecl>(); | 
|---|
| [e874605] | 52 | buffer.push_back( new ast::ObjectDecl( | 
|---|
| [19a8c40] | 53 | // Get the location from the "inline" declaration. | 
|---|
|  | 54 | value->location, | 
|---|
|  | 55 | enumObject->name, | 
|---|
|  | 56 | // Construct a new EnumInstType as the type. | 
|---|
| [e874605] | 57 | new ast::EnumInstType( decl, ast::CV::Const ), | 
|---|
| [19a8c40] | 58 | enumObject->init, | 
|---|
|  | 59 | enumObject->storage, | 
|---|
|  | 60 | enumObject->linkage, | 
|---|
|  | 61 | enumObject->bitfieldWidth, | 
|---|
| [e874605] | 62 | {}, | 
|---|
| [19a8c40] | 63 | enumObject->funcSpec | 
|---|
| [e874605] | 64 | ) ); | 
|---|
|  | 65 | } | 
|---|
|  | 66 | } | 
|---|
|  | 67 | } | 
|---|
| [1931bb01] | 68 | } | 
|---|
| [e874605] | 69 | mut->members = buffer; | 
|---|
| [1931bb01] | 70 | return mut; | 
|---|
|  | 71 | } | 
|---|
|  | 72 |  | 
|---|
|  | 73 | template<typename Member> | 
|---|
|  | 74 | void fixFunctionList( CodeLocation const & location, bool isVarArgs, | 
|---|
|  | 75 | std::vector<ast::ptr<Member>> & list ) { | 
|---|
|  | 76 | bool hasVoid = false; | 
|---|
|  | 77 | for ( ast::ptr<Member> & member : list ) { | 
|---|
|  | 78 | member = SymTab::fixFunction( member, hasVoid ); | 
|---|
|  | 79 | } | 
|---|
|  | 80 |  | 
|---|
|  | 81 | // The remaining code only applies if void is present. | 
|---|
|  | 82 | if ( !hasVoid ) { | 
|---|
|  | 83 | return; | 
|---|
|  | 84 | } | 
|---|
|  | 85 |  | 
|---|
|  | 86 | // So there is a void, which is only valid if it is the only argument. | 
|---|
|  | 87 | if ( 1 < list.size() || isVarArgs ) { | 
|---|
|  | 88 | SemanticError( location, "invalid type void in function type " ); | 
|---|
|  | 89 | } | 
|---|
|  | 90 |  | 
|---|
|  | 91 | // If a single "void" thing in the list to remove it. | 
|---|
|  | 92 | list.clear(); | 
|---|
|  | 93 | } | 
|---|
|  | 94 |  | 
|---|
|  | 95 | ast::FunctionDecl const * EnumAndPointerDecayCore::previsit( | 
|---|
|  | 96 | ast::FunctionDecl const * decl ) { | 
|---|
|  | 97 | auto mut = ast::mutate( decl ); | 
|---|
|  | 98 | ast::ArgumentFlag isVarArgs = mut->type->isVarArgs; | 
|---|
|  | 99 | // It seems fixFunction (via fixFunctionList) does the pointer decay part. | 
|---|
|  | 100 | fixFunctionList( mut->location, isVarArgs, mut->params ); | 
|---|
|  | 101 | fixFunctionList( mut->location, false, mut->returns ); | 
|---|
|  | 102 | return mut; | 
|---|
|  | 103 | } | 
|---|
|  | 104 |  | 
|---|
|  | 105 | ast::FunctionType const * EnumAndPointerDecayCore::previsit( | 
|---|
|  | 106 | ast::FunctionType const * type ) { | 
|---|
|  | 107 | assert( location ); | 
|---|
|  | 108 | auto mut = ast::mutate( type ); | 
|---|
|  | 109 | ast::ArgumentFlag isVarArgs = mut->isVarArgs; | 
|---|
|  | 110 | // It seems fixFunction (via fixFunctionList) does the pointer decay part. | 
|---|
|  | 111 | fixFunctionList( *location, isVarArgs, mut->params ); | 
|---|
|  | 112 | fixFunctionList( *location, false, mut->returns ); | 
|---|
|  | 113 | return mut; | 
|---|
|  | 114 | } | 
|---|
|  | 115 |  | 
|---|
|  | 116 | } // namespace | 
|---|
|  | 117 |  | 
|---|
|  | 118 | void decayEnumsAndPointers( ast::TranslationUnit & translationUnit ) { | 
|---|
|  | 119 | ast::Pass<EnumAndPointerDecayCore>::run( translationUnit ); | 
|---|
|  | 120 | } | 
|---|
|  | 121 |  | 
|---|
|  | 122 | } // namespace Validate | 
|---|
|  | 123 |  | 
|---|
|  | 124 | // Local Variables: // | 
|---|
|  | 125 | // tab-width: 4 // | 
|---|
|  | 126 | // mode: c++ // | 
|---|
|  | 127 | // compile-command: "make install" // | 
|---|
|  | 128 | // End: // | 
|---|