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