clang API Documentation

ExprCXX.cpp
Go to the documentation of this file.
00001 //===--- ExprCXX.cpp - (C++) Expression AST Node Implementation -----------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the subclesses of Expr class declared in ExprCXX.h
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "clang/Basic/IdentifierTable.h"
00015 #include "clang/AST/DeclCXX.h"
00016 #include "clang/AST/DeclTemplate.h"
00017 #include "clang/AST/ExprCXX.h"
00018 #include "clang/AST/TypeLoc.h"
00019 using namespace clang;
00020 
00021 
00022 //===----------------------------------------------------------------------===//
00023 //  Child Iterators for iterating over subexpressions/substatements
00024 //===----------------------------------------------------------------------===//
00025 
00026 QualType CXXTypeidExpr::getTypeOperand() const {
00027   assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)");
00028   return Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType()
00029                                                         .getUnqualifiedType();
00030 }
00031 
00032 QualType CXXUuidofExpr::getTypeOperand() const {
00033   assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)");
00034   return Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType()
00035                                                         .getUnqualifiedType();
00036 }
00037 
00038 // CXXScalarValueInitExpr
00039 SourceRange CXXScalarValueInitExpr::getSourceRange() const {
00040   SourceLocation Start = RParenLoc;
00041   if (TypeInfo)
00042     Start = TypeInfo->getTypeLoc().getBeginLoc();
00043   return SourceRange(Start, RParenLoc);
00044 }
00045 
00046 // CXXNewExpr
00047 CXXNewExpr::CXXNewExpr(ASTContext &C, bool globalNew, FunctionDecl *operatorNew,
00048                        FunctionDecl *operatorDelete,
00049                        bool usualArrayDeleteWantsSize,
00050                        Expr **placementArgs, unsigned numPlaceArgs,
00051                        SourceRange typeIdParens, Expr *arraySize,
00052                        InitializationStyle initializationStyle,
00053                        Expr *initializer, QualType ty,
00054                        TypeSourceInfo *allocatedTypeInfo,
00055                        SourceLocation startLoc, SourceRange directInitRange)
00056   : Expr(CXXNewExprClass, ty, VK_RValue, OK_Ordinary,
00057          ty->isDependentType(), ty->isDependentType(),
00058          ty->isInstantiationDependentType(),
00059          ty->containsUnexpandedParameterPack()),
00060     SubExprs(0), OperatorNew(operatorNew), OperatorDelete(operatorDelete),
00061     AllocatedTypeInfo(allocatedTypeInfo), TypeIdParens(typeIdParens),
00062     StartLoc(startLoc), DirectInitRange(directInitRange),
00063     GlobalNew(globalNew), UsualArrayDeleteWantsSize(usualArrayDeleteWantsSize) {
00064   assert((initializer != 0 || initializationStyle == NoInit) &&
00065          "Only NoInit can have no initializer.");
00066   StoredInitializationStyle = initializer ? initializationStyle + 1 : 0;
00067   AllocateArgsArray(C, arraySize != 0, numPlaceArgs, initializer != 0);
00068   unsigned i = 0;
00069   if (Array) {
00070     if (arraySize->isInstantiationDependent())
00071       ExprBits.InstantiationDependent = true;
00072     
00073     if (arraySize->containsUnexpandedParameterPack())
00074       ExprBits.ContainsUnexpandedParameterPack = true;
00075 
00076     SubExprs[i++] = arraySize;
00077   }
00078 
00079   if (initializer) {
00080     if (initializer->isInstantiationDependent())
00081       ExprBits.InstantiationDependent = true;
00082 
00083     if (initializer->containsUnexpandedParameterPack())
00084       ExprBits.ContainsUnexpandedParameterPack = true;
00085 
00086     SubExprs[i++] = initializer;
00087   }
00088 
00089   for (unsigned j = 0; j < NumPlacementArgs; ++j) {
00090     if (placementArgs[j]->isInstantiationDependent())
00091       ExprBits.InstantiationDependent = true;
00092     if (placementArgs[j]->containsUnexpandedParameterPack())
00093       ExprBits.ContainsUnexpandedParameterPack = true;
00094 
00095     SubExprs[i++] = placementArgs[j];
00096   }
00097 }
00098 
00099 void CXXNewExpr::AllocateArgsArray(ASTContext &C, bool isArray,
00100                                    unsigned numPlaceArgs, bool hasInitializer){
00101   assert(SubExprs == 0 && "SubExprs already allocated");
00102   Array = isArray;
00103   NumPlacementArgs = numPlaceArgs;
00104 
00105   unsigned TotalSize = Array + hasInitializer + NumPlacementArgs;
00106   SubExprs = new (C) Stmt*[TotalSize];
00107 }
00108 
00109 bool CXXNewExpr::shouldNullCheckAllocation(ASTContext &Ctx) const {
00110   return getOperatorNew()->getType()->
00111     castAs<FunctionProtoType>()->isNothrow(Ctx);
00112 }
00113 
00114 SourceLocation CXXNewExpr::getEndLoc() const {
00115   switch (getInitializationStyle()) {
00116   case NoInit:
00117     return AllocatedTypeInfo->getTypeLoc().getEndLoc();
00118   case CallInit:
00119     return DirectInitRange.getEnd();
00120   case ListInit:
00121     return getInitializer()->getSourceRange().getEnd();
00122   }
00123   llvm_unreachable("bogus initialization style");
00124 }
00125 
00126 // CXXDeleteExpr
00127 QualType CXXDeleteExpr::getDestroyedType() const {
00128   const Expr *Arg = getArgument();
00129   while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) {
00130     if (ICE->getCastKind() != CK_UserDefinedConversion &&
00131         ICE->getType()->isVoidPointerType())
00132       Arg = ICE->getSubExpr();
00133     else
00134       break;
00135   }
00136   // The type-to-delete may not be a pointer if it's a dependent type.
00137   const QualType ArgType = Arg->getType();
00138 
00139   if (ArgType->isDependentType() && !ArgType->isPointerType())
00140     return QualType();
00141 
00142   return ArgType->getAs<PointerType>()->getPointeeType();
00143 }
00144 
00145 // CXXPseudoDestructorExpr
00146 PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
00147  : Type(Info) 
00148 {
00149   Location = Info->getTypeLoc().getLocalSourceRange().getBegin();
00150 }
00151 
00152 CXXPseudoDestructorExpr::CXXPseudoDestructorExpr(ASTContext &Context,
00153                 Expr *Base, bool isArrow, SourceLocation OperatorLoc,
00154                 NestedNameSpecifierLoc QualifierLoc, TypeSourceInfo *ScopeType, 
00155                 SourceLocation ColonColonLoc, SourceLocation TildeLoc, 
00156                 PseudoDestructorTypeStorage DestroyedType)
00157   : Expr(CXXPseudoDestructorExprClass,
00158          Context.getPointerType(Context.getFunctionType(Context.VoidTy, 0, 0,
00159                                          FunctionProtoType::ExtProtoInfo())),
00160          VK_RValue, OK_Ordinary,
00161          /*isTypeDependent=*/(Base->isTypeDependent() ||
00162            (DestroyedType.getTypeSourceInfo() &&
00163             DestroyedType.getTypeSourceInfo()->getType()->isDependentType())),
00164          /*isValueDependent=*/Base->isValueDependent(),
00165          (Base->isInstantiationDependent() ||
00166           (QualifierLoc &&
00167            QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent()) ||
00168           (ScopeType &&
00169            ScopeType->getType()->isInstantiationDependentType()) ||
00170           (DestroyedType.getTypeSourceInfo() &&
00171            DestroyedType.getTypeSourceInfo()->getType()
00172                                              ->isInstantiationDependentType())),
00173          // ContainsUnexpandedParameterPack
00174          (Base->containsUnexpandedParameterPack() ||
00175           (QualifierLoc && 
00176            QualifierLoc.getNestedNameSpecifier()
00177                                         ->containsUnexpandedParameterPack()) ||
00178           (ScopeType && 
00179            ScopeType->getType()->containsUnexpandedParameterPack()) ||
00180           (DestroyedType.getTypeSourceInfo() &&
00181            DestroyedType.getTypeSourceInfo()->getType()
00182                                    ->containsUnexpandedParameterPack()))),
00183     Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
00184     OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
00185     ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc),
00186     DestroyedType(DestroyedType) { }
00187 
00188 QualType CXXPseudoDestructorExpr::getDestroyedType() const {
00189   if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
00190     return TInfo->getType();
00191   
00192   return QualType();
00193 }
00194 
00195 SourceRange CXXPseudoDestructorExpr::getSourceRange() const {
00196   SourceLocation End = DestroyedType.getLocation();
00197   if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
00198     End = TInfo->getTypeLoc().getLocalSourceRange().getEnd();
00199   return SourceRange(Base->getLocStart(), End);
00200 }
00201 
00202 // UnresolvedLookupExpr
00203 UnresolvedLookupExpr *
00204 UnresolvedLookupExpr::Create(ASTContext &C, 
00205                              CXXRecordDecl *NamingClass,
00206                              NestedNameSpecifierLoc QualifierLoc,
00207                              SourceLocation TemplateKWLoc,
00208                              const DeclarationNameInfo &NameInfo,
00209                              bool ADL,
00210                              const TemplateArgumentListInfo *Args,
00211                              UnresolvedSetIterator Begin,
00212                              UnresolvedSetIterator End)
00213 {
00214   assert(Args || TemplateKWLoc.isValid());
00215   unsigned num_args = Args ? Args->size() : 0;
00216   void *Mem = C.Allocate(sizeof(UnresolvedLookupExpr) +
00217                          ASTTemplateKWAndArgsInfo::sizeFor(num_args));
00218   return new (Mem) UnresolvedLookupExpr(C, NamingClass, QualifierLoc,
00219                                         TemplateKWLoc, NameInfo,
00220                                         ADL, /*Overload*/ true, Args,
00221                                         Begin, End, /*StdIsAssociated=*/false);
00222 }
00223 
00224 UnresolvedLookupExpr *
00225 UnresolvedLookupExpr::CreateEmpty(ASTContext &C,
00226                                   bool HasTemplateKWAndArgsInfo,
00227                                   unsigned NumTemplateArgs) {
00228   std::size_t size = sizeof(UnresolvedLookupExpr);
00229   if (HasTemplateKWAndArgsInfo)
00230     size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
00231 
00232   void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedLookupExpr>());
00233   UnresolvedLookupExpr *E = new (Mem) UnresolvedLookupExpr(EmptyShell());
00234   E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
00235   return E;
00236 }
00237 
00238 OverloadExpr::OverloadExpr(StmtClass K, ASTContext &C, 
00239                            NestedNameSpecifierLoc QualifierLoc,
00240                            SourceLocation TemplateKWLoc,
00241                            const DeclarationNameInfo &NameInfo,
00242                            const TemplateArgumentListInfo *TemplateArgs,
00243                            UnresolvedSetIterator Begin, 
00244                            UnresolvedSetIterator End,
00245                            bool KnownDependent,
00246                            bool KnownInstantiationDependent,
00247                            bool KnownContainsUnexpandedParameterPack)
00248   : Expr(K, C.OverloadTy, VK_LValue, OK_Ordinary, KnownDependent, 
00249          KnownDependent,
00250          (KnownInstantiationDependent ||
00251           NameInfo.isInstantiationDependent() ||
00252           (QualifierLoc &&
00253            QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
00254          (KnownContainsUnexpandedParameterPack ||
00255           NameInfo.containsUnexpandedParameterPack() ||
00256           (QualifierLoc && 
00257            QualifierLoc.getNestedNameSpecifier()
00258                                       ->containsUnexpandedParameterPack()))),
00259     NameInfo(NameInfo), QualifierLoc(QualifierLoc),
00260     Results(0), NumResults(End - Begin),
00261     HasTemplateKWAndArgsInfo(TemplateArgs != 0 || TemplateKWLoc.isValid())
00262 {
00263   NumResults = End - Begin;
00264   if (NumResults) {
00265     // Determine whether this expression is type-dependent.
00266     for (UnresolvedSetImpl::const_iterator I = Begin; I != End; ++I) {
00267       if ((*I)->getDeclContext()->isDependentContext() ||
00268           isa<UnresolvedUsingValueDecl>(*I)) {
00269         ExprBits.TypeDependent = true;
00270         ExprBits.ValueDependent = true;
00271       }
00272     }
00273 
00274     Results = static_cast<DeclAccessPair *>(
00275                                 C.Allocate(sizeof(DeclAccessPair) * NumResults, 
00276                                            llvm::alignOf<DeclAccessPair>()));
00277     memcpy(Results, &*Begin.getIterator(), 
00278            NumResults * sizeof(DeclAccessPair));
00279   }
00280 
00281   // If we have explicit template arguments, check for dependent
00282   // template arguments and whether they contain any unexpanded pack
00283   // expansions.
00284   if (TemplateArgs) {
00285     bool Dependent = false;
00286     bool InstantiationDependent = false;
00287     bool ContainsUnexpandedParameterPack = false;
00288     getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
00289                                                Dependent,
00290                                                InstantiationDependent,
00291                                                ContainsUnexpandedParameterPack);
00292 
00293     if (Dependent) {
00294       ExprBits.TypeDependent = true;
00295       ExprBits.ValueDependent = true;
00296     }
00297     if (InstantiationDependent)
00298       ExprBits.InstantiationDependent = true;
00299     if (ContainsUnexpandedParameterPack)
00300       ExprBits.ContainsUnexpandedParameterPack = true;
00301   } else if (TemplateKWLoc.isValid()) {
00302     getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
00303   }
00304 
00305   if (isTypeDependent())
00306     setType(C.DependentTy);
00307 }
00308 
00309 void OverloadExpr::initializeResults(ASTContext &C,
00310                                      UnresolvedSetIterator Begin,
00311                                      UnresolvedSetIterator End) {
00312   assert(Results == 0 && "Results already initialized!");
00313   NumResults = End - Begin;
00314   if (NumResults) {
00315      Results = static_cast<DeclAccessPair *>(
00316                                C.Allocate(sizeof(DeclAccessPair) * NumResults,
00317  
00318                                           llvm::alignOf<DeclAccessPair>()));
00319      memcpy(Results, &*Begin.getIterator(), 
00320             NumResults * sizeof(DeclAccessPair));
00321   }
00322 }
00323 
00324 CXXRecordDecl *OverloadExpr::getNamingClass() const {
00325   if (isa<UnresolvedLookupExpr>(this))
00326     return cast<UnresolvedLookupExpr>(this)->getNamingClass();
00327   else
00328     return cast<UnresolvedMemberExpr>(this)->getNamingClass();
00329 }
00330 
00331 // DependentScopeDeclRefExpr
00332 DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(QualType T,
00333                             NestedNameSpecifierLoc QualifierLoc,
00334                             SourceLocation TemplateKWLoc,
00335                             const DeclarationNameInfo &NameInfo,
00336                             const TemplateArgumentListInfo *Args)
00337   : Expr(DependentScopeDeclRefExprClass, T, VK_LValue, OK_Ordinary,
00338          true, true,
00339          (NameInfo.isInstantiationDependent() ||
00340           (QualifierLoc && 
00341            QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
00342          (NameInfo.containsUnexpandedParameterPack() ||
00343           (QualifierLoc && 
00344            QualifierLoc.getNestedNameSpecifier()
00345                             ->containsUnexpandedParameterPack()))),
00346     QualifierLoc(QualifierLoc), NameInfo(NameInfo), 
00347     HasTemplateKWAndArgsInfo(Args != 0 || TemplateKWLoc.isValid())
00348 {
00349   if (Args) {
00350     bool Dependent = true;
00351     bool InstantiationDependent = true;
00352     bool ContainsUnexpandedParameterPack
00353       = ExprBits.ContainsUnexpandedParameterPack;
00354     getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *Args,
00355                                                Dependent,
00356                                                InstantiationDependent,
00357                                                ContainsUnexpandedParameterPack);
00358     ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack;
00359   } else if (TemplateKWLoc.isValid()) {
00360     getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
00361   }
00362 }
00363 
00364 DependentScopeDeclRefExpr *
00365 DependentScopeDeclRefExpr::Create(ASTContext &C,
00366                                   NestedNameSpecifierLoc QualifierLoc,
00367                                   SourceLocation TemplateKWLoc,
00368                                   const DeclarationNameInfo &NameInfo,
00369                                   const TemplateArgumentListInfo *Args) {
00370   std::size_t size = sizeof(DependentScopeDeclRefExpr);
00371   if (Args)
00372     size += ASTTemplateKWAndArgsInfo::sizeFor(Args->size());
00373   else if (TemplateKWLoc.isValid())
00374     size += ASTTemplateKWAndArgsInfo::sizeFor(0);
00375   void *Mem = C.Allocate(size);
00376   return new (Mem) DependentScopeDeclRefExpr(C.DependentTy, QualifierLoc,
00377                                              TemplateKWLoc, NameInfo, Args);
00378 }
00379 
00380 DependentScopeDeclRefExpr *
00381 DependentScopeDeclRefExpr::CreateEmpty(ASTContext &C,
00382                                        bool HasTemplateKWAndArgsInfo,
00383                                        unsigned NumTemplateArgs) {
00384   std::size_t size = sizeof(DependentScopeDeclRefExpr);
00385   if (HasTemplateKWAndArgsInfo)
00386     size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
00387   void *Mem = C.Allocate(size);
00388   DependentScopeDeclRefExpr *E
00389     = new (Mem) DependentScopeDeclRefExpr(QualType(), NestedNameSpecifierLoc(),
00390                                           SourceLocation(),
00391                                           DeclarationNameInfo(), 0);
00392   E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
00393   return E;
00394 }
00395 
00396 SourceRange CXXConstructExpr::getSourceRange() const {
00397   if (isa<CXXTemporaryObjectExpr>(this))
00398     return cast<CXXTemporaryObjectExpr>(this)->getSourceRange();
00399 
00400   if (ParenRange.isValid())
00401     return SourceRange(Loc, ParenRange.getEnd());
00402 
00403   SourceLocation End = Loc;
00404   for (unsigned I = getNumArgs(); I > 0; --I) {
00405     const Expr *Arg = getArg(I-1);
00406     if (!Arg->isDefaultArgument()) {
00407       SourceLocation NewEnd = Arg->getLocEnd();
00408       if (NewEnd.isValid()) {
00409         End = NewEnd;
00410         break;
00411       }
00412     }
00413   }
00414 
00415   return SourceRange(Loc, End);
00416 }
00417 
00418 SourceRange CXXOperatorCallExpr::getSourceRangeImpl() const {
00419   OverloadedOperatorKind Kind = getOperator();
00420   if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
00421     if (getNumArgs() == 1)
00422       // Prefix operator
00423       return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
00424     else
00425       // Postfix operator
00426       return SourceRange(getArg(0)->getLocStart(), getOperatorLoc());
00427   } else if (Kind == OO_Arrow) {
00428     return getArg(0)->getSourceRange();
00429   } else if (Kind == OO_Call) {
00430     return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
00431   } else if (Kind == OO_Subscript) {
00432     return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
00433   } else if (getNumArgs() == 1) {
00434     return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
00435   } else if (getNumArgs() == 2) {
00436     return SourceRange(getArg(0)->getLocStart(), getArg(1)->getLocEnd());
00437   } else {
00438     return getOperatorLoc();
00439   }
00440 }
00441 
00442 Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
00443   if (const MemberExpr *MemExpr = 
00444         dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
00445     return MemExpr->getBase();
00446 
00447   // FIXME: Will eventually need to cope with member pointers.
00448   return 0;
00449 }
00450 
00451 CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const {
00452   if (const MemberExpr *MemExpr = 
00453       dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
00454     return cast<CXXMethodDecl>(MemExpr->getMemberDecl());
00455 
00456   // FIXME: Will eventually need to cope with member pointers.
00457   return 0;
00458 }
00459 
00460 
00461 CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() const {
00462   Expr* ThisArg = getImplicitObjectArgument();
00463   if (!ThisArg)
00464     return 0;
00465 
00466   if (ThisArg->getType()->isAnyPointerType())
00467     return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
00468 
00469   return ThisArg->getType()->getAsCXXRecordDecl();
00470 }
00471 
00472 
00473 //===----------------------------------------------------------------------===//
00474 //  Named casts
00475 //===----------------------------------------------------------------------===//
00476 
00477 /// getCastName - Get the name of the C++ cast being used, e.g.,
00478 /// "static_cast", "dynamic_cast", "reinterpret_cast", or
00479 /// "const_cast". The returned pointer must not be freed.
00480 const char *CXXNamedCastExpr::getCastName() const {
00481   switch (getStmtClass()) {
00482   case CXXStaticCastExprClass:      return "static_cast";
00483   case CXXDynamicCastExprClass:     return "dynamic_cast";
00484   case CXXReinterpretCastExprClass: return "reinterpret_cast";
00485   case CXXConstCastExprClass:       return "const_cast";
00486   default:                          return "<invalid cast>";
00487   }
00488 }
00489 
00490 CXXStaticCastExpr *CXXStaticCastExpr::Create(ASTContext &C, QualType T,
00491                                              ExprValueKind VK,
00492                                              CastKind K, Expr *Op,
00493                                              const CXXCastPath *BasePath,
00494                                              TypeSourceInfo *WrittenTy,
00495                                              SourceLocation L, 
00496                                              SourceLocation RParenLoc) {
00497   unsigned PathSize = (BasePath ? BasePath->size() : 0);
00498   void *Buffer = C.Allocate(sizeof(CXXStaticCastExpr)
00499                             + PathSize * sizeof(CXXBaseSpecifier*));
00500   CXXStaticCastExpr *E =
00501     new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
00502                                    RParenLoc);
00503   if (PathSize) E->setCastPath(*BasePath);
00504   return E;
00505 }
00506 
00507 CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(ASTContext &C,
00508                                                   unsigned PathSize) {
00509   void *Buffer =
00510     C.Allocate(sizeof(CXXStaticCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
00511   return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
00512 }
00513 
00514 CXXDynamicCastExpr *CXXDynamicCastExpr::Create(ASTContext &C, QualType T,
00515                                                ExprValueKind VK,
00516                                                CastKind K, Expr *Op,
00517                                                const CXXCastPath *BasePath,
00518                                                TypeSourceInfo *WrittenTy,
00519                                                SourceLocation L, 
00520                                                SourceLocation RParenLoc) {
00521   unsigned PathSize = (BasePath ? BasePath->size() : 0);
00522   void *Buffer = C.Allocate(sizeof(CXXDynamicCastExpr)
00523                             + PathSize * sizeof(CXXBaseSpecifier*));
00524   CXXDynamicCastExpr *E =
00525     new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
00526                                     RParenLoc);
00527   if (PathSize) E->setCastPath(*BasePath);
00528   return E;
00529 }
00530 
00531 CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(ASTContext &C,
00532                                                     unsigned PathSize) {
00533   void *Buffer =
00534     C.Allocate(sizeof(CXXDynamicCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
00535   return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
00536 }
00537 
00538 /// isAlwaysNull - Return whether the result of the dynamic_cast is proven
00539 /// to always be null. For example:
00540 ///
00541 /// struct A { };
00542 /// struct B final : A { };
00543 /// struct C { };
00544 ///
00545 /// C *f(B* b) { return dynamic_cast<C*>(b); }
00546 bool CXXDynamicCastExpr::isAlwaysNull() const
00547 {
00548   QualType SrcType = getSubExpr()->getType();
00549   QualType DestType = getType();
00550 
00551   if (const PointerType *SrcPTy = SrcType->getAs<PointerType>()) {
00552     SrcType = SrcPTy->getPointeeType();
00553     DestType = DestType->castAs<PointerType>()->getPointeeType();
00554   }
00555 
00556   const CXXRecordDecl *SrcRD = 
00557     cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl());
00558 
00559   if (!SrcRD->hasAttr<FinalAttr>())
00560     return false;
00561 
00562   const CXXRecordDecl *DestRD = 
00563     cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl());
00564 
00565   return !DestRD->isDerivedFrom(SrcRD);
00566 }
00567 
00568 CXXReinterpretCastExpr *
00569 CXXReinterpretCastExpr::Create(ASTContext &C, QualType T, ExprValueKind VK,
00570                                CastKind K, Expr *Op,
00571                                const CXXCastPath *BasePath,
00572                                TypeSourceInfo *WrittenTy, SourceLocation L, 
00573                                SourceLocation RParenLoc) {
00574   unsigned PathSize = (BasePath ? BasePath->size() : 0);
00575   void *Buffer =
00576     C.Allocate(sizeof(CXXReinterpretCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
00577   CXXReinterpretCastExpr *E =
00578     new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
00579                                         RParenLoc);
00580   if (PathSize) E->setCastPath(*BasePath);
00581   return E;
00582 }
00583 
00584 CXXReinterpretCastExpr *
00585 CXXReinterpretCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
00586   void *Buffer = C.Allocate(sizeof(CXXReinterpretCastExpr)
00587                             + PathSize * sizeof(CXXBaseSpecifier*));
00588   return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
00589 }
00590 
00591 CXXConstCastExpr *CXXConstCastExpr::Create(ASTContext &C, QualType T,
00592                                            ExprValueKind VK, Expr *Op,
00593                                            TypeSourceInfo *WrittenTy,
00594                                            SourceLocation L, 
00595                                            SourceLocation RParenLoc) {
00596   return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc);
00597 }
00598 
00599 CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(ASTContext &C) {
00600   return new (C) CXXConstCastExpr(EmptyShell());
00601 }
00602 
00603 CXXFunctionalCastExpr *
00604 CXXFunctionalCastExpr::Create(ASTContext &C, QualType T, ExprValueKind VK,
00605                               TypeSourceInfo *Written, SourceLocation L,
00606                               CastKind K, Expr *Op, const CXXCastPath *BasePath,
00607                                SourceLocation R) {
00608   unsigned PathSize = (BasePath ? BasePath->size() : 0);
00609   void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
00610                             + PathSize * sizeof(CXXBaseSpecifier*));
00611   CXXFunctionalCastExpr *E =
00612     new (Buffer) CXXFunctionalCastExpr(T, VK, Written, L, K, Op, PathSize, R);
00613   if (PathSize) E->setCastPath(*BasePath);
00614   return E;
00615 }
00616 
00617 CXXFunctionalCastExpr *
00618 CXXFunctionalCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
00619   void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
00620                             + PathSize * sizeof(CXXBaseSpecifier*));
00621   return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
00622 }
00623 
00624 UserDefinedLiteral::LiteralOperatorKind
00625 UserDefinedLiteral::getLiteralOperatorKind() const {
00626   if (getNumArgs() == 0)
00627     return LOK_Template;
00628   if (getNumArgs() == 2)
00629     return LOK_String;
00630 
00631   assert(getNumArgs() == 1 && "unexpected #args in literal operator call");
00632   QualType ParamTy =
00633     cast<FunctionDecl>(getCalleeDecl())->getParamDecl(0)->getType();
00634   if (ParamTy->isPointerType())
00635     return LOK_Raw;
00636   if (ParamTy->isAnyCharacterType())
00637     return LOK_Character;
00638   if (ParamTy->isIntegerType())
00639     return LOK_Integer;
00640   if (ParamTy->isFloatingType())
00641     return LOK_Floating;
00642 
00643   llvm_unreachable("unknown kind of literal operator");
00644 }
00645 
00646 Expr *UserDefinedLiteral::getCookedLiteral() {
00647 #ifndef NDEBUG
00648   LiteralOperatorKind LOK = getLiteralOperatorKind();
00649   assert(LOK != LOK_Template && LOK != LOK_Raw && "not a cooked literal");
00650 #endif
00651   return getArg(0);
00652 }
00653 
00654 const IdentifierInfo *UserDefinedLiteral::getUDSuffix() const {
00655   return cast<FunctionDecl>(getCalleeDecl())->getLiteralIdentifier();
00656 }
00657 
00658 CXXDefaultArgExpr *
00659 CXXDefaultArgExpr::Create(ASTContext &C, SourceLocation Loc, 
00660                           ParmVarDecl *Param, Expr *SubExpr) {
00661   void *Mem = C.Allocate(sizeof(CXXDefaultArgExpr) + sizeof(Stmt *));
00662   return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param, 
00663                                      SubExpr);
00664 }
00665 
00666 CXXTemporary *CXXTemporary::Create(ASTContext &C,
00667                                    const CXXDestructorDecl *Destructor) {
00668   return new (C) CXXTemporary(Destructor);
00669 }
00670 
00671 CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(ASTContext &C,
00672                                                    CXXTemporary *Temp,
00673                                                    Expr* SubExpr) {
00674   assert((SubExpr->getType()->isRecordType() ||
00675           SubExpr->getType()->isArrayType()) &&
00676          "Expression bound to a temporary must have record or array type!");
00677 
00678   return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
00679 }
00680 
00681 CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(ASTContext &C,
00682                                                CXXConstructorDecl *Cons,
00683                                                TypeSourceInfo *Type,
00684                                                Expr **Args,
00685                                                unsigned NumArgs,
00686                                                SourceRange parenRange,
00687                                                bool HadMultipleCandidates,
00688                                                bool ZeroInitialization)
00689   : CXXConstructExpr(C, CXXTemporaryObjectExprClass, 
00690                      Type->getType().getNonReferenceType(), 
00691                      Type->getTypeLoc().getBeginLoc(),
00692                      Cons, false, Args, NumArgs,
00693                      HadMultipleCandidates, /*FIXME*/false, ZeroInitialization,
00694                      CXXConstructExpr::CK_Complete, parenRange),
00695     Type(Type) {
00696 }
00697 
00698 SourceRange CXXTemporaryObjectExpr::getSourceRange() const {
00699   return SourceRange(Type->getTypeLoc().getBeginLoc(),
00700                      getParenRange().getEnd());
00701 }
00702 
00703 CXXConstructExpr *CXXConstructExpr::Create(ASTContext &C, QualType T,
00704                                            SourceLocation Loc,
00705                                            CXXConstructorDecl *D, bool Elidable,
00706                                            Expr **Args, unsigned NumArgs,
00707                                            bool HadMultipleCandidates,
00708                                            bool ListInitialization,
00709                                            bool ZeroInitialization,
00710                                            ConstructionKind ConstructKind,
00711                                            SourceRange ParenRange) {
00712   return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc, D, 
00713                                   Elidable, Args, NumArgs,
00714                                   HadMultipleCandidates, ListInitialization,
00715                                   ZeroInitialization, ConstructKind,
00716                                   ParenRange);
00717 }
00718 
00719 CXXConstructExpr::CXXConstructExpr(ASTContext &C, StmtClass SC, QualType T,
00720                                    SourceLocation Loc,
00721                                    CXXConstructorDecl *D, bool elidable,
00722                                    Expr **args, unsigned numargs,
00723                                    bool HadMultipleCandidates,
00724                                    bool ListInitialization,
00725                                    bool ZeroInitialization,
00726                                    ConstructionKind ConstructKind,
00727                                    SourceRange ParenRange)
00728   : Expr(SC, T, VK_RValue, OK_Ordinary,
00729          T->isDependentType(), T->isDependentType(),
00730          T->isInstantiationDependentType(),
00731          T->containsUnexpandedParameterPack()),
00732     Constructor(D), Loc(Loc), ParenRange(ParenRange),  NumArgs(numargs),
00733     Elidable(elidable), HadMultipleCandidates(HadMultipleCandidates),
00734     ListInitialization(ListInitialization),
00735     ZeroInitialization(ZeroInitialization),
00736     ConstructKind(ConstructKind), Args(0)
00737 {
00738   if (NumArgs) {
00739     Args = new (C) Stmt*[NumArgs];
00740     
00741     for (unsigned i = 0; i != NumArgs; ++i) {
00742       assert(args[i] && "NULL argument in CXXConstructExpr");
00743 
00744       if (args[i]->isValueDependent())
00745         ExprBits.ValueDependent = true;
00746       if (args[i]->isInstantiationDependent())
00747         ExprBits.InstantiationDependent = true;
00748       if (args[i]->containsUnexpandedParameterPack())
00749         ExprBits.ContainsUnexpandedParameterPack = true;
00750   
00751       Args[i] = args[i];
00752     }
00753   }
00754 }
00755 
00756 LambdaExpr::Capture::Capture(SourceLocation Loc, bool Implicit,
00757                              LambdaCaptureKind Kind, VarDecl *Var,
00758                              SourceLocation EllipsisLoc)
00759   : VarAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc)
00760 {
00761   unsigned Bits = 0;
00762   if (Implicit)
00763     Bits |= Capture_Implicit;
00764   
00765   switch (Kind) {
00766   case LCK_This: 
00767     assert(Var == 0 && "'this' capture cannot have a variable!");
00768     break;
00769 
00770   case LCK_ByCopy:
00771     Bits |= Capture_ByCopy;
00772     // Fall through 
00773   case LCK_ByRef:
00774     assert(Var && "capture must have a variable!");
00775     break;
00776   }
00777   VarAndBits.setInt(Bits);
00778 }
00779 
00780 LambdaCaptureKind LambdaExpr::Capture::getCaptureKind() const {
00781   if (capturesThis())
00782     return LCK_This;
00783 
00784   return (VarAndBits.getInt() & Capture_ByCopy)? LCK_ByCopy : LCK_ByRef;
00785 }
00786 
00787 LambdaExpr::LambdaExpr(QualType T, 
00788                        SourceRange IntroducerRange,
00789                        LambdaCaptureDefault CaptureDefault,
00790                        ArrayRef<Capture> Captures, 
00791                        bool ExplicitParams,
00792                        bool ExplicitResultType,
00793                        ArrayRef<Expr *> CaptureInits,
00794                        ArrayRef<VarDecl *> ArrayIndexVars,
00795                        ArrayRef<unsigned> ArrayIndexStarts,
00796                        SourceLocation ClosingBrace)
00797   : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary,
00798          T->isDependentType(), T->isDependentType(), T->isDependentType(),
00799          /*ContainsUnexpandedParameterPack=*/false),
00800     IntroducerRange(IntroducerRange),
00801     NumCaptures(Captures.size()),
00802     CaptureDefault(CaptureDefault),
00803     ExplicitParams(ExplicitParams),
00804     ExplicitResultType(ExplicitResultType),
00805     ClosingBrace(ClosingBrace)
00806 {
00807   assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
00808   CXXRecordDecl *Class = getLambdaClass();
00809   CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
00810   
00811   // FIXME: Propagate "has unexpanded parameter pack" bit.
00812   
00813   // Copy captures.
00814   ASTContext &Context = Class->getASTContext();
00815   Data.NumCaptures = NumCaptures;
00816   Data.NumExplicitCaptures = 0;
00817   Data.Captures = (Capture *)Context.Allocate(sizeof(Capture) * NumCaptures);
00818   Capture *ToCapture = Data.Captures;
00819   for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
00820     if (Captures[I].isExplicit())
00821       ++Data.NumExplicitCaptures;
00822     
00823     *ToCapture++ = Captures[I];
00824   }
00825  
00826   // Copy initialization expressions for the non-static data members.
00827   Stmt **Stored = getStoredStmts();
00828   for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
00829     *Stored++ = CaptureInits[I];
00830   
00831   // Copy the body of the lambda.
00832   *Stored++ = getCallOperator()->getBody();
00833 
00834   // Copy the array index variables, if any.
00835   HasArrayIndexVars = !ArrayIndexVars.empty();
00836   if (HasArrayIndexVars) {
00837     assert(ArrayIndexStarts.size() == NumCaptures);
00838     memcpy(getArrayIndexVars(), ArrayIndexVars.data(),
00839            sizeof(VarDecl *) * ArrayIndexVars.size());
00840     memcpy(getArrayIndexStarts(), ArrayIndexStarts.data(), 
00841            sizeof(unsigned) * Captures.size());
00842     getArrayIndexStarts()[Captures.size()] = ArrayIndexVars.size();
00843   }
00844 }
00845 
00846 LambdaExpr *LambdaExpr::Create(ASTContext &Context, 
00847                                CXXRecordDecl *Class,
00848                                SourceRange IntroducerRange,
00849                                LambdaCaptureDefault CaptureDefault,
00850                                ArrayRef<Capture> Captures, 
00851                                bool ExplicitParams,
00852                                bool ExplicitResultType,
00853                                ArrayRef<Expr *> CaptureInits,
00854                                ArrayRef<VarDecl *> ArrayIndexVars,
00855                                ArrayRef<unsigned> ArrayIndexStarts,
00856                                SourceLocation ClosingBrace) {
00857   // Determine the type of the expression (i.e., the type of the
00858   // function object we're creating).
00859   QualType T = Context.getTypeDeclType(Class);
00860 
00861   unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (Captures.size() + 1);
00862   if (!ArrayIndexVars.empty())
00863     Size += sizeof(VarDecl *) * ArrayIndexVars.size()
00864           + sizeof(unsigned) * (Captures.size() + 1);
00865   void *Mem = Context.Allocate(Size);
00866   return new (Mem) LambdaExpr(T, IntroducerRange, CaptureDefault, 
00867                               Captures, ExplicitParams, ExplicitResultType,
00868                               CaptureInits, ArrayIndexVars, ArrayIndexStarts,
00869                               ClosingBrace);
00870 }
00871 
00872 LambdaExpr *LambdaExpr::CreateDeserialized(ASTContext &C, unsigned NumCaptures,
00873                                            unsigned NumArrayIndexVars) {
00874   unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (NumCaptures + 1);
00875   if (NumArrayIndexVars)
00876     Size += sizeof(VarDecl) * NumArrayIndexVars
00877           + sizeof(unsigned) * (NumCaptures + 1);
00878   void *Mem = C.Allocate(Size);
00879   return new (Mem) LambdaExpr(EmptyShell(), NumCaptures, NumArrayIndexVars > 0);
00880 }
00881 
00882 LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
00883   return getLambdaClass()->getLambdaData().Captures;
00884 }
00885 
00886 LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
00887   return capture_begin() + NumCaptures;
00888 }
00889 
00890 LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
00891   return capture_begin();
00892 }
00893 
00894 LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
00895   struct CXXRecordDecl::LambdaDefinitionData &Data
00896     = getLambdaClass()->getLambdaData();
00897   return Data.Captures + Data.NumExplicitCaptures;
00898 }
00899 
00900 LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
00901   return explicit_capture_end();
00902 }
00903 
00904 LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
00905   return capture_end();
00906 }
00907 
00908 ArrayRef<VarDecl *> 
00909 LambdaExpr::getCaptureInitIndexVars(capture_init_iterator Iter) const {
00910   assert(HasArrayIndexVars && "No array index-var data?");
00911   
00912   unsigned Index = Iter - capture_init_begin();
00913   assert(Index < getLambdaClass()->getLambdaData().NumCaptures &&
00914          "Capture index out-of-range");
00915   VarDecl **IndexVars = getArrayIndexVars();
00916   unsigned *IndexStarts = getArrayIndexStarts();
00917   return ArrayRef<VarDecl *>(IndexVars + IndexStarts[Index],
00918                              IndexVars + IndexStarts[Index + 1]);
00919 }
00920 
00921 CXXRecordDecl *LambdaExpr::getLambdaClass() const {
00922   return getType()->getAsCXXRecordDecl();
00923 }
00924 
00925 CXXMethodDecl *LambdaExpr::getCallOperator() const {
00926   CXXRecordDecl *Record = getLambdaClass();
00927   DeclarationName Name
00928     = Record->getASTContext().DeclarationNames.getCXXOperatorName(OO_Call);
00929   DeclContext::lookup_result Calls = Record->lookup(Name);
00930   assert(Calls.first != Calls.second && "Missing lambda call operator!");
00931   CXXMethodDecl *Result = cast<CXXMethodDecl>(*Calls.first++);
00932   assert(Calls.first == Calls.second && "More than lambda one call operator?");
00933   return Result;
00934 }
00935 
00936 CompoundStmt *LambdaExpr::getBody() const {
00937   if (!getStoredStmts()[NumCaptures])
00938     getStoredStmts()[NumCaptures] = getCallOperator()->getBody();
00939     
00940   return reinterpret_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
00941 }
00942 
00943 bool LambdaExpr::isMutable() const {
00944   return (getCallOperator()->getTypeQualifiers() & Qualifiers::Const) == 0;
00945 }
00946 
00947 ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
00948                                    ArrayRef<CleanupObject> objects)
00949   : Expr(ExprWithCleanupsClass, subexpr->getType(),
00950          subexpr->getValueKind(), subexpr->getObjectKind(),
00951          subexpr->isTypeDependent(), subexpr->isValueDependent(),
00952          subexpr->isInstantiationDependent(),
00953          subexpr->containsUnexpandedParameterPack()),
00954     SubExpr(subexpr) {
00955   ExprWithCleanupsBits.NumObjects = objects.size();
00956   for (unsigned i = 0, e = objects.size(); i != e; ++i)
00957     getObjectsBuffer()[i] = objects[i];
00958 }
00959 
00960 ExprWithCleanups *ExprWithCleanups::Create(ASTContext &C, Expr *subexpr,
00961                                            ArrayRef<CleanupObject> objects) {
00962   size_t size = sizeof(ExprWithCleanups)
00963               + objects.size() * sizeof(CleanupObject);
00964   void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
00965   return new (buffer) ExprWithCleanups(subexpr, objects);
00966 }
00967 
00968 ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
00969   : Expr(ExprWithCleanupsClass, empty) {
00970   ExprWithCleanupsBits.NumObjects = numObjects;
00971 }
00972 
00973 ExprWithCleanups *ExprWithCleanups::Create(ASTContext &C, EmptyShell empty,
00974                                            unsigned numObjects) {
00975   size_t size = sizeof(ExprWithCleanups) + numObjects * sizeof(CleanupObject);
00976   void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
00977   return new (buffer) ExprWithCleanups(empty, numObjects);
00978 }
00979 
00980 CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
00981                                                  SourceLocation LParenLoc,
00982                                                  Expr **Args,
00983                                                  unsigned NumArgs,
00984                                                  SourceLocation RParenLoc)
00985   : Expr(CXXUnresolvedConstructExprClass, 
00986          Type->getType().getNonReferenceType(),
00987          (Type->getType()->isLValueReferenceType() ? VK_LValue
00988           :Type->getType()->isRValueReferenceType()? VK_XValue
00989           :VK_RValue),
00990          OK_Ordinary,
00991          Type->getType()->isDependentType(), true, true,
00992          Type->getType()->containsUnexpandedParameterPack()),
00993     Type(Type),
00994     LParenLoc(LParenLoc),
00995     RParenLoc(RParenLoc),
00996     NumArgs(NumArgs) {
00997   Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1);
00998   for (unsigned I = 0; I != NumArgs; ++I) {
00999     if (Args[I]->containsUnexpandedParameterPack())
01000       ExprBits.ContainsUnexpandedParameterPack = true;
01001 
01002     StoredArgs[I] = Args[I];
01003   }
01004 }
01005 
01006 CXXUnresolvedConstructExpr *
01007 CXXUnresolvedConstructExpr::Create(ASTContext &C,
01008                                    TypeSourceInfo *Type,
01009                                    SourceLocation LParenLoc,
01010                                    Expr **Args,
01011                                    unsigned NumArgs,
01012                                    SourceLocation RParenLoc) {
01013   void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
01014                          sizeof(Expr *) * NumArgs);
01015   return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc,
01016                                               Args, NumArgs, RParenLoc);
01017 }
01018 
01019 CXXUnresolvedConstructExpr *
01020 CXXUnresolvedConstructExpr::CreateEmpty(ASTContext &C, unsigned NumArgs) {
01021   Stmt::EmptyShell Empty;
01022   void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
01023                          sizeof(Expr *) * NumArgs);
01024   return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
01025 }
01026 
01027 SourceRange CXXUnresolvedConstructExpr::getSourceRange() const {
01028   return SourceRange(Type->getTypeLoc().getBeginLoc(), RParenLoc);
01029 }
01030 
01031 CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
01032                                                  Expr *Base, QualType BaseType,
01033                                                  bool IsArrow,
01034                                                  SourceLocation OperatorLoc,
01035                                           NestedNameSpecifierLoc QualifierLoc,
01036                                           SourceLocation TemplateKWLoc,
01037                                           NamedDecl *FirstQualifierFoundInScope,
01038                                           DeclarationNameInfo MemberNameInfo,
01039                                    const TemplateArgumentListInfo *TemplateArgs)
01040   : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
01041          VK_LValue, OK_Ordinary, true, true, true,
01042          ((Base && Base->containsUnexpandedParameterPack()) ||
01043           (QualifierLoc && 
01044            QualifierLoc.getNestedNameSpecifier()
01045                                        ->containsUnexpandedParameterPack()) ||
01046           MemberNameInfo.containsUnexpandedParameterPack())),
01047     Base(Base), BaseType(BaseType), IsArrow(IsArrow),
01048     HasTemplateKWAndArgsInfo(TemplateArgs != 0 || TemplateKWLoc.isValid()),
01049     OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc), 
01050     FirstQualifierFoundInScope(FirstQualifierFoundInScope),
01051     MemberNameInfo(MemberNameInfo) {
01052   if (TemplateArgs) {
01053     bool Dependent = true;
01054     bool InstantiationDependent = true;
01055     bool ContainsUnexpandedParameterPack = false;
01056     getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
01057                                                Dependent,
01058                                                InstantiationDependent,
01059                                                ContainsUnexpandedParameterPack);
01060     if (ContainsUnexpandedParameterPack)
01061       ExprBits.ContainsUnexpandedParameterPack = true;
01062   } else if (TemplateKWLoc.isValid()) {
01063     getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
01064   }
01065 }
01066 
01067 CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(ASTContext &C,
01068                           Expr *Base, QualType BaseType,
01069                           bool IsArrow,
01070                           SourceLocation OperatorLoc,
01071                           NestedNameSpecifierLoc QualifierLoc,
01072                           NamedDecl *FirstQualifierFoundInScope,
01073                           DeclarationNameInfo MemberNameInfo)
01074   : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
01075          VK_LValue, OK_Ordinary, true, true, true,
01076          ((Base && Base->containsUnexpandedParameterPack()) ||
01077           (QualifierLoc && 
01078            QualifierLoc.getNestedNameSpecifier()->
01079                                          containsUnexpandedParameterPack()) ||
01080           MemberNameInfo.containsUnexpandedParameterPack())),
01081     Base(Base), BaseType(BaseType), IsArrow(IsArrow),
01082     HasTemplateKWAndArgsInfo(false),
01083     OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
01084     FirstQualifierFoundInScope(FirstQualifierFoundInScope),
01085     MemberNameInfo(MemberNameInfo) { }
01086 
01087 CXXDependentScopeMemberExpr *
01088 CXXDependentScopeMemberExpr::Create(ASTContext &C,
01089                                 Expr *Base, QualType BaseType, bool IsArrow,
01090                                 SourceLocation OperatorLoc,
01091                                 NestedNameSpecifierLoc QualifierLoc,
01092                                 SourceLocation TemplateKWLoc,
01093                                 NamedDecl *FirstQualifierFoundInScope,
01094                                 DeclarationNameInfo MemberNameInfo,
01095                                 const TemplateArgumentListInfo *TemplateArgs) {
01096   if (!TemplateArgs && !TemplateKWLoc.isValid())
01097     return new (C) CXXDependentScopeMemberExpr(C, Base, BaseType,
01098                                                IsArrow, OperatorLoc,
01099                                                QualifierLoc,
01100                                                FirstQualifierFoundInScope,
01101                                                MemberNameInfo);
01102 
01103   unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
01104   std::size_t size = sizeof(CXXDependentScopeMemberExpr)
01105     + ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
01106 
01107   void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
01108   return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
01109                                                IsArrow, OperatorLoc,
01110                                                QualifierLoc,
01111                                                TemplateKWLoc,
01112                                                FirstQualifierFoundInScope,
01113                                                MemberNameInfo, TemplateArgs);
01114 }
01115 
01116 CXXDependentScopeMemberExpr *
01117 CXXDependentScopeMemberExpr::CreateEmpty(ASTContext &C,
01118                                          bool HasTemplateKWAndArgsInfo,
01119                                          unsigned NumTemplateArgs) {
01120   if (!HasTemplateKWAndArgsInfo)
01121     return new (C) CXXDependentScopeMemberExpr(C, 0, QualType(),
01122                                                0, SourceLocation(),
01123                                                NestedNameSpecifierLoc(), 0,
01124                                                DeclarationNameInfo());
01125 
01126   std::size_t size = sizeof(CXXDependentScopeMemberExpr) +
01127                      ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
01128   void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
01129   CXXDependentScopeMemberExpr *E
01130     =  new (Mem) CXXDependentScopeMemberExpr(C, 0, QualType(),
01131                                              0, SourceLocation(),
01132                                              NestedNameSpecifierLoc(),
01133                                              SourceLocation(), 0,
01134                                              DeclarationNameInfo(), 0);
01135   E->HasTemplateKWAndArgsInfo = true;
01136   return E;
01137 }
01138 
01139 bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
01140   if (Base == 0)
01141     return true;
01142   
01143   return cast<Expr>(Base)->isImplicitCXXThis();
01144 }
01145 
01146 static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
01147                                             UnresolvedSetIterator end) {
01148   do {
01149     NamedDecl *decl = *begin;
01150     if (isa<UnresolvedUsingValueDecl>(decl))
01151       return false;
01152     if (isa<UsingShadowDecl>(decl))
01153       decl = cast<UsingShadowDecl>(decl)->getUnderlyingDecl();
01154 
01155     // Unresolved member expressions should only contain methods and
01156     // method templates.
01157     assert(isa<CXXMethodDecl>(decl) || isa<FunctionTemplateDecl>(decl));
01158 
01159     if (isa<FunctionTemplateDecl>(decl))
01160       decl = cast<FunctionTemplateDecl>(decl)->getTemplatedDecl();
01161     if (cast<CXXMethodDecl>(decl)->isStatic())
01162       return false;
01163   } while (++begin != end);
01164 
01165   return true;
01166 }
01167 
01168 UnresolvedMemberExpr::UnresolvedMemberExpr(ASTContext &C, 
01169                                            bool HasUnresolvedUsing,
01170                                            Expr *Base, QualType BaseType,
01171                                            bool IsArrow,
01172                                            SourceLocation OperatorLoc,
01173                                            NestedNameSpecifierLoc QualifierLoc,
01174                                            SourceLocation TemplateKWLoc,
01175                                    const DeclarationNameInfo &MemberNameInfo,
01176                                    const TemplateArgumentListInfo *TemplateArgs,
01177                                            UnresolvedSetIterator Begin, 
01178                                            UnresolvedSetIterator End)
01179   : OverloadExpr(UnresolvedMemberExprClass, C, QualifierLoc, TemplateKWLoc,
01180                  MemberNameInfo, TemplateArgs, Begin, End,
01181                  // Dependent
01182                  ((Base && Base->isTypeDependent()) ||
01183                   BaseType->isDependentType()),
01184                  ((Base && Base->isInstantiationDependent()) ||
01185                    BaseType->isInstantiationDependentType()),
01186                  // Contains unexpanded parameter pack
01187                  ((Base && Base->containsUnexpandedParameterPack()) ||
01188                   BaseType->containsUnexpandedParameterPack())),
01189     IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
01190     Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
01191 
01192   // Check whether all of the members are non-static member functions,
01193   // and if so, mark give this bound-member type instead of overload type.
01194   if (hasOnlyNonStaticMemberFunctions(Begin, End))
01195     setType(C.BoundMemberTy);
01196 }
01197 
01198 bool UnresolvedMemberExpr::isImplicitAccess() const {
01199   if (Base == 0)
01200     return true;
01201   
01202   return cast<Expr>(Base)->isImplicitCXXThis();
01203 }
01204 
01205 UnresolvedMemberExpr *
01206 UnresolvedMemberExpr::Create(ASTContext &C, 
01207                              bool HasUnresolvedUsing,
01208                              Expr *Base, QualType BaseType, bool IsArrow,
01209                              SourceLocation OperatorLoc,
01210                              NestedNameSpecifierLoc QualifierLoc,
01211                              SourceLocation TemplateKWLoc,
01212                              const DeclarationNameInfo &MemberNameInfo,
01213                              const TemplateArgumentListInfo *TemplateArgs,
01214                              UnresolvedSetIterator Begin, 
01215                              UnresolvedSetIterator End) {
01216   std::size_t size = sizeof(UnresolvedMemberExpr);
01217   if (TemplateArgs)
01218     size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
01219   else if (TemplateKWLoc.isValid())
01220     size += ASTTemplateKWAndArgsInfo::sizeFor(0);
01221 
01222   void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
01223   return new (Mem) UnresolvedMemberExpr(C, 
01224                              HasUnresolvedUsing, Base, BaseType,
01225                              IsArrow, OperatorLoc, QualifierLoc, TemplateKWLoc,
01226                              MemberNameInfo, TemplateArgs, Begin, End);
01227 }
01228 
01229 UnresolvedMemberExpr *
01230 UnresolvedMemberExpr::CreateEmpty(ASTContext &C, bool HasTemplateKWAndArgsInfo,
01231                                   unsigned NumTemplateArgs) {
01232   std::size_t size = sizeof(UnresolvedMemberExpr);
01233   if (HasTemplateKWAndArgsInfo)
01234     size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
01235 
01236   void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
01237   UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
01238   E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
01239   return E;
01240 }
01241 
01242 CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
01243   // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
01244 
01245   // If there was a nested name specifier, it names the naming class.
01246   // It can't be dependent: after all, we were actually able to do the
01247   // lookup.
01248   CXXRecordDecl *Record = 0;
01249   if (getQualifier()) {
01250     const Type *T = getQualifier()->getAsType();
01251     assert(T && "qualifier in member expression does not name type");
01252     Record = T->getAsCXXRecordDecl();
01253     assert(Record && "qualifier in member expression does not name record");
01254   }
01255   // Otherwise the naming class must have been the base class.
01256   else {
01257     QualType BaseType = getBaseType().getNonReferenceType();
01258     if (isArrow()) {
01259       const PointerType *PT = BaseType->getAs<PointerType>();
01260       assert(PT && "base of arrow member access is not pointer");
01261       BaseType = PT->getPointeeType();
01262     }
01263     
01264     Record = BaseType->getAsCXXRecordDecl();
01265     assert(Record && "base of member expression does not name record");
01266   }
01267   
01268   return Record;
01269 }
01270 
01271 SubstNonTypeTemplateParmPackExpr::
01272 SubstNonTypeTemplateParmPackExpr(QualType T, 
01273                                  NonTypeTemplateParmDecl *Param,
01274                                  SourceLocation NameLoc,
01275                                  const TemplateArgument &ArgPack)
01276   : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary, 
01277          true, true, true, true),
01278     Param(Param), Arguments(ArgPack.pack_begin()), 
01279     NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) { }
01280 
01281 TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
01282   return TemplateArgument(Arguments, NumArguments);
01283 }
01284 
01285 TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
01286                              ArrayRef<TypeSourceInfo *> Args,
01287                              SourceLocation RParenLoc,
01288                              bool Value)
01289   : Expr(TypeTraitExprClass, T, VK_RValue, OK_Ordinary,
01290          /*TypeDependent=*/false,
01291          /*ValueDependent=*/false,
01292          /*InstantiationDependent=*/false,
01293          /*ContainsUnexpandedParameterPack=*/false),
01294     Loc(Loc), RParenLoc(RParenLoc)
01295 {
01296   TypeTraitExprBits.Kind = Kind;
01297   TypeTraitExprBits.Value = Value;
01298   TypeTraitExprBits.NumArgs = Args.size();
01299 
01300   TypeSourceInfo **ToArgs = getTypeSourceInfos();
01301   
01302   for (unsigned I = 0, N = Args.size(); I != N; ++I) {
01303     if (Args[I]->getType()->isDependentType())
01304       setValueDependent(true);
01305     if (Args[I]->getType()->isInstantiationDependentType())
01306       setInstantiationDependent(true);
01307     if (Args[I]->getType()->containsUnexpandedParameterPack())
01308       setContainsUnexpandedParameterPack(true);
01309     
01310     ToArgs[I] = Args[I];
01311   }
01312 }
01313 
01314 TypeTraitExpr *TypeTraitExpr::Create(ASTContext &C, QualType T, 
01315                                      SourceLocation Loc, 
01316                                      TypeTrait Kind,
01317                                      ArrayRef<TypeSourceInfo *> Args,
01318                                      SourceLocation RParenLoc,
01319                                      bool Value) {
01320   unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * Args.size();
01321   void *Mem = C.Allocate(Size);
01322   return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value);
01323 }
01324 
01325 TypeTraitExpr *TypeTraitExpr::CreateDeserialized(ASTContext &C,
01326                                                  unsigned NumArgs) {
01327   unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * NumArgs;
01328   void *Mem = C.Allocate(Size);
01329   return new (Mem) TypeTraitExpr(EmptyShell());
01330 }
01331 
01332 void ArrayTypeTraitExpr::anchor() { }