| 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 | // Actors.cpp -- generate code needed by the actor system
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // Author           : Colby Parsons
 | 
|---|
| 10 | // Created On       : Thurs Jan  19 15:34:00 2023
 | 
|---|
| 11 | // Last Modified By : Colby Parsons
 | 
|---|
| 12 | // Last Modified On : Thurs Jan  19 15:34:00 2023
 | 
|---|
| 13 | // Update Count     : 0
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include "AST/Print.hpp"
 | 
|---|
| 17 | #include "AST/Decl.hpp"
 | 
|---|
| 18 | #include "AST/Pass.hpp"
 | 
|---|
| 19 | #include "AST/Type.hpp"
 | 
|---|
| 20 | #include "AST/Stmt.hpp"
 | 
|---|
| 21 | #include "AST/TranslationUnit.hpp"
 | 
|---|
| 22 | #include "AST/Expr.hpp"
 | 
|---|
| 23 | #include <algorithm>
 | 
|---|
| 24 | using namespace ast;
 | 
|---|
| 25 | using namespace std;
 | 
|---|
| 26 | 
 | 
|---|
| 27 | namespace Concurrency {
 | 
|---|
| 28 | 
 | 
|---|
| 29 | struct CollectactorStructDecls : public ast::WithGuards {
 | 
|---|
| 30 |     unordered_set<const StructDecl *> & actorStructDecls;
 | 
|---|
| 31 |     unordered_set<const StructDecl *>  & messageStructDecls;
 | 
|---|
| 32 |     const StructDecl ** requestDecl;
 | 
|---|
| 33 |     const EnumDecl ** allocationDecl;
 | 
|---|
| 34 |     const StructDecl ** actorDecl;
 | 
|---|
| 35 |     const StructDecl ** msgDecl;
 | 
|---|
| 36 |     StructDecl * parentDecl;
 | 
|---|
| 37 |     bool insideStruct = false;
 | 
|---|
| 38 |     bool namedDecl = false;
 | 
|---|
| 39 | 
 | 
|---|
| 40 |     // finds and sets a ptr to the Allocation enum, which is needed in the next pass
 | 
|---|
| 41 |     void previsit( const EnumDecl * decl ) {
 | 
|---|
| 42 |         if( decl->name == "Allocation" ) *allocationDecl = decl;
 | 
|---|
| 43 |     }
 | 
|---|
| 44 | 
 | 
|---|
| 45 |     // finds and sets a ptr to the actor, message, and request structs, which are needed in the next pass
 | 
|---|
| 46 |     void previsit( const StructDecl * decl ) {
 | 
|---|
| 47 |         GuardValue(insideStruct);
 | 
|---|
| 48 |         insideStruct = true;
 | 
|---|
| 49 |         parentDecl = mutate( decl );
 | 
|---|
| 50 |         if( decl->name == "actor" ) *actorDecl = decl;
 | 
|---|
| 51 |         if( decl->name == "message" ) *msgDecl = decl;
 | 
|---|
| 52 |         if( decl->name == "request" ) *requestDecl = decl;
 | 
|---|
| 53 |         }
 | 
|---|
| 54 | 
 | 
|---|
| 55 |     // this catches structs of the form:
 | 
|---|
| 56 |     //     struct dummy_actor { actor a; };
 | 
|---|
| 57 |     // since they should be:
 | 
|---|
| 58 |     //     struct dummy_actor { inline actor; };
 | 
|---|
| 59 |     void previsit ( const ObjectDecl * decl ) {
 | 
|---|
| 60 |         if ( insideStruct && ! decl->name.empty() ) {
 | 
|---|
| 61 |             GuardValue(namedDecl);
 | 
|---|
| 62 |             namedDecl = true;
 | 
|---|
| 63 |         }
 | 
|---|
| 64 |     }
 | 
|---|
| 65 | 
 | 
|---|
| 66 |     // this collects the valid actor and message struct decl pts
 | 
|---|
| 67 |     void postvisit( const StructInstType * node ) {
 | 
|---|
| 68 |         if ( ! *actorDecl || ! *msgDecl ) return;
 | 
|---|
| 69 |         if ( insideStruct && !namedDecl ) {
 | 
|---|
| 70 |             if ( node->aggr() == *actorDecl ) {
 | 
|---|
| 71 |                 actorStructDecls.insert( parentDecl );
 | 
|---|
| 72 |             } else if ( node->aggr() == *msgDecl ) {
 | 
|---|
| 73 |                 messageStructDecls.insert( parentDecl );
 | 
|---|
| 74 |             }
 | 
|---|
| 75 |         }
 | 
|---|
| 76 |         }
 | 
|---|
| 77 | 
 | 
|---|
| 78 |   public:
 | 
|---|
| 79 |     CollectactorStructDecls( unordered_set<const StructDecl *> & actorStructDecls, unordered_set<const StructDecl *> & messageStructDecls,
 | 
|---|
| 80 |         const StructDecl ** requestDecl, const EnumDecl ** allocationDecl, const StructDecl ** actorDecl, const StructDecl ** msgDecl ) 
 | 
|---|
| 81 |         : actorStructDecls( actorStructDecls ), messageStructDecls( messageStructDecls ), requestDecl( requestDecl ), 
 | 
|---|
| 82 |         allocationDecl( allocationDecl ), actorDecl(actorDecl), msgDecl(msgDecl) {}
 | 
|---|
| 83 | };
 | 
|---|
| 84 | 
 | 
|---|
| 85 | // keeps track of all fwdDecls of message routines so that we can hoist them to right after the appropriate decls
 | 
|---|
| 86 | class FwdDeclTable {
 | 
|---|
| 87 | 
 | 
|---|
| 88 |     // tracks which decls we have seen so that we can hoist the FunctionDecl to the highest point possible
 | 
|---|
| 89 |     struct FwdDeclData { 
 | 
|---|
| 90 |         const StructDecl * actorDecl;
 | 
|---|
| 91 |         const StructDecl * msgDecl;
 | 
|---|
| 92 |         FunctionDecl * fwdDecl;
 | 
|---|
| 93 |         bool actorFound;
 | 
|---|
| 94 |         bool msgFound;
 | 
|---|
| 95 | 
 | 
|---|
| 96 |         bool readyToInsert() { return actorFound && msgFound; }
 | 
|---|
| 97 |         bool foundActor() { actorFound = true; return readyToInsert(); }
 | 
|---|
| 98 |         bool foundMsg() { msgFound = true; return readyToInsert(); }
 | 
|---|
| 99 | 
 | 
|---|
| 100 |         FwdDeclData( const StructDecl * actorDecl, const StructDecl * msgDecl, FunctionDecl * fwdDecl ) :
 | 
|---|
| 101 |             actorDecl(actorDecl), msgDecl(msgDecl), fwdDecl(fwdDecl), actorFound(false), msgFound(false) {}
 | 
|---|
| 102 |     };
 | 
|---|
| 103 | 
 | 
|---|
| 104 |     // map indexed by actor struct ptr
 | 
|---|
| 105 |     // value is map of all FwdDeclData that contains said actor struct ptr
 | 
|---|
| 106 |     // inner map is indexed by the message struct ptr of FwdDeclData
 | 
|---|
| 107 |     unordered_map<const StructDecl *, unordered_map<const StructDecl *, FwdDeclData *>> actorMap;
 | 
|---|
| 108 | 
 | 
|---|
| 109 |     // this map is the same except the outer map is indexed by message ptr and the inner is indexed by actor ptr
 | 
|---|
| 110 |     unordered_map<const StructDecl *, unordered_map<const StructDecl *, FwdDeclData *>> msgMap;
 | 
|---|
| 111 | 
 | 
|---|
| 112 |     void insert( const StructDecl * decl, const StructDecl * otherDecl, unordered_map<const StructDecl *, unordered_map<const StructDecl *, FwdDeclData *>> & map, FwdDeclData * data ) {
 | 
|---|
| 113 |         auto iter = map.find( decl );
 | 
|---|
| 114 |         if ( iter != map.end() ) { // if decl exists in map append data to existing inner map
 | 
|---|
| 115 |             iter->second.emplace( make_pair( otherDecl, data ) );
 | 
|---|
| 116 |         } else { // else create inner map for key
 | 
|---|
| 117 |             map.emplace( make_pair( decl, unordered_map<const StructDecl *, FwdDeclData *>( { make_pair( otherDecl, data ) } ) ) );
 | 
|---|
| 118 |         }
 | 
|---|
| 119 |     }
 | 
|---|
| 120 | 
 | 
|---|
| 121 |   public:
 | 
|---|
| 122 |     // insert decl into table so that we can fwd declare it later (average cost: O(1))
 | 
|---|
| 123 |     void insertDecl( const StructDecl * actorDecl, const StructDecl * msgDecl, FunctionDecl * fwdDecl ) {
 | 
|---|
| 124 |         FwdDeclData * declToInsert = new FwdDeclData( actorDecl, msgDecl, fwdDecl );
 | 
|---|
| 125 |         insert( actorDecl, msgDecl, actorMap, declToInsert );
 | 
|---|
| 126 |         insert( msgDecl, actorDecl, msgMap, declToInsert );
 | 
|---|
| 127 |     }
 | 
|---|
| 128 | 
 | 
|---|
| 129 |     // returns list of decls to insert after current struct decl
 | 
|---|
| 130 |     // Over the entire pass the runtime of this routine is O(r) where r is the # of receive routines
 | 
|---|
| 131 |     list<FunctionDecl *> updateDecl( const StructDecl * decl, bool isMsg ) {
 | 
|---|
| 132 |         unordered_map<const StructDecl *, unordered_map<const StructDecl *, FwdDeclData *>> & map = isMsg ? msgMap : actorMap;
 | 
|---|
| 133 |         unordered_map<const StructDecl *, unordered_map<const StructDecl *, FwdDeclData *>> & otherMap =  isMsg ? actorMap : msgMap;
 | 
|---|
| 134 |         auto iter = map.find( decl );
 | 
|---|
| 135 |         list<FunctionDecl *> toInsertAfter; // this is populated with decls that are ready to insert
 | 
|---|
| 136 |         if ( iter == map.end() ) return toInsertAfter;
 | 
|---|
| 137 |         
 | 
|---|
| 138 |         // iterate over inner map
 | 
|---|
| 139 |         unordered_map<const StructDecl *, FwdDeclData *> & currInnerMap = iter->second;
 | 
|---|
| 140 |         for ( auto innerIter = currInnerMap.begin(); innerIter != currInnerMap.end(); ) {
 | 
|---|
| 141 |             FwdDeclData * currentDatum = innerIter->second;
 | 
|---|
| 142 |             bool readyToInsert = isMsg ? currentDatum->foundMsg() : currentDatum->foundActor();
 | 
|---|
| 143 |             if ( ! readyToInsert ) { ++innerIter; continue; }
 | 
|---|
| 144 |             
 | 
|---|
| 145 |             // readyToInsert is true so we are good to insert the forward decl of the message fn
 | 
|---|
| 146 |             toInsertAfter.push_back( currentDatum->fwdDecl );
 | 
|---|
| 147 | 
 | 
|---|
| 148 |             // need to remove from other map before deleting
 | 
|---|
| 149 |             // find inner map in other map ( other map is actor map if original is msg map and vice versa )
 | 
|---|
| 150 |             const StructDecl * otherDecl = isMsg ? currentDatum->actorDecl : currentDatum->msgDecl;
 | 
|---|
| 151 |             auto otherMapIter = otherMap.find( otherDecl );
 | 
|---|
| 152 | 
 | 
|---|
| 153 |             unordered_map<const StructDecl *, FwdDeclData *> & otherInnerMap = otherMapIter->second;
 | 
|---|
| 154 | 
 | 
|---|
| 155 |             // find the FwdDeclData we need to remove in the other inner map
 | 
|---|
| 156 |             auto otherInnerIter = otherInnerMap.find( decl );
 | 
|---|
| 157 | 
 | 
|---|
| 158 |             // remove references to deleted FwdDeclData from current inner map
 | 
|---|
| 159 |             innerIter = currInnerMap.erase( innerIter ); // this does the increment so no explicit inc needed
 | 
|---|
| 160 | 
 | 
|---|
| 161 |             // remove references to deleted FwdDeclData from other inner map
 | 
|---|
| 162 |             otherInnerMap.erase( otherInnerIter );
 | 
|---|
| 163 |             
 | 
|---|
| 164 |             // if other inner map is now empty, remove key from other outer map
 | 
|---|
| 165 |             if ( otherInnerMap.empty() )
 | 
|---|
| 166 |                 otherMap.erase( otherDecl );
 | 
|---|
| 167 | 
 | 
|---|
| 168 |             // now we are safe to delete the FwdDeclData since we are done with it
 | 
|---|
| 169 |             // and we have removed all references to it from our data structures
 | 
|---|
| 170 |             delete currentDatum;
 | 
|---|
| 171 |         }
 | 
|---|
| 172 | 
 | 
|---|
| 173 |         // if current inner map is now empty, remove key from outer map.
 | 
|---|
| 174 |         // Have to do this after iterating for safety
 | 
|---|
| 175 |         if ( currInnerMap.empty() )
 | 
|---|
| 176 |             map.erase( decl );
 | 
|---|
| 177 | 
 | 
|---|
| 178 |         return toInsertAfter;
 | 
|---|
| 179 |     }
 | 
|---|
| 180 | };
 | 
|---|
| 181 | 
 | 
|---|
| 182 | struct GenReceiveDecls : public ast::WithDeclsToAdd<> {
 | 
|---|
| 183 |     unordered_set<const StructDecl *> & actorStructDecls;
 | 
|---|
| 184 |     unordered_set<const StructDecl *>  & messageStructDecls;
 | 
|---|
| 185 |     const StructDecl ** requestDecl;
 | 
|---|
| 186 |     const EnumDecl ** allocationDecl;
 | 
|---|
| 187 |     const StructDecl ** actorDecl;
 | 
|---|
| 188 |     const StructDecl ** msgDecl;
 | 
|---|
| 189 |     FwdDeclTable & forwardDecls;
 | 
|---|
| 190 | 
 | 
|---|
| 191 |         void postvisit( const FunctionDecl * decl ) {
 | 
|---|
| 192 |         // return if not of the form receive( param1, param2 ) or if it is a forward decl
 | 
|---|
| 193 |         if ( decl->name != "receive" || decl->params.size() != 2 || !decl->stmts ) return;
 | 
|---|
| 194 | 
 | 
|---|
| 195 |         // the params should be references
 | 
|---|
| 196 |         const ReferenceType * derivedActorRef = dynamic_cast<const ReferenceType *>(decl->params.at(0)->get_type());
 | 
|---|
| 197 |         const ReferenceType * derivedMsgRef = dynamic_cast<const ReferenceType *>(decl->params.at(1)->get_type());
 | 
|---|
| 198 |         if ( !derivedActorRef || !derivedMsgRef ) return;
 | 
|---|
| 199 | 
 | 
|---|
| 200 |         // the references should be to struct instances
 | 
|---|
| 201 |         const StructInstType * arg1InstType = dynamic_cast<const StructInstType *>(derivedActorRef->base.get());
 | 
|---|
| 202 |         const StructInstType * arg2InstType = dynamic_cast<const StructInstType *>(derivedMsgRef->base.get());
 | 
|---|
| 203 |         if ( !arg1InstType || !arg2InstType ) return;
 | 
|---|
| 204 | 
 | 
|---|
| 205 |         // If the struct instances are derived actor and message types then generate the message send routine
 | 
|---|
| 206 |         auto actorIter = actorStructDecls.find( arg1InstType->aggr() );
 | 
|---|
| 207 |         auto messageIter = messageStructDecls.find( arg2InstType->aggr() );
 | 
|---|
| 208 |         if ( actorIter != actorStructDecls.end() && messageIter != messageStructDecls.end() ) {
 | 
|---|
| 209 | 
 | 
|---|
| 210 |             // check that we have found all the decls we need from <actor.hfa>
 | 
|---|
| 211 |             if ( !*allocationDecl || !*requestDecl ) 
 | 
|---|
| 212 |                 SemanticError( decl->location, "using actors requires a header, add #include <actor.hfa>\n" );
 | 
|---|
| 213 | 
 | 
|---|
| 214 |             //////////////////////////////////////////////////////////////////////
 | 
|---|
| 215 |             // The following generates this send message operator routine for all receive(derived_actor &, derived_msg &) functions
 | 
|---|
| 216 |             /*
 | 
|---|
| 217 |                 static inline derived_actor & ?|?( derived_actor & receiver, derived_msg & msg ) {
 | 
|---|
| 218 |                     request new_req;
 | 
|---|
| 219 |                     Allocation (*my_work_fn)( derived_actor &, derived_msg & ) = receive;
 | 
|---|
| 220 |                     __receive_fn fn = (__receive_fn)my_work_fn;
 | 
|---|
| 221 |                     new_req{ &receiver, &msg, fn };
 | 
|---|
| 222 |                     send( receiver, new_req );
 | 
|---|
| 223 |                     return receiver;
 | 
|---|
| 224 |                 }
 | 
|---|
| 225 |             */ // C_TODO: update this with new no alloc version
 | 
|---|
| 226 |             CompoundStmt * sendBody = new CompoundStmt( decl->location );
 | 
|---|
| 227 | 
 | 
|---|
| 228 |             // Generates: request new_req;
 | 
|---|
| 229 |             sendBody->push_back( new DeclStmt(
 | 
|---|
| 230 |                 decl->location,
 | 
|---|
| 231 |                 new ObjectDecl(
 | 
|---|
| 232 |                     decl->location,
 | 
|---|
| 233 |                     "new_req",
 | 
|---|
| 234 |                     new StructInstType( *requestDecl )
 | 
|---|
| 235 |                 )
 | 
|---|
| 236 |             ));
 | 
|---|
| 237 |             
 | 
|---|
| 238 |             // Function type is: Allocation (*)( derived_actor &, derived_msg & )
 | 
|---|
| 239 |             FunctionType * derivedReceive = new FunctionType();
 | 
|---|
| 240 |             derivedReceive->params.push_back( ast::deepCopy( derivedActorRef ) );
 | 
|---|
| 241 |             derivedReceive->params.push_back( ast::deepCopy( derivedMsgRef ) );
 | 
|---|
| 242 |             derivedReceive->returns.push_back( new EnumInstType( *allocationDecl ) );
 | 
|---|
| 243 | 
 | 
|---|
| 244 |             // Generates: Allocation (*my_work_fn)( derived_actor &, derived_msg & ) = receive;
 | 
|---|
| 245 |             sendBody->push_back( new DeclStmt(
 | 
|---|
| 246 |                 decl->location,
 | 
|---|
| 247 |                 new ObjectDecl(
 | 
|---|
| 248 |                     decl->location,
 | 
|---|
| 249 |                     "my_work_fn",
 | 
|---|
| 250 |                     new PointerType( derivedReceive ),
 | 
|---|
| 251 |                     new SingleInit( decl->location, new NameExpr( decl->location, "receive" ) )
 | 
|---|
| 252 |                 )
 | 
|---|
| 253 |             ));
 | 
|---|
| 254 | 
 | 
|---|
| 255 |             // Function type is: Allocation (*)( actor &, messsage & )
 | 
|---|
| 256 |             FunctionType * genericReceive = new FunctionType();
 | 
|---|
| 257 |             genericReceive->params.push_back( new ReferenceType( new StructInstType( *actorDecl ) ) );
 | 
|---|
| 258 |             genericReceive->params.push_back( new ReferenceType( new StructInstType( *msgDecl ) ) );
 | 
|---|
| 259 |             genericReceive->returns.push_back( new EnumInstType( *allocationDecl ) );
 | 
|---|
| 260 | 
 | 
|---|
| 261 |             // Generates: Allocation (*fn)( actor &, messsage & ) = (Allocation (*)( actor &, messsage & ))my_work_fn;
 | 
|---|
| 262 |             // More readable synonymous code: 
 | 
|---|
| 263 |             //     typedef Allocation (*__receive_fn)(actor &, message &);
 | 
|---|
| 264 |             //     __receive_fn fn = (__receive_fn)my_work_fn;
 | 
|---|
| 265 |             sendBody->push_back( new DeclStmt(
 | 
|---|
| 266 |                 decl->location,
 | 
|---|
| 267 |                 new ObjectDecl(
 | 
|---|
| 268 |                     decl->location,
 | 
|---|
| 269 |                     "fn",
 | 
|---|
| 270 |                     new PointerType( genericReceive ),
 | 
|---|
| 271 |                     new SingleInit( decl->location, 
 | 
|---|
| 272 |                         new CastExpr( decl->location, new NameExpr( decl->location, "my_work_fn" ), new PointerType( genericReceive ), ExplicitCast )
 | 
|---|
| 273 |                     )
 | 
|---|
| 274 |                 )
 | 
|---|
| 275 |             ));
 | 
|---|
| 276 | 
 | 
|---|
| 277 |             // Generates: new_req{ &receiver, &msg, fn };
 | 
|---|
| 278 |             sendBody->push_back( new ExprStmt(
 | 
|---|
| 279 |                 decl->location,
 | 
|---|
| 280 |                                 new UntypedExpr (
 | 
|---|
| 281 |                     decl->location, 
 | 
|---|
| 282 |                                         new NameExpr( decl->location, "?{}" ),
 | 
|---|
| 283 |                                         {
 | 
|---|
| 284 |                                                 new NameExpr( decl->location, "new_req" ),
 | 
|---|
| 285 |                         new AddressExpr( new NameExpr( decl->location, "receiver" ) ),
 | 
|---|
| 286 |                         new AddressExpr( new NameExpr( decl->location, "msg" ) ),
 | 
|---|
| 287 |                         new NameExpr( decl->location, "fn" )
 | 
|---|
| 288 |                                         }
 | 
|---|
| 289 |                                 )
 | 
|---|
| 290 |                         ));
 | 
|---|
| 291 | 
 | 
|---|
| 292 |             // Generates: send( receiver, new_req );
 | 
|---|
| 293 |             sendBody->push_back( new ExprStmt(
 | 
|---|
| 294 |                 decl->location,
 | 
|---|
| 295 |                                 new UntypedExpr (
 | 
|---|
| 296 |                     decl->location,
 | 
|---|
| 297 |                                         new NameExpr( decl->location, "send" ),
 | 
|---|
| 298 |                                         {
 | 
|---|
| 299 |                                                 {
 | 
|---|
| 300 |                             new NameExpr( decl->location, "receiver" ),
 | 
|---|
| 301 |                             new NameExpr( decl->location, "new_req" )
 | 
|---|
| 302 |                         }
 | 
|---|
| 303 |                                         }
 | 
|---|
| 304 |                                 )
 | 
|---|
| 305 |                         ));
 | 
|---|
| 306 |             
 | 
|---|
| 307 |             // Generates: return receiver;
 | 
|---|
| 308 |             sendBody->push_back( new ReturnStmt( decl->location, new NameExpr( decl->location, "receiver" ) ) );
 | 
|---|
| 309 | 
 | 
|---|
| 310 |             // put it all together into the complete function decl from above
 | 
|---|
| 311 |             FunctionDecl * sendOperatorFunction = new FunctionDecl(
 | 
|---|
| 312 |                 decl->location,
 | 
|---|
| 313 |                 "?|?",
 | 
|---|
| 314 |                 {},                     // forall
 | 
|---|
| 315 |                 {
 | 
|---|
| 316 |                     new ObjectDecl(
 | 
|---|
| 317 |                         decl->location,
 | 
|---|
| 318 |                         "receiver",
 | 
|---|
| 319 |                         ast::deepCopy( derivedActorRef )
 | 
|---|
| 320 |                     ),
 | 
|---|
| 321 |                     new ObjectDecl(
 | 
|---|
| 322 |                         decl->location,
 | 
|---|
| 323 |                         "msg",
 | 
|---|
| 324 |                         ast::deepCopy( derivedMsgRef )
 | 
|---|
| 325 |                     )
 | 
|---|
| 326 |                 },                      // params
 | 
|---|
| 327 |                 { 
 | 
|---|
| 328 |                     new ObjectDecl(
 | 
|---|
| 329 |                         decl->location,
 | 
|---|
| 330 |                         "receiver_ret",
 | 
|---|
| 331 |                         ast::deepCopy( derivedActorRef )
 | 
|---|
| 332 |                     )
 | 
|---|
| 333 |                 },
 | 
|---|
| 334 |                 nullptr,               // body
 | 
|---|
| 335 |                 { Storage::Static },    // storage
 | 
|---|
| 336 |                 Linkage::Cforall,       // linkage
 | 
|---|
| 337 |                 {},                     // attributes
 | 
|---|
| 338 |                 { Function::Inline }
 | 
|---|
| 339 |             );
 | 
|---|
| 340 |             
 | 
|---|
| 341 |             // forward decls to resolve use before decl problem for '|' routines
 | 
|---|
| 342 |             forwardDecls.insertDecl( *actorIter, *messageIter , ast::deepCopy( sendOperatorFunction ) );
 | 
|---|
| 343 |             // forwardDecls.push_back( ast::deepCopy( sendOperatorFunction ) );
 | 
|---|
| 344 | 
 | 
|---|
| 345 |             sendOperatorFunction->stmts = sendBody;
 | 
|---|
| 346 |             declsToAddAfter.push_back( sendOperatorFunction );
 | 
|---|
| 347 |         }
 | 
|---|
| 348 |         }
 | 
|---|
| 349 | 
 | 
|---|
| 350 |   public:
 | 
|---|
| 351 |     GenReceiveDecls( unordered_set<const StructDecl *> & actorStructDecls, unordered_set<const StructDecl *> & messageStructDecls,
 | 
|---|
| 352 |         const StructDecl ** requestDecl, const EnumDecl ** allocationDecl, const StructDecl ** actorDecl, const StructDecl ** msgDecl, 
 | 
|---|
| 353 |         FwdDeclTable & forwardDecls ) : actorStructDecls(actorStructDecls), messageStructDecls(messageStructDecls), 
 | 
|---|
| 354 |         requestDecl(requestDecl), allocationDecl(allocationDecl), actorDecl(actorDecl), msgDecl(msgDecl), forwardDecls(forwardDecls) {}
 | 
|---|
| 355 | };
 | 
|---|
| 356 | 
 | 
|---|
| 357 | struct GenFwdDecls : public ast::WithDeclsToAdd<> {
 | 
|---|
| 358 |     unordered_set<const StructDecl *> & actorStructDecls;
 | 
|---|
| 359 |     unordered_set<const StructDecl *>  & messageStructDecls;
 | 
|---|
| 360 |     FwdDeclTable & forwardDecls;
 | 
|---|
| 361 | 
 | 
|---|
| 362 |     void postvisit( const StructDecl * decl ) {
 | 
|---|
| 363 |         list<FunctionDecl *> toAddAfter;
 | 
|---|
| 364 |         auto actorIter = actorStructDecls.find( decl );
 | 
|---|
| 365 |         if ( actorIter != actorStructDecls.end() ) { // this is a derived actor decl
 | 
|---|
| 366 |             // get list of fwd decls that we can now insert
 | 
|---|
| 367 |             toAddAfter = forwardDecls.updateDecl( decl, false );
 | 
|---|
| 368 | 
 | 
|---|
| 369 |             // get rid of decl from actorStructDecls since we no longer need it
 | 
|---|
| 370 |             actorStructDecls.erase( actorIter );
 | 
|---|
| 371 |         } else {
 | 
|---|
| 372 |             auto messageIter = messageStructDecls.find( decl );
 | 
|---|
| 373 |             if ( messageIter == messageStructDecls.end() ) return;
 | 
|---|
| 374 | 
 | 
|---|
| 375 |             toAddAfter = forwardDecls.updateDecl( decl, true );
 | 
|---|
| 376 | 
 | 
|---|
| 377 |             // get rid of decl from messageStructDecls since we no longer need it
 | 
|---|
| 378 |             messageStructDecls.erase( messageIter );
 | 
|---|
| 379 |         }
 | 
|---|
| 380 | 
 | 
|---|
| 381 |         // add the fwd decls to declsToAddAfter
 | 
|---|
| 382 |         for ( FunctionDecl * func : toAddAfter ) {
 | 
|---|
| 383 |             declsToAddAfter.push_back( func );
 | 
|---|
| 384 |         }
 | 
|---|
| 385 |     }
 | 
|---|
| 386 | 
 | 
|---|
| 387 |   public:
 | 
|---|
| 388 |     GenFwdDecls( unordered_set<const StructDecl *> & actorStructDecls, unordered_set<const StructDecl *> & messageStructDecls, 
 | 
|---|
| 389 |         FwdDeclTable & forwardDecls ) : actorStructDecls(actorStructDecls), messageStructDecls(messageStructDecls),
 | 
|---|
| 390 |         forwardDecls(forwardDecls) {}
 | 
|---|
| 391 | };
 | 
|---|
| 392 | 
 | 
|---|
| 393 | void implementActors( TranslationUnit & translationUnit ) {
 | 
|---|
| 394 |     // unordered_maps to collect all derived actor and message types
 | 
|---|
| 395 |     unordered_set<const StructDecl *> actorStructDecls;
 | 
|---|
| 396 |     unordered_set<const StructDecl *> messageStructDecls;
 | 
|---|
| 397 |     FwdDeclTable forwardDecls;
 | 
|---|
| 398 | 
 | 
|---|
| 399 |     // for storing through the passes
 | 
|---|
| 400 |     // these are populated with various important struct decls
 | 
|---|
| 401 |     const StructDecl * requestDeclPtr = nullptr;
 | 
|---|
| 402 |     const EnumDecl * allocationDeclPtr = nullptr;
 | 
|---|
| 403 |     const StructDecl * actorDeclPtr = nullptr;
 | 
|---|
| 404 |     const StructDecl * msgDeclPtr = nullptr;
 | 
|---|
| 405 | 
 | 
|---|
| 406 |     // double pointer to modify local ptrs above
 | 
|---|
| 407 |     const StructDecl ** requestDecl = &requestDeclPtr;
 | 
|---|
| 408 |     const EnumDecl ** allocationDecl = &allocationDeclPtr;
 | 
|---|
| 409 |     const StructDecl ** actorDecl = &actorDeclPtr;
 | 
|---|
| 410 |     const StructDecl ** msgDecl = &msgDeclPtr;
 | 
|---|
| 411 | 
 | 
|---|
| 412 |     // first pass collects ptrs to Allocation enum, request type, and generic receive fn typedef
 | 
|---|
| 413 |     // also populates maps of all derived actors and messages
 | 
|---|
| 414 |     Pass<CollectactorStructDecls>::run( translationUnit, actorStructDecls, messageStructDecls, requestDecl, 
 | 
|---|
| 415 |         allocationDecl, actorDecl, msgDecl );
 | 
|---|
| 416 |         
 | 
|---|
| 417 |     // second pass locates all receive() routines that overload the generic receive fn
 | 
|---|
| 418 |     // it then generates the appropriate operator '|' send routines for the receive routines
 | 
|---|
| 419 |     Pass<GenReceiveDecls>::run( translationUnit, actorStructDecls, messageStructDecls, requestDecl, 
 | 
|---|
| 420 |         allocationDecl, actorDecl, msgDecl, forwardDecls );
 | 
|---|
| 421 | 
 | 
|---|
| 422 |     // The third pass forward declares operator '|' send routines
 | 
|---|
| 423 |     Pass<GenFwdDecls>::run( translationUnit, actorStructDecls, messageStructDecls, forwardDecls );
 | 
|---|
| 424 | }
 | 
|---|
| 425 | 
 | 
|---|
| 426 | 
 | 
|---|
| 427 | } // namespace Concurrency
 | 
|---|
| 428 | 
 | 
|---|
| 429 | // Local Variables: //
 | 
|---|
| 430 | // tab-width: 4 //
 | 
|---|
| 431 | // mode: c++ //
 | 
|---|
| 432 | // compile-command: "make install" //
 | 
|---|
| 433 | // End: //
 | 
|---|
| 434 | 
 | 
|---|