00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 #include "comma/ast/AttribExpr.h"
00010 #include "comma/ast/Expr.h"
00011 #include "comma/ast/KeywordSelector.h"
00012 
00013 #include <cstring>
00014 
00015 using namespace comma;
00016 using llvm::dyn_cast;
00017 using llvm::cast;
00018 using llvm::isa;
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 bool Expr::isMutable(Expr *&immutable)
00027 {
00028     
00029     Expr *cursor = this;
00030 
00031 TRY_AGAIN:
00032     AstKind kind = cursor->getKind();
00033 
00034     
00035     
00036     
00037     
00038     
00039     
00040     if (kind == AST_DeclRefExpr) {
00041         DeclRefExpr *ref = cast<DeclRefExpr>(cursor);
00042         ValueDecl *decl = ref->getDeclaration();
00043         bool result = true;
00044 
00045         
00046         if (!isa<ObjectDecl>(decl)) {
00047             kind = decl->getKind();
00048             switch (kind) {
00049 
00050             case AST_ParamValueDecl: {
00051                 ParamValueDecl *PVD = cast<ParamValueDecl>(decl);
00052                 if (PVD->getParameterMode() == PM::MODE_IN) {
00053                     result = false;
00054                     immutable = cursor;
00055                 }
00056                 break;
00057             }
00058 
00059             case AST_RenamedObjectDecl: {
00060                 
00061                 RenamedObjectDecl *ROD = cast<RenamedObjectDecl>(decl);
00062                 result = ROD->getRenamedExpr()->isMutable(immutable);
00063                 break;
00064             }
00065 
00066             default:
00067                 result = false;
00068                 immutable = cursor;
00069                 break;
00070             }
00071         }
00072         return result;
00073     }
00074 
00075     
00076     
00077     
00078     switch (kind) {
00079 
00080     default:
00081         
00082         immutable = cursor;
00083         return 0;
00084 
00085     case AST_SelectedExpr:
00086         cursor = cast<SelectedExpr>(cursor)->getPrefix();
00087         break;
00088 
00089     case AST_IndexedArrayExpr:
00090         cursor = cast<IndexedArrayExpr>(cursor)->getPrefix();
00091         break;
00092 
00093     case AST_DereferenceExpr:
00094         cursor = cast<DereferenceExpr>(cursor)->getPrefix();
00095         break;
00096 
00097     case AST_InjExpr:
00098         cursor = cast<InjExpr>(cursor)->getOperand();
00099         break;
00100 
00101     case AST_PrjExpr:
00102         cursor = cast<PrjExpr>(cursor)->getOperand();
00103         break;
00104     }
00105 
00106     
00107     goto TRY_AGAIN;
00108 }
00109 
00110 Expr *Expr::ignoreInjPrj()
00111 {
00112     Expr *cursor = this;
00113 
00114     for (;;) {
00115         switch (cursor->getKind()) {
00116 
00117         default:
00118             return cursor;
00119 
00120         case Ast::AST_InjExpr:
00121             cursor = cast<InjExpr>(cursor)->getOperand();
00122             break;
00123 
00124         case Ast::AST_PrjExpr:
00125             cursor = cast<PrjExpr>(cursor)->getOperand();
00126             break;
00127         }
00128     }
00129 }
00130 
00131 bool Expr::denotesName() const
00132 {
00133     bool result = false;
00134 
00135     switch (getKind()) {
00136 
00137     default:
00138         result = isa<AttribExpr>(this);
00139         break;
00140 
00141     case AST_DeclRefExpr:
00142     case AST_SelectedExpr:
00143     case AST_IndexedArrayExpr:
00144     case AST_ConversionExpr:
00145     case AST_DereferenceExpr:
00146     case AST_InjExpr:
00147     case AST_PrjExpr:
00148         result = true;
00149         break;
00150 
00151     case AST_FunctionCallExpr:
00152         result = !cast<FunctionCallExpr>(this)->denotesOperator();
00153         break;
00154     }
00155 
00156     return result;
00157 }
00158 
00159 
00160 
00161 
00162 FunctionCallExpr::FunctionCallExpr(SubroutineRef *connective,
00163                                    Expr **posArgs, unsigned numPos,
00164                                    KeywordSelector **keyArgs, unsigned numKeys)
00165     : Expr(AST_FunctionCallExpr, connective->getLocation()),
00166       SubroutineCall(connective, posArgs, numPos, keyArgs, numKeys)
00167 {
00168     setTypeForConnective();
00169 }
00170 
00171 FunctionCallExpr::FunctionCallExpr(FunctionDecl *connective, Location loc,
00172                                    Expr **posArgs, unsigned numPos,
00173                                    KeywordSelector **keyArgs, unsigned numKeys)
00174     : Expr(AST_FunctionCallExpr, loc),
00175       SubroutineCall(connective, posArgs, numPos, keyArgs, numKeys)
00176 {
00177     setTypeForConnective();
00178 }
00179 
00180 FunctionCallExpr::FunctionCallExpr(SubroutineRef *connective)
00181     : Expr(AST_FunctionCallExpr, connective->getLocation()),
00182       SubroutineCall(connective, 0, 0, 0, 0)
00183 {
00184     setTypeForConnective();
00185 }
00186 
00187 FunctionCallExpr::FunctionCallExpr(FunctionDecl *connective, Location loc)
00188     : Expr(AST_FunctionCallExpr, loc),
00189       SubroutineCall(connective, 0, 0, 0, 0)
00190 {
00191     setTypeForConnective();
00192 }
00193 
00194 void FunctionCallExpr::setTypeForConnective()
00195 {
00196     if (isUnambiguous()) {
00197         FunctionDecl *fdecl = getConnective();
00198         setType(fdecl->getReturnType());
00199     }
00200 }
00201 
00202 void FunctionCallExpr::resolveConnective(FunctionDecl *decl)
00203 {
00204     SubroutineCall::resolveConnective(decl);
00205     setTypeForConnective();
00206 }
00207 
00208 
00209 
00210 
00211 IndexedArrayExpr::IndexedArrayExpr(Expr *arrExpr,
00212                                    Expr **indices, unsigned numIndices)
00213     : Expr(AST_IndexedArrayExpr, arrExpr->getLocation()),
00214       indexedArray(arrExpr),
00215       numIndices(numIndices)
00216 {
00217     assert(numIndices != 0 && "Missing indices!");
00218 
00219     if (arrExpr->hasType()) {
00220         ArrayType *arrTy = cast<ArrayType>(arrExpr->getType());
00221         setType(arrTy->getComponentType());
00222     }
00223 
00224     indexExprs = new Expr*[numIndices];
00225     std::copy(indices, indices + numIndices, indexExprs);
00226 }
00227 
00228 
00229 
00230 
00231 void StringLiteral::init(const char *string, unsigned len)
00232 {
00233     this->rep = new char[len];
00234     this->len = len;
00235     std::strncpy(this->rep, string, len);
00236 }
00237 
00238 StringLiteral::const_component_iterator
00239 StringLiteral::findComponent(EnumerationType *type) const
00240 {
00241     EnumerationType *root = type->getRootType();
00242 
00243     const_component_iterator I = begin_component_types();
00244     const_component_iterator E = end_component_types();
00245     for ( ; I != E; ++I) {
00246         const EnumerationDecl *decl = *I;
00247         if (root == decl->getType()->getRootType())
00248             return I;
00249     }
00250     return E;
00251 }
00252 
00253 StringLiteral::component_iterator
00254 StringLiteral::findComponent(EnumerationType *type)
00255 {
00256     EnumerationType *root = type->getRootType();
00257 
00258     component_iterator I = begin_component_types();
00259     component_iterator E = end_component_types();
00260     for ( ; I != E; ++I) {
00261         EnumerationDecl *decl = *I;
00262         if (root == decl->getType()->getRootType())
00263             return I;
00264     }
00265     return E;
00266 }
00267 
00268 bool StringLiteral::resolveComponentType(EnumerationType *type)
00269 {
00270     component_iterator I = findComponent(type);
00271 
00272     if (I == end_component_types())
00273         return false;
00274 
00275     EnumerationDecl *decl = *I;
00276     interps.clear();
00277     interps.insert(decl);
00278     return true;
00279 }
00280 
00281 
00282 
00283 
00284 DereferenceExpr::DereferenceExpr(Expr *prefix, Location loc, bool isImplicit)
00285     : Expr(AST_DereferenceExpr, loc),
00286       prefix(prefix)
00287 {
00288     AccessType *prefixType = cast<AccessType>(prefix->getType());
00289     setType(prefixType->getTargetType());
00290 
00291     bits = isImplicit;
00292 }