xlat-i/xi-symbol.h

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * $Source: /cvsroot/charm/src/xlat-i/xi-symbol.h,v $
00003  * $Author: gioachin $
00004  * $Date: 2008-06-02 05:45:02 $
00005  * $Revision: 1.92 $
00006  *****************************************************************************/
00007 
00008 #ifndef _SYMBOL_H
00009 #define _SYMBOL_H
00010 
00011 #include <stdio.h>
00012 #include <string.h>
00013 #include <stdlib.h>
00014 
00015 #include "xi-util.h"
00016 #include "EToken.h"
00017 #include "CEntry.h"
00018 #include "sdag-globals.h"
00019 #include "CList.h"
00020 #include "CStateVar.h"
00021 #include "CParsedFile.h"
00022 
00023 /******************* Utilities ****************/
00024 
00025 class Prefix {
00026 public:
00027   static const char *Proxy;
00028   static const char *ProxyElement;
00029   static const char *ProxySection;
00030   static const char *Message;
00031   static const char *Index;
00032   static const char *Python;
00033 };
00034 
00035 typedef enum {
00036   forAll=0,forIndividual=1,forSection=2,forPython=3,forIndex=-1
00037 } forWhom;
00038 
00039 class Chare;//Forward declaration
00040 class Message;
00041 class TParamList;
00042 extern int fortranMode;
00043 extern int internalMode;
00044 extern const char *cur_file;
00045 void die(const char *why,int line=-1);
00046 
00047 class Value : public Printable {
00048   private:
00049     int factor;
00050     const char *val;
00051   public:
00052     Value(const char *s);
00053     void print(XStr& str) { str << val; }
00054     int getIntVal(void);
00055 };
00056 
00057 class ValueList : public Printable {
00058   private:
00059     Value *val;
00060     ValueList *next;
00061   public:
00062     ValueList(Value* v, ValueList* n=0) : val(v), next(n) {}
00063     void print(XStr& str) {
00064       if(val) {
00065         str << "["; val->print(str); str << "]";
00066       }
00067       if(next)
00068         next->print(str);
00069     }
00070     void printValue(XStr& str) {
00071       if(val) {
00072         val->print(str);
00073       }
00074       if(next) {
00075           cout<<"Unsupported type\n";
00076           abort();
00077       }
00078     }
00079 };
00080 
00081 class Construct : public Printable {
00082   protected:
00083     int external;
00084   public:
00085     int line;
00086     Construct() {external=0;line=-1;}
00087     void setExtern(int e) { external = e; }
00088     virtual void genPub(XStr& declstr, XStr& defstr, XStr& defconstr, int& connectPresent) = 0;
00089     virtual void genDecls(XStr& str) = 0;
00090     virtual void genDefs(XStr& str) = 0;
00091     virtual void genReg(XStr& str) = 0;
00092 };
00093 
00094 class ConstructList : public Construct {
00095     Construct *construct;
00096     ConstructList *next;
00097   public:
00098     ConstructList(int l, Construct *c, ConstructList *n=0) :
00099       construct(c), next(n) {line = l;}
00100     void setExtern(int e);
00101     void print(XStr& str);
00102     void genPub(XStr& declstr, XStr& defstr, XStr& defconstr, int& connectPresent);
00103     void genDecls(XStr& str);
00104     void genDefs(XStr& str);
00105     void genReg(XStr& str);
00106 };
00107 
00108 /*********************** Type System **********************/
00109 class Type : public Printable {
00110   public:
00111     virtual void print(XStr&) = 0;
00112     virtual int isVoid(void) const {return 0;}
00113     virtual int isBuiltin(void) const { return 0; }
00114     virtual int isMessage(void) const {return 0;}
00115     virtual int isTemplated(void) const { return 0; }
00116     virtual int isPointer(void) const {return 0;}
00117     virtual int isNamed(void) const { return 0; }
00118     virtual int isCkArgMsgPtr(void) const {return 0;}
00119     virtual int isCkArgMsg(void) const {return 0;}
00120     virtual int isCkMigMsgPtr(void) const {return 0;}
00121     virtual int isCkMigMsg(void) const {return 0;}
00122     virtual int isReference(void) const {return 0;}
00123     virtual Type *deref(void) {return this;}
00124     virtual char *getBaseName(void) const = 0;
00125     virtual int getNumStars(void) const {return 0;}
00126     virtual void genProxyName(XStr &str,forWhom forElement);
00127     virtual void genIndexName(XStr &str);
00128     virtual void genMsgProxyName(XStr& str);
00129     XStr proxyName(forWhom w)
00130         {XStr ret; genProxyName(ret,w); return ret;}
00131     XStr indexName(void) 
00132         {XStr ret; genIndexName(ret); return ret;}
00133     XStr msgProxyName(void) 
00134         {XStr ret; genMsgProxyName(ret); return ret;}
00135     virtual void printVar(XStr &str, char *var) {print(str); str<<" "; str<<var;}
00136     int operator==(const Type &tp) const {
00137       return  (strcmp(getBaseName(), tp.getBaseName())==0);
00138     }
00139 };
00140 
00141 class BuiltinType : public Type {
00142   private:
00143     char *name;
00144   public:
00145     BuiltinType(const char *n) : name((char *)n) {}
00146     int isBuiltin(void) const {return 1;}
00147     void print(XStr& str) { str << name; }
00148     int isVoid(void) const { return !strcmp(name, "void"); }
00149     int isInt(void) const { return !strcmp(name, "int"); }
00150     char *getBaseName(void) const { return name; }
00151 };
00152 
00153 class NamedType : public Type {
00154   private:
00155     char *name;
00156     TParamList *tparams;
00157   public:
00158     NamedType(const char* n, TParamList* t=0)
00159        : name((char *)n), tparams(t) {}
00160     int isTemplated(void) const { return (tparams!=0); }
00161     int isCkArgMsg(void) const {return 0==strcmp(name,"CkArgMsg");}
00162     int isCkMigMsg(void) const {return 0==strcmp(name,"CkMigrateMessage");}
00163     void print(XStr& str);
00164     int isNamed(void) const {return 1;}
00165     char *getBaseName(void) const { return name; }
00166     virtual void genProxyName(XStr& str,forWhom forElement);
00167     virtual void genIndexName(XStr& str) 
00168     { 
00169       str << Prefix::Index; 
00170       print(str);
00171     }
00172     virtual void genMsgProxyName(XStr& str) 
00173     { 
00174       str << Prefix::Message; print(str);
00175     }
00176 };
00177 
00178 class PtrType : public Type {
00179   private:
00180     Type *type;
00181     int numstars; // level of indirection
00182   public:
00183     PtrType(Type *t) : type(t), numstars(1) {}
00184     int isPointer(void) const {return 1;}
00185     int isCkArgMsgPtr(void) const {return numstars==1 && type->isCkArgMsg();}
00186     int isCkMigMsgPtr(void) const {return numstars==1 && type->isCkMigMsg();}
00187     int isMessage(void) const {return numstars==1 && !type->isBuiltin();}
00188     void indirect(void) { numstars++; }
00189     int getNumStars(void) const {return numstars; }
00190     void print(XStr& str);
00191     char *getBaseName(void) const { return type->getBaseName(); }
00192     virtual void genMsgProxyName(XStr& str) { 
00193       if(numstars != 1) {
00194         die("too many stars-- entry parameter must have form 'MTYPE *msg'"); 
00195       } else {
00196         str << Prefix::Message;
00197         type->print(str);
00198       }
00199     }
00200 };
00201 
00202 /* I don't think these are useful any longer (OSL 11/30/2001) */
00203 class ReferenceType : public Type {
00204   private:
00205     Type *referant;
00206   public:
00207     ReferenceType(Type *t) : referant(t) {}
00208     int isReference(void) const {return 1;}
00209     void print(XStr& str) {str<<referant<<" &";}
00210     virtual Type *deref(void) {return referant;}
00211     char *getBaseName(void) const { return referant->getBaseName(); }
00212 };
00213 /* I don't think these are useful any longer (OSL 11/30/2001)
00214 class ConstType : public Type {
00215   private:
00216     Type *type;
00217   public:
00218     ConstType(Type *t) : type(t) {}
00219     int isConst(void) const {return 1;}
00220     void print(XStr& str) {str<<"const "<<type;}
00221     char *getBaseName(void) const { return type->getBaseName(); }
00222 };
00223 */
00224 //This is used as a list of base classes
00225 class TypeList : public Printable {
00226     Type *type;
00227     TypeList *next;
00228   public:
00229     TypeList(Type *t, TypeList *n=0) : type(t), next(n) {}
00230     int length(void) const;
00231     Type *getFirst(void) {return type;}
00232     Type *getSecond(void) {if (next) return next->getFirst(); return NULL;}
00233     void print(XStr& str);
00234     void genProxyNames(XStr& str, const char *prefix, const char *middle, 
00235                         const char *suffix, const char *sep, forWhom forElement);
00236 };
00237 
00238 /**************** Parameter types & lists (for marshalling) ************/
00239 class Parameter {
00240     Type *type;
00241     const char *name; /*The name of the variable, if any*/
00242     const char *given_name; /*The name of the msg in ci file, if any*/
00243     const char *arrLen; /*The expression for the length of the array;
00244                          NULL if not an array*/
00245     Value *val; /*Initial value, if any*/
00246     int line;
00247     int byReference; //Fake a pass-by-reference (for efficiency)
00248     friend class ParamList;
00249     void pup(XStr &str);
00250     void marshallArraySizes(XStr &str);
00251     void marshallArrayData(XStr &str);
00252     void beginUnmarshall(XStr &str);
00253     void unmarshallArrayData(XStr &str);
00254     void pupAllValues(XStr &str);
00255   public:
00256     Parameter(int Nline,Type *Ntype,const char *Nname=0,
00257         const char *NarrLen=0,Value *Nvalue=0);
00258     void print(XStr &str,int withDefaultValues=0,int useConst=1);
00259     void printAddress(XStr &str);
00260     void printValue(XStr &str);
00261     int isMessage(void) const {return type->isMessage();}
00262     int isVoid(void) const {return type->isVoid();}
00263     int isCkArgMsgPtr(void) const {return type->isCkArgMsgPtr();}
00264     int isCkMigMsgPtr(void) const {return type->isCkMigMsgPtr();}
00265     int isArray(void) const {return arrLen!=NULL;}
00266     Type *getType(void) {return type;}
00267     const char *getArrayLen(void) const {return arrLen;}
00268     const char *getGivenName(void) const {return given_name;}
00269     const char *getName(void) const {return name;}
00270     void printMsg(XStr& str) {
00271       type->print(str);
00272       if(given_name!=0)
00273         str <<given_name;
00274     }
00275     int operator==(const Parameter &parm) const {
00276       return *type == *parm.type;
00277     }
00278 };
00279 class ParamList {
00280     typedef int (Parameter::*pred_t)(void) const;
00281     int orEach(pred_t f);
00282     typedef void (Parameter::*fn_t)(XStr &str);
00283     void callEach(fn_t f,XStr &str);
00284   public:
00285     Parameter *param;
00286     ParamList *next;
00287     ParamList(ParamList *pl) :param(pl->param), next(pl->next) {}
00288     ParamList(Parameter *Nparam,ParamList *Nnext=NULL)
00289         :param(Nparam), next(Nnext) {}
00290     void print(XStr &str,int withDefaultValues=0,int useConst=1);
00291     void printAddress(XStr &str);
00292     void printValue(XStr &str);
00293     int isNamed(void) const {return param->type->isNamed();}
00294     int isBuiltin(void) const {return param->type->isBuiltin();}
00295     int isMessage(void) const {
00296         return (next==NULL) && param->isMessage();
00297     }
00298     const char *getArrayLen(void) const {return param->getArrayLen();}
00299     int isArray(void) const {return param->isArray();}
00300     int isReference(void) const {return param->type->isReference();}
00301     int isVoid(void) const {
00302         return (next==NULL) && param->isVoid();
00303     }
00304     int isPointer(void) const {return param->type->isPointer();}
00305     const char *getGivenName(void) const {return param->getGivenName();}
00306     int isMarshalled(void) const {
00307         return !isVoid() && !isMessage();
00308     }
00309     int isCkArgMsgPtr(void) const {
00310         return (next==NULL) && param->isCkArgMsgPtr();
00311     }
00312     int isCkMigMsgPtr(void) const {
00313         return (next==NULL) && param->isCkMigMsgPtr();
00314     }
00315     int getNumStars(void) const {return param->type->getNumStars(); }
00316     char *getBaseName(void) {
00317         return param->type->getBaseName();
00318     }
00319     void genMsgProxyName(XStr &str) {
00320         param->type->genMsgProxyName(str);
00321     }
00322     void printMsg(XStr& str) {
00323         ParamList *pl;
00324         param->printMsg(str);
00325         pl = next;
00326         while (pl != NULL)
00327         {
00328            str <<", ";
00329            pl->param->printMsg(str);
00330            pl = pl->next;
00331         } 
00332     }
00333     void marshall(XStr &str);
00334     void beginUnmarshall(XStr &str);
00335     void unmarshall(XStr &str, int isFirst=1);
00336     void unmarshallAddress(XStr &str, int isFirst=1);
00337     void pupAllValues(XStr &str);
00338     void endUnmarshall(XStr &str);
00339     int operator==(const ParamList &plist) const {
00340       if (!(*param == *(plist.param))) return 0;
00341       if (!next && !plist.next) return 1;
00342       if (!next || !plist.next) return 0;
00343       return *next ==  *plist.next;
00344     }
00345 };
00346 
00347 class FuncType : public Type {
00348   private:
00349     Type *rtype;
00350     char *name;
00351     ParamList *params;
00352   public:
00353     FuncType(Type* r, char* n, ParamList* p) 
00354         :rtype(r),name(n),params(p) {}
00355     void print(XStr& str) { 
00356       rtype->print(str);
00357       str << "(*" << name << ")(";
00358       if(params)
00359         params->print(str);
00360     }
00361     char *getBaseName(void) const { return name; }
00362 };
00363 
00364 /****************** Template Support **************/
00365 /* Template Instantiation Parameter */
00366 class TParam : public Printable {
00367   public:
00368     virtual void genSpec(XStr& str)=0;
00369 };
00370 
00371 /* List of Template Instantiation parameters */
00372 class TParamList : public Printable {
00373     TParam *tparam;
00374     TParamList *next;
00375   public:
00376     TParamList(TParam *t, TParamList *n=0) : tparam(t), next(n) {}
00377     void print(XStr& str);
00378     void genSpec(XStr& str);
00379 };
00380 
00381 /* A type instantiation parameter */
00382 class TParamType : public TParam {
00383   Type *type;
00384   public:
00385     TParamType(Type *t) : type(t) {}
00386     void print(XStr& str) { type->print(str); }
00387     void genSpec(XStr& str) { type->print(str); }
00388 };
00389 
00390 /* A Value instantiation parameter */
00391 class TParamVal : public TParam {
00392     char *val;
00393   public:
00394     TParamVal(char *v) : val(v) {}
00395     void print(XStr& str) { str << val; }
00396     void genSpec(XStr& str) { str << val; }
00397 };
00398 /* A template construct */
00399 class TVarList;
00400 class TEntity;
00401 
00402 class Template : public Construct {
00403     TVarList *tspec;
00404     TEntity *entity;
00405   public:
00406     Template(TVarList *t, TEntity *e) : tspec(t), entity(e) {}
00407     virtual void setExtern(int e);
00408     void print(XStr& str);
00409     void genPub(XStr& declstr, XStr& defstr, XStr& defconstr, int& connectPresent);
00410     void genDecls(XStr& str);
00411     void genDefs(XStr& str);
00412     void genReg(XStr& str);
00413     void genSpec(XStr& str);
00414     void genVars(XStr& str);
00415 };
00416 
00417 /* An entity that could be templated, i.e. chare, group or a message */
00418 class TEntity : public Construct {
00419   protected:
00420     Template *templat;
00421   public:
00422     void setTemplate(Template *t) { templat = t; }
00423     virtual XStr tspec(void) { 
00424         XStr str; 
00425         if (templat) templat->genSpec(str); 
00426         return str;
00427     }
00428     virtual XStr tvars(void) { 
00429         XStr str;
00430         if (templat) templat->genVars(str); 
00431         return str;
00432     }
00433 };
00434 /* A formal argument of a template */
00435 class TVar : public Printable {
00436   public:
00437     virtual void genLong(XStr& str) = 0;
00438     virtual void genShort(XStr& str) = 0;
00439 };
00440 
00441 /* a formal type argument */
00442 class TType : public TVar {
00443     Type *type;
00444     Type *init;
00445   public:
00446     TType(Type *t, Type *i=0) : type(t), init(i) {}
00447     void print(XStr& str);
00448     void genLong(XStr& str);
00449     void genShort(XStr& str);
00450 };
00451 
00452 /* a formal function argument */
00453 class TFunc : public TVar {
00454     FuncType *type;
00455     char *init;
00456   public:
00457     TFunc(FuncType *t, char *v=0) : type(t), init(v) {}
00458     void print(XStr& str) { type->print(str); if(init) str << "=" << init; }
00459     void genLong(XStr& str){ type->print(str); if(init) str << "=" << init; }
00460     void genShort(XStr& str) {str << type->getBaseName(); }
00461 };
00462 
00463 /* A formal variable argument */
00464 class TName : public TVar {
00465     Type *type;
00466     char *name;
00467     char *val;
00468   public:
00469     TName(Type *t, char *n, char *v=0) : type(t), name(n), val(v) {}
00470     void print(XStr& str);
00471     void genLong(XStr& str);
00472     void genShort(XStr& str);
00473 };
00474 
00475 /* A list of formal arguments to a template */
00476 class TVarList : public Printable {
00477     TVar *tvar;
00478     TVarList *next;
00479   public:
00480     TVarList(TVar *v, TVarList *n=0) : tvar(v), next(n) {}
00481     void print(XStr& str);
00482     void genLong(XStr& str);
00483     void genShort(XStr& str);
00484 };
00485 
00486 /******************* Chares, Arrays, Groups ***********/
00487 
00488 /* Member of a chare or group, i.e. entry, RO or ROM */
00489 class Member : public Construct {
00490    //friend class CParsedFile;
00491   protected:
00492     Chare *container;
00493   public:
00494     virtual void setChare(Chare *c) { container = c; }
00495     virtual int isSdag(void) { return 0; }
00496     virtual void collectSdagCode(CParsedFile *pf, int& sdagPresent) { return; }
00497     XStr makeDecl(const XStr &returnType,int forProxy=0);
00498     virtual void genPythonDecls(XStr& str) {}
00499     virtual void genIndexDecls(XStr& str)=0;
00500     virtual void genPythonDefs(XStr& str) {}
00501     virtual void genPythonStaticDefs(XStr& str) {}
00502     virtual void genPythonStaticDocs(XStr& str) {}
00503     virtual void lookforCEntry(CEntry *centry)  {}
00504 };
00505 
00506 /* List of members of a chare or group */
00507 class MemberList : public Printable {
00508     //friend class CParsedFile;
00509   public:
00510     Member *member;
00511     MemberList *next;
00512     MemberList(Member *m, MemberList *n=0) : member(m), next(n) {}
00513     void appendMember(Member *m) 
00514     {
00515       MemberList *temp;
00516       if (next != 0) {
00517         temp = next;
00518         while ( temp->next != 0) {
00519           temp = temp->next;
00520         }
00521         temp->next = new MemberList(m, 0);
00522       }
00523       else
00524         next = new MemberList(m, 0);
00525      
00526 
00527     }
00528     void print(XStr& str);
00529     void setChare(Chare *c);
00530     void genPub(XStr& declstr, XStr& defstr, XStr& defconstr, int& connectPresent);
00531     void genDecls(XStr& str);
00532     void genIndexDecls(XStr& str);
00533     void genPythonDecls(XStr& str);
00534     void genDefs(XStr& str);
00535     void genReg(XStr& str);
00536     void genPythonDefs(XStr& str);
00537     void genPythonStaticDefs(XStr& str);
00538     void genPythonStaticDocs(XStr& str);
00539     void collectSdagCode(CParsedFile *pf, int& sdagPresent);
00540     virtual void lookforCEntry(CEntry *centry);
00541 };
00542 
00543 /* Chare or group is a templated entity */
00544 class Chare : public TEntity {
00545   public:
00546     enum { //Set these attribute bits in "attrib"
00547         CMIGRATABLE=1<<2,
00548         CPYTHON=1<<3,
00549         CMAINCHARE=1<<10,
00550         CARRAY=1<<11,
00551         CGROUP=1<<12,
00552         CNODEGROUP=1<<13
00553     };
00554     typedef unsigned int attrib_t;
00555   protected:
00556     attrib_t attrib;
00557     int hasElement;//0-- no element type; 1-- has element type
00558     forWhom forElement;
00559     int hasSection; //1-- applies only to array section
00560 
00561     NamedType *type;
00562     MemberList *list;
00563     TypeList *bases; //Base classes used by proxy
00564     TypeList *bases_CBase; //Base classes used by CBase (or NULL)
00565     
00566     int entryCount;
00567     int hasSdagEntry;
00568 
00569     void genTypedefs(XStr& str);
00570     void genRegisterMethodDef(XStr& str);
00571     void sharedDisambiguation(XStr &str,const XStr &superclass);
00572   public:
00573     Chare(int ln, attrib_t Nattr,
00574         NamedType *t, TypeList *b=0, MemberList *l=0);
00575     void genProxyNames(XStr& str, const char *prefix, const char *middle, 
00576                         const char *suffix, const char *sep);
00577     void genIndexNames(XStr& str, const