00001 #ifndef _TYPE_H
00002 #define _TYPE_H
00003
00004 #include "xi-util.h"
00005
00006 namespace xi {
00007
00008 class TParamList;
00009 class ParamList;
00010
00011
00012 class Type : public Printable {
00013 public:
00014 virtual void print(XStr&) = 0;
00015 virtual int isVoid(void) const { return 0; }
00016 virtual int isBuiltin(void) const { return 0; }
00017 virtual int isMessage(void) const { return 0; }
00018 virtual int isTemplated(void) const { return 0; }
00019 virtual int isPointer(void) const { return 0; }
00020 virtual int isNamed(void) const { return 0; }
00021 virtual int isCkArgMsgPtr(void) const { return 0; }
00022 virtual int isCkArgMsg(void) const { return 0; }
00023 virtual int isCkMigMsgPtr(void) const { return 0; }
00024 virtual int isCkMigMsg(void) const { return 0; }
00025 virtual int isReference(void) const { return 0; }
00026 virtual int isInt(void) const { return 0; }
00027 virtual bool isConst(void) const { return false; }
00028 virtual Type* deref(void) { return this; }
00029 virtual const char* getBaseName(void) const = 0;
00030 virtual const char* getScope(void) const = 0;
00031 virtual int getNumStars(void) const { return 0; }
00032 virtual void genProxyName(XStr& str, forWhom forElement);
00033 virtual void genIndexName(XStr& str);
00034 virtual void genMsgProxyName(XStr& str);
00035 XStr proxyName(forWhom w) {
00036 XStr ret;
00037 genProxyName(ret, w);
00038 return ret;
00039 }
00040 XStr indexName(void) {
00041 XStr ret;
00042 genIndexName(ret);
00043 return ret;
00044 }
00045 XStr msgProxyName(void) {
00046 XStr ret;
00047 genMsgProxyName(ret);
00048 return ret;
00049 }
00050 virtual void printVar(XStr& str, char* var) {
00051 print(str);
00052 str << " ";
00053 str << var;
00054 }
00055 int operator==(const Type& tp) const {
00056 return (strcmp(getBaseName(), tp.getBaseName()) == 0);
00057 }
00058 virtual ~Type() {}
00059 };
00060
00061 class BuiltinType : public Type {
00062 private:
00063 char* name;
00064
00065 public:
00066 BuiltinType(const char* n) : name((char*)n) {}
00067 int isBuiltin(void) const { return 1; }
00068 void print(XStr& str) { str << name; }
00069 int isVoid(void) const { return !strcmp(name, "void"); }
00070 int isInt(void) const { return !strcmp(name, "int"); }
00071 const char* getBaseName(void) const { return name; }
00072 const char* getScope(void) const { return NULL; }
00073 };
00074
00075 class NamedType : public Type {
00076 protected:
00077 const char* name;
00078 const char* scope;
00079 TParamList* tparams;
00080 bool useTypename;
00081
00082 public:
00083 NamedType(const char* n, TParamList* t = 0, const char* scope_ = NULL, bool useTypename_ = false)
00084 : name(n), scope(scope_), tparams(t), useTypename(useTypename_) {}
00085 int isTemplated(void) const { return (tparams != 0); }
00086 int isCkArgMsg(void) const { return 0 == strcmp(name, "CkArgMsg"); }
00087 int isCkMigMsg(void) const { return 0 == strcmp(name, "CkMigrateMessage"); }
00088 int isVector(void) const { return 0 == strcmp(name, "vector"); }
00089 void print(XStr& str);
00090 int isNamed(void) const { return 1; }
00091 TParamList *getTparams(void) const { return tparams; }
00092 virtual const char* getBaseName(void) const { return name; }
00093 virtual const char* getScope(void) const { return scope; }
00094 virtual void genProxyName(XStr& str, forWhom forElement);
00095 virtual void genIndexName(XStr& str);
00096 virtual void genMsgProxyName(XStr& str);
00097 };
00098
00099 class NamedEllipsisType : public NamedType {
00100 protected:
00101 XStr nameWithEllipsis;
00102 public:
00103 NamedEllipsisType(const char* n)
00104 : NamedType (n) {
00105 nameWithEllipsis << n;
00106 nameWithEllipsis << "...";
00107 }
00108 void print(XStr& str);
00109 void printWithoutEllipsis(XStr& str);
00110 virtual const char* getBaseName(void) const { return nameWithEllipsis.get_string_const(); }
00111 };
00112
00113 class PtrType : public Type {
00114 private:
00115 Type* type;
00116 int numstars;
00117
00118 public:
00119 PtrType(Type* t) : type(t), numstars(1) {}
00120 int isPointer(void) const { return 1; }
00121 int isCkArgMsgPtr(void) const { return numstars == 1 && type->isCkArgMsg(); }
00122 int isCkMigMsgPtr(void) const { return numstars == 1 && type->isCkMigMsg(); }
00123 int isMessage(void) const { return numstars == 1 && !type->isBuiltin(); }
00124 void indirect(void) { numstars++; }
00125 int getNumStars(void) const { return numstars; }
00126 void print(XStr& str);
00127 Type* deref(void) { return type; }
00128 const char* getBaseName(void) const { return type->getBaseName(); }
00129 const char* getScope(void) const { return NULL; }
00130 virtual void genMsgProxyName(XStr& str) {
00131 if (numstars != 1) {
00132 die("too many stars-- entry parameter must have form 'MTYPE *msg'");
00133 } else {
00134 type->genMsgProxyName(str);
00135 }
00136 }
00137 };
00138
00139 class EllipsisType : public Type {
00140 private:
00141 Type* referant;
00142
00143 public:
00144 EllipsisType(Type* t) : referant(t) {}
00145 void print(XStr& str) { str << referant << "..."; }
00146 virtual Type* deref(void) { return referant; }
00147 const char* getBaseName(void) const { return referant->getBaseName(); }
00148 const char* getScope(void) const { return NULL; }
00149 };
00150
00151 class RValueReferenceType : public Type {
00152 private:
00153 Type* referant;
00154
00155 public:
00156 RValueReferenceType(Type* t) : referant(t) {}
00157 int isReference(void) const { return 1; }
00158 void print(XStr& str) { str << referant << " &&"; }
00159 virtual Type* deref(void) { return referant; }
00160 const char* getBaseName(void) const { return referant->getBaseName(); }
00161 const char* getScope(void) const { return NULL; }
00162 };
00163
00164 class ReferenceType : public Type {
00165 private:
00166 Type* referant;
00167
00168 public:
00169 ReferenceType(Type* t) : referant(t) {}
00170 int isReference(void) const { return 1; }
00171 void print(XStr& str) { str << referant << " &"; }
00172 virtual Type* deref(void) { return referant; }
00173 const char* getBaseName(void) const { return referant->getBaseName(); }
00174 const char* getScope(void) const { return NULL; }
00175 };
00176
00177 class ConstType : public Type {
00178 private:
00179 Type* constType;
00180
00181 public:
00182 ConstType(Type* t) : constType(t) {}
00183 void print(XStr& str) { str << "const " << constType; }
00184 virtual bool isConst(void) const { return true; }
00185 virtual Type* deref(void) { return constType; }
00186 const char* getBaseName(void) const { return constType->getBaseName(); }
00187 const char* getScope(void) const { return NULL; }
00188 };
00189
00190 class FuncType : public Type {
00191 private:
00192 Type* rtype;
00193 const char* name;
00194 ParamList* params;
00195
00196 public:
00197 FuncType(Type* r, const char* n, ParamList* p) : rtype(r), name(n), params(p) {}
00198 void print(XStr& str);
00199 const char* getBaseName(void) const { return name; }
00200 const char* getScope(void) const { return NULL; }
00201 };
00202
00203
00204 class TypeList : public Printable {
00205 public:
00206 Type* type;
00207 TypeList* next;
00208 TypeList(Type* t, TypeList* n = 0) : type(t), next(n) {}
00209 ~TypeList() {
00210 delete type;
00211 delete next;
00212 }
00213 int length(void) const;
00214 Type* getFirst(void) { return type; }
00215 void print(XStr& str);
00216 void genProxyNames(XStr& str, const char* prefix, const char* middle,
00217 const char* suffix, const char* sep, forWhom forElement);
00218 };
00219
00220 }
00221
00222 #endif // ifndef _TYPE_H