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