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