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