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