| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- SemaExprCXX.cpp - Semantic Analysis for Expressions --------------===// | 
|  | 2 | // | 
|  | 3 | //                     The LLVM Compiler Infrastructure | 
|  | 4 | // | 
| Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source | 
|  | 6 | // License. See LICENSE.TXT for details. | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // | 
|  | 8 | //===----------------------------------------------------------------------===// | 
|  | 9 | // | 
|  | 10 | //  This file implements semantic analysis for C++ expressions. | 
|  | 11 | // | 
|  | 12 | //===----------------------------------------------------------------------===// | 
|  | 13 |  | 
|  | 14 | #include "Sema.h" | 
| Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 15 | #include "SemaInit.h" | 
| John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 16 | #include "Lookup.h" | 
| Steve Naroff | 210679c | 2007-08-25 14:02:58 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" | 
| Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 18 | #include "clang/AST/CXXInheritance.h" | 
| Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 19 | #include "clang/AST/ExprCXX.h" | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 20 | #include "clang/AST/TypeLoc.h" | 
| Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 21 | #include "clang/Basic/PartialDiagnostic.h" | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 22 | #include "clang/Basic/TargetInfo.h" | 
| Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 23 | #include "clang/Lex/Preprocessor.h" | 
|  | 24 | #include "clang/Parse/DeclSpec.h" | 
| Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 25 | #include "clang/Parse/Template.h" | 
| Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/STLExtras.h" | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 27 | using namespace clang; | 
|  | 28 |  | 
| Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 29 | Action::TypeTy *Sema::getDestructorName(SourceLocation TildeLoc, | 
|  | 30 | IdentifierInfo &II, | 
|  | 31 | SourceLocation NameLoc, | 
|  | 32 | Scope *S, const CXXScopeSpec &SS, | 
|  | 33 | TypeTy *ObjectTypePtr, | 
|  | 34 | bool EnteringContext) { | 
|  | 35 | // Determine where to perform name lookup. | 
|  | 36 |  | 
|  | 37 | // FIXME: This area of the standard is very messy, and the current | 
|  | 38 | // wording is rather unclear about which scopes we search for the | 
|  | 39 | // destructor name; see core issues 399 and 555. Issue 399 in | 
|  | 40 | // particular shows where the current description of destructor name | 
|  | 41 | // lookup is completely out of line with existing practice, e.g., | 
|  | 42 | // this appears to be ill-formed: | 
|  | 43 | // | 
|  | 44 | //   namespace N { | 
|  | 45 | //     template <typename T> struct S { | 
|  | 46 | //       ~S(); | 
|  | 47 | //     }; | 
|  | 48 | //   } | 
|  | 49 | // | 
|  | 50 | //   void f(N::S<int>* s) { | 
|  | 51 | //     s->N::S<int>::~S(); | 
|  | 52 | //   } | 
|  | 53 | // | 
| Douglas Gregor | 93649fd | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 54 | // See also PR6358 and PR6359. | 
| Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 55 | QualType SearchType; | 
|  | 56 | DeclContext *LookupCtx = 0; | 
|  | 57 | bool isDependent = false; | 
|  | 58 | bool LookInScope = false; | 
|  | 59 |  | 
|  | 60 | // If we have an object type, it's because we are in a | 
|  | 61 | // pseudo-destructor-expression or a member access expression, and | 
|  | 62 | // we know what type we're looking for. | 
|  | 63 | if (ObjectTypePtr) | 
|  | 64 | SearchType = GetTypeFromParser(ObjectTypePtr); | 
|  | 65 |  | 
|  | 66 | if (SS.isSet()) { | 
| Douglas Gregor | 93649fd | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 67 | NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); | 
|  | 68 |  | 
|  | 69 | bool AlreadySearched = false; | 
|  | 70 | bool LookAtPrefix = true; | 
|  | 71 | if (!getLangOptions().CPlusPlus0x) { | 
|  | 72 | // C++ [basic.lookup.qual]p6: | 
|  | 73 | //   If a pseudo-destructor-name (5.2.4) contains a nested-name-specifier, | 
|  | 74 | //   the type-names are looked up as types in the scope designated by the | 
|  | 75 | //   nested-name-specifier. In a qualified-id of the form: | 
|  | 76 | // | 
|  | 77 | //     ::[opt] nested-name-specifier  ̃ class-name | 
|  | 78 | // | 
|  | 79 | //   where the nested-name-specifier designates a namespace scope, and in | 
|  | 80 | //   a qualified-id of the form: | 
|  | 81 | // | 
|  | 82 | //     ::opt nested-name-specifier class-name ::  ̃ class-name | 
|  | 83 | // | 
|  | 84 | //   the class-names are looked up as types in the scope designated by | 
|  | 85 | //   the nested-name-specifier. | 
|  | 86 | // | 
|  | 87 | // Here, we check the first case (completely) and determine whether the | 
|  | 88 | // code below is permitted to look at the prefix of the | 
|  | 89 | // nested-name-specifier (as we do in C++0x). | 
|  | 90 | DeclContext *DC = computeDeclContext(SS, EnteringContext); | 
|  | 91 | if (DC && DC->isFileContext()) { | 
|  | 92 | AlreadySearched = true; | 
|  | 93 | LookupCtx = DC; | 
|  | 94 | isDependent = false; | 
|  | 95 | } else if (DC && isa<CXXRecordDecl>(DC)) | 
|  | 96 | LookAtPrefix = false; | 
|  | 97 | } | 
|  | 98 |  | 
|  | 99 | // C++0x [basic.lookup.qual]p6: | 
| Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 100 | //   If a pseudo-destructor-name (5.2.4) contains a | 
|  | 101 | //   nested-name-specifier, the type-names are looked up as types | 
|  | 102 | //   in the scope designated by the nested-name-specifier. Similarly, in | 
| Chandler Carruth | 5e895a8 | 2010-02-21 10:19:54 +0000 | [diff] [blame] | 103 | //   a qualified-id of the form: | 
| Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 104 | // | 
|  | 105 | //     :: [opt] nested-name-specifier[opt] class-name :: ~class-name | 
|  | 106 | // | 
|  | 107 | //   the second class-name is looked up in the same scope as the first. | 
|  | 108 | // | 
| Douglas Gregor | 93649fd | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 109 | // To implement this, we look at the prefix of the | 
|  | 110 | // nested-name-specifier we were given, and determine the lookup | 
|  | 111 | // context from that. | 
|  | 112 | // | 
|  | 113 | // We also fold in the second case from the C++03 rules quoted further | 
|  | 114 | // above. | 
|  | 115 | NestedNameSpecifier *Prefix = 0; | 
|  | 116 | if (AlreadySearched) { | 
|  | 117 | // Nothing left to do. | 
|  | 118 | } else if (LookAtPrefix && (Prefix = NNS->getPrefix())) { | 
|  | 119 | CXXScopeSpec PrefixSS; | 
|  | 120 | PrefixSS.setScopeRep(Prefix); | 
|  | 121 | LookupCtx = computeDeclContext(PrefixSS, EnteringContext); | 
|  | 122 | isDependent = isDependentScopeSpecifier(PrefixSS); | 
|  | 123 | } else if (getLangOptions().CPlusPlus0x && | 
|  | 124 | (LookupCtx = computeDeclContext(SS, EnteringContext))) { | 
|  | 125 | if (!LookupCtx->isTranslationUnit()) | 
|  | 126 | LookupCtx = LookupCtx->getParent(); | 
|  | 127 | isDependent = LookupCtx && LookupCtx->isDependentContext(); | 
|  | 128 | } else if (ObjectTypePtr) { | 
| Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 129 | LookupCtx = computeDeclContext(SearchType); | 
|  | 130 | isDependent = SearchType->isDependentType(); | 
|  | 131 | } else { | 
|  | 132 | LookupCtx = computeDeclContext(SS, EnteringContext); | 
| Douglas Gregor | 93649fd | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 133 | isDependent = LookupCtx && LookupCtx->isDependentContext(); | 
| Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 134 | } | 
| Douglas Gregor | 93649fd | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 135 |  | 
| Douglas Gregor | edc9050 | 2010-02-25 04:46:04 +0000 | [diff] [blame] | 136 | LookInScope = false; | 
| Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 137 | } else if (ObjectTypePtr) { | 
|  | 138 | // C++ [basic.lookup.classref]p3: | 
|  | 139 | //   If the unqualified-id is ~type-name, the type-name is looked up | 
|  | 140 | //   in the context of the entire postfix-expression. If the type T | 
|  | 141 | //   of the object expression is of a class type C, the type-name is | 
|  | 142 | //   also looked up in the scope of class C. At least one of the | 
|  | 143 | //   lookups shall find a name that refers to (possibly | 
|  | 144 | //   cv-qualified) T. | 
|  | 145 | LookupCtx = computeDeclContext(SearchType); | 
|  | 146 | isDependent = SearchType->isDependentType(); | 
|  | 147 | assert((isDependent || !SearchType->isIncompleteType()) && | 
|  | 148 | "Caller should have completed object type"); | 
|  | 149 |  | 
|  | 150 | LookInScope = true; | 
|  | 151 | } else { | 
|  | 152 | // Perform lookup into the current scope (only). | 
|  | 153 | LookInScope = true; | 
|  | 154 | } | 
|  | 155 |  | 
|  | 156 | LookupResult Found(*this, &II, NameLoc, LookupOrdinaryName); | 
|  | 157 | for (unsigned Step = 0; Step != 2; ++Step) { | 
|  | 158 | // Look for the name first in the computed lookup context (if we | 
|  | 159 | // have one) and, if that fails to find a match, in the sope (if | 
|  | 160 | // we're allowed to look there). | 
|  | 161 | Found.clear(); | 
|  | 162 | if (Step == 0 && LookupCtx) | 
|  | 163 | LookupQualifiedName(Found, LookupCtx); | 
| Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 164 | else if (Step == 1 && LookInScope && S) | 
| Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 165 | LookupName(Found, S); | 
|  | 166 | else | 
|  | 167 | continue; | 
|  | 168 |  | 
|  | 169 | // FIXME: Should we be suppressing ambiguities here? | 
|  | 170 | if (Found.isAmbiguous()) | 
|  | 171 | return 0; | 
|  | 172 |  | 
|  | 173 | if (TypeDecl *Type = Found.getAsSingle<TypeDecl>()) { | 
|  | 174 | QualType T = Context.getTypeDeclType(Type); | 
|  | 175 | // If we found the injected-class-name of a class template, retrieve the | 
|  | 176 | // type of that template. | 
|  | 177 | // FIXME: We really shouldn't need to do this. | 
|  | 178 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Type)) | 
|  | 179 | if (Record->isInjectedClassName()) | 
|  | 180 | if (Record->getDescribedClassTemplate()) | 
|  | 181 | T = Record->getDescribedClassTemplate() | 
|  | 182 | ->getInjectedClassNameType(Context); | 
|  | 183 |  | 
|  | 184 | if (SearchType.isNull() || SearchType->isDependentType() || | 
|  | 185 | Context.hasSameUnqualifiedType(T, SearchType)) { | 
|  | 186 | // We found our type! | 
|  | 187 |  | 
|  | 188 | return T.getAsOpaquePtr(); | 
|  | 189 | } | 
|  | 190 | } | 
|  | 191 |  | 
|  | 192 | // If the name that we found is a class template name, and it is | 
|  | 193 | // the same name as the template name in the last part of the | 
|  | 194 | // nested-name-specifier (if present) or the object type, then | 
|  | 195 | // this is the destructor for that class. | 
|  | 196 | // FIXME: This is a workaround until we get real drafting for core | 
|  | 197 | // issue 399, for which there isn't even an obvious direction. | 
|  | 198 | if (ClassTemplateDecl *Template = Found.getAsSingle<ClassTemplateDecl>()) { | 
|  | 199 | QualType MemberOfType; | 
|  | 200 | if (SS.isSet()) { | 
|  | 201 | if (DeclContext *Ctx = computeDeclContext(SS, EnteringContext)) { | 
|  | 202 | // Figure out the type of the context, if it has one. | 
|  | 203 | if (ClassTemplateSpecializationDecl *Spec | 
|  | 204 | = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) | 
|  | 205 | MemberOfType = Context.getTypeDeclType(Spec); | 
|  | 206 | else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx)) { | 
|  | 207 | if (Record->getDescribedClassTemplate()) | 
|  | 208 | MemberOfType = Record->getDescribedClassTemplate() | 
|  | 209 | ->getInjectedClassNameType(Context); | 
|  | 210 | else | 
|  | 211 | MemberOfType = Context.getTypeDeclType(Record); | 
|  | 212 | } | 
|  | 213 | } | 
|  | 214 | } | 
|  | 215 | if (MemberOfType.isNull()) | 
|  | 216 | MemberOfType = SearchType; | 
|  | 217 |  | 
|  | 218 | if (MemberOfType.isNull()) | 
|  | 219 | continue; | 
|  | 220 |  | 
|  | 221 | // We're referring into a class template specialization. If the | 
|  | 222 | // class template we found is the same as the template being | 
|  | 223 | // specialized, we found what we are looking for. | 
|  | 224 | if (const RecordType *Record = MemberOfType->getAs<RecordType>()) { | 
|  | 225 | if (ClassTemplateSpecializationDecl *Spec | 
|  | 226 | = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) { | 
|  | 227 | if (Spec->getSpecializedTemplate()->getCanonicalDecl() == | 
|  | 228 | Template->getCanonicalDecl()) | 
|  | 229 | return MemberOfType.getAsOpaquePtr(); | 
|  | 230 | } | 
|  | 231 |  | 
|  | 232 | continue; | 
|  | 233 | } | 
|  | 234 |  | 
|  | 235 | // We're referring to an unresolved class template | 
|  | 236 | // specialization. Determine whether we class template we found | 
|  | 237 | // is the same as the template being specialized or, if we don't | 
|  | 238 | // know which template is being specialized, that it at least | 
|  | 239 | // has the same name. | 
|  | 240 | if (const TemplateSpecializationType *SpecType | 
|  | 241 | = MemberOfType->getAs<TemplateSpecializationType>()) { | 
|  | 242 | TemplateName SpecName = SpecType->getTemplateName(); | 
|  | 243 |  | 
|  | 244 | // The class template we found is the same template being | 
|  | 245 | // specialized. | 
|  | 246 | if (TemplateDecl *SpecTemplate = SpecName.getAsTemplateDecl()) { | 
|  | 247 | if (SpecTemplate->getCanonicalDecl() == Template->getCanonicalDecl()) | 
|  | 248 | return MemberOfType.getAsOpaquePtr(); | 
|  | 249 |  | 
|  | 250 | continue; | 
|  | 251 | } | 
|  | 252 |  | 
|  | 253 | // The class template we found has the same name as the | 
|  | 254 | // (dependent) template name being specialized. | 
|  | 255 | if (DependentTemplateName *DepTemplate | 
|  | 256 | = SpecName.getAsDependentTemplateName()) { | 
|  | 257 | if (DepTemplate->isIdentifier() && | 
|  | 258 | DepTemplate->getIdentifier() == Template->getIdentifier()) | 
|  | 259 | return MemberOfType.getAsOpaquePtr(); | 
|  | 260 |  | 
|  | 261 | continue; | 
|  | 262 | } | 
|  | 263 | } | 
|  | 264 | } | 
|  | 265 | } | 
|  | 266 |  | 
|  | 267 | if (isDependent) { | 
|  | 268 | // We didn't find our type, but that's okay: it's dependent | 
|  | 269 | // anyway. | 
|  | 270 | NestedNameSpecifier *NNS = 0; | 
|  | 271 | SourceRange Range; | 
|  | 272 | if (SS.isSet()) { | 
|  | 273 | NNS = (NestedNameSpecifier *)SS.getScopeRep(); | 
|  | 274 | Range = SourceRange(SS.getRange().getBegin(), NameLoc); | 
|  | 275 | } else { | 
|  | 276 | NNS = NestedNameSpecifier::Create(Context, &II); | 
|  | 277 | Range = SourceRange(NameLoc); | 
|  | 278 | } | 
|  | 279 |  | 
|  | 280 | return CheckTypenameType(NNS, II, Range).getAsOpaquePtr(); | 
|  | 281 | } | 
|  | 282 |  | 
|  | 283 | if (ObjectTypePtr) | 
|  | 284 | Diag(NameLoc, diag::err_ident_in_pseudo_dtor_not_a_type) | 
|  | 285 | << &II; | 
|  | 286 | else | 
|  | 287 | Diag(NameLoc, diag::err_destructor_class_name); | 
|  | 288 |  | 
|  | 289 | return 0; | 
|  | 290 | } | 
|  | 291 |  | 
| Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 292 | /// ActOnCXXTypeidOfType - Parse typeid( type-id ). | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 293 | Action::OwningExprResult | 
| Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 294 | Sema::ActOnCXXTypeid(SourceLocation OpLoc, SourceLocation LParenLoc, | 
|  | 295 | bool isType, void *TyOrExpr, SourceLocation RParenLoc) { | 
| Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 296 | if (!StdNamespace) | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 297 | return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid)); | 
| Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 298 |  | 
| Douglas Gregor | f57f207 | 2009-12-23 20:51:04 +0000 | [diff] [blame] | 299 | if (isType) { | 
|  | 300 | // C++ [expr.typeid]p4: | 
|  | 301 | //   The top-level cv-qualifiers of the lvalue expression or the type-id | 
|  | 302 | //   that is the operand of typeid are always ignored. | 
| Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 303 | // FIXME: Preserve type source info. | 
| Douglas Gregor | f57f207 | 2009-12-23 20:51:04 +0000 | [diff] [blame] | 304 | // FIXME: Preserve the type before we stripped the cv-qualifiers? | 
| Douglas Gregor | 765ccba | 2009-12-23 21:06:06 +0000 | [diff] [blame] | 305 | QualType T = GetTypeFromParser(TyOrExpr); | 
|  | 306 | if (T.isNull()) | 
|  | 307 | return ExprError(); | 
|  | 308 |  | 
|  | 309 | // C++ [expr.typeid]p4: | 
|  | 310 | //   If the type of the type-id is a class type or a reference to a class | 
|  | 311 | //   type, the class shall be completely-defined. | 
|  | 312 | QualType CheckT = T; | 
|  | 313 | if (const ReferenceType *RefType = CheckT->getAs<ReferenceType>()) | 
|  | 314 | CheckT = RefType->getPointeeType(); | 
|  | 315 |  | 
|  | 316 | if (CheckT->getAs<RecordType>() && | 
|  | 317 | RequireCompleteType(OpLoc, CheckT, diag::err_incomplete_typeid)) | 
|  | 318 | return ExprError(); | 
|  | 319 |  | 
|  | 320 | TyOrExpr = T.getUnqualifiedType().getAsOpaquePtr(); | 
| Douglas Gregor | f57f207 | 2009-12-23 20:51:04 +0000 | [diff] [blame] | 321 | } | 
| Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 322 |  | 
| Chris Lattner | 572af49 | 2008-11-20 05:51:55 +0000 | [diff] [blame] | 323 | IdentifierInfo *TypeInfoII = &PP.getIdentifierTable().get("type_info"); | 
| John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 324 | LookupResult R(*this, TypeInfoII, SourceLocation(), LookupTagName); | 
|  | 325 | LookupQualifiedName(R, StdNamespace); | 
| John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 326 | RecordDecl *TypeInfoRecordDecl = R.getAsSingle<RecordDecl>(); | 
| Chris Lattner | 572af49 | 2008-11-20 05:51:55 +0000 | [diff] [blame] | 327 | if (!TypeInfoRecordDecl) | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 328 | return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid)); | 
| Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 329 |  | 
|  | 330 | QualType TypeInfoType = Context.getTypeDeclType(TypeInfoRecordDecl); | 
|  | 331 |  | 
| Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 332 | if (!isType) { | 
| Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 333 | bool isUnevaluatedOperand = true; | 
|  | 334 | Expr *E = static_cast<Expr *>(TyOrExpr); | 
| Douglas Gregor | f57f207 | 2009-12-23 20:51:04 +0000 | [diff] [blame] | 335 | if (E && !E->isTypeDependent()) { | 
| Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 336 | QualType T = E->getType(); | 
| Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 337 | if (const RecordType *RecordT = T->getAs<RecordType>()) { | 
| Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 338 | CXXRecordDecl *RecordD = cast<CXXRecordDecl>(RecordT->getDecl()); | 
| Douglas Gregor | f57f207 | 2009-12-23 20:51:04 +0000 | [diff] [blame] | 339 | // C++ [expr.typeid]p3: | 
| John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 340 | //   [...] If the type of the expression is a class type, the class | 
|  | 341 | //   shall be completely-defined. | 
|  | 342 | if (RequireCompleteType(OpLoc, T, diag::err_incomplete_typeid)) | 
|  | 343 | return ExprError(); | 
|  | 344 |  | 
|  | 345 | // C++ [expr.typeid]p3: | 
| Douglas Gregor | f57f207 | 2009-12-23 20:51:04 +0000 | [diff] [blame] | 346 | //   When typeid is applied to an expression other than an lvalue of a | 
|  | 347 | //   polymorphic class type [...] [the] expression is an unevaluated | 
|  | 348 | //   operand. [...] | 
|  | 349 | if (RecordD->isPolymorphic() && E->isLvalue(Context) == Expr::LV_Valid) | 
| Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 350 | isUnevaluatedOperand = false; | 
| Douglas Gregor | f57f207 | 2009-12-23 20:51:04 +0000 | [diff] [blame] | 351 | } | 
|  | 352 |  | 
|  | 353 | // C++ [expr.typeid]p4: | 
|  | 354 | //   [...] If the type of the type-id is a reference to a possibly | 
|  | 355 | //   cv-qualified type, the result of the typeid expression refers to a | 
|  | 356 | //   std::type_info object representing the cv-unqualified referenced | 
|  | 357 | //   type. | 
|  | 358 | if (T.hasQualifiers()) { | 
|  | 359 | ImpCastExprToType(E, T.getUnqualifiedType(), CastExpr::CK_NoOp, | 
|  | 360 | E->isLvalue(Context)); | 
|  | 361 | TyOrExpr = E; | 
| Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 362 | } | 
|  | 363 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 364 |  | 
| Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 365 | // If this is an unevaluated operand, clear out the set of | 
|  | 366 | // declaration references we have been computing and eliminate any | 
|  | 367 | // temporaries introduced in its computation. | 
| Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 368 | if (isUnevaluatedOperand) | 
| Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 369 | ExprEvalContexts.back().Context = Unevaluated; | 
| Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 370 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 371 |  | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 372 | return Owned(new (Context) CXXTypeidExpr(isType, TyOrExpr, | 
|  | 373 | TypeInfoType.withConst(), | 
|  | 374 | SourceRange(OpLoc, RParenLoc))); | 
| Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 375 | } | 
|  | 376 |  | 
| Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 377 | /// ActOnCXXBoolLiteral - Parse {true,false} literals. | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 378 | Action::OwningExprResult | 
| Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 379 | Sema::ActOnCXXBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) { | 
| Douglas Gregor | 2f639b9 | 2008-10-24 15:36:09 +0000 | [diff] [blame] | 380 | assert((Kind == tok::kw_true || Kind == tok::kw_false) && | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 381 | "Unknown C++ Boolean value!"); | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 382 | return Owned(new (Context) CXXBoolLiteralExpr(Kind == tok::kw_true, | 
|  | 383 | Context.BoolTy, OpLoc)); | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 384 | } | 
| Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 385 |  | 
| Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 386 | /// ActOnCXXNullPtrLiteral - Parse 'nullptr'. | 
|  | 387 | Action::OwningExprResult | 
|  | 388 | Sema::ActOnCXXNullPtrLiteral(SourceLocation Loc) { | 
|  | 389 | return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc)); | 
|  | 390 | } | 
|  | 391 |  | 
| Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 392 | /// ActOnCXXThrow - Parse throw expressions. | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 393 | Action::OwningExprResult | 
|  | 394 | Sema::ActOnCXXThrow(SourceLocation OpLoc, ExprArg E) { | 
| Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 395 | Expr *Ex = E.takeAs<Expr>(); | 
|  | 396 | if (Ex && !Ex->isTypeDependent() && CheckCXXThrowOperand(OpLoc, Ex)) | 
|  | 397 | return ExprError(); | 
|  | 398 | return Owned(new (Context) CXXThrowExpr(Ex, Context.VoidTy, OpLoc)); | 
|  | 399 | } | 
|  | 400 |  | 
|  | 401 | /// CheckCXXThrowOperand - Validate the operand of a throw. | 
|  | 402 | bool Sema::CheckCXXThrowOperand(SourceLocation ThrowLoc, Expr *&E) { | 
|  | 403 | // C++ [except.throw]p3: | 
| Douglas Gregor | 154fe98 | 2009-12-23 22:04:40 +0000 | [diff] [blame] | 404 | //   A throw-expression initializes a temporary object, called the exception | 
|  | 405 | //   object, the type of which is determined by removing any top-level | 
|  | 406 | //   cv-qualifiers from the static type of the operand of throw and adjusting | 
|  | 407 | //   the type from "array of T" or "function returning T" to "pointer to T" | 
|  | 408 | //   or "pointer to function returning T", [...] | 
|  | 409 | if (E->getType().hasQualifiers()) | 
|  | 410 | ImpCastExprToType(E, E->getType().getUnqualifiedType(), CastExpr::CK_NoOp, | 
|  | 411 | E->isLvalue(Context) == Expr::LV_Valid); | 
|  | 412 |  | 
| Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 413 | DefaultFunctionArrayConversion(E); | 
|  | 414 |  | 
|  | 415 | //   If the type of the exception would be an incomplete type or a pointer | 
|  | 416 | //   to an incomplete type other than (cv) void the program is ill-formed. | 
|  | 417 | QualType Ty = E->getType(); | 
|  | 418 | int isPointer = 0; | 
| Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 419 | if (const PointerType* Ptr = Ty->getAs<PointerType>()) { | 
| Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 420 | Ty = Ptr->getPointeeType(); | 
|  | 421 | isPointer = 1; | 
|  | 422 | } | 
|  | 423 | if (!isPointer || !Ty->isVoidType()) { | 
|  | 424 | if (RequireCompleteType(ThrowLoc, Ty, | 
| Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 425 | PDiag(isPointer ? diag::err_throw_incomplete_ptr | 
|  | 426 | : diag::err_throw_incomplete) | 
|  | 427 | << E->getSourceRange())) | 
| Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 428 | return true; | 
|  | 429 | } | 
|  | 430 |  | 
|  | 431 | // FIXME: Construct a temporary here. | 
|  | 432 | return false; | 
| Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 433 | } | 
| Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 434 |  | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 435 | Action::OwningExprResult Sema::ActOnCXXThis(SourceLocation ThisLoc) { | 
| Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 436 | /// C++ 9.3.2: In the body of a non-static member function, the keyword this | 
|  | 437 | /// is a non-lvalue expression whose value is the address of the object for | 
|  | 438 | /// which the function is called. | 
|  | 439 |  | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 440 | if (!isa<FunctionDecl>(CurContext)) | 
|  | 441 | return ExprError(Diag(ThisLoc, diag::err_invalid_this_use)); | 
| Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 442 |  | 
|  | 443 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) | 
|  | 444 | if (MD->isInstance()) | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 445 | return Owned(new (Context) CXXThisExpr(ThisLoc, | 
| Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 446 | MD->getThisType(Context), | 
|  | 447 | /*isImplicit=*/false)); | 
| Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 448 |  | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 449 | return ExprError(Diag(ThisLoc, diag::err_invalid_this_use)); | 
| Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 450 | } | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 451 |  | 
|  | 452 | /// ActOnCXXTypeConstructExpr - Parse construction of a specified type. | 
|  | 453 | /// Can be interpreted either as function-style casting ("int(x)") | 
|  | 454 | /// or class type construction ("ClassType(x,y,z)") | 
|  | 455 | /// or creation of a value-initialized type ("int()"). | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 456 | Action::OwningExprResult | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 457 | Sema::ActOnCXXTypeConstructExpr(SourceRange TypeRange, TypeTy *TypeRep, | 
|  | 458 | SourceLocation LParenLoc, | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 459 | MultiExprArg exprs, | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 460 | SourceLocation *CommaLocs, | 
|  | 461 | SourceLocation RParenLoc) { | 
| Douglas Gregor | ae4c77d | 2010-02-05 19:11:37 +0000 | [diff] [blame] | 462 | if (!TypeRep) | 
|  | 463 | return ExprError(); | 
|  | 464 |  | 
| John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 465 | TypeSourceInfo *TInfo; | 
|  | 466 | QualType Ty = GetTypeFromParser(TypeRep, &TInfo); | 
|  | 467 | if (!TInfo) | 
|  | 468 | TInfo = Context.getTrivialTypeSourceInfo(Ty, SourceLocation()); | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 469 | unsigned NumExprs = exprs.size(); | 
|  | 470 | Expr **Exprs = (Expr**)exprs.get(); | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 471 | SourceLocation TyBeginLoc = TypeRange.getBegin(); | 
|  | 472 | SourceRange FullRange = SourceRange(TyBeginLoc, RParenLoc); | 
|  | 473 |  | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 474 | if (Ty->isDependentType() || | 
| Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 475 | CallExpr::hasAnyTypeDependentArguments(Exprs, NumExprs)) { | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 476 | exprs.release(); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 477 |  | 
|  | 478 | return Owned(CXXUnresolvedConstructExpr::Create(Context, | 
|  | 479 | TypeRange.getBegin(), Ty, | 
| Douglas Gregor | d81e6ca | 2009-05-20 18:46:25 +0000 | [diff] [blame] | 480 | LParenLoc, | 
|  | 481 | Exprs, NumExprs, | 
|  | 482 | RParenLoc)); | 
| Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 483 | } | 
|  | 484 |  | 
| Anders Carlsson | bb60a50 | 2009-08-27 03:53:50 +0000 | [diff] [blame] | 485 | if (Ty->isArrayType()) | 
|  | 486 | return ExprError(Diag(TyBeginLoc, | 
|  | 487 | diag::err_value_init_for_array_type) << FullRange); | 
|  | 488 | if (!Ty->isVoidType() && | 
|  | 489 | RequireCompleteType(TyBeginLoc, Ty, | 
|  | 490 | PDiag(diag::err_invalid_incomplete_type_use) | 
|  | 491 | << FullRange)) | 
|  | 492 | return ExprError(); | 
| Fariborz Jahanian | f071e9b | 2009-10-23 21:01:39 +0000 | [diff] [blame] | 493 |  | 
| Anders Carlsson | bb60a50 | 2009-08-27 03:53:50 +0000 | [diff] [blame] | 494 | if (RequireNonAbstractType(TyBeginLoc, Ty, | 
|  | 495 | diag::err_allocation_of_abstract_type)) | 
|  | 496 | return ExprError(); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 497 |  | 
|  | 498 |  | 
| Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 499 | // C++ [expr.type.conv]p1: | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 500 | // If the expression list is a single expression, the type conversion | 
|  | 501 | // expression is equivalent (in definedness, and if defined in meaning) to the | 
|  | 502 | // corresponding cast expression. | 
|  | 503 | // | 
|  | 504 | if (NumExprs == 1) { | 
| Anders Carlsson | cdb6197 | 2009-08-07 22:21:05 +0000 | [diff] [blame] | 505 | CastExpr::CastKind Kind = CastExpr::CK_Unknown; | 
| Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 506 | CXXMethodDecl *Method = 0; | 
|  | 507 | if (CheckCastTypes(TypeRange, Ty, Exprs[0], Kind, Method, | 
|  | 508 | /*FunctionalStyle=*/true)) | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 509 | return ExprError(); | 
| Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 510 |  | 
|  | 511 | exprs.release(); | 
|  | 512 | if (Method) { | 
|  | 513 | OwningExprResult CastArg | 
|  | 514 | = BuildCXXCastArgument(TypeRange.getBegin(), Ty.getNonReferenceType(), | 
|  | 515 | Kind, Method, Owned(Exprs[0])); | 
|  | 516 | if (CastArg.isInvalid()) | 
|  | 517 | return ExprError(); | 
|  | 518 |  | 
|  | 519 | Exprs[0] = CastArg.takeAs<Expr>(); | 
| Fariborz Jahanian | 4fc7ab3 | 2009-08-28 15:11:24 +0000 | [diff] [blame] | 520 | } | 
| Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 521 |  | 
|  | 522 | return Owned(new (Context) CXXFunctionalCastExpr(Ty.getNonReferenceType(), | 
| John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 523 | TInfo, TyBeginLoc, Kind, | 
| Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 524 | Exprs[0], RParenLoc)); | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 525 | } | 
|  | 526 |  | 
| Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 527 | if (const RecordType *RT = Ty->getAs<RecordType>()) { | 
| Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 528 | CXXRecordDecl *Record = cast<CXXRecordDecl>(RT->getDecl()); | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 529 |  | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 530 | if (NumExprs > 1 || !Record->hasTrivialConstructor() || | 
| Anders Carlsson | e7624a7 | 2009-08-27 05:08:22 +0000 | [diff] [blame] | 531 | !Record->hasTrivialDestructor()) { | 
| Eli Friedman | 6997aae | 2010-01-31 20:58:15 +0000 | [diff] [blame] | 532 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(Ty); | 
|  | 533 | InitializationKind Kind | 
|  | 534 | = NumExprs ? InitializationKind::CreateDirect(TypeRange.getBegin(), | 
|  | 535 | LParenLoc, RParenLoc) | 
|  | 536 | : InitializationKind::CreateValue(TypeRange.getBegin(), | 
|  | 537 | LParenLoc, RParenLoc); | 
|  | 538 | InitializationSequence InitSeq(*this, Entity, Kind, Exprs, NumExprs); | 
|  | 539 | OwningExprResult Result = InitSeq.Perform(*this, Entity, Kind, | 
|  | 540 | move(exprs)); | 
| Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 541 |  | 
| Eli Friedman | 6997aae | 2010-01-31 20:58:15 +0000 | [diff] [blame] | 542 | // FIXME: Improve AST representation? | 
|  | 543 | return move(Result); | 
| Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 544 | } | 
|  | 545 |  | 
|  | 546 | // Fall through to value-initialize an object of class type that | 
|  | 547 | // doesn't have a user-declared default constructor. | 
|  | 548 | } | 
|  | 549 |  | 
|  | 550 | // C++ [expr.type.conv]p1: | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 551 | // If the expression list specifies more than a single value, the type shall | 
|  | 552 | // be a class with a suitably declared constructor. | 
|  | 553 | // | 
|  | 554 | if (NumExprs > 1) | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 555 | return ExprError(Diag(CommaLocs[0], | 
|  | 556 | diag::err_builtin_func_cast_more_than_one_arg) | 
|  | 557 | << FullRange); | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 558 |  | 
|  | 559 | assert(NumExprs == 0 && "Expected 0 expressions"); | 
| Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 560 | // C++ [expr.type.conv]p2: | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 561 | // The expression T(), where T is a simple-type-specifier for a non-array | 
|  | 562 | // complete object type or the (possibly cv-qualified) void type, creates an | 
|  | 563 | // rvalue of the specified type, which is value-initialized. | 
|  | 564 | // | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 565 | exprs.release(); | 
|  | 566 | return Owned(new (Context) CXXZeroInitValueExpr(Ty, TyBeginLoc, RParenLoc)); | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 567 | } | 
| Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 568 |  | 
|  | 569 |  | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 570 | /// ActOnCXXNew - Parsed a C++ 'new' expression (C++ 5.3.4), as in e.g.: | 
|  | 571 | /// @code new (memory) int[size][4] @endcode | 
|  | 572 | /// or | 
|  | 573 | /// @code ::new Foo(23, "hello") @endcode | 
|  | 574 | /// For the interpretation of this heap of arguments, consult the base version. | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 575 | Action::OwningExprResult | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 576 | Sema::ActOnCXXNew(SourceLocation StartLoc, bool UseGlobal, | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 577 | SourceLocation PlacementLParen, MultiExprArg PlacementArgs, | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 578 | SourceLocation PlacementRParen, bool ParenTypeId, | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 579 | Declarator &D, SourceLocation ConstructorLParen, | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 580 | MultiExprArg ConstructorArgs, | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 581 | SourceLocation ConstructorRParen) { | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 582 | Expr *ArraySize = 0; | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 583 | // If the specified type is an array, unwrap it and save the expression. | 
|  | 584 | if (D.getNumTypeObjects() > 0 && | 
|  | 585 | D.getTypeObject(0).Kind == DeclaratorChunk::Array) { | 
|  | 586 | DeclaratorChunk &Chunk = D.getTypeObject(0); | 
|  | 587 | if (Chunk.Arr.hasStatic) | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 588 | return ExprError(Diag(Chunk.Loc, diag::err_static_illegal_in_new) | 
|  | 589 | << D.getSourceRange()); | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 590 | if (!Chunk.Arr.NumElts) | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 591 | return ExprError(Diag(Chunk.Loc, diag::err_array_new_needs_size) | 
|  | 592 | << D.getSourceRange()); | 
| Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 593 |  | 
|  | 594 | if (ParenTypeId) { | 
|  | 595 | // Can't have dynamic array size when the type-id is in parentheses. | 
|  | 596 | Expr *NumElts = (Expr *)Chunk.Arr.NumElts; | 
|  | 597 | if (!NumElts->isTypeDependent() && !NumElts->isValueDependent() && | 
|  | 598 | !NumElts->isIntegerConstantExpr(Context)) { | 
|  | 599 | Diag(D.getTypeObject(0).Loc, diag::err_new_paren_array_nonconst) | 
|  | 600 | << NumElts->getSourceRange(); | 
|  | 601 | return ExprError(); | 
|  | 602 | } | 
|  | 603 | } | 
|  | 604 |  | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 605 | ArraySize = static_cast<Expr*>(Chunk.Arr.NumElts); | 
| Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 606 | D.DropFirstTypeObject(); | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 607 | } | 
|  | 608 |  | 
| Douglas Gregor | 043cad2 | 2009-09-11 00:18:58 +0000 | [diff] [blame] | 609 | // Every dimension shall be of constant size. | 
| Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 610 | if (ArraySize) { | 
|  | 611 | for (unsigned I = 0, N = D.getNumTypeObjects(); I < N; ++I) { | 
| Douglas Gregor | 043cad2 | 2009-09-11 00:18:58 +0000 | [diff] [blame] | 612 | if (D.getTypeObject(I).Kind != DeclaratorChunk::Array) | 
|  | 613 | break; | 
|  | 614 |  | 
|  | 615 | DeclaratorChunk::ArrayTypeInfo &Array = D.getTypeObject(I).Arr; | 
|  | 616 | if (Expr *NumElts = (Expr *)Array.NumElts) { | 
|  | 617 | if (!NumElts->isTypeDependent() && !NumElts->isValueDependent() && | 
|  | 618 | !NumElts->isIntegerConstantExpr(Context)) { | 
|  | 619 | Diag(D.getTypeObject(I).Loc, diag::err_new_array_nonconst) | 
|  | 620 | << NumElts->getSourceRange(); | 
|  | 621 | return ExprError(); | 
|  | 622 | } | 
|  | 623 | } | 
|  | 624 | } | 
|  | 625 | } | 
| Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 626 |  | 
| John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 627 | //FIXME: Store TypeSourceInfo in CXXNew expression. | 
|  | 628 | TypeSourceInfo *TInfo = 0; | 
|  | 629 | QualType AllocType = GetTypeForDeclarator(D, /*Scope=*/0, &TInfo); | 
| Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 630 | if (D.isInvalidType()) | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 631 | return ExprError(); | 
| Fariborz Jahanian | 498429f | 2009-11-19 18:39:40 +0000 | [diff] [blame] | 632 |  | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 633 | return BuildCXXNew(StartLoc, UseGlobal, | 
| Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 634 | PlacementLParen, | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 635 | move(PlacementArgs), | 
| Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 636 | PlacementRParen, | 
|  | 637 | ParenTypeId, | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 638 | AllocType, | 
| Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 639 | D.getSourceRange().getBegin(), | 
|  | 640 | D.getSourceRange(), | 
|  | 641 | Owned(ArraySize), | 
|  | 642 | ConstructorLParen, | 
|  | 643 | move(ConstructorArgs), | 
|  | 644 | ConstructorRParen); | 
|  | 645 | } | 
|  | 646 |  | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 647 | Sema::OwningExprResult | 
| Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 648 | Sema::BuildCXXNew(SourceLocation StartLoc, bool UseGlobal, | 
|  | 649 | SourceLocation PlacementLParen, | 
|  | 650 | MultiExprArg PlacementArgs, | 
|  | 651 | SourceLocation PlacementRParen, | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 652 | bool ParenTypeId, | 
| Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 653 | QualType AllocType, | 
|  | 654 | SourceLocation TypeLoc, | 
|  | 655 | SourceRange TypeRange, | 
|  | 656 | ExprArg ArraySizeE, | 
|  | 657 | SourceLocation ConstructorLParen, | 
|  | 658 | MultiExprArg ConstructorArgs, | 
|  | 659 | SourceLocation ConstructorRParen) { | 
|  | 660 | if (CheckAllocatedType(AllocType, TypeLoc, TypeRange)) | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 661 | return ExprError(); | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 662 |  | 
| Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 663 | QualType ResultType = Context.getPointerType(AllocType); | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 664 |  | 
|  | 665 | // That every array dimension except the first is constant was already | 
|  | 666 | // checked by the type check above. | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 667 |  | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 668 | // C++ 5.3.4p6: "The expression in a direct-new-declarator shall have integral | 
|  | 669 | //   or enumeration type with a non-negative value." | 
| Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 670 | Expr *ArraySize = (Expr *)ArraySizeE.get(); | 
| Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 671 | if (ArraySize && !ArraySize->isTypeDependent()) { | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 672 | QualType SizeType = ArraySize->getType(); | 
|  | 673 | if (!SizeType->isIntegralType() && !SizeType->isEnumeralType()) | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 674 | return ExprError(Diag(ArraySize->getSourceRange().getBegin(), | 
|  | 675 | diag::err_array_size_not_integral) | 
|  | 676 | << SizeType << ArraySize->getSourceRange()); | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 677 | // Let's see if this is a constant < 0. If so, we reject it out of hand. | 
|  | 678 | // We don't care about special rules, so we tell the machinery it's not | 
|  | 679 | // evaluated - it gives us a result in more cases. | 
| Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 680 | if (!ArraySize->isValueDependent()) { | 
|  | 681 | llvm::APSInt Value; | 
|  | 682 | if (ArraySize->isIntegerConstantExpr(Value, Context, 0, false)) { | 
|  | 683 | if (Value < llvm::APSInt( | 
| Anders Carlsson | ac18b2e | 2009-09-23 00:37:25 +0000 | [diff] [blame] | 684 | llvm::APInt::getNullValue(Value.getBitWidth()), | 
|  | 685 | Value.isUnsigned())) | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 686 | return ExprError(Diag(ArraySize->getSourceRange().getBegin(), | 
|  | 687 | diag::err_typecheck_negative_array_size) | 
|  | 688 | << ArraySize->getSourceRange()); | 
| Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 689 | } | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 690 | } | 
| Anders Carlsson | ac18b2e | 2009-09-23 00:37:25 +0000 | [diff] [blame] | 691 |  | 
| Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 692 | ImpCastExprToType(ArraySize, Context.getSizeType(), | 
|  | 693 | CastExpr::CK_IntegralCast); | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 694 | } | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 695 |  | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 696 | FunctionDecl *OperatorNew = 0; | 
|  | 697 | FunctionDecl *OperatorDelete = 0; | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 698 | Expr **PlaceArgs = (Expr**)PlacementArgs.get(); | 
|  | 699 | unsigned NumPlaceArgs = PlacementArgs.size(); | 
| Fariborz Jahanian | 498429f | 2009-11-19 18:39:40 +0000 | [diff] [blame] | 700 |  | 
| Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 701 | if (!AllocType->isDependentType() && | 
|  | 702 | !Expr::hasAnyTypeDependentArguments(PlaceArgs, NumPlaceArgs) && | 
|  | 703 | FindAllocationFunctions(StartLoc, | 
| Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 704 | SourceRange(PlacementLParen, PlacementRParen), | 
|  | 705 | UseGlobal, AllocType, ArraySize, PlaceArgs, | 
|  | 706 | NumPlaceArgs, OperatorNew, OperatorDelete)) | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 707 | return ExprError(); | 
| Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 708 | llvm::SmallVector<Expr *, 8> AllPlaceArgs; | 
| Fariborz Jahanian | 498429f | 2009-11-19 18:39:40 +0000 | [diff] [blame] | 709 | if (OperatorNew) { | 
|  | 710 | // Add default arguments, if any. | 
|  | 711 | const FunctionProtoType *Proto = | 
|  | 712 | OperatorNew->getType()->getAs<FunctionProtoType>(); | 
| Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 713 | VariadicCallType CallType = | 
|  | 714 | Proto->isVariadic() ? VariadicFunction : VariadicDoesNotApply; | 
| Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 715 | bool Invalid = GatherArgumentsForCall(PlacementLParen, OperatorNew, | 
|  | 716 | Proto, 1, PlaceArgs, NumPlaceArgs, | 
| Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 717 | AllPlaceArgs, CallType); | 
| Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 718 | if (Invalid) | 
|  | 719 | return ExprError(); | 
| Fariborz Jahanian | 498429f | 2009-11-19 18:39:40 +0000 | [diff] [blame] | 720 |  | 
| Fariborz Jahanian | 498429f | 2009-11-19 18:39:40 +0000 | [diff] [blame] | 721 | NumPlaceArgs = AllPlaceArgs.size(); | 
|  | 722 | if (NumPlaceArgs > 0) | 
|  | 723 | PlaceArgs = &AllPlaceArgs[0]; | 
|  | 724 | } | 
|  | 725 |  | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 726 | bool Init = ConstructorLParen.isValid(); | 
|  | 727 | // --- Choosing a constructor --- | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 728 | CXXConstructorDecl *Constructor = 0; | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 729 | Expr **ConsArgs = (Expr**)ConstructorArgs.get(); | 
|  | 730 | unsigned NumConsArgs = ConstructorArgs.size(); | 
| Eli Friedman | a8ce9ec | 2009-11-08 22:15:39 +0000 | [diff] [blame] | 731 | ASTOwningVector<&ActionBase::DeleteExpr> ConvertedConstructorArgs(*this); | 
|  | 732 |  | 
| Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 733 | if (!AllocType->isDependentType() && | 
|  | 734 | !Expr::hasAnyTypeDependentArguments(ConsArgs, NumConsArgs)) { | 
|  | 735 | // C++0x [expr.new]p15: | 
|  | 736 | //   A new-expression that creates an object of type T initializes that | 
|  | 737 | //   object as follows: | 
|  | 738 | InitializationKind Kind | 
|  | 739 | //     - If the new-initializer is omitted, the object is default- | 
|  | 740 | //       initialized (8.5); if no initialization is performed, | 
|  | 741 | //       the object has indeterminate value | 
|  | 742 | = !Init? InitializationKind::CreateDefault(TypeLoc) | 
|  | 743 | //     - Otherwise, the new-initializer is interpreted according to the | 
|  | 744 | //       initialization rules of 8.5 for direct-initialization. | 
|  | 745 | : InitializationKind::CreateDirect(TypeLoc, | 
|  | 746 | ConstructorLParen, | 
|  | 747 | ConstructorRParen); | 
|  | 748 |  | 
| Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 749 | InitializedEntity Entity | 
| Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 750 | = InitializedEntity::InitializeNew(StartLoc, AllocType); | 
| Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 751 | InitializationSequence InitSeq(*this, Entity, Kind, ConsArgs, NumConsArgs); | 
| Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 752 | OwningExprResult FullInit = InitSeq.Perform(*this, Entity, Kind, | 
|  | 753 | move(ConstructorArgs)); | 
|  | 754 | if (FullInit.isInvalid()) | 
|  | 755 | return ExprError(); | 
|  | 756 |  | 
|  | 757 | // FullInit is our initializer; walk through it to determine if it's a | 
|  | 758 | // constructor call, which CXXNewExpr handles directly. | 
|  | 759 | if (Expr *FullInitExpr = (Expr *)FullInit.get()) { | 
|  | 760 | if (CXXBindTemporaryExpr *Binder | 
|  | 761 | = dyn_cast<CXXBindTemporaryExpr>(FullInitExpr)) | 
|  | 762 | FullInitExpr = Binder->getSubExpr(); | 
|  | 763 | if (CXXConstructExpr *Construct | 
|  | 764 | = dyn_cast<CXXConstructExpr>(FullInitExpr)) { | 
|  | 765 | Constructor = Construct->getConstructor(); | 
|  | 766 | for (CXXConstructExpr::arg_iterator A = Construct->arg_begin(), | 
|  | 767 | AEnd = Construct->arg_end(); | 
|  | 768 | A != AEnd; ++A) | 
|  | 769 | ConvertedConstructorArgs.push_back(A->Retain()); | 
|  | 770 | } else { | 
|  | 771 | // Take the converted initializer. | 
|  | 772 | ConvertedConstructorArgs.push_back(FullInit.release()); | 
|  | 773 | } | 
|  | 774 | } else { | 
|  | 775 | // No initialization required. | 
|  | 776 | } | 
|  | 777 |  | 
|  | 778 | // Take the converted arguments and use them for the new expression. | 
| Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 779 | NumConsArgs = ConvertedConstructorArgs.size(); | 
|  | 780 | ConsArgs = (Expr **)ConvertedConstructorArgs.take(); | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 781 | } | 
| Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 782 |  | 
| Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame^] | 783 | // Mark the new and delete operators as referenced. | 
|  | 784 | if (OperatorNew) | 
|  | 785 | MarkDeclarationReferenced(StartLoc, OperatorNew); | 
|  | 786 | if (OperatorDelete) | 
|  | 787 | MarkDeclarationReferenced(StartLoc, OperatorDelete); | 
|  | 788 |  | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 789 | // FIXME: Also check that the destructor is accessible. (C++ 5.3.4p16) | 
| Douglas Gregor | 089407b | 2009-10-17 21:40:42 +0000 | [diff] [blame] | 790 |  | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 791 | PlacementArgs.release(); | 
|  | 792 | ConstructorArgs.release(); | 
| Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 793 | ArraySizeE.release(); | 
| Ted Kremenek | ad7fe86 | 2010-02-11 22:51:03 +0000 | [diff] [blame] | 794 | return Owned(new (Context) CXXNewExpr(Context, UseGlobal, OperatorNew, | 
|  | 795 | PlaceArgs, NumPlaceArgs, ParenTypeId, | 
|  | 796 | ArraySize, Constructor, Init, | 
|  | 797 | ConsArgs, NumConsArgs, OperatorDelete, | 
|  | 798 | ResultType, StartLoc, | 
|  | 799 | Init ? ConstructorRParen : | 
|  | 800 | SourceLocation())); | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 801 | } | 
|  | 802 |  | 
|  | 803 | /// CheckAllocatedType - Checks that a type is suitable as the allocated type | 
|  | 804 | /// in a new-expression. | 
|  | 805 | /// dimension off and stores the size expression in ArraySize. | 
| Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 806 | bool Sema::CheckAllocatedType(QualType AllocType, SourceLocation Loc, | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 807 | SourceRange R) { | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 808 | // C++ 5.3.4p1: "[The] type shall be a complete object type, but not an | 
|  | 809 | //   abstract class type or array thereof. | 
| Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 810 | if (AllocType->isFunctionType()) | 
| Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 811 | return Diag(Loc, diag::err_bad_new_type) | 
|  | 812 | << AllocType << 0 << R; | 
| Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 813 | else if (AllocType->isReferenceType()) | 
| Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 814 | return Diag(Loc, diag::err_bad_new_type) | 
|  | 815 | << AllocType << 1 << R; | 
| Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 816 | else if (!AllocType->isDependentType() && | 
| Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 817 | RequireCompleteType(Loc, AllocType, | 
| Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 818 | PDiag(diag::err_new_incomplete_type) | 
|  | 819 | << R)) | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 820 | return true; | 
| Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 821 | else if (RequireNonAbstractType(Loc, AllocType, | 
| Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 822 | diag::err_allocation_of_abstract_type)) | 
|  | 823 | return true; | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 824 |  | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 825 | return false; | 
|  | 826 | } | 
|  | 827 |  | 
| Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame^] | 828 | /// \brief Determine whether the given function is a non-placement | 
|  | 829 | /// deallocation function. | 
|  | 830 | static bool isNonPlacementDeallocationFunction(FunctionDecl *FD) { | 
|  | 831 | if (FD->isInvalidDecl()) | 
|  | 832 | return false; | 
|  | 833 |  | 
|  | 834 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FD)) | 
|  | 835 | return Method->isUsualDeallocationFunction(); | 
|  | 836 |  | 
|  | 837 | return ((FD->getOverloadedOperator() == OO_Delete || | 
|  | 838 | FD->getOverloadedOperator() == OO_Array_Delete) && | 
|  | 839 | FD->getNumParams() == 1); | 
|  | 840 | } | 
|  | 841 |  | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 842 | /// FindAllocationFunctions - Finds the overloads of operator new and delete | 
|  | 843 | /// that are appropriate for the allocation. | 
| Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 844 | bool Sema::FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range, | 
|  | 845 | bool UseGlobal, QualType AllocType, | 
|  | 846 | bool IsArray, Expr **PlaceArgs, | 
|  | 847 | unsigned NumPlaceArgs, | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 848 | FunctionDecl *&OperatorNew, | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 849 | FunctionDecl *&OperatorDelete) { | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 850 | // --- Choosing an allocation function --- | 
|  | 851 | // C++ 5.3.4p8 - 14 & 18 | 
|  | 852 | // 1) If UseGlobal is true, only look in the global scope. Else, also look | 
|  | 853 | //   in the scope of the allocated class. | 
|  | 854 | // 2) If an array size is given, look for operator new[], else look for | 
|  | 855 | //   operator new. | 
|  | 856 | // 3) The first argument is always size_t. Append the arguments from the | 
|  | 857 | //   placement form. | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 858 |  | 
|  | 859 | llvm::SmallVector<Expr*, 8> AllocArgs(1 + NumPlaceArgs); | 
|  | 860 | // We don't care about the actual value of this argument. | 
|  | 861 | // FIXME: Should the Sema create the expression and embed it in the syntax | 
|  | 862 | // tree? Or should the consumer just recalculate the value? | 
| Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 863 | IntegerLiteral Size(llvm::APInt::getNullValue( | 
|  | 864 | Context.Target.getPointerWidth(0)), | 
|  | 865 | Context.getSizeType(), | 
|  | 866 | SourceLocation()); | 
|  | 867 | AllocArgs[0] = &Size; | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 868 | std::copy(PlaceArgs, PlaceArgs + NumPlaceArgs, AllocArgs.begin() + 1); | 
|  | 869 |  | 
| Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame^] | 870 | // C++ [expr.new]p8: | 
|  | 871 | //   If the allocated type is a non-array type, the allocation | 
|  | 872 | //   function’s name is operator new and the deallocation function’s | 
|  | 873 | //   name is operator delete. If the allocated type is an array | 
|  | 874 | //   type, the allocation function’s name is operator new[] and the | 
|  | 875 | //   deallocation function’s name is operator delete[]. | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 876 | DeclarationName NewName = Context.DeclarationNames.getCXXOperatorName( | 
|  | 877 | IsArray ? OO_Array_New : OO_New); | 
| Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame^] | 878 | DeclarationName DeleteName = Context.DeclarationNames.getCXXOperatorName( | 
|  | 879 | IsArray ? OO_Array_Delete : OO_Delete); | 
|  | 880 |  | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 881 | if (AllocType->isRecordType() && !UseGlobal) { | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 882 | CXXRecordDecl *Record | 
| Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 883 | = cast<CXXRecordDecl>(AllocType->getAs<RecordType>()->getDecl()); | 
| Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 884 | if (FindAllocationOverload(StartLoc, Range, NewName, &AllocArgs[0], | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 885 | AllocArgs.size(), Record, /*AllowMissing=*/true, | 
|  | 886 | OperatorNew)) | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 887 | return true; | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 888 | } | 
|  | 889 | if (!OperatorNew) { | 
|  | 890 | // Didn't find a member overload. Look for a global one. | 
|  | 891 | DeclareGlobalNewDelete(); | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 892 | DeclContext *TUDecl = Context.getTranslationUnitDecl(); | 
| Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 893 | if (FindAllocationOverload(StartLoc, Range, NewName, &AllocArgs[0], | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 894 | AllocArgs.size(), TUDecl, /*AllowMissing=*/false, | 
|  | 895 | OperatorNew)) | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 896 | return true; | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 897 | } | 
|  | 898 |  | 
| Anders Carlsson | d958389 | 2009-05-31 20:26:12 +0000 | [diff] [blame] | 899 | // FindAllocationOverload can change the passed in arguments, so we need to | 
|  | 900 | // copy them back. | 
|  | 901 | if (NumPlaceArgs > 0) | 
|  | 902 | std::copy(&AllocArgs[1], AllocArgs.end(), PlaceArgs); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 903 |  | 
| Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame^] | 904 | // C++ [expr.new]p19: | 
|  | 905 | // | 
|  | 906 | //   If the new-expression begins with a unary :: operator, the | 
|  | 907 | //   deallocation function’s name is looked up in the global | 
|  | 908 | //   scope. Otherwise, if the allocated type is a class type T or an | 
|  | 909 | //   array thereof, the deallocation function’s name is looked up in | 
|  | 910 | //   the scope of T. If this lookup fails to find the name, or if | 
|  | 911 | //   the allocated type is not a class type or array thereof, the | 
|  | 912 | //   deallocation function’s name is looked up in the global scope. | 
|  | 913 | LookupResult FoundDelete(*this, DeleteName, StartLoc, LookupOrdinaryName); | 
|  | 914 | if (AllocType->isRecordType() && !UseGlobal) { | 
|  | 915 | CXXRecordDecl *RD | 
|  | 916 | = cast<CXXRecordDecl>(AllocType->getAs<RecordType>()->getDecl()); | 
|  | 917 | LookupQualifiedName(FoundDelete, RD); | 
|  | 918 | } | 
|  | 919 |  | 
|  | 920 | if (FoundDelete.empty()) { | 
|  | 921 | DeclareGlobalNewDelete(); | 
|  | 922 | LookupQualifiedName(FoundDelete, Context.getTranslationUnitDecl()); | 
|  | 923 | } | 
|  | 924 |  | 
|  | 925 | FoundDelete.suppressDiagnostics(); | 
|  | 926 | llvm::SmallVector<NamedDecl *, 4> Matches; | 
|  | 927 | if (NumPlaceArgs > 1) { | 
|  | 928 | // C++ [expr.new]p20: | 
|  | 929 | //   A declaration of a placement deallocation function matches the | 
|  | 930 | //   declaration of a placement allocation function if it has the | 
|  | 931 | //   same number of parameters and, after parameter transformations | 
|  | 932 | //   (8.3.5), all parameter types except the first are | 
|  | 933 | //   identical. [...] | 
|  | 934 | // | 
|  | 935 | // To perform this comparison, we compute the function type that | 
|  | 936 | // the deallocation function should have, and use that type both | 
|  | 937 | // for template argument deduction and for comparison purposes. | 
|  | 938 | QualType ExpectedFunctionType; | 
|  | 939 | { | 
|  | 940 | const FunctionProtoType *Proto | 
|  | 941 | = OperatorNew->getType()->getAs<FunctionProtoType>(); | 
|  | 942 | llvm::SmallVector<QualType, 4> ArgTypes; | 
|  | 943 | ArgTypes.push_back(Context.VoidPtrTy); | 
|  | 944 | for (unsigned I = 1, N = Proto->getNumArgs(); I < N; ++I) | 
|  | 945 | ArgTypes.push_back(Proto->getArgType(I)); | 
|  | 946 |  | 
|  | 947 | ExpectedFunctionType | 
|  | 948 | = Context.getFunctionType(Context.VoidTy, ArgTypes.data(), | 
|  | 949 | ArgTypes.size(), | 
|  | 950 | Proto->isVariadic(), | 
|  | 951 | 0, false, false, 0, 0, false, CC_Default); | 
|  | 952 | } | 
|  | 953 |  | 
|  | 954 | for (LookupResult::iterator D = FoundDelete.begin(), | 
|  | 955 | DEnd = FoundDelete.end(); | 
|  | 956 | D != DEnd; ++D) { | 
|  | 957 | FunctionDecl *Fn = 0; | 
|  | 958 | if (FunctionTemplateDecl *FnTmpl | 
|  | 959 | = dyn_cast<FunctionTemplateDecl>((*D)->getUnderlyingDecl())) { | 
|  | 960 | // Perform template argument deduction to try to match the | 
|  | 961 | // expected function type. | 
|  | 962 | TemplateDeductionInfo Info(Context, StartLoc); | 
|  | 963 | if (DeduceTemplateArguments(FnTmpl, 0, ExpectedFunctionType, Fn, Info)) | 
|  | 964 | continue; | 
|  | 965 | } else | 
|  | 966 | Fn = cast<FunctionDecl>((*D)->getUnderlyingDecl()); | 
|  | 967 |  | 
|  | 968 | if (Context.hasSameType(Fn->getType(), ExpectedFunctionType)) | 
|  | 969 | Matches.push_back(Fn); | 
|  | 970 | } | 
|  | 971 | } else { | 
|  | 972 | // C++ [expr.new]p20: | 
|  | 973 | //   [...] Any non-placement deallocation function matches a | 
|  | 974 | //   non-placement allocation function. [...] | 
|  | 975 | for (LookupResult::iterator D = FoundDelete.begin(), | 
|  | 976 | DEnd = FoundDelete.end(); | 
|  | 977 | D != DEnd; ++D) { | 
|  | 978 | if (FunctionDecl *Fn = dyn_cast<FunctionDecl>((*D)->getUnderlyingDecl())) | 
|  | 979 | if (isNonPlacementDeallocationFunction(Fn)) | 
|  | 980 | Matches.push_back(*D); | 
|  | 981 | } | 
|  | 982 | } | 
|  | 983 |  | 
|  | 984 | // C++ [expr.new]p20: | 
|  | 985 | //   [...] If the lookup finds a single matching deallocation | 
|  | 986 | //   function, that function will be called; otherwise, no | 
|  | 987 | //   deallocation function will be called. | 
|  | 988 | if (Matches.size() == 1) { | 
|  | 989 | // FIXME: Drops access, using-declaration info! | 
|  | 990 | OperatorDelete = cast<FunctionDecl>(Matches[0]->getUnderlyingDecl()); | 
|  | 991 |  | 
|  | 992 | // C++0x [expr.new]p20: | 
|  | 993 | //   If the lookup finds the two-parameter form of a usual | 
|  | 994 | //   deallocation function (3.7.4.2) and that function, considered | 
|  | 995 | //   as a placement deallocation function, would have been | 
|  | 996 | //   selected as a match for the allocation function, the program | 
|  | 997 | //   is ill-formed. | 
|  | 998 | if (NumPlaceArgs && getLangOptions().CPlusPlus0x && | 
|  | 999 | isNonPlacementDeallocationFunction(OperatorDelete)) { | 
|  | 1000 | Diag(StartLoc, diag::err_placement_new_non_placement_delete) | 
|  | 1001 | << SourceRange(PlaceArgs[0]->getLocStart(), | 
|  | 1002 | PlaceArgs[NumPlaceArgs - 1]->getLocEnd()); | 
|  | 1003 | Diag(OperatorDelete->getLocation(), diag::note_previous_decl) | 
|  | 1004 | << DeleteName; | 
|  | 1005 | } | 
|  | 1006 | } | 
|  | 1007 |  | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1008 | return false; | 
|  | 1009 | } | 
|  | 1010 |  | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1011 | /// FindAllocationOverload - Find an fitting overload for the allocation | 
|  | 1012 | /// function in the specified scope. | 
| Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 1013 | bool Sema::FindAllocationOverload(SourceLocation StartLoc, SourceRange Range, | 
|  | 1014 | DeclarationName Name, Expr** Args, | 
|  | 1015 | unsigned NumArgs, DeclContext *Ctx, | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1016 | bool AllowMissing, FunctionDecl *&Operator) { | 
| John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1017 | LookupResult R(*this, Name, StartLoc, LookupOrdinaryName); | 
|  | 1018 | LookupQualifiedName(R, Ctx); | 
| John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1019 | if (R.empty()) { | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1020 | if (AllowMissing) | 
|  | 1021 | return false; | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1022 | return Diag(StartLoc, diag::err_ovl_no_viable_function_in_call) | 
| Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 1023 | << Name << Range; | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1024 | } | 
|  | 1025 |  | 
| John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1026 | // FIXME: handle ambiguity | 
|  | 1027 |  | 
| John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1028 | OverloadCandidateSet Candidates(StartLoc); | 
| Douglas Gregor | 5d64e5b | 2009-09-30 00:03:47 +0000 | [diff] [blame] | 1029 | for (LookupResult::iterator Alloc = R.begin(), AllocEnd = R.end(); | 
|  | 1030 | Alloc != AllocEnd; ++Alloc) { | 
| Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1031 | // Even member operator new/delete are implicitly treated as | 
|  | 1032 | // static, so don't use AddMemberCandidate. | 
| Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1033 |  | 
|  | 1034 | if (FunctionTemplateDecl *FnTemplate = | 
|  | 1035 | dyn_cast<FunctionTemplateDecl>((*Alloc)->getUnderlyingDecl())) { | 
|  | 1036 | AddTemplateOverloadCandidate(FnTemplate, Alloc.getAccess(), | 
|  | 1037 | /*ExplicitTemplateArgs=*/0, Args, NumArgs, | 
|  | 1038 | Candidates, | 
|  | 1039 | /*SuppressUserConversions=*/false); | 
| Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 1040 | continue; | 
| Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1041 | } | 
|  | 1042 |  | 
|  | 1043 | FunctionDecl *Fn = cast<FunctionDecl>((*Alloc)->getUnderlyingDecl()); | 
|  | 1044 | AddOverloadCandidate(Fn, Alloc.getAccess(), Args, NumArgs, Candidates, | 
|  | 1045 | /*SuppressUserConversions=*/false); | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1046 | } | 
|  | 1047 |  | 
|  | 1048 | // Do the resolution. | 
|  | 1049 | OverloadCandidateSet::iterator Best; | 
| Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1050 | switch(BestViableFunction(Candidates, StartLoc, Best)) { | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1051 | case OR_Success: { | 
|  | 1052 | // Got one! | 
|  | 1053 | FunctionDecl *FnDecl = Best->Function; | 
|  | 1054 | // The first argument is size_t, and the first parameter must be size_t, | 
|  | 1055 | // too. This is checked on declaration and can be assumed. (It can't be | 
|  | 1056 | // asserted on, though, since invalid decls are left in there.) | 
| Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 1057 | // Whatch out for variadic allocator function. | 
|  | 1058 | unsigned NumArgsInFnDecl = FnDecl->getNumParams(); | 
|  | 1059 | for (unsigned i = 0; (i < NumArgs && i < NumArgsInFnDecl); ++i) { | 
| Anders Carlsson | fc27d26 | 2009-05-31 19:49:47 +0000 | [diff] [blame] | 1060 | if (PerformCopyInitialization(Args[i], | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1061 | FnDecl->getParamDecl(i)->getType(), | 
| Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 1062 | AA_Passing)) | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1063 | return true; | 
|  | 1064 | } | 
|  | 1065 | Operator = FnDecl; | 
|  | 1066 | return false; | 
|  | 1067 | } | 
|  | 1068 |  | 
|  | 1069 | case OR_No_Viable_Function: | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1070 | Diag(StartLoc, diag::err_ovl_no_viable_function_in_call) | 
| Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 1071 | << Name << Range; | 
| John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 1072 | PrintOverloadCandidates(Candidates, OCD_AllCandidates, Args, NumArgs); | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1073 | return true; | 
|  | 1074 |  | 
|  | 1075 | case OR_Ambiguous: | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1076 | Diag(StartLoc, diag::err_ovl_ambiguous_call) | 
| Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 1077 | << Name << Range; | 
| John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 1078 | PrintOverloadCandidates(Candidates, OCD_ViableCandidates, Args, NumArgs); | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1079 | return true; | 
| Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1080 |  | 
|  | 1081 | case OR_Deleted: | 
|  | 1082 | Diag(StartLoc, diag::err_ovl_deleted_call) | 
|  | 1083 | << Best->Function->isDeleted() | 
|  | 1084 | << Name << Range; | 
| John McCall | cbce606 | 2010-01-12 07:18:19 +0000 | [diff] [blame] | 1085 | PrintOverloadCandidates(Candidates, OCD_AllCandidates, Args, NumArgs); | 
| Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1086 | return true; | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1087 | } | 
|  | 1088 | assert(false && "Unreachable, bad result from BestViableFunction"); | 
|  | 1089 | return true; | 
|  | 1090 | } | 
|  | 1091 |  | 
|  | 1092 |  | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1093 | /// DeclareGlobalNewDelete - Declare the global forms of operator new and | 
|  | 1094 | /// delete. These are: | 
|  | 1095 | /// @code | 
|  | 1096 | ///   void* operator new(std::size_t) throw(std::bad_alloc); | 
|  | 1097 | ///   void* operator new[](std::size_t) throw(std::bad_alloc); | 
|  | 1098 | ///   void operator delete(void *) throw(); | 
|  | 1099 | ///   void operator delete[](void *) throw(); | 
|  | 1100 | /// @endcode | 
|  | 1101 | /// Note that the placement and nothrow forms of new are *not* implicitly | 
|  | 1102 | /// declared. Their use requires including \<new\>. | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1103 | void Sema::DeclareGlobalNewDelete() { | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1104 | if (GlobalNewDeleteDeclared) | 
|  | 1105 | return; | 
| Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1106 |  | 
|  | 1107 | // C++ [basic.std.dynamic]p2: | 
|  | 1108 | //   [...] The following allocation and deallocation functions (18.4) are | 
|  | 1109 | //   implicitly declared in global scope in each translation unit of a | 
|  | 1110 | //   program | 
|  | 1111 | // | 
|  | 1112 | //     void* operator new(std::size_t) throw(std::bad_alloc); | 
|  | 1113 | //     void* operator new[](std::size_t) throw(std::bad_alloc); | 
|  | 1114 | //     void  operator delete(void*) throw(); | 
|  | 1115 | //     void  operator delete[](void*) throw(); | 
|  | 1116 | // | 
|  | 1117 | //   These implicit declarations introduce only the function names operator | 
|  | 1118 | //   new, operator new[], operator delete, operator delete[]. | 
|  | 1119 | // | 
|  | 1120 | // Here, we need to refer to std::bad_alloc, so we will implicitly declare | 
|  | 1121 | // "std" or "bad_alloc" as necessary to form the exception specification. | 
|  | 1122 | // However, we do not make these implicit declarations visible to name | 
|  | 1123 | // lookup. | 
|  | 1124 | if (!StdNamespace) { | 
|  | 1125 | // The "std" namespace has not yet been defined, so build one implicitly. | 
|  | 1126 | StdNamespace = NamespaceDecl::Create(Context, | 
|  | 1127 | Context.getTranslationUnitDecl(), | 
|  | 1128 | SourceLocation(), | 
|  | 1129 | &PP.getIdentifierTable().get("std")); | 
|  | 1130 | StdNamespace->setImplicit(true); | 
|  | 1131 | } | 
|  | 1132 |  | 
|  | 1133 | if (!StdBadAlloc) { | 
|  | 1134 | // The "std::bad_alloc" class has not yet been declared, so build it | 
|  | 1135 | // implicitly. | 
|  | 1136 | StdBadAlloc = CXXRecordDecl::Create(Context, TagDecl::TK_class, | 
|  | 1137 | StdNamespace, | 
|  | 1138 | SourceLocation(), | 
|  | 1139 | &PP.getIdentifierTable().get("bad_alloc"), | 
|  | 1140 | SourceLocation(), 0); | 
|  | 1141 | StdBadAlloc->setImplicit(true); | 
|  | 1142 | } | 
|  | 1143 |  | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1144 | GlobalNewDeleteDeclared = true; | 
|  | 1145 |  | 
|  | 1146 | QualType VoidPtr = Context.getPointerType(Context.VoidTy); | 
|  | 1147 | QualType SizeT = Context.getSizeType(); | 
| Nuno Lopes | fc28448 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 1148 | bool AssumeSaneOperatorNew = getLangOptions().AssumeSaneOperatorNew; | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1149 |  | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1150 | DeclareGlobalAllocationFunction( | 
|  | 1151 | Context.DeclarationNames.getCXXOperatorName(OO_New), | 
| Nuno Lopes | fc28448 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 1152 | VoidPtr, SizeT, AssumeSaneOperatorNew); | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1153 | DeclareGlobalAllocationFunction( | 
|  | 1154 | Context.DeclarationNames.getCXXOperatorName(OO_Array_New), | 
| Nuno Lopes | fc28448 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 1155 | VoidPtr, SizeT, AssumeSaneOperatorNew); | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1156 | DeclareGlobalAllocationFunction( | 
|  | 1157 | Context.DeclarationNames.getCXXOperatorName(OO_Delete), | 
|  | 1158 | Context.VoidTy, VoidPtr); | 
|  | 1159 | DeclareGlobalAllocationFunction( | 
|  | 1160 | Context.DeclarationNames.getCXXOperatorName(OO_Array_Delete), | 
|  | 1161 | Context.VoidTy, VoidPtr); | 
|  | 1162 | } | 
|  | 1163 |  | 
|  | 1164 | /// DeclareGlobalAllocationFunction - Declares a single implicit global | 
|  | 1165 | /// allocation function if it doesn't already exist. | 
|  | 1166 | void Sema::DeclareGlobalAllocationFunction(DeclarationName Name, | 
| Nuno Lopes | fc28448 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 1167 | QualType Return, QualType Argument, | 
|  | 1168 | bool AddMallocAttr) { | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1169 | DeclContext *GlobalCtx = Context.getTranslationUnitDecl(); | 
|  | 1170 |  | 
|  | 1171 | // Check if this function is already declared. | 
| Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1172 | { | 
| Douglas Gregor | 5cc3709 | 2008-12-23 22:05:29 +0000 | [diff] [blame] | 1173 | DeclContext::lookup_iterator Alloc, AllocEnd; | 
| Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1174 | for (llvm::tie(Alloc, AllocEnd) = GlobalCtx->lookup(Name); | 
| Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1175 | Alloc != AllocEnd; ++Alloc) { | 
| Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1176 | // Only look at non-template functions, as it is the predefined, | 
|  | 1177 | // non-templated allocation function we are trying to declare here. | 
|  | 1178 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(*Alloc)) { | 
|  | 1179 | QualType InitialParamType = | 
| Douglas Gregor | 6e790ab | 2009-12-22 23:42:49 +0000 | [diff] [blame] | 1180 | Context.getCanonicalType( | 
| Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1181 | Func->getParamDecl(0)->getType().getUnqualifiedType()); | 
|  | 1182 | // FIXME: Do we need to check for default arguments here? | 
|  | 1183 | if (Func->getNumParams() == 1 && InitialParamType == Argument) | 
|  | 1184 | return; | 
|  | 1185 | } | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1186 | } | 
|  | 1187 | } | 
|  | 1188 |  | 
| Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1189 | QualType BadAllocType; | 
|  | 1190 | bool HasBadAllocExceptionSpec | 
|  | 1191 | = (Name.getCXXOverloadedOperator() == OO_New || | 
|  | 1192 | Name.getCXXOverloadedOperator() == OO_Array_New); | 
|  | 1193 | if (HasBadAllocExceptionSpec) { | 
|  | 1194 | assert(StdBadAlloc && "Must have std::bad_alloc declared"); | 
|  | 1195 | BadAllocType = Context.getTypeDeclType(StdBadAlloc); | 
|  | 1196 | } | 
|  | 1197 |  | 
|  | 1198 | QualType FnType = Context.getFunctionType(Return, &Argument, 1, false, 0, | 
|  | 1199 | true, false, | 
|  | 1200 | HasBadAllocExceptionSpec? 1 : 0, | 
| Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 1201 | &BadAllocType, false, CC_Default); | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1202 | FunctionDecl *Alloc = | 
|  | 1203 | FunctionDecl::Create(Context, GlobalCtx, SourceLocation(), Name, | 
| John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1204 | FnType, /*TInfo=*/0, FunctionDecl::None, false, true); | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1205 | Alloc->setImplicit(); | 
| Nuno Lopes | fc28448 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 1206 |  | 
|  | 1207 | if (AddMallocAttr) | 
|  | 1208 | Alloc->addAttr(::new (Context) MallocAttr()); | 
|  | 1209 |  | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1210 | ParmVarDecl *Param = ParmVarDecl::Create(Context, Alloc, SourceLocation(), | 
| John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1211 | 0, Argument, /*TInfo=*/0, | 
| Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 1212 | VarDecl::None, 0); | 
| Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1213 | Alloc->setParams(&Param, 1); | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1214 |  | 
| Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1215 | // FIXME: Also add this declaration to the IdentifierResolver, but | 
|  | 1216 | // make sure it is at the end of the chain to coincide with the | 
|  | 1217 | // global scope. | 
| Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1218 | ((DeclContext *)TUScope->getEntity())->addDecl(Alloc); | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1219 | } | 
|  | 1220 |  | 
| Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1221 | bool Sema::FindDeallocationFunction(SourceLocation StartLoc, CXXRecordDecl *RD, | 
|  | 1222 | DeclarationName Name, | 
| Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 1223 | FunctionDecl* &Operator) { | 
| John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1224 | LookupResult Found(*this, Name, StartLoc, LookupOrdinaryName); | 
| Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1225 | // Try to find operator delete/operator delete[] in class scope. | 
| John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1226 | LookupQualifiedName(Found, RD); | 
| Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1227 |  | 
| John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1228 | if (Found.isAmbiguous()) | 
| Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1229 | return true; | 
| Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1230 |  | 
|  | 1231 | for (LookupResult::iterator F = Found.begin(), FEnd = Found.end(); | 
|  | 1232 | F != FEnd; ++F) { | 
|  | 1233 | if (CXXMethodDecl *Delete = dyn_cast<CXXMethodDecl>(*F)) | 
|  | 1234 | if (Delete->isUsualDeallocationFunction()) { | 
|  | 1235 | Operator = Delete; | 
|  | 1236 | return false; | 
|  | 1237 | } | 
|  | 1238 | } | 
|  | 1239 |  | 
|  | 1240 | // We did find operator delete/operator delete[] declarations, but | 
|  | 1241 | // none of them were suitable. | 
|  | 1242 | if (!Found.empty()) { | 
|  | 1243 | Diag(StartLoc, diag::err_no_suitable_delete_member_function_found) | 
|  | 1244 | << Name << RD; | 
|  | 1245 |  | 
|  | 1246 | for (LookupResult::iterator F = Found.begin(), FEnd = Found.end(); | 
|  | 1247 | F != FEnd; ++F) { | 
|  | 1248 | Diag((*F)->getLocation(), | 
|  | 1249 | diag::note_delete_member_function_declared_here) | 
|  | 1250 | << Name; | 
|  | 1251 | } | 
|  | 1252 |  | 
|  | 1253 | return true; | 
|  | 1254 | } | 
|  | 1255 |  | 
|  | 1256 | // Look for a global declaration. | 
|  | 1257 | DeclareGlobalNewDelete(); | 
|  | 1258 | DeclContext *TUDecl = Context.getTranslationUnitDecl(); | 
|  | 1259 |  | 
|  | 1260 | CXXNullPtrLiteralExpr Null(Context.VoidPtrTy, SourceLocation()); | 
|  | 1261 | Expr* DeallocArgs[1]; | 
|  | 1262 | DeallocArgs[0] = &Null; | 
|  | 1263 | if (FindAllocationOverload(StartLoc, SourceRange(), Name, | 
|  | 1264 | DeallocArgs, 1, TUDecl, /*AllowMissing=*/false, | 
|  | 1265 | Operator)) | 
|  | 1266 | return true; | 
|  | 1267 |  | 
|  | 1268 | assert(Operator && "Did not find a deallocation function!"); | 
|  | 1269 | return false; | 
|  | 1270 | } | 
|  | 1271 |  | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1272 | /// ActOnCXXDelete - Parsed a C++ 'delete' expression (C++ 5.3.5), as in: | 
|  | 1273 | /// @code ::delete ptr; @endcode | 
|  | 1274 | /// or | 
|  | 1275 | /// @code delete [] ptr; @endcode | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1276 | Action::OwningExprResult | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1277 | Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal, | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1278 | bool ArrayForm, ExprArg Operand) { | 
| Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1279 | // C++ [expr.delete]p1: | 
|  | 1280 | //   The operand shall have a pointer type, or a class type having a single | 
|  | 1281 | //   conversion function to a pointer type. The result has type void. | 
|  | 1282 | // | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1283 | // DR599 amends "pointer type" to "pointer to object type" in both cases. | 
|  | 1284 |  | 
| Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1285 | FunctionDecl *OperatorDelete = 0; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1286 |  | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1287 | Expr *Ex = (Expr *)Operand.get(); | 
| Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1288 | if (!Ex->isTypeDependent()) { | 
|  | 1289 | QualType Type = Ex->getType(); | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1290 |  | 
| Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1291 | if (const RecordType *Record = Type->getAs<RecordType>()) { | 
| Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1292 | llvm::SmallVector<CXXConversionDecl *, 4> ObjectPtrConversions; | 
| Fariborz Jahanian | 5346278 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 1293 | CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl()); | 
| John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 1294 | const UnresolvedSetImpl *Conversions = RD->getVisibleConversionFunctions(); | 
| Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1295 |  | 
| John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 1296 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), | 
| John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1297 | E = Conversions->end(); I != E; ++I) { | 
| Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1298 | // Skip over templated conversion functions; they aren't considered. | 
| John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1299 | if (isa<FunctionTemplateDecl>(*I)) | 
| Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1300 | continue; | 
|  | 1301 |  | 
| John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1302 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(*I); | 
| Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1303 |  | 
|  | 1304 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); | 
|  | 1305 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) | 
|  | 1306 | if (ConvPtrType->getPointeeType()->isObjectType()) | 
| Fariborz Jahanian | 8b915e7 | 2009-09-15 22:15:23 +0000 | [diff] [blame] | 1307 | ObjectPtrConversions.push_back(Conv); | 
| Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1308 | } | 
| Fariborz Jahanian | 8b915e7 | 2009-09-15 22:15:23 +0000 | [diff] [blame] | 1309 | if (ObjectPtrConversions.size() == 1) { | 
|  | 1310 | // We have a single conversion to a pointer-to-object type. Perform | 
|  | 1311 | // that conversion. | 
|  | 1312 | Operand.release(); | 
|  | 1313 | if (!PerformImplicitConversion(Ex, | 
|  | 1314 | ObjectPtrConversions.front()->getConversionType(), | 
| Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 1315 | AA_Converting)) { | 
| Fariborz Jahanian | 8b915e7 | 2009-09-15 22:15:23 +0000 | [diff] [blame] | 1316 | Operand = Owned(Ex); | 
|  | 1317 | Type = Ex->getType(); | 
|  | 1318 | } | 
|  | 1319 | } | 
|  | 1320 | else if (ObjectPtrConversions.size() > 1) { | 
|  | 1321 | Diag(StartLoc, diag::err_ambiguous_delete_operand) | 
|  | 1322 | << Type << Ex->getSourceRange(); | 
|  | 1323 | for (unsigned i= 0; i < ObjectPtrConversions.size(); i++) { | 
|  | 1324 | CXXConversionDecl *Conv = ObjectPtrConversions[i]; | 
| John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 1325 | NoteOverloadCandidate(Conv); | 
| Fariborz Jahanian | 8b915e7 | 2009-09-15 22:15:23 +0000 | [diff] [blame] | 1326 | } | 
|  | 1327 | return ExprError(); | 
| Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1328 | } | 
| Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1329 | } | 
|  | 1330 |  | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1331 | if (!Type->isPointerType()) | 
|  | 1332 | return ExprError(Diag(StartLoc, diag::err_delete_operand) | 
|  | 1333 | << Type << Ex->getSourceRange()); | 
| Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1334 |  | 
| Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1335 | QualType Pointee = Type->getAs<PointerType>()->getPointeeType(); | 
| Douglas Gregor | 8dcb29d | 2009-03-24 20:13:58 +0000 | [diff] [blame] | 1336 | if (Pointee->isFunctionType() || Pointee->isVoidType()) | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1337 | return ExprError(Diag(StartLoc, diag::err_delete_operand) | 
|  | 1338 | << Type << Ex->getSourceRange()); | 
| Douglas Gregor | 8dcb29d | 2009-03-24 20:13:58 +0000 | [diff] [blame] | 1339 | else if (!Pointee->isDependentType() && | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1340 | RequireCompleteType(StartLoc, Pointee, | 
| Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 1341 | PDiag(diag::warn_delete_incomplete) | 
|  | 1342 | << Ex->getSourceRange())) | 
| Douglas Gregor | 8dcb29d | 2009-03-24 20:13:58 +0000 | [diff] [blame] | 1343 | return ExprError(); | 
| Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1344 |  | 
| Douglas Gregor | 1070c9f | 2009-09-29 21:38:53 +0000 | [diff] [blame] | 1345 | // C++ [expr.delete]p2: | 
|  | 1346 | //   [Note: a pointer to a const type can be the operand of a | 
|  | 1347 | //   delete-expression; it is not necessary to cast away the constness | 
|  | 1348 | //   (5.2.11) of the pointer expression before it is used as the operand | 
|  | 1349 | //   of the delete-expression. ] | 
|  | 1350 | ImpCastExprToType(Ex, Context.getPointerType(Context.VoidTy), | 
|  | 1351 | CastExpr::CK_NoOp); | 
|  | 1352 |  | 
|  | 1353 | // Update the operand. | 
|  | 1354 | Operand.take(); | 
|  | 1355 | Operand = ExprArg(*this, Ex); | 
|  | 1356 |  | 
| Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1357 | DeclarationName DeleteName = Context.DeclarationNames.getCXXOperatorName( | 
|  | 1358 | ArrayForm ? OO_Array_Delete : OO_Delete); | 
|  | 1359 |  | 
| Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1360 | if (const RecordType *RT = Pointee->getAs<RecordType>()) { | 
|  | 1361 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); | 
|  | 1362 |  | 
|  | 1363 | if (!UseGlobal && | 
|  | 1364 | FindDeallocationFunction(StartLoc, RD, DeleteName, OperatorDelete)) | 
| Anders Carlsson | 0ba63ea | 2009-11-14 03:17:38 +0000 | [diff] [blame] | 1365 | return ExprError(); | 
| Anders Carlsson | 0ba63ea | 2009-11-14 03:17:38 +0000 | [diff] [blame] | 1366 |  | 
| Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1367 | if (!RD->hasTrivialDestructor()) | 
|  | 1368 | if (const CXXDestructorDecl *Dtor = RD->getDestructor(Context)) | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1369 | MarkDeclarationReferenced(StartLoc, | 
| Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 1370 | const_cast<CXXDestructorDecl*>(Dtor)); | 
| Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1371 | } | 
| Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1372 |  | 
| Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1373 | if (!OperatorDelete) { | 
| Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1374 | // Look for a global declaration. | 
| Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1375 | DeclareGlobalNewDelete(); | 
|  | 1376 | DeclContext *TUDecl = Context.getTranslationUnitDecl(); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1377 | if (FindAllocationOverload(StartLoc, SourceRange(), DeleteName, | 
| Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 1378 | &Ex, 1, TUDecl, /*AllowMissing=*/false, | 
| Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1379 | OperatorDelete)) | 
|  | 1380 | return ExprError(); | 
|  | 1381 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1382 |  | 
| Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1383 | // FIXME: Check access and ambiguity of operator delete and destructor. | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1384 | } | 
|  | 1385 |  | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1386 | Operand.release(); | 
|  | 1387 | return Owned(new (Context) CXXDeleteExpr(Context.VoidTy, UseGlobal, ArrayForm, | 
| Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1388 | OperatorDelete, Ex, StartLoc)); | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1389 | } | 
|  | 1390 |  | 
| Douglas Gregor | 8cfe5a7 | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 1391 | /// \brief Check the use of the given variable as a C++ condition in an if, | 
|  | 1392 | /// while, do-while, or switch statement. | 
|  | 1393 | Action::OwningExprResult Sema::CheckConditionVariable(VarDecl *ConditionVar) { | 
|  | 1394 | QualType T = ConditionVar->getType(); | 
|  | 1395 |  | 
|  | 1396 | // C++ [stmt.select]p2: | 
|  | 1397 | //   The declarator shall not specify a function or an array. | 
|  | 1398 | if (T->isFunctionType()) | 
|  | 1399 | return ExprError(Diag(ConditionVar->getLocation(), | 
|  | 1400 | diag::err_invalid_use_of_function_type) | 
|  | 1401 | << ConditionVar->getSourceRange()); | 
|  | 1402 | else if (T->isArrayType()) | 
|  | 1403 | return ExprError(Diag(ConditionVar->getLocation(), | 
|  | 1404 | diag::err_invalid_use_of_array_type) | 
|  | 1405 | << ConditionVar->getSourceRange()); | 
| Douglas Gregor | a7605db | 2009-11-24 16:07:02 +0000 | [diff] [blame] | 1406 |  | 
| Douglas Gregor | 8cfe5a7 | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 1407 | return Owned(DeclRefExpr::Create(Context, 0, SourceRange(), ConditionVar, | 
|  | 1408 | ConditionVar->getLocation(), | 
|  | 1409 | ConditionVar->getType().getNonReferenceType())); | 
|  | 1410 | } | 
|  | 1411 |  | 
| Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 1412 | /// CheckCXXBooleanCondition - Returns true if a conversion to bool is invalid. | 
|  | 1413 | bool Sema::CheckCXXBooleanCondition(Expr *&CondExpr) { | 
|  | 1414 | // C++ 6.4p4: | 
|  | 1415 | // The value of a condition that is an initialized declaration in a statement | 
|  | 1416 | // other than a switch statement is the value of the declared variable | 
|  | 1417 | // implicitly converted to type bool. If that conversion is ill-formed, the | 
|  | 1418 | // program is ill-formed. | 
|  | 1419 | // The value of a condition that is an expression is the value of the | 
|  | 1420 | // expression, implicitly converted to bool. | 
|  | 1421 | // | 
| Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1422 | return PerformContextuallyConvertToBool(CondExpr); | 
| Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 1423 | } | 
| Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1424 |  | 
|  | 1425 | /// Helper function to determine whether this is the (deprecated) C++ | 
|  | 1426 | /// conversion from a string literal to a pointer to non-const char or | 
|  | 1427 | /// non-const wchar_t (for narrow and wide string literals, | 
|  | 1428 | /// respectively). | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1429 | bool | 
| Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1430 | Sema::IsStringLiteralToNonConstPointerConversion(Expr *From, QualType ToType) { | 
|  | 1431 | // Look inside the implicit cast, if it exists. | 
|  | 1432 | if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(From)) | 
|  | 1433 | From = Cast->getSubExpr(); | 
|  | 1434 |  | 
|  | 1435 | // A string literal (2.13.4) that is not a wide string literal can | 
|  | 1436 | // be converted to an rvalue of type "pointer to char"; a wide | 
|  | 1437 | // string literal can be converted to an rvalue of type "pointer | 
|  | 1438 | // to wchar_t" (C++ 4.2p2). | 
|  | 1439 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(From)) | 
| Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1440 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1441 | if (const BuiltinType *ToPointeeType | 
| John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1442 | = ToPtrType->getPointeeType()->getAs<BuiltinType>()) { | 
| Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1443 | // This conversion is considered only when there is an | 
|  | 1444 | // explicit appropriate pointer target type (C++ 4.2p2). | 
| John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1445 | if (!ToPtrType->getPointeeType().hasQualifiers() && | 
| Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1446 | ((StrLit->isWide() && ToPointeeType->isWideCharType()) || | 
|  | 1447 | (!StrLit->isWide() && | 
|  | 1448 | (ToPointeeType->getKind() == BuiltinType::Char_U || | 
|  | 1449 | ToPointeeType->getKind() == BuiltinType::Char_S)))) | 
|  | 1450 | return true; | 
|  | 1451 | } | 
|  | 1452 |  | 
|  | 1453 | return false; | 
|  | 1454 | } | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1455 |  | 
|  | 1456 | /// PerformImplicitConversion - Perform an implicit conversion of the | 
|  | 1457 | /// expression From to the type ToType. Returns true if there was an | 
|  | 1458 | /// error, false otherwise. The expression From is replaced with the | 
| Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1459 | /// converted expression. Flavor is the kind of conversion we're | 
| Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1460 | /// performing, used in the error message. If @p AllowExplicit, | 
| Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1461 | /// explicit user-defined conversions are permitted. @p Elidable should be true | 
|  | 1462 | /// when called for copies which may be elided (C++ 12.8p15). C++0x overload | 
|  | 1463 | /// resolution works differently in that case. | 
|  | 1464 | bool | 
| Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1465 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType, | 
| Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 1466 | AssignmentAction Action, bool AllowExplicit, | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1467 | bool Elidable) { | 
| Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1468 | ImplicitConversionSequence ICS; | 
| Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 1469 | return PerformImplicitConversion(From, ToType, Action, AllowExplicit, | 
| Fariborz Jahanian | 51bebc8 | 2009-09-23 20:55:32 +0000 | [diff] [blame] | 1470 | Elidable, ICS); | 
|  | 1471 | } | 
|  | 1472 |  | 
|  | 1473 | bool | 
|  | 1474 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType, | 
| Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 1475 | AssignmentAction Action, bool AllowExplicit, | 
| Fariborz Jahanian | 51bebc8 | 2009-09-23 20:55:32 +0000 | [diff] [blame] | 1476 | bool Elidable, | 
|  | 1477 | ImplicitConversionSequence& ICS) { | 
| John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 1478 | ICS.setBad(BadConversionSequence::no_conversion, From, ToType); | 
| Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1479 | if (Elidable && getLangOptions().CPlusPlus0x) { | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1480 | ICS = TryImplicitConversion(From, ToType, | 
| Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 1481 | /*SuppressUserConversions=*/false, | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1482 | AllowExplicit, | 
| Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 1483 | /*ForceRValue=*/true, | 
|  | 1484 | /*InOverloadResolution=*/false); | 
| Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1485 | } | 
| John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1486 | if (ICS.isBad()) { | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1487 | ICS = TryImplicitConversion(From, ToType, | 
| Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 1488 | /*SuppressUserConversions=*/false, | 
|  | 1489 | AllowExplicit, | 
| Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 1490 | /*ForceRValue=*/false, | 
|  | 1491 | /*InOverloadResolution=*/false); | 
| Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1492 | } | 
| Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 1493 | return PerformImplicitConversion(From, ToType, ICS, Action); | 
| Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1494 | } | 
|  | 1495 |  | 
|  | 1496 | /// PerformImplicitConversion - Perform an implicit conversion of the | 
|  | 1497 | /// expression From to the type ToType using the pre-computed implicit | 
|  | 1498 | /// conversion sequence ICS. Returns true if there was an error, false | 
|  | 1499 | /// otherwise. The expression From is replaced with the converted | 
| Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 1500 | /// expression. Action is the kind of conversion we're performing, | 
| Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1501 | /// used in the error message. | 
|  | 1502 | bool | 
|  | 1503 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType, | 
|  | 1504 | const ImplicitConversionSequence &ICS, | 
| Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 1505 | AssignmentAction Action, bool IgnoreBaseAccess) { | 
| John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1506 | switch (ICS.getKind()) { | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1507 | case ImplicitConversionSequence::StandardConversion: | 
| Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 1508 | if (PerformImplicitConversion(From, ToType, ICS.Standard, Action, | 
| Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1509 | IgnoreBaseAccess)) | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1510 | return true; | 
|  | 1511 | break; | 
|  | 1512 |  | 
| Anders Carlsson | f6c213a | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 1513 | case ImplicitConversionSequence::UserDefinedConversion: { | 
|  | 1514 |  | 
| Fariborz Jahanian | 7fe5d72 | 2009-08-28 22:04:50 +0000 | [diff] [blame] | 1515 | FunctionDecl *FD = ICS.UserDefined.ConversionFunction; | 
|  | 1516 | CastExpr::CastKind CastKind = CastExpr::CK_Unknown; | 
| Anders Carlsson | f6c213a | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 1517 | QualType BeforeToType; | 
|  | 1518 | if (const CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(FD)) { | 
| Fariborz Jahanian | 7fe5d72 | 2009-08-28 22:04:50 +0000 | [diff] [blame] | 1519 | CastKind = CastExpr::CK_UserDefinedConversion; | 
| Anders Carlsson | f6c213a | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 1520 |  | 
|  | 1521 | // If the user-defined conversion is specified by a conversion function, | 
|  | 1522 | // the initial standard conversion sequence converts the source type to | 
|  | 1523 | // the implicit object parameter of the conversion function. | 
|  | 1524 | BeforeToType = Context.getTagDeclType(Conv->getParent()); | 
|  | 1525 | } else if (const CXXConstructorDecl *Ctor = | 
|  | 1526 | dyn_cast<CXXConstructorDecl>(FD)) { | 
| Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 1527 | CastKind = CastExpr::CK_ConstructorConversion; | 
| Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1528 | // Do no conversion if dealing with ... for the first conversion. | 
| Douglas Gregor | e44201a | 2009-11-20 02:31:03 +0000 | [diff] [blame] | 1529 | if (!ICS.UserDefined.EllipsisConversion) { | 
| Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1530 | // If the user-defined conversion is specified by a constructor, the | 
|  | 1531 | // initial standard conversion sequence converts the source type to the | 
|  | 1532 | // type required by the argument of the constructor | 
| Douglas Gregor | e44201a | 2009-11-20 02:31:03 +0000 | [diff] [blame] | 1533 | BeforeToType = Ctor->getParamDecl(0)->getType().getNonReferenceType(); | 
|  | 1534 | } | 
| Anders Carlsson | f6c213a | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 1535 | } | 
| Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 1536 | else | 
|  | 1537 | assert(0 && "Unknown conversion function kind!"); | 
| Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1538 | // Whatch out for elipsis conversion. | 
| Fariborz Jahanian | 4c0cea2 | 2009-11-06 00:55:14 +0000 | [diff] [blame] | 1539 | if (!ICS.UserDefined.EllipsisConversion) { | 
| Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1540 | if (PerformImplicitConversion(From, BeforeToType, | 
| Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 1541 | ICS.UserDefined.Before, AA_Converting, | 
| Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1542 | IgnoreBaseAccess)) | 
| Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1543 | return true; | 
|  | 1544 | } | 
| Anders Carlsson | f6c213a | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 1545 |  | 
| Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 1546 | OwningExprResult CastArg | 
|  | 1547 | = BuildCXXCastArgument(From->getLocStart(), | 
|  | 1548 | ToType.getNonReferenceType(), | 
|  | 1549 | CastKind, cast<CXXMethodDecl>(FD), | 
|  | 1550 | Owned(From)); | 
|  | 1551 |  | 
|  | 1552 | if (CastArg.isInvalid()) | 
|  | 1553 | return true; | 
| Eli Friedman | d888962 | 2009-11-27 04:41:50 +0000 | [diff] [blame] | 1554 |  | 
|  | 1555 | From = CastArg.takeAs<Expr>(); | 
|  | 1556 |  | 
| Eli Friedman | d888962 | 2009-11-27 04:41:50 +0000 | [diff] [blame] | 1557 | return PerformImplicitConversion(From, ToType, ICS.UserDefined.After, | 
| Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 1558 | AA_Converting, IgnoreBaseAccess); | 
| Fariborz Jahanian | 93034ca | 2009-10-16 19:20:59 +0000 | [diff] [blame] | 1559 | } | 
| John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1560 |  | 
|  | 1561 | case ImplicitConversionSequence::AmbiguousConversion: | 
|  | 1562 | DiagnoseAmbiguousConversion(ICS, From->getExprLoc(), | 
|  | 1563 | PDiag(diag::err_typecheck_ambiguous_condition) | 
|  | 1564 | << From->getSourceRange()); | 
|  | 1565 | return true; | 
| Fariborz Jahanian | 93034ca | 2009-10-16 19:20:59 +0000 | [diff] [blame] | 1566 |  | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1567 | case ImplicitConversionSequence::EllipsisConversion: | 
|  | 1568 | assert(false && "Cannot perform an ellipsis conversion"); | 
| Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1569 | return false; | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1570 |  | 
|  | 1571 | case ImplicitConversionSequence::BadConversion: | 
|  | 1572 | return true; | 
|  | 1573 | } | 
|  | 1574 |  | 
|  | 1575 | // Everything went well. | 
|  | 1576 | return false; | 
|  | 1577 | } | 
|  | 1578 |  | 
|  | 1579 | /// PerformImplicitConversion - Perform an implicit conversion of the | 
|  | 1580 | /// expression From to the type ToType by following the standard | 
|  | 1581 | /// conversion sequence SCS. Returns true if there was an error, false | 
|  | 1582 | /// otherwise. The expression From is replaced with the converted | 
| Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1583 | /// expression. Flavor is the context in which we're performing this | 
|  | 1584 | /// conversion, for use in error messages. | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1585 | bool | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1586 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType, | 
| Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1587 | const StandardConversionSequence& SCS, | 
| Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 1588 | AssignmentAction Action, bool IgnoreBaseAccess) { | 
| Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1589 | // Overall FIXME: we are recomputing too many types here and doing far too | 
|  | 1590 | // much extra work. What this means is that we need to keep track of more | 
|  | 1591 | // information that is computed when we try the implicit conversion initially, | 
|  | 1592 | // so that we don't need to recompute anything here. | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1593 | QualType FromType = From->getType(); | 
|  | 1594 |  | 
| Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1595 | if (SCS.CopyConstructor) { | 
| Anders Carlsson | 7c3e8a1 | 2009-05-19 04:45:15 +0000 | [diff] [blame] | 1596 | // FIXME: When can ToType be a reference type? | 
|  | 1597 | assert(!ToType->isReferenceType()); | 
| Fariborz Jahanian | b3c4774 | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 1598 | if (SCS.Second == ICK_Derived_To_Base) { | 
|  | 1599 | ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this); | 
|  | 1600 | if (CompleteConstructorCall(cast<CXXConstructorDecl>(SCS.CopyConstructor), | 
|  | 1601 | MultiExprArg(*this, (void **)&From, 1), | 
|  | 1602 | /*FIXME:ConstructLoc*/SourceLocation(), | 
|  | 1603 | ConstructorArgs)) | 
|  | 1604 | return true; | 
|  | 1605 | OwningExprResult FromResult = | 
|  | 1606 | BuildCXXConstructExpr(/*FIXME:ConstructLoc*/SourceLocation(), | 
|  | 1607 | ToType, SCS.CopyConstructor, | 
|  | 1608 | move_arg(ConstructorArgs)); | 
|  | 1609 | if (FromResult.isInvalid()) | 
|  | 1610 | return true; | 
|  | 1611 | From = FromResult.takeAs<Expr>(); | 
|  | 1612 | return false; | 
|  | 1613 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1614 | OwningExprResult FromResult = | 
|  | 1615 | BuildCXXConstructExpr(/*FIXME:ConstructLoc*/SourceLocation(), | 
|  | 1616 | ToType, SCS.CopyConstructor, | 
| Anders Carlsson | f47511a | 2009-09-07 22:23:31 +0000 | [diff] [blame] | 1617 | MultiExprArg(*this, (void**)&From, 1)); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1618 |  | 
| Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 1619 | if (FromResult.isInvalid()) | 
|  | 1620 | return true; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1621 |  | 
| Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 1622 | From = FromResult.takeAs<Expr>(); | 
| Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1623 | return false; | 
|  | 1624 | } | 
|  | 1625 |  | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1626 | // Perform the first implicit conversion. | 
|  | 1627 | switch (SCS.First) { | 
|  | 1628 | case ICK_Identity: | 
|  | 1629 | case ICK_Lvalue_To_Rvalue: | 
|  | 1630 | // Nothing to do. | 
|  | 1631 | break; | 
|  | 1632 |  | 
|  | 1633 | case ICK_Array_To_Pointer: | 
| Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1634 | FromType = Context.getArrayDecayedType(FromType); | 
| Anders Carlsson | 8249576 | 2009-08-08 21:04:35 +0000 | [diff] [blame] | 1635 | ImpCastExprToType(From, FromType, CastExpr::CK_ArrayToPointerDecay); | 
| Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1636 | break; | 
|  | 1637 |  | 
|  | 1638 | case ICK_Function_To_Pointer: | 
| Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 1639 | if (Context.getCanonicalType(FromType) == Context.OverloadTy) { | 
| Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 1640 | FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(From, ToType, true); | 
|  | 1641 | if (!Fn) | 
|  | 1642 | return true; | 
|  | 1643 |  | 
| Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1644 | if (DiagnoseUseOfDecl(Fn, From->getSourceRange().getBegin())) | 
|  | 1645 | return true; | 
|  | 1646 |  | 
| Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 1647 | From = FixOverloadedFunctionReference(From, Fn); | 
| Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 1648 | FromType = From->getType(); | 
| Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 1649 |  | 
| Sebastian Redl | 759986e | 2009-10-17 20:50:27 +0000 | [diff] [blame] | 1650 | // If there's already an address-of operator in the expression, we have | 
|  | 1651 | // the right type already, and the code below would just introduce an | 
|  | 1652 | // invalid additional pointer level. | 
| Anders Carlsson | 96ad533 | 2009-10-21 17:16:23 +0000 | [diff] [blame] | 1653 | if (FromType->isPointerType() || FromType->isMemberFunctionPointerType()) | 
| Sebastian Redl | 759986e | 2009-10-17 20:50:27 +0000 | [diff] [blame] | 1654 | break; | 
| Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 1655 | } | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1656 | FromType = Context.getPointerType(FromType); | 
| Anders Carlsson | b633c4e | 2009-09-01 20:37:18 +0000 | [diff] [blame] | 1657 | ImpCastExprToType(From, FromType, CastExpr::CK_FunctionToPointerDecay); | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1658 | break; | 
|  | 1659 |  | 
|  | 1660 | default: | 
|  | 1661 | assert(false && "Improper first standard conversion"); | 
|  | 1662 | break; | 
|  | 1663 | } | 
|  | 1664 |  | 
|  | 1665 | // Perform the second implicit conversion | 
|  | 1666 | switch (SCS.Second) { | 
|  | 1667 | case ICK_Identity: | 
| Sebastian Redl | 2c7588f | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 1668 | // If both sides are functions (or pointers/references to them), there could | 
|  | 1669 | // be incompatible exception declarations. | 
|  | 1670 | if (CheckExceptionSpecCompatibility(From, ToType)) | 
|  | 1671 | return true; | 
|  | 1672 | // Nothing else to do. | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1673 | break; | 
|  | 1674 |  | 
| Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1675 | case ICK_NoReturn_Adjustment: | 
|  | 1676 | // If both sides are functions (or pointers/references to them), there could | 
|  | 1677 | // be incompatible exception declarations. | 
|  | 1678 | if (CheckExceptionSpecCompatibility(From, ToType)) | 
|  | 1679 | return true; | 
|  | 1680 |  | 
|  | 1681 | ImpCastExprToType(From, Context.getNoReturnType(From->getType(), false), | 
|  | 1682 | CastExpr::CK_NoOp); | 
|  | 1683 | break; | 
|  | 1684 |  | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1685 | case ICK_Integral_Promotion: | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1686 | case ICK_Integral_Conversion: | 
| Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1687 | ImpCastExprToType(From, ToType, CastExpr::CK_IntegralCast); | 
|  | 1688 | break; | 
|  | 1689 |  | 
|  | 1690 | case ICK_Floating_Promotion: | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1691 | case ICK_Floating_Conversion: | 
| Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1692 | ImpCastExprToType(From, ToType, CastExpr::CK_FloatingCast); | 
|  | 1693 | break; | 
|  | 1694 |  | 
|  | 1695 | case ICK_Complex_Promotion: | 
| Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1696 | case ICK_Complex_Conversion: | 
| Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1697 | ImpCastExprToType(From, ToType, CastExpr::CK_Unknown); | 
|  | 1698 | break; | 
|  | 1699 |  | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1700 | case ICK_Floating_Integral: | 
| Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1701 | if (ToType->isFloatingType()) | 
|  | 1702 | ImpCastExprToType(From, ToType, CastExpr::CK_IntegralToFloating); | 
|  | 1703 | else | 
|  | 1704 | ImpCastExprToType(From, ToType, CastExpr::CK_FloatingToIntegral); | 
|  | 1705 | break; | 
|  | 1706 |  | 
| Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 1707 | case ICK_Complex_Real: | 
| Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1708 | ImpCastExprToType(From, ToType, CastExpr::CK_Unknown); | 
|  | 1709 | break; | 
|  | 1710 |  | 
| Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1711 | case ICK_Compatible_Conversion: | 
| Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1712 | ImpCastExprToType(From, ToType, CastExpr::CK_NoOp); | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1713 | break; | 
|  | 1714 |  | 
| Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1715 | case ICK_Pointer_Conversion: { | 
| Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1716 | if (SCS.IncompatibleObjC) { | 
|  | 1717 | // Diagnose incompatible Objective-C conversions | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1718 | Diag(From->getSourceRange().getBegin(), | 
| Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1719 | diag::ext_typecheck_convert_incompatible_pointer) | 
| Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 1720 | << From->getType() << ToType << Action | 
| Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1721 | << From->getSourceRange(); | 
|  | 1722 | } | 
|  | 1723 |  | 
| Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1724 |  | 
|  | 1725 | CastExpr::CastKind Kind = CastExpr::CK_Unknown; | 
| Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1726 | if (CheckPointerConversion(From, ToType, Kind, IgnoreBaseAccess)) | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1727 | return true; | 
| Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1728 | ImpCastExprToType(From, ToType, Kind); | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1729 | break; | 
| Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1730 | } | 
|  | 1731 |  | 
|  | 1732 | case ICK_Pointer_Member: { | 
|  | 1733 | CastExpr::CastKind Kind = CastExpr::CK_Unknown; | 
| Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1734 | if (CheckMemberPointerConversion(From, ToType, Kind, IgnoreBaseAccess)) | 
| Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1735 | return true; | 
| Sebastian Redl | 2c7588f | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 1736 | if (CheckExceptionSpecCompatibility(From, ToType)) | 
|  | 1737 | return true; | 
| Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1738 | ImpCastExprToType(From, ToType, Kind); | 
|  | 1739 | break; | 
|  | 1740 | } | 
| Anders Carlsson | bc0e078 | 2009-11-23 20:04:44 +0000 | [diff] [blame] | 1741 | case ICK_Boolean_Conversion: { | 
|  | 1742 | CastExpr::CastKind Kind = CastExpr::CK_Unknown; | 
|  | 1743 | if (FromType->isMemberPointerType()) | 
|  | 1744 | Kind = CastExpr::CK_MemberPointerToBoolean; | 
|  | 1745 |  | 
|  | 1746 | ImpCastExprToType(From, Context.BoolTy, Kind); | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1747 | break; | 
| Anders Carlsson | bc0e078 | 2009-11-23 20:04:44 +0000 | [diff] [blame] | 1748 | } | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1749 |  | 
| Douglas Gregor | b7a86f5 | 2009-11-06 01:02:41 +0000 | [diff] [blame] | 1750 | case ICK_Derived_To_Base: | 
|  | 1751 | if (CheckDerivedToBaseConversion(From->getType(), | 
|  | 1752 | ToType.getNonReferenceType(), | 
|  | 1753 | From->getLocStart(), | 
| Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1754 | From->getSourceRange(), | 
|  | 1755 | IgnoreBaseAccess)) | 
| Douglas Gregor | b7a86f5 | 2009-11-06 01:02:41 +0000 | [diff] [blame] | 1756 | return true; | 
|  | 1757 | ImpCastExprToType(From, ToType.getNonReferenceType(), | 
|  | 1758 | CastExpr::CK_DerivedToBase); | 
|  | 1759 | break; | 
|  | 1760 |  | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1761 | default: | 
|  | 1762 | assert(false && "Improper second standard conversion"); | 
|  | 1763 | break; | 
|  | 1764 | } | 
|  | 1765 |  | 
|  | 1766 | switch (SCS.Third) { | 
|  | 1767 | case ICK_Identity: | 
|  | 1768 | // Nothing to do. | 
|  | 1769 | break; | 
|  | 1770 |  | 
|  | 1771 | case ICK_Qualification: | 
| Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1772 | // FIXME: Not sure about lvalue vs rvalue here in the presence of rvalue | 
|  | 1773 | // references. | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1774 | ImpCastExprToType(From, ToType.getNonReferenceType(), | 
| Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1775 | CastExpr::CK_NoOp, | 
| Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1776 | ToType->isLValueReferenceType()); | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1777 | break; | 
| Douglas Gregor | b7a86f5 | 2009-11-06 01:02:41 +0000 | [diff] [blame] | 1778 |  | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1779 | default: | 
|  | 1780 | assert(false && "Improper second standard conversion"); | 
|  | 1781 | break; | 
|  | 1782 | } | 
|  | 1783 |  | 
|  | 1784 | return false; | 
|  | 1785 | } | 
|  | 1786 |  | 
| Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1787 | Sema::OwningExprResult Sema::ActOnUnaryTypeTrait(UnaryTypeTrait OTT, | 
|  | 1788 | SourceLocation KWLoc, | 
|  | 1789 | SourceLocation LParen, | 
|  | 1790 | TypeTy *Ty, | 
|  | 1791 | SourceLocation RParen) { | 
| Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 1792 | QualType T = GetTypeFromParser(Ty); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1793 |  | 
| Anders Carlsson | 3292d5c | 2009-07-07 19:06:02 +0000 | [diff] [blame] | 1794 | // According to http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html | 
|  | 1795 | // all traits except __is_class, __is_enum and __is_union require a the type | 
|  | 1796 | // to be complete. | 
|  | 1797 | if (OTT != UTT_IsClass && OTT != UTT_IsEnum && OTT != UTT_IsUnion) { | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1798 | if (RequireCompleteType(KWLoc, T, | 
| Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 1799 | diag::err_incomplete_type_used_in_type_trait_expr)) | 
| Anders Carlsson | 3292d5c | 2009-07-07 19:06:02 +0000 | [diff] [blame] | 1800 | return ExprError(); | 
|  | 1801 | } | 
| Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1802 |  | 
|  | 1803 | // There is no point in eagerly computing the value. The traits are designed | 
|  | 1804 | // to be used from type trait templates, so Ty will be a template parameter | 
|  | 1805 | // 99% of the time. | 
| Anders Carlsson | 3292d5c | 2009-07-07 19:06:02 +0000 | [diff] [blame] | 1806 | return Owned(new (Context) UnaryTypeTraitExpr(KWLoc, OTT, T, | 
|  | 1807 | RParen, Context.BoolTy)); | 
| Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1808 | } | 
| Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 1809 |  | 
|  | 1810 | QualType Sema::CheckPointerToMemberOperands( | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1811 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isIndirect) { | 
| Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 1812 | const char *OpSpelling = isIndirect ? "->*" : ".*"; | 
|  | 1813 | // C++ 5.5p2 | 
|  | 1814 | //   The binary operator .* [p3: ->*] binds its second operand, which shall | 
|  | 1815 | //   be of type "pointer to member of T" (where T is a completely-defined | 
|  | 1816 | //   class type) [...] | 
|  | 1817 | QualType RType = rex->getType(); | 
| Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1818 | const MemberPointerType *MemPtr = RType->getAs<MemberPointerType>(); | 
| Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1819 | if (!MemPtr) { | 
| Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 1820 | Diag(Loc, diag::err_bad_memptr_rhs) | 
|  | 1821 | << OpSpelling << RType << rex->getSourceRange(); | 
|  | 1822 | return QualType(); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1823 | } | 
| Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1824 |  | 
| Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 1825 | QualType Class(MemPtr->getClass(), 0); | 
|  | 1826 |  | 
|  | 1827 | // C++ 5.5p2 | 
|  | 1828 | //   [...] to its first operand, which shall be of class T or of a class of | 
|  | 1829 | //   which T is an unambiguous and accessible base class. [p3: a pointer to | 
|  | 1830 | //   such a class] | 
|  | 1831 | QualType LType = lex->getType(); | 
|  | 1832 | if (isIndirect) { | 
| Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1833 | if (const PointerType *Ptr = LType->getAs<PointerType>()) | 
| Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 1834 | LType = Ptr->getPointeeType().getNonReferenceType(); | 
|  | 1835 | else { | 
|  | 1836 | Diag(Loc, diag::err_bad_memptr_lhs) | 
| Fariborz Jahanian | ef78ac6 | 2009-10-26 20:45:27 +0000 | [diff] [blame] | 1837 | << OpSpelling << 1 << LType | 
|  | 1838 | << CodeModificationHint::CreateReplacement(SourceRange(Loc), ".*"); | 
| Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 1839 | return QualType(); | 
|  | 1840 | } | 
|  | 1841 | } | 
|  | 1842 |  | 
| Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1843 | if (!Context.hasSameUnqualifiedType(Class, LType)) { | 
| Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 1844 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/false, | 
|  | 1845 | /*DetectVirtual=*/false); | 
| Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1846 | // FIXME: Would it be useful to print full ambiguity paths, or is that | 
|  | 1847 | // overkill? | 
| Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 1848 | if (!IsDerivedFrom(LType, Class, Paths) || | 
|  | 1849 | Paths.isAmbiguous(Context.getCanonicalType(Class))) { | 
|  | 1850 | Diag(Loc, diag::err_bad_memptr_lhs) << OpSpelling | 
| Eli Friedman | 3005efe | 2010-01-16 00:00:48 +0000 | [diff] [blame] | 1851 | << (int)isIndirect << lex->getType(); | 
| Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 1852 | return QualType(); | 
|  | 1853 | } | 
| Eli Friedman | 3005efe | 2010-01-16 00:00:48 +0000 | [diff] [blame] | 1854 | // Cast LHS to type of use. | 
|  | 1855 | QualType UseType = isIndirect ? Context.getPointerType(Class) : Class; | 
|  | 1856 | bool isLValue = !isIndirect && lex->isLvalue(Context) == Expr::LV_Valid; | 
|  | 1857 | ImpCastExprToType(lex, UseType, CastExpr::CK_DerivedToBase, isLValue); | 
| Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 1858 | } | 
|  | 1859 |  | 
| Fariborz Jahanian | 19d7073 | 2009-11-18 22:16:17 +0000 | [diff] [blame] | 1860 | if (isa<CXXZeroInitValueExpr>(rex->IgnoreParens())) { | 
| Fariborz Jahanian | 05ebda9 | 2009-11-18 21:54:48 +0000 | [diff] [blame] | 1861 | // Diagnose use of pointer-to-member type which when used as | 
|  | 1862 | // the functional cast in a pointer-to-member expression. | 
|  | 1863 | Diag(Loc, diag::err_pointer_to_member_type) << isIndirect; | 
|  | 1864 | return QualType(); | 
|  | 1865 | } | 
| Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 1866 | // C++ 5.5p2 | 
|  | 1867 | //   The result is an object or a function of the type specified by the | 
|  | 1868 | //   second operand. | 
|  | 1869 | // The cv qualifiers are the union of those in the pointer and the left side, | 
|  | 1870 | // in accordance with 5.5p5 and 5.2.5. | 
|  | 1871 | // FIXME: This returns a dereferenced member function pointer as a normal | 
|  | 1872 | // function type. However, the only operation valid on such functions is | 
| Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1873 | // calling them. There's also a GCC extension to get a function pointer to the | 
|  | 1874 | // thing, which is another complication, because this type - unlike the type | 
|  | 1875 | // that is the result of this expression - takes the class as the first | 
| Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 1876 | // argument. | 
|  | 1877 | // We probably need a "MemberFunctionClosureType" or something like that. | 
|  | 1878 | QualType Result = MemPtr->getPointeeType(); | 
| John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1879 | Result = Context.getCVRQualifiedType(Result, LType.getCVRQualifiers()); | 
| Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 1880 | return Result; | 
|  | 1881 | } | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1882 |  | 
|  | 1883 | /// \brief Get the target type of a standard or user-defined conversion. | 
|  | 1884 | static QualType TargetType(const ImplicitConversionSequence &ICS) { | 
| John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1885 | switch (ICS.getKind()) { | 
|  | 1886 | case ImplicitConversionSequence::StandardConversion: | 
| Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1887 | return ICS.Standard.getToType(2); | 
| John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1888 | case ImplicitConversionSequence::UserDefinedConversion: | 
| Douglas Gregor | ad323a8 | 2010-01-27 03:51:04 +0000 | [diff] [blame] | 1889 | return ICS.UserDefined.After.getToType(2); | 
| John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1890 | case ImplicitConversionSequence::AmbiguousConversion: | 
|  | 1891 | return ICS.Ambiguous.getToType(); | 
| John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 1892 |  | 
| John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1893 | case ImplicitConversionSequence::EllipsisConversion: | 
|  | 1894 | case ImplicitConversionSequence::BadConversion: | 
|  | 1895 | llvm_unreachable("function not valid for ellipsis or bad conversions"); | 
|  | 1896 | } | 
|  | 1897 | return QualType(); // silence warnings | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1898 | } | 
|  | 1899 |  | 
|  | 1900 | /// \brief Try to convert a type to another according to C++0x 5.16p3. | 
|  | 1901 | /// | 
|  | 1902 | /// This is part of the parameter validation for the ? operator. If either | 
|  | 1903 | /// value operand is a class type, the two operands are attempted to be | 
|  | 1904 | /// converted to each other. This function does the conversion in one direction. | 
|  | 1905 | /// It emits a diagnostic and returns true only if it finds an ambiguous | 
|  | 1906 | /// conversion. | 
|  | 1907 | static bool TryClassUnification(Sema &Self, Expr *From, Expr *To, | 
|  | 1908 | SourceLocation QuestionLoc, | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1909 | ImplicitConversionSequence &ICS) { | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1910 | // C++0x 5.16p3 | 
|  | 1911 | //   The process for determining whether an operand expression E1 of type T1 | 
|  | 1912 | //   can be converted to match an operand expression E2 of type T2 is defined | 
|  | 1913 | //   as follows: | 
|  | 1914 | //   -- If E2 is an lvalue: | 
|  | 1915 | if (To->isLvalue(Self.Context) == Expr::LV_Valid) { | 
|  | 1916 | //   E1 can be converted to match E2 if E1 can be implicitly converted to | 
|  | 1917 | //   type "lvalue reference to T2", subject to the constraint that in the | 
|  | 1918 | //   conversion the reference must bind directly to E1. | 
|  | 1919 | if (!Self.CheckReferenceInit(From, | 
|  | 1920 | Self.Context.getLValueReferenceType(To->getType()), | 
| Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 1921 | To->getLocStart(), | 
| Anders Carlsson | 2de3ace | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 1922 | /*SuppressUserConversions=*/false, | 
|  | 1923 | /*AllowExplicit=*/false, | 
|  | 1924 | /*ForceRValue=*/false, | 
|  | 1925 | &ICS)) | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1926 | { | 
| John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1927 | assert((ICS.isStandard() || ICS.isUserDefined()) && | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1928 | "expected a definite conversion"); | 
|  | 1929 | bool DirectBinding = | 
| John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1930 | ICS.isStandard() ? ICS.Standard.DirectBinding | 
|  | 1931 | : ICS.UserDefined.After.DirectBinding; | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1932 | if (DirectBinding) | 
|  | 1933 | return false; | 
|  | 1934 | } | 
|  | 1935 | } | 
| John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 1936 |  | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1937 | //   -- If E2 is an rvalue, or if the conversion above cannot be done: | 
|  | 1938 | //      -- if E1 and E2 have class type, and the underlying class types are | 
|  | 1939 | //         the same or one is a base class of the other: | 
|  | 1940 | QualType FTy = From->getType(); | 
|  | 1941 | QualType TTy = To->getType(); | 
| Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1942 | const RecordType *FRec = FTy->getAs<RecordType>(); | 
|  | 1943 | const RecordType *TRec = TTy->getAs<RecordType>(); | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1944 | bool FDerivedFromT = FRec && TRec && Self.IsDerivedFrom(FTy, TTy); | 
|  | 1945 | if (FRec && TRec && (FRec == TRec || | 
|  | 1946 | FDerivedFromT || Self.IsDerivedFrom(TTy, FTy))) { | 
|  | 1947 | //         E1 can be converted to match E2 if the class of T2 is the | 
|  | 1948 | //         same type as, or a base class of, the class of T1, and | 
|  | 1949 | //         [cv2 > cv1]. | 
| John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 1950 | if (FRec == TRec || FDerivedFromT) { | 
|  | 1951 | if (TTy.isAtLeastAsQualifiedAs(FTy)) { | 
|  | 1952 | // Could still fail if there's no copy constructor. | 
|  | 1953 | // FIXME: Is this a hard error then, or just a conversion failure? The | 
|  | 1954 | // standard doesn't say. | 
|  | 1955 | ICS = Self.TryCopyInitialization(From, TTy, | 
|  | 1956 | /*SuppressUserConversions=*/false, | 
|  | 1957 | /*ForceRValue=*/false, | 
|  | 1958 | /*InOverloadResolution=*/false); | 
|  | 1959 | } else { | 
|  | 1960 | ICS.setBad(BadConversionSequence::bad_qualifiers, From, TTy); | 
|  | 1961 | } | 
|  | 1962 | } else { | 
|  | 1963 | // Can't implicitly convert FTy to a derived class TTy. | 
|  | 1964 | // TODO: more specific error for this. | 
|  | 1965 | ICS.setBad(BadConversionSequence::no_conversion, From, TTy); | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1966 | } | 
|  | 1967 | } else { | 
|  | 1968 | //     -- Otherwise: E1 can be converted to match E2 if E1 can be | 
|  | 1969 | //        implicitly converted to the type that expression E2 would have | 
|  | 1970 | //        if E2 were converted to an rvalue. | 
|  | 1971 | // First find the decayed type. | 
|  | 1972 | if (TTy->isFunctionType()) | 
|  | 1973 | TTy = Self.Context.getPointerType(TTy); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1974 | else if (TTy->isArrayType()) | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1975 | TTy = Self.Context.getArrayDecayedType(TTy); | 
|  | 1976 |  | 
|  | 1977 | // Now try the implicit conversion. | 
|  | 1978 | // FIXME: This doesn't detect ambiguities. | 
| Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 1979 | ICS = Self.TryImplicitConversion(From, TTy, | 
|  | 1980 | /*SuppressUserConversions=*/false, | 
|  | 1981 | /*AllowExplicit=*/false, | 
| Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 1982 | /*ForceRValue=*/false, | 
|  | 1983 | /*InOverloadResolution=*/false); | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1984 | } | 
|  | 1985 | return false; | 
|  | 1986 | } | 
|  | 1987 |  | 
|  | 1988 | /// \brief Try to find a common type for two according to C++0x 5.16p5. | 
|  | 1989 | /// | 
|  | 1990 | /// This is part of the parameter validation for the ? operator. If either | 
|  | 1991 | /// value operand is a class type, overload resolution is used to find a | 
|  | 1992 | /// conversion to a common type. | 
|  | 1993 | static bool FindConditionalOverload(Sema &Self, Expr *&LHS, Expr *&RHS, | 
|  | 1994 | SourceLocation Loc) { | 
|  | 1995 | Expr *Args[2] = { LHS, RHS }; | 
| John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1996 | OverloadCandidateSet CandidateSet(Loc); | 
| Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 1997 | Self.AddBuiltinOperatorCandidates(OO_Conditional, Loc, Args, 2, CandidateSet); | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 1998 |  | 
|  | 1999 | OverloadCandidateSet::iterator Best; | 
| Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 2000 | switch (Self.BestViableFunction(CandidateSet, Loc, Best)) { | 
| Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2001 | case OR_Success: | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2002 | // We found a match. Perform the conversions on the arguments and move on. | 
|  | 2003 | if (Self.PerformImplicitConversion(LHS, Best->BuiltinTypes.ParamTypes[0], | 
| Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 2004 | Best->Conversions[0], Sema::AA_Converting) || | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2005 | Self.PerformImplicitConversion(RHS, Best->BuiltinTypes.ParamTypes[1], | 
| Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 2006 | Best->Conversions[1], Sema::AA_Converting)) | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2007 | break; | 
|  | 2008 | return false; | 
|  | 2009 |  | 
| Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2010 | case OR_No_Viable_Function: | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2011 | Self.Diag(Loc, diag::err_typecheck_cond_incompatible_operands) | 
|  | 2012 | << LHS->getType() << RHS->getType() | 
|  | 2013 | << LHS->getSourceRange() << RHS->getSourceRange(); | 
|  | 2014 | return true; | 
|  | 2015 |  | 
| Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2016 | case OR_Ambiguous: | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2017 | Self.Diag(Loc, diag::err_conditional_ambiguous_ovl) | 
|  | 2018 | << LHS->getType() << RHS->getType() | 
|  | 2019 | << LHS->getSourceRange() << RHS->getSourceRange(); | 
| Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2020 | // FIXME: Print the possible common types by printing the return types of | 
|  | 2021 | // the viable candidates. | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2022 | break; | 
|  | 2023 |  | 
| Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2024 | case OR_Deleted: | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2025 | assert(false && "Conditional operator has only built-in overloads"); | 
|  | 2026 | break; | 
|  | 2027 | } | 
|  | 2028 | return true; | 
|  | 2029 | } | 
|  | 2030 |  | 
| Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 2031 | /// \brief Perform an "extended" implicit conversion as returned by | 
|  | 2032 | /// TryClassUnification. | 
|  | 2033 | /// | 
|  | 2034 | /// TryClassUnification generates ICSs that include reference bindings. | 
|  | 2035 | /// PerformImplicitConversion is not suitable for this; it chokes if the | 
|  | 2036 | /// second part of a standard conversion is ICK_DerivedToBase. This function | 
|  | 2037 | /// handles the reference binding specially. | 
|  | 2038 | static bool ConvertForConditional(Sema &Self, Expr *&E, | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2039 | const ImplicitConversionSequence &ICS) { | 
| John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2040 | if (ICS.isStandard() && ICS.Standard.ReferenceBinding) { | 
| Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 2041 | assert(ICS.Standard.DirectBinding && | 
|  | 2042 | "TryClassUnification should never generate indirect ref bindings"); | 
| Sebastian Redl | a5cd2cd | 2009-04-26 11:21:02 +0000 | [diff] [blame] | 2043 | // FIXME: CheckReferenceInit should be able to reuse the ICS instead of | 
|  | 2044 | // redoing all the work. | 
|  | 2045 | return Self.CheckReferenceInit(E, Self.Context.getLValueReferenceType( | 
| Anders Carlsson | 2de3ace | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 2046 | TargetType(ICS)), | 
| Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 2047 | /*FIXME:*/E->getLocStart(), | 
| Anders Carlsson | 2de3ace | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 2048 | /*SuppressUserConversions=*/false, | 
|  | 2049 | /*AllowExplicit=*/false, | 
|  | 2050 | /*ForceRValue=*/false); | 
| Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 2051 | } | 
| John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2052 | if (ICS.isUserDefined() && ICS.UserDefined.After.ReferenceBinding) { | 
| Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 2053 | assert(ICS.UserDefined.After.DirectBinding && | 
|  | 2054 | "TryClassUnification should never generate indirect ref bindings"); | 
| Sebastian Redl | a5cd2cd | 2009-04-26 11:21:02 +0000 | [diff] [blame] | 2055 | return Self.CheckReferenceInit(E, Self.Context.getLValueReferenceType( | 
| Anders Carlsson | 2de3ace | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 2056 | TargetType(ICS)), | 
| Douglas Gregor | 739d828 | 2009-09-23 23:04:10 +0000 | [diff] [blame] | 2057 | /*FIXME:*/E->getLocStart(), | 
| Anders Carlsson | 2de3ace | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 2058 | /*SuppressUserConversions=*/false, | 
|  | 2059 | /*AllowExplicit=*/false, | 
|  | 2060 | /*ForceRValue=*/false); | 
| Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 2061 | } | 
| Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 2062 | if (Self.PerformImplicitConversion(E, TargetType(ICS), ICS, Sema::AA_Converting)) | 
| Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 2063 | return true; | 
|  | 2064 | return false; | 
|  | 2065 | } | 
|  | 2066 |  | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2067 | /// \brief Check the operands of ?: under C++ semantics. | 
|  | 2068 | /// | 
|  | 2069 | /// See C++ [expr.cond]. Note that LHS is never null, even for the GNU x ?: y | 
|  | 2070 | /// extension. In this case, LHS == Cond. (But they're not aliases.) | 
|  | 2071 | QualType Sema::CXXCheckConditionalOperands(Expr *&Cond, Expr *&LHS, Expr *&RHS, | 
|  | 2072 | SourceLocation QuestionLoc) { | 
| Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2073 | // FIXME: Handle C99's complex types, vector types, block pointers and Obj-C++ | 
|  | 2074 | // interface pointers. | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2075 |  | 
|  | 2076 | // C++0x 5.16p1 | 
|  | 2077 | //   The first expression is contextually converted to bool. | 
|  | 2078 | if (!Cond->isTypeDependent()) { | 
|  | 2079 | if (CheckCXXBooleanCondition(Cond)) | 
|  | 2080 | return QualType(); | 
|  | 2081 | } | 
|  | 2082 |  | 
|  | 2083 | // Either of the arguments dependent? | 
|  | 2084 | if (LHS->isTypeDependent() || RHS->isTypeDependent()) | 
|  | 2085 | return Context.DependentTy; | 
|  | 2086 |  | 
| John McCall | b13c87f | 2009-11-05 09:23:39 +0000 | [diff] [blame] | 2087 | CheckSignCompare(LHS, RHS, QuestionLoc, diag::warn_mixed_sign_conditional); | 
|  | 2088 |  | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2089 | // C++0x 5.16p2 | 
|  | 2090 | //   If either the second or the third operand has type (cv) void, ... | 
|  | 2091 | QualType LTy = LHS->getType(); | 
|  | 2092 | QualType RTy = RHS->getType(); | 
|  | 2093 | bool LVoid = LTy->isVoidType(); | 
|  | 2094 | bool RVoid = RTy->isVoidType(); | 
|  | 2095 | if (LVoid || RVoid) { | 
|  | 2096 | //   ... then the [l2r] conversions are performed on the second and third | 
|  | 2097 | //   operands ... | 
| Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 2098 | DefaultFunctionArrayLvalueConversion(LHS); | 
|  | 2099 | DefaultFunctionArrayLvalueConversion(RHS); | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2100 | LTy = LHS->getType(); | 
|  | 2101 | RTy = RHS->getType(); | 
|  | 2102 |  | 
|  | 2103 | //   ... and one of the following shall hold: | 
|  | 2104 | //   -- The second or the third operand (but not both) is a throw- | 
|  | 2105 | //      expression; the result is of the type of the other and is an rvalue. | 
|  | 2106 | bool LThrow = isa<CXXThrowExpr>(LHS); | 
|  | 2107 | bool RThrow = isa<CXXThrowExpr>(RHS); | 
|  | 2108 | if (LThrow && !RThrow) | 
|  | 2109 | return RTy; | 
|  | 2110 | if (RThrow && !LThrow) | 
|  | 2111 | return LTy; | 
|  | 2112 |  | 
|  | 2113 | //   -- Both the second and third operands have type void; the result is of | 
|  | 2114 | //      type void and is an rvalue. | 
|  | 2115 | if (LVoid && RVoid) | 
|  | 2116 | return Context.VoidTy; | 
|  | 2117 |  | 
|  | 2118 | // Neither holds, error. | 
|  | 2119 | Diag(QuestionLoc, diag::err_conditional_void_nonvoid) | 
|  | 2120 | << (LVoid ? RTy : LTy) << (LVoid ? 0 : 1) | 
|  | 2121 | << LHS->getSourceRange() << RHS->getSourceRange(); | 
|  | 2122 | return QualType(); | 
|  | 2123 | } | 
|  | 2124 |  | 
|  | 2125 | // Neither is void. | 
|  | 2126 |  | 
|  | 2127 | // C++0x 5.16p3 | 
|  | 2128 | //   Otherwise, if the second and third operand have different types, and | 
|  | 2129 | //   either has (cv) class type, and attempt is made to convert each of those | 
|  | 2130 | //   operands to the other. | 
|  | 2131 | if (Context.getCanonicalType(LTy) != Context.getCanonicalType(RTy) && | 
|  | 2132 | (LTy->isRecordType() || RTy->isRecordType())) { | 
|  | 2133 | ImplicitConversionSequence ICSLeftToRight, ICSRightToLeft; | 
|  | 2134 | // These return true if a single direction is already ambiguous. | 
|  | 2135 | if (TryClassUnification(*this, LHS, RHS, QuestionLoc, ICSLeftToRight)) | 
|  | 2136 | return QualType(); | 
|  | 2137 | if (TryClassUnification(*this, RHS, LHS, QuestionLoc, ICSRightToLeft)) | 
|  | 2138 | return QualType(); | 
|  | 2139 |  | 
| John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2140 | bool HaveL2R = !ICSLeftToRight.isBad(); | 
|  | 2141 | bool HaveR2L = !ICSRightToLeft.isBad(); | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2142 | //   If both can be converted, [...] the program is ill-formed. | 
|  | 2143 | if (HaveL2R && HaveR2L) { | 
|  | 2144 | Diag(QuestionLoc, diag::err_conditional_ambiguous) | 
|  | 2145 | << LTy << RTy << LHS->getSourceRange() << RHS->getSourceRange(); | 
|  | 2146 | return QualType(); | 
|  | 2147 | } | 
|  | 2148 |  | 
|  | 2149 | //   If exactly one conversion is possible, that conversion is applied to | 
|  | 2150 | //   the chosen operand and the converted operands are used in place of the | 
|  | 2151 | //   original operands for the remainder of this section. | 
|  | 2152 | if (HaveL2R) { | 
| Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 2153 | if (ConvertForConditional(*this, LHS, ICSLeftToRight)) | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2154 | return QualType(); | 
|  | 2155 | LTy = LHS->getType(); | 
|  | 2156 | } else if (HaveR2L) { | 
| Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 2157 | if (ConvertForConditional(*this, RHS, ICSRightToLeft)) | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2158 | return QualType(); | 
|  | 2159 | RTy = RHS->getType(); | 
|  | 2160 | } | 
|  | 2161 | } | 
|  | 2162 |  | 
|  | 2163 | // C++0x 5.16p4 | 
|  | 2164 | //   If the second and third operands are lvalues and have the same type, | 
|  | 2165 | //   the result is of that type [...] | 
|  | 2166 | bool Same = Context.getCanonicalType(LTy) == Context.getCanonicalType(RTy); | 
|  | 2167 | if (Same && LHS->isLvalue(Context) == Expr::LV_Valid && | 
|  | 2168 | RHS->isLvalue(Context) == Expr::LV_Valid) | 
|  | 2169 | return LTy; | 
|  | 2170 |  | 
|  | 2171 | // C++0x 5.16p5 | 
|  | 2172 | //   Otherwise, the result is an rvalue. If the second and third operands | 
|  | 2173 | //   do not have the same type, and either has (cv) class type, ... | 
|  | 2174 | if (!Same && (LTy->isRecordType() || RTy->isRecordType())) { | 
|  | 2175 | //   ... overload resolution is used to determine the conversions (if any) | 
|  | 2176 | //   to be applied to the operands. If the overload resolution fails, the | 
|  | 2177 | //   program is ill-formed. | 
|  | 2178 | if (FindConditionalOverload(*this, LHS, RHS, QuestionLoc)) | 
|  | 2179 | return QualType(); | 
|  | 2180 | } | 
|  | 2181 |  | 
|  | 2182 | // C++0x 5.16p6 | 
|  | 2183 | //   LValue-to-rvalue, array-to-pointer, and function-to-pointer standard | 
|  | 2184 | //   conversions are performed on the second and third operands. | 
| Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 2185 | DefaultFunctionArrayLvalueConversion(LHS); | 
|  | 2186 | DefaultFunctionArrayLvalueConversion(RHS); | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2187 | LTy = LHS->getType(); | 
|  | 2188 | RTy = RHS->getType(); | 
|  | 2189 |  | 
|  | 2190 | //   After those conversions, one of the following shall hold: | 
|  | 2191 | //   -- The second and third operands have the same type; the result | 
|  | 2192 | //      is of that type. | 
|  | 2193 | if (Context.getCanonicalType(LTy) == Context.getCanonicalType(RTy)) | 
|  | 2194 | return LTy; | 
|  | 2195 |  | 
|  | 2196 | //   -- The second and third operands have arithmetic or enumeration type; | 
|  | 2197 | //      the usual arithmetic conversions are performed to bring them to a | 
|  | 2198 | //      common type, and the result is of that type. | 
|  | 2199 | if (LTy->isArithmeticType() && RTy->isArithmeticType()) { | 
|  | 2200 | UsualArithmeticConversions(LHS, RHS); | 
|  | 2201 | return LHS->getType(); | 
|  | 2202 | } | 
|  | 2203 |  | 
|  | 2204 | //   -- The second and third operands have pointer type, or one has pointer | 
|  | 2205 | //      type and the other is a null pointer constant; pointer conversions | 
|  | 2206 | //      and qualification conversions are performed to bring them to their | 
|  | 2207 | //      composite pointer type. The result is of the composite pointer type. | 
| Eli Friedman | de8ac49 | 2010-01-02 22:56:07 +0000 | [diff] [blame] | 2208 | //   -- The second and third operands have pointer to member type, or one has | 
|  | 2209 | //      pointer to member type and the other is a null pointer constant; | 
|  | 2210 | //      pointer to member conversions and qualification conversions are | 
|  | 2211 | //      performed to bring them to a common type, whose cv-qualification | 
|  | 2212 | //      shall match the cv-qualification of either the second or the third | 
|  | 2213 | //      operand. The result is of the common type. | 
| Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 2214 | bool NonStandardCompositeType = false; | 
|  | 2215 | QualType Composite = FindCompositePointerType(LHS, RHS, | 
|  | 2216 | isSFINAEContext()? 0 : &NonStandardCompositeType); | 
|  | 2217 | if (!Composite.isNull()) { | 
|  | 2218 | if (NonStandardCompositeType) | 
|  | 2219 | Diag(QuestionLoc, | 
|  | 2220 | diag::ext_typecheck_cond_incompatible_operands_nonstandard) | 
|  | 2221 | << LTy << RTy << Composite | 
|  | 2222 | << LHS->getSourceRange() << RHS->getSourceRange(); | 
|  | 2223 |  | 
| Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2224 | return Composite; | 
| Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 2225 | } | 
| Fariborz Jahanian | 5501636 | 2009-12-10 20:46:08 +0000 | [diff] [blame] | 2226 |  | 
|  | 2227 | // Similarly, attempt to find composite type of twp objective-c pointers. | 
|  | 2228 | Composite = FindCompositeObjCPointerType(LHS, RHS, QuestionLoc); | 
|  | 2229 | if (!Composite.isNull()) | 
|  | 2230 | return Composite; | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2231 |  | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2232 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) | 
|  | 2233 | << LHS->getType() << RHS->getType() | 
|  | 2234 | << LHS->getSourceRange() << RHS->getSourceRange(); | 
|  | 2235 | return QualType(); | 
|  | 2236 | } | 
| Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2237 |  | 
|  | 2238 | /// \brief Find a merged pointer type and convert the two expressions to it. | 
|  | 2239 | /// | 
| Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 2240 | /// This finds the composite pointer type (or member pointer type) for @p E1 | 
|  | 2241 | /// and @p E2 according to C++0x 5.9p2. It converts both expressions to this | 
|  | 2242 | /// type and returns it. | 
| Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2243 | /// It does not emit diagnostics. | 
| Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 2244 | /// | 
|  | 2245 | /// If \p NonStandardCompositeType is non-NULL, then we are permitted to find | 
|  | 2246 | /// a non-standard (but still sane) composite type to which both expressions | 
|  | 2247 | /// can be converted. When such a type is chosen, \c *NonStandardCompositeType | 
|  | 2248 | /// will be set true. | 
|  | 2249 | QualType Sema::FindCompositePointerType(Expr *&E1, Expr *&E2, | 
|  | 2250 | bool *NonStandardCompositeType) { | 
|  | 2251 | if (NonStandardCompositeType) | 
|  | 2252 | *NonStandardCompositeType = false; | 
|  | 2253 |  | 
| Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2254 | assert(getLangOptions().CPlusPlus && "This function assumes C++"); | 
|  | 2255 | QualType T1 = E1->getType(), T2 = E2->getType(); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2256 |  | 
| Fariborz Jahanian | 0cedfbd | 2009-12-08 20:04:24 +0000 | [diff] [blame] | 2257 | if (!T1->isAnyPointerType() && !T1->isMemberPointerType() && | 
|  | 2258 | !T2->isAnyPointerType() && !T2->isMemberPointerType()) | 
| Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 2259 | return QualType(); | 
| Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2260 |  | 
|  | 2261 | // C++0x 5.9p2 | 
|  | 2262 | //   Pointer conversions and qualification conversions are performed on | 
|  | 2263 | //   pointer operands to bring them to their composite pointer type. If | 
|  | 2264 | //   one operand is a null pointer constant, the composite pointer type is | 
|  | 2265 | //   the type of the other operand. | 
| Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2266 | if (E1->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { | 
| Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2267 | if (T2->isMemberPointerType()) | 
|  | 2268 | ImpCastExprToType(E1, T2, CastExpr::CK_NullToMemberPointer); | 
|  | 2269 | else | 
|  | 2270 | ImpCastExprToType(E1, T2, CastExpr::CK_IntegralToPointer); | 
| Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2271 | return T2; | 
|  | 2272 | } | 
| Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2273 | if (E2->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { | 
| Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2274 | if (T1->isMemberPointerType()) | 
|  | 2275 | ImpCastExprToType(E2, T1, CastExpr::CK_NullToMemberPointer); | 
|  | 2276 | else | 
|  | 2277 | ImpCastExprToType(E2, T1, CastExpr::CK_IntegralToPointer); | 
| Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2278 | return T1; | 
|  | 2279 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2280 |  | 
| Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 2281 | // Now both have to be pointers or member pointers. | 
| Sebastian Redl | a439e6f | 2009-11-16 21:03:45 +0000 | [diff] [blame] | 2282 | if ((!T1->isPointerType() && !T1->isMemberPointerType()) || | 
|  | 2283 | (!T2->isPointerType() && !T2->isMemberPointerType())) | 
| Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2284 | return QualType(); | 
|  | 2285 |  | 
|  | 2286 | //   Otherwise, of one of the operands has type "pointer to cv1 void," then | 
|  | 2287 | //   the other has type "pointer to cv2 T" and the composite pointer type is | 
|  | 2288 | //   "pointer to cv12 void," where cv12 is the union of cv1 and cv2. | 
|  | 2289 | //   Otherwise, the composite pointer type is a pointer type similar to the | 
|  | 2290 | //   type of one of the operands, with a cv-qualification signature that is | 
|  | 2291 | //   the union of the cv-qualification signatures of the operand types. | 
|  | 2292 | // In practice, the first part here is redundant; it's subsumed by the second. | 
|  | 2293 | // What we do here is, we build the two possible composite types, and try the | 
|  | 2294 | // conversions in both directions. If only one works, or if the two composite | 
|  | 2295 | // types are the same, we have succeeded. | 
| John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2296 | // FIXME: extended qualifiers? | 
| Sebastian Redl | a439e6f | 2009-11-16 21:03:45 +0000 | [diff] [blame] | 2297 | typedef llvm::SmallVector<unsigned, 4> QualifierVector; | 
|  | 2298 | QualifierVector QualifierUnion; | 
|  | 2299 | typedef llvm::SmallVector<std::pair<const Type *, const Type *>, 4> | 
|  | 2300 | ContainingClassVector; | 
|  | 2301 | ContainingClassVector MemberOfClass; | 
|  | 2302 | QualType Composite1 = Context.getCanonicalType(T1), | 
|  | 2303 | Composite2 = Context.getCanonicalType(T2); | 
| Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 2304 | unsigned NeedConstBefore = 0; | 
| Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 2305 | do { | 
|  | 2306 | const PointerType *Ptr1, *Ptr2; | 
|  | 2307 | if ((Ptr1 = Composite1->getAs<PointerType>()) && | 
|  | 2308 | (Ptr2 = Composite2->getAs<PointerType>())) { | 
|  | 2309 | Composite1 = Ptr1->getPointeeType(); | 
|  | 2310 | Composite2 = Ptr2->getPointeeType(); | 
| Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 2311 |  | 
|  | 2312 | // If we're allowed to create a non-standard composite type, keep track | 
|  | 2313 | // of where we need to fill in additional 'const' qualifiers. | 
|  | 2314 | if (NonStandardCompositeType && | 
|  | 2315 | Composite1.getCVRQualifiers() != Composite2.getCVRQualifiers()) | 
|  | 2316 | NeedConstBefore = QualifierUnion.size(); | 
|  | 2317 |  | 
| Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 2318 | QualifierUnion.push_back( | 
|  | 2319 | Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers()); | 
|  | 2320 | MemberOfClass.push_back(std::make_pair((const Type *)0, (const Type *)0)); | 
|  | 2321 | continue; | 
|  | 2322 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2323 |  | 
| Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 2324 | const MemberPointerType *MemPtr1, *MemPtr2; | 
|  | 2325 | if ((MemPtr1 = Composite1->getAs<MemberPointerType>()) && | 
|  | 2326 | (MemPtr2 = Composite2->getAs<MemberPointerType>())) { | 
|  | 2327 | Composite1 = MemPtr1->getPointeeType(); | 
|  | 2328 | Composite2 = MemPtr2->getPointeeType(); | 
| Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 2329 |  | 
|  | 2330 | // If we're allowed to create a non-standard composite type, keep track | 
|  | 2331 | // of where we need to fill in additional 'const' qualifiers. | 
|  | 2332 | if (NonStandardCompositeType && | 
|  | 2333 | Composite1.getCVRQualifiers() != Composite2.getCVRQualifiers()) | 
|  | 2334 | NeedConstBefore = QualifierUnion.size(); | 
|  | 2335 |  | 
| Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 2336 | QualifierUnion.push_back( | 
|  | 2337 | Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers()); | 
|  | 2338 | MemberOfClass.push_back(std::make_pair(MemPtr1->getClass(), | 
|  | 2339 | MemPtr2->getClass())); | 
|  | 2340 | continue; | 
|  | 2341 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2342 |  | 
| Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 2343 | // FIXME: block pointer types? | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2344 |  | 
| Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 2345 | // Cannot unwrap any more types. | 
|  | 2346 | break; | 
|  | 2347 | } while (true); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2348 |  | 
| Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 2349 | if (NeedConstBefore && NonStandardCompositeType) { | 
|  | 2350 | // Extension: Add 'const' to qualifiers that come before the first qualifier | 
|  | 2351 | // mismatch, so that our (non-standard!) composite type meets the | 
|  | 2352 | // requirements of C++ [conv.qual]p4 bullet 3. | 
|  | 2353 | for (unsigned I = 0; I != NeedConstBefore; ++I) { | 
|  | 2354 | if ((QualifierUnion[I] & Qualifiers::Const) == 0) { | 
|  | 2355 | QualifierUnion[I] = QualifierUnion[I] | Qualifiers::Const; | 
|  | 2356 | *NonStandardCompositeType = true; | 
|  | 2357 | } | 
|  | 2358 | } | 
|  | 2359 | } | 
|  | 2360 |  | 
| Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 2361 | // Rewrap the composites as pointers or member pointers with the union CVRs. | 
| Sebastian Redl | a439e6f | 2009-11-16 21:03:45 +0000 | [diff] [blame] | 2362 | ContainingClassVector::reverse_iterator MOC | 
|  | 2363 | = MemberOfClass.rbegin(); | 
|  | 2364 | for (QualifierVector::reverse_iterator | 
|  | 2365 | I = QualifierUnion.rbegin(), | 
|  | 2366 | E = QualifierUnion.rend(); | 
| Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 2367 | I != E; (void)++I, ++MOC) { | 
| John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2368 | Qualifiers Quals = Qualifiers::fromCVRMask(*I); | 
| Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 2369 | if (MOC->first && MOC->second) { | 
|  | 2370 | // Rebuild member pointer type | 
| John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2371 | Composite1 = Context.getMemberPointerType( | 
|  | 2372 | Context.getQualifiedType(Composite1, Quals), | 
|  | 2373 | MOC->first); | 
|  | 2374 | Composite2 = Context.getMemberPointerType( | 
|  | 2375 | Context.getQualifiedType(Composite2, Quals), | 
|  | 2376 | MOC->second); | 
| Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 2377 | } else { | 
|  | 2378 | // Rebuild pointer type | 
| John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2379 | Composite1 | 
|  | 2380 | = Context.getPointerType(Context.getQualifiedType(Composite1, Quals)); | 
|  | 2381 | Composite2 | 
|  | 2382 | = Context.getPointerType(Context.getQualifiedType(Composite2, Quals)); | 
| Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 2383 | } | 
| Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2384 | } | 
|  | 2385 |  | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2386 | ImplicitConversionSequence E1ToC1 = | 
| Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 2387 | TryImplicitConversion(E1, Composite1, | 
|  | 2388 | /*SuppressUserConversions=*/false, | 
|  | 2389 | /*AllowExplicit=*/false, | 
| Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2390 | /*ForceRValue=*/false, | 
|  | 2391 | /*InOverloadResolution=*/false); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2392 | ImplicitConversionSequence E2ToC1 = | 
| Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 2393 | TryImplicitConversion(E2, Composite1, | 
|  | 2394 | /*SuppressUserConversions=*/false, | 
|  | 2395 | /*AllowExplicit=*/false, | 
| Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2396 | /*ForceRValue=*/false, | 
|  | 2397 | /*InOverloadResolution=*/false); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2398 |  | 
| John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 2399 | bool ToC2Viable = false; | 
| Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2400 | ImplicitConversionSequence E1ToC2, E2ToC2; | 
| Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2401 | if (Context.getCanonicalType(Composite1) != | 
|  | 2402 | Context.getCanonicalType(Composite2)) { | 
| Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 2403 | E1ToC2 = TryImplicitConversion(E1, Composite2, | 
|  | 2404 | /*SuppressUserConversions=*/false, | 
|  | 2405 | /*AllowExplicit=*/false, | 
| Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2406 | /*ForceRValue=*/false, | 
|  | 2407 | /*InOverloadResolution=*/false); | 
| Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 2408 | E2ToC2 = TryImplicitConversion(E2, Composite2, | 
|  | 2409 | /*SuppressUserConversions=*/false, | 
|  | 2410 | /*AllowExplicit=*/false, | 
| Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2411 | /*ForceRValue=*/false, | 
|  | 2412 | /*InOverloadResolution=*/false); | 
| John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 2413 | ToC2Viable = !E1ToC2.isBad() && !E2ToC2.isBad(); | 
| Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2414 | } | 
|  | 2415 |  | 
| John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2416 | bool ToC1Viable = !E1ToC1.isBad() && !E2ToC1.isBad(); | 
| Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2417 | if (ToC1Viable && !ToC2Viable) { | 
| Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 2418 | if (!PerformImplicitConversion(E1, Composite1, E1ToC1, Sema::AA_Converting) && | 
|  | 2419 | !PerformImplicitConversion(E2, Composite1, E2ToC1, Sema::AA_Converting)) | 
| Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2420 | return Composite1; | 
|  | 2421 | } | 
|  | 2422 | if (ToC2Viable && !ToC1Viable) { | 
| Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 2423 | if (!PerformImplicitConversion(E1, Composite2, E1ToC2, Sema::AA_Converting) && | 
|  | 2424 | !PerformImplicitConversion(E2, Composite2, E2ToC2, Sema::AA_Converting)) | 
| Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2425 | return Composite2; | 
|  | 2426 | } | 
|  | 2427 | return QualType(); | 
|  | 2428 | } | 
| Anders Carlsson | 165a0a0 | 2009-05-17 18:41:29 +0000 | [diff] [blame] | 2429 |  | 
| Anders Carlsson | def1199 | 2009-05-30 20:36:53 +0000 | [diff] [blame] | 2430 | Sema::OwningExprResult Sema::MaybeBindToTemporary(Expr *E) { | 
| Anders Carlsson | 089c260 | 2009-08-15 23:41:35 +0000 | [diff] [blame] | 2431 | if (!Context.getLangOptions().CPlusPlus) | 
|  | 2432 | return Owned(E); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2433 |  | 
| Douglas Gregor | 5132655 | 2009-12-24 18:51:59 +0000 | [diff] [blame] | 2434 | assert(!isa<CXXBindTemporaryExpr>(E) && "Double-bound temporary?"); | 
|  | 2435 |  | 
| Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2436 | const RecordType *RT = E->getType()->getAs<RecordType>(); | 
| Anders Carlsson | def1199 | 2009-05-30 20:36:53 +0000 | [diff] [blame] | 2437 | if (!RT) | 
|  | 2438 | return Owned(E); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2439 |  | 
| John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2440 | // If this is the result of a call expression, our source might | 
|  | 2441 | // actually be a reference, in which case we shouldn't bind. | 
| Anders Carlsson | 283e4d5 | 2009-09-14 01:30:44 +0000 | [diff] [blame] | 2442 | if (CallExpr *CE = dyn_cast<CallExpr>(E)) { | 
|  | 2443 | QualType Ty = CE->getCallee()->getType(); | 
|  | 2444 | if (const PointerType *PT = Ty->getAs<PointerType>()) | 
|  | 2445 | Ty = PT->getPointeeType(); | 
| Fariborz Jahanian | b372b0f | 2010-02-18 20:31:02 +0000 | [diff] [blame] | 2446 | else if (const BlockPointerType *BPT = Ty->getAs<BlockPointerType>()) | 
|  | 2447 | Ty = BPT->getPointeeType(); | 
|  | 2448 |  | 
| John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2449 | const FunctionType *FTy = Ty->getAs<FunctionType>(); | 
| Anders Carlsson | 283e4d5 | 2009-09-14 01:30:44 +0000 | [diff] [blame] | 2450 | if (FTy->getResultType()->isReferenceType()) | 
|  | 2451 | return Owned(E); | 
|  | 2452 | } | 
| John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2453 |  | 
|  | 2454 | // That should be enough to guarantee that this type is complete. | 
|  | 2455 | // If it has a trivial destructor, we can avoid the extra copy. | 
|  | 2456 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); | 
|  | 2457 | if (RD->hasTrivialDestructor()) | 
|  | 2458 | return Owned(E); | 
|  | 2459 |  | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2460 | CXXTemporary *Temp = CXXTemporary::Create(Context, | 
| Anders Carlsson | def1199 | 2009-05-30 20:36:53 +0000 | [diff] [blame] | 2461 | RD->getDestructor(Context)); | 
| Anders Carlsson | 860306e | 2009-05-30 21:21:49 +0000 | [diff] [blame] | 2462 | ExprTemporaries.push_back(Temp); | 
| Fariborz Jahanian | a83f7ed | 2009-08-03 19:13:25 +0000 | [diff] [blame] | 2463 | if (CXXDestructorDecl *Destructor = | 
|  | 2464 | const_cast<CXXDestructorDecl*>(RD->getDestructor(Context))) | 
|  | 2465 | MarkDeclarationReferenced(E->getExprLoc(), Destructor); | 
| Anders Carlsson | def1199 | 2009-05-30 20:36:53 +0000 | [diff] [blame] | 2466 | // FIXME: Add the temporary to the temporaries vector. | 
|  | 2467 | return Owned(CXXBindTemporaryExpr::Create(Context, Temp, E)); | 
|  | 2468 | } | 
|  | 2469 |  | 
| Anders Carlsson | 0ece491 | 2009-12-15 20:51:39 +0000 | [diff] [blame] | 2470 | Expr *Sema::MaybeCreateCXXExprWithTemporaries(Expr *SubExpr) { | 
| Anders Carlsson | 99ba36d | 2009-06-05 15:38:08 +0000 | [diff] [blame] | 2471 | assert(SubExpr && "sub expression can't be null!"); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2472 |  | 
| Douglas Gregor | 1f5f3a4 | 2009-12-03 17:10:37 +0000 | [diff] [blame] | 2473 | unsigned FirstTemporary = ExprEvalContexts.back().NumTemporaries; | 
|  | 2474 | assert(ExprTemporaries.size() >= FirstTemporary); | 
|  | 2475 | if (ExprTemporaries.size() == FirstTemporary) | 
| Anders Carlsson | 99ba36d | 2009-06-05 15:38:08 +0000 | [diff] [blame] | 2476 | return SubExpr; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2477 |  | 
| Anders Carlsson | 99ba36d | 2009-06-05 15:38:08 +0000 | [diff] [blame] | 2478 | Expr *E = CXXExprWithTemporaries::Create(Context, SubExpr, | 
| Douglas Gregor | 1f5f3a4 | 2009-12-03 17:10:37 +0000 | [diff] [blame] | 2479 | &ExprTemporaries[FirstTemporary], | 
| Anders Carlsson | 0ece491 | 2009-12-15 20:51:39 +0000 | [diff] [blame] | 2480 | ExprTemporaries.size() - FirstTemporary); | 
| Douglas Gregor | 1f5f3a4 | 2009-12-03 17:10:37 +0000 | [diff] [blame] | 2481 | ExprTemporaries.erase(ExprTemporaries.begin() + FirstTemporary, | 
|  | 2482 | ExprTemporaries.end()); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2483 |  | 
| Anders Carlsson | 99ba36d | 2009-06-05 15:38:08 +0000 | [diff] [blame] | 2484 | return E; | 
|  | 2485 | } | 
|  | 2486 |  | 
| Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 2487 | Sema::OwningExprResult | 
|  | 2488 | Sema::MaybeCreateCXXExprWithTemporaries(OwningExprResult SubExpr) { | 
|  | 2489 | if (SubExpr.isInvalid()) | 
|  | 2490 | return ExprError(); | 
|  | 2491 |  | 
|  | 2492 | return Owned(MaybeCreateCXXExprWithTemporaries(SubExpr.takeAs<Expr>())); | 
|  | 2493 | } | 
|  | 2494 |  | 
| Anders Carlsson | 5ee56e9 | 2009-12-16 02:09:40 +0000 | [diff] [blame] | 2495 | FullExpr Sema::CreateFullExpr(Expr *SubExpr) { | 
|  | 2496 | unsigned FirstTemporary = ExprEvalContexts.back().NumTemporaries; | 
|  | 2497 | assert(ExprTemporaries.size() >= FirstTemporary); | 
|  | 2498 |  | 
|  | 2499 | unsigned NumTemporaries = ExprTemporaries.size() - FirstTemporary; | 
|  | 2500 | CXXTemporary **Temporaries = | 
|  | 2501 | NumTemporaries == 0 ? 0 : &ExprTemporaries[FirstTemporary]; | 
|  | 2502 |  | 
|  | 2503 | FullExpr E = FullExpr::Create(Context, SubExpr, Temporaries, NumTemporaries); | 
|  | 2504 |  | 
|  | 2505 | ExprTemporaries.erase(ExprTemporaries.begin() + FirstTemporary, | 
|  | 2506 | ExprTemporaries.end()); | 
|  | 2507 |  | 
|  | 2508 | return E; | 
|  | 2509 | } | 
|  | 2510 |  | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2511 | Sema::OwningExprResult | 
| Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2512 | Sema::ActOnStartCXXMemberReference(Scope *S, ExprArg Base, SourceLocation OpLoc, | 
| Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 2513 | tok::TokenKind OpKind, TypeTy *&ObjectType, | 
|  | 2514 | bool &MayBePseudoDestructor) { | 
| Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2515 | // Since this might be a postfix expression, get rid of ParenListExprs. | 
|  | 2516 | Base = MaybeConvertParenListExprToParenExpr(S, move(Base)); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2517 |  | 
| Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2518 | Expr *BaseExpr = (Expr*)Base.get(); | 
|  | 2519 | assert(BaseExpr && "no record expansion"); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2520 |  | 
| Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2521 | QualType BaseType = BaseExpr->getType(); | 
| Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 2522 | MayBePseudoDestructor = false; | 
| Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2523 | if (BaseType->isDependentType()) { | 
| Douglas Gregor | 43d8863 | 2009-11-04 22:49:18 +0000 | [diff] [blame] | 2524 | // If we have a pointer to a dependent type and are using the -> operator, | 
|  | 2525 | // the object type is the type that the pointer points to. We might still | 
|  | 2526 | // have enough information about that type to do something useful. | 
|  | 2527 | if (OpKind == tok::arrow) | 
|  | 2528 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) | 
|  | 2529 | BaseType = Ptr->getPointeeType(); | 
|  | 2530 |  | 
| Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2531 | ObjectType = BaseType.getAsOpaquePtr(); | 
| Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 2532 | MayBePseudoDestructor = true; | 
| Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2533 | return move(Base); | 
|  | 2534 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2535 |  | 
| Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2536 | // C++ [over.match.oper]p8: | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2537 | //   [...] When operator->returns, the operator-> is applied  to the value | 
| Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2538 | //   returned, with the original second operand. | 
|  | 2539 | if (OpKind == tok::arrow) { | 
| John McCall | c4e8321 | 2009-09-30 01:01:30 +0000 | [diff] [blame] | 2540 | // The set of types we've considered so far. | 
| John McCall | 432887f | 2009-09-30 01:30:54 +0000 | [diff] [blame] | 2541 | llvm::SmallPtrSet<CanQualType,8> CTypes; | 
| Fariborz Jahanian | 7a8233a | 2009-09-30 17:46:20 +0000 | [diff] [blame] | 2542 | llvm::SmallVector<SourceLocation, 8> Locations; | 
| John McCall | 432887f | 2009-09-30 01:30:54 +0000 | [diff] [blame] | 2543 | CTypes.insert(Context.getCanonicalType(BaseType)); | 
| John McCall | c4e8321 | 2009-09-30 01:01:30 +0000 | [diff] [blame] | 2544 |  | 
| Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2545 | while (BaseType->isRecordType()) { | 
| Anders Carlsson | 15ea378 | 2009-10-13 22:43:21 +0000 | [diff] [blame] | 2546 | Base = BuildOverloadedArrowExpr(S, move(Base), OpLoc); | 
| Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2547 | BaseExpr = (Expr*)Base.get(); | 
|  | 2548 | if (BaseExpr == NULL) | 
|  | 2549 | return ExprError(); | 
| Fariborz Jahanian | 7a8233a | 2009-09-30 17:46:20 +0000 | [diff] [blame] | 2550 | if (CXXOperatorCallExpr *OpCall = dyn_cast<CXXOperatorCallExpr>(BaseExpr)) | 
| Anders Carlsson | de699e5 | 2009-10-13 22:55:59 +0000 | [diff] [blame] | 2551 | Locations.push_back(OpCall->getDirectCallee()->getLocation()); | 
| John McCall | c4e8321 | 2009-09-30 01:01:30 +0000 | [diff] [blame] | 2552 | BaseType = BaseExpr->getType(); | 
|  | 2553 | CanQualType CBaseType = Context.getCanonicalType(BaseType); | 
| John McCall | 432887f | 2009-09-30 01:30:54 +0000 | [diff] [blame] | 2554 | if (!CTypes.insert(CBaseType)) { | 
| Fariborz Jahanian | 4a4e345 | 2009-09-30 00:19:41 +0000 | [diff] [blame] | 2555 | Diag(OpLoc, diag::err_operator_arrow_circular); | 
| Fariborz Jahanian | 7a8233a | 2009-09-30 17:46:20 +0000 | [diff] [blame] | 2556 | for (unsigned i = 0; i < Locations.size(); i++) | 
|  | 2557 | Diag(Locations[i], diag::note_declared_at); | 
| Fariborz Jahanian | 4a4e345 | 2009-09-30 00:19:41 +0000 | [diff] [blame] | 2558 | return ExprError(); | 
|  | 2559 | } | 
| Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2560 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2561 |  | 
| Douglas Gregor | 31658df | 2009-11-20 19:58:21 +0000 | [diff] [blame] | 2562 | if (BaseType->isPointerType()) | 
|  | 2563 | BaseType = BaseType->getPointeeType(); | 
|  | 2564 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2565 |  | 
|  | 2566 | // We could end up with various non-record types here, such as extended | 
| Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2567 | // vector types or Objective-C interfaces. Just return early and let | 
|  | 2568 | // ActOnMemberReferenceExpr do the work. | 
| Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 2569 | if (!BaseType->isRecordType()) { | 
|  | 2570 | // C++ [basic.lookup.classref]p2: | 
|  | 2571 | //   [...] If the type of the object expression is of pointer to scalar | 
|  | 2572 | //   type, the unqualified-id is looked up in the context of the complete | 
|  | 2573 | //   postfix-expression. | 
| Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 2574 | // | 
|  | 2575 | // This also indicates that we should be parsing a | 
|  | 2576 | // pseudo-destructor-name. | 
| Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 2577 | ObjectType = 0; | 
| Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 2578 | MayBePseudoDestructor = true; | 
| Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2579 | return move(Base); | 
| Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 2580 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2581 |  | 
| Douglas Gregor | 03c5705 | 2009-11-17 05:17:33 +0000 | [diff] [blame] | 2582 | // The object type must be complete (or dependent). | 
|  | 2583 | if (!BaseType->isDependentType() && | 
|  | 2584 | RequireCompleteType(OpLoc, BaseType, | 
|  | 2585 | PDiag(diag::err_incomplete_member_access))) | 
|  | 2586 | return ExprError(); | 
|  | 2587 |  | 
| Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 2588 | // C++ [basic.lookup.classref]p2: | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2589 | //   If the id-expression in a class member access (5.2.5) is an | 
| Douglas Gregor | 03c5705 | 2009-11-17 05:17:33 +0000 | [diff] [blame] | 2590 | //   unqualified-id, and the type of the object expression is of a class | 
| Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 2591 | //   type C (or of pointer to a class type C), the unqualified-id is looked | 
|  | 2592 | //   up in the scope of class C. [...] | 
| Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2593 | ObjectType = BaseType.getAsOpaquePtr(); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2594 | return move(Base); | 
| Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 2595 | } | 
|  | 2596 |  | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 2597 | Sema::OwningExprResult Sema::DiagnoseDtorReference(SourceLocation NameLoc, | 
|  | 2598 | ExprArg MemExpr) { | 
|  | 2599 | Expr *E = (Expr *) MemExpr.get(); | 
|  | 2600 | SourceLocation ExpectedLParenLoc = PP.getLocForEndOfToken(NameLoc); | 
|  | 2601 | Diag(E->getLocStart(), diag::err_dtor_expr_without_call) | 
|  | 2602 | << isa<CXXPseudoDestructorExpr>(E) | 
|  | 2603 | << CodeModificationHint::CreateInsertion(ExpectedLParenLoc, "()"); | 
|  | 2604 |  | 
|  | 2605 | return ActOnCallExpr(/*Scope*/ 0, | 
|  | 2606 | move(MemExpr), | 
|  | 2607 | /*LPLoc*/ ExpectedLParenLoc, | 
|  | 2608 | Sema::MultiExprArg(*this, 0, 0), | 
|  | 2609 | /*CommaLocs*/ 0, | 
|  | 2610 | /*RPLoc*/ ExpectedLParenLoc); | 
|  | 2611 | } | 
| Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 2612 |  | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 2613 | Sema::OwningExprResult Sema::BuildPseudoDestructorExpr(ExprArg Base, | 
|  | 2614 | SourceLocation OpLoc, | 
|  | 2615 | tok::TokenKind OpKind, | 
|  | 2616 | const CXXScopeSpec &SS, | 
| Douglas Gregor | 26d4ac9 | 2010-02-24 23:40:28 +0000 | [diff] [blame] | 2617 | TypeSourceInfo *ScopeTypeInfo, | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 2618 | SourceLocation CCLoc, | 
| Douglas Gregor | fce46ee | 2010-02-24 23:50:37 +0000 | [diff] [blame] | 2619 | SourceLocation TildeLoc, | 
| Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 2620 | PseudoDestructorTypeStorage Destructed, | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 2621 | bool HasTrailingLParen) { | 
| Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 2622 | TypeSourceInfo *DestructedTypeInfo = Destructed.getTypeSourceInfo(); | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 2623 |  | 
|  | 2624 | // C++ [expr.pseudo]p2: | 
|  | 2625 | //   The left-hand side of the dot operator shall be of scalar type. The | 
|  | 2626 | //   left-hand side of the arrow operator shall be of pointer to scalar type. | 
|  | 2627 | //   This scalar type is the object type. | 
|  | 2628 | Expr *BaseE = (Expr *)Base.get(); | 
|  | 2629 | QualType ObjectType = BaseE->getType(); | 
|  | 2630 | if (OpKind == tok::arrow) { | 
|  | 2631 | if (const PointerType *Ptr = ObjectType->getAs<PointerType>()) { | 
|  | 2632 | ObjectType = Ptr->getPointeeType(); | 
|  | 2633 | } else if (!BaseE->isTypeDependent()) { | 
|  | 2634 | // The user wrote "p->" when she probably meant "p."; fix it. | 
|  | 2635 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) | 
|  | 2636 | << ObjectType << true | 
|  | 2637 | << CodeModificationHint::CreateReplacement(OpLoc, "."); | 
|  | 2638 | if (isSFINAEContext()) | 
|  | 2639 | return ExprError(); | 
|  | 2640 |  | 
|  | 2641 | OpKind = tok::period; | 
|  | 2642 | } | 
|  | 2643 | } | 
|  | 2644 |  | 
|  | 2645 | if (!ObjectType->isDependentType() && !ObjectType->isScalarType()) { | 
|  | 2646 | Diag(OpLoc, diag::err_pseudo_dtor_base_not_scalar) | 
|  | 2647 | << ObjectType << BaseE->getSourceRange(); | 
|  | 2648 | return ExprError(); | 
|  | 2649 | } | 
|  | 2650 |  | 
|  | 2651 | // C++ [expr.pseudo]p2: | 
|  | 2652 | //   [...] The cv-unqualified versions of the object type and of the type | 
|  | 2653 | //   designated by the pseudo-destructor-name shall be the same type. | 
| Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 2654 | if (DestructedTypeInfo) { | 
|  | 2655 | QualType DestructedType = DestructedTypeInfo->getType(); | 
|  | 2656 | SourceLocation DestructedTypeStart | 
|  | 2657 | = DestructedTypeInfo->getTypeLoc().getSourceRange().getBegin(); | 
|  | 2658 | if (!DestructedType->isDependentType() && !ObjectType->isDependentType() && | 
|  | 2659 | !Context.hasSameUnqualifiedType(DestructedType, ObjectType)) { | 
|  | 2660 | Diag(DestructedTypeStart, diag::err_pseudo_dtor_type_mismatch) | 
|  | 2661 | << ObjectType << DestructedType << BaseE->getSourceRange() | 
|  | 2662 | << DestructedTypeInfo->getTypeLoc().getSourceRange(); | 
|  | 2663 |  | 
|  | 2664 | // Recover by setting the destructed type to the object type. | 
|  | 2665 | DestructedType = ObjectType; | 
|  | 2666 | DestructedTypeInfo = Context.getTrivialTypeSourceInfo(ObjectType, | 
|  | 2667 | DestructedTypeStart); | 
|  | 2668 | Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo); | 
|  | 2669 | } | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 2670 | } | 
| Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 2671 |  | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 2672 | // C++ [expr.pseudo]p2: | 
|  | 2673 | //   [...] Furthermore, the two type-names in a pseudo-destructor-name of the | 
|  | 2674 | //   form | 
|  | 2675 | // | 
|  | 2676 | //     ::[opt] nested-name-specifier[opt] type-name :: ~ type-name | 
|  | 2677 | // | 
|  | 2678 | //   shall designate the same scalar type. | 
|  | 2679 | if (ScopeTypeInfo) { | 
|  | 2680 | QualType ScopeType = ScopeTypeInfo->getType(); | 
|  | 2681 | if (!ScopeType->isDependentType() && !ObjectType->isDependentType() && | 
|  | 2682 | !Context.hasSameType(ScopeType, ObjectType)) { | 
|  | 2683 |  | 
|  | 2684 | Diag(ScopeTypeInfo->getTypeLoc().getSourceRange().getBegin(), | 
|  | 2685 | diag::err_pseudo_dtor_type_mismatch) | 
|  | 2686 | << ObjectType << ScopeType << BaseE->getSourceRange() | 
|  | 2687 | << ScopeTypeInfo->getTypeLoc().getSourceRange(); | 
|  | 2688 |  | 
|  | 2689 | ScopeType = QualType(); | 
|  | 2690 | ScopeTypeInfo = 0; | 
|  | 2691 | } | 
|  | 2692 | } | 
|  | 2693 |  | 
|  | 2694 | OwningExprResult Result | 
|  | 2695 | = Owned(new (Context) CXXPseudoDestructorExpr(Context, | 
|  | 2696 | Base.takeAs<Expr>(), | 
|  | 2697 | OpKind == tok::arrow, | 
|  | 2698 | OpLoc, | 
|  | 2699 | (NestedNameSpecifier *) SS.getScopeRep(), | 
|  | 2700 | SS.getRange(), | 
|  | 2701 | ScopeTypeInfo, | 
|  | 2702 | CCLoc, | 
| Douglas Gregor | fce46ee | 2010-02-24 23:50:37 +0000 | [diff] [blame] | 2703 | TildeLoc, | 
| Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 2704 | Destructed)); | 
|  | 2705 |  | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 2706 | if (HasTrailingLParen) | 
|  | 2707 | return move(Result); | 
|  | 2708 |  | 
| Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 2709 | return DiagnoseDtorReference(Destructed.getLocation(), move(Result)); | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 2710 | } | 
|  | 2711 |  | 
|  | 2712 | Sema::OwningExprResult Sema::ActOnPseudoDestructorExpr(Scope *S, ExprArg Base, | 
|  | 2713 | SourceLocation OpLoc, | 
|  | 2714 | tok::TokenKind OpKind, | 
|  | 2715 | const CXXScopeSpec &SS, | 
|  | 2716 | UnqualifiedId &FirstTypeName, | 
|  | 2717 | SourceLocation CCLoc, | 
|  | 2718 | SourceLocation TildeLoc, | 
|  | 2719 | UnqualifiedId &SecondTypeName, | 
|  | 2720 | bool HasTrailingLParen) { | 
|  | 2721 | assert((FirstTypeName.getKind() == UnqualifiedId::IK_TemplateId || | 
|  | 2722 | FirstTypeName.getKind() == UnqualifiedId::IK_Identifier) && | 
|  | 2723 | "Invalid first type name in pseudo-destructor"); | 
|  | 2724 | assert((SecondTypeName.getKind() == UnqualifiedId::IK_TemplateId || | 
|  | 2725 | SecondTypeName.getKind() == UnqualifiedId::IK_Identifier) && | 
|  | 2726 | "Invalid second type name in pseudo-destructor"); | 
|  | 2727 |  | 
|  | 2728 | Expr *BaseE = (Expr *)Base.get(); | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 2729 |  | 
|  | 2730 | // C++ [expr.pseudo]p2: | 
|  | 2731 | //   The left-hand side of the dot operator shall be of scalar type. The | 
|  | 2732 | //   left-hand side of the arrow operator shall be of pointer to scalar type. | 
|  | 2733 | //   This scalar type is the object type. | 
|  | 2734 | QualType ObjectType = BaseE->getType(); | 
|  | 2735 | if (OpKind == tok::arrow) { | 
|  | 2736 | if (const PointerType *Ptr = ObjectType->getAs<PointerType>()) { | 
|  | 2737 | ObjectType = Ptr->getPointeeType(); | 
| Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 2738 | } else if (!ObjectType->isDependentType()) { | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 2739 | // The user wrote "p->" when she probably meant "p."; fix it. | 
|  | 2740 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) | 
| Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 2741 | << ObjectType << true | 
|  | 2742 | << CodeModificationHint::CreateReplacement(OpLoc, "."); | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 2743 | if (isSFINAEContext()) | 
|  | 2744 | return ExprError(); | 
|  | 2745 |  | 
|  | 2746 | OpKind = tok::period; | 
|  | 2747 | } | 
|  | 2748 | } | 
| Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 2749 |  | 
|  | 2750 | // Compute the object type that we should use for name lookup purposes. Only | 
|  | 2751 | // record types and dependent types matter. | 
|  | 2752 | void *ObjectTypePtrForLookup = 0; | 
|  | 2753 | if (!SS.isSet()) { | 
|  | 2754 | ObjectTypePtrForLookup = (void *)ObjectType->getAs<RecordType>(); | 
|  | 2755 | if (!ObjectTypePtrForLookup && ObjectType->isDependentType()) | 
|  | 2756 | ObjectTypePtrForLookup = Context.DependentTy.getAsOpaquePtr(); | 
|  | 2757 | } | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 2758 |  | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 2759 | // Convert the name of the type being destructed (following the ~) into a | 
|  | 2760 | // type (with source-location information). | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 2761 | QualType DestructedType; | 
|  | 2762 | TypeSourceInfo *DestructedTypeInfo = 0; | 
| Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 2763 | PseudoDestructorTypeStorage Destructed; | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 2764 | if (SecondTypeName.getKind() == UnqualifiedId::IK_Identifier) { | 
|  | 2765 | TypeTy *T = getTypeName(*SecondTypeName.Identifier, | 
|  | 2766 | SecondTypeName.StartLocation, | 
| Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 2767 | S, &SS, true, ObjectTypePtrForLookup); | 
|  | 2768 | if (!T && | 
|  | 2769 | ((SS.isSet() && !computeDeclContext(SS, false)) || | 
|  | 2770 | (!SS.isSet() && ObjectType->isDependentType()))) { | 
|  | 2771 | // The name of the type being destroyed is a dependent name, and we | 
|  | 2772 | // couldn't find anything useful in scope. Just store the identifier and | 
|  | 2773 | // it's location, and we'll perform (qualified) name lookup again at | 
|  | 2774 | // template instantiation time. | 
|  | 2775 | Destructed = PseudoDestructorTypeStorage(SecondTypeName.Identifier, | 
|  | 2776 | SecondTypeName.StartLocation); | 
|  | 2777 | } else if (!T) { | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 2778 | Diag(SecondTypeName.StartLocation, | 
|  | 2779 | diag::err_pseudo_dtor_destructor_non_type) | 
|  | 2780 | << SecondTypeName.Identifier << ObjectType; | 
|  | 2781 | if (isSFINAEContext()) | 
|  | 2782 | return ExprError(); | 
|  | 2783 |  | 
|  | 2784 | // Recover by assuming we had the right type all along. | 
|  | 2785 | DestructedType = ObjectType; | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 2786 | } else | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 2787 | DestructedType = GetTypeFromParser(T, &DestructedTypeInfo); | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 2788 | } else { | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 2789 | // Resolve the template-id to a type. | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 2790 | TemplateIdAnnotation *TemplateId = SecondTypeName.TemplateId; | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 2791 | ASTTemplateArgsPtr TemplateArgsPtr(*this, | 
|  | 2792 | TemplateId->getTemplateArgs(), | 
|  | 2793 | TemplateId->NumArgs); | 
|  | 2794 | TypeResult T = ActOnTemplateIdType(TemplateTy::make(TemplateId->Template), | 
|  | 2795 | TemplateId->TemplateNameLoc, | 
|  | 2796 | TemplateId->LAngleLoc, | 
|  | 2797 | TemplateArgsPtr, | 
|  | 2798 | TemplateId->RAngleLoc); | 
|  | 2799 | if (T.isInvalid() || !T.get()) { | 
|  | 2800 | // Recover by assuming we had the right type all along. | 
|  | 2801 | DestructedType = ObjectType; | 
|  | 2802 | } else | 
|  | 2803 | DestructedType = GetTypeFromParser(T.get(), &DestructedTypeInfo); | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 2804 | } | 
|  | 2805 |  | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 2806 | // If we've performed some kind of recovery, (re-)build the type source | 
|  | 2807 | // information. | 
| Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 2808 | if (!DestructedType.isNull()) { | 
|  | 2809 | if (!DestructedTypeInfo) | 
|  | 2810 | DestructedTypeInfo = Context.getTrivialTypeSourceInfo(DestructedType, | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 2811 | SecondTypeName.StartLocation); | 
| Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 2812 | Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo); | 
|  | 2813 | } | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 2814 |  | 
|  | 2815 | // Convert the name of the scope type (the type prior to '::') into a type. | 
|  | 2816 | TypeSourceInfo *ScopeTypeInfo = 0; | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 2817 | QualType ScopeType; | 
|  | 2818 | if (FirstTypeName.getKind() == UnqualifiedId::IK_TemplateId || | 
|  | 2819 | FirstTypeName.Identifier) { | 
|  | 2820 | if (FirstTypeName.getKind() == UnqualifiedId::IK_Identifier) { | 
|  | 2821 | TypeTy *T = getTypeName(*FirstTypeName.Identifier, | 
|  | 2822 | FirstTypeName.StartLocation, | 
| Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 2823 | S, &SS, false, ObjectTypePtrForLookup); | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 2824 | if (!T) { | 
|  | 2825 | Diag(FirstTypeName.StartLocation, | 
|  | 2826 | diag::err_pseudo_dtor_destructor_non_type) | 
|  | 2827 | << FirstTypeName.Identifier << ObjectType; | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 2828 |  | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 2829 | if (isSFINAEContext()) | 
|  | 2830 | return ExprError(); | 
|  | 2831 |  | 
|  | 2832 | // Just drop this type. It's unnecessary anyway. | 
|  | 2833 | ScopeType = QualType(); | 
|  | 2834 | } else | 
|  | 2835 | ScopeType = GetTypeFromParser(T, &ScopeTypeInfo); | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 2836 | } else { | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 2837 | // Resolve the template-id to a type. | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 2838 | TemplateIdAnnotation *TemplateId = FirstTypeName.TemplateId; | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 2839 | ASTTemplateArgsPtr TemplateArgsPtr(*this, | 
|  | 2840 | TemplateId->getTemplateArgs(), | 
|  | 2841 | TemplateId->NumArgs); | 
|  | 2842 | TypeResult T = ActOnTemplateIdType(TemplateTy::make(TemplateId->Template), | 
|  | 2843 | TemplateId->TemplateNameLoc, | 
|  | 2844 | TemplateId->LAngleLoc, | 
|  | 2845 | TemplateArgsPtr, | 
|  | 2846 | TemplateId->RAngleLoc); | 
|  | 2847 | if (T.isInvalid() || !T.get()) { | 
|  | 2848 | // Recover by dropping this type. | 
|  | 2849 | ScopeType = QualType(); | 
|  | 2850 | } else | 
|  | 2851 | ScopeType = GetTypeFromParser(T.get(), &ScopeTypeInfo); | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 2852 | } | 
|  | 2853 | } | 
| Douglas Gregor | b4a418f | 2010-02-24 23:02:30 +0000 | [diff] [blame] | 2854 |  | 
|  | 2855 | if (!ScopeType.isNull() && !ScopeTypeInfo) | 
|  | 2856 | ScopeTypeInfo = Context.getTrivialTypeSourceInfo(ScopeType, | 
|  | 2857 | FirstTypeName.StartLocation); | 
|  | 2858 |  | 
|  | 2859 |  | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 2860 | return BuildPseudoDestructorExpr(move(Base), OpLoc, OpKind, SS, | 
| Douglas Gregor | fce46ee | 2010-02-24 23:50:37 +0000 | [diff] [blame] | 2861 | ScopeTypeInfo, CCLoc, TildeLoc, | 
| Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 2862 | Destructed, HasTrailingLParen); | 
| Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 2863 | } | 
|  | 2864 |  | 
| Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 2865 | CXXMemberCallExpr *Sema::BuildCXXMemberCallExpr(Expr *Exp, | 
|  | 2866 | CXXMethodDecl *Method) { | 
| Eli Friedman | 772fffa | 2009-12-09 04:53:56 +0000 | [diff] [blame] | 2867 | if (PerformObjectArgumentInitialization(Exp, Method)) | 
|  | 2868 | assert(0 && "Calling BuildCXXMemberCallExpr with invalid call?"); | 
|  | 2869 |  | 
| Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 2870 | MemberExpr *ME = | 
|  | 2871 | new (Context) MemberExpr(Exp, /*IsArrow=*/false, Method, | 
|  | 2872 | SourceLocation(), Method->getType()); | 
| Eli Friedman | 772fffa | 2009-12-09 04:53:56 +0000 | [diff] [blame] | 2873 | QualType ResultType = Method->getResultType().getNonReferenceType(); | 
| Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 2874 | MarkDeclarationReferenced(Exp->getLocStart(), Method); | 
|  | 2875 | CXXMemberCallExpr *CE = | 
|  | 2876 | new (Context) CXXMemberCallExpr(Context, ME, 0, 0, ResultType, | 
|  | 2877 | Exp->getLocEnd()); | 
| Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 2878 | return CE; | 
|  | 2879 | } | 
|  | 2880 |  | 
| Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 2881 | Sema::OwningExprResult Sema::BuildCXXCastArgument(SourceLocation CastLoc, | 
|  | 2882 | QualType Ty, | 
|  | 2883 | CastExpr::CastKind Kind, | 
|  | 2884 | CXXMethodDecl *Method, | 
|  | 2885 | ExprArg Arg) { | 
|  | 2886 | Expr *From = Arg.takeAs<Expr>(); | 
|  | 2887 |  | 
|  | 2888 | switch (Kind) { | 
|  | 2889 | default: assert(0 && "Unhandled cast kind!"); | 
|  | 2890 | case CastExpr::CK_ConstructorConversion: { | 
| Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 2891 | ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this); | 
|  | 2892 |  | 
|  | 2893 | if (CompleteConstructorCall(cast<CXXConstructorDecl>(Method), | 
|  | 2894 | MultiExprArg(*this, (void **)&From, 1), | 
|  | 2895 | CastLoc, ConstructorArgs)) | 
|  | 2896 | return ExprError(); | 
| Anders Carlsson | 4fa2684 | 2009-10-18 21:20:14 +0000 | [diff] [blame] | 2897 |  | 
|  | 2898 | OwningExprResult Result = | 
|  | 2899 | BuildCXXConstructExpr(CastLoc, Ty, cast<CXXConstructorDecl>(Method), | 
|  | 2900 | move_arg(ConstructorArgs)); | 
|  | 2901 | if (Result.isInvalid()) | 
|  | 2902 | return ExprError(); | 
|  | 2903 |  | 
|  | 2904 | return MaybeBindToTemporary(Result.takeAs<Expr>()); | 
| Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 2905 | } | 
|  | 2906 |  | 
|  | 2907 | case CastExpr::CK_UserDefinedConversion: { | 
| Anders Carlsson | aac6e3a | 2009-09-15 07:42:44 +0000 | [diff] [blame] | 2908 | assert(!From->getType()->isPointerType() && "Arg can't have pointer type!"); | 
| Eli Friedman | 772fffa | 2009-12-09 04:53:56 +0000 | [diff] [blame] | 2909 |  | 
| Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 2910 | // Create an implicit call expr that calls it. | 
|  | 2911 | CXXMemberCallExpr *CE = BuildCXXMemberCallExpr(From, Method); | 
| Anders Carlsson | 4fa2684 | 2009-10-18 21:20:14 +0000 | [diff] [blame] | 2912 | return MaybeBindToTemporary(CE); | 
| Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 2913 | } | 
| Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 2914 | } | 
|  | 2915 | } | 
|  | 2916 |  | 
| Anders Carlsson | 165a0a0 | 2009-05-17 18:41:29 +0000 | [diff] [blame] | 2917 | Sema::OwningExprResult Sema::ActOnFinishFullExpr(ExprArg Arg) { | 
|  | 2918 | Expr *FullExpr = Arg.takeAs<Expr>(); | 
| Anders Carlsson | 99ba36d | 2009-06-05 15:38:08 +0000 | [diff] [blame] | 2919 | if (FullExpr) | 
| Anders Carlsson | 0ece491 | 2009-12-15 20:51:39 +0000 | [diff] [blame] | 2920 | FullExpr = MaybeCreateCXXExprWithTemporaries(FullExpr); | 
| Anders Carlsson | ec77387 | 2009-08-25 23:46:41 +0000 | [diff] [blame] | 2921 |  | 
| Anders Carlsson | 165a0a0 | 2009-05-17 18:41:29 +0000 | [diff] [blame] | 2922 | return Owned(FullExpr); | 
|  | 2923 | } |