| 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 | //
 | 
|---|
| 7 | // Box.cc --
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // Author           : Richard C. Bilson
 | 
|---|
| 10 | // Created On       : Mon May 18 07:44:20 2015
 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr
 | 
|---|
| 12 | // Last Modified On : Wed Jun 29 21:43:03 2016
 | 
|---|
| 13 | // Update Count     : 296
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include <algorithm>
 | 
|---|
| 17 | #include <iterator>
 | 
|---|
| 18 | #include <list>
 | 
|---|
| 19 | #include <map>
 | 
|---|
| 20 | #include <set>
 | 
|---|
| 21 | #include <stack>
 | 
|---|
| 22 | #include <string>
 | 
|---|
| 23 | #include <utility>
 | 
|---|
| 24 | #include <vector>
 | 
|---|
| 25 | #include <cassert>
 | 
|---|
| 26 | 
 | 
|---|
| 27 | #include "Box.h"
 | 
|---|
| 28 | #include "DeclMutator.h"
 | 
|---|
| 29 | #include "PolyMutator.h"
 | 
|---|
| 30 | #include "FindFunction.h"
 | 
|---|
| 31 | #include "ScopedSet.h"
 | 
|---|
| 32 | #include "ScrubTyVars.h"
 | 
|---|
| 33 | 
 | 
|---|
| 34 | #include "Parser/ParseNode.h"
 | 
|---|
| 35 | 
 | 
|---|
| 36 | #include "SynTree/Constant.h"
 | 
|---|
| 37 | #include "SynTree/Declaration.h"
 | 
|---|
| 38 | #include "SynTree/Expression.h"
 | 
|---|
| 39 | #include "SynTree/Initializer.h"
 | 
|---|
| 40 | #include "SynTree/Mutator.h"
 | 
|---|
| 41 | #include "SynTree/Statement.h"
 | 
|---|
| 42 | #include "SynTree/Type.h"
 | 
|---|
| 43 | #include "SynTree/TypeSubstitution.h"
 | 
|---|
| 44 | 
 | 
|---|
| 45 | #include "ResolvExpr/TypeEnvironment.h"
 | 
|---|
| 46 | #include "ResolvExpr/TypeMap.h"
 | 
|---|
| 47 | #include "ResolvExpr/typeops.h"
 | 
|---|
| 48 | 
 | 
|---|
| 49 | #include "SymTab/Indexer.h"
 | 
|---|
| 50 | #include "SymTab/Mangler.h"
 | 
|---|
| 51 | 
 | 
|---|
| 52 | #include "Common/ScopedMap.h"
 | 
|---|
| 53 | #include "Common/SemanticError.h"
 | 
|---|
| 54 | #include "Common/UniqueName.h"
 | 
|---|
| 55 | #include "Common/utility.h"
 | 
|---|
| 56 | 
 | 
|---|
| 57 | #include <ext/functional> // temporary
 | 
|---|
| 58 | 
 | 
|---|
| 59 | namespace GenPoly {
 | 
|---|
| 60 |         namespace {
 | 
|---|
| 61 |                 const std::list<Label> noLabels;
 | 
|---|
| 62 | 
 | 
|---|
| 63 |                 FunctionType *makeAdapterType( FunctionType *adaptee, const TyVarMap &tyVars );
 | 
|---|
| 64 | 
 | 
|---|
| 65 |                 /// Adds layout-generation functions to polymorphic types
 | 
|---|
| 66 |                 class LayoutFunctionBuilder : public DeclMutator {
 | 
|---|
| 67 |                         unsigned int functionNesting;  // current level of nested functions
 | 
|---|
| 68 |                 public:
 | 
|---|
| 69 |                         LayoutFunctionBuilder() : functionNesting( 0 ) {}
 | 
|---|
| 70 | 
 | 
|---|
| 71 |                         virtual DeclarationWithType *mutate( FunctionDecl *functionDecl );
 | 
|---|
| 72 |                         virtual Declaration *mutate( StructDecl *structDecl );
 | 
|---|
| 73 |                         virtual Declaration *mutate( UnionDecl *unionDecl );
 | 
|---|
| 74 |                 };
 | 
|---|
| 75 | 
 | 
|---|
| 76 |                 /// Replaces polymorphic return types with out-parameters, replaces calls to polymorphic functions with adapter calls as needed, and adds appropriate type variables to the function call
 | 
|---|
| 77 |                 class Pass1 : public PolyMutator {
 | 
|---|
| 78 |                   public:
 | 
|---|
| 79 |                         Pass1();
 | 
|---|
| 80 |                         virtual Expression *mutate( ApplicationExpr *appExpr );
 | 
|---|
| 81 |                         virtual Expression *mutate( AddressExpr *addrExpr );
 | 
|---|
| 82 |                         virtual Expression *mutate( UntypedExpr *expr );
 | 
|---|
| 83 |                         virtual DeclarationWithType* mutate( FunctionDecl *functionDecl );
 | 
|---|
| 84 |                         virtual TypeDecl *mutate( TypeDecl *typeDecl );
 | 
|---|
| 85 |                         virtual Expression *mutate( CommaExpr *commaExpr );
 | 
|---|
| 86 |                         virtual Expression *mutate( ConditionalExpr *condExpr );
 | 
|---|
| 87 |                         virtual Statement * mutate( ReturnStmt *returnStmt );
 | 
|---|
| 88 |                         virtual Type *mutate( PointerType *pointerType );
 | 
|---|
| 89 |                         virtual Type * mutate( FunctionType *functionType );
 | 
|---|
| 90 | 
 | 
|---|
| 91 |                         virtual void doBeginScope();
 | 
|---|
| 92 |                         virtual void doEndScope();
 | 
|---|
| 93 |                   private:
 | 
|---|
| 94 |                         /// Pass the extra type parameters from polymorphic generic arguments or return types into a function application
 | 
|---|
| 95 |                         void passArgTypeVars( ApplicationExpr *appExpr, Type *parmType, Type *argBaseType, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars, std::set< std::string > &seenTypes );
 | 
|---|
| 96 |                         /// passes extra type parameters into a polymorphic function application
 | 
|---|
| 97 |                         void passTypeVars( ApplicationExpr *appExpr, ReferenceToType *polyRetType, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars );
 | 
|---|
| 98 |                         /// wraps a function application with a new temporary for the out-parameter return value
 | 
|---|
| 99 |                         Expression *addRetParam( ApplicationExpr *appExpr, FunctionType *function, Type *retType, std::list< Expression *>::iterator &arg );
 | 
|---|
| 100 |                         /// Replaces all the type parameters of a generic type with their concrete equivalents under the current environment
 | 
|---|
| 101 |                         void replaceParametersWithConcrete( ApplicationExpr *appExpr, std::list< Expression* >& params );
 | 
|---|
| 102 |                         /// Replaces a polymorphic type with its concrete equivalant under the current environment (returns itself if concrete).
 | 
|---|
| 103 |                         /// If `doClone` is set to false, will not clone interior types
 | 
|---|
| 104 |                         Type *replaceWithConcrete( ApplicationExpr *appExpr, Type *type, bool doClone = true );
 | 
|---|
| 105 |                         /// wraps a function application returning a polymorphic type with a new temporary for the out-parameter return value
 | 
|---|
| 106 |                         Expression *addDynRetParam( ApplicationExpr *appExpr, FunctionType *function, ReferenceToType *polyType, std::list< Expression *>::iterator &arg );
 | 
|---|
| 107 |                         Expression *applyAdapter( ApplicationExpr *appExpr, FunctionType *function, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars );
 | 
|---|
| 108 |                         void boxParam( Type *formal, Expression *&arg, const TyVarMap &exprTyVars );
 | 
|---|
| 109 |                         void boxParams( ApplicationExpr *appExpr, FunctionType *function, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars );
 | 
|---|
| 110 |                         void addInferredParams( ApplicationExpr *appExpr, FunctionType *functionType, std::list< Expression *>::iterator &arg, const TyVarMap &tyVars );
 | 
|---|
| 111 |                         /// Stores assignment operators from assertion list in local map of assignment operations
 | 
|---|
| 112 |                         void findTypeOps( const std::list< TypeDecl *> &forall );
 | 
|---|
| 113 |                         void passAdapters( ApplicationExpr *appExpr, FunctionType *functionType, const TyVarMap &exprTyVars );
 | 
|---|
| 114 |                         FunctionDecl *makeAdapter( FunctionType *adaptee, FunctionType *realType, const std::string &mangleName, const TyVarMap &tyVars );
 | 
|---|
| 115 |                         /// Replaces intrinsic operator functions with their arithmetic desugaring
 | 
|---|
| 116 |                         Expression *handleIntrinsics( ApplicationExpr *appExpr );
 | 
|---|
| 117 |                         /// Inserts a new temporary variable into the current scope with an auto-generated name
 | 
|---|
| 118 |                         ObjectDecl *makeTemporary( Type *type );
 | 
|---|
| 119 | 
 | 
|---|
| 120 |                         ScopedMap< std::string, DeclarationWithType* > assignOps;    ///< Currently known type variable assignment operators
 | 
|---|
| 121 |                         ScopedMap< std::string, DeclarationWithType* > ctorOps;      ///< Currently known type variable constructors
 | 
|---|
| 122 |                         ScopedMap< std::string, DeclarationWithType* > copyOps;      ///< Currently known type variable copy constructors
 | 
|---|
| 123 |                         ScopedMap< std::string, DeclarationWithType* > dtorOps;      ///< Currently known type variable destructors
 | 
|---|
| 124 |                         ResolvExpr::TypeMap< DeclarationWithType > scopedAssignOps;  ///< Currently known assignment operators
 | 
|---|
| 125 |                         ResolvExpr::TypeMap< DeclarationWithType > scopedCtorOps;    ///< Currently known assignment operators
 | 
|---|
| 126 |                         ResolvExpr::TypeMap< DeclarationWithType > scopedCopyOps;    ///< Currently known assignment operators
 | 
|---|
| 127 |                         ResolvExpr::TypeMap< DeclarationWithType > scopedDtorOps;    ///< Currently known assignment operators
 | 
|---|
| 128 |                         ScopedMap< std::string, DeclarationWithType* > adapters;     ///< Set of adapter functions in the current scope
 | 
|---|
| 129 | 
 | 
|---|
| 130 |                         DeclarationWithType *retval;
 | 
|---|
| 131 |                         bool useRetval;
 | 
|---|
| 132 |                         UniqueName tempNamer;
 | 
|---|
| 133 |                 };
 | 
|---|
| 134 | 
 | 
|---|
| 135 |                 /// * Moves polymorphic returns in function types to pointer-type parameters
 | 
|---|
| 136 |                 /// * adds type size and assertion parameters to parameter lists
 | 
|---|
| 137 |                 class Pass2 : public PolyMutator {
 | 
|---|
| 138 |                   public:
 | 
|---|
| 139 |                         template< typename DeclClass >
 | 
|---|
| 140 |                         DeclClass *handleDecl( DeclClass *decl, Type *type );
 | 
|---|
| 141 |                         virtual DeclarationWithType *mutate( FunctionDecl *functionDecl );
 | 
|---|
| 142 |                         virtual ObjectDecl *mutate( ObjectDecl *objectDecl );
 | 
|---|
| 143 |                         virtual TypeDecl *mutate( TypeDecl *typeDecl );
 | 
|---|
| 144 |                         virtual TypedefDecl *mutate( TypedefDecl *typedefDecl );
 | 
|---|
| 145 |                         virtual Type *mutate( PointerType *pointerType );
 | 
|---|
| 146 |                         virtual Type *mutate( FunctionType *funcType );
 | 
|---|
| 147 | 
 | 
|---|
| 148 |                   private:
 | 
|---|
| 149 |                         void addAdapters( FunctionType *functionType );
 | 
|---|
| 150 | 
 | 
|---|
| 151 |                         std::map< UniqueId, std::string > adapterName;
 | 
|---|
| 152 |                 };
 | 
|---|
| 153 | 
 | 
|---|
| 154 |                 /// Replaces member and size/align/offsetof expressions on polymorphic generic types with calculated expressions.
 | 
|---|
| 155 |                 /// * Replaces member expressions for polymorphic types with calculated add-field-offset-and-dereference
 | 
|---|
| 156 |                 /// * Calculates polymorphic offsetof expressions from offset array
 | 
|---|
| 157 |                 /// * Inserts dynamic calculation of polymorphic type layouts where needed
 | 
|---|
| 158 |                 class PolyGenericCalculator : public PolyMutator {
 | 
|---|
| 159 |                 public:
 | 
|---|
| 160 |                         typedef PolyMutator Parent;
 | 
|---|
| 161 |                         using Parent::mutate;
 | 
|---|
| 162 | 
 | 
|---|
| 163 |                         template< typename DeclClass >
 | 
|---|
| 164 |                         DeclClass *handleDecl( DeclClass *decl, Type *type );
 | 
|---|
| 165 |                         virtual DeclarationWithType *mutate( FunctionDecl *functionDecl );
 | 
|---|
| 166 |                         virtual ObjectDecl *mutate( ObjectDecl *objectDecl );
 | 
|---|
| 167 |                         virtual TypedefDecl *mutate( TypedefDecl *objectDecl );
 | 
|---|
| 168 |                         virtual TypeDecl *mutate( TypeDecl *objectDecl );
 | 
|---|
| 169 |                         virtual Statement *mutate( DeclStmt *declStmt );
 | 
|---|
| 170 |                         virtual Type *mutate( PointerType *pointerType );
 | 
|---|
| 171 |                         virtual Type *mutate( FunctionType *funcType );
 | 
|---|
| 172 |                         virtual Expression *mutate( MemberExpr *memberExpr );
 | 
|---|
| 173 |                         virtual Expression *mutate( SizeofExpr *sizeofExpr );
 | 
|---|
| 174 |                         virtual Expression *mutate( AlignofExpr *alignofExpr );
 | 
|---|
| 175 |                         virtual Expression *mutate( OffsetofExpr *offsetofExpr );
 | 
|---|
| 176 |                         virtual Expression *mutate( OffsetPackExpr *offsetPackExpr );
 | 
|---|
| 177 | 
 | 
|---|
| 178 |                         virtual void doBeginScope();
 | 
|---|
| 179 |                         virtual void doEndScope();
 | 
|---|
| 180 | 
 | 
|---|
| 181 |                 private:
 | 
|---|
| 182 |                         /// Makes a new variable in the current scope with the given name, type & optional initializer
 | 
|---|
| 183 |                         ObjectDecl *makeVar( const std::string &name, Type *type, Initializer *init = 0 );
 | 
|---|
| 184 |                         /// returns true if the type has a dynamic layout; such a layout will be stored in appropriately-named local variables when the function returns
 | 
|---|
| 185 |                         bool findGeneric( Type *ty );
 | 
|---|
| 186 |                         /// adds type parameters to the layout call; will generate the appropriate parameters if needed
 | 
|---|
| 187 |                         void addOtypeParamsToLayoutCall( UntypedExpr *layoutCall, const std::list< Type* > &otypeParams );
 | 
|---|
| 188 | 
 | 
|---|
| 189 |                         /// Enters a new scope for type-variables, adding the type variables from ty
 | 
|---|
| 190 |                         void beginTypeScope( Type *ty );
 | 
|---|
| 191 |                         /// Exits the type-variable scope
 | 
|---|
| 192 |                         void endTypeScope();
 | 
|---|
| 193 | 
 | 
|---|
| 194 |                         ScopedSet< std::string > knownLayouts;          ///< Set of generic type layouts known in the current scope, indexed by sizeofName
 | 
|---|
| 195 |                         ScopedSet< std::string > knownOffsets;          ///< Set of non-generic types for which the offset array exists in the current scope, indexed by offsetofName
 | 
|---|
| 196 |                 };
 | 
|---|
| 197 | 
 | 
|---|
| 198 |                 /// Replaces initialization of polymorphic values with alloca, declaration of dtype/ftype with appropriate void expression, and sizeof expressions of polymorphic types with the proper variable
 | 
|---|
| 199 |                 class Pass3 : public PolyMutator {
 | 
|---|
| 200 |                   public:
 | 
|---|
| 201 |                         template< typename DeclClass >
 | 
|---|
| 202 |                         DeclClass *handleDecl( DeclClass *decl, Type *type );
 | 
|---|
| 203 |                         virtual DeclarationWithType *mutate( FunctionDecl *functionDecl );
 | 
|---|
| 204 |                         virtual ObjectDecl *mutate( ObjectDecl *objectDecl );
 | 
|---|
| 205 |                         virtual TypedefDecl *mutate( TypedefDecl *objectDecl );
 | 
|---|
| 206 |                         virtual TypeDecl *mutate( TypeDecl *objectDecl );
 | 
|---|
| 207 |                         virtual Type *mutate( PointerType *pointerType );
 | 
|---|
| 208 |                         virtual Type *mutate( FunctionType *funcType );
 | 
|---|
| 209 |                   private:
 | 
|---|
| 210 |                 };
 | 
|---|
| 211 | 
 | 
|---|
| 212 |         } // anonymous namespace
 | 
|---|
| 213 | 
 | 
|---|
| 214 |         /// version of mutateAll with special handling for translation unit so you can check the end of the prelude when debugging
 | 
|---|
| 215 |         template< typename MutatorType >
 | 
|---|
| 216 |         inline void mutateTranslationUnit( std::list< Declaration* > &translationUnit, MutatorType &mutator ) {
 | 
|---|
| 217 |                 bool seenIntrinsic = false;
 | 
|---|
| 218 |                 SemanticError errors;
 | 
|---|
| 219 |                 for ( typename std::list< Declaration* >::iterator i = translationUnit.begin(); i != translationUnit.end(); ++i ) {
 | 
|---|
| 220 |                         try {
 | 
|---|
| 221 |                                 if ( *i ) {
 | 
|---|
| 222 |                                         if ( (*i)->get_linkage() == LinkageSpec::Intrinsic ) {
 | 
|---|
| 223 |                                                 seenIntrinsic = true;
 | 
|---|
| 224 |                                         } else if ( seenIntrinsic ) {
 | 
|---|
| 225 |                                                 seenIntrinsic = false; // break on this line when debugging for end of prelude
 | 
|---|
| 226 |                                         }
 | 
|---|
| 227 | 
 | 
|---|
| 228 |                                         *i = dynamic_cast< Declaration* >( (*i)->acceptMutator( mutator ) );
 | 
|---|
| 229 |                                         assert( *i );
 | 
|---|
| 230 |                                 } // if
 | 
|---|
| 231 |                         } catch( SemanticError &e ) {
 | 
|---|
| 232 |                                 errors.append( e );
 | 
|---|
| 233 |                         } // try
 | 
|---|
| 234 |                 } // for
 | 
|---|
| 235 |                 if ( ! errors.isEmpty() ) {
 | 
|---|
| 236 |                         throw errors;
 | 
|---|
| 237 |                 } // if
 | 
|---|
| 238 |         }
 | 
|---|
| 239 | 
 | 
|---|
| 240 |         void box( std::list< Declaration *>& translationUnit ) {
 | 
|---|
| 241 |                 LayoutFunctionBuilder layoutBuilder;
 | 
|---|
| 242 |                 Pass1 pass1;
 | 
|---|
| 243 |                 Pass2 pass2;
 | 
|---|
| 244 |                 PolyGenericCalculator polyCalculator;
 | 
|---|
| 245 |                 Pass3 pass3;
 | 
|---|
| 246 | 
 | 
|---|
| 247 |                 layoutBuilder.mutateDeclarationList( translationUnit );
 | 
|---|
| 248 |                 mutateTranslationUnit/*All*/( translationUnit, pass1 );
 | 
|---|
| 249 |                 mutateTranslationUnit/*All*/( translationUnit, pass2 );
 | 
|---|
| 250 |                 mutateTranslationUnit/*All*/( translationUnit, polyCalculator );
 | 
|---|
| 251 |                 mutateTranslationUnit/*All*/( translationUnit, pass3 );
 | 
|---|
| 252 |         }
 | 
|---|
| 253 | 
 | 
|---|
| 254 |         ////////////////////////////////// LayoutFunctionBuilder ////////////////////////////////////////////
 | 
|---|
| 255 | 
 | 
|---|
| 256 |         DeclarationWithType *LayoutFunctionBuilder::mutate( FunctionDecl *functionDecl ) {
 | 
|---|
| 257 |                 functionDecl->set_functionType( maybeMutate( functionDecl->get_functionType(), *this ) );
 | 
|---|
| 258 |                 mutateAll( functionDecl->get_oldDecls(), *this );
 | 
|---|
| 259 |                 ++functionNesting;
 | 
|---|
| 260 |                 functionDecl->set_statements( maybeMutate( functionDecl->get_statements(), *this ) );
 | 
|---|
| 261 |                 --functionNesting;
 | 
|---|
| 262 |                 return functionDecl;
 | 
|---|
| 263 |         }
 | 
|---|
| 264 | 
 | 
|---|
| 265 |         /// Get a list of type declarations that will affect a layout function
 | 
|---|
| 266 |         std::list< TypeDecl* > takeOtypeOnly( std::list< TypeDecl* > &decls ) {
 | 
|---|
| 267 |                 std::list< TypeDecl * > otypeDecls;
 | 
|---|
| 268 | 
 | 
|---|
| 269 |                 for ( std::list< TypeDecl* >::const_iterator decl = decls.begin(); decl != decls.end(); ++decl ) {
 | 
|---|
| 270 |                         if ( (*decl)->get_kind() == TypeDecl::Any ) {
 | 
|---|
| 271 |                                 otypeDecls.push_back( *decl );
 | 
|---|
| 272 |                         }
 | 
|---|
| 273 |                 }
 | 
|---|
| 274 | 
 | 
|---|
| 275 |                 return otypeDecls;
 | 
|---|
| 276 |         }
 | 
|---|
| 277 | 
 | 
|---|
| 278 |         /// Adds parameters for otype layout to a function type
 | 
|---|
| 279 |         void addOtypeParams( FunctionType *layoutFnType, std::list< TypeDecl* > &otypeParams ) {
 | 
|---|
| 280 |                 BasicType sizeAlignType( Type::Qualifiers(), BasicType::LongUnsignedInt );
 | 
|---|
| 281 | 
 | 
|---|
| 282 |                 for ( std::list< TypeDecl* >::const_iterator param = otypeParams.begin(); param != otypeParams.end(); ++param ) {
 | 
|---|
| 283 |                         TypeInstType paramType( Type::Qualifiers(), (*param)->get_name(), *param );
 | 
|---|
| 284 |                         std::string paramName = mangleType( ¶mType );
 | 
|---|
| 285 |                         layoutFnType->get_parameters().push_back( new ObjectDecl( sizeofName( paramName ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignType.clone(), 0 ) );
 | 
|---|
| 286 |                         layoutFnType->get_parameters().push_back( new ObjectDecl( alignofName( paramName ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignType.clone(), 0 ) );
 | 
|---|
| 287 |                 }
 | 
|---|
| 288 |         }
 | 
|---|
| 289 | 
 | 
|---|
| 290 |         /// Builds a layout function declaration
 | 
|---|
| 291 |         FunctionDecl *buildLayoutFunctionDecl( AggregateDecl *typeDecl, unsigned int functionNesting, FunctionType *layoutFnType ) {
 | 
|---|
| 292 |                 // Routines at global scope marked "static" to prevent multiple definitions is separate translation units
 | 
|---|
| 293 |                 // because each unit generates copies of the default routines for each aggregate.
 | 
|---|
| 294 |                 FunctionDecl *layoutDecl = new FunctionDecl(
 | 
|---|
| 295 |                         layoutofName( typeDecl ), functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, layoutFnType, new CompoundStmt( noLabels ), true, false );
 | 
|---|
| 296 |                 layoutDecl->fixUniqueId();
 | 
|---|
| 297 |                 return layoutDecl;
 | 
|---|
| 298 |         }
 | 
|---|
| 299 | 
 | 
|---|
| 300 |         /// Makes a unary operation
 | 
|---|
| 301 |         Expression *makeOp( const std::string &name, Expression *arg ) {
 | 
|---|
| 302 |                 UntypedExpr *expr = new UntypedExpr( new NameExpr( name ) );
 | 
|---|
| 303 |                 expr->get_args().push_back( arg );
 | 
|---|
| 304 |                 return expr;
 | 
|---|
| 305 |         }
 | 
|---|
| 306 | 
 | 
|---|
| 307 |         /// Makes a binary operation
 | 
|---|
| 308 |         Expression *makeOp( const std::string &name, Expression *lhs, Expression *rhs ) {
 | 
|---|
| 309 |                 UntypedExpr *expr = new UntypedExpr( new NameExpr( name ) );
 | 
|---|
| 310 |                 expr->get_args().push_back( lhs );
 | 
|---|
| 311 |                 expr->get_args().push_back( rhs );
 | 
|---|
| 312 |                 return expr;
 | 
|---|
| 313 |         }
 | 
|---|
| 314 | 
 | 
|---|
| 315 |         /// Returns the dereference of a local pointer variable
 | 
|---|
| 316 |         Expression *derefVar( ObjectDecl *var ) {
 | 
|---|
| 317 |                 return makeOp( "*?", new VariableExpr( var ) );
 | 
|---|
| 318 |         }
 | 
|---|
| 319 | 
 | 
|---|
| 320 |         /// makes an if-statement with a single-expression if-block and no then block
 | 
|---|
| 321 |         Statement *makeCond( Expression *cond, Expression *ifPart ) {
 | 
|---|
| 322 |                 return new IfStmt( noLabels, cond, new ExprStmt( noLabels, ifPart ), 0 );
 | 
|---|
| 323 |         }
 | 
|---|
| 324 | 
 | 
|---|
| 325 |         /// makes a statement that assigns rhs to lhs if lhs < rhs
 | 
|---|
| 326 |         Statement *makeAssignMax( Expression *lhs, Expression *rhs ) {
 | 
|---|
| 327 |                 return makeCond( makeOp( "?<?", lhs, rhs ), makeOp( "?=?", lhs->clone(), rhs->clone() ) );
 | 
|---|
| 328 |         }
 | 
|---|
| 329 | 
 | 
|---|
| 330 |         /// makes a statement that aligns lhs to rhs (rhs should be an integer power of two)
 | 
|---|
| 331 |         Statement *makeAlignTo( Expression *lhs, Expression *rhs ) {
 | 
|---|
| 332 |                 // check that the lhs is zeroed out to the level of rhs
 | 
|---|
| 333 |                 Expression *ifCond = makeOp( "?&?", lhs, makeOp( "?-?", rhs, new ConstantExpr( Constant( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), "1" ) ) ) );
 | 
|---|
| 334 |                 // if not aligned, increment to alignment
 | 
|---|
| 335 |                 Expression *ifExpr = makeOp( "?+=?", lhs->clone(), makeOp( "?-?", rhs->clone(), ifCond->clone() ) );
 | 
|---|
| 336 |                 return makeCond( ifCond, ifExpr );
 | 
|---|
| 337 |         }
 | 
|---|
| 338 | 
 | 
|---|
| 339 |         /// adds an expression to a compound statement
 | 
|---|
| 340 |         void addExpr( CompoundStmt *stmts, Expression *expr ) {
 | 
|---|
| 341 |                 stmts->get_kids().push_back( new ExprStmt( noLabels, expr ) );
 | 
|---|
| 342 |         }
 | 
|---|
| 343 | 
 | 
|---|
| 344 |         /// adds a statement to a compound statement
 | 
|---|
| 345 |         void addStmt( CompoundStmt *stmts, Statement *stmt ) {
 | 
|---|
| 346 |                 stmts->get_kids().push_back( stmt );
 | 
|---|
| 347 |         }
 | 
|---|
| 348 | 
 | 
|---|
| 349 |         Declaration *LayoutFunctionBuilder::mutate( StructDecl *structDecl ) {
 | 
|---|
| 350 |                 // do not generate layout function for "empty" tag structs
 | 
|---|
| 351 |                 if ( structDecl->get_members().empty() ) return structDecl;
 | 
|---|
| 352 | 
 | 
|---|
| 353 |                 // get parameters that can change layout, exiting early if none
 | 
|---|
| 354 |                 std::list< TypeDecl* > otypeParams = takeOtypeOnly( structDecl->get_parameters() );
 | 
|---|
| 355 |                 if ( otypeParams.empty() ) return structDecl;
 | 
|---|
| 356 | 
 | 
|---|
| 357 |                 // build layout function signature
 | 
|---|
| 358 |                 FunctionType *layoutFnType = new FunctionType( Type::Qualifiers(), false );
 | 
|---|
| 359 |                 BasicType *sizeAlignType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt );
 | 
|---|
| 360 |                 PointerType *sizeAlignOutType = new PointerType( Type::Qualifiers(), sizeAlignType );
 | 
|---|
| 361 | 
 | 
|---|
| 362 |                 ObjectDecl *sizeParam = new ObjectDecl( sizeofName( structDecl->get_name() ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType, 0 );
 | 
|---|
| 363 |                 layoutFnType->get_parameters().push_back( sizeParam );
 | 
|---|
| 364 |                 ObjectDecl *alignParam = new ObjectDecl( alignofName( structDecl->get_name() ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType->clone(), 0 );
 | 
|---|
| 365 |                 layoutFnType->get_parameters().push_back( alignParam );
 | 
|---|
| 366 |                 ObjectDecl *offsetParam = new ObjectDecl( offsetofName( structDecl->get_name() ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType->clone(), 0 );
 | 
|---|
| 367 |                 layoutFnType->get_parameters().push_back( offsetParam );
 | 
|---|
| 368 |                 addOtypeParams( layoutFnType, otypeParams );
 | 
|---|
| 369 | 
 | 
|---|
| 370 |                 // build function decl
 | 
|---|
| 371 |                 FunctionDecl *layoutDecl = buildLayoutFunctionDecl( structDecl, functionNesting, layoutFnType );
 | 
|---|
| 372 | 
 | 
|---|
| 373 |                 // calculate struct layout in function body
 | 
|---|
| 374 | 
 | 
|---|
| 375 |                 // initialize size and alignment to 0 and 1 (will have at least one member to re-edit size
 | 
|---|
| 376 |                 addExpr( layoutDecl->get_statements(), makeOp( "?=?", derefVar( sizeParam ), new ConstantExpr( Constant( sizeAlignType->clone(), "0" ) ) ) );
 | 
|---|
| 377 |                 addExpr( layoutDecl->get_statements(), makeOp( "?=?", derefVar( alignParam ), new ConstantExpr( Constant( sizeAlignType->clone(), "1" ) ) ) );
 | 
|---|
| 378 |                 unsigned long n_members = 0;
 | 
|---|
| 379 |                 bool firstMember = true;
 | 
|---|
| 380 |                 for ( std::list< Declaration* >::const_iterator member = structDecl->get_members().begin(); member != structDecl->get_members().end(); ++member ) {
 | 
|---|
| 381 |                         DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( *member );
 | 
|---|
| 382 |                         assert( dwt );
 | 
|---|
| 383 |                         Type *memberType = dwt->get_type();
 | 
|---|
| 384 | 
 | 
|---|
| 385 |                         if ( firstMember ) {
 | 
|---|
| 386 |                                 firstMember = false;
 | 
|---|
| 387 |                         } else {
 | 
|---|
| 388 |                                 // make sure all members after the first (automatically aligned at 0) are properly padded for alignment
 | 
|---|
| 389 |                                 addStmt( layoutDecl->get_statements(), makeAlignTo( derefVar( sizeParam ), new AlignofExpr( memberType->clone() ) ) );
 | 
|---|
| 390 |                         }
 | 
|---|
| 391 | 
 | 
|---|
| 392 |                         // place current size in the current offset index
 | 
|---|
| 393 |                         addExpr( layoutDecl->get_statements(), makeOp( "?=?", makeOp( "?[?]", new VariableExpr( offsetParam ), new ConstantExpr( Constant::from_ulong( n_members ) ) ),
 | 
|---|
| 394 |                                                                               derefVar( sizeParam ) ) );
 | 
|---|
| 395 |                         ++n_members;
 | 
|---|
| 396 | 
 | 
|---|
| 397 |                         // add member size to current size
 | 
|---|
| 398 |                         addExpr( layoutDecl->get_statements(), makeOp( "?+=?", derefVar( sizeParam ), new SizeofExpr( memberType->clone() ) ) );
 | 
|---|
| 399 | 
 | 
|---|
| 400 |                         // take max of member alignment and global alignment
 | 
|---|
| 401 |                         addStmt( layoutDecl->get_statements(), makeAssignMax( derefVar( alignParam ), new AlignofExpr( memberType->clone() ) ) );
 | 
|---|
| 402 |                 }
 | 
|---|
| 403 |                 // make sure the type is end-padded to a multiple of its alignment
 | 
|---|
| 404 |                 addStmt( layoutDecl->get_statements(), makeAlignTo( derefVar( sizeParam ), derefVar( alignParam ) ) );
 | 
|---|
| 405 | 
 | 
|---|
| 406 |                 addDeclarationAfter( layoutDecl );
 | 
|---|
| 407 |                 return structDecl;
 | 
|---|
| 408 |         }
 | 
|---|
| 409 | 
 | 
|---|
| 410 |         Declaration *LayoutFunctionBuilder::mutate( UnionDecl *unionDecl ) {
 | 
|---|
| 411 |                 // do not generate layout function for "empty" tag unions
 | 
|---|
| 412 |                 if ( unionDecl->get_members().empty() ) return unionDecl;
 | 
|---|
| 413 | 
 | 
|---|
| 414 |                 // get parameters that can change layout, exiting early if none
 | 
|---|
| 415 |                 std::list< TypeDecl* > otypeParams = takeOtypeOnly( unionDecl->get_parameters() );
 | 
|---|
| 416 |                 if ( otypeParams.empty() ) return unionDecl;
 | 
|---|
| 417 | 
 | 
|---|
| 418 |                 // build layout function signature
 | 
|---|
| 419 |                 FunctionType *layoutFnType = new FunctionType( Type::Qualifiers(), false );
 | 
|---|
| 420 |                 BasicType *sizeAlignType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt );
 | 
|---|
| 421 |                 PointerType *sizeAlignOutType = new PointerType( Type::Qualifiers(), sizeAlignType );
 | 
|---|
| 422 | 
 | 
|---|
| 423 |                 ObjectDecl *sizeParam = new ObjectDecl( sizeofName( unionDecl->get_name() ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType, 0 );
 | 
|---|
| 424 |                 layoutFnType->get_parameters().push_back( sizeParam );
 | 
|---|
| 425 |                 ObjectDecl *alignParam = new ObjectDecl( alignofName( unionDecl->get_name() ), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, sizeAlignOutType->clone(), 0 );
 | 
|---|
| 426 |                 layoutFnType->get_parameters().push_back( alignParam );
 | 
|---|
| 427 |                 addOtypeParams( layoutFnType, otypeParams );
 | 
|---|
| 428 | 
 | 
|---|
| 429 |                 // build function decl
 | 
|---|
| 430 |                 FunctionDecl *layoutDecl = buildLayoutFunctionDecl( unionDecl, functionNesting, layoutFnType );
 | 
|---|
| 431 | 
 | 
|---|
| 432 |                 // calculate union layout in function body
 | 
|---|
| 433 |                 addExpr( layoutDecl->get_statements(), makeOp( "?=?", derefVar( sizeParam ), new ConstantExpr( Constant( sizeAlignType->clone(), "1" ) ) ) );
 | 
|---|
| 434 |                 addExpr( layoutDecl->get_statements(), makeOp( "?=?", derefVar( alignParam ), new ConstantExpr( Constant( sizeAlignType->clone(), "1" ) ) ) );
 | 
|---|
| 435 |                 for ( std::list< Declaration* >::const_iterator member = unionDecl->get_members().begin(); member != unionDecl->get_members().end(); ++member ) {
 | 
|---|
| 436 |                         DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( *member );
 | 
|---|
| 437 |                         assert( dwt );
 | 
|---|
| 438 |                         Type *memberType = dwt->get_type();
 | 
|---|
| 439 | 
 | 
|---|
| 440 |                         // take max member size and global size
 | 
|---|
| 441 |                         addStmt( layoutDecl->get_statements(), makeAssignMax( derefVar( sizeParam ), new SizeofExpr( memberType->clone() ) ) );
 | 
|---|
| 442 | 
 | 
|---|
| 443 |                         // take max of member alignment and global alignment
 | 
|---|
| 444 |                         addStmt( layoutDecl->get_statements(), makeAssignMax( derefVar( alignParam ), new AlignofExpr( memberType->clone() ) ) );
 | 
|---|
| 445 |                 }
 | 
|---|
| 446 |                 // make sure the type is end-padded to a multiple of its alignment
 | 
|---|
| 447 |                 addStmt( layoutDecl->get_statements(), makeAlignTo( derefVar( sizeParam ), derefVar( alignParam ) ) );
 | 
|---|
| 448 | 
 | 
|---|
| 449 |                 addDeclarationAfter( layoutDecl );
 | 
|---|
| 450 |                 return unionDecl;
 | 
|---|
| 451 |         }
 | 
|---|
| 452 | 
 | 
|---|
| 453 |         ////////////////////////////////////////// Pass1 ////////////////////////////////////////////////////
 | 
|---|
| 454 | 
 | 
|---|
| 455 |         namespace {
 | 
|---|
| 456 |                 std::string makePolyMonoSuffix( FunctionType * function, const TyVarMap &tyVars ) {
 | 
|---|
| 457 |                         std::stringstream name;
 | 
|---|
| 458 | 
 | 
|---|
| 459 |                         // NOTE: this function previously used isPolyObj, which failed to produce
 | 
|---|
| 460 |                         // the correct thing in some situations. It's not clear to me why this wasn't working.
 | 
|---|
| 461 | 
 | 
|---|
| 462 |                         // if the return type or a parameter type involved polymorphic types, then the adapter will need
 | 
|---|
| 463 |                         // to take those polymorphic types as pointers. Therefore, there can be two different functions
 | 
|---|
| 464 |                         // with the same mangled name, so we need to further mangle the names.
 | 
|---|
| 465 |                         for ( std::list< DeclarationWithType *>::iterator retval = function->get_returnVals().begin(); retval != function->get_returnVals().end(); ++retval ) {
 | 
|---|
| 466 |                                 if ( isPolyType( (*retval)->get_type(), tyVars ) ) {
 | 
|---|
| 467 |                                         name << "P";
 | 
|---|
| 468 |                                 } else {
 | 
|---|
| 469 |                                         name << "M";
 | 
|---|
| 470 |                                 }
 | 
|---|
| 471 |                         }
 | 
|---|
| 472 |                         name << "_";
 | 
|---|
| 473 |                         std::list< DeclarationWithType *> ¶mList = function->get_parameters();
 | 
|---|
| 474 |                         for ( std::list< DeclarationWithType *>::iterator arg = paramList.begin(); arg != paramList.end(); ++arg ) {
 | 
|---|
| 475 |                                 if ( isPolyType( (*arg)->get_type(), tyVars ) ) {
 | 
|---|
| 476 |                                         name << "P";
 | 
|---|
| 477 |                                 } else {
 | 
|---|
| 478 |                                         name << "M";
 | 
|---|
| 479 |                                 }
 | 
|---|
| 480 |                         } // for
 | 
|---|
| 481 |                         return name.str();
 | 
|---|
| 482 |                 }
 | 
|---|
| 483 | 
 | 
|---|
| 484 |                 std::string mangleAdapterName( FunctionType * function, const TyVarMap &tyVars ) {
 | 
|---|
| 485 |                         return SymTab::Mangler::mangle( function ) + makePolyMonoSuffix( function, tyVars );
 | 
|---|
| 486 |                 }
 | 
|---|
| 487 | 
 | 
|---|
| 488 |                 std::string makeAdapterName( const std::string &mangleName ) {
 | 
|---|
| 489 |                         return "_adapter" + mangleName;
 | 
|---|
| 490 |                 }
 | 
|---|
| 491 | 
 | 
|---|
| 492 |                 Pass1::Pass1() : useRetval( false ), tempNamer( "_temp" ) {}
 | 
|---|
| 493 | 
 | 
|---|
| 494 |                 /// Returns T if the given declaration is a function with parameter (T*) for some TypeInstType T, NULL otherwise
 | 
|---|
| 495 |                 TypeInstType *isTypeInstPtrFn( DeclarationWithType *decl ) {
 | 
|---|
| 496 |                         if ( FunctionType *funType = getFunctionType( decl->get_type() ) ) {
 | 
|---|
| 497 |                                 if ( funType->get_parameters().size() == 1 ) {
 | 
|---|
| 498 |                                         if ( PointerType *pointer = dynamic_cast< PointerType *>( funType->get_parameters().front()->get_type() ) ) {
 | 
|---|
| 499 |                                                 if ( TypeInstType *refType = dynamic_cast< TypeInstType *>( pointer->get_base() ) ) {
 | 
|---|
| 500 |                                                         return refType;
 | 
|---|
| 501 |                                                 } // if
 | 
|---|
| 502 |                                         } // if
 | 
|---|
| 503 |                                 } // if
 | 
|---|
| 504 |                         } // if
 | 
|---|
| 505 |                         return 0;
 | 
|---|
| 506 |                 }
 | 
|---|
| 507 | 
 | 
|---|
| 508 |                 /// Returns T if the given declaration is a function with parameters (T*, T) for some TypeInstType T, NULL otherwise
 | 
|---|
| 509 |                 TypeInstType *isTypeInstPtrValFn( DeclarationWithType *decl ) {
 | 
|---|
| 510 |                         if ( FunctionType *funType = getFunctionType( decl->get_type() ) ) {
 | 
|---|
| 511 |                                 if ( funType->get_parameters().size() == 2 ) {
 | 
|---|
| 512 |                                         if ( PointerType *pointer = dynamic_cast< PointerType *>( funType->get_parameters().front()->get_type() ) ) {
 | 
|---|
| 513 |                                                 if ( TypeInstType *refType = dynamic_cast< TypeInstType *>( pointer->get_base() ) ) {
 | 
|---|
| 514 |                                                         if ( TypeInstType *refType2 = dynamic_cast< TypeInstType *>( funType->get_parameters().back()->get_type() ) ) {
 | 
|---|
| 515 |                                                                 if ( refType->get_name() == refType2->get_name() ) {
 | 
|---|
| 516 |                                                                         return refType;
 | 
|---|
| 517 |                                                                 } // if
 | 
|---|
| 518 |                                                         } // if
 | 
|---|
| 519 |                                                 } // if
 | 
|---|
| 520 |                                         } // if
 | 
|---|
| 521 |                                 } // if
 | 
|---|
| 522 |                         } // if
 | 
|---|
| 523 |                         return 0;
 | 
|---|
| 524 |                 }
 | 
|---|
| 525 | 
 | 
|---|
| 526 |                 /// Returns T if the given declaration is (*?=?)(T *, T) for some TypeInstType T (return not checked, but maybe should be), NULL otherwise
 | 
|---|
| 527 |                 TypeInstType *isTypeInstAssignment( DeclarationWithType *decl ) {
 | 
|---|
| 528 |                         return decl->get_name() == "?=?" ? isTypeInstPtrValFn( decl ) : 0;
 | 
|---|
| 529 |                 }
 | 
|---|
| 530 | 
 | 
|---|
| 531 |                 /// Returns T if the given declaration is (*?{})(T *) for some TypeInstType T (return not checked, but maybe should be), NULL otherwise
 | 
|---|
| 532 |                 TypeInstType *isTypeInstCtor( DeclarationWithType *decl ) {
 | 
|---|
| 533 |                         return decl->get_name() == "?{}" ? isTypeInstPtrFn( decl ) : 0;
 | 
|---|
| 534 |                 }
 | 
|---|
| 535 | 
 | 
|---|
| 536 |                 /// Returns T if the given declaration is (*?{})(T *, T) for some TypeInstType T (return not checked, but maybe should be), NULL otherwise
 | 
|---|
| 537 |                 TypeInstType *isTypeInstCopy( DeclarationWithType *decl ) {
 | 
|---|
| 538 |                         return decl->get_name() == "?{}" ? isTypeInstPtrValFn( decl ) : 0;
 | 
|---|
| 539 |                 }
 | 
|---|
| 540 | 
 | 
|---|
| 541 |                 /// Returns T if the given declaration is (*^?{})(T *) for some TypeInstType T (return not checked, but maybe should be), NULL otherwise
 | 
|---|
| 542 |                 TypeInstType *isTypeInstDtor( DeclarationWithType *decl ) {
 | 
|---|
| 543 |                         return decl->get_name() == "^?{}" ? isTypeInstPtrFn( decl ) : 0;
 | 
|---|
| 544 |                 }
 | 
|---|
| 545 | 
 | 
|---|
| 546 |                 /// Returns T if the given declaration is a function with parameters (T*, T) for some type T, where neither parameter is cv-qualified,
 | 
|---|
| 547 |                 /// NULL otherwise
 | 
|---|
| 548 |                 Type *isNoCvPtrFn( DeclarationWithType *decl ) {
 | 
|---|
| 549 |                         if ( FunctionType *funType = getFunctionType( decl->get_type() ) ) {
 | 
|---|
| 550 |                                 if ( funType->get_parameters().size() == 1 ) {
 | 
|---|
| 551 |                                         Type::Qualifiers defaultQualifiers;
 | 
|---|
| 552 |                                         Type *paramType = funType->get_parameters().front()->get_type();
 | 
|---|
| 553 |                                         if ( paramType->get_qualifiers() != defaultQualifiers ) return 0;
 | 
|---|
| 554 | 
 | 
|---|
| 555 |                                         if ( PointerType *pointerType = dynamic_cast< PointerType* >( paramType ) ) {
 | 
|---|
| 556 |                                                 Type *baseType = pointerType->get_base();
 | 
|---|
| 557 |                                                 if ( baseType->get_qualifiers() == defaultQualifiers ) {
 | 
|---|
| 558 |                                                         return baseType;
 | 
|---|
| 559 |                                                 } // if
 | 
|---|
| 560 |                                         } // if
 | 
|---|
| 561 |                                 } // if
 | 
|---|
| 562 |                         } // if
 | 
|---|
| 563 |                         return 0;
 | 
|---|
| 564 |                 }
 | 
|---|
| 565 | 
 | 
|---|
| 566 |                 /// Returns T if the given declaration is a function with parameters (T*, T) for some type T, where neither parameter is cv-qualified,
 | 
|---|
| 567 |                 /// NULL otherwise
 | 
|---|
| 568 |                 Type *isNoCvPtrValFn( DeclarationWithType *decl ) {
 | 
|---|
| 569 |                         if ( FunctionType *funType = getFunctionType( decl->get_type() ) ) {
 | 
|---|
| 570 |                                 if ( funType->get_parameters().size() == 2 ) {
 | 
|---|
| 571 |                                         Type::Qualifiers defaultQualifiers;
 | 
|---|
| 572 |                                         Type *paramType1 = funType->get_parameters().front()->get_type();
 | 
|---|
| 573 |                                         if ( paramType1->get_qualifiers() != defaultQualifiers ) return 0;
 | 
|---|
| 574 |                                         Type *paramType2 = funType->get_parameters().back()->get_type();
 | 
|---|
| 575 |                                         if ( paramType2->get_qualifiers() != defaultQualifiers ) return 0;
 | 
|---|
| 576 | 
 | 
|---|
| 577 |                                         if ( PointerType *pointerType = dynamic_cast< PointerType* >( paramType1 ) ) {
 | 
|---|
| 578 |                                                 Type *baseType1 = pointerType->get_base();
 | 
|---|
| 579 |                                                 if ( baseType1->get_qualifiers() != defaultQualifiers ) return 0;
 | 
|---|
| 580 |                                                 SymTab::Indexer dummy;
 | 
|---|
| 581 |                                                 if ( ResolvExpr::typesCompatible( baseType1, paramType2, dummy ) ) {
 | 
|---|
| 582 |                                                         return baseType1;
 | 
|---|
| 583 |                                                 } // if
 | 
|---|
| 584 |                                         } // if
 | 
|---|
| 585 |                                 } // if
 | 
|---|
| 586 |                         } // if
 | 
|---|
| 587 |                         return 0;
 | 
|---|
| 588 |                 }
 | 
|---|
| 589 | 
 | 
|---|
| 590 |                 /// returns T if the given declaration is: (*?=?)(T *, T) for some type T (return not checked, but maybe should be), NULL otherwise
 | 
|---|
| 591 |                 /// Only picks assignments where neither parameter is cv-qualified
 | 
|---|
| 592 |                 Type *isAssignment( DeclarationWithType *decl ) {
 | 
|---|
| 593 |                         return decl->get_name() == "?=?" ? isNoCvPtrValFn( decl ) : 0;
 | 
|---|
| 594 |                 }
 | 
|---|
| 595 | 
 | 
|---|
| 596 |                 /// returns T if the given declaration is: (*?{})(T *) for some type T, NULL otherwise
 | 
|---|
| 597 |                 /// Only picks ctors where the parameter is not cv-qualified
 | 
|---|
| 598 |                 Type *isCtor( DeclarationWithType *decl ) {
 | 
|---|
| 599 |                         return decl->get_name() == "?{}" ? isNoCvPtrFn( decl ) : 0;
 | 
|---|
| 600 |                 }
 | 
|---|
| 601 | 
 | 
|---|
| 602 |                 /// returns T if the given declaration is: (*?{})(T *, T) for some type T (return not checked, but maybe should be), NULL otherwise
 | 
|---|
| 603 |                 /// Only picks copy constructors where neither parameter is cv-qualified
 | 
|---|
| 604 |                 Type *isCopy( DeclarationWithType *decl ) {
 | 
|---|
| 605 |                         return decl->get_name() == "?{}" ? isNoCvPtrValFn( decl ) : 0;
 | 
|---|
| 606 |                 }
 | 
|---|
| 607 | 
 | 
|---|
| 608 |                 /// returns T if the given declaration is: (*?{})(T *) for some type T, NULL otherwise
 | 
|---|
| 609 |                 /// Only picks ctors where the parameter is not cv-qualified
 | 
|---|
| 610 |                 Type *isDtor( DeclarationWithType *decl ) {
 | 
|---|
| 611 |                         return decl->get_name() == "^?{}" ? isNoCvPtrFn( decl ) : 0;
 | 
|---|
| 612 |                 }
 | 
|---|
| 613 | 
 | 
|---|
| 614 |                 void Pass1::findTypeOps( const std::list< TypeDecl *> &forall ) {
 | 
|---|
| 615 |                         // what if a nested function uses an assignment operator?
 | 
|---|
| 616 |                         // assignOps.clear();
 | 
|---|
| 617 |                         for ( std::list< TypeDecl *>::const_iterator i = forall.begin(); i != forall.end(); ++i ) {
 | 
|---|
| 618 |                                 for ( std::list< DeclarationWithType *>::const_iterator assert = (*i)->get_assertions().begin(); assert != (*i)->get_assertions().end(); ++assert ) {
 | 
|---|
| 619 |                                         std::string typeName;
 | 
|---|
| 620 |                                         if ( TypeInstType *typeInst = isTypeInstAssignment( *assert ) ) {
 | 
|---|
| 621 |                                                 assignOps[ typeInst->get_name() ] = *assert;
 | 
|---|
| 622 |                                         } else if ( TypeInstType *typeInst = isTypeInstCtor( *assert ) ) {
 | 
|---|
| 623 |                                                 ctorOps[ typeInst->get_name() ] = *assert;
 | 
|---|
| 624 |                                         } else if ( TypeInstType *typeInst = isTypeInstCopy( *assert ) ) {
 | 
|---|
| 625 |                                                 copyOps[ typeInst->get_name() ] = *assert;
 | 
|---|
| 626 |                                         } else if ( TypeInstType *typeInst = isTypeInstDtor( *assert ) ) {
 | 
|---|
| 627 |                                                 dtorOps[ typeInst->get_name() ] = *assert;
 | 
|---|
| 628 |                                         } // if
 | 
|---|
| 629 |                                 } // for
 | 
|---|
| 630 |                         } // for
 | 
|---|
| 631 |                 }
 | 
|---|
| 632 | 
 | 
|---|
| 633 |                 DeclarationWithType *Pass1::mutate( FunctionDecl *functionDecl ) {
 | 
|---|
| 634 |                         // if this is a assignment function, put it in the map for this scope
 | 
|---|
| 635 |                         if ( Type *paramType = isAssignment( functionDecl ) ) {
 | 
|---|
| 636 |                                 if ( ! dynamic_cast< TypeInstType* >( paramType ) ) {
 | 
|---|
| 637 |                                         scopedAssignOps.insert( paramType, functionDecl );
 | 
|---|
| 638 |                                 }
 | 
|---|
| 639 |                         } else if ( Type *paramType = isCtor( functionDecl ) ) {
 | 
|---|
| 640 |                                 if ( ! dynamic_cast< TypeInstType* >( paramType ) ) {
 | 
|---|
| 641 |                                         scopedCtorOps.insert( paramType, functionDecl );
 | 
|---|
| 642 |                                 }
 | 
|---|
| 643 |                         } else if ( Type *paramType = isCopy( functionDecl ) ) {
 | 
|---|
| 644 |                                 if ( ! dynamic_cast< TypeInstType* >( paramType ) ) {
 | 
|---|
| 645 |                                         scopedCopyOps.insert( paramType, functionDecl );
 | 
|---|
| 646 |                                 }
 | 
|---|
| 647 |                         } else if ( Type *paramType = isDtor( functionDecl ) ) {
 | 
|---|
| 648 |                                 if ( ! dynamic_cast< TypeInstType* >( paramType ) ) {
 | 
|---|
| 649 |                                         scopedDtorOps.insert( paramType, functionDecl );
 | 
|---|
| 650 |                                 }
 | 
|---|
| 651 |                         }
 | 
|---|
| 652 | 
 | 
|---|
| 653 |                         if ( functionDecl->get_statements() ) {         // empty routine body ?
 | 
|---|
| 654 |                                 doBeginScope();
 | 
|---|
| 655 |                                 scopeTyVars.beginScope();
 | 
|---|
| 656 |                                 assignOps.beginScope();
 | 
|---|
| 657 |                                 ctorOps.beginScope();
 | 
|---|
| 658 |                                 copyOps.beginScope();
 | 
|---|
| 659 |                                 dtorOps.beginScope();
 | 
|---|
| 660 | 
 | 
|---|
| 661 |                                 DeclarationWithType *oldRetval = retval;
 | 
|---|
| 662 |                                 bool oldUseRetval = useRetval;
 | 
|---|
| 663 | 
 | 
|---|
| 664 |                                 // process polymorphic return value
 | 
|---|
| 665 |                                 retval = 0;
 | 
|---|
| 666 |                                 if ( isDynRet( functionDecl->get_functionType() ) && functionDecl->get_linkage() == LinkageSpec::Cforall ) {
 | 
|---|
| 667 |                                         retval = functionDecl->get_functionType()->get_returnVals().front();
 | 
|---|
| 668 | 
 | 
|---|
| 669 |                                         // give names to unnamed return values
 | 
|---|
| 670 |                                         if ( retval->get_name() == "" ) {
 | 
|---|
| 671 |                                                 retval->set_name( "_retparm" );
 | 
|---|
| 672 |                                                 retval->set_linkage( LinkageSpec::C );
 | 
|---|
| 673 |                                         } // if
 | 
|---|
| 674 |                                 } // if
 | 
|---|
| 675 | 
 | 
|---|
| 676 |                                 FunctionType *functionType = functionDecl->get_functionType();
 | 
|---|
| 677 |                                 makeTyVarMap( functionDecl->get_functionType(), scopeTyVars );
 | 
|---|
| 678 |                                 findTypeOps( functionDecl->get_functionType()->get_forall() );
 | 
|---|
| 679 | 
 | 
|---|
| 680 |                                 std::list< DeclarationWithType *> ¶mList = functionType->get_parameters();
 | 
|---|
| 681 |                                 std::list< FunctionType *> functions;
 | 
|---|
| 682 |                                 for ( std::list< TypeDecl *>::iterator tyVar = functionType->get_forall().begin(); tyVar != functionType->get_forall().end(); ++tyVar ) {
 | 
|---|
| 683 |                                         for ( std::list< DeclarationWithType *>::iterator assert = (*tyVar)->get_assertions().begin(); assert != (*tyVar)->get_assertions().end(); ++assert ) {
 | 
|---|
| 684 |                                                 findFunction( (*assert)->get_type(), functions, scopeTyVars, needsAdapter );
 | 
|---|
| 685 |                                         } // for
 | 
|---|
| 686 |                                 } // for
 | 
|---|
| 687 |                                 for ( std::list< DeclarationWithType *>::iterator arg = paramList.begin(); arg != paramList.end(); ++arg ) {
 | 
|---|
| 688 |                                         findFunction( (*arg)->get_type(), functions, scopeTyVars, needsAdapter );
 | 
|---|
| 689 |                                 } // for
 | 
|---|
| 690 | 
 | 
|---|
| 691 |                                 for ( std::list< FunctionType *>::iterator funType = functions.begin(); funType != functions.end(); ++funType ) {
 | 
|---|
| 692 |                                         std::string mangleName = mangleAdapterName( *funType, scopeTyVars );
 | 
|---|
| 693 |                                         if ( adapters.find( mangleName ) == adapters.end() ) {
 | 
|---|
| 694 |                                                 std::string adapterName = makeAdapterName( mangleName );
 | 
|---|
| 695 |                                                 adapters.insert( std::pair< std::string, DeclarationWithType *>( mangleName, new ObjectDecl( adapterName, DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new PointerType( Type::Qualifiers(), makeAdapterType( *funType, scopeTyVars ) ), 0 ) ) );
 | 
|---|
| 696 |                                         } // if
 | 
|---|
| 697 |                                 } // for
 | 
|---|
| 698 | 
 | 
|---|
| 699 |                                 functionDecl->set_statements( functionDecl->get_statements()->acceptMutator( *this ) );
 | 
|---|
| 700 | 
 | 
|---|
| 701 |                                 scopeTyVars.endScope();
 | 
|---|
| 702 |                                 assignOps.endScope();
 | 
|---|
| 703 |                                 ctorOps.endScope();
 | 
|---|
| 704 |                                 copyOps.endScope();
 | 
|---|
| 705 |                                 dtorOps.endScope();
 | 
|---|
| 706 |                                 retval = oldRetval;
 | 
|---|
| 707 |                                 useRetval = oldUseRetval;
 | 
|---|
| 708 |                                 doEndScope();
 | 
|---|
| 709 |                         } // if
 | 
|---|
| 710 |                         return functionDecl;
 | 
|---|
| 711 |                 }
 | 
|---|
| 712 | 
 | 
|---|
| 713 |                 TypeDecl *Pass1::mutate( TypeDecl *typeDecl ) {
 | 
|---|
| 714 |                         scopeTyVars[ typeDecl->get_name() ] = typeDecl->get_kind();
 | 
|---|
| 715 |                         return Mutator::mutate( typeDecl );
 | 
|---|
| 716 |                 }
 | 
|---|
| 717 | 
 | 
|---|
| 718 |                 Expression *Pass1::mutate( CommaExpr *commaExpr ) {
 | 
|---|
| 719 |                         bool oldUseRetval = useRetval;
 | 
|---|
| 720 |                         useRetval = false;
 | 
|---|
| 721 |                         commaExpr->set_arg1( maybeMutate( commaExpr->get_arg1(), *this ) );
 | 
|---|
| 722 |                         useRetval = oldUseRetval;
 | 
|---|
| 723 |                         commaExpr->set_arg2( maybeMutate( commaExpr->get_arg2(), *this ) );
 | 
|---|
| 724 |                         return commaExpr;
 | 
|---|
| 725 |                 }
 | 
|---|
| 726 | 
 | 
|---|
| 727 |                 Expression *Pass1::mutate( ConditionalExpr *condExpr ) {
 | 
|---|
| 728 |                         bool oldUseRetval = useRetval;
 | 
|---|
| 729 |                         useRetval = false;
 | 
|---|
| 730 |                         condExpr->set_arg1( maybeMutate( condExpr->get_arg1(), *this ) );
 | 
|---|
| 731 |                         useRetval = oldUseRetval;
 | 
|---|
| 732 |                         condExpr->set_arg2( maybeMutate( condExpr->get_arg2(), *this ) );
 | 
|---|
| 733 |                         condExpr->set_arg3( maybeMutate( condExpr->get_arg3(), *this ) );
 | 
|---|
| 734 |                         return condExpr;
 | 
|---|
| 735 | 
 | 
|---|
| 736 |                 }
 | 
|---|
| 737 | 
 | 
|---|
| 738 |                 void Pass1::passArgTypeVars( ApplicationExpr *appExpr, Type *parmType, Type *argBaseType, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars, std::set< std::string > &seenTypes ) {
 | 
|---|
| 739 |                         Type *polyType = isPolyType( parmType, exprTyVars );
 | 
|---|
| 740 |                         if ( polyType && ! dynamic_cast< TypeInstType* >( polyType ) ) {
 | 
|---|
| 741 |                                 std::string typeName = mangleType( polyType );
 | 
|---|
| 742 |                                 if ( seenTypes.count( typeName ) ) return;
 | 
|---|
| 743 | 
 | 
|---|
| 744 |                                 arg = appExpr->get_args().insert( arg, new SizeofExpr( argBaseType->clone() ) );
 | 
|---|
| 745 |                                 arg++;
 | 
|---|
| 746 |                                 arg = appExpr->get_args().insert( arg, new AlignofExpr( argBaseType->clone() ) );
 | 
|---|
| 747 |                                 arg++;
 | 
|---|
| 748 |                                 if ( dynamic_cast< StructInstType* >( polyType ) ) {
 | 
|---|
| 749 |                                         if ( StructInstType *argBaseStructType = dynamic_cast< StructInstType* >( argBaseType ) ) {
 | 
|---|
| 750 |                                                 // zero-length arrays are forbidden by C, so don't pass offset for empty struct
 | 
|---|
| 751 |                                                 if ( ! argBaseStructType->get_baseStruct()->get_members().empty() ) {
 | 
|---|
| 752 |                                                         arg = appExpr->get_args().insert( arg, new OffsetPackExpr( argBaseStructType->clone() ) );
 | 
|---|
| 753 |                                                         arg++;
 | 
|---|
| 754 |                                                 }
 | 
|---|
| 755 |                                         } else {
 | 
|---|
| 756 |                                                 throw SemanticError( "Cannot pass non-struct type for generic struct" );
 | 
|---|
| 757 |                                         }
 | 
|---|
| 758 |                                 }
 | 
|---|
| 759 | 
 | 
|---|
| 760 |                                 seenTypes.insert( typeName );
 | 
|---|
| 761 |                         }
 | 
|---|
| 762 |                 }
 | 
|---|
| 763 | 
 | 
|---|
| 764 |                 void Pass1::passTypeVars( ApplicationExpr *appExpr, ReferenceToType *polyRetType, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ) {
 | 
|---|
| 765 |                         // pass size/align for type variables
 | 
|---|
| 766 |                         for ( TyVarMap::const_iterator tyParm = exprTyVars.begin(); tyParm != exprTyVars.end(); ++tyParm ) {
 | 
|---|
| 767 |                                 ResolvExpr::EqvClass eqvClass;
 | 
|---|
| 768 |                                 assert( env );
 | 
|---|
| 769 |                                 if ( tyParm->second == TypeDecl::Any ) {
 | 
|---|
| 770 |                                         Type *concrete = env->lookup( tyParm->first );
 | 
|---|
| 771 |                                         if ( concrete ) {
 | 
|---|
| 772 |                                                 arg = appExpr->get_args().insert( arg, new SizeofExpr( concrete->clone() ) );
 | 
|---|
| 773 |                                                 arg++;
 | 
|---|
| 774 |                                                 arg = appExpr->get_args().insert( arg, new AlignofExpr( concrete->clone() ) );
 | 
|---|
| 775 |                                                 arg++;
 | 
|---|
| 776 |                                         } else {
 | 
|---|
| 777 |                                                 // xxx - should this be an assertion?
 | 
|---|
| 778 |                                                 throw SemanticError( "unbound type variable: " + tyParm->first + " in application ", appExpr );
 | 
|---|
| 779 |                                         } // if
 | 
|---|
| 780 |                                 } // if
 | 
|---|
| 781 |                         } // for
 | 
|---|
| 782 | 
 | 
|---|
| 783 |                         // add size/align for generic types to parameter list
 | 
|---|
| 784 |                         if ( appExpr->get_function()->get_results().empty() ) return;
 | 
|---|
| 785 |                         FunctionType *funcType = getFunctionType( appExpr->get_function()->get_results().front() );
 | 
|---|
| 786 |                         assert( funcType );
 | 
|---|
| 787 | 
 | 
|---|
| 788 |                         std::list< DeclarationWithType* >::const_iterator fnParm = funcType->get_parameters().begin();
 | 
|---|
| 789 |                         std::list< Expression* >::const_iterator fnArg = arg;
 | 
|---|
| 790 |                         std::set< std::string > seenTypes; ///< names for generic types we've seen
 | 
|---|
| 791 | 
 | 
|---|
| 792 |                         // a polymorphic return type may need to be added to the argument list
 | 
|---|
| 793 |                         if ( polyRetType ) {
 | 
|---|
| 794 |                                 Type *concRetType = replaceWithConcrete( appExpr, polyRetType );
 | 
|---|
| 795 |                                 passArgTypeVars( appExpr, polyRetType, concRetType, arg, exprTyVars, seenTypes );
 | 
|---|
| 796 |                         }
 | 
|---|
| 797 | 
 | 
|---|
| 798 |                         // add type information args for presently unseen types in parameter list
 | 
|---|
| 799 |                         for ( ; fnParm != funcType->get_parameters().end() && fnArg != appExpr->get_args().end(); ++fnParm, ++fnArg ) {
 | 
|---|
| 800 |                                 VariableExpr *fnArgBase = getBaseVar( *fnArg );
 | 
|---|
| 801 |                                 if ( ! fnArgBase || fnArgBase->get_results().empty() ) continue;
 | 
|---|
| 802 |                                 passArgTypeVars( appExpr, (*fnParm)->get_type(), fnArgBase->get_results().front(), arg, exprTyVars, seenTypes );
 | 
|---|
| 803 |                         }
 | 
|---|
| 804 |                 }
 | 
|---|
| 805 | 
 | 
|---|
| 806 |                 ObjectDecl *Pass1::makeTemporary( Type *type ) {
 | 
|---|
| 807 |                         ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, type, 0 );
 | 
|---|
| 808 |                         stmtsToAdd.push_back( new DeclStmt( noLabels, newObj ) );
 | 
|---|
| 809 |                         return newObj;
 | 
|---|
| 810 |                 }
 | 
|---|
| 811 | 
 | 
|---|
| 812 |                 Expression *Pass1::addRetParam( ApplicationExpr *appExpr, FunctionType *function, Type *retType, std::list< Expression *>::iterator &arg ) {
 | 
|---|
| 813 |                         // ***** Code Removal ***** After introducing a temporary variable for all return expressions, the following code appears superfluous.
 | 
|---|
| 814 |                         // if ( useRetval ) {
 | 
|---|
| 815 |                         //      assert( retval );
 | 
|---|
| 816 |                         //      arg = appExpr->get_args().insert( arg, new VariableExpr( retval ) );
 | 
|---|
| 817 |                         //      arg++;
 | 
|---|
| 818 |                         // } else {
 | 
|---|
| 819 | 
 | 
|---|
| 820 |                         // Create temporary to hold return value of polymorphic function and produce that temporary as a result
 | 
|---|
| 821 |                         // using a comma expression.  Possibly change comma expression into statement expression "{}" for multiple
 | 
|---|
| 822 |                         // return values.
 | 
|---|
| 823 |                         ObjectDecl *newObj = makeTemporary( retType->clone() );
 | 
|---|
| 824 |                         Expression *paramExpr = new VariableExpr( newObj );
 | 
|---|
| 825 | 
 | 
|---|
| 826 |                         // If the type of the temporary is not polymorphic, box temporary by taking its address;
 | 
|---|
| 827 |                         // otherwise the temporary is already boxed and can be used directly.
 | 
|---|
| 828 |                         if ( ! isPolyType( newObj->get_type(), scopeTyVars, env ) ) {
 | 
|---|
| 829 |                                 paramExpr = new AddressExpr( paramExpr );
 | 
|---|
| 830 |                         } // if
 | 
|---|
| 831 |                         arg = appExpr->get_args().insert( arg, paramExpr ); // add argument to function call
 | 
|---|
| 832 |                         arg++;
 | 
|---|
| 833 |                         // Build a comma expression to call the function and emulate a normal return.
 | 
|---|
| 834 |                         CommaExpr *commaExpr = new CommaExpr( appExpr, new VariableExpr( newObj ) );
 | 
|---|
| 835 |                         commaExpr->set_env( appExpr->get_env() );
 | 
|---|
| 836 |                         appExpr->set_env( 0 );
 | 
|---|
| 837 |                         return commaExpr;
 | 
|---|
| 838 |                         // } // if
 | 
|---|
| 839 |                         // return appExpr;
 | 
|---|
| 840 |                 }
 | 
|---|
| 841 | 
 | 
|---|
| 842 |                 void Pass1::replaceParametersWithConcrete( ApplicationExpr *appExpr, std::list< Expression* >& params ) {
 | 
|---|
| 843 |                         for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) {
 | 
|---|
| 844 |                                 TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );
 | 
|---|
| 845 |                                 assert(paramType && "Aggregate parameters should be type expressions");
 | 
|---|
| 846 |                                 paramType->set_type( replaceWithConcrete( appExpr, paramType->get_type(), false ) );
 | 
|---|
| 847 |                         }
 | 
|---|
| 848 |                 }
 | 
|---|
| 849 | 
 | 
|---|
| 850 |                 Type *Pass1::replaceWithConcrete( ApplicationExpr *appExpr, Type *type, bool doClone ) {
 | 
|---|
| 851 |                         if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) {
 | 
|---|
| 852 |                                 Type *concrete = env->lookup( typeInst->get_name() );
 | 
|---|
| 853 |                                 if ( concrete == 0 ) {
 | 
|---|
| 854 |                                         throw SemanticError( "Unbound type variable " + typeInst->get_name() + " in ", appExpr );
 | 
|---|
| 855 |                                 } // if
 | 
|---|
| 856 |                                 return concrete;
 | 
|---|
| 857 |                         } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) {
 | 
|---|
| 858 |                                 if ( doClone ) {
 | 
|---|
| 859 |                                         structType = structType->clone();
 | 
|---|
| 860 |                                 }
 | 
|---|
| 861 |                                 replaceParametersWithConcrete( appExpr, structType->get_parameters() );
 | 
|---|
| 862 |                                 return structType;
 | 
|---|
| 863 |                         } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) {
 | 
|---|
| 864 |                                 if ( doClone ) {
 | 
|---|
| 865 |                                         unionType = unionType->clone();
 | 
|---|
| 866 |                                 }
 | 
|---|
| 867 |                                 replaceParametersWithConcrete( appExpr, unionType->get_parameters() );
 | 
|---|
| 868 |                                 return unionType;
 | 
|---|
| 869 |                         }
 | 
|---|
| 870 |                         return type;
 | 
|---|
| 871 |                 }
 | 
|---|
| 872 | 
 | 
|---|
| 873 |                 Expression *Pass1::addDynRetParam( ApplicationExpr *appExpr, FunctionType *function, ReferenceToType *dynType, std::list< Expression *>::iterator &arg ) {
 | 
|---|
| 874 |                         assert( env );
 | 
|---|
| 875 |                         Type *concrete = replaceWithConcrete( appExpr, dynType );
 | 
|---|
| 876 |                         // add out-parameter for return value
 | 
|---|
| 877 |                         return addRetParam( appExpr, function, concrete, arg );
 | 
|---|
| 878 |                 }
 | 
|---|
| 879 | 
 | 
|---|
| 880 |                 Expression *Pass1::applyAdapter( ApplicationExpr *appExpr, FunctionType *function, std::list< Expression *>::iterator &arg, const TyVarMap &tyVars ) {
 | 
|---|
| 881 |                         Expression *ret = appExpr;
 | 
|---|
| 882 | //                      if ( ! function->get_returnVals().empty() && isPolyType( function->get_returnVals().front()->get_type(), tyVars ) ) {
 | 
|---|
| 883 |                         if ( isDynRet( function, tyVars ) ) {
 | 
|---|
| 884 |                                 ret = addRetParam( appExpr, function, function->get_returnVals().front()->get_type(), arg );
 | 
|---|
| 885 |                         } // if
 | 
|---|
| 886 |                         std::string mangleName = mangleAdapterName( function, tyVars );
 | 
|---|
| 887 |                         std::string adapterName = makeAdapterName( mangleName );
 | 
|---|
| 888 | 
 | 
|---|
| 889 |                         // cast adaptee to void (*)(), since it may have any type inside a polymorphic function
 | 
|---|
| 890 |                         Type * adapteeType = new PointerType( Type::Qualifiers(), new FunctionType( Type::Qualifiers(), true ) );
 | 
|---|
| 891 |                         appExpr->get_args().push_front( new CastExpr( appExpr->get_function(), adapteeType ) );
 | 
|---|
| 892 |                         appExpr->set_function( new NameExpr( adapterName ) );
 | 
|---|
| 893 | 
 | 
|---|
| 894 |                         return ret;
 | 
|---|
| 895 |                 }
 | 
|---|
| 896 | 
 | 
|---|
| 897 |                 void Pass1::boxParam( Type *param, Expression *&arg, const TyVarMap &exprTyVars ) {
 | 
|---|
| 898 |                         assert( ! arg->get_results().empty() );
 | 
|---|
| 899 |                         if ( isPolyType( param, exprTyVars ) ) {
 | 
|---|
| 900 |                                 if ( isPolyType( arg->get_results().front() ) ) {
 | 
|---|
| 901 |                                         // if the argument's type is polymorphic, we don't need to box again!
 | 
|---|
| 902 |                                         return;
 | 
|---|
| 903 |                                 } else if ( arg->get_results().front()->get_isLvalue() ) {
 | 
|---|
| 904 |                                         // VariableExpr and MemberExpr are lvalues; need to check this isn't coming from the second arg of a comma expression though (not an lvalue)
 | 
|---|
| 905 |                                         // xxx - need to test that this code is still reachable
 | 
|---|
| 906 |                                         if ( CommaExpr *commaArg = dynamic_cast< CommaExpr* >( arg ) ) {
 | 
|---|
| 907 |                                                 commaArg->set_arg2( new AddressExpr( commaArg->get_arg2() ) );
 | 
|---|
| 908 |                                         } else {
 | 
|---|
| 909 |                                                 arg = new AddressExpr( arg );
 | 
|---|
| 910 |                                         }
 | 
|---|
| 911 |                                 } else {
 | 
|---|
| 912 |                                         // use type computed in unification to declare boxed variables
 | 
|---|
| 913 |                                         Type * newType = param->clone();
 | 
|---|
| 914 |                                         if ( env ) env->apply( newType );
 | 
|---|
| 915 |                                         ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, newType, 0 );
 | 
|---|
| 916 |                                         newObj->get_type()->get_qualifiers() = Type::Qualifiers(); // TODO: is this right???
 | 
|---|
| 917 |                                         stmtsToAdd.push_back( new DeclStmt( noLabels, newObj ) );
 | 
|---|
| 918 |                                         UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) );
 | 
|---|
| 919 |                                         assign->get_args().push_back( new VariableExpr( newObj ) );
 | 
|---|
| 920 |                                         assign->get_args().push_back( arg );
 | 
|---|
| 921 |                                         stmtsToAdd.push_back( new ExprStmt( noLabels, assign ) );
 | 
|---|
| 922 |                                         arg = new AddressExpr( new VariableExpr( newObj ) );
 | 
|---|
| 923 |                                 } // if
 | 
|---|
| 924 |                         } // if
 | 
|---|
| 925 |                 }
 | 
|---|
| 926 | 
 | 
|---|
| 927 |                 /// cast parameters to polymorphic functions so that types are replaced with
 | 
|---|
| 928 |                 /// void * if they are type parameters in the formal type.
 | 
|---|
| 929 |                 /// this gets rid of warnings from gcc.
 | 
|---|
| 930 |                 void addCast( Expression *&actual, Type *formal, const TyVarMap &tyVars ) {
 | 
|---|
| 931 |                         if ( getFunctionType( formal ) ) {
 | 
|---|
| 932 |                                 Type * newType = formal->clone();
 | 
|---|
| 933 |                                 newType = ScrubTyVars::scrub( newType, tyVars );
 | 
|---|
| 934 |                                 actual = new CastExpr( actual, newType );
 | 
|---|
| 935 |                         } // if
 | 
|---|
| 936 |                 }
 | 
|---|
| 937 | 
 | 
|---|
| 938 |                 void Pass1::boxParams( ApplicationExpr *appExpr, FunctionType *function, std::list< Expression *>::iterator &arg, const TyVarMap &exprTyVars ) {
 | 
|---|
| 939 |                         for ( std::list< DeclarationWithType *>::const_iterator param = function->get_parameters().begin(); param != function->get_parameters().end(); ++param, ++arg ) {
 | 
|---|
| 940 |                                 assert( arg != appExpr->get_args().end() );
 | 
|---|
| 941 |                                 addCast( *arg, (*param)->get_type(), exprTyVars );
 | 
|---|
| 942 |                                 boxParam( (*param)->get_type(), *arg, exprTyVars );
 | 
|---|
| 943 |                         } // for
 | 
|---|
| 944 |                 }
 | 
|---|
| 945 | 
 | 
|---|
| 946 |                 void Pass1::addInferredParams( ApplicationExpr *appExpr, FunctionType *functionType, std::list< Expression *>::iterator &arg, const TyVarMap &tyVars ) {
 | 
|---|
| 947 |                         std::list< Expression *>::iterator cur = arg;
 | 
|---|
| 948 |                         for ( std::list< TypeDecl *>::iterator tyVar = functionType->get_forall().begin(); tyVar != functionType->get_forall().end(); ++tyVar ) {
 | 
|---|
| 949 |                                 for ( std::list< DeclarationWithType *>::iterator assert = (*tyVar)->get_assertions().begin(); assert != (*tyVar)->get_assertions().end(); ++assert ) {
 | 
|---|
| 950 |                                         InferredParams::const_iterator inferParam = appExpr->get_inferParams().find( (*assert)->get_uniqueId() );
 | 
|---|
| 951 |                                         assert( inferParam != appExpr->get_inferParams().end() && "NOTE: Explicit casts of polymorphic functions to compatible monomorphic functions are currently unsupported" );
 | 
|---|
| 952 |                                         Expression *newExpr = inferParam->second.expr->clone();
 | 
|---|
| 953 |                                         addCast( newExpr, (*assert)->get_type(), tyVars );
 | 
|---|
| 954 |                                         boxParam( (*assert)->get_type(), newExpr, tyVars );
 | 
|---|
| 955 |                                         appExpr->get_args().insert( cur, newExpr );
 | 
|---|
| 956 |                                 } // for
 | 
|---|
| 957 |                         } // for
 | 
|---|
| 958 |                 }
 | 
|---|
| 959 | 
 | 
|---|
| 960 |                 void makeRetParm( FunctionType *funcType ) {
 | 
|---|
| 961 |                         DeclarationWithType *retParm = funcType->get_returnVals().front();
 | 
|---|
| 962 | 
 | 
|---|
| 963 |                         // make a new parameter that is a pointer to the type of the old return value
 | 
|---|
| 964 |                         retParm->set_type( new PointerType( Type::Qualifiers(), retParm->get_type() ) );
 | 
|---|
| 965 |                         funcType->get_parameters().push_front( retParm );
 | 
|---|
| 966 | 
 | 
|---|
| 967 |                         // we don't need the return value any more
 | 
|---|
| 968 |                         funcType->get_returnVals().clear();
 | 
|---|
| 969 |                 }
 | 
|---|
| 970 | 
 | 
|---|
| 971 |                 FunctionType *makeAdapterType( FunctionType *adaptee, const TyVarMap &tyVars ) {
 | 
|---|
| 972 |                         // actually make the adapter type
 | 
|---|
| 973 |                         FunctionType *adapter = adaptee->clone();
 | 
|---|
| 974 | //                      if ( ! adapter->get_returnVals().empty() && isPolyType( adapter->get_returnVals().front()->get_type(), tyVars ) ) {
 | 
|---|
| 975 |                         if ( isDynRet( adapter, tyVars ) ) {
 | 
|---|
| 976 |                                 makeRetParm( adapter );
 | 
|---|
| 977 |                         } // if
 | 
|---|
| 978 |                         adapter->get_parameters().push_front( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new PointerType( Type::Qualifiers(), new FunctionType( Type::Qualifiers(), true ) ), 0 ) );
 | 
|---|
| 979 |                         return adapter;
 | 
|---|
| 980 |                 }
 | 
|---|
| 981 | 
 | 
|---|
| 982 |                 Expression *makeAdapterArg( DeclarationWithType *param, DeclarationWithType *arg, DeclarationWithType *realParam, const TyVarMap &tyVars ) {
 | 
|---|
| 983 |                         assert( param );
 | 
|---|
| 984 |                         assert( arg );
 | 
|---|
| 985 |                         if ( isPolyType( realParam->get_type(), tyVars ) ) {
 | 
|---|
| 986 |                                 if ( ! isPolyType( arg->get_type() ) ) {
 | 
|---|
| 987 |                                         UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) );
 | 
|---|
| 988 |                                         deref->get_args().push_back( new CastExpr( new VariableExpr( param ), new PointerType( Type::Qualifiers(), arg->get_type()->clone() ) ) );
 | 
|---|
| 989 |                                         deref->get_results().push_back( arg->get_type()->clone() );
 | 
|---|
| 990 |                                         return deref;
 | 
|---|
| 991 |                                 } // if
 | 
|---|
| 992 |                         } // if
 | 
|---|
| 993 |                         return new VariableExpr( param );
 | 
|---|
| 994 |                 }
 | 
|---|
| 995 | 
 | 
|---|
| 996 |                 void addAdapterParams( ApplicationExpr *adapteeApp, std::list< DeclarationWithType *>::iterator arg, std::list< DeclarationWithType *>::iterator param, std::list< DeclarationWithType *>::iterator paramEnd, std::list< DeclarationWithType *>::iterator realParam, const TyVarMap &tyVars ) {
 | 
|---|
| 997 |                         UniqueName paramNamer( "_p" );
 | 
|---|
| 998 |                         for ( ; param != paramEnd; ++param, ++arg, ++realParam ) {
 | 
|---|
| 999 |                                 if ( (*param)->get_name() == "" ) {
 | 
|---|
| 1000 |                                         (*param)->set_name( paramNamer.newName() );
 | 
|---|
| 1001 |                                         (*param)->set_linkage( LinkageSpec::C );
 | 
|---|
| 1002 |                                 } // if
 | 
|---|
| 1003 |                                 adapteeApp->get_args().push_back( makeAdapterArg( *param, *arg, *realParam, tyVars ) );
 | 
|---|
| 1004 |                         } // for
 | 
|---|
| 1005 |                 }
 | 
|---|
| 1006 | 
 | 
|---|
| 1007 |                 FunctionDecl *Pass1::makeAdapter( FunctionType *adaptee, FunctionType *realType, const std::string &mangleName, const TyVarMap &tyVars ) {
 | 
|---|
| 1008 |                         FunctionType *adapterType = makeAdapterType( adaptee, tyVars );
 | 
|---|
| 1009 |                         adapterType = ScrubTyVars::scrub( adapterType, tyVars );
 | 
|---|
| 1010 |                         DeclarationWithType *adapteeDecl = adapterType->get_parameters().front();
 | 
|---|
| 1011 |                         adapteeDecl->set_name( "_adaptee" );
 | 
|---|
| 1012 |                         ApplicationExpr *adapteeApp = new ApplicationExpr( new CastExpr( new VariableExpr( adapteeDecl ), new PointerType( Type::Qualifiers(), realType ) ) );
 | 
|---|
| 1013 |                         Statement *bodyStmt;
 | 
|---|
| 1014 | 
 | 
|---|
| 1015 |                         std::list< TypeDecl *>::iterator tyArg = realType->get_forall().begin();
 | 
|---|
| 1016 |                         std::list< TypeDecl *>::iterator tyParam = adapterType->get_forall().begin();
 | 
|---|
| 1017 |                         std::list< TypeDecl *>::iterator realTyParam = adaptee->get_forall().begin();
 | 
|---|
| 1018 |                         for ( ; tyParam != adapterType->get_forall().end(); ++tyArg, ++tyParam, ++realTyParam ) {
 | 
|---|
| 1019 |                                 assert( tyArg != realType->get_forall().end() );
 | 
|---|
| 1020 |                                 std::list< DeclarationWithType *>::iterator assertArg = (*tyArg)->get_assertions().begin();
 | 
|---|
| 1021 |                                 std::list< DeclarationWithType *>::iterator assertParam = (*tyParam)->get_assertions().begin();
 | 
|---|
| 1022 |                                 std::list< DeclarationWithType *>::iterator realAssertParam = (*realTyParam)->get_assertions().begin();
 | 
|---|
| 1023 |                                 for ( ; assertParam != (*tyParam)->get_assertions().end(); ++assertArg, ++assertParam, ++realAssertParam ) {
 | 
|---|
| 1024 |                                         assert( assertArg != (*tyArg)->get_assertions().end() );
 | 
|---|
| 1025 |                                         adapteeApp->get_args().push_back( makeAdapterArg( *assertParam, *assertArg, *realAssertParam, tyVars ) );
 | 
|---|
| 1026 |                                 } // for
 | 
|---|
| 1027 |                         } // for
 | 
|---|
| 1028 | 
 | 
|---|
| 1029 |                         std::list< DeclarationWithType *>::iterator arg = realType->get_parameters().begin();
 | 
|---|
| 1030 |                         std::list< DeclarationWithType *>::iterator param = adapterType->get_parameters().begin();
 | 
|---|
| 1031 |                         std::list< DeclarationWithType *>::iterator realParam = adaptee->get_parameters().begin();
 | 
|---|
| 1032 |                         param++;                // skip adaptee parameter in the adapter type
 | 
|---|
| 1033 |                         if ( realType->get_returnVals().empty() ) {
 | 
|---|
| 1034 |                                 // void return
 | 
|---|
| 1035 |                                 addAdapterParams( adapteeApp, arg, param, adapterType->get_parameters().end(), realParam, tyVars );
 | 
|---|
| 1036 |                                 bodyStmt = new ExprStmt( noLabels, adapteeApp );
 | 
|---|
| 1037 | //                      } else if ( isPolyType( adaptee->get_returnVals().front()->get_type(), tyVars ) ) {
 | 
|---|
| 1038 |                         } else if ( isDynType( adaptee->get_returnVals().front()->get_type(), tyVars ) ) {
 | 
|---|
| 1039 |                                 // return type T
 | 
|---|
| 1040 |                                 if ( (*param)->get_name() == "" ) {
 | 
|---|
| 1041 |                                         (*param)->set_name( "_ret" );
 | 
|---|
| 1042 |                                         (*param)->set_linkage( LinkageSpec::C );
 | 
|---|
| 1043 |                                 } // if
 | 
|---|
| 1044 |                                 UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) );
 | 
|---|
| 1045 |                                 UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) );
 | 
|---|
| 1046 |                                 deref->get_args().push_back( new CastExpr( new VariableExpr( *param++ ), new PointerType( Type::Qualifiers(), realType->get_returnVals().front()->get_type()->clone() ) ) );
 | 
|---|
| 1047 |                                 assign->get_args().push_back( deref );
 | 
|---|
| 1048 |                                 addAdapterParams( adapteeApp, arg, param, adapterType->get_parameters().end(), realParam, tyVars );
 | 
|---|
| 1049 |                                 assign->get_args().push_back( adapteeApp );
 | 
|---|
| 1050 |                                 bodyStmt = new ExprStmt( noLabels, assign );
 | 
|---|
| 1051 |                         } else {
 | 
|---|
| 1052 |                                 // adapter for a function that returns a monomorphic value
 | 
|---|
| 1053 |                                 addAdapterParams( adapteeApp, arg, param, adapterType->get_parameters().end(), realParam, tyVars );
 | 
|---|
| 1054 |                                 bodyStmt = new ReturnStmt( noLabels, adapteeApp );
 | 
|---|
| 1055 |                         } // if
 | 
|---|
| 1056 |                         CompoundStmt *adapterBody = new CompoundStmt( noLabels );
 | 
|---|
| 1057 |                         adapterBody->get_kids().push_back( bodyStmt );
 | 
|---|
| 1058 |                         std::string adapterName = makeAdapterName( mangleName );
 | 
|---|
| 1059 |                         return new FunctionDecl( adapterName, DeclarationNode::NoStorageClass, LinkageSpec::C, adapterType, adapterBody, false, false );
 | 
|---|
| 1060 |                 }
 | 
|---|
| 1061 | 
 | 
|---|
| 1062 |                 void Pass1::passAdapters( ApplicationExpr * appExpr, FunctionType * functionType, const TyVarMap & exprTyVars ) {
 | 
|---|
| 1063 |                         // collect a list of function types passed as parameters or implicit parameters (assertions)
 | 
|---|
| 1064 |                         std::list< DeclarationWithType *> ¶mList = functionType->get_parameters();
 | 
|---|
| 1065 |                         std::list< FunctionType *> functions;
 | 
|---|
| 1066 |                         for ( std::list< TypeDecl *>::iterator tyVar = functionType->get_forall().begin(); tyVar != functionType->get_forall().end(); ++tyVar ) {
 | 
|---|
| 1067 |                                 for ( std::list< DeclarationWithType *>::iterator assert = (*tyVar)->get_assertions().begin(); assert != (*tyVar)->get_assertions().end(); ++assert ) {
 | 
|---|
| 1068 |                                         findFunction( (*assert)->get_type(), functions, exprTyVars, needsAdapter );
 | 
|---|
| 1069 |                                 } // for
 | 
|---|
| 1070 |                         } // for
 | 
|---|
| 1071 |                         for ( std::list< DeclarationWithType *>::iterator arg = paramList.begin(); arg != paramList.end(); ++arg ) {
 | 
|---|
| 1072 |                                 findFunction( (*arg)->get_type(), functions, exprTyVars, needsAdapter );
 | 
|---|
| 1073 |                         } // for
 | 
|---|
| 1074 | 
 | 
|---|
| 1075 |                         // parameter function types for which an appropriate adapter has been generated.  we cannot use the types
 | 
|---|
| 1076 |                         // after applying substitutions, since two different parameter types may be unified to the same type
 | 
|---|
| 1077 |                         std::set< std::string > adaptersDone;
 | 
|---|
| 1078 | 
 | 
|---|
| 1079 |                         for ( std::list< FunctionType *>::iterator funType = functions.begin(); funType != functions.end(); ++funType ) {
 | 
|---|
| 1080 |                                 FunctionType *originalFunction = (*funType)->clone();
 | 
|---|
| 1081 |                                 FunctionType *realFunction = (*funType)->clone();
 | 
|---|
| 1082 |                                 std::string mangleName = SymTab::Mangler::mangle( realFunction );
 | 
|---|
| 1083 | 
 | 
|---|
| 1084 |                                 // only attempt to create an adapter or pass one as a parameter if we haven't already done so for this
 | 
|---|
| 1085 |                                 // pre-substitution parameter function type.
 | 
|---|
| 1086 |                                 if ( adaptersDone.find( mangleName ) == adaptersDone.end() ) {
 | 
|---|
| 1087 |                                         adaptersDone.insert( adaptersDone.begin(), mangleName );
 | 
|---|
| 1088 | 
 | 
|---|
| 1089 |                                         // apply substitution to type variables to figure out what the adapter's type should look like
 | 
|---|
| 1090 |                                         assert( env );
 | 
|---|
| 1091 |                                         env->apply( realFunction );
 | 
|---|
| 1092 |                                         mangleName = SymTab::Mangler::mangle( realFunction );
 | 
|---|
| 1093 |                                         mangleName += makePolyMonoSuffix( originalFunction, exprTyVars );
 | 
|---|
| 1094 | 
 | 
|---|
| 1095 |                                         typedef ScopedMap< std::string, DeclarationWithType* >::iterator AdapterIter;
 | 
|---|
| 1096 |                                         AdapterIter adapter = adapters.find( mangleName );
 | 
|---|
| 1097 |                                         if ( adapter == adapters.end() ) {
 | 
|---|
| 1098 |                                                 // adapter has not been created yet in the current scope, so define it
 | 
|---|
| 1099 |                                                 FunctionDecl *newAdapter = makeAdapter( *funType, realFunction, mangleName, exprTyVars );
 | 
|---|
| 1100 |                                                 std::pair< AdapterIter, bool > answer = adapters.insert( std::pair< std::string, DeclarationWithType *>( mangleName, newAdapter ) );
 | 
|---|
| 1101 |                                                 adapter = answer.first;
 | 
|---|
| 1102 |                                                 stmtsToAdd.push_back( new DeclStmt( noLabels, newAdapter ) );
 | 
|---|
| 1103 |                                         } // if
 | 
|---|
| 1104 |                                         assert( adapter != adapters.end() );
 | 
|---|
| 1105 | 
 | 
|---|
| 1106 |                                         // add the appropriate adapter as a parameter
 | 
|---|
| 1107 |                                         appExpr->get_args().push_front( new VariableExpr( adapter->second ) );
 | 
|---|
| 1108 |                                 } // if
 | 
|---|
| 1109 |                         } // for
 | 
|---|
| 1110 |                 } // passAdapters
 | 
|---|
| 1111 | 
 | 
|---|
| 1112 |                 Expression *makeIncrDecrExpr( ApplicationExpr *appExpr, Type *polyType, bool isIncr ) {
 | 
|---|
| 1113 |                         NameExpr *opExpr;
 | 
|---|
| 1114 |                         if ( isIncr ) {
 | 
|---|
| 1115 |                                 opExpr = new NameExpr( "?+=?" );
 | 
|---|
| 1116 |                         } else {
 | 
|---|
| 1117 |                                 opExpr = new NameExpr( "?-=?" );
 | 
|---|
| 1118 |                         } // if
 | 
|---|
| 1119 |                         UntypedExpr *addAssign = new UntypedExpr( opExpr );
 | 
|---|
| 1120 |                         if ( AddressExpr *address = dynamic_cast< AddressExpr *>( appExpr->get_args().front() ) ) {
 | 
|---|
| 1121 |                                 addAssign->get_args().push_back( address->get_arg() );
 | 
|---|
| 1122 |                         } else {
 | 
|---|
| 1123 |                                 addAssign->get_args().push_back( appExpr->get_args().front() );
 | 
|---|
| 1124 |                         } // if
 | 
|---|
| 1125 |                         addAssign->get_args().push_back( new NameExpr( sizeofName( mangleType( polyType ) ) ) );
 | 
|---|
| 1126 |                         addAssign->get_results().front() = appExpr->get_results().front()->clone();
 | 
|---|
| 1127 |                         if ( appExpr->get_env() ) {
 | 
|---|
| 1128 |                                 addAssign->set_env( appExpr->get_env() );
 | 
|---|
| 1129 |                                 appExpr->set_env( 0 );
 | 
|---|
| 1130 |                         } // if
 | 
|---|
| 1131 |                         appExpr->get_args().clear();
 | 
|---|
| 1132 |                         delete appExpr;
 | 
|---|
| 1133 |                         return addAssign;
 | 
|---|
| 1134 |                 }
 | 
|---|
| 1135 | 
 | 
|---|
| 1136 |                 Expression *Pass1::handleIntrinsics( ApplicationExpr *appExpr ) {
 | 
|---|
| 1137 |                         if ( VariableExpr *varExpr = dynamic_cast< VariableExpr *>( appExpr->get_function() ) ) {
 | 
|---|
| 1138 |                                 if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic ) {
 | 
|---|
| 1139 |                                         if ( varExpr->get_var()->get_name() == "?[?]" ) {
 | 
|---|
| 1140 |                                                 assert( ! appExpr->get_results().empty() );
 | 
|---|
| 1141 |                                                 assert( appExpr->get_args().size() == 2 );
 | 
|---|
| 1142 |                                                 Type *baseType1 = isPolyPtr( appExpr->get_args().front()->get_results().front(), scopeTyVars, env );
 | 
|---|
| 1143 |                                                 Type *baseType2 = isPolyPtr( appExpr->get_args().back()->get_results().front(), scopeTyVars, env );
 | 
|---|
| 1144 |                                                 assert( ! baseType1 || ! baseType2 ); // the arguments cannot both be polymorphic pointers
 | 
|---|
| 1145 |                                                 UntypedExpr *ret = 0;
 | 
|---|
| 1146 |                                                 if ( baseType1 || baseType2 ) { // one of the arguments is a polymorphic pointer
 | 
|---|
| 1147 |                                                         ret = new UntypedExpr( new NameExpr( "?+?" ) );
 | 
|---|
| 1148 |                                                 } // if
 | 
|---|
| 1149 |                                                 if ( baseType1 ) {
 | 
|---|
| 1150 |                                                         UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) );
 | 
|---|
| 1151 |                                                         multiply->get_args().push_back( appExpr->get_args().back() );
 | 
|---|
| 1152 |                                                         multiply->get_args().push_back( new SizeofExpr( baseType1->clone() ) );
 | 
|---|
| 1153 |                                                         ret->get_args().push_back( appExpr->get_args().front() );
 | 
|---|
| 1154 |                                                         ret->get_args().push_back( multiply );
 | 
|---|
| 1155 |                                                 } else if ( baseType2 ) {
 | 
|---|
| 1156 |                                                         UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) );
 | 
|---|
| 1157 |                                                         multiply->get_args().push_back( appExpr->get_args().front() );
 | 
|---|
| 1158 |                                                         multiply->get_args().push_back( new SizeofExpr( baseType2->clone() ) );
 | 
|---|
| 1159 |                                                         ret->get_args().push_back( multiply );
 | 
|---|
| 1160 |                                                         ret->get_args().push_back( appExpr->get_args().back() );
 | 
|---|
| 1161 |                                                 } // if
 | 
|---|
| 1162 |                                                 if ( baseType1 || baseType2 ) {
 | 
|---|
| 1163 |                                                         ret->get_results().push_front( appExpr->get_results().front()->clone() );
 | 
|---|
| 1164 |                                                         if ( appExpr->get_env() ) {
 | 
|---|
| 1165 |                                                                 ret->set_env( appExpr->get_env() );
 | 
|---|
| 1166 |                                                                 appExpr->set_env( 0 );
 | 
|---|
| 1167 |                                                         } // if
 | 
|---|
| 1168 |                                                         appExpr->get_args().clear();
 | 
|---|
| 1169 |                                                         delete appExpr;
 | 
|---|
| 1170 |                                                         return ret;
 | 
|---|
| 1171 |                                                 } // if
 | 
|---|
| 1172 |                                         } else if ( varExpr->get_var()->get_name() == "*?" ) {
 | 
|---|
| 1173 |                                                 assert( ! appExpr->get_results().empty() );
 | 
|---|
| 1174 |                                                 assert( ! appExpr->get_args().empty() );
 | 
|---|
| 1175 |                                                 if ( isPolyType( appExpr->get_results().front(), scopeTyVars, env ) ) {
 | 
|---|
| 1176 |                                                         Expression *ret = appExpr->get_args().front();
 | 
|---|
| 1177 |                                                         delete ret->get_results().front();
 | 
|---|
| 1178 |                                                         ret->get_results().front() = appExpr->get_results().front()->clone();
 | 
|---|
| 1179 |                                                         if ( appExpr->get_env() ) {
 | 
|---|
| 1180 |                                                                 ret->set_env( appExpr->get_env() );
 | 
|---|
| 1181 |                                                                 appExpr->set_env( 0 );
 | 
|---|
| 1182 |                                                         } // if
 | 
|---|
| 1183 |                                                         appExpr->get_args().clear();
 | 
|---|
| 1184 |                                                         delete appExpr;
 | 
|---|
| 1185 |                                                         return ret;
 | 
|---|
| 1186 |                                                 } // if
 | 
|---|
| 1187 |                                         } else if ( varExpr->get_var()->get_name() == "?++" || varExpr->get_var()->get_name() == "?--" ) {
 | 
|---|
| 1188 |                                                 assert( ! appExpr->get_results().empty() );
 | 
|---|
| 1189 |                                                 assert( appExpr->get_args().size() == 1 );
 | 
|---|
| 1190 |                                                 if ( Type *baseType = isPolyPtr( appExpr->get_results().front(), scopeTyVars, env ) ) {
 | 
|---|
| 1191 |                                                         Type *tempType = appExpr->get_results().front()->clone();
 | 
|---|
| 1192 |                                                         if ( env ) {
 | 
|---|
| 1193 |                                                                 env->apply( tempType );
 | 
|---|
| 1194 |                                                         } // if
 | 
|---|
| 1195 |                                                         ObjectDecl *newObj = makeTemporary( tempType );
 | 
|---|
| 1196 |                                                         VariableExpr *tempExpr = new VariableExpr( newObj );
 | 
|---|
| 1197 |                                                         UntypedExpr *assignExpr = new UntypedExpr( new NameExpr( "?=?" ) );
 | 
|---|
| 1198 |                                                         assignExpr->get_args().push_back( tempExpr->clone() );
 | 
|---|
| 1199 |                                                         if ( AddressExpr *address = dynamic_cast< AddressExpr *>( appExpr->get_args().front() ) ) {
 | 
|---|
| 1200 |                                                                 assignExpr->get_args().push_back( address->get_arg()->clone() );
 | 
|---|
| 1201 |                                                         } else {
 | 
|---|
| 1202 |                                                                 assignExpr->get_args().push_back( appExpr->get_args().front()->clone() );
 | 
|---|
| 1203 |                                                         } // if
 | 
|---|
| 1204 |                                                         CommaExpr *firstComma = new CommaExpr( assignExpr, makeIncrDecrExpr( appExpr, baseType, varExpr->get_var()->get_name() == "?++" ) );
 | 
|---|
| 1205 |                                                         return new CommaExpr( firstComma, tempExpr );
 | 
|---|
| 1206 |                                                 } // if
 | 
|---|
| 1207 |                                         } else if ( varExpr->get_var()->get_name() == "++?" || varExpr->get_var()->get_name() == "--?" ) {
 | 
|---|
| 1208 |                                                 assert( ! appExpr->get_results().empty() );
 | 
|---|
| 1209 |                                                 assert( appExpr->get_args().size() == 1 );
 | 
|---|
| 1210 |                                                 if ( Type *baseType = isPolyPtr( appExpr->get_results().front(), scopeTyVars, env ) ) {
 | 
|---|
| 1211 |                                                         return makeIncrDecrExpr( appExpr, baseType, varExpr->get_var()->get_name() == "++?" );
 | 
|---|
| 1212 |                                                 } // if
 | 
|---|
| 1213 |                                         } else if ( varExpr->get_var()->get_name() == "?+?" || varExpr->get_var()->get_name() == "?-?" ) {
 | 
|---|
| 1214 |                                                 assert( ! appExpr->get_results().empty() );
 | 
|---|
| 1215 |                                                 assert( appExpr->get_args().size() == 2 );
 | 
|---|
| 1216 |                                                 Type *baseType1 = isPolyPtr( appExpr->get_args().front()->get_results().front(), scopeTyVars, env );
 | 
|---|
| 1217 |                                                 Type *baseType2 = isPolyPtr( appExpr->get_args().back()->get_results().front(), scopeTyVars, env );
 | 
|---|
| 1218 |                                                 if ( baseType1 && baseType2 ) {
 | 
|---|
| 1219 |                                                         UntypedExpr *divide = new UntypedExpr( new NameExpr( "?/?" ) );
 | 
|---|
| 1220 |                                                         divide->get_args().push_back( appExpr );
 | 
|---|
| 1221 |                                                         divide->get_args().push_back( new SizeofExpr( baseType1->clone() ) );
 | 
|---|
| 1222 |                                                         divide->get_results().push_front( appExpr->get_results().front()->clone() );
 | 
|---|
| 1223 |                                                         if ( appExpr->get_env() ) {
 | 
|---|
| 1224 |                                                                 divide->set_env( appExpr->get_env() );
 | 
|---|
| 1225 |                                                                 appExpr->set_env( 0 );
 | 
|---|
| 1226 |                                                         } // if
 | 
|---|
| 1227 |                                                         return divide;
 | 
|---|
| 1228 |                                                 } else if ( baseType1 ) {
 | 
|---|
| 1229 |                                                         UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) );
 | 
|---|
| 1230 |                                                         multiply->get_args().push_back( appExpr->get_args().back() );
 | 
|---|
| 1231 |                                                         multiply->get_args().push_back( new SizeofExpr( baseType1->clone() ) );
 | 
|---|
| 1232 |                                                         appExpr->get_args().back() = multiply;
 | 
|---|
| 1233 |                                                 } else if ( baseType2 ) {
 | 
|---|
| 1234 |                                                         UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) );
 | 
|---|
| 1235 |                                                         multiply->get_args().push_back( appExpr->get_args().front() );
 | 
|---|
| 1236 |                                                         multiply->get_args().push_back( new SizeofExpr( baseType2->clone() ) );
 | 
|---|
| 1237 |                                                         appExpr->get_args().front() = multiply;
 | 
|---|
| 1238 |                                                 } // if
 | 
|---|
| 1239 |                                         } else if ( varExpr->get_var()->get_name() == "?+=?" || varExpr->get_var()->get_name() == "?-=?" ) {
 | 
|---|
| 1240 |                                                 assert( ! appExpr->get_results().empty() );
 | 
|---|
| 1241 |                                                 assert( appExpr->get_args().size() == 2 );
 | 
|---|
| 1242 |                                                 Type *baseType = isPolyPtr( appExpr->get_results().front(), scopeTyVars, env );
 | 
|---|
| 1243 |                                                 if ( baseType ) {
 | 
|---|
| 1244 |                                                         UntypedExpr *multiply = new UntypedExpr( new NameExpr( "?*?" ) );
 | 
|---|
| 1245 |                                                         multiply->get_args().push_back( appExpr->get_args().back() );
 | 
|---|
| 1246 |                                                         multiply->get_args().push_back( new SizeofExpr( baseType->clone() ) );
 | 
|---|
| 1247 |                                                         appExpr->get_args().back() = multiply;
 | 
|---|
| 1248 |                                                 } // if
 | 
|---|
| 1249 |                                         } // if
 | 
|---|
| 1250 |                                         return appExpr;
 | 
|---|
| 1251 |                                 } // if
 | 
|---|
| 1252 |                         } // if
 | 
|---|
| 1253 |                         return 0;
 | 
|---|
| 1254 |                 }
 | 
|---|
| 1255 | 
 | 
|---|
| 1256 |                 Expression *Pass1::mutate( ApplicationExpr *appExpr ) {
 | 
|---|
| 1257 |                         // std::cerr << "mutate appExpr: ";
 | 
|---|
| 1258 |                         // for ( TyVarMap::iterator i = scopeTyVars.begin(); i != scopeTyVars.end(); ++i ) {
 | 
|---|
| 1259 |                         //      std::cerr << i->first << " ";
 | 
|---|
| 1260 |                         // }
 | 
|---|
| 1261 |                         // std::cerr << "\n";
 | 
|---|
| 1262 |                         bool oldUseRetval = useRetval;
 | 
|---|
| 1263 |                         useRetval = false;
 | 
|---|
| 1264 |                         appExpr->get_function()->acceptMutator( *this );
 | 
|---|
| 1265 |                         mutateAll( appExpr->get_args(), *this );
 | 
|---|
| 1266 |                         useRetval = oldUseRetval;
 | 
|---|
| 1267 | 
 | 
|---|
| 1268 |                         assert( ! appExpr->get_function()->get_results().empty() );
 | 
|---|
| 1269 |                         PointerType *pointer = dynamic_cast< PointerType *>( appExpr->get_function()->get_results().front() );
 | 
|---|
| 1270 |                         assert( pointer );
 | 
|---|
| 1271 |                         FunctionType *function = dynamic_cast< FunctionType *>( pointer->get_base() );
 | 
|---|
| 1272 |                         assert( function );
 | 
|---|
| 1273 | 
 | 
|---|
| 1274 |                         if ( Expression *newExpr = handleIntrinsics( appExpr ) ) {
 | 
|---|
| 1275 |                                 return newExpr;
 | 
|---|
| 1276 |                         } // if
 | 
|---|
| 1277 | 
 | 
|---|
| 1278 |                         Expression *ret = appExpr;
 | 
|---|
| 1279 | 
 | 
|---|
| 1280 |                         std::list< Expression *>::iterator arg = appExpr->get_args().begin();
 | 
|---|
| 1281 |                         std::list< Expression *>::iterator paramBegin = appExpr->get_args().begin();
 | 
|---|
| 1282 | 
 | 
|---|
| 1283 |                         TyVarMap exprTyVars( (TypeDecl::Kind)-1 );
 | 
|---|
| 1284 |                         makeTyVarMap( function, exprTyVars );
 | 
|---|
| 1285 |                         ReferenceToType *dynRetType = isDynRet( function, exprTyVars );
 | 
|---|
| 1286 | 
 | 
|---|
| 1287 |                         if ( dynRetType ) {
 | 
|---|
| 1288 |                                 ret = addDynRetParam( appExpr, function, dynRetType, arg );
 | 
|---|
| 1289 |                         } else if ( needsAdapter( function, scopeTyVars ) ) {
 | 
|---|
| 1290 |                                 // std::cerr << "needs adapter: ";
 | 
|---|
| 1291 |                                 // printTyVarMap( std::cerr, scopeTyVars );
 | 
|---|
| 1292 |                                 // std::cerr << *env << std::endl;
 | 
|---|
| 1293 |                                 // change the application so it calls the adapter rather than the passed function
 | 
|---|
| 1294 |                                 ret = applyAdapter( appExpr, function, arg, scopeTyVars );
 | 
|---|
| 1295 |                         } // if
 | 
|---|
| 1296 |                         arg = appExpr->get_args().begin();
 | 
|---|
| 1297 | 
 | 
|---|
| 1298 |                         passTypeVars( appExpr, dynRetType, arg, exprTyVars );
 | 
|---|
| 1299 |                         addInferredParams( appExpr, function, arg, exprTyVars );
 | 
|---|
| 1300 | 
 | 
|---|
| 1301 |                         arg = paramBegin;
 | 
|---|
| 1302 | 
 | 
|---|
| 1303 |                         boxParams( appExpr, function, arg, exprTyVars );
 | 
|---|
| 1304 |                         passAdapters( appExpr, function, exprTyVars );
 | 
|---|
| 1305 | 
 | 
|---|
| 1306 |                         return ret;
 | 
|---|
| 1307 |                 }
 | 
|---|
| 1308 | 
 | 
|---|
| 1309 |                 Expression *Pass1::mutate( UntypedExpr *expr ) {
 | 
|---|
| 1310 |                         if ( ! expr->get_results().empty() && isPolyType( expr->get_results().front(), scopeTyVars, env ) ) {
 | 
|---|
| 1311 |                                 if ( NameExpr *name = dynamic_cast< NameExpr *>( expr->get_function() ) ) {
 | 
|---|
| 1312 |                                         if ( name->get_name() == "*?" ) {
 | 
|---|
| 1313 |                                                 Expression *ret = expr->get_args().front();
 | 
|---|
| 1314 |                                                 expr->get_args().clear();
 | 
|---|
| 1315 |                                                 delete expr;
 | 
|---|
| 1316 |                                                 return ret->acceptMutator( *this );
 | 
|---|
| 1317 |                                         } // if
 | 
|---|
| 1318 |                                 } // if
 | 
|---|
| 1319 |                         } // if
 | 
|---|
| 1320 |                         return PolyMutator::mutate( expr );
 | 
|---|
| 1321 |                 }
 | 
|---|
| 1322 | 
 | 
|---|
| 1323 |                 Expression *Pass1::mutate( AddressExpr *addrExpr ) {
 | 
|---|
| 1324 |                         assert( ! addrExpr->get_arg()->get_results().empty() );
 | 
|---|
| 1325 | 
 | 
|---|
| 1326 |                         bool needs = false;
 | 
|---|
| 1327 |                         if ( UntypedExpr *expr = dynamic_cast< UntypedExpr *>( addrExpr->get_arg() ) ) {
 | 
|---|
| 1328 |                                 if ( ! expr->get_results().empty() && isPolyType( expr->get_results().front(), scopeTyVars, env ) ) {
 | 
|---|
| 1329 |                                         if ( NameExpr *name = dynamic_cast< NameExpr *>( expr->get_function() ) ) {
 | 
|---|
| 1330 |                                                 if ( name->get_name() == "*?" ) {
 | 
|---|
| 1331 |                                                         if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr->get_args().front() ) ) {
 | 
|---|
| 1332 |                                                                 assert( ! appExpr->get_function()->get_results().empty() );
 | 
|---|
| 1333 |                                                                 PointerType *pointer = dynamic_cast< PointerType *>( appExpr->get_function()->get_results().front() );
 | 
|---|
| 1334 |                                                                 assert( pointer );
 | 
|---|
| 1335 |                                                                 FunctionType *function = dynamic_cast< FunctionType *>( pointer->get_base() );
 | 
|---|
| 1336 |                                                                 assert( function );
 | 
|---|
| 1337 |                                                                 needs = needsAdapter( function, scopeTyVars );
 | 
|---|
| 1338 |                                                         } // if
 | 
|---|
| 1339 |                                                 } // if
 | 
|---|
| 1340 |                                         } // if
 | 
|---|
| 1341 |                                 } // if
 | 
|---|
| 1342 |                         } // if
 | 
|---|
| 1343 |                         // isPolyType check needs to happen before mutating addrExpr arg, so pull it forward
 | 
|---|
| 1344 |                         // out of the if condition.
 | 
|---|
| 1345 |                         bool polytype = isPolyType( addrExpr->get_arg()->get_results().front(), scopeTyVars, env );
 | 
|---|
| 1346 |                         addrExpr->set_arg( mutateExpression( addrExpr->get_arg() ) );
 | 
|---|
| 1347 |                         if ( polytype || needs ) {
 | 
|---|
| 1348 |                                 Expression *ret = addrExpr->get_arg();
 | 
|---|
| 1349 |                                 delete ret->get_results().front();
 | 
|---|
| 1350 |                                 ret->get_results().front() = addrExpr->get_results().front()->clone();
 | 
|---|
| 1351 |                                 addrExpr->set_arg( 0 );
 | 
|---|
| 1352 |                                 delete addrExpr;
 | 
|---|
| 1353 |                                 return ret;
 | 
|---|
| 1354 |                         } else {
 | 
|---|
| 1355 |                                 return addrExpr;
 | 
|---|
| 1356 |                         } // if
 | 
|---|
| 1357 |                 }
 | 
|---|
| 1358 | 
 | 
|---|
| 1359 |                 /// Wraps a function declaration in a new pointer-to-function variable expression
 | 
|---|
| 1360 |                 VariableExpr *wrapFunctionDecl( DeclarationWithType *functionDecl ) {
 | 
|---|
| 1361 |                         // line below cloned from FixFunction.cc
 | 
|---|
| 1362 |                         // xxx - functionObj is never added to a list of declarations...
 | 
|---|
| 1363 |                         ObjectDecl *functionObj = new ObjectDecl( functionDecl->get_name(), functionDecl->get_storageClass(), functionDecl->get_linkage(), 0,
 | 
|---|
| 1364 |                                                                   new PointerType( Type::Qualifiers(), functionDecl->get_type()->clone() ), 0 );
 | 
|---|
| 1365 |                         functionObj->set_mangleName( functionDecl->get_mangleName() );
 | 
|---|
| 1366 |                         functionObj->set_scopeLevel( functionDecl->get_scopeLevel() );
 | 
|---|
| 1367 |                         return new VariableExpr( functionObj );
 | 
|---|
| 1368 |                 }
 | 
|---|
| 1369 | 
 | 
|---|
| 1370 |                 /// Finds the operation declaration for a given type in one of the two maps
 | 
|---|
| 1371 |                 DeclarationWithType* findOpForType( Type *formalType, const ScopedMap< std::string, DeclarationWithType* >& ops, ResolvExpr::TypeMap< DeclarationWithType >& scopedOps ) {
 | 
|---|
| 1372 |                         if ( TypeInstType *formalTypeInstType = dynamic_cast< TypeInstType* >( formalType ) ) {
 | 
|---|
| 1373 |                                 ScopedMap< std::string, DeclarationWithType *>::const_iterator opIt = ops.find( formalTypeInstType->get_name() );
 | 
|---|
| 1374 |                                 return opIt == ops.end() ? 0 : opIt->second;
 | 
|---|
| 1375 |                         } else {
 | 
|---|
| 1376 |                                 return scopedOps.find( formalType );
 | 
|---|
| 1377 |                         }
 | 
|---|
| 1378 |                 }
 | 
|---|
| 1379 | 
 | 
|---|
| 1380 |                 /// Adds an assertion parameter to the application expression for the actual assertion declaration valued with the assert op
 | 
|---|
| 1381 |                 void addAssertionFor( ApplicationExpr *appExpr, DeclarationWithType *actualDecl, DeclarationWithType *assertOp ) {
 | 
|---|
| 1382 |                         appExpr->get_inferParams()[ actualDecl->get_uniqueId() ]
 | 
|---|
| 1383 |                                         = ParamEntry( assertOp->get_uniqueId(), assertOp->get_type()->clone(), actualDecl->get_type()->clone(), wrapFunctionDecl( assertOp ) );
 | 
|---|
| 1384 |                 }
 | 
|---|
| 1385 | 
 | 
|---|
| 1386 |                 Statement * Pass1::mutate( ReturnStmt *returnStmt ) {
 | 
|---|
| 1387 |                         if ( retval && returnStmt->get_expr() ) {
 | 
|---|
| 1388 |                                 assert( ! returnStmt->get_expr()->get_results().empty() );
 | 
|---|
| 1389 |                                 // ***** Code Removal ***** After introducing a temporary variable for all return expressions, the following code appears superfluous.
 | 
|---|
| 1390 |                                 // if ( returnStmt->get_expr()->get_results().front()->get_isLvalue() ) {
 | 
|---|
| 1391 |                                 // by this point, a cast expr on a polymorphic return value is redundant
 | 
|---|
| 1392 |                                 while ( CastExpr *castExpr = dynamic_cast< CastExpr *>( returnStmt->get_expr() ) ) {
 | 
|---|
| 1393 |                                         returnStmt->set_expr( castExpr->get_arg() );
 | 
|---|
| 1394 |                                         returnStmt->get_expr()->set_env( castExpr->get_env() );
 | 
|---|
| 1395 |                                         castExpr->set_env( 0 );
 | 
|---|
| 1396 |                                         castExpr->set_arg( 0 );
 | 
|---|
| 1397 |                                         delete castExpr;
 | 
|---|
| 1398 |                                 } //while
 | 
|---|
| 1399 | 
 | 
|---|
| 1400 |                                 // find assignment operator for (polymorphic) return type
 | 
|---|
| 1401 |                                 ApplicationExpr *assignExpr = 0;
 | 
|---|
| 1402 |                                 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( retval->get_type() ) ) {
 | 
|---|
| 1403 |                                         // find assignment operator for type variable
 | 
|---|
| 1404 |                                         ScopedMap< std::string, DeclarationWithType *>::const_iterator assignIter = assignOps.find( typeInst->get_name() );
 | 
|---|
| 1405 |                                         if ( assignIter == assignOps.end() ) {
 | 
|---|
| 1406 |                                                 throw SemanticError( "Attempt to return dtype or ftype object in ", returnStmt->get_expr() );
 | 
|---|
| 1407 |                                         } // if
 | 
|---|
| 1408 |                                         assignExpr = new ApplicationExpr( new VariableExpr( assignIter->second ) );
 | 
|---|
| 1409 |                                 } else if ( ReferenceToType *refType = dynamic_cast< ReferenceToType *>( retval->get_type() ) ) {
 | 
|---|
| 1410 |                                         // find assignment operator for generic type
 | 
|---|
| 1411 |                                         DeclarationWithType *functionDecl = scopedAssignOps.find( refType );
 | 
|---|
| 1412 |                                         if ( ! functionDecl ) {
 | 
|---|
| 1413 |                                                 throw SemanticError( "Attempt to return dtype or ftype generic object in ", returnStmt->get_expr() );
 | 
|---|
| 1414 |                                         }
 | 
|---|
| 1415 | 
 | 
|---|
| 1416 |                                         // wrap it up in an application expression
 | 
|---|
| 1417 |                                         assignExpr = new ApplicationExpr( wrapFunctionDecl( functionDecl ) );
 | 
|---|
| 1418 |                                         assignExpr->set_env( env->clone() );
 | 
|---|
| 1419 | 
 | 
|---|
| 1420 |                                         // find each of its needed secondary assignment operators
 | 
|---|
| 1421 |                                         std::list< Expression* > &tyParams = refType->get_parameters();
 | 
|---|
| 1422 |                                         std::list< TypeDecl* > &forallParams = functionDecl->get_type()->get_forall();
 | 
|---|
| 1423 |                                         std::list< Expression* >::const_iterator tyIt = tyParams.begin();
 | 
|---|
| 1424 |                                         std::list< TypeDecl* >::const_iterator forallIt = forallParams.begin();
 | 
|---|
| 1425 |                                         for ( ; tyIt != tyParams.end() && forallIt != forallParams.end(); ++tyIt, ++forallIt ) {
 | 
|---|
| 1426 |                                                 // Add appropriate mapping to assignment expression environment
 | 
|---|
| 1427 |                                                 TypeExpr *formalTypeExpr = dynamic_cast< TypeExpr* >( *tyIt );
 | 
|---|
| 1428 |                                                 assert( formalTypeExpr && "type parameters must be type expressions" );
 | 
|---|
| 1429 |                                                 Type *formalType = formalTypeExpr->get_type();
 | 
|---|
| 1430 |                                                 assignExpr->get_env()->add( (*forallIt)->get_name(), formalType );
 | 
|---|
| 1431 | 
 | 
|---|
| 1432 |                                                 // skip non-otype parameters (ftype/dtype)
 | 
|---|
| 1433 |                                                 if ( (*forallIt)->get_kind() != TypeDecl::Any ) continue;
 | 
|---|
| 1434 | 
 | 
|---|
| 1435 |                                                 // find otype operators for formal type
 | 
|---|
| 1436 |                                                 DeclarationWithType *assertAssign = findOpForType( formalType, assignOps, scopedAssignOps );
 | 
|---|
| 1437 |                                                 if ( ! assertAssign ) throw SemanticError( "No assignment operation found for ", formalType );
 | 
|---|
| 1438 | 
 | 
|---|
| 1439 |                                                 DeclarationWithType *assertCtor = findOpForType( formalType, ctorOps, scopedCtorOps );
 | 
|---|
| 1440 |                                                 if ( ! assertCtor ) throw SemanticError( "No default constructor found for ", formalType );
 | 
|---|
| 1441 | 
 | 
|---|
| 1442 |                                                 DeclarationWithType *assertCopy = findOpForType( formalType, copyOps, scopedCopyOps );
 | 
|---|
| 1443 |                                                 if ( ! assertCopy ) throw SemanticError( "No copy constructor found for ", formalType );
 | 
|---|
| 1444 | 
 | 
|---|
| 1445 |                                                 DeclarationWithType *assertDtor = findOpForType( formalType, dtorOps, scopedDtorOps );
 | 
|---|
| 1446 |                                                 if ( ! assertDtor ) throw SemanticError( "No destructor found for ", formalType );
 | 
|---|
| 1447 | 
 | 
|---|
| 1448 |                                                 // add inferred parameters for otype operators to assignment expression
 | 
|---|
| 1449 |                                                 // NOTE: Code here assumes that first four assertions are assign op, ctor, copy ctor, dtor, in that order
 | 
|---|
| 1450 |                                                 std::list< DeclarationWithType* > &asserts = (*forallIt)->get_assertions();
 | 
|---|
| 1451 |                                                 assert( asserts.size() >= 4 && "Type param needs otype operator assertions" );
 | 
|---|
| 1452 | 
 | 
|---|
| 1453 |                                                 std::list< DeclarationWithType* >::iterator actualIt = asserts.begin();
 | 
|---|
| 1454 |                                                 addAssertionFor( assignExpr, *actualIt, assertAssign );
 | 
|---|
| 1455 |                                                 ++actualIt;
 | 
|---|
| 1456 |                                                 addAssertionFor( assignExpr, *actualIt, assertCtor );
 | 
|---|
| 1457 |                                                 ++actualIt;
 | 
|---|
| 1458 |                                                 addAssertionFor( assignExpr, *actualIt, assertCopy );
 | 
|---|
| 1459 |                                                 ++actualIt;
 | 
|---|
| 1460 |                                                 addAssertionFor( assignExpr, *actualIt, assertDtor );
 | 
|---|
| 1461 | 
 | 
|---|
| 1462 |                                         }
 | 
|---|
| 1463 |                                 }
 | 
|---|
| 1464 |                                 assert( assignExpr );
 | 
|---|
| 1465 | 
 | 
|---|
| 1466 |                                 // replace return statement with appropriate assignment to out parameter
 | 
|---|
| 1467 |                                 Expression *retParm = new NameExpr( retval->get_name() );
 | 
|---|
| 1468 |                                 retParm->get_results().push_back( new PointerType( Type::Qualifiers(), retval->get_type()->clone() ) );
 | 
|---|
| 1469 |                                 assignExpr->get_args().push_back( retParm );
 | 
|---|
| 1470 |                                 assignExpr->get_args().push_back( returnStmt->get_expr() );
 | 
|---|
| 1471 |                                 stmtsToAdd.push_back( new ExprStmt( noLabels, mutateExpression( assignExpr ) ) );
 | 
|---|
| 1472 |                                 // } else {
 | 
|---|
| 1473 |                                 //      useRetval = true;
 | 
|---|
| 1474 |                                 //      stmtsToAdd.push_back( new ExprStmt( noLabels, mutateExpression( returnStmt->get_expr() ) ) );
 | 
|---|
| 1475 |                                 //      useRetval = false;
 | 
|---|
| 1476 |                                 // } // if
 | 
|---|
| 1477 |                                 returnStmt->set_expr( 0 );
 | 
|---|
| 1478 |                         } else {
 | 
|---|
| 1479 |                                 returnStmt->set_expr( mutateExpression( returnStmt->get_expr() ) );
 | 
|---|
| 1480 |                         } // if
 | 
|---|
| 1481 |                         return returnStmt;
 | 
|---|
| 1482 |                 }
 | 
|---|
| 1483 | 
 | 
|---|
| 1484 |                 Type * Pass1::mutate( PointerType *pointerType ) {
 | 
|---|
| 1485 |                         scopeTyVars.beginScope();
 | 
|---|
| 1486 |                         makeTyVarMap( pointerType, scopeTyVars );
 | 
|---|
| 1487 | 
 | 
|---|
| 1488 |                         Type *ret = Mutator::mutate( pointerType );
 | 
|---|
| 1489 | 
 | 
|---|
| 1490 |                         scopeTyVars.endScope();
 | 
|---|
| 1491 |                         return ret;
 | 
|---|
| 1492 |                 }
 | 
|---|
| 1493 | 
 | 
|---|
| 1494 |                 Type * Pass1::mutate( FunctionType *functionType ) {
 | 
|---|
| 1495 |                         scopeTyVars.beginScope();
 | 
|---|
| 1496 |                         makeTyVarMap( functionType, scopeTyVars );
 | 
|---|
| 1497 | 
 | 
|---|
| 1498 |                         Type *ret = Mutator::mutate( functionType );
 | 
|---|
| 1499 | 
 | 
|---|
| 1500 |                         scopeTyVars.endScope();
 | 
|---|
| 1501 |                         return ret;
 | 
|---|
| 1502 |                 }
 | 
|---|
| 1503 | 
 | 
|---|
| 1504 |                 void Pass1::doBeginScope() {
 | 
|---|
| 1505 |                         adapters.beginScope();
 | 
|---|
| 1506 |                         scopedAssignOps.beginScope();
 | 
|---|
| 1507 |                         scopedCtorOps.beginScope();
 | 
|---|
| 1508 |                         scopedCopyOps.beginScope();
 | 
|---|
| 1509 |                         scopedDtorOps.beginScope();
 | 
|---|
| 1510 |                 }
 | 
|---|
| 1511 | 
 | 
|---|
| 1512 |                 void Pass1::doEndScope() {
 | 
|---|
| 1513 |                         adapters.endScope();
 | 
|---|
| 1514 |                         scopedAssignOps.endScope();
 | 
|---|
| 1515 |                         scopedCtorOps.endScope();
 | 
|---|
| 1516 |                         scopedCopyOps.endScope();
 | 
|---|
| 1517 |                         scopedDtorOps.endScope();
 | 
|---|
| 1518 |                 }
 | 
|---|
| 1519 | 
 | 
|---|
| 1520 | ////////////////////////////////////////// Pass2 ////////////////////////////////////////////////////
 | 
|---|
| 1521 | 
 | 
|---|
| 1522 |                 void Pass2::addAdapters( FunctionType *functionType ) {
 | 
|---|
| 1523 |                         std::list< DeclarationWithType *> ¶mList = functionType->get_parameters();
 | 
|---|
| 1524 |                         std::list< FunctionType *> functions;
 | 
|---|
| 1525 |                         for ( std::list< DeclarationWithType *>::iterator arg = paramList.begin(); arg != paramList.end(); ++arg ) {
 | 
|---|
| 1526 |                                 Type *orig = (*arg)->get_type();
 | 
|---|
| 1527 |                                 findAndReplaceFunction( orig, functions, scopeTyVars, needsAdapter );
 | 
|---|
| 1528 |                                 (*arg)->set_type( orig );
 | 
|---|
| 1529 |                         }
 | 
|---|
| 1530 |                         std::set< std::string > adaptersDone;
 | 
|---|
| 1531 |                         for ( std::list< FunctionType *>::iterator funType = functions.begin(); funType != functions.end(); ++funType ) {
 | 
|---|
| 1532 |                                 std::string mangleName = mangleAdapterName( *funType, scopeTyVars );
 | 
|---|
| 1533 |                                 if ( adaptersDone.find( mangleName ) == adaptersDone.end() ) {
 | 
|---|
| 1534 |                                         std::string adapterName = makeAdapterName( mangleName );
 | 
|---|
| 1535 |                                         paramList.push_front( new ObjectDecl( adapterName, DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new PointerType( Type::Qualifiers(), makeAdapterType( *funType, scopeTyVars ) ), 0 ) );
 | 
|---|
| 1536 |                                         adaptersDone.insert( adaptersDone.begin(), mangleName );
 | 
|---|
| 1537 |                                 }
 | 
|---|
| 1538 |                         }
 | 
|---|
| 1539 | //  deleteAll( functions );
 | 
|---|
| 1540 |                 }
 | 
|---|
| 1541 | 
 | 
|---|
| 1542 |                 template< typename DeclClass >
 | 
|---|
| 1543 |                 DeclClass * Pass2::handleDecl( DeclClass *decl, Type *type ) {
 | 
|---|
| 1544 |                         DeclClass *ret = static_cast< DeclClass *>( Mutator::mutate( decl ) );
 | 
|---|
| 1545 | 
 | 
|---|
| 1546 |                         return ret;
 | 
|---|
| 1547 |                 }
 | 
|---|
| 1548 | 
 | 
|---|
| 1549 |                 DeclarationWithType * Pass2::mutate( FunctionDecl *functionDecl ) {
 | 
|---|
| 1550 |                         return handleDecl( functionDecl, functionDecl->get_functionType() );
 | 
|---|
| 1551 |                 }
 | 
|---|
| 1552 | 
 | 
|---|
| 1553 |                 ObjectDecl * Pass2::mutate( ObjectDecl *objectDecl ) {
 | 
|---|
| 1554 |                         return handleDecl( objectDecl, objectDecl->get_type() );
 | 
|---|
| 1555 |                 }
 | 
|---|
| 1556 | 
 | 
|---|
| 1557 |                 TypeDecl * Pass2::mutate( TypeDecl *typeDecl ) {
 | 
|---|
| 1558 |                         scopeTyVars[ typeDecl->get_name() ] = typeDecl->get_kind();
 | 
|---|
| 1559 |                         if ( typeDecl->get_base() ) {
 | 
|---|
| 1560 |                                 return handleDecl( typeDecl, typeDecl->get_base() );
 | 
|---|
| 1561 |                         } else {
 | 
|---|
| 1562 |                                 return Mutator::mutate( typeDecl );
 | 
|---|
| 1563 |                         }
 | 
|---|
| 1564 |                 }
 | 
|---|
| 1565 | 
 | 
|---|
| 1566 |                 TypedefDecl * Pass2::mutate( TypedefDecl *typedefDecl ) {
 | 
|---|
| 1567 |                         return handleDecl( typedefDecl, typedefDecl->get_base() );
 | 
|---|
| 1568 |                 }
 | 
|---|
| 1569 | 
 | 
|---|
| 1570 |                 Type * Pass2::mutate( PointerType *pointerType ) {
 | 
|---|
| 1571 |                         scopeTyVars.beginScope();
 | 
|---|
| 1572 |                         makeTyVarMap( pointerType, scopeTyVars );
 | 
|---|
| 1573 | 
 | 
|---|
| 1574 |                         Type *ret = Mutator::mutate( pointerType );
 | 
|---|
| 1575 | 
 | 
|---|
| 1576 |                         scopeTyVars.endScope();
 | 
|---|
| 1577 |                         return ret;
 | 
|---|
| 1578 |                 }
 | 
|---|
| 1579 | 
 | 
|---|
| 1580 |                 Type *Pass2::mutate( FunctionType *funcType ) {
 | 
|---|
| 1581 |                         scopeTyVars.beginScope();
 | 
|---|
| 1582 |                         makeTyVarMap( funcType, scopeTyVars );
 | 
|---|
| 1583 | 
 | 
|---|
| 1584 |                         // move polymorphic return type to parameter list
 | 
|---|
| 1585 |                         if ( isDynRet( funcType ) ) {
 | 
|---|
| 1586 |                                 DeclarationWithType *ret = funcType->get_returnVals().front();
 | 
|---|
| 1587 |                                 ret->set_type( new PointerType( Type::Qualifiers(), ret->get_type() ) );
 | 
|---|
| 1588 |                                 funcType->get_parameters().push_front( ret );
 | 
|---|
| 1589 |                                 funcType->get_returnVals().pop_front();
 | 
|---|
| 1590 |                         }
 | 
|---|
| 1591 | 
 | 
|---|
| 1592 |                         // add size/align and assertions for type parameters to parameter list
 | 
|---|
| 1593 |                         std::list< DeclarationWithType *>::iterator last = funcType->get_parameters().begin();
 | 
|---|
| 1594 |                         std::list< DeclarationWithType *> inferredParams;
 | 
|---|
| 1595 |                         ObjectDecl newObj( "", DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), 0 );
 | 
|---|
| 1596 |                         ObjectDecl newPtr( "", DeclarationNode::NoStorageClass, LinkageSpec::C, 0,
 | 
|---|
| 1597 |                                            new PointerType( Type::Qualifiers(), new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ) ), 0 );
 | 
|---|
| 1598 |                         for ( std::list< TypeDecl *>::const_iterator tyParm = funcType->get_forall().begin(); tyParm != funcType->get_forall().end(); ++tyParm ) {
 | 
|---|
| 1599 |                                 ObjectDecl *sizeParm, *alignParm;
 | 
|---|
| 1600 |                                 // add all size and alignment parameters to parameter list
 | 
|---|
| 1601 |                                 if ( (*tyParm)->get_kind() == TypeDecl::Any ) {
 | 
|---|
| 1602 |                                         TypeInstType parmType( Type::Qualifiers(), (*tyParm)->get_name(), *tyParm );
 | 
|---|
| 1603 |                                         std::string parmName = mangleType( &parmType );
 | 
|---|
| 1604 | 
 | 
|---|
| 1605 |                                         sizeParm = newObj.clone();
 | 
|---|
| 1606 |                                         sizeParm->set_name( sizeofName( parmName ) );
 | 
|---|
| 1607 |                                         last = funcType->get_parameters().insert( last, sizeParm );
 | 
|---|
| 1608 |                                         ++last;
 | 
|---|
| 1609 | 
 | 
|---|
| 1610 |                                         alignParm = newObj.clone();
 | 
|---|
| 1611 |                                         alignParm->set_name( alignofName( parmName ) );
 | 
|---|
| 1612 |                                         last = funcType->get_parameters().insert( last, alignParm );
 | 
|---|
| 1613 |                                         ++last;
 | 
|---|
| 1614 |                                 }
 | 
|---|
| 1615 |                                 // move all assertions into parameter list
 | 
|---|
| 1616 |                                 for ( std::list< DeclarationWithType *>::iterator assert = (*tyParm)->get_assertions().begin(); assert != (*tyParm)->get_assertions().end(); ++assert ) {
 | 
|---|
| 1617 | //      *assert = (*assert)->acceptMutator( *this );
 | 
|---|
| 1618 |                                         inferredParams.push_back( *assert );
 | 
|---|
| 1619 |                                 }
 | 
|---|
| 1620 |                                 (*tyParm)->get_assertions().clear();
 | 
|---|
| 1621 |                         }
 | 
|---|
| 1622 | 
 | 
|---|
| 1623 |                         // add size/align for generic parameter types to parameter list
 | 
|---|
| 1624 |                         std::set< std::string > seenTypes; // sizeofName for generic types we've seen
 | 
|---|
| 1625 |                         for ( std::list< DeclarationWithType* >::const_iterator fnParm = last; fnParm != funcType->get_parameters().end(); ++fnParm ) {
 | 
|---|
| 1626 |                                 Type *polyType = isPolyType( (*fnParm)->get_type(), scopeTyVars );
 | 
|---|
| 1627 |                                 if ( polyType && ! dynamic_cast< TypeInstType* >( polyType ) ) {
 | 
|---|
| 1628 |                                         std::string typeName = mangleType( polyType );
 | 
|---|
| 1629 |                                         if ( seenTypes.count( typeName ) ) continue;
 | 
|---|
| 1630 | 
 | 
|---|
| 1631 |                                         ObjectDecl *sizeParm, *alignParm, *offsetParm;
 | 
|---|
| 1632 |                                         sizeParm = newObj.clone();
 | 
|---|
| 1633 |                                         sizeParm->set_name( sizeofName( typeName ) );
 | 
|---|
| 1634 |                                         last = funcType->get_parameters().insert( last, sizeParm );
 | 
|---|
| 1635 |                                         ++last;
 | 
|---|
| 1636 | 
 | 
|---|
| 1637 |                                         alignParm = newObj.clone();
 | 
|---|
| 1638 |                                         alignParm->set_name( alignofName( typeName ) );
 | 
|---|
| 1639 |                                         last = funcType->get_parameters().insert( last, alignParm );
 | 
|---|
| 1640 |                                         ++last;
 | 
|---|
| 1641 | 
 | 
|---|
| 1642 |                                         if ( StructInstType *polyBaseStruct = dynamic_cast< StructInstType* >( polyType ) ) {
 | 
|---|
| 1643 |                                                 // NOTE zero-length arrays are illegal in C, so empty structs have no offset array
 | 
|---|
| 1644 |                                                 if ( ! polyBaseStruct->get_baseStruct()->get_members().empty() ) {
 | 
|---|
| 1645 |                                                         offsetParm = newPtr.clone();
 | 
|---|
| 1646 |                                                         offsetParm->set_name( offsetofName( typeName ) );
 | 
|---|
| 1647 |                                                         last = funcType->get_parameters().insert( last, offsetParm );
 | 
|---|
| 1648 |                                                         ++last;
 | 
|---|
| 1649 |                                                 }
 | 
|---|
| 1650 |                                         }
 | 
|---|
| 1651 | 
 | 
|---|
| 1652 |                                         seenTypes.insert( typeName );
 | 
|---|
| 1653 |                                 }
 | 
|---|
| 1654 |                         }
 | 
|---|
| 1655 | 
 | 
|---|
| 1656 |                         // splice assertion parameters into parameter list
 | 
|---|
| 1657 |                         funcType->get_parameters().splice( last, inferredParams );
 | 
|---|
| 1658 |                         addAdapters( funcType );
 | 
|---|
| 1659 |                         mutateAll( funcType->get_returnVals(), *this );
 | 
|---|
| 1660 |                         mutateAll( funcType->get_parameters(), *this );
 | 
|---|
| 1661 | 
 | 
|---|
| 1662 |                         scopeTyVars.endScope();
 | 
|---|
| 1663 |                         return funcType;
 | 
|---|
| 1664 |                 }
 | 
|---|
| 1665 | 
 | 
|---|
| 1666 | ////////////////////////////////////////// PolyGenericCalculator ////////////////////////////////////////////////////
 | 
|---|
| 1667 | 
 | 
|---|
| 1668 |                 void PolyGenericCalculator::beginTypeScope( Type *ty ) {
 | 
|---|
| 1669 |                         scopeTyVars.beginScope();
 | 
|---|
| 1670 |                         makeTyVarMap( ty, scopeTyVars );
 | 
|---|
| 1671 |                 }
 | 
|---|
| 1672 | 
 | 
|---|
| 1673 |                 void PolyGenericCalculator::endTypeScope() {
 | 
|---|
| 1674 |                         scopeTyVars.endScope();
 | 
|---|
| 1675 |                 }
 | 
|---|
| 1676 | 
 | 
|---|
| 1677 |                 template< typename DeclClass >
 | 
|---|
| 1678 |                 DeclClass * PolyGenericCalculator::handleDecl( DeclClass *decl, Type *type ) {
 | 
|---|
| 1679 |                         beginTypeScope( type );
 | 
|---|
| 1680 |                         // knownLayouts.beginScope();
 | 
|---|
| 1681 |                         // knownOffsets.beginScope();
 | 
|---|
| 1682 | 
 | 
|---|
| 1683 |                         DeclClass *ret = static_cast< DeclClass *>( Parent::mutate( decl ) );
 | 
|---|
| 1684 | 
 | 
|---|
| 1685 |                         // knownOffsets.endScope();
 | 
|---|
| 1686 |                         // knownLayouts.endScope();
 | 
|---|
| 1687 |                         endTypeScope();
 | 
|---|
| 1688 |                         return ret;
 | 
|---|
| 1689 |                 }
 | 
|---|
| 1690 | 
 | 
|---|
| 1691 |                 ObjectDecl * PolyGenericCalculator::mutate( ObjectDecl *objectDecl ) {
 | 
|---|
| 1692 |                         return handleDecl( objectDecl, objectDecl->get_type() );
 | 
|---|
| 1693 |                 }
 | 
|---|
| 1694 | 
 | 
|---|
| 1695 |                 DeclarationWithType * PolyGenericCalculator::mutate( FunctionDecl *functionDecl ) {
 | 
|---|
| 1696 |                         knownLayouts.beginScope();
 | 
|---|
| 1697 |                         knownOffsets.beginScope();
 | 
|---|
| 1698 | 
 | 
|---|
| 1699 |                         DeclarationWithType * decl = handleDecl( functionDecl, functionDecl->get_functionType() );
 | 
|---|
| 1700 |                         knownOffsets.endScope();
 | 
|---|
| 1701 |                         knownLayouts.endScope();
 | 
|---|
| 1702 |                         return decl;
 | 
|---|
| 1703 |                 }
 | 
|---|
| 1704 | 
 | 
|---|
| 1705 |                 TypedefDecl * PolyGenericCalculator::mutate( TypedefDecl *typedefDecl ) {
 | 
|---|
| 1706 |                         return handleDecl( typedefDecl, typedefDecl->get_base() );
 | 
|---|
| 1707 |                 }
 | 
|---|
| 1708 | 
 | 
|---|
| 1709 |                 TypeDecl * PolyGenericCalculator::mutate( TypeDecl *typeDecl ) {
 | 
|---|
| 1710 |                         scopeTyVars[ typeDecl->get_name() ] = typeDecl->get_kind();
 | 
|---|
| 1711 |                         return Parent::mutate( typeDecl );
 | 
|---|
| 1712 |                 }
 | 
|---|
| 1713 | 
 | 
|---|
| 1714 |                 Type * PolyGenericCalculator::mutate( PointerType *pointerType ) {
 | 
|---|
| 1715 |                         beginTypeScope( pointerType );
 | 
|---|
| 1716 | 
 | 
|---|
| 1717 |                         Type *ret = Parent::mutate( pointerType );
 | 
|---|
| 1718 | 
 | 
|---|
| 1719 |                         endTypeScope();
 | 
|---|
| 1720 |                         return ret;
 | 
|---|
| 1721 |                 }
 | 
|---|
| 1722 | 
 | 
|---|
| 1723 |                 Type * PolyGenericCalculator::mutate( FunctionType *funcType ) {
 | 
|---|
| 1724 |                         beginTypeScope( funcType );
 | 
|---|
| 1725 | 
 | 
|---|
| 1726 |                         // make sure that any type information passed into the function is accounted for
 | 
|---|
| 1727 |                         for ( std::list< DeclarationWithType* >::const_iterator fnParm = funcType->get_parameters().begin(); fnParm != funcType->get_parameters().end(); ++fnParm ) {
 | 
|---|
| 1728 |                                 // condition here duplicates that in Pass2::mutate( FunctionType* )
 | 
|---|
| 1729 |                                 Type *polyType = isPolyType( (*fnParm)->get_type(), scopeTyVars );
 | 
|---|
| 1730 |                                 if ( polyType && ! dynamic_cast< TypeInstType* >( polyType ) ) {
 | 
|---|
| 1731 |                                         knownLayouts.insert( mangleType( polyType ) );
 | 
|---|
| 1732 |                                 }
 | 
|---|
| 1733 |                         }
 | 
|---|
| 1734 | 
 | 
|---|
| 1735 |                         Type *ret = Parent::mutate( funcType );
 | 
|---|
| 1736 | 
 | 
|---|
| 1737 |                         endTypeScope();
 | 
|---|
| 1738 |                         return ret;
 | 
|---|
| 1739 |                 }
 | 
|---|
| 1740 | 
 | 
|---|
| 1741 |                 Statement *PolyGenericCalculator::mutate( DeclStmt *declStmt ) {
 | 
|---|
| 1742 |                         if ( ObjectDecl *objectDecl = dynamic_cast< ObjectDecl *>( declStmt->get_decl() ) ) {
 | 
|---|
| 1743 |                                 if ( findGeneric( objectDecl->get_type() ) ) {
 | 
|---|
| 1744 |                                         // change initialization of a polymorphic value object
 | 
|---|
| 1745 |                                         // to allocate storage with alloca
 | 
|---|
| 1746 |                                         Type *declType = objectDecl->get_type();
 | 
|---|
| 1747 |                                         UntypedExpr *alloc = new UntypedExpr( new NameExpr( "__builtin_alloca" ) );
 | 
|---|
| 1748 |                                         alloc->get_args().push_back( new NameExpr( sizeofName( mangleType( declType ) ) ) );
 | 
|---|
| 1749 | 
 | 
|---|
| 1750 |                                         delete objectDecl->get_init();
 | 
|---|
| 1751 | 
 | 
|---|
| 1752 |                                         std::list<Expression*> designators;
 | 
|---|
| 1753 |                                         objectDecl->set_init( new SingleInit( alloc, designators, false ) ); // not constructed
 | 
|---|
| 1754 |                                 }
 | 
|---|
| 1755 |                         }
 | 
|---|
| 1756 |                         return Parent::mutate( declStmt );
 | 
|---|
| 1757 |                 }
 | 
|---|
| 1758 | 
 | 
|---|
| 1759 |                 /// Finds the member in the base list that matches the given declaration; returns its index, or -1 if not present
 | 
|---|
| 1760 |                 long findMember( DeclarationWithType *memberDecl, std::list< Declaration* > &baseDecls ) {
 | 
|---|
| 1761 |                         long i = 0;
 | 
|---|
| 1762 |                         for(std::list< Declaration* >::const_iterator decl = baseDecls.begin(); decl != baseDecls.end(); ++decl, ++i ) {
 | 
|---|
| 1763 |                                 if ( memberDecl->get_name() != (*decl)->get_name() ) continue;
 | 
|---|
| 1764 | 
 | 
|---|
| 1765 |                                 if ( DeclarationWithType *declWithType = dynamic_cast< DeclarationWithType* >( *decl ) ) {
 | 
|---|
| 1766 |                                         if ( memberDecl->get_mangleName().empty() || declWithType->get_mangleName().empty()
 | 
|---|
| 1767 |                                              || memberDecl->get_mangleName() == declWithType->get_mangleName() ) return i;
 | 
|---|
| 1768 |                                         else continue;
 | 
|---|
| 1769 |                                 } else return i;
 | 
|---|
| 1770 |                         }
 | 
|---|
| 1771 |                         return -1;
 | 
|---|
| 1772 |                 }
 | 
|---|
| 1773 | 
 | 
|---|
| 1774 |                 /// Returns an index expression into the offset array for a type
 | 
|---|
| 1775 |                 Expression *makeOffsetIndex( Type *objectType, long i ) {
 | 
|---|
| 1776 |                         std::stringstream offset_namer;
 | 
|---|
| 1777 |                         offset_namer << i;
 | 
|---|
| 1778 |                         ConstantExpr *fieldIndex = new ConstantExpr( Constant( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), offset_namer.str() ) );
 | 
|---|
| 1779 |                         UntypedExpr *fieldOffset = new UntypedExpr( new NameExpr( "?[?]" ) );
 | 
|---|
| 1780 |                         fieldOffset->get_args().push_back( new NameExpr( offsetofName( mangleType( objectType ) ) ) );
 | 
|---|
| 1781 |                         fieldOffset->get_args().push_back( fieldIndex );
 | 
|---|
| 1782 |                         return fieldOffset;
 | 
|---|
| 1783 |                 }
 | 
|---|
| 1784 | 
 | 
|---|
| 1785 |                 /// Returns an expression dereferenced n times
 | 
|---|
| 1786 |                 Expression *makeDerefdVar( Expression *derefdVar, long n ) {
 | 
|---|
| 1787 |                         for ( int i = 1; i < n; ++i ) {
 | 
|---|
| 1788 |                                 UntypedExpr *derefExpr = new UntypedExpr( new NameExpr( "*?" ) );
 | 
|---|
| 1789 |                                 derefExpr->get_args().push_back( derefdVar );
 | 
|---|
| 1790 |                                 // xxx - should set results on derefExpr
 | 
|---|
| 1791 |                                 derefdVar = derefExpr;
 | 
|---|
| 1792 |                         }
 | 
|---|
| 1793 |                         return derefdVar;
 | 
|---|
| 1794 |                 }
 | 
|---|
| 1795 | 
 | 
|---|
| 1796 |                 Expression *PolyGenericCalculator::mutate( MemberExpr *memberExpr ) {
 | 
|---|
| 1797 |                         // mutate, exiting early if no longer MemberExpr
 | 
|---|
| 1798 |                         Expression *expr = Parent::mutate( memberExpr );
 | 
|---|
| 1799 |                         memberExpr = dynamic_cast< MemberExpr* >( expr );
 | 
|---|
| 1800 |                         if ( ! memberExpr ) return expr;
 | 
|---|
| 1801 | 
 | 
|---|
| 1802 |                         // get declaration for base struct, exiting early if not found
 | 
|---|
| 1803 |                         int varDepth;
 | 
|---|
| 1804 |                         VariableExpr *varExpr = getBaseVar( memberExpr->get_aggregate(), &varDepth );
 | 
|---|
| 1805 |                         if ( ! varExpr ) return memberExpr;
 | 
|---|
| 1806 |                         ObjectDecl *objectDecl = dynamic_cast< ObjectDecl* >( varExpr->get_var() );
 | 
|---|
| 1807 |                         if ( ! objectDecl ) return memberExpr;
 | 
|---|
| 1808 | 
 | 
|---|
| 1809 |                         // only mutate member expressions for polymorphic types
 | 
|---|
| 1810 |                         int tyDepth;
 | 
|---|
| 1811 |                         Type *objectType = hasPolyBase( objectDecl->get_type(), scopeTyVars, &tyDepth );
 | 
|---|
| 1812 |                         if ( ! objectType ) return memberExpr;
 | 
|---|
| 1813 |                         findGeneric( objectType ); // ensure layout for this type is available
 | 
|---|
| 1814 | 
 | 
|---|
| 1815 |                         // replace member expression with dynamically-computed layout expression
 | 
|---|
| 1816 |                         Expression *newMemberExpr = 0;
 | 
|---|
| 1817 |                         if ( StructInstType *structType = dynamic_cast< StructInstType* >( objectType ) ) {
 | 
|---|
| 1818 |                                 // look up offset index
 | 
|---|
| 1819 |                                 long i = findMember( memberExpr->get_member(), structType->get_baseStruct()->get_members() );
 | 
|---|
| 1820 |                                 if ( i == -1 ) return memberExpr;
 | 
|---|
| 1821 | 
 | 
|---|
| 1822 |                                 // replace member expression with pointer to base plus offset
 | 
|---|
| 1823 |                                 UntypedExpr *fieldLoc = new UntypedExpr( new NameExpr( "?+?" ) );
 | 
|---|
| 1824 |                                 fieldLoc->get_args().push_back( makeDerefdVar( varExpr->clone(), varDepth ) );
 | 
|---|
| 1825 |                                 fieldLoc->get_args().push_back( makeOffsetIndex( objectType, i ) );
 | 
|---|
| 1826 |                                 newMemberExpr = fieldLoc;
 | 
|---|
| 1827 |                         } else if ( dynamic_cast< UnionInstType* >( objectType ) ) {
 | 
|---|
| 1828 |                                 // union members are all at offset zero, so build appropriately-dereferenced variable
 | 
|---|
| 1829 |                                 newMemberExpr = makeDerefdVar( varExpr->clone(), varDepth );
 | 
|---|
| 1830 |                         } else return memberExpr;
 | 
|---|
| 1831 |                         assert( newMemberExpr );
 | 
|---|
| 1832 | 
 | 
|---|
| 1833 |                         Type *memberType = memberExpr->get_member()->get_type();
 | 
|---|
| 1834 |                         if ( ! isPolyType( memberType, scopeTyVars ) ) {
 | 
|---|
| 1835 |                                 // Not all members of a polymorphic type are themselves of polymorphic type; in this case the member expression should be wrapped and dereferenced to form an lvalue
 | 
|---|
| 1836 |                                 CastExpr *ptrCastExpr = new CastExpr( newMemberExpr, new PointerType( Type::Qualifiers(), memberType->clone() ) );
 | 
|---|
| 1837 |                                 UntypedExpr *derefExpr = new UntypedExpr( new NameExpr( "*?" ) );
 | 
|---|
| 1838 |                                 derefExpr->get_args().push_back( ptrCastExpr );
 | 
|---|
| 1839 |                                 newMemberExpr = derefExpr;
 | 
|---|
| 1840 |                         }
 | 
|---|
| 1841 | 
 | 
|---|
| 1842 |                         delete memberExpr;
 | 
|---|
| 1843 |                         return newMemberExpr;
 | 
|---|
| 1844 |                 }
 | 
|---|
| 1845 | 
 | 
|---|
| 1846 |                 ObjectDecl *PolyGenericCalculator::makeVar( const std::string &name, Type *type, Initializer *init ) {
 | 
|---|
| 1847 |                         ObjectDecl *newObj = new ObjectDecl( name, DeclarationNode::NoStorageClass, LinkageSpec::C, 0, type, init );
 | 
|---|
| 1848 |                         stmtsToAdd.push_back( new DeclStmt( noLabels, newObj ) );
 | 
|---|
| 1849 |                         return newObj;
 | 
|---|
| 1850 |                 }
 | 
|---|
| 1851 | 
 | 
|---|
| 1852 |                 void PolyGenericCalculator::addOtypeParamsToLayoutCall( UntypedExpr *layoutCall, const std::list< Type* > &otypeParams ) {
 | 
|---|
| 1853 |                         for ( std::list< Type* >::const_iterator param = otypeParams.begin(); param != otypeParams.end(); ++param ) {
 | 
|---|
| 1854 |                                 if ( findGeneric( *param ) ) {
 | 
|---|
| 1855 |                                         // push size/align vars for a generic parameter back
 | 
|---|
| 1856 |                                         std::string paramName = mangleType( *param );
 | 
|---|
| 1857 |                                         layoutCall->get_args().push_back( new NameExpr( sizeofName( paramName ) ) );
 | 
|---|
| 1858 |                                         layoutCall->get_args().push_back( new NameExpr( alignofName( paramName ) ) );
 | 
|---|
| 1859 |                                 } else {
 | 
|---|
| 1860 |                                         layoutCall->get_args().push_back( new SizeofExpr( (*param)->clone() ) );
 | 
|---|
| 1861 |                                         layoutCall->get_args().push_back( new AlignofExpr( (*param)->clone() ) );
 | 
|---|
| 1862 |                                 }
 | 
|---|
| 1863 |                         }
 | 
|---|
| 1864 |                 }
 | 
|---|
| 1865 | 
 | 
|---|
| 1866 |                 /// returns true if any of the otype parameters have a dynamic layout and puts all otype parameters in the output list
 | 
|---|
| 1867 |                 bool findGenericParams( std::list< TypeDecl* > &baseParams, std::list< Expression* > &typeParams, std::list< Type* > &out ) {
 | 
|---|
| 1868 |                         bool hasDynamicLayout = false;
 | 
|---|
| 1869 | 
 | 
|---|
| 1870 |                         std::list< TypeDecl* >::const_iterator baseParam = baseParams.begin();
 | 
|---|
| 1871 |                         std::list< Expression* >::const_iterator typeParam = typeParams.begin();
 | 
|---|
| 1872 |                         for ( ; baseParam != baseParams.end() && typeParam != typeParams.end(); ++baseParam, ++typeParam ) {
 | 
|---|
| 1873 |                                 // skip non-otype parameters
 | 
|---|
| 1874 |                                 if ( (*baseParam)->get_kind() != TypeDecl::Any ) continue;
 | 
|---|
| 1875 |                                 TypeExpr *typeExpr = dynamic_cast< TypeExpr* >( *typeParam );
 | 
|---|
| 1876 |                                 assert( typeExpr && "all otype parameters should be type expressions" );
 | 
|---|
| 1877 | 
 | 
|---|
| 1878 |                                 Type *type = typeExpr->get_type();
 | 
|---|
| 1879 |                                 out.push_back( type );
 | 
|---|
| 1880 |                                 if ( isPolyType( type ) ) hasDynamicLayout = true;
 | 
|---|
| 1881 |                         }
 | 
|---|
| 1882 |                         assert( baseParam == baseParams.end() && typeParam == typeParams.end() );
 | 
|---|
| 1883 | 
 | 
|---|
| 1884 |                         return hasDynamicLayout;
 | 
|---|
| 1885 |                 }
 | 
|---|
| 1886 | 
 | 
|---|
| 1887 |                 bool PolyGenericCalculator::findGeneric( Type *ty ) {
 | 
|---|
| 1888 |                         ty = replaceTypeInst( ty, env );
 | 
|---|
| 1889 | 
 | 
|---|
| 1890 |                         if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( ty ) ) {
 | 
|---|
| 1891 |                                 if ( scopeTyVars.find( typeInst->get_name() ) != scopeTyVars.end() ) {
 | 
|---|
| 1892 |                                         // NOTE assumes here that getting put in the scopeTyVars included having the layout variables set
 | 
|---|
| 1893 |                                         return true;
 | 
|---|
| 1894 |                                 }
 | 
|---|
| 1895 |                                 return false;
 | 
|---|
| 1896 |                         } else if ( StructInstType *structTy = dynamic_cast< StructInstType* >( ty ) ) {
 | 
|---|
| 1897 |                                 // check if this type already has a layout generated for it
 | 
|---|
| 1898 |                                 std::string typeName = mangleType( ty );
 | 
|---|
| 1899 |                                 if ( knownLayouts.find( typeName ) != knownLayouts.end() ) return true;
 | 
|---|
| 1900 | 
 | 
|---|
| 1901 |                                 // check if any of the type parameters have dynamic layout; if none do, this type is (or will be) monomorphized
 | 
|---|
| 1902 |                                 std::list< Type* > otypeParams;
 | 
|---|
| 1903 |                                 if ( ! findGenericParams( *structTy->get_baseParameters(), structTy->get_parameters(), otypeParams ) ) return false;
 | 
|---|
| 1904 | 
 | 
|---|
| 1905 |                                 // insert local variables for layout and generate call to layout function
 | 
|---|
| 1906 |                                 knownLayouts.insert( typeName );  // done early so as not to interfere with the later addition of parameters to the layout call
 | 
|---|
| 1907 |                                 Type *layoutType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt );
 | 
|---|
| 1908 | 
 | 
|---|
| 1909 |                                 int n_members = structTy->get_baseStruct()->get_members().size();
 | 
|---|
| 1910 |                                 if ( n_members == 0 ) {
 | 
|---|
| 1911 |                                         // all empty structs have the same layout - size 1, align 1
 | 
|---|
| 1912 |                                         makeVar( sizeofName( typeName ), layoutType, new SingleInit( new ConstantExpr( Constant::from_ulong( (unsigned long)1 ) ) ) );
 | 
|---|
| 1913 |                                         makeVar( alignofName( typeName ), layoutType->clone(), new SingleInit( new ConstantExpr( Constant::from_ulong( (unsigned long)1 ) ) ) );
 | 
|---|
| 1914 |                                         // NOTE zero-length arrays are forbidden in C, so empty structs have no offsetof array
 | 
|---|
| 1915 |                                 } else {
 | 
|---|
| 1916 |                                         ObjectDecl *sizeVar = makeVar( sizeofName( typeName ), layoutType );
 | 
|---|
| 1917 |                                         ObjectDecl *alignVar = makeVar( alignofName( typeName ), layoutType->clone() );
 | 
|---|
| 1918 |                                         ObjectDecl *offsetVar = makeVar( offsetofName( typeName ), new ArrayType( Type::Qualifiers(), layoutType->clone(), new ConstantExpr( Constant::from_int( n_members ) ), false, false ) );
 | 
|---|
| 1919 | 
 | 
|---|
| 1920 |                                         // generate call to layout function
 | 
|---|
| 1921 |                                         UntypedExpr *layoutCall = new UntypedExpr( new NameExpr( layoutofName( structTy->get_baseStruct() ) ) );
 | 
|---|
| 1922 |                                         layoutCall->get_args().push_back( new AddressExpr( new VariableExpr( sizeVar ) ) );
 | 
|---|
| 1923 |                                         layoutCall->get_args().push_back( new AddressExpr( new VariableExpr( alignVar ) ) );
 | 
|---|
| 1924 |                                         layoutCall->get_args().push_back( new VariableExpr( offsetVar ) );
 | 
|---|
| 1925 |                                         addOtypeParamsToLayoutCall( layoutCall, otypeParams );
 | 
|---|
| 1926 | 
 | 
|---|
| 1927 |                                         stmtsToAdd.push_back( new ExprStmt( noLabels, layoutCall ) );
 | 
|---|
| 1928 |                                 }
 | 
|---|
| 1929 | 
 | 
|---|
| 1930 |                                 return true;
 | 
|---|
| 1931 |                         } else if ( UnionInstType *unionTy = dynamic_cast< UnionInstType* >( ty ) ) {
 | 
|---|
| 1932 |                                 // check if this type already has a layout generated for it
 | 
|---|
| 1933 |                                 std::string typeName = mangleType( ty );
 | 
|---|
| 1934 |                                 if ( knownLayouts.find( typeName ) != knownLayouts.end() ) return true;
 | 
|---|
| 1935 | 
 | 
|---|
| 1936 |                                 // check if any of the type parameters have dynamic layout; if none do, this type is (or will be) monomorphized
 | 
|---|
| 1937 |                                 std::list< Type* > otypeParams;
 | 
|---|
| 1938 |                                 if ( ! findGenericParams( *unionTy->get_baseParameters(), unionTy->get_parameters(), otypeParams ) ) return false;
 | 
|---|
| 1939 | 
 | 
|---|
| 1940 |                                 // insert local variables for layout and generate call to layout function
 | 
|---|
| 1941 |                                 knownLayouts.insert( typeName );  // done early so as not to interfere with the later addition of parameters to the layout call
 | 
|---|
| 1942 |                                 Type *layoutType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt );
 | 
|---|
| 1943 | 
 | 
|---|
| 1944 |                                 ObjectDecl *sizeVar = makeVar( sizeofName( typeName ), layoutType );
 | 
|---|
| 1945 |                                 ObjectDecl *alignVar = makeVar( alignofName( typeName ), layoutType->clone() );
 | 
|---|
| 1946 | 
 | 
|---|
| 1947 |                                 // generate call to layout function
 | 
|---|
| 1948 |                                 UntypedExpr *layoutCall = new UntypedExpr( new NameExpr( layoutofName( unionTy->get_baseUnion() ) ) );
 | 
|---|
| 1949 |                                 layoutCall->get_args().push_back( new AddressExpr( new VariableExpr( sizeVar ) ) );
 | 
|---|
| 1950 |                                 layoutCall->get_args().push_back( new AddressExpr( new VariableExpr( alignVar ) ) );
 | 
|---|
| 1951 |                                 addOtypeParamsToLayoutCall( layoutCall, otypeParams );
 | 
|---|
| 1952 | 
 | 
|---|
| 1953 |                                 stmtsToAdd.push_back( new ExprStmt( noLabels, layoutCall ) );
 | 
|---|
| 1954 | 
 | 
|---|
| 1955 |                                 return true;
 | 
|---|
| 1956 |                         }
 | 
|---|
| 1957 | 
 | 
|---|
| 1958 |                         return false;
 | 
|---|
| 1959 |                 }
 | 
|---|
| 1960 | 
 | 
|---|
| 1961 |                 Expression *PolyGenericCalculator::mutate( SizeofExpr *sizeofExpr ) {
 | 
|---|
| 1962 |                         Type *ty = sizeofExpr->get_type();
 | 
|---|
| 1963 |                         if ( findGeneric( ty ) ) {
 | 
|---|
| 1964 |                                 Expression *ret = new NameExpr( sizeofName( mangleType( ty ) ) );
 | 
|---|
| 1965 |                                 delete sizeofExpr;
 | 
|---|
| 1966 |                                 return ret;
 | 
|---|
| 1967 |                         }
 | 
|---|
| 1968 |                         return sizeofExpr;
 | 
|---|
| 1969 |                 }
 | 
|---|
| 1970 | 
 | 
|---|
| 1971 |                 Expression *PolyGenericCalculator::mutate( AlignofExpr *alignofExpr ) {
 | 
|---|
| 1972 |                         Type *ty = alignofExpr->get_type();
 | 
|---|
| 1973 |                         if ( findGeneric( ty ) ) {
 | 
|---|
| 1974 |                                 Expression *ret = new NameExpr( alignofName( mangleType( ty ) ) );
 | 
|---|
| 1975 |                                 delete alignofExpr;
 | 
|---|
| 1976 |                                 return ret;
 | 
|---|
| 1977 |                         }
 | 
|---|
| 1978 |                         return alignofExpr;
 | 
|---|
| 1979 |                 }
 | 
|---|
| 1980 | 
 | 
|---|
| 1981 |                 Expression *PolyGenericCalculator::mutate( OffsetofExpr *offsetofExpr ) {
 | 
|---|
| 1982 |                         // mutate, exiting early if no longer OffsetofExpr
 | 
|---|
| 1983 |                         Expression *expr = Parent::mutate( offsetofExpr );
 | 
|---|
| 1984 |                         offsetofExpr = dynamic_cast< OffsetofExpr* >( expr );
 | 
|---|
| 1985 |                         if ( ! offsetofExpr ) return expr;
 | 
|---|
| 1986 | 
 | 
|---|
| 1987 |                         // only mutate expressions for polymorphic structs/unions
 | 
|---|
| 1988 |                         Type *ty = offsetofExpr->get_type();
 | 
|---|
| 1989 |                         if ( ! findGeneric( ty ) ) return offsetofExpr;
 | 
|---|
| 1990 | 
 | 
|---|
| 1991 |                         if ( StructInstType *structType = dynamic_cast< StructInstType* >( ty ) ) {
 | 
|---|
| 1992 |                                 // replace offsetof expression by index into offset array
 | 
|---|
| 1993 |                                 long i = findMember( offsetofExpr->get_member(), structType->get_baseStruct()->get_members() );
 | 
|---|
| 1994 |                                 if ( i == -1 ) return offsetofExpr;
 | 
|---|
| 1995 | 
 | 
|---|
| 1996 |                                 Expression *offsetInd = makeOffsetIndex( ty, i );
 | 
|---|
| 1997 |                                 delete offsetofExpr;
 | 
|---|
| 1998 |                                 return offsetInd;
 | 
|---|
| 1999 |                         } else if ( dynamic_cast< UnionInstType* >( ty ) ) {
 | 
|---|
| 2000 |                                 // all union members are at offset zero
 | 
|---|
| 2001 |                                 delete offsetofExpr;
 | 
|---|
| 2002 |                                 return new ConstantExpr( Constant( new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ), "0" ) );
 | 
|---|
| 2003 |                         } else return offsetofExpr;
 | 
|---|
| 2004 |                 }
 | 
|---|
| 2005 | 
 | 
|---|
| 2006 |                 Expression *PolyGenericCalculator::mutate( OffsetPackExpr *offsetPackExpr ) {
 | 
|---|
| 2007 |                         StructInstType *ty = offsetPackExpr->get_type();
 | 
|---|
| 2008 | 
 | 
|---|
| 2009 |                         Expression *ret = 0;
 | 
|---|
| 2010 |                         if ( findGeneric( ty ) ) {
 | 
|---|
| 2011 |                                 // pull offset back from generated type information
 | 
|---|
| 2012 |                                 ret = new NameExpr( offsetofName( mangleType( ty ) ) );
 | 
|---|
| 2013 |                         } else {
 | 
|---|
| 2014 |                                 std::string offsetName = offsetofName( mangleType( ty ) );
 | 
|---|
| 2015 |                                 if ( knownOffsets.find( offsetName ) != knownOffsets.end() ) {
 | 
|---|
| 2016 |                                         // use the already-generated offsets for this type
 | 
|---|
| 2017 |                                         ret = new NameExpr( offsetName );
 | 
|---|
| 2018 |                                 } else {
 | 
|---|
| 2019 |                                         knownOffsets.insert( offsetName );
 | 
|---|
| 2020 | 
 | 
|---|
| 2021 |                                         std::list< Declaration* > &baseMembers = ty->get_baseStruct()->get_members();
 | 
|---|
| 2022 |                                         Type *offsetType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt );
 | 
|---|
| 2023 | 
 | 
|---|
| 2024 |                                         // build initializer list for offset array
 | 
|---|
| 2025 |                                         std::list< Initializer* > inits;
 | 
|---|
| 2026 |                                         for ( std::list< Declaration* >::const_iterator member = baseMembers.begin(); member != baseMembers.end(); ++member ) {
 | 
|---|
| 2027 |                                                 DeclarationWithType *memberDecl;
 | 
|---|
| 2028 |                                                 if ( DeclarationWithType *origMember = dynamic_cast< DeclarationWithType* >( *member ) ) {
 | 
|---|
| 2029 |                                                         memberDecl = origMember->clone();
 | 
|---|
| 2030 |                                                 } else {
 | 
|---|
| 2031 |                                                         memberDecl = new ObjectDecl( (*member)->get_name(), DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, offsetType->clone(), 0 );
 | 
|---|
| 2032 |                                                 }
 | 
|---|
| 2033 |                                                 inits.push_back( new SingleInit( new OffsetofExpr( ty->clone(), memberDecl ) ) );
 | 
|---|
| 2034 |                                         }
 | 
|---|
| 2035 | 
 | 
|---|
| 2036 |                                         // build the offset array and replace the pack with a reference to it
 | 
|---|
| 2037 |                                         ObjectDecl *offsetArray = makeVar( offsetName, new ArrayType( Type::Qualifiers(), offsetType, new ConstantExpr( Constant::from_ulong( baseMembers.size() ) ), false, false ),
 | 
|---|
| 2038 |                                                         new ListInit( inits ) );
 | 
|---|
| 2039 |                                         ret = new VariableExpr( offsetArray );
 | 
|---|
| 2040 |                                 }
 | 
|---|
| 2041 |                         }
 | 
|---|
| 2042 | 
 | 
|---|
| 2043 |                         delete offsetPackExpr;
 | 
|---|
| 2044 |                         return ret;
 | 
|---|
| 2045 |                 }
 | 
|---|
| 2046 | 
 | 
|---|
| 2047 |                 void PolyGenericCalculator::doBeginScope() {
 | 
|---|
| 2048 |                         knownLayouts.beginScope();
 | 
|---|
| 2049 |                         knownOffsets.beginScope();
 | 
|---|
| 2050 |                 }
 | 
|---|
| 2051 | 
 | 
|---|
| 2052 |                 void PolyGenericCalculator::doEndScope() {
 | 
|---|
| 2053 |                         knownLayouts.endScope();
 | 
|---|
| 2054 |                         knownOffsets.endScope();
 | 
|---|
| 2055 |                 }
 | 
|---|
| 2056 | 
 | 
|---|
| 2057 | ////////////////////////////////////////// Pass3 ////////////////////////////////////////////////////
 | 
|---|
| 2058 | 
 | 
|---|
| 2059 |                 template< typename DeclClass >
 | 
|---|
| 2060 |                 DeclClass * Pass3::handleDecl( DeclClass *decl, Type *type ) {
 | 
|---|
| 2061 |                         scopeTyVars.beginScope();
 | 
|---|
| 2062 |                         makeTyVarMap( type, scopeTyVars );
 | 
|---|
| 2063 | 
 | 
|---|
| 2064 |                         DeclClass *ret = static_cast< DeclClass *>( Mutator::mutate( decl ) );
 | 
|---|
| 2065 |                         ScrubTyVars::scrub( decl, scopeTyVars );
 | 
|---|
| 2066 | 
 | 
|---|
| 2067 |                         scopeTyVars.endScope();
 | 
|---|
| 2068 |                         return ret;
 | 
|---|
| 2069 |                 }
 | 
|---|
| 2070 | 
 | 
|---|
| 2071 |                 ObjectDecl * Pass3::mutate( ObjectDecl *objectDecl ) {
 | 
|---|
| 2072 |                         return handleDecl( objectDecl, objectDecl->get_type() );
 | 
|---|
| 2073 |                 }
 | 
|---|
| 2074 | 
 | 
|---|
| 2075 |                 DeclarationWithType * Pass3::mutate( FunctionDecl *functionDecl ) {
 | 
|---|
| 2076 |                         return handleDecl( functionDecl, functionDecl->get_functionType() );
 | 
|---|
| 2077 |                 }
 | 
|---|
| 2078 | 
 | 
|---|
| 2079 |                 TypedefDecl * Pass3::mutate( TypedefDecl *typedefDecl ) {
 | 
|---|
| 2080 |                         return handleDecl( typedefDecl, typedefDecl->get_base() );
 | 
|---|
| 2081 |                 }
 | 
|---|
| 2082 | 
 | 
|---|
| 2083 |                 TypeDecl * Pass3::mutate( TypeDecl *typeDecl ) {
 | 
|---|
| 2084 | //   Initializer *init = 0;
 | 
|---|
| 2085 | //   std::list< Expression *> designators;
 | 
|---|
| 2086 | //   scopeTyVars[ typeDecl->get_name() ] = typeDecl->get_kind();
 | 
|---|
| 2087 | //   if ( typeDecl->get_base() ) {
 | 
|---|
| 2088 | //     init = new SimpleInit( new SizeofExpr( handleDecl( typeDecl, typeDecl->get_base() ) ), designators );
 | 
|---|
| 2089 | //   }
 | 
|---|
| 2090 | //   return new ObjectDecl( typeDecl->get_name(), Declaration::Extern, LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::UnsignedInt ), init );
 | 
|---|
| 2091 | 
 | 
|---|
| 2092 |                         scopeTyVars[ typeDecl->get_name() ] = typeDecl->get_kind();
 | 
|---|
| 2093 |                         return Mutator::mutate( typeDecl );
 | 
|---|
| 2094 |                 }
 | 
|---|
| 2095 | 
 | 
|---|
| 2096 |                 Type * Pass3::mutate( PointerType *pointerType ) {
 | 
|---|
| 2097 |                         scopeTyVars.beginScope();
 | 
|---|
| 2098 |                         makeTyVarMap( pointerType, scopeTyVars );
 | 
|---|
| 2099 | 
 | 
|---|
| 2100 |                         Type *ret = Mutator::mutate( pointerType );
 | 
|---|
| 2101 | 
 | 
|---|
| 2102 |                         scopeTyVars.endScope();
 | 
|---|
| 2103 |                         return ret;
 | 
|---|
| 2104 |                 }
 | 
|---|
| 2105 | 
 | 
|---|
| 2106 |                 Type * Pass3::mutate( FunctionType *functionType ) {
 | 
|---|
| 2107 |                         scopeTyVars.beginScope();
 | 
|---|
| 2108 |                         makeTyVarMap( functionType, scopeTyVars );
 | 
|---|
| 2109 | 
 | 
|---|
| 2110 |                         Type *ret = Mutator::mutate( functionType );
 | 
|---|
| 2111 | 
 | 
|---|
| 2112 |                         scopeTyVars.endScope();
 | 
|---|
| 2113 |                         return ret;
 | 
|---|
| 2114 |                 }
 | 
|---|
| 2115 |         } // anonymous namespace
 | 
|---|
| 2116 | } // namespace GenPoly
 | 
|---|
| 2117 | 
 | 
|---|
| 2118 | // Local Variables: //
 | 
|---|
| 2119 | // tab-width: 4 //
 | 
|---|
| 2120 | // mode: c++ //
 | 
|---|
| 2121 | // compile-command: "make install" //
 | 
|---|
| 2122 | // End: //
 | 
|---|