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