| 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 | //
 | 
|---|
| 7 | // ForallPointerDecay.cpp --
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // Author           : Andrew Beach
 | 
|---|
| 10 | // Created On       : Tue Dec  7 16:15:00 2021
 | 
|---|
| 11 | // Last Modified By : Andrew Beach
 | 
|---|
| 12 | // Last Modified On : Fri Feb 11 10:59:00 2022
 | 
|---|
| 13 | // Update Count     : 0
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include "ForallPointerDecay.hpp"
 | 
|---|
| 17 | 
 | 
|---|
| 18 | #include "AST/Copy.hpp"
 | 
|---|
| 19 | #include "AST/Decl.hpp"
 | 
|---|
| 20 | #include "AST/DeclReplacer.hpp"
 | 
|---|
| 21 | #include "AST/Pass.hpp"
 | 
|---|
| 22 | #include "CodeGen/OperatorTable.h"
 | 
|---|
| 23 | #include "Common/CodeLocation.h"
 | 
|---|
| 24 | #include "SymTab/FixFunction.h"
 | 
|---|
| 25 | 
 | 
|---|
| 26 | #include "AST/Print.hpp"
 | 
|---|
| 27 | 
 | 
|---|
| 28 | namespace Validate {
 | 
|---|
| 29 | 
 | 
|---|
| 30 | namespace {
 | 
|---|
| 31 | 
 | 
|---|
| 32 | // Create a function type only using information on the FunctionDecl.
 | 
|---|
| 33 | ast::FunctionType * makeFuncType( const ast::FunctionDecl * decl ) {
 | 
|---|
| 34 |         auto type = new ast::FunctionType( decl->type->isVarArgs );
 | 
|---|
| 35 |         for ( auto & param : decl->params ) {
 | 
|---|
| 36 |                 type->params.emplace_back( param->get_type() );
 | 
|---|
| 37 |         }
 | 
|---|
| 38 |         for ( auto & ret : decl->returns ) {
 | 
|---|
| 39 |                 type->returns.emplace_back( ret->get_type() );
 | 
|---|
| 40 |         }
 | 
|---|
| 41 |         for ( auto & type_param : decl->type_params ) {
 | 
|---|
| 42 |                 type->forall.emplace_back(
 | 
|---|
| 43 |                         new ast::TypeInstType( type_param->name, type_param ) );
 | 
|---|
| 44 |         }
 | 
|---|
| 45 |         for ( auto & assertion : decl->assertions ) {
 | 
|---|
| 46 |                 type->assertions.emplace_back( new ast::VariableExpr(
 | 
|---|
| 47 |                         assertion->location, assertion ) );
 | 
|---|
| 48 |         }
 | 
|---|
| 49 |         return type;
 | 
|---|
| 50 | }
 | 
|---|
| 51 | 
 | 
|---|
| 52 | template<typename T>
 | 
|---|
| 53 | void append( std::vector<T> & dst, std::vector<T> & src ) {
 | 
|---|
| 54 |         dst.reserve( dst.size() + src.size() );
 | 
|---|
| 55 |         for ( auto el : src ) {
 | 
|---|
| 56 |                 dst.emplace_back( std::move( el ) );
 | 
|---|
| 57 |         }
 | 
|---|
| 58 |         src.clear();
 | 
|---|
| 59 | }
 | 
|---|
| 60 | 
 | 
|---|
| 61 | // Component Passes:
 | 
|---|
| 62 | /// Expand assertions from a trait instance,
 | 
|---|
| 63 | /// performing appropriate type variable substitutions.
 | 
|---|
| 64 | struct TraitExpander final {
 | 
|---|
| 65 |         using AssertionList = std::vector<ast::ptr<ast::DeclWithType>>;
 | 
|---|
| 66 | 
 | 
|---|
| 67 |         static AssertionList expandTrait( const ast::TraitInstType * inst ) {
 | 
|---|
| 68 |                 assertf( inst->base, "Trait instance not linked to base trait: %s",
 | 
|---|
| 69 |                         toCString( inst ) );
 | 
|---|
| 70 |                 AssertionList assertions;
 | 
|---|
| 71 |                 // Substitute trait decl parameters for instance parameters.
 | 
|---|
| 72 |                 ast::TypeSubstitution sub(
 | 
|---|
| 73 |                         inst->base->params.begin(),
 | 
|---|
| 74 |                         inst->base->params.end(),
 | 
|---|
| 75 |                         inst->params.begin()
 | 
|---|
| 76 |                 );
 | 
|---|
| 77 |                 for ( const ast::ptr<ast::Decl> & decl : inst->base->members ) {
 | 
|---|
| 78 |                         ast::ptr<ast::DeclWithType> copy =
 | 
|---|
| 79 |                                 ast::deepCopy( decl.strict_as<ast::DeclWithType>() );
 | 
|---|
| 80 | 
 | 
|---|
| 81 |                         int count = sub.apply( copy );
 | 
|---|
| 82 |                         (void)count;
 | 
|---|
| 83 | 
 | 
|---|
| 84 |                         // Update the type (type substution does not seem to cover it).
 | 
|---|
| 85 |                         if ( auto func = copy.as<ast::FunctionDecl>() ) {
 | 
|---|
| 86 |                                 auto mut = ast::mutate( func );
 | 
|---|
| 87 |                                 mut->type = makeFuncType( func );
 | 
|---|
| 88 |                                 copy = mut;
 | 
|---|
| 89 |                         }
 | 
|---|
| 90 |                         assertions.push_back( copy );
 | 
|---|
| 91 |                 }
 | 
|---|
| 92 |                 return assertions;
 | 
|---|
| 93 |         }
 | 
|---|
| 94 | 
 | 
|---|
| 95 |         static AssertionList expandAssertions( const AssertionList & old ) {
 | 
|---|
| 96 |                 AssertionList assertions;
 | 
|---|
| 97 |                 for ( const ast::ptr<ast::DeclWithType> & decl : old ) {
 | 
|---|
| 98 |                         if ( auto traitInst = dynamic_cast<const ast::TraitInstType *>(
 | 
|---|
| 99 |                                         decl->get_type() ) ) {
 | 
|---|
| 100 |                                 auto moreAsserts = expandTrait( traitInst );
 | 
|---|
| 101 |                                 append( assertions, moreAsserts );
 | 
|---|
| 102 |                         } else {
 | 
|---|
| 103 |                                 assertions.push_back( decl );
 | 
|---|
| 104 |                         }
 | 
|---|
| 105 |                 }
 | 
|---|
| 106 |                 return assertions;
 | 
|---|
| 107 |         }
 | 
|---|
| 108 | 
 | 
|---|
| 109 |         using TypeDeclVec = std::vector<ast::ptr<ast::TypeDecl>>;
 | 
|---|
| 110 | 
 | 
|---|
| 111 |         static TypeDeclVec expandTypeDecls( const TypeDeclVec & old ) {
 | 
|---|
| 112 |                 TypeDeclVec typeDecls;
 | 
|---|
| 113 |                 for ( const ast::TypeDecl * typeDecl : old ) {
 | 
|---|
| 114 |                         typeDecls.push_back( ast::mutate_field( typeDecl,
 | 
|---|
| 115 |                                 &ast::TypeDecl::assertions,
 | 
|---|
| 116 |                                 expandAssertions( typeDecl->assertions ) ) );
 | 
|---|
| 117 |                 }
 | 
|---|
| 118 |                 return typeDecls;
 | 
|---|
| 119 |         }
 | 
|---|
| 120 | 
 | 
|---|
| 121 |         const ast::FunctionDecl * postvisit( const ast::FunctionDecl * decl ) {
 | 
|---|
| 122 |                 if ( decl->assertions.empty() ) {
 | 
|---|
| 123 |                         return decl;
 | 
|---|
| 124 |                 }
 | 
|---|
| 125 |                 auto mut = ast::mutate( decl );
 | 
|---|
| 126 |                 mut->assertions = expandAssertions( decl->assertions );
 | 
|---|
| 127 |                 // Update the assertion list on the type as well.
 | 
|---|
| 128 |                 auto mutType = ast::mutate( mut->type.get() );
 | 
|---|
| 129 |                 mutType->assertions.clear();
 | 
|---|
| 130 |                 for ( auto & assertion : mut->assertions ) {
 | 
|---|
| 131 |                         mutType->assertions.emplace_back(
 | 
|---|
| 132 |                                 new ast::VariableExpr( mut->location, assertion ) );
 | 
|---|
| 133 |                 }
 | 
|---|
| 134 |                 mut->type = mutType;
 | 
|---|
| 135 |                 return mut;
 | 
|---|
| 136 |         }
 | 
|---|
| 137 | 
 | 
|---|
| 138 |         const ast::StructDecl * previsit( const ast::StructDecl * decl ) {
 | 
|---|
| 139 |                 if ( decl->params.empty() ) {
 | 
|---|
| 140 |                         return decl;
 | 
|---|
| 141 |                 }
 | 
|---|
| 142 |                 return ast::mutate_field( decl, &ast::StructDecl::params,
 | 
|---|
| 143 |                         expandTypeDecls( decl->params ) );
 | 
|---|
| 144 |         }
 | 
|---|
| 145 | 
 | 
|---|
| 146 |         const ast::UnionDecl * previsit( const ast::UnionDecl * decl ) {
 | 
|---|
| 147 |                 if ( decl->params.empty() ) {
 | 
|---|
| 148 |                         return decl;
 | 
|---|
| 149 |                 }
 | 
|---|
| 150 |                 return ast::mutate_field( decl, &ast::UnionDecl::params,
 | 
|---|
| 151 |                         expandTypeDecls( decl->params ) );
 | 
|---|
| 152 |         }
 | 
|---|
| 153 | };
 | 
|---|
| 154 | 
 | 
|---|
| 155 | std::vector<ast::ptr<ast::DeclWithType>> fixAssertionList(
 | 
|---|
| 156 |                 const ast::ParseNode * node,
 | 
|---|
| 157 |                 const std::vector<ast::ptr<ast::DeclWithType>> & assertions ) {
 | 
|---|
| 158 |         std::vector<ast::ptr<ast::DeclWithType>> ret;
 | 
|---|
| 159 |         for ( const auto & assn : assertions ) {
 | 
|---|
| 160 |                 bool isVoid = false;
 | 
|---|
| 161 |                 ret.push_back( SymTab::fixFunction( assn, isVoid ) );
 | 
|---|
| 162 |                 if ( isVoid ) {
 | 
|---|
| 163 |                         SemanticError( node->location, node,
 | 
|---|
| 164 |                                 "invalid type void in assertion of function " );
 | 
|---|
| 165 |                 }
 | 
|---|
| 166 |         }
 | 
|---|
| 167 |         return ret;
 | 
|---|
| 168 | }
 | 
|---|
| 169 | 
 | 
|---|
| 170 | std::vector<ast::ptr<ast::TypeDecl>> fixTypeDeclList(
 | 
|---|
| 171 |                 const ast::ParseNode * node,
 | 
|---|
| 172 |                 const std::vector<ast::ptr<ast::TypeDecl>> & type_params ) {
 | 
|---|
| 173 |         std::vector<ast::ptr<ast::TypeDecl>> ret;
 | 
|---|
| 174 |         ret.reserve( type_params.size() );
 | 
|---|
| 175 |         for ( const ast::TypeDecl * type_param : type_params ) {
 | 
|---|
| 176 |                 auto mutParam = ast::mutate( type_param );
 | 
|---|
| 177 |                 mutParam->assertions = fixAssertionList( node, mutParam->assertions );
 | 
|---|
| 178 |                 ret.push_back( mutParam );
 | 
|---|
| 179 |         }
 | 
|---|
| 180 |         return ret;
 | 
|---|
| 181 | }
 | 
|---|
| 182 | 
 | 
|---|
| 183 | struct AssertionFunctionFixer final {
 | 
|---|
| 184 |         const ast::FunctionDecl * previsit( const ast::FunctionDecl * decl ) {
 | 
|---|
| 185 |                 if ( decl->assertions.empty() ) {
 | 
|---|
| 186 |                         return decl;
 | 
|---|
| 187 |                 }
 | 
|---|
| 188 |                 return ast::mutate_field( decl, &ast::FunctionDecl::assertions,
 | 
|---|
| 189 |                         fixAssertionList( decl, decl->assertions ) );
 | 
|---|
| 190 |         }
 | 
|---|
| 191 | 
 | 
|---|
| 192 |         const ast::StructDecl * previsit( const ast::StructDecl * decl ) {
 | 
|---|
| 193 |                 if ( decl->params.empty() ) {
 | 
|---|
| 194 |                         return decl;
 | 
|---|
| 195 |                 }
 | 
|---|
| 196 |                 return ast::mutate_field( decl, &ast::StructDecl::params,
 | 
|---|
| 197 |                         fixTypeDeclList( decl, decl->params ) );
 | 
|---|
| 198 |         }
 | 
|---|
| 199 | 
 | 
|---|
| 200 |         const ast::UnionDecl * previsit( const ast::UnionDecl * decl ) {
 | 
|---|
| 201 |                 if ( decl->params.empty() ) {
 | 
|---|
| 202 |                         return decl;
 | 
|---|
| 203 |                 }
 | 
|---|
| 204 |                 return ast::mutate_field( decl, &ast::UnionDecl::params,
 | 
|---|
| 205 |                         fixTypeDeclList( decl, decl->params ) );
 | 
|---|
| 206 |         }
 | 
|---|
| 207 | };
 | 
|---|
| 208 | 
 | 
|---|
| 209 | struct OberatorChecker final {
 | 
|---|
| 210 |         void previsit( const ast::ObjectDecl * obj ) {
 | 
|---|
| 211 |                 if ( CodeGen::isOperator( obj->name ) ) {
 | 
|---|
| 212 |                         auto type = obj->type->stripDeclarator();
 | 
|---|
| 213 |                         if ( ! dynamic_cast< const ast::FunctionType * >( type ) ) {
 | 
|---|
| 214 |                                 SemanticError( obj->location,
 | 
|---|
| 215 |                                         toCString( "operator ", obj->name.c_str(), " is not "
 | 
|---|
| 216 |                                         "a function or function pointer." ) );
 | 
|---|
| 217 |                         }
 | 
|---|
| 218 |                 }
 | 
|---|
| 219 |         }
 | 
|---|
| 220 | };
 | 
|---|
| 221 | 
 | 
|---|
| 222 | struct UniqueFixCore final {
 | 
|---|
| 223 |         const ast::DeclWithType * postvisit( const ast::DeclWithType * decl ) {
 | 
|---|
| 224 |                 if ( decl->uniqueId ) {
 | 
|---|
| 225 |                         return decl;
 | 
|---|
| 226 |                 } else {
 | 
|---|
| 227 |                         auto mut = ast::mutate( decl );
 | 
|---|
| 228 |                         mut->fixUniqueId();
 | 
|---|
| 229 |                         return mut;
 | 
|---|
| 230 |                 }
 | 
|---|
| 231 |         }
 | 
|---|
| 232 | };
 | 
|---|
| 233 | 
 | 
|---|
| 234 | } // namespace
 | 
|---|
| 235 | 
 | 
|---|
| 236 | void decayForallPointers( ast::TranslationUnit & transUnit ) {
 | 
|---|
| 237 |         ast::Pass<TraitExpander>::run( transUnit );
 | 
|---|
| 238 |         ast::Pass<AssertionFunctionFixer>::run( transUnit );
 | 
|---|
| 239 |         ast::Pass<OberatorChecker>::run( transUnit );
 | 
|---|
| 240 |         ast::Pass<UniqueFixCore>::run( transUnit );
 | 
|---|
| 241 | }
 | 
|---|
| 242 | 
 | 
|---|
| 243 | } // namespace Validate
 | 
|---|
| 244 | 
 | 
|---|
| 245 | // Local Variables: //
 | 
|---|
| 246 | // tab-width: 4 //
 | 
|---|
| 247 | // mode: c++ //
 | 
|---|
| 248 | // compile-command: "make install" //
 | 
|---|
| 249 | // End: //
 | 
|---|