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 | 4eb4f0f | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 265 | SourceLocation TypeidLoc, |
| 266 | TypeSourceInfo *Operand, |
| 267 | SourceLocation RParenLoc) { |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 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 | 4eb4f0f | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 288 | SourceLocation TypeidLoc, |
| 289 | Expr *E, |
| 290 | SourceLocation RParenLoc) { |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 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 | |
Douglas Gregor | 4eb4f0f | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 346 | if (!CXXTypeInfoDecl) { |
| 347 | IdentifierInfo *TypeInfoII = &PP.getIdentifierTable().get("type_info"); |
| 348 | LookupResult R(*this, TypeInfoII, SourceLocation(), LookupTagName); |
| 349 | LookupQualifiedName(R, getStdNamespace()); |
| 350 | CXXTypeInfoDecl = R.getAsSingle<RecordDecl>(); |
| 351 | if (!CXXTypeInfoDecl) |
| 352 | return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid)); |
| 353 | } |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 354 | |
Douglas Gregor | 4eb4f0f | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 355 | QualType TypeInfoType = Context.getTypeDeclType(CXXTypeInfoDecl); |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 356 | |
| 357 | if (isType) { |
| 358 | // The operand is a type; handle it as such. |
| 359 | TypeSourceInfo *TInfo = 0; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 360 | QualType T = GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrExpr), |
| 361 | &TInfo); |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 362 | if (T.isNull()) |
| 363 | return ExprError(); |
| 364 | |
| 365 | if (!TInfo) |
| 366 | TInfo = Context.getTrivialTypeSourceInfo(T, OpLoc); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 367 | |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 368 | return BuildCXXTypeId(TypeInfoType, OpLoc, TInfo, RParenLoc); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 369 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 370 | |
Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 371 | // The operand is an expression. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 372 | return BuildCXXTypeId(TypeInfoType, OpLoc, (Expr*)TyOrExpr, RParenLoc); |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Francois Pichet | 6915c52 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 375 | /// Retrieve the UuidAttr associated with QT. |
| 376 | static UuidAttr *GetUuidAttrOfType(QualType QT) { |
| 377 | // Optionally remove one level of pointer, reference or array indirection. |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 378 | const Type *Ty = QT.getTypePtr();; |
Francois Pichet | 913b7bf | 2010-12-20 03:51:03 +0000 | [diff] [blame] | 379 | if (QT->isPointerType() || QT->isReferenceType()) |
| 380 | Ty = QT->getPointeeType().getTypePtr(); |
| 381 | else if (QT->isArrayType()) |
| 382 | Ty = cast<ArrayType>(QT)->getElementType().getTypePtr(); |
| 383 | |
Francois Pichet | 6915c52 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 384 | // Loop all class definition and declaration looking for an uuid attribute. |
| 385 | CXXRecordDecl *RD = Ty->getAsCXXRecordDecl(); |
| 386 | while (RD) { |
| 387 | if (UuidAttr *Uuid = RD->getAttr<UuidAttr>()) |
| 388 | return Uuid; |
| 389 | RD = RD->getPreviousDeclaration(); |
| 390 | } |
| 391 | return 0; |
Francois Pichet | 913b7bf | 2010-12-20 03:51:03 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 394 | /// \brief Build a Microsoft __uuidof expression with a type operand. |
| 395 | ExprResult Sema::BuildCXXUuidof(QualType TypeInfoType, |
| 396 | SourceLocation TypeidLoc, |
| 397 | TypeSourceInfo *Operand, |
| 398 | SourceLocation RParenLoc) { |
Francois Pichet | 6915c52 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 399 | if (!Operand->getType()->isDependentType()) { |
| 400 | if (!GetUuidAttrOfType(Operand->getType())) |
| 401 | return ExprError(Diag(TypeidLoc, diag::err_uuidof_without_guid)); |
| 402 | } |
| 403 | |
Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 404 | // FIXME: add __uuidof semantic analysis for type operand. |
| 405 | return Owned(new (Context) CXXUuidofExpr(TypeInfoType.withConst(), |
| 406 | Operand, |
| 407 | SourceRange(TypeidLoc, RParenLoc))); |
| 408 | } |
| 409 | |
| 410 | /// \brief Build a Microsoft __uuidof expression with an expression operand. |
| 411 | ExprResult Sema::BuildCXXUuidof(QualType TypeInfoType, |
| 412 | SourceLocation TypeidLoc, |
| 413 | Expr *E, |
| 414 | SourceLocation RParenLoc) { |
Francois Pichet | 6915c52 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 415 | if (!E->getType()->isDependentType()) { |
| 416 | if (!GetUuidAttrOfType(E->getType()) && |
| 417 | !E->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) |
| 418 | return ExprError(Diag(TypeidLoc, diag::err_uuidof_without_guid)); |
| 419 | } |
| 420 | // FIXME: add __uuidof semantic analysis for type operand. |
Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 421 | return Owned(new (Context) CXXUuidofExpr(TypeInfoType.withConst(), |
| 422 | E, |
| 423 | SourceRange(TypeidLoc, RParenLoc))); |
| 424 | } |
| 425 | |
| 426 | /// ActOnCXXUuidof - Parse __uuidof( type-id ) or __uuidof (expression); |
| 427 | ExprResult |
| 428 | Sema::ActOnCXXUuidof(SourceLocation OpLoc, SourceLocation LParenLoc, |
| 429 | bool isType, void *TyOrExpr, SourceLocation RParenLoc) { |
| 430 | // If MSVCGuidDecl has not been cached, do the lookup. |
| 431 | if (!MSVCGuidDecl) { |
| 432 | IdentifierInfo *GuidII = &PP.getIdentifierTable().get("_GUID"); |
| 433 | LookupResult R(*this, GuidII, SourceLocation(), LookupTagName); |
| 434 | LookupQualifiedName(R, Context.getTranslationUnitDecl()); |
| 435 | MSVCGuidDecl = R.getAsSingle<RecordDecl>(); |
| 436 | if (!MSVCGuidDecl) |
| 437 | return ExprError(Diag(OpLoc, diag::err_need_header_before_ms_uuidof)); |
| 438 | } |
| 439 | |
| 440 | QualType GuidType = Context.getTypeDeclType(MSVCGuidDecl); |
| 441 | |
| 442 | if (isType) { |
| 443 | // The operand is a type; handle it as such. |
| 444 | TypeSourceInfo *TInfo = 0; |
| 445 | QualType T = GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrExpr), |
| 446 | &TInfo); |
| 447 | if (T.isNull()) |
| 448 | return ExprError(); |
| 449 | |
| 450 | if (!TInfo) |
| 451 | TInfo = Context.getTrivialTypeSourceInfo(T, OpLoc); |
| 452 | |
| 453 | return BuildCXXUuidof(GuidType, OpLoc, TInfo, RParenLoc); |
| 454 | } |
| 455 | |
| 456 | // The operand is an expression. |
| 457 | return BuildCXXUuidof(GuidType, OpLoc, (Expr*)TyOrExpr, RParenLoc); |
| 458 | } |
| 459 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 460 | /// ActOnCXXBoolLiteral - Parse {true,false} literals. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 461 | ExprResult |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 462 | Sema::ActOnCXXBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) { |
Douglas Gregor | 2f639b9 | 2008-10-24 15:36:09 +0000 | [diff] [blame] | 463 | assert((Kind == tok::kw_true || Kind == tok::kw_false) && |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 464 | "Unknown C++ Boolean value!"); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 465 | return Owned(new (Context) CXXBoolLiteralExpr(Kind == tok::kw_true, |
| 466 | Context.BoolTy, OpLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 467 | } |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 468 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 469 | /// ActOnCXXNullPtrLiteral - Parse 'nullptr'. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 470 | ExprResult |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 471 | Sema::ActOnCXXNullPtrLiteral(SourceLocation Loc) { |
| 472 | return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc)); |
| 473 | } |
| 474 | |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 475 | /// ActOnCXXThrow - Parse throw expressions. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 476 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 477 | Sema::ActOnCXXThrow(SourceLocation OpLoc, Expr *Ex) { |
Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 478 | if (Ex && !Ex->isTypeDependent() && CheckCXXThrowOperand(OpLoc, Ex)) |
| 479 | return ExprError(); |
| 480 | return Owned(new (Context) CXXThrowExpr(Ex, Context.VoidTy, OpLoc)); |
| 481 | } |
| 482 | |
| 483 | /// CheckCXXThrowOperand - Validate the operand of a throw. |
| 484 | bool Sema::CheckCXXThrowOperand(SourceLocation ThrowLoc, Expr *&E) { |
| 485 | // C++ [except.throw]p3: |
Douglas Gregor | 154fe98 | 2009-12-23 22:04:40 +0000 | [diff] [blame] | 486 | // A throw-expression initializes a temporary object, called the exception |
| 487 | // object, the type of which is determined by removing any top-level |
| 488 | // cv-qualifiers from the static type of the operand of throw and adjusting |
| 489 | // the type from "array of T" or "function returning T" to "pointer to T" |
| 490 | // or "pointer to function returning T", [...] |
| 491 | if (E->getType().hasQualifiers()) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 492 | ImpCastExprToType(E, E->getType().getUnqualifiedType(), CK_NoOp, |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 493 | CastCategory(E)); |
Douglas Gregor | 154fe98 | 2009-12-23 22:04:40 +0000 | [diff] [blame] | 494 | |
Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 495 | DefaultFunctionArrayConversion(E); |
| 496 | |
| 497 | // If the type of the exception would be an incomplete type or a pointer |
| 498 | // to an incomplete type other than (cv) void the program is ill-formed. |
| 499 | QualType Ty = E->getType(); |
John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 500 | bool isPointer = false; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 501 | if (const PointerType* Ptr = Ty->getAs<PointerType>()) { |
Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 502 | Ty = Ptr->getPointeeType(); |
John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 503 | isPointer = true; |
Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 504 | } |
| 505 | if (!isPointer || !Ty->isVoidType()) { |
| 506 | if (RequireCompleteType(ThrowLoc, Ty, |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 507 | PDiag(isPointer ? diag::err_throw_incomplete_ptr |
| 508 | : diag::err_throw_incomplete) |
| 509 | << E->getSourceRange())) |
Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 510 | return true; |
Rafael Espindola | 7b9a5aa | 2010-03-02 21:28:26 +0000 | [diff] [blame] | 511 | |
Douglas Gregor | bf422f9 | 2010-04-15 18:05:39 +0000 | [diff] [blame] | 512 | if (RequireNonAbstractType(ThrowLoc, E->getType(), |
| 513 | PDiag(diag::err_throw_abstract_type) |
| 514 | << E->getSourceRange())) |
| 515 | return true; |
Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 516 | } |
| 517 | |
John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 518 | // Initialize the exception result. This implicitly weeds out |
| 519 | // abstract types or types with inaccessible copy constructors. |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 520 | // 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] | 521 | InitializedEntity Entity = |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 522 | InitializedEntity::InitializeException(ThrowLoc, E->getType(), |
| 523 | /*NRVO=*/false); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 524 | ExprResult Res = PerformCopyInitialization(Entity, |
John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 525 | SourceLocation(), |
| 526 | Owned(E)); |
| 527 | if (Res.isInvalid()) |
| 528 | return true; |
| 529 | E = Res.takeAs<Expr>(); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 530 | |
Eli Friedman | 5ed9b93 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 531 | // If the exception has class type, we need additional handling. |
| 532 | const RecordType *RecordTy = Ty->getAs<RecordType>(); |
| 533 | if (!RecordTy) |
| 534 | return false; |
| 535 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 536 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 537 | // If we are throwing a polymorphic class type or pointer thereof, |
| 538 | // exception handling will make use of the vtable. |
Eli Friedman | 5ed9b93 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 539 | MarkVTableUsed(ThrowLoc, RD); |
| 540 | |
Eli Friedman | 98efb9f | 2010-10-12 20:32:36 +0000 | [diff] [blame] | 541 | // If a pointer is thrown, the referenced object will not be destroyed. |
| 542 | if (isPointer) |
| 543 | return false; |
| 544 | |
Eli Friedman | 5ed9b93 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 545 | // If the class has a non-trivial destructor, we must be able to call it. |
| 546 | if (RD->hasTrivialDestructor()) |
| 547 | return false; |
| 548 | |
Douglas Gregor | 1d110e0 | 2010-07-01 14:13:13 +0000 | [diff] [blame] | 549 | CXXDestructorDecl *Destructor |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 550 | = const_cast<CXXDestructorDecl*>(LookupDestructor(RD)); |
Eli Friedman | 5ed9b93 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 551 | if (!Destructor) |
| 552 | return false; |
| 553 | |
| 554 | MarkDeclarationReferenced(E->getExprLoc(), Destructor); |
| 555 | CheckDestructorAccess(E->getExprLoc(), Destructor, |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 556 | PDiag(diag::err_access_dtor_exception) << Ty); |
Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 557 | return false; |
Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 558 | } |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 559 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 560 | ExprResult Sema::ActOnCXXThis(SourceLocation ThisLoc) { |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 561 | /// C++ 9.3.2: In the body of a non-static member function, the keyword this |
| 562 | /// is a non-lvalue expression whose value is the address of the object for |
| 563 | /// which the function is called. |
| 564 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 565 | DeclContext *DC = getFunctionLevelDeclContext(); |
| 566 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(DC)) |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 567 | if (MD->isInstance()) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 568 | return Owned(new (Context) CXXThisExpr(ThisLoc, |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 569 | MD->getThisType(Context), |
| 570 | /*isImplicit=*/false)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 571 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 572 | return ExprError(Diag(ThisLoc, diag::err_invalid_this_use)); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 573 | } |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 574 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 575 | ExprResult |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 576 | Sema::ActOnCXXTypeConstructExpr(ParsedType TypeRep, |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 577 | SourceLocation LParenLoc, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 578 | MultiExprArg exprs, |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 579 | SourceLocation RParenLoc) { |
Douglas Gregor | ae4c77d | 2010-02-05 19:11:37 +0000 | [diff] [blame] | 580 | if (!TypeRep) |
| 581 | return ExprError(); |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 582 | |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 583 | TypeSourceInfo *TInfo; |
| 584 | QualType Ty = GetTypeFromParser(TypeRep, &TInfo); |
| 585 | if (!TInfo) |
| 586 | TInfo = Context.getTrivialTypeSourceInfo(Ty, SourceLocation()); |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 587 | |
| 588 | return BuildCXXTypeConstructExpr(TInfo, LParenLoc, exprs, RParenLoc); |
| 589 | } |
| 590 | |
| 591 | /// ActOnCXXTypeConstructExpr - Parse construction of a specified type. |
| 592 | /// Can be interpreted either as function-style casting ("int(x)") |
| 593 | /// or class type construction ("ClassType(x,y,z)") |
| 594 | /// or creation of a value-initialized type ("int()"). |
| 595 | ExprResult |
| 596 | Sema::BuildCXXTypeConstructExpr(TypeSourceInfo *TInfo, |
| 597 | SourceLocation LParenLoc, |
| 598 | MultiExprArg exprs, |
| 599 | SourceLocation RParenLoc) { |
| 600 | QualType Ty = TInfo->getType(); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 601 | unsigned NumExprs = exprs.size(); |
| 602 | Expr **Exprs = (Expr**)exprs.get(); |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 603 | SourceLocation TyBeginLoc = TInfo->getTypeLoc().getBeginLoc(); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 604 | SourceRange FullRange = SourceRange(TyBeginLoc, RParenLoc); |
| 605 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 606 | if (Ty->isDependentType() || |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 607 | CallExpr::hasAnyTypeDependentArguments(Exprs, NumExprs)) { |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 608 | exprs.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 609 | |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 610 | return Owned(CXXUnresolvedConstructExpr::Create(Context, TInfo, |
Douglas Gregor | d81e6ca | 2009-05-20 18:46:25 +0000 | [diff] [blame] | 611 | LParenLoc, |
| 612 | Exprs, NumExprs, |
| 613 | RParenLoc)); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 614 | } |
| 615 | |
Anders Carlsson | bb60a50 | 2009-08-27 03:53:50 +0000 | [diff] [blame] | 616 | if (Ty->isArrayType()) |
| 617 | return ExprError(Diag(TyBeginLoc, |
| 618 | diag::err_value_init_for_array_type) << FullRange); |
| 619 | if (!Ty->isVoidType() && |
| 620 | RequireCompleteType(TyBeginLoc, Ty, |
| 621 | PDiag(diag::err_invalid_incomplete_type_use) |
| 622 | << FullRange)) |
| 623 | return ExprError(); |
Fariborz Jahanian | f071e9b | 2009-10-23 21:01:39 +0000 | [diff] [blame] | 624 | |
Anders Carlsson | bb60a50 | 2009-08-27 03:53:50 +0000 | [diff] [blame] | 625 | if (RequireNonAbstractType(TyBeginLoc, Ty, |
| 626 | diag::err_allocation_of_abstract_type)) |
| 627 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 628 | |
| 629 | |
Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 630 | // C++ [expr.type.conv]p1: |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 631 | // If the expression list is a single expression, the type conversion |
| 632 | // expression is equivalent (in definedness, and if defined in meaning) to the |
| 633 | // corresponding cast expression. |
| 634 | // |
| 635 | if (NumExprs == 1) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 636 | CastKind Kind = CK_Invalid; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 637 | ExprValueKind VK = VK_RValue; |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 638 | CXXCastPath BasePath; |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 639 | if (CheckCastTypes(TInfo->getTypeLoc().getSourceRange(), Ty, Exprs[0], |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 640 | Kind, VK, BasePath, |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 641 | /*FunctionalStyle=*/true)) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 642 | return ExprError(); |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 643 | |
| 644 | exprs.release(); |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 645 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 646 | return Owned(CXXFunctionalCastExpr::Create(Context, |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 647 | Ty.getNonLValueExprType(Context), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 648 | VK, TInfo, TyBeginLoc, Kind, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 649 | Exprs[0], &BasePath, |
| 650 | RParenLoc)); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 651 | } |
| 652 | |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 653 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(TInfo); |
| 654 | InitializationKind Kind |
| 655 | = NumExprs ? InitializationKind::CreateDirect(TyBeginLoc, |
| 656 | LParenLoc, RParenLoc) |
| 657 | : InitializationKind::CreateValue(TyBeginLoc, |
| 658 | LParenLoc, RParenLoc); |
| 659 | InitializationSequence InitSeq(*this, Entity, Kind, Exprs, NumExprs); |
| 660 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, move(exprs)); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 661 | |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 662 | // FIXME: Improve AST representation? |
| 663 | return move(Result); |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 664 | } |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 665 | |
| 666 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 667 | /// ActOnCXXNew - Parsed a C++ 'new' expression (C++ 5.3.4), as in e.g.: |
| 668 | /// @code new (memory) int[size][4] @endcode |
| 669 | /// or |
| 670 | /// @code ::new Foo(23, "hello") @endcode |
| 671 | /// For the interpretation of this heap of arguments, consult the base version. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 672 | ExprResult |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 673 | Sema::ActOnCXXNew(SourceLocation StartLoc, bool UseGlobal, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 674 | SourceLocation PlacementLParen, MultiExprArg PlacementArgs, |
Douglas Gregor | 4bd4031 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 675 | SourceLocation PlacementRParen, SourceRange TypeIdParens, |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 676 | Declarator &D, SourceLocation ConstructorLParen, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 677 | MultiExprArg ConstructorArgs, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 678 | SourceLocation ConstructorRParen) { |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 679 | Expr *ArraySize = 0; |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 680 | // If the specified type is an array, unwrap it and save the expression. |
| 681 | if (D.getNumTypeObjects() > 0 && |
| 682 | D.getTypeObject(0).Kind == DeclaratorChunk::Array) { |
| 683 | DeclaratorChunk &Chunk = D.getTypeObject(0); |
| 684 | if (Chunk.Arr.hasStatic) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 685 | return ExprError(Diag(Chunk.Loc, diag::err_static_illegal_in_new) |
| 686 | << D.getSourceRange()); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 687 | if (!Chunk.Arr.NumElts) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 688 | return ExprError(Diag(Chunk.Loc, diag::err_array_new_needs_size) |
| 689 | << D.getSourceRange()); |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 690 | |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 691 | ArraySize = static_cast<Expr*>(Chunk.Arr.NumElts); |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 692 | D.DropFirstTypeObject(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 693 | } |
| 694 | |
Douglas Gregor | 043cad2 | 2009-09-11 00:18:58 +0000 | [diff] [blame] | 695 | // Every dimension shall be of constant size. |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 696 | if (ArraySize) { |
| 697 | for (unsigned I = 0, N = D.getNumTypeObjects(); I < N; ++I) { |
Douglas Gregor | 043cad2 | 2009-09-11 00:18:58 +0000 | [diff] [blame] | 698 | if (D.getTypeObject(I).Kind != DeclaratorChunk::Array) |
| 699 | break; |
| 700 | |
| 701 | DeclaratorChunk::ArrayTypeInfo &Array = D.getTypeObject(I).Arr; |
| 702 | if (Expr *NumElts = (Expr *)Array.NumElts) { |
| 703 | if (!NumElts->isTypeDependent() && !NumElts->isValueDependent() && |
| 704 | !NumElts->isIntegerConstantExpr(Context)) { |
| 705 | Diag(D.getTypeObject(I).Loc, diag::err_new_array_nonconst) |
| 706 | << NumElts->getSourceRange(); |
| 707 | return ExprError(); |
| 708 | } |
| 709 | } |
| 710 | } |
| 711 | } |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 712 | |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 713 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, /*Scope=*/0); |
| 714 | QualType AllocType = TInfo->getType(); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 715 | if (D.isInvalidType()) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 716 | return ExprError(); |
Ted Kremenek | f9d5bac | 2010-06-25 22:48:49 +0000 | [diff] [blame] | 717 | |
Douglas Gregor | 1bb2a93 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 718 | if (!TInfo) |
| 719 | TInfo = Context.getTrivialTypeSourceInfo(AllocType); |
| 720 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 721 | return BuildCXXNew(StartLoc, UseGlobal, |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 722 | PlacementLParen, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 723 | move(PlacementArgs), |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 724 | PlacementRParen, |
Douglas Gregor | 4bd4031 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 725 | TypeIdParens, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 726 | AllocType, |
Douglas Gregor | 1bb2a93 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 727 | TInfo, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 728 | ArraySize, |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 729 | ConstructorLParen, |
| 730 | move(ConstructorArgs), |
| 731 | ConstructorRParen); |
| 732 | } |
| 733 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 734 | ExprResult |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 735 | Sema::BuildCXXNew(SourceLocation StartLoc, bool UseGlobal, |
| 736 | SourceLocation PlacementLParen, |
| 737 | MultiExprArg PlacementArgs, |
| 738 | SourceLocation PlacementRParen, |
Douglas Gregor | 4bd4031 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 739 | SourceRange TypeIdParens, |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 740 | QualType AllocType, |
Douglas Gregor | 1bb2a93 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 741 | TypeSourceInfo *AllocTypeInfo, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 742 | Expr *ArraySize, |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 743 | SourceLocation ConstructorLParen, |
| 744 | MultiExprArg ConstructorArgs, |
| 745 | SourceLocation ConstructorRParen) { |
Douglas Gregor | 1bb2a93 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 746 | SourceRange TypeRange = AllocTypeInfo->getTypeLoc().getSourceRange(); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 747 | |
Douglas Gregor | 3caf04e | 2010-05-16 16:01:03 +0000 | [diff] [blame] | 748 | // Per C++0x [expr.new]p5, the type being constructed may be a |
| 749 | // typedef of an array type. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 750 | if (!ArraySize) { |
Douglas Gregor | 3caf04e | 2010-05-16 16:01:03 +0000 | [diff] [blame] | 751 | if (const ConstantArrayType *Array |
| 752 | = Context.getAsConstantArrayType(AllocType)) { |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 753 | ArraySize = IntegerLiteral::Create(Context, Array->getSize(), |
| 754 | Context.getSizeType(), |
| 755 | TypeRange.getEnd()); |
Douglas Gregor | 3caf04e | 2010-05-16 16:01:03 +0000 | [diff] [blame] | 756 | AllocType = Array->getElementType(); |
| 757 | } |
| 758 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 759 | |
Douglas Gregor | a075076 | 2010-10-06 16:00:31 +0000 | [diff] [blame] | 760 | if (CheckAllocatedType(AllocType, TypeRange.getBegin(), TypeRange)) |
| 761 | return ExprError(); |
| 762 | |
Douglas Gregor | 3caf04e | 2010-05-16 16:01:03 +0000 | [diff] [blame] | 763 | QualType ResultType = Context.getPointerType(AllocType); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 764 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 765 | // C++ 5.3.4p6: "The expression in a direct-new-declarator shall have integral |
| 766 | // or enumeration type with a non-negative value." |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 767 | if (ArraySize && !ArraySize->isTypeDependent()) { |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 768 | |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 769 | QualType SizeType = ArraySize->getType(); |
Douglas Gregor | c30614b | 2010-06-29 23:17:37 +0000 | [diff] [blame] | 770 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 771 | ExprResult ConvertedSize |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 772 | = ConvertToIntegralOrEnumerationType(StartLoc, ArraySize, |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 773 | PDiag(diag::err_array_size_not_integral), |
| 774 | PDiag(diag::err_array_size_incomplete_type) |
| 775 | << ArraySize->getSourceRange(), |
| 776 | PDiag(diag::err_array_size_explicit_conversion), |
| 777 | PDiag(diag::note_array_size_conversion), |
| 778 | PDiag(diag::err_array_size_ambiguous_conversion), |
| 779 | PDiag(diag::note_array_size_conversion), |
| 780 | PDiag(getLangOptions().CPlusPlus0x? 0 |
| 781 | : diag::ext_array_size_conversion)); |
| 782 | if (ConvertedSize.isInvalid()) |
| 783 | return ExprError(); |
| 784 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 785 | ArraySize = ConvertedSize.take(); |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 786 | SizeType = ArraySize->getType(); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 787 | if (!SizeType->isIntegralOrUnscopedEnumerationType()) |
Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 788 | return ExprError(); |
| 789 | |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 790 | // Let's see if this is a constant < 0. If so, we reject it out of hand. |
| 791 | // We don't care about special rules, so we tell the machinery it's not |
| 792 | // evaluated - it gives us a result in more cases. |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 793 | if (!ArraySize->isValueDependent()) { |
| 794 | llvm::APSInt Value; |
| 795 | if (ArraySize->isIntegerConstantExpr(Value, Context, 0, false)) { |
| 796 | if (Value < llvm::APSInt( |
Anders Carlsson | ac18b2e | 2009-09-23 00:37:25 +0000 | [diff] [blame] | 797 | llvm::APInt::getNullValue(Value.getBitWidth()), |
| 798 | Value.isUnsigned())) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 799 | return ExprError(Diag(ArraySize->getSourceRange().getBegin(), |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 800 | diag::err_typecheck_negative_array_size) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 801 | << ArraySize->getSourceRange()); |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 802 | |
| 803 | if (!AllocType->isDependentType()) { |
| 804 | unsigned ActiveSizeBits |
| 805 | = ConstantArrayType::getNumAddressingBits(Context, AllocType, Value); |
| 806 | if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context)) { |
| 807 | Diag(ArraySize->getSourceRange().getBegin(), |
| 808 | diag::err_array_too_large) |
| 809 | << Value.toString(10) |
| 810 | << ArraySize->getSourceRange(); |
| 811 | return ExprError(); |
| 812 | } |
| 813 | } |
Douglas Gregor | 4bd4031 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 814 | } else if (TypeIdParens.isValid()) { |
| 815 | // Can't have dynamic array size when the type-id is in parentheses. |
| 816 | Diag(ArraySize->getLocStart(), diag::ext_new_paren_array_nonconst) |
| 817 | << ArraySize->getSourceRange() |
| 818 | << FixItHint::CreateRemoval(TypeIdParens.getBegin()) |
| 819 | << FixItHint::CreateRemoval(TypeIdParens.getEnd()); |
| 820 | |
| 821 | TypeIdParens = SourceRange(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 822 | } |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 823 | } |
Anders Carlsson | ac18b2e | 2009-09-23 00:37:25 +0000 | [diff] [blame] | 824 | |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 825 | ImpCastExprToType(ArraySize, Context.getSizeType(), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 826 | CK_IntegralCast); |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 827 | } |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 828 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 829 | FunctionDecl *OperatorNew = 0; |
| 830 | FunctionDecl *OperatorDelete = 0; |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 831 | Expr **PlaceArgs = (Expr**)PlacementArgs.get(); |
| 832 | unsigned NumPlaceArgs = PlacementArgs.size(); |
Fariborz Jahanian | 498429f | 2009-11-19 18:39:40 +0000 | [diff] [blame] | 833 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 834 | if (!AllocType->isDependentType() && |
| 835 | !Expr::hasAnyTypeDependentArguments(PlaceArgs, NumPlaceArgs) && |
| 836 | FindAllocationFunctions(StartLoc, |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 837 | SourceRange(PlacementLParen, PlacementRParen), |
| 838 | UseGlobal, AllocType, ArraySize, PlaceArgs, |
| 839 | NumPlaceArgs, OperatorNew, OperatorDelete)) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 840 | return ExprError(); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 841 | llvm::SmallVector<Expr *, 8> AllPlaceArgs; |
Fariborz Jahanian | 498429f | 2009-11-19 18:39:40 +0000 | [diff] [blame] | 842 | if (OperatorNew) { |
| 843 | // Add default arguments, if any. |
| 844 | const FunctionProtoType *Proto = |
| 845 | OperatorNew->getType()->getAs<FunctionProtoType>(); |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 846 | VariadicCallType CallType = |
| 847 | Proto->isVariadic() ? VariadicFunction : VariadicDoesNotApply; |
Anders Carlsson | 28e9483 | 2010-05-03 02:07:56 +0000 | [diff] [blame] | 848 | |
| 849 | if (GatherArgumentsForCall(PlacementLParen, OperatorNew, |
| 850 | Proto, 1, PlaceArgs, NumPlaceArgs, |
| 851 | AllPlaceArgs, CallType)) |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 852 | return ExprError(); |
Fariborz Jahanian | 498429f | 2009-11-19 18:39:40 +0000 | [diff] [blame] | 853 | |
Fariborz Jahanian | 498429f | 2009-11-19 18:39:40 +0000 | [diff] [blame] | 854 | NumPlaceArgs = AllPlaceArgs.size(); |
| 855 | if (NumPlaceArgs > 0) |
| 856 | PlaceArgs = &AllPlaceArgs[0]; |
| 857 | } |
| 858 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 859 | bool Init = ConstructorLParen.isValid(); |
| 860 | // --- Choosing a constructor --- |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 861 | CXXConstructorDecl *Constructor = 0; |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 862 | Expr **ConsArgs = (Expr**)ConstructorArgs.get(); |
| 863 | unsigned NumConsArgs = ConstructorArgs.size(); |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 864 | ASTOwningVector<Expr*> ConvertedConstructorArgs(*this); |
Eli Friedman | a8ce9ec | 2009-11-08 22:15:39 +0000 | [diff] [blame] | 865 | |
Anders Carlsson | 48c9501 | 2010-05-03 15:45:23 +0000 | [diff] [blame] | 866 | // Array 'new' can't have any initializers. |
Anders Carlsson | 55cbd6e | 2010-05-16 16:24:20 +0000 | [diff] [blame] | 867 | if (NumConsArgs && (ResultType->isArrayType() || ArraySize)) { |
Anders Carlsson | 48c9501 | 2010-05-03 15:45:23 +0000 | [diff] [blame] | 868 | SourceRange InitRange(ConsArgs[0]->getLocStart(), |
| 869 | ConsArgs[NumConsArgs - 1]->getLocEnd()); |
| 870 | |
| 871 | Diag(StartLoc, diag::err_new_array_init_args) << InitRange; |
| 872 | return ExprError(); |
| 873 | } |
| 874 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 875 | if (!AllocType->isDependentType() && |
| 876 | !Expr::hasAnyTypeDependentArguments(ConsArgs, NumConsArgs)) { |
| 877 | // C++0x [expr.new]p15: |
| 878 | // A new-expression that creates an object of type T initializes that |
| 879 | // object as follows: |
| 880 | InitializationKind Kind |
| 881 | // - If the new-initializer is omitted, the object is default- |
| 882 | // initialized (8.5); if no initialization is performed, |
| 883 | // the object has indeterminate value |
Douglas Gregor | 1bb2a93 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 884 | = !Init? InitializationKind::CreateDefault(TypeRange.getBegin()) |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 885 | // - Otherwise, the new-initializer is interpreted according to the |
| 886 | // initialization rules of 8.5 for direct-initialization. |
Douglas Gregor | 1bb2a93 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 887 | : InitializationKind::CreateDirect(TypeRange.getBegin(), |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 888 | ConstructorLParen, |
| 889 | ConstructorRParen); |
| 890 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 891 | InitializedEntity Entity |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 892 | = InitializedEntity::InitializeNew(StartLoc, AllocType); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 893 | InitializationSequence InitSeq(*this, Entity, Kind, ConsArgs, NumConsArgs); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 894 | ExprResult FullInit = InitSeq.Perform(*this, Entity, Kind, |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 895 | move(ConstructorArgs)); |
| 896 | if (FullInit.isInvalid()) |
| 897 | return ExprError(); |
| 898 | |
| 899 | // FullInit is our initializer; walk through it to determine if it's a |
| 900 | // constructor call, which CXXNewExpr handles directly. |
| 901 | if (Expr *FullInitExpr = (Expr *)FullInit.get()) { |
| 902 | if (CXXBindTemporaryExpr *Binder |
| 903 | = dyn_cast<CXXBindTemporaryExpr>(FullInitExpr)) |
| 904 | FullInitExpr = Binder->getSubExpr(); |
| 905 | if (CXXConstructExpr *Construct |
| 906 | = dyn_cast<CXXConstructExpr>(FullInitExpr)) { |
| 907 | Constructor = Construct->getConstructor(); |
| 908 | for (CXXConstructExpr::arg_iterator A = Construct->arg_begin(), |
| 909 | AEnd = Construct->arg_end(); |
| 910 | A != AEnd; ++A) |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 911 | ConvertedConstructorArgs.push_back(*A); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 912 | } else { |
| 913 | // Take the converted initializer. |
| 914 | ConvertedConstructorArgs.push_back(FullInit.release()); |
| 915 | } |
| 916 | } else { |
| 917 | // No initialization required. |
| 918 | } |
| 919 | |
| 920 | // Take the converted arguments and use them for the new expression. |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 921 | NumConsArgs = ConvertedConstructorArgs.size(); |
| 922 | ConsArgs = (Expr **)ConvertedConstructorArgs.take(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 923 | } |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 924 | |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 925 | // Mark the new and delete operators as referenced. |
| 926 | if (OperatorNew) |
| 927 | MarkDeclarationReferenced(StartLoc, OperatorNew); |
| 928 | if (OperatorDelete) |
| 929 | MarkDeclarationReferenced(StartLoc, OperatorDelete); |
| 930 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 931 | // FIXME: Also check that the destructor is accessible. (C++ 5.3.4p16) |
Douglas Gregor | 089407b | 2009-10-17 21:40:42 +0000 | [diff] [blame] | 932 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 933 | PlacementArgs.release(); |
| 934 | ConstructorArgs.release(); |
Ted Kremenek | f9d5bac | 2010-06-25 22:48:49 +0000 | [diff] [blame] | 935 | |
Ted Kremenek | ad7fe86 | 2010-02-11 22:51:03 +0000 | [diff] [blame] | 936 | return Owned(new (Context) CXXNewExpr(Context, UseGlobal, OperatorNew, |
Douglas Gregor | 4bd4031 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 937 | PlaceArgs, NumPlaceArgs, TypeIdParens, |
Ted Kremenek | ad7fe86 | 2010-02-11 22:51:03 +0000 | [diff] [blame] | 938 | ArraySize, Constructor, Init, |
| 939 | ConsArgs, NumConsArgs, OperatorDelete, |
Douglas Gregor | 1bb2a93 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 940 | ResultType, AllocTypeInfo, |
| 941 | StartLoc, |
Ted Kremenek | ad7fe86 | 2010-02-11 22:51:03 +0000 | [diff] [blame] | 942 | Init ? ConstructorRParen : |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 943 | TypeRange.getEnd(), |
| 944 | ConstructorLParen, ConstructorRParen)); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | /// CheckAllocatedType - Checks that a type is suitable as the allocated type |
| 948 | /// in a new-expression. |
| 949 | /// dimension off and stores the size expression in ArraySize. |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 950 | bool Sema::CheckAllocatedType(QualType AllocType, SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 951 | SourceRange R) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 952 | // C++ 5.3.4p1: "[The] type shall be a complete object type, but not an |
| 953 | // abstract class type or array thereof. |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 954 | if (AllocType->isFunctionType()) |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 955 | return Diag(Loc, diag::err_bad_new_type) |
| 956 | << AllocType << 0 << R; |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 957 | else if (AllocType->isReferenceType()) |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 958 | return Diag(Loc, diag::err_bad_new_type) |
| 959 | << AllocType << 1 << R; |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 960 | else if (!AllocType->isDependentType() && |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 961 | RequireCompleteType(Loc, AllocType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 962 | PDiag(diag::err_new_incomplete_type) |
| 963 | << R)) |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 964 | return true; |
Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 965 | else if (RequireNonAbstractType(Loc, AllocType, |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 966 | diag::err_allocation_of_abstract_type)) |
| 967 | return true; |
Douglas Gregor | a075076 | 2010-10-06 16:00:31 +0000 | [diff] [blame] | 968 | else if (AllocType->isVariablyModifiedType()) |
| 969 | return Diag(Loc, diag::err_variably_modified_new_type) |
| 970 | << AllocType; |
| 971 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 972 | return false; |
| 973 | } |
| 974 | |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 975 | /// \brief Determine whether the given function is a non-placement |
| 976 | /// deallocation function. |
| 977 | static bool isNonPlacementDeallocationFunction(FunctionDecl *FD) { |
| 978 | if (FD->isInvalidDecl()) |
| 979 | return false; |
| 980 | |
| 981 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FD)) |
| 982 | return Method->isUsualDeallocationFunction(); |
| 983 | |
| 984 | return ((FD->getOverloadedOperator() == OO_Delete || |
| 985 | FD->getOverloadedOperator() == OO_Array_Delete) && |
| 986 | FD->getNumParams() == 1); |
| 987 | } |
| 988 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 989 | /// FindAllocationFunctions - Finds the overloads of operator new and delete |
| 990 | /// that are appropriate for the allocation. |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 991 | bool Sema::FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range, |
| 992 | bool UseGlobal, QualType AllocType, |
| 993 | bool IsArray, Expr **PlaceArgs, |
| 994 | unsigned NumPlaceArgs, |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 995 | FunctionDecl *&OperatorNew, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 996 | FunctionDecl *&OperatorDelete) { |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 997 | // --- Choosing an allocation function --- |
| 998 | // C++ 5.3.4p8 - 14 & 18 |
| 999 | // 1) If UseGlobal is true, only look in the global scope. Else, also look |
| 1000 | // in the scope of the allocated class. |
| 1001 | // 2) If an array size is given, look for operator new[], else look for |
| 1002 | // operator new. |
| 1003 | // 3) The first argument is always size_t. Append the arguments from the |
| 1004 | // placement form. |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1005 | |
| 1006 | llvm::SmallVector<Expr*, 8> AllocArgs(1 + NumPlaceArgs); |
| 1007 | // We don't care about the actual value of this argument. |
| 1008 | // FIXME: Should the Sema create the expression and embed it in the syntax |
| 1009 | // tree? Or should the consumer just recalculate the value? |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 1010 | IntegerLiteral Size(Context, llvm::APInt::getNullValue( |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1011 | Context.Target.getPointerWidth(0)), |
| 1012 | Context.getSizeType(), |
| 1013 | SourceLocation()); |
| 1014 | AllocArgs[0] = &Size; |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1015 | std::copy(PlaceArgs, PlaceArgs + NumPlaceArgs, AllocArgs.begin() + 1); |
| 1016 | |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1017 | // C++ [expr.new]p8: |
| 1018 | // If the allocated type is a non-array type, the allocation |
| 1019 | // function’s name is operator new and the deallocation function’s |
| 1020 | // name is operator delete. If the allocated type is an array |
| 1021 | // type, the allocation function’s name is operator new[] and the |
| 1022 | // deallocation function’s name is operator delete[]. |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1023 | DeclarationName NewName = Context.DeclarationNames.getCXXOperatorName( |
| 1024 | IsArray ? OO_Array_New : OO_New); |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1025 | DeclarationName DeleteName = Context.DeclarationNames.getCXXOperatorName( |
| 1026 | IsArray ? OO_Array_Delete : OO_Delete); |
| 1027 | |
Argyrios Kyrtzidis | d293298 | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 1028 | QualType AllocElemType = Context.getBaseElementType(AllocType); |
| 1029 | |
| 1030 | if (AllocElemType->isRecordType() && !UseGlobal) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1031 | CXXRecordDecl *Record |
Argyrios Kyrtzidis | d293298 | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 1032 | = cast<CXXRecordDecl>(AllocElemType->getAs<RecordType>()->getDecl()); |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 1033 | if (FindAllocationOverload(StartLoc, Range, NewName, &AllocArgs[0], |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1034 | AllocArgs.size(), Record, /*AllowMissing=*/true, |
| 1035 | OperatorNew)) |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1036 | return true; |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1037 | } |
| 1038 | if (!OperatorNew) { |
| 1039 | // Didn't find a member overload. Look for a global one. |
| 1040 | DeclareGlobalNewDelete(); |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1041 | DeclContext *TUDecl = Context.getTranslationUnitDecl(); |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 1042 | if (FindAllocationOverload(StartLoc, Range, NewName, &AllocArgs[0], |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1043 | AllocArgs.size(), TUDecl, /*AllowMissing=*/false, |
| 1044 | OperatorNew)) |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1045 | return true; |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
John McCall | 9c82afc | 2010-04-20 02:18:25 +0000 | [diff] [blame] | 1048 | // We don't need an operator delete if we're running under |
| 1049 | // -fno-exceptions. |
| 1050 | if (!getLangOptions().Exceptions) { |
| 1051 | OperatorDelete = 0; |
| 1052 | return false; |
| 1053 | } |
| 1054 | |
Anders Carlsson | d958389 | 2009-05-31 20:26:12 +0000 | [diff] [blame] | 1055 | // FindAllocationOverload can change the passed in arguments, so we need to |
| 1056 | // copy them back. |
| 1057 | if (NumPlaceArgs > 0) |
| 1058 | std::copy(&AllocArgs[1], AllocArgs.end(), PlaceArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1059 | |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1060 | // C++ [expr.new]p19: |
| 1061 | // |
| 1062 | // If the new-expression begins with a unary :: operator, the |
| 1063 | // deallocation function’s name is looked up in the global |
| 1064 | // scope. Otherwise, if the allocated type is a class type T or an |
| 1065 | // array thereof, the deallocation function’s name is looked up in |
| 1066 | // the scope of T. If this lookup fails to find the name, or if |
| 1067 | // the allocated type is not a class type or array thereof, the |
| 1068 | // deallocation function’s name is looked up in the global scope. |
| 1069 | LookupResult FoundDelete(*this, DeleteName, StartLoc, LookupOrdinaryName); |
Argyrios Kyrtzidis | d293298 | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 1070 | if (AllocElemType->isRecordType() && !UseGlobal) { |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1071 | CXXRecordDecl *RD |
Argyrios Kyrtzidis | d293298 | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 1072 | = cast<CXXRecordDecl>(AllocElemType->getAs<RecordType>()->getDecl()); |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1073 | LookupQualifiedName(FoundDelete, RD); |
| 1074 | } |
John McCall | 90c8c57 | 2010-03-18 08:19:33 +0000 | [diff] [blame] | 1075 | if (FoundDelete.isAmbiguous()) |
| 1076 | return true; // FIXME: clean up expressions? |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1077 | |
| 1078 | if (FoundDelete.empty()) { |
| 1079 | DeclareGlobalNewDelete(); |
| 1080 | LookupQualifiedName(FoundDelete, Context.getTranslationUnitDecl()); |
| 1081 | } |
| 1082 | |
| 1083 | FoundDelete.suppressDiagnostics(); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1084 | |
| 1085 | llvm::SmallVector<std::pair<DeclAccessPair,FunctionDecl*>, 2> Matches; |
| 1086 | |
John McCall | edeb6c9 | 2010-09-14 21:34:24 +0000 | [diff] [blame] | 1087 | // Whether we're looking for a placement operator delete is dictated |
| 1088 | // by whether we selected a placement operator new, not by whether |
| 1089 | // we had explicit placement arguments. This matters for things like |
| 1090 | // struct A { void *operator new(size_t, int = 0); ... }; |
| 1091 | // A *a = new A() |
| 1092 | bool isPlacementNew = (NumPlaceArgs > 0 || OperatorNew->param_size() != 1); |
| 1093 | |
| 1094 | if (isPlacementNew) { |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1095 | // C++ [expr.new]p20: |
| 1096 | // A declaration of a placement deallocation function matches the |
| 1097 | // declaration of a placement allocation function if it has the |
| 1098 | // same number of parameters and, after parameter transformations |
| 1099 | // (8.3.5), all parameter types except the first are |
| 1100 | // identical. [...] |
| 1101 | // |
| 1102 | // To perform this comparison, we compute the function type that |
| 1103 | // the deallocation function should have, and use that type both |
| 1104 | // for template argument deduction and for comparison purposes. |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1105 | // |
| 1106 | // FIXME: this comparison should ignore CC and the like. |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1107 | QualType ExpectedFunctionType; |
| 1108 | { |
| 1109 | const FunctionProtoType *Proto |
| 1110 | = OperatorNew->getType()->getAs<FunctionProtoType>(); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1111 | |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1112 | llvm::SmallVector<QualType, 4> ArgTypes; |
| 1113 | ArgTypes.push_back(Context.VoidPtrTy); |
| 1114 | for (unsigned I = 1, N = Proto->getNumArgs(); I < N; ++I) |
| 1115 | ArgTypes.push_back(Proto->getArgType(I)); |
| 1116 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1117 | FunctionProtoType::ExtProtoInfo EPI; |
| 1118 | EPI.Variadic = Proto->isVariadic(); |
| 1119 | |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1120 | ExpectedFunctionType |
| 1121 | = Context.getFunctionType(Context.VoidTy, ArgTypes.data(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1122 | ArgTypes.size(), EPI); |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | for (LookupResult::iterator D = FoundDelete.begin(), |
| 1126 | DEnd = FoundDelete.end(); |
| 1127 | D != DEnd; ++D) { |
| 1128 | FunctionDecl *Fn = 0; |
| 1129 | if (FunctionTemplateDecl *FnTmpl |
| 1130 | = dyn_cast<FunctionTemplateDecl>((*D)->getUnderlyingDecl())) { |
| 1131 | // Perform template argument deduction to try to match the |
| 1132 | // expected function type. |
| 1133 | TemplateDeductionInfo Info(Context, StartLoc); |
| 1134 | if (DeduceTemplateArguments(FnTmpl, 0, ExpectedFunctionType, Fn, Info)) |
| 1135 | continue; |
| 1136 | } else |
| 1137 | Fn = cast<FunctionDecl>((*D)->getUnderlyingDecl()); |
| 1138 | |
| 1139 | if (Context.hasSameType(Fn->getType(), ExpectedFunctionType)) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1140 | Matches.push_back(std::make_pair(D.getPair(), Fn)); |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1141 | } |
| 1142 | } else { |
| 1143 | // C++ [expr.new]p20: |
| 1144 | // [...] Any non-placement deallocation function matches a |
| 1145 | // non-placement allocation function. [...] |
| 1146 | for (LookupResult::iterator D = FoundDelete.begin(), |
| 1147 | DEnd = FoundDelete.end(); |
| 1148 | D != DEnd; ++D) { |
| 1149 | if (FunctionDecl *Fn = dyn_cast<FunctionDecl>((*D)->getUnderlyingDecl())) |
| 1150 | if (isNonPlacementDeallocationFunction(Fn)) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1151 | Matches.push_back(std::make_pair(D.getPair(), Fn)); |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1152 | } |
| 1153 | } |
| 1154 | |
| 1155 | // C++ [expr.new]p20: |
| 1156 | // [...] If the lookup finds a single matching deallocation |
| 1157 | // function, that function will be called; otherwise, no |
| 1158 | // deallocation function will be called. |
| 1159 | if (Matches.size() == 1) { |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1160 | OperatorDelete = Matches[0].second; |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1161 | |
| 1162 | // C++0x [expr.new]p20: |
| 1163 | // If the lookup finds the two-parameter form of a usual |
| 1164 | // deallocation function (3.7.4.2) and that function, considered |
| 1165 | // as a placement deallocation function, would have been |
| 1166 | // selected as a match for the allocation function, the program |
| 1167 | // is ill-formed. |
| 1168 | if (NumPlaceArgs && getLangOptions().CPlusPlus0x && |
| 1169 | isNonPlacementDeallocationFunction(OperatorDelete)) { |
| 1170 | Diag(StartLoc, diag::err_placement_new_non_placement_delete) |
| 1171 | << SourceRange(PlaceArgs[0]->getLocStart(), |
| 1172 | PlaceArgs[NumPlaceArgs - 1]->getLocEnd()); |
| 1173 | Diag(OperatorDelete->getLocation(), diag::note_previous_decl) |
| 1174 | << DeleteName; |
John McCall | 90c8c57 | 2010-03-18 08:19:33 +0000 | [diff] [blame] | 1175 | } else { |
| 1176 | CheckAllocationAccess(StartLoc, Range, FoundDelete.getNamingClass(), |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1177 | Matches[0].first); |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1178 | } |
| 1179 | } |
| 1180 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1181 | return false; |
| 1182 | } |
| 1183 | |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1184 | /// FindAllocationOverload - Find an fitting overload for the allocation |
| 1185 | /// function in the specified scope. |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 1186 | bool Sema::FindAllocationOverload(SourceLocation StartLoc, SourceRange Range, |
| 1187 | DeclarationName Name, Expr** Args, |
| 1188 | unsigned NumArgs, DeclContext *Ctx, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1189 | bool AllowMissing, FunctionDecl *&Operator) { |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1190 | LookupResult R(*this, Name, StartLoc, LookupOrdinaryName); |
| 1191 | LookupQualifiedName(R, Ctx); |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1192 | if (R.empty()) { |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1193 | if (AllowMissing) |
| 1194 | return false; |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1195 | return Diag(StartLoc, diag::err_ovl_no_viable_function_in_call) |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 1196 | << Name << Range; |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
John McCall | 90c8c57 | 2010-03-18 08:19:33 +0000 | [diff] [blame] | 1199 | if (R.isAmbiguous()) |
| 1200 | return true; |
| 1201 | |
| 1202 | R.suppressDiagnostics(); |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1203 | |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1204 | OverloadCandidateSet Candidates(StartLoc); |
Douglas Gregor | 5d64e5b | 2009-09-30 00:03:47 +0000 | [diff] [blame] | 1205 | for (LookupResult::iterator Alloc = R.begin(), AllocEnd = R.end(); |
| 1206 | Alloc != AllocEnd; ++Alloc) { |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1207 | // Even member operator new/delete are implicitly treated as |
| 1208 | // static, so don't use AddMemberCandidate. |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1209 | NamedDecl *D = (*Alloc)->getUnderlyingDecl(); |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1210 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1211 | if (FunctionTemplateDecl *FnTemplate = dyn_cast<FunctionTemplateDecl>(D)) { |
| 1212 | AddTemplateOverloadCandidate(FnTemplate, Alloc.getPair(), |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1213 | /*ExplicitTemplateArgs=*/0, Args, NumArgs, |
| 1214 | Candidates, |
| 1215 | /*SuppressUserConversions=*/false); |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 1216 | continue; |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1217 | } |
| 1218 | |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1219 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
| 1220 | AddOverloadCandidate(Fn, Alloc.getPair(), Args, NumArgs, Candidates, |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1221 | /*SuppressUserConversions=*/false); |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | // Do the resolution. |
| 1225 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1226 | switch (Candidates.BestViableFunction(*this, StartLoc, Best)) { |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1227 | case OR_Success: { |
| 1228 | // Got one! |
| 1229 | FunctionDecl *FnDecl = Best->Function; |
| 1230 | // The first argument is size_t, and the first parameter must be size_t, |
| 1231 | // too. This is checked on declaration and can be assumed. (It can't be |
| 1232 | // asserted on, though, since invalid decls are left in there.) |
John McCall | 90c8c57 | 2010-03-18 08:19:33 +0000 | [diff] [blame] | 1233 | // Watch out for variadic allocator function. |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 1234 | unsigned NumArgsInFnDecl = FnDecl->getNumParams(); |
| 1235 | for (unsigned i = 0; (i < NumArgs && i < NumArgsInFnDecl); ++i) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1236 | ExprResult Result |
Douglas Gregor | 29ecaba | 2010-03-26 20:35:59 +0000 | [diff] [blame] | 1237 | = PerformCopyInitialization(InitializedEntity::InitializeParameter( |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1238 | Context, |
Douglas Gregor | 29ecaba | 2010-03-26 20:35:59 +0000 | [diff] [blame] | 1239 | FnDecl->getParamDecl(i)), |
| 1240 | SourceLocation(), |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 1241 | Owned(Args[i])); |
Douglas Gregor | 29ecaba | 2010-03-26 20:35:59 +0000 | [diff] [blame] | 1242 | if (Result.isInvalid()) |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1243 | return true; |
Douglas Gregor | 29ecaba | 2010-03-26 20:35:59 +0000 | [diff] [blame] | 1244 | |
| 1245 | Args[i] = Result.takeAs<Expr>(); |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1246 | } |
| 1247 | Operator = FnDecl; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1248 | CheckAllocationAccess(StartLoc, Range, R.getNamingClass(), Best->FoundDecl); |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1249 | return false; |
| 1250 | } |
| 1251 | |
| 1252 | case OR_No_Viable_Function: |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1253 | Diag(StartLoc, diag::err_ovl_no_viable_function_in_call) |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 1254 | << Name << Range; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1255 | Candidates.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1256 | return true; |
| 1257 | |
| 1258 | case OR_Ambiguous: |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1259 | Diag(StartLoc, diag::err_ovl_ambiguous_call) |
Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 1260 | << Name << Range; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1261 | Candidates.NoteCandidates(*this, OCD_ViableCandidates, Args, NumArgs); |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1262 | return true; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1263 | |
| 1264 | case OR_Deleted: |
| 1265 | Diag(StartLoc, diag::err_ovl_deleted_call) |
| 1266 | << Best->Function->isDeleted() |
| 1267 | << Name << Range; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1268 | Candidates.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1269 | return true; |
Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1270 | } |
| 1271 | assert(false && "Unreachable, bad result from BestViableFunction"); |
| 1272 | return true; |
| 1273 | } |
| 1274 | |
| 1275 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1276 | /// DeclareGlobalNewDelete - Declare the global forms of operator new and |
| 1277 | /// delete. These are: |
| 1278 | /// @code |
| 1279 | /// void* operator new(std::size_t) throw(std::bad_alloc); |
| 1280 | /// void* operator new[](std::size_t) throw(std::bad_alloc); |
| 1281 | /// void operator delete(void *) throw(); |
| 1282 | /// void operator delete[](void *) throw(); |
| 1283 | /// @endcode |
| 1284 | /// Note that the placement and nothrow forms of new are *not* implicitly |
| 1285 | /// declared. Their use requires including \<new\>. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1286 | void Sema::DeclareGlobalNewDelete() { |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1287 | if (GlobalNewDeleteDeclared) |
| 1288 | return; |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1289 | |
| 1290 | // C++ [basic.std.dynamic]p2: |
| 1291 | // [...] The following allocation and deallocation functions (18.4) are |
| 1292 | // implicitly declared in global scope in each translation unit of a |
| 1293 | // program |
| 1294 | // |
| 1295 | // void* operator new(std::size_t) throw(std::bad_alloc); |
| 1296 | // void* operator new[](std::size_t) throw(std::bad_alloc); |
| 1297 | // void operator delete(void*) throw(); |
| 1298 | // void operator delete[](void*) throw(); |
| 1299 | // |
| 1300 | // These implicit declarations introduce only the function names operator |
| 1301 | // new, operator new[], operator delete, operator delete[]. |
| 1302 | // |
| 1303 | // Here, we need to refer to std::bad_alloc, so we will implicitly declare |
| 1304 | // "std" or "bad_alloc" as necessary to form the exception specification. |
| 1305 | // However, we do not make these implicit declarations visible to name |
| 1306 | // lookup. |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1307 | if (!StdBadAlloc) { |
| 1308 | // The "std::bad_alloc" class has not yet been declared, so build it |
| 1309 | // implicitly. |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1310 | StdBadAlloc = CXXRecordDecl::Create(Context, TTK_Class, |
Argyrios Kyrtzidis | 26faaac | 2010-08-02 07:14:39 +0000 | [diff] [blame] | 1311 | getOrCreateStdNamespace(), |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1312 | SourceLocation(), |
| 1313 | &PP.getIdentifierTable().get("bad_alloc"), |
| 1314 | SourceLocation(), 0); |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 1315 | getStdBadAlloc()->setImplicit(true); |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1318 | GlobalNewDeleteDeclared = true; |
| 1319 | |
| 1320 | QualType VoidPtr = Context.getPointerType(Context.VoidTy); |
| 1321 | QualType SizeT = Context.getSizeType(); |
Nuno Lopes | fc28448 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 1322 | bool AssumeSaneOperatorNew = getLangOptions().AssumeSaneOperatorNew; |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1323 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1324 | DeclareGlobalAllocationFunction( |
| 1325 | Context.DeclarationNames.getCXXOperatorName(OO_New), |
Nuno Lopes | fc28448 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 1326 | VoidPtr, SizeT, AssumeSaneOperatorNew); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1327 | DeclareGlobalAllocationFunction( |
| 1328 | Context.DeclarationNames.getCXXOperatorName(OO_Array_New), |
Nuno Lopes | fc28448 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 1329 | VoidPtr, SizeT, AssumeSaneOperatorNew); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1330 | DeclareGlobalAllocationFunction( |
| 1331 | Context.DeclarationNames.getCXXOperatorName(OO_Delete), |
| 1332 | Context.VoidTy, VoidPtr); |
| 1333 | DeclareGlobalAllocationFunction( |
| 1334 | Context.DeclarationNames.getCXXOperatorName(OO_Array_Delete), |
| 1335 | Context.VoidTy, VoidPtr); |
| 1336 | } |
| 1337 | |
| 1338 | /// DeclareGlobalAllocationFunction - Declares a single implicit global |
| 1339 | /// allocation function if it doesn't already exist. |
| 1340 | void Sema::DeclareGlobalAllocationFunction(DeclarationName Name, |
Nuno Lopes | fc28448 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 1341 | QualType Return, QualType Argument, |
| 1342 | bool AddMallocAttr) { |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1343 | DeclContext *GlobalCtx = Context.getTranslationUnitDecl(); |
| 1344 | |
| 1345 | // Check if this function is already declared. |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1346 | { |
Douglas Gregor | 5cc3709 | 2008-12-23 22:05:29 +0000 | [diff] [blame] | 1347 | DeclContext::lookup_iterator Alloc, AllocEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1348 | for (llvm::tie(Alloc, AllocEnd) = GlobalCtx->lookup(Name); |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1349 | Alloc != AllocEnd; ++Alloc) { |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1350 | // Only look at non-template functions, as it is the predefined, |
| 1351 | // non-templated allocation function we are trying to declare here. |
| 1352 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(*Alloc)) { |
| 1353 | QualType InitialParamType = |
Douglas Gregor | 6e790ab | 2009-12-22 23:42:49 +0000 | [diff] [blame] | 1354 | Context.getCanonicalType( |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1355 | Func->getParamDecl(0)->getType().getUnqualifiedType()); |
| 1356 | // FIXME: Do we need to check for default arguments here? |
Douglas Gregor | 7b86862 | 2010-08-18 15:06:25 +0000 | [diff] [blame] | 1357 | if (Func->getNumParams() == 1 && InitialParamType == Argument) { |
| 1358 | if(AddMallocAttr && !Func->hasAttr<MallocAttr>()) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1359 | Func->addAttr(::new (Context) MallocAttr(SourceLocation(), Context)); |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1360 | return; |
Douglas Gregor | 7b86862 | 2010-08-18 15:06:25 +0000 | [diff] [blame] | 1361 | } |
Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1362 | } |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1363 | } |
| 1364 | } |
| 1365 | |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1366 | QualType BadAllocType; |
| 1367 | bool HasBadAllocExceptionSpec |
| 1368 | = (Name.getCXXOverloadedOperator() == OO_New || |
| 1369 | Name.getCXXOverloadedOperator() == OO_Array_New); |
| 1370 | if (HasBadAllocExceptionSpec) { |
| 1371 | assert(StdBadAlloc && "Must have std::bad_alloc declared"); |
Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 1372 | BadAllocType = Context.getTypeDeclType(getStdBadAlloc()); |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1373 | } |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1374 | |
| 1375 | FunctionProtoType::ExtProtoInfo EPI; |
| 1376 | EPI.HasExceptionSpec = true; |
| 1377 | if (HasBadAllocExceptionSpec) { |
| 1378 | EPI.NumExceptions = 1; |
| 1379 | EPI.Exceptions = &BadAllocType; |
| 1380 | } |
Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1381 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1382 | QualType FnType = Context.getFunctionType(Return, &Argument, 1, EPI); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1383 | FunctionDecl *Alloc = |
| 1384 | FunctionDecl::Create(Context, GlobalCtx, SourceLocation(), Name, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1385 | FnType, /*TInfo=*/0, SC_None, |
| 1386 | SC_None, false, true); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1387 | Alloc->setImplicit(); |
Nuno Lopes | fc28448 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 1388 | |
| 1389 | if (AddMallocAttr) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1390 | Alloc->addAttr(::new (Context) MallocAttr(SourceLocation(), Context)); |
Nuno Lopes | fc28448 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 1391 | |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1392 | ParmVarDecl *Param = ParmVarDecl::Create(Context, Alloc, SourceLocation(), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1393 | 0, Argument, /*TInfo=*/0, |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1394 | SC_None, |
| 1395 | SC_None, 0); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1396 | Alloc->setParams(&Param, 1); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1397 | |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1398 | // FIXME: Also add this declaration to the IdentifierResolver, but |
| 1399 | // make sure it is at the end of the chain to coincide with the |
| 1400 | // global scope. |
John McCall | 5f1e094 | 2010-08-24 08:50:51 +0000 | [diff] [blame] | 1401 | Context.getTranslationUnitDecl()->addDecl(Alloc); |
Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1402 | } |
| 1403 | |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1404 | bool Sema::FindDeallocationFunction(SourceLocation StartLoc, CXXRecordDecl *RD, |
| 1405 | DeclarationName Name, |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 1406 | FunctionDecl* &Operator) { |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1407 | LookupResult Found(*this, Name, StartLoc, LookupOrdinaryName); |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1408 | // Try to find operator delete/operator delete[] in class scope. |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1409 | LookupQualifiedName(Found, RD); |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1410 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1411 | if (Found.isAmbiguous()) |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1412 | return true; |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1413 | |
Chandler Carruth | 2389324 | 2010-06-28 00:30:51 +0000 | [diff] [blame] | 1414 | Found.suppressDiagnostics(); |
| 1415 | |
John McCall | 046a746 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 1416 | llvm::SmallVector<DeclAccessPair,4> Matches; |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1417 | for (LookupResult::iterator F = Found.begin(), FEnd = Found.end(); |
| 1418 | F != FEnd; ++F) { |
Chandler Carruth | 09556fd | 2010-08-08 07:04:00 +0000 | [diff] [blame] | 1419 | NamedDecl *ND = (*F)->getUnderlyingDecl(); |
| 1420 | |
| 1421 | // Ignore template operator delete members from the check for a usual |
| 1422 | // deallocation function. |
| 1423 | if (isa<FunctionTemplateDecl>(ND)) |
| 1424 | continue; |
| 1425 | |
| 1426 | if (cast<CXXMethodDecl>(ND)->isUsualDeallocationFunction()) |
John McCall | 046a746 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 1427 | Matches.push_back(F.getPair()); |
| 1428 | } |
| 1429 | |
| 1430 | // There's exactly one suitable operator; pick it. |
| 1431 | if (Matches.size() == 1) { |
| 1432 | Operator = cast<CXXMethodDecl>(Matches[0]->getUnderlyingDecl()); |
| 1433 | CheckAllocationAccess(StartLoc, SourceRange(), Found.getNamingClass(), |
| 1434 | Matches[0]); |
| 1435 | return false; |
| 1436 | |
| 1437 | // We found multiple suitable operators; complain about the ambiguity. |
| 1438 | } else if (!Matches.empty()) { |
| 1439 | Diag(StartLoc, diag::err_ambiguous_suitable_delete_member_function_found) |
| 1440 | << Name << RD; |
| 1441 | |
| 1442 | for (llvm::SmallVectorImpl<DeclAccessPair>::iterator |
| 1443 | F = Matches.begin(), FEnd = Matches.end(); F != FEnd; ++F) |
| 1444 | Diag((*F)->getUnderlyingDecl()->getLocation(), |
| 1445 | diag::note_member_declared_here) << Name; |
| 1446 | return true; |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
| 1449 | // We did find operator delete/operator delete[] declarations, but |
| 1450 | // none of them were suitable. |
| 1451 | if (!Found.empty()) { |
| 1452 | Diag(StartLoc, diag::err_no_suitable_delete_member_function_found) |
| 1453 | << Name << RD; |
| 1454 | |
| 1455 | for (LookupResult::iterator F = Found.begin(), FEnd = Found.end(); |
John McCall | 046a746 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 1456 | F != FEnd; ++F) |
| 1457 | Diag((*F)->getUnderlyingDecl()->getLocation(), |
| 1458 | diag::note_member_declared_here) << Name; |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1459 | |
| 1460 | return true; |
| 1461 | } |
| 1462 | |
| 1463 | // Look for a global declaration. |
| 1464 | DeclareGlobalNewDelete(); |
| 1465 | DeclContext *TUDecl = Context.getTranslationUnitDecl(); |
| 1466 | |
| 1467 | CXXNullPtrLiteralExpr Null(Context.VoidPtrTy, SourceLocation()); |
| 1468 | Expr* DeallocArgs[1]; |
| 1469 | DeallocArgs[0] = &Null; |
| 1470 | if (FindAllocationOverload(StartLoc, SourceRange(), Name, |
| 1471 | DeallocArgs, 1, TUDecl, /*AllowMissing=*/false, |
| 1472 | Operator)) |
| 1473 | return true; |
| 1474 | |
| 1475 | assert(Operator && "Did not find a deallocation function!"); |
| 1476 | return false; |
| 1477 | } |
| 1478 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1479 | /// ActOnCXXDelete - Parsed a C++ 'delete' expression (C++ 5.3.5), as in: |
| 1480 | /// @code ::delete ptr; @endcode |
| 1481 | /// or |
| 1482 | /// @code delete [] ptr; @endcode |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1483 | ExprResult |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1484 | Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1485 | bool ArrayForm, Expr *Ex) { |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1486 | // C++ [expr.delete]p1: |
| 1487 | // The operand shall have a pointer type, or a class type having a single |
| 1488 | // conversion function to a pointer type. The result has type void. |
| 1489 | // |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1490 | // DR599 amends "pointer type" to "pointer to object type" in both cases. |
| 1491 | |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1492 | FunctionDecl *OperatorDelete = 0; |
Argyrios Kyrtzidis | 4076dac | 2010-09-13 20:15:54 +0000 | [diff] [blame] | 1493 | bool ArrayFormAsWritten = ArrayForm; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1494 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1495 | if (!Ex->isTypeDependent()) { |
| 1496 | QualType Type = Ex->getType(); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1497 | |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1498 | if (const RecordType *Record = Type->getAs<RecordType>()) { |
Douglas Gregor | 254a942 | 2010-07-29 14:44:35 +0000 | [diff] [blame] | 1499 | if (RequireCompleteType(StartLoc, Type, |
| 1500 | PDiag(diag::err_delete_incomplete_class_type))) |
| 1501 | return ExprError(); |
| 1502 | |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1503 | llvm::SmallVector<CXXConversionDecl*, 4> ObjectPtrConversions; |
| 1504 | |
Fariborz Jahanian | 5346278 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 1505 | CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl()); |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1506 | const UnresolvedSetImpl *Conversions = RD->getVisibleConversionFunctions(); |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 1507 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1508 | E = Conversions->end(); I != E; ++I) { |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1509 | NamedDecl *D = I.getDecl(); |
| 1510 | if (isa<UsingShadowDecl>(D)) |
| 1511 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 1512 | |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1513 | // Skip over templated conversion functions; they aren't considered. |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1514 | if (isa<FunctionTemplateDecl>(D)) |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1515 | continue; |
| 1516 | |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1517 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1518 | |
| 1519 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 1520 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 1521 | if (ConvPtrType->getPointeeType()->isIncompleteOrObjectType()) |
Fariborz Jahanian | 8b915e7 | 2009-09-15 22:15:23 +0000 | [diff] [blame] | 1522 | ObjectPtrConversions.push_back(Conv); |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1523 | } |
Fariborz Jahanian | 8b915e7 | 2009-09-15 22:15:23 +0000 | [diff] [blame] | 1524 | if (ObjectPtrConversions.size() == 1) { |
| 1525 | // We have a single conversion to a pointer-to-object type. Perform |
| 1526 | // that conversion. |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1527 | // TODO: don't redo the conversion calculation. |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1528 | if (!PerformImplicitConversion(Ex, |
| 1529 | ObjectPtrConversions.front()->getConversionType(), |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 1530 | AA_Converting)) { |
Fariborz Jahanian | 8b915e7 | 2009-09-15 22:15:23 +0000 | [diff] [blame] | 1531 | Type = Ex->getType(); |
| 1532 | } |
| 1533 | } |
| 1534 | else if (ObjectPtrConversions.size() > 1) { |
| 1535 | Diag(StartLoc, diag::err_ambiguous_delete_operand) |
| 1536 | << Type << Ex->getSourceRange(); |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1537 | for (unsigned i= 0; i < ObjectPtrConversions.size(); i++) |
| 1538 | NoteOverloadCandidate(ObjectPtrConversions[i]); |
Fariborz Jahanian | 8b915e7 | 2009-09-15 22:15:23 +0000 | [diff] [blame] | 1539 | return ExprError(); |
Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1540 | } |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1541 | } |
| 1542 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1543 | if (!Type->isPointerType()) |
| 1544 | return ExprError(Diag(StartLoc, diag::err_delete_operand) |
| 1545 | << Type << Ex->getSourceRange()); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1546 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1547 | QualType Pointee = Type->getAs<PointerType>()->getPointeeType(); |
Douglas Gregor | 94a6157 | 2010-05-24 17:01:56 +0000 | [diff] [blame] | 1548 | if (Pointee->isVoidType() && !isSFINAEContext()) { |
| 1549 | // The C++ standard bans deleting a pointer to a non-object type, which |
| 1550 | // effectively bans deletion of "void*". However, most compilers support |
| 1551 | // this, so we treat it as a warning unless we're in a SFINAE context. |
| 1552 | Diag(StartLoc, diag::ext_delete_void_ptr_operand) |
| 1553 | << Type << Ex->getSourceRange(); |
| 1554 | } else if (Pointee->isFunctionType() || Pointee->isVoidType()) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1555 | return ExprError(Diag(StartLoc, diag::err_delete_operand) |
| 1556 | << Type << Ex->getSourceRange()); |
Douglas Gregor | 8dcb29d | 2009-03-24 20:13:58 +0000 | [diff] [blame] | 1557 | else if (!Pointee->isDependentType() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1558 | RequireCompleteType(StartLoc, Pointee, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 1559 | PDiag(diag::warn_delete_incomplete) |
| 1560 | << Ex->getSourceRange())) |
Douglas Gregor | 8dcb29d | 2009-03-24 20:13:58 +0000 | [diff] [blame] | 1561 | return ExprError(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1562 | |
Douglas Gregor | 1070c9f | 2009-09-29 21:38:53 +0000 | [diff] [blame] | 1563 | // C++ [expr.delete]p2: |
| 1564 | // [Note: a pointer to a const type can be the operand of a |
| 1565 | // delete-expression; it is not necessary to cast away the constness |
| 1566 | // (5.2.11) of the pointer expression before it is used as the operand |
| 1567 | // of the delete-expression. ] |
| 1568 | ImpCastExprToType(Ex, Context.getPointerType(Context.VoidTy), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1569 | CK_NoOp); |
Argyrios Kyrtzidis | 4076dac | 2010-09-13 20:15:54 +0000 | [diff] [blame] | 1570 | |
| 1571 | if (Pointee->isArrayType() && !ArrayForm) { |
| 1572 | Diag(StartLoc, diag::warn_delete_array_type) |
| 1573 | << Type << Ex->getSourceRange() |
| 1574 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(StartLoc), "[]"); |
| 1575 | ArrayForm = true; |
| 1576 | } |
| 1577 | |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1578 | DeclarationName DeleteName = Context.DeclarationNames.getCXXOperatorName( |
| 1579 | ArrayForm ? OO_Array_Delete : OO_Delete); |
| 1580 | |
Argyrios Kyrtzidis | d293298 | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 1581 | QualType PointeeElem = Context.getBaseElementType(Pointee); |
| 1582 | if (const RecordType *RT = PointeeElem->getAs<RecordType>()) { |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1583 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 1584 | |
| 1585 | if (!UseGlobal && |
| 1586 | FindDeallocationFunction(StartLoc, RD, DeleteName, OperatorDelete)) |
Anders Carlsson | 0ba63ea | 2009-11-14 03:17:38 +0000 | [diff] [blame] | 1587 | return ExprError(); |
Anders Carlsson | 0ba63ea | 2009-11-14 03:17:38 +0000 | [diff] [blame] | 1588 | |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1589 | if (!RD->hasTrivialDestructor()) |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 1590 | if (CXXDestructorDecl *Dtor = LookupDestructor(RD)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1591 | MarkDeclarationReferenced(StartLoc, |
Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 1592 | const_cast<CXXDestructorDecl*>(Dtor)); |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 1593 | DiagnoseUseOfDecl(Dtor, StartLoc); |
| 1594 | } |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1595 | } |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1596 | |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1597 | if (!OperatorDelete) { |
Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1598 | // Look for a global declaration. |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1599 | DeclareGlobalNewDelete(); |
| 1600 | DeclContext *TUDecl = Context.getTranslationUnitDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1601 | if (FindAllocationOverload(StartLoc, SourceRange(), DeleteName, |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 1602 | &Ex, 1, TUDecl, /*AllowMissing=*/false, |
Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1603 | OperatorDelete)) |
| 1604 | return ExprError(); |
| 1605 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1606 | |
John McCall | 9c82afc | 2010-04-20 02:18:25 +0000 | [diff] [blame] | 1607 | MarkDeclarationReferenced(StartLoc, OperatorDelete); |
| 1608 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1609 | // FIXME: Check access and ambiguity of operator delete and destructor. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1610 | } |
| 1611 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1612 | return Owned(new (Context) CXXDeleteExpr(Context.VoidTy, UseGlobal, ArrayForm, |
Argyrios Kyrtzidis | 4076dac | 2010-09-13 20:15:54 +0000 | [diff] [blame] | 1613 | ArrayFormAsWritten, OperatorDelete, |
| 1614 | Ex, StartLoc)); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1615 | } |
| 1616 | |
Douglas Gregor | 8cfe5a7 | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 1617 | /// \brief Check the use of the given variable as a C++ condition in an if, |
| 1618 | /// while, do-while, or switch statement. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1619 | ExprResult Sema::CheckConditionVariable(VarDecl *ConditionVar, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1620 | SourceLocation StmtLoc, |
| 1621 | bool ConvertToBoolean) { |
Douglas Gregor | 8cfe5a7 | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 1622 | QualType T = ConditionVar->getType(); |
| 1623 | |
| 1624 | // C++ [stmt.select]p2: |
| 1625 | // The declarator shall not specify a function or an array. |
| 1626 | if (T->isFunctionType()) |
| 1627 | return ExprError(Diag(ConditionVar->getLocation(), |
| 1628 | diag::err_invalid_use_of_function_type) |
| 1629 | << ConditionVar->getSourceRange()); |
| 1630 | else if (T->isArrayType()) |
| 1631 | return ExprError(Diag(ConditionVar->getLocation(), |
| 1632 | diag::err_invalid_use_of_array_type) |
| 1633 | << ConditionVar->getSourceRange()); |
Douglas Gregor | a7605db | 2009-11-24 16:07:02 +0000 | [diff] [blame] | 1634 | |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1635 | Expr *Condition = DeclRefExpr::Create(Context, 0, SourceRange(), ConditionVar, |
| 1636 | ConditionVar->getLocation(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1637 | ConditionVar->getType().getNonReferenceType(), |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 1638 | VK_LValue); |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 1639 | if (ConvertToBoolean && CheckBooleanCondition(Condition, StmtLoc)) |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1640 | return ExprError(); |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 1641 | |
| 1642 | return Owned(Condition); |
Douglas Gregor | 8cfe5a7 | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 1643 | } |
| 1644 | |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 1645 | /// CheckCXXBooleanCondition - Returns true if a conversion to bool is invalid. |
| 1646 | bool Sema::CheckCXXBooleanCondition(Expr *&CondExpr) { |
| 1647 | // C++ 6.4p4: |
| 1648 | // The value of a condition that is an initialized declaration in a statement |
| 1649 | // other than a switch statement is the value of the declared variable |
| 1650 | // implicitly converted to type bool. If that conversion is ill-formed, the |
| 1651 | // program is ill-formed. |
| 1652 | // The value of a condition that is an expression is the value of the |
| 1653 | // expression, implicitly converted to bool. |
| 1654 | // |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1655 | return PerformContextuallyConvertToBool(CondExpr); |
Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 1656 | } |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1657 | |
| 1658 | /// Helper function to determine whether this is the (deprecated) C++ |
| 1659 | /// conversion from a string literal to a pointer to non-const char or |
| 1660 | /// non-const wchar_t (for narrow and wide string literals, |
| 1661 | /// respectively). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1662 | bool |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1663 | Sema::IsStringLiteralToNonConstPointerConversion(Expr *From, QualType ToType) { |
| 1664 | // Look inside the implicit cast, if it exists. |
| 1665 | if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(From)) |
| 1666 | From = Cast->getSubExpr(); |
| 1667 | |
| 1668 | // A string literal (2.13.4) that is not a wide string literal can |
| 1669 | // be converted to an rvalue of type "pointer to char"; a wide |
| 1670 | // string literal can be converted to an rvalue of type "pointer |
| 1671 | // to wchar_t" (C++ 4.2p2). |
Douglas Gregor | 1984eb9 | 2010-06-22 23:47:37 +0000 | [diff] [blame] | 1672 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(From->IgnoreParens())) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1673 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1674 | if (const BuiltinType *ToPointeeType |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1675 | = ToPtrType->getPointeeType()->getAs<BuiltinType>()) { |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1676 | // This conversion is considered only when there is an |
| 1677 | // explicit appropriate pointer target type (C++ 4.2p2). |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1678 | if (!ToPtrType->getPointeeType().hasQualifiers() && |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1679 | ((StrLit->isWide() && ToPointeeType->isWideCharType()) || |
| 1680 | (!StrLit->isWide() && |
| 1681 | (ToPointeeType->getKind() == BuiltinType::Char_U || |
| 1682 | ToPointeeType->getKind() == BuiltinType::Char_S)))) |
| 1683 | return true; |
| 1684 | } |
| 1685 | |
| 1686 | return false; |
| 1687 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1688 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1689 | static ExprResult BuildCXXCastArgument(Sema &S, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1690 | SourceLocation CastLoc, |
| 1691 | QualType Ty, |
| 1692 | CastKind Kind, |
| 1693 | CXXMethodDecl *Method, |
| 1694 | Expr *From) { |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 1695 | switch (Kind) { |
| 1696 | default: assert(0 && "Unhandled cast kind!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1697 | case CK_ConstructorConversion: { |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 1698 | ASTOwningVector<Expr*> ConstructorArgs(S); |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 1699 | |
| 1700 | if (S.CompleteConstructorCall(cast<CXXConstructorDecl>(Method), |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1701 | MultiExprArg(&From, 1), |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 1702 | CastLoc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1703 | return ExprError(); |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 1704 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1705 | ExprResult Result = |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 1706 | S.BuildCXXConstructExpr(CastLoc, Ty, cast<CXXConstructorDecl>(Method), |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 1707 | move_arg(ConstructorArgs), |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 1708 | /*ZeroInit*/ false, CXXConstructExpr::CK_Complete, |
| 1709 | SourceRange()); |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 1710 | if (Result.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1711 | return ExprError(); |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 1712 | |
| 1713 | return S.MaybeBindToTemporary(Result.takeAs<Expr>()); |
| 1714 | } |
| 1715 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1716 | case CK_UserDefinedConversion: { |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 1717 | assert(!From->getType()->isPointerType() && "Arg can't have pointer type!"); |
| 1718 | |
| 1719 | // Create an implicit call expr that calls it. |
| 1720 | // FIXME: pass the FoundDecl for the user-defined conversion here |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame^] | 1721 | ExprResult Result = S.BuildCXXMemberCallExpr(From, Method, Method); |
| 1722 | if (Result.isInvalid()) |
| 1723 | return ExprError(); |
| 1724 | |
| 1725 | return S.MaybeBindToTemporary(Result.get()); |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 1726 | } |
| 1727 | } |
| 1728 | } |
| 1729 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1730 | /// PerformImplicitConversion - Perform an implicit conversion of the |
| 1731 | /// expression From to the type ToType using the pre-computed implicit |
| 1732 | /// conversion sequence ICS. Returns true if there was an error, false |
| 1733 | /// otherwise. The expression From is replaced with the converted |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 1734 | /// expression. Action is the kind of conversion we're performing, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1735 | /// used in the error message. |
| 1736 | bool |
| 1737 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType, |
| 1738 | const ImplicitConversionSequence &ICS, |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 1739 | AssignmentAction Action, bool IgnoreBaseAccess) { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1740 | switch (ICS.getKind()) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1741 | case ImplicitConversionSequence::StandardConversion: |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 1742 | if (PerformImplicitConversion(From, ToType, ICS.Standard, Action, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1743 | IgnoreBaseAccess)) |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1744 | return true; |
| 1745 | break; |
| 1746 | |
Anders Carlsson | f6c213a | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 1747 | case ImplicitConversionSequence::UserDefinedConversion: { |
| 1748 | |
Fariborz Jahanian | 7fe5d72 | 2009-08-28 22:04:50 +0000 | [diff] [blame] | 1749 | FunctionDecl *FD = ICS.UserDefined.ConversionFunction; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1750 | CastKind CastKind; |
Anders Carlsson | f6c213a | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 1751 | QualType BeforeToType; |
| 1752 | if (const CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(FD)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1753 | CastKind = CK_UserDefinedConversion; |
Anders Carlsson | f6c213a | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 1754 | |
| 1755 | // If the user-defined conversion is specified by a conversion function, |
| 1756 | // the initial standard conversion sequence converts the source type to |
| 1757 | // the implicit object parameter of the conversion function. |
| 1758 | BeforeToType = Context.getTagDeclType(Conv->getParent()); |
John McCall | 9ec9445 | 2010-12-04 09:57:16 +0000 | [diff] [blame] | 1759 | } else { |
| 1760 | const CXXConstructorDecl *Ctor = cast<CXXConstructorDecl>(FD); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1761 | CastKind = CK_ConstructorConversion; |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1762 | // Do no conversion if dealing with ... for the first conversion. |
Douglas Gregor | e44201a | 2009-11-20 02:31:03 +0000 | [diff] [blame] | 1763 | if (!ICS.UserDefined.EllipsisConversion) { |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1764 | // If the user-defined conversion is specified by a constructor, the |
| 1765 | // initial standard conversion sequence converts the source type to the |
| 1766 | // type required by the argument of the constructor |
Douglas Gregor | e44201a | 2009-11-20 02:31:03 +0000 | [diff] [blame] | 1767 | BeforeToType = Ctor->getParamDecl(0)->getType().getNonReferenceType(); |
| 1768 | } |
Anders Carlsson | f6c213a | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 1769 | } |
Douglas Gregor | a3998bd | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 1770 | // Watch out for elipsis conversion. |
Fariborz Jahanian | 4c0cea2 | 2009-11-06 00:55:14 +0000 | [diff] [blame] | 1771 | if (!ICS.UserDefined.EllipsisConversion) { |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1772 | if (PerformImplicitConversion(From, BeforeToType, |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 1773 | ICS.UserDefined.Before, AA_Converting, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 1774 | IgnoreBaseAccess)) |
Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 1775 | return true; |
| 1776 | } |
Anders Carlsson | f6c213a | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 1777 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1778 | ExprResult CastArg |
Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 1779 | = BuildCXXCastArgument(*this, |
| 1780 | From->getLocStart(), |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 1781 | ToType.getNonReferenceType(), |
| 1782 | CastKind, cast<CXXMethodDecl>(FD), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1783 | From); |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 1784 | |
| 1785 | if (CastArg.isInvalid()) |
| 1786 | return true; |
Eli Friedman | d888962 | 2009-11-27 04:41:50 +0000 | [diff] [blame] | 1787 | |
| 1788 | From = CastArg.takeAs<Expr>(); |
| 1789 | |
Eli Friedman | d888962 | 2009-11-27 04:41:50 +0000 | [diff] [blame] | 1790 | return PerformImplicitConversion(From, ToType, ICS.UserDefined.After, |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 1791 | AA_Converting, IgnoreBaseAccess); |
Fariborz Jahanian | 93034ca | 2009-10-16 19:20:59 +0000 | [diff] [blame] | 1792 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1793 | |
| 1794 | case ImplicitConversionSequence::AmbiguousConversion: |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1795 | ICS.DiagnoseAmbiguousConversion(*this, From->getExprLoc(), |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 1796 | PDiag(diag::err_typecheck_ambiguous_condition) |
| 1797 | << From->getSourceRange()); |
| 1798 | return true; |
Fariborz Jahanian | 93034ca | 2009-10-16 19:20:59 +0000 | [diff] [blame] | 1799 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1800 | case ImplicitConversionSequence::EllipsisConversion: |
| 1801 | assert(false && "Cannot perform an ellipsis conversion"); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1802 | return false; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1803 | |
| 1804 | case ImplicitConversionSequence::BadConversion: |
| 1805 | return true; |
| 1806 | } |
| 1807 | |
| 1808 | // Everything went well. |
| 1809 | return false; |
| 1810 | } |
| 1811 | |
| 1812 | /// PerformImplicitConversion - Perform an implicit conversion of the |
| 1813 | /// expression From to the type ToType by following the standard |
| 1814 | /// conversion sequence SCS. Returns true if there was an error, false |
| 1815 | /// otherwise. The expression From is replaced with the converted |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1816 | /// expression. Flavor is the context in which we're performing this |
| 1817 | /// conversion, for use in error messages. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1818 | bool |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1819 | Sema::PerformImplicitConversion(Expr *&From, QualType ToType, |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1820 | const StandardConversionSequence& SCS, |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 1821 | AssignmentAction Action, bool IgnoreBaseAccess) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1822 | // Overall FIXME: we are recomputing too many types here and doing far too |
| 1823 | // much extra work. What this means is that we need to keep track of more |
| 1824 | // information that is computed when we try the implicit conversion initially, |
| 1825 | // so that we don't need to recompute anything here. |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1826 | QualType FromType = From->getType(); |
| 1827 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1828 | if (SCS.CopyConstructor) { |
Anders Carlsson | 7c3e8a1 | 2009-05-19 04:45:15 +0000 | [diff] [blame] | 1829 | // FIXME: When can ToType be a reference type? |
| 1830 | assert(!ToType->isReferenceType()); |
Fariborz Jahanian | b3c4774 | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 1831 | if (SCS.Second == ICK_Derived_To_Base) { |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 1832 | ASTOwningVector<Expr*> ConstructorArgs(*this); |
Fariborz Jahanian | b3c4774 | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 1833 | if (CompleteConstructorCall(cast<CXXConstructorDecl>(SCS.CopyConstructor), |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 1834 | MultiExprArg(*this, &From, 1), |
Fariborz Jahanian | b3c4774 | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 1835 | /*FIXME:ConstructLoc*/SourceLocation(), |
| 1836 | ConstructorArgs)) |
| 1837 | return true; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1838 | ExprResult FromResult = |
Fariborz Jahanian | b3c4774 | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 1839 | BuildCXXConstructExpr(/*FIXME:ConstructLoc*/SourceLocation(), |
| 1840 | ToType, SCS.CopyConstructor, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 1841 | move_arg(ConstructorArgs), |
| 1842 | /*ZeroInit*/ false, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 1843 | CXXConstructExpr::CK_Complete, |
| 1844 | SourceRange()); |
Fariborz Jahanian | b3c4774 | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 1845 | if (FromResult.isInvalid()) |
| 1846 | return true; |
| 1847 | From = FromResult.takeAs<Expr>(); |
| 1848 | return false; |
| 1849 | } |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1850 | ExprResult FromResult = |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1851 | BuildCXXConstructExpr(/*FIXME:ConstructLoc*/SourceLocation(), |
| 1852 | ToType, SCS.CopyConstructor, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 1853 | MultiExprArg(*this, &From, 1), |
| 1854 | /*ZeroInit*/ false, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 1855 | CXXConstructExpr::CK_Complete, |
| 1856 | SourceRange()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1857 | |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 1858 | if (FromResult.isInvalid()) |
| 1859 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1860 | |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 1861 | From = FromResult.takeAs<Expr>(); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1862 | return false; |
| 1863 | } |
| 1864 | |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1865 | // Resolve overloaded function references. |
| 1866 | if (Context.hasSameType(FromType, Context.OverloadTy)) { |
| 1867 | DeclAccessPair Found; |
| 1868 | FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(From, ToType, |
| 1869 | true, Found); |
| 1870 | if (!Fn) |
| 1871 | return true; |
| 1872 | |
| 1873 | if (DiagnoseUseOfDecl(Fn, From->getSourceRange().getBegin())) |
| 1874 | return true; |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 1875 | |
Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 1876 | From = FixOverloadedFunctionReference(From, Found, Fn); |
| 1877 | FromType = From->getType(); |
| 1878 | } |
| 1879 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1880 | // Perform the first implicit conversion. |
| 1881 | switch (SCS.First) { |
| 1882 | case ICK_Identity: |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1883 | // Nothing to do. |
| 1884 | break; |
| 1885 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 1886 | case ICK_Lvalue_To_Rvalue: |
| 1887 | // Should this get its own ICK? |
| 1888 | if (From->getObjectKind() == OK_ObjCProperty) { |
| 1889 | ConvertPropertyForRValue(From); |
John McCall | 241d558 | 2010-12-07 22:54:16 +0000 | [diff] [blame] | 1890 | if (!From->isGLValue()) break; |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 1891 | } |
| 1892 | |
| 1893 | FromType = FromType.getUnqualifiedType(); |
| 1894 | From = ImplicitCastExpr::Create(Context, FromType, CK_LValueToRValue, |
| 1895 | From, 0, VK_RValue); |
| 1896 | break; |
| 1897 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1898 | case ICK_Array_To_Pointer: |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1899 | FromType = Context.getArrayDecayedType(FromType); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1900 | ImpCastExprToType(From, FromType, CK_ArrayToPointerDecay); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1901 | break; |
| 1902 | |
| 1903 | case ICK_Function_To_Pointer: |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1904 | FromType = Context.getPointerType(FromType); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1905 | ImpCastExprToType(From, FromType, CK_FunctionToPointerDecay); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1906 | break; |
| 1907 | |
| 1908 | default: |
| 1909 | assert(false && "Improper first standard conversion"); |
| 1910 | break; |
| 1911 | } |
| 1912 | |
| 1913 | // Perform the second implicit conversion |
| 1914 | switch (SCS.Second) { |
| 1915 | case ICK_Identity: |
Sebastian Redl | 2c7588f | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 1916 | // If both sides are functions (or pointers/references to them), there could |
| 1917 | // be incompatible exception declarations. |
| 1918 | if (CheckExceptionSpecCompatibility(From, ToType)) |
| 1919 | return true; |
| 1920 | // Nothing else to do. |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1921 | break; |
| 1922 | |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1923 | case ICK_NoReturn_Adjustment: |
| 1924 | // If both sides are functions (or pointers/references to them), there could |
| 1925 | // be incompatible exception declarations. |
| 1926 | if (CheckExceptionSpecCompatibility(From, ToType)) |
| 1927 | return true; |
| 1928 | |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 1929 | ImpCastExprToType(From, ToType, CK_NoOp); |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1930 | break; |
| 1931 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1932 | case ICK_Integral_Promotion: |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1933 | case ICK_Integral_Conversion: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1934 | ImpCastExprToType(From, ToType, CK_IntegralCast); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1935 | break; |
| 1936 | |
| 1937 | case ICK_Floating_Promotion: |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1938 | case ICK_Floating_Conversion: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1939 | ImpCastExprToType(From, ToType, CK_FloatingCast); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1940 | break; |
| 1941 | |
| 1942 | case ICK_Complex_Promotion: |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1943 | case ICK_Complex_Conversion: { |
| 1944 | QualType FromEl = From->getType()->getAs<ComplexType>()->getElementType(); |
| 1945 | QualType ToEl = ToType->getAs<ComplexType>()->getElementType(); |
| 1946 | CastKind CK; |
| 1947 | if (FromEl->isRealFloatingType()) { |
| 1948 | if (ToEl->isRealFloatingType()) |
| 1949 | CK = CK_FloatingComplexCast; |
| 1950 | else |
| 1951 | CK = CK_FloatingComplexToIntegralComplex; |
| 1952 | } else if (ToEl->isRealFloatingType()) { |
| 1953 | CK = CK_IntegralComplexToFloatingComplex; |
| 1954 | } else { |
| 1955 | CK = CK_IntegralComplexCast; |
| 1956 | } |
| 1957 | ImpCastExprToType(From, ToType, CK); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1958 | break; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1959 | } |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1960 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1961 | case ICK_Floating_Integral: |
Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 1962 | if (ToType->isRealFloatingType()) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1963 | ImpCastExprToType(From, ToType, CK_IntegralToFloating); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1964 | else |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1965 | ImpCastExprToType(From, ToType, CK_FloatingToIntegral); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1966 | break; |
| 1967 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1968 | case ICK_Compatible_Conversion: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1969 | ImpCastExprToType(From, ToType, CK_NoOp); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1970 | break; |
| 1971 | |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1972 | case ICK_Pointer_Conversion: { |
Douglas Gregor | a3998bd | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 1973 | if (SCS.IncompatibleObjC && Action != AA_Casting) { |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1974 | // Diagnose incompatible Objective-C conversions |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1975 | Diag(From->getSourceRange().getBegin(), |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1976 | diag::ext_typecheck_convert_incompatible_pointer) |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 1977 | << From->getType() << ToType << Action |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 1978 | << From->getSourceRange(); |
| 1979 | } |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1980 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1981 | CastKind Kind = CK_Invalid; |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1982 | CXXCastPath BasePath; |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 1983 | if (CheckPointerConversion(From, ToType, Kind, BasePath, IgnoreBaseAccess)) |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1984 | return true; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 1985 | ImpCastExprToType(From, ToType, Kind, VK_RValue, &BasePath); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1986 | break; |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1987 | } |
| 1988 | |
| 1989 | case ICK_Pointer_Member: { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1990 | CastKind Kind = CK_Invalid; |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1991 | CXXCastPath BasePath; |
Anders Carlsson | f9d68e1 | 2010-04-24 19:36:51 +0000 | [diff] [blame] | 1992 | if (CheckMemberPointerConversion(From, ToType, Kind, BasePath, |
| 1993 | IgnoreBaseAccess)) |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1994 | return true; |
Sebastian Redl | 2c7588f | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 1995 | if (CheckExceptionSpecCompatibility(From, ToType)) |
| 1996 | return true; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 1997 | ImpCastExprToType(From, ToType, Kind, VK_RValue, &BasePath); |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1998 | break; |
| 1999 | } |
Anders Carlsson | bc0e078 | 2009-11-23 20:04:44 +0000 | [diff] [blame] | 2000 | case ICK_Boolean_Conversion: { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2001 | CastKind Kind = CK_Invalid; |
| 2002 | switch (FromType->getScalarTypeKind()) { |
| 2003 | case Type::STK_Pointer: Kind = CK_PointerToBoolean; break; |
| 2004 | case Type::STK_MemberPointer: Kind = CK_MemberPointerToBoolean; break; |
| 2005 | case Type::STK_Bool: llvm_unreachable("bool -> bool conversion?"); |
| 2006 | case Type::STK_Integral: Kind = CK_IntegralToBoolean; break; |
| 2007 | case Type::STK_Floating: Kind = CK_FloatingToBoolean; break; |
| 2008 | case Type::STK_IntegralComplex: Kind = CK_IntegralComplexToBoolean; break; |
| 2009 | case Type::STK_FloatingComplex: Kind = CK_FloatingComplexToBoolean; break; |
| 2010 | } |
Anders Carlsson | bc0e078 | 2009-11-23 20:04:44 +0000 | [diff] [blame] | 2011 | |
| 2012 | ImpCastExprToType(From, Context.BoolTy, Kind); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2013 | break; |
Anders Carlsson | bc0e078 | 2009-11-23 20:04:44 +0000 | [diff] [blame] | 2014 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2015 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 2016 | case ICK_Derived_To_Base: { |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2017 | CXXCastPath BasePath; |
Douglas Gregor | b7a86f5 | 2009-11-06 01:02:41 +0000 | [diff] [blame] | 2018 | if (CheckDerivedToBaseConversion(From->getType(), |
| 2019 | ToType.getNonReferenceType(), |
| 2020 | From->getLocStart(), |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 2021 | From->getSourceRange(), |
| 2022 | &BasePath, |
Sebastian Redl | a82e4ae | 2009-11-14 21:15:49 +0000 | [diff] [blame] | 2023 | IgnoreBaseAccess)) |
Douglas Gregor | b7a86f5 | 2009-11-06 01:02:41 +0000 | [diff] [blame] | 2024 | return true; |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 2025 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2026 | ImpCastExprToType(From, ToType.getNonReferenceType(), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2027 | CK_DerivedToBase, CastCategory(From), |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2028 | &BasePath); |
Douglas Gregor | b7a86f5 | 2009-11-06 01:02:41 +0000 | [diff] [blame] | 2029 | break; |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 2030 | } |
| 2031 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 2032 | case ICK_Vector_Conversion: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2033 | ImpCastExprToType(From, ToType, CK_BitCast); |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 2034 | break; |
| 2035 | |
| 2036 | case ICK_Vector_Splat: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2037 | ImpCastExprToType(From, ToType, CK_VectorSplat); |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 2038 | break; |
| 2039 | |
| 2040 | case ICK_Complex_Real: |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2041 | // Case 1. x -> _Complex y |
| 2042 | if (const ComplexType *ToComplex = ToType->getAs<ComplexType>()) { |
| 2043 | QualType ElType = ToComplex->getElementType(); |
| 2044 | bool isFloatingComplex = ElType->isRealFloatingType(); |
| 2045 | |
| 2046 | // x -> y |
| 2047 | if (Context.hasSameUnqualifiedType(ElType, From->getType())) { |
| 2048 | // do nothing |
| 2049 | } else if (From->getType()->isRealFloatingType()) { |
| 2050 | ImpCastExprToType(From, ElType, |
| 2051 | isFloatingComplex ? CK_FloatingCast : CK_FloatingToIntegral); |
| 2052 | } else { |
| 2053 | assert(From->getType()->isIntegerType()); |
| 2054 | ImpCastExprToType(From, ElType, |
| 2055 | isFloatingComplex ? CK_IntegralToFloating : CK_IntegralCast); |
| 2056 | } |
| 2057 | // y -> _Complex y |
| 2058 | ImpCastExprToType(From, ToType, |
| 2059 | isFloatingComplex ? CK_FloatingRealToComplex |
| 2060 | : CK_IntegralRealToComplex); |
| 2061 | |
| 2062 | // Case 2. _Complex x -> y |
| 2063 | } else { |
| 2064 | const ComplexType *FromComplex = From->getType()->getAs<ComplexType>(); |
| 2065 | assert(FromComplex); |
| 2066 | |
| 2067 | QualType ElType = FromComplex->getElementType(); |
| 2068 | bool isFloatingComplex = ElType->isRealFloatingType(); |
| 2069 | |
| 2070 | // _Complex x -> x |
| 2071 | ImpCastExprToType(From, ElType, |
| 2072 | isFloatingComplex ? CK_FloatingComplexToReal |
| 2073 | : CK_IntegralComplexToReal); |
| 2074 | |
| 2075 | // x -> y |
| 2076 | if (Context.hasSameUnqualifiedType(ElType, ToType)) { |
| 2077 | // do nothing |
| 2078 | } else if (ToType->isRealFloatingType()) { |
| 2079 | ImpCastExprToType(From, ToType, |
| 2080 | isFloatingComplex ? CK_FloatingCast : CK_IntegralToFloating); |
| 2081 | } else { |
| 2082 | assert(ToType->isIntegerType()); |
| 2083 | ImpCastExprToType(From, ToType, |
| 2084 | isFloatingComplex ? CK_FloatingToIntegral : CK_IntegralCast); |
| 2085 | } |
| 2086 | } |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 2087 | break; |
| 2088 | |
| 2089 | case ICK_Lvalue_To_Rvalue: |
| 2090 | case ICK_Array_To_Pointer: |
| 2091 | case ICK_Function_To_Pointer: |
| 2092 | case ICK_Qualification: |
| 2093 | case ICK_Num_Conversion_Kinds: |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2094 | assert(false && "Improper second standard conversion"); |
| 2095 | break; |
| 2096 | } |
| 2097 | |
| 2098 | switch (SCS.Third) { |
| 2099 | case ICK_Identity: |
| 2100 | // Nothing to do. |
| 2101 | break; |
| 2102 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2103 | case ICK_Qualification: { |
| 2104 | // The qualification keeps the category of the inner expression, unless the |
| 2105 | // target type isn't a reference. |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2106 | ExprValueKind VK = ToType->isReferenceType() ? |
| 2107 | CastCategory(From) : VK_RValue; |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 2108 | ImpCastExprToType(From, ToType.getNonLValueExprType(Context), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2109 | CK_NoOp, VK); |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 2110 | |
| 2111 | if (SCS.DeprecatedStringLiteralToCharPtr) |
| 2112 | Diag(From->getLocStart(), diag::warn_deprecated_string_literal_conversion) |
| 2113 | << ToType.getNonReferenceType(); |
| 2114 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2115 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2116 | } |
| 2117 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2118 | default: |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 2119 | assert(false && "Improper third standard conversion"); |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2120 | break; |
| 2121 | } |
| 2122 | |
| 2123 | return false; |
| 2124 | } |
| 2125 | |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2126 | ExprResult Sema::ActOnUnaryTypeTrait(UnaryTypeTrait UTT, |
Douglas Gregor | 3d37c0a | 2010-09-09 16:14:44 +0000 | [diff] [blame] | 2127 | SourceLocation KWLoc, |
| 2128 | ParsedType Ty, |
| 2129 | SourceLocation RParen) { |
| 2130 | TypeSourceInfo *TSInfo; |
| 2131 | QualType T = GetTypeFromParser(Ty, &TSInfo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2132 | |
Douglas Gregor | 3d37c0a | 2010-09-09 16:14:44 +0000 | [diff] [blame] | 2133 | if (!TSInfo) |
| 2134 | TSInfo = Context.getTrivialTypeSourceInfo(T); |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2135 | return BuildUnaryTypeTrait(UTT, KWLoc, TSInfo, RParen); |
Douglas Gregor | 3d37c0a | 2010-09-09 16:14:44 +0000 | [diff] [blame] | 2136 | } |
| 2137 | |
Sebastian Redl | f8aca86 | 2010-09-14 23:40:14 +0000 | [diff] [blame] | 2138 | static bool EvaluateUnaryTypeTrait(Sema &Self, UnaryTypeTrait UTT, QualType T, |
| 2139 | SourceLocation KeyLoc) { |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2140 | assert(!T->isDependentType() && |
| 2141 | "Cannot evaluate traits for dependent types."); |
| 2142 | ASTContext &C = Self.Context; |
| 2143 | switch(UTT) { |
| 2144 | default: assert(false && "Unknown type trait or not implemented"); |
| 2145 | case UTT_IsPOD: return T->isPODType(); |
| 2146 | case UTT_IsLiteral: return T->isLiteralType(); |
| 2147 | case UTT_IsClass: // Fallthrough |
| 2148 | case UTT_IsUnion: |
| 2149 | if (const RecordType *Record = T->getAs<RecordType>()) { |
| 2150 | bool Union = Record->getDecl()->isUnion(); |
| 2151 | return UTT == UTT_IsUnion ? Union : !Union; |
| 2152 | } |
| 2153 | return false; |
| 2154 | case UTT_IsEnum: return T->isEnumeralType(); |
| 2155 | case UTT_IsPolymorphic: |
| 2156 | if (const RecordType *Record = T->getAs<RecordType>()) { |
| 2157 | // Type traits are only parsed in C++, so we've got CXXRecords. |
| 2158 | return cast<CXXRecordDecl>(Record->getDecl())->isPolymorphic(); |
| 2159 | } |
| 2160 | return false; |
| 2161 | case UTT_IsAbstract: |
| 2162 | if (const RecordType *RT = T->getAs<RecordType>()) |
| 2163 | return cast<CXXRecordDecl>(RT->getDecl())->isAbstract(); |
| 2164 | return false; |
| 2165 | case UTT_IsEmpty: |
| 2166 | if (const RecordType *Record = T->getAs<RecordType>()) { |
| 2167 | return !Record->getDecl()->isUnion() |
| 2168 | && cast<CXXRecordDecl>(Record->getDecl())->isEmpty(); |
| 2169 | } |
| 2170 | return false; |
| 2171 | case UTT_HasTrivialConstructor: |
| 2172 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 2173 | // If __is_pod (type) is true then the trait is true, else if type is |
| 2174 | // a cv class or union type (or array thereof) with a trivial default |
| 2175 | // constructor ([class.ctor]) then the trait is true, else it is false. |
| 2176 | if (T->isPODType()) |
| 2177 | return true; |
| 2178 | if (const RecordType *RT = |
| 2179 | C.getBaseElementType(T)->getAs<RecordType>()) |
| 2180 | return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialConstructor(); |
| 2181 | return false; |
| 2182 | case UTT_HasTrivialCopy: |
| 2183 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 2184 | // If __is_pod (type) is true or type is a reference type then |
| 2185 | // the trait is true, else if type is a cv class or union type |
| 2186 | // with a trivial copy constructor ([class.copy]) then the trait |
| 2187 | // is true, else it is false. |
| 2188 | if (T->isPODType() || T->isReferenceType()) |
| 2189 | return true; |
| 2190 | if (const RecordType *RT = T->getAs<RecordType>()) |
| 2191 | return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialCopyConstructor(); |
| 2192 | return false; |
| 2193 | case UTT_HasTrivialAssign: |
| 2194 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 2195 | // If type is const qualified or is a reference type then the |
| 2196 | // trait is false. Otherwise if __is_pod (type) is true then the |
| 2197 | // trait is true, else if type is a cv class or union type with |
| 2198 | // a trivial copy assignment ([class.copy]) then the trait is |
| 2199 | // true, else it is false. |
| 2200 | // Note: the const and reference restrictions are interesting, |
| 2201 | // given that const and reference members don't prevent a class |
| 2202 | // from having a trivial copy assignment operator (but do cause |
| 2203 | // errors if the copy assignment operator is actually used, q.v. |
| 2204 | // [class.copy]p12). |
| 2205 | |
| 2206 | if (C.getBaseElementType(T).isConstQualified()) |
| 2207 | return false; |
| 2208 | if (T->isPODType()) |
| 2209 | return true; |
| 2210 | if (const RecordType *RT = T->getAs<RecordType>()) |
| 2211 | return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialCopyAssignment(); |
| 2212 | return false; |
| 2213 | case UTT_HasTrivialDestructor: |
| 2214 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 2215 | // If __is_pod (type) is true or type is a reference type |
| 2216 | // then the trait is true, else if type is a cv class or union |
| 2217 | // type (or array thereof) with a trivial destructor |
| 2218 | // ([class.dtor]) then the trait is true, else it is |
| 2219 | // false. |
| 2220 | if (T->isPODType() || T->isReferenceType()) |
| 2221 | return true; |
| 2222 | if (const RecordType *RT = |
| 2223 | C.getBaseElementType(T)->getAs<RecordType>()) |
| 2224 | return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialDestructor(); |
| 2225 | return false; |
| 2226 | // TODO: Propagate nothrowness for implicitly declared special members. |
| 2227 | case UTT_HasNothrowAssign: |
| 2228 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 2229 | // If type is const qualified or is a reference type then the |
| 2230 | // trait is false. Otherwise if __has_trivial_assign (type) |
| 2231 | // is true then the trait is true, else if type is a cv class |
| 2232 | // or union type with copy assignment operators that are known |
| 2233 | // not to throw an exception then the trait is true, else it is |
| 2234 | // false. |
| 2235 | if (C.getBaseElementType(T).isConstQualified()) |
| 2236 | return false; |
| 2237 | if (T->isReferenceType()) |
| 2238 | return false; |
| 2239 | if (T->isPODType()) |
| 2240 | return true; |
| 2241 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 2242 | CXXRecordDecl* RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 2243 | if (RD->hasTrivialCopyAssignment()) |
| 2244 | return true; |
| 2245 | |
| 2246 | bool FoundAssign = false; |
| 2247 | bool AllNoThrow = true; |
| 2248 | DeclarationName Name = C.DeclarationNames.getCXXOperatorName(OO_Equal); |
Sebastian Redl | f8aca86 | 2010-09-14 23:40:14 +0000 | [diff] [blame] | 2249 | LookupResult Res(Self, DeclarationNameInfo(Name, KeyLoc), |
| 2250 | Sema::LookupOrdinaryName); |
| 2251 | if (Self.LookupQualifiedName(Res, RD)) { |
| 2252 | for (LookupResult::iterator Op = Res.begin(), OpEnd = Res.end(); |
| 2253 | Op != OpEnd; ++Op) { |
| 2254 | CXXMethodDecl *Operator = cast<CXXMethodDecl>(*Op); |
| 2255 | if (Operator->isCopyAssignmentOperator()) { |
| 2256 | FoundAssign = true; |
| 2257 | const FunctionProtoType *CPT |
| 2258 | = Operator->getType()->getAs<FunctionProtoType>(); |
| 2259 | if (!CPT->hasEmptyExceptionSpec()) { |
| 2260 | AllNoThrow = false; |
| 2261 | break; |
| 2262 | } |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2263 | } |
| 2264 | } |
| 2265 | } |
| 2266 | |
| 2267 | return FoundAssign && AllNoThrow; |
| 2268 | } |
| 2269 | return false; |
| 2270 | case UTT_HasNothrowCopy: |
| 2271 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 2272 | // If __has_trivial_copy (type) is true then the trait is true, else |
| 2273 | // if type is a cv class or union type with copy constructors that are |
| 2274 | // known not to throw an exception then the trait is true, else it is |
| 2275 | // false. |
| 2276 | if (T->isPODType() || T->isReferenceType()) |
| 2277 | return true; |
| 2278 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 2279 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 2280 | if (RD->hasTrivialCopyConstructor()) |
| 2281 | return true; |
| 2282 | |
| 2283 | bool FoundConstructor = false; |
| 2284 | bool AllNoThrow = true; |
| 2285 | unsigned FoundTQs; |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2286 | DeclContext::lookup_const_iterator Con, ConEnd; |
Sebastian Redl | 5f4e899 | 2010-09-13 21:10:20 +0000 | [diff] [blame] | 2287 | for (llvm::tie(Con, ConEnd) = Self.LookupConstructors(RD); |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2288 | Con != ConEnd; ++Con) { |
Sebastian Redl | 08295a5 | 2010-09-13 22:18:28 +0000 | [diff] [blame] | 2289 | // A template constructor is never a copy constructor. |
| 2290 | // FIXME: However, it may actually be selected at the actual overload |
| 2291 | // resolution point. |
| 2292 | if (isa<FunctionTemplateDecl>(*Con)) |
| 2293 | continue; |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2294 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con); |
| 2295 | if (Constructor->isCopyConstructor(FoundTQs)) { |
| 2296 | FoundConstructor = true; |
| 2297 | const FunctionProtoType *CPT |
| 2298 | = Constructor->getType()->getAs<FunctionProtoType>(); |
Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 2299 | // TODO: check whether evaluating default arguments can throw. |
| 2300 | // For now, we'll be conservative and assume that they can throw. |
| 2301 | if (!CPT->hasEmptyExceptionSpec() || CPT->getNumArgs() > 1) { |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2302 | AllNoThrow = false; |
| 2303 | break; |
| 2304 | } |
| 2305 | } |
| 2306 | } |
| 2307 | |
| 2308 | return FoundConstructor && AllNoThrow; |
| 2309 | } |
| 2310 | return false; |
| 2311 | case UTT_HasNothrowConstructor: |
| 2312 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 2313 | // If __has_trivial_constructor (type) is true then the trait is |
| 2314 | // true, else if type is a cv class or union type (or array |
| 2315 | // thereof) with a default constructor that is known not to |
| 2316 | // throw an exception then the trait is true, else it is false. |
| 2317 | if (T->isPODType()) |
| 2318 | return true; |
| 2319 | if (const RecordType *RT = C.getBaseElementType(T)->getAs<RecordType>()) { |
| 2320 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 2321 | if (RD->hasTrivialConstructor()) |
| 2322 | return true; |
| 2323 | |
Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 2324 | DeclContext::lookup_const_iterator Con, ConEnd; |
| 2325 | for (llvm::tie(Con, ConEnd) = Self.LookupConstructors(RD); |
| 2326 | Con != ConEnd; ++Con) { |
Sebastian Redl | 08295a5 | 2010-09-13 22:18:28 +0000 | [diff] [blame] | 2327 | // FIXME: In C++0x, a constructor template can be a default constructor. |
| 2328 | if (isa<FunctionTemplateDecl>(*Con)) |
| 2329 | continue; |
Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 2330 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con); |
| 2331 | if (Constructor->isDefaultConstructor()) { |
| 2332 | const FunctionProtoType *CPT |
| 2333 | = Constructor->getType()->getAs<FunctionProtoType>(); |
| 2334 | // TODO: check whether evaluating default arguments can throw. |
| 2335 | // For now, we'll be conservative and assume that they can throw. |
| 2336 | return CPT->hasEmptyExceptionSpec() && CPT->getNumArgs() == 0; |
| 2337 | } |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2338 | } |
| 2339 | } |
| 2340 | return false; |
| 2341 | case UTT_HasVirtualDestructor: |
| 2342 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: |
| 2343 | // If type is a class type with a virtual destructor ([class.dtor]) |
| 2344 | // then the trait is true, else it is false. |
| 2345 | if (const RecordType *Record = T->getAs<RecordType>()) { |
| 2346 | CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl()); |
Sebastian Redl | f8aca86 | 2010-09-14 23:40:14 +0000 | [diff] [blame] | 2347 | if (CXXDestructorDecl *Destructor = Self.LookupDestructor(RD)) |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2348 | return Destructor->isVirtual(); |
| 2349 | } |
| 2350 | return false; |
| 2351 | } |
| 2352 | } |
| 2353 | |
| 2354 | ExprResult Sema::BuildUnaryTypeTrait(UnaryTypeTrait UTT, |
Douglas Gregor | 3d37c0a | 2010-09-09 16:14:44 +0000 | [diff] [blame] | 2355 | SourceLocation KWLoc, |
| 2356 | TypeSourceInfo *TSInfo, |
| 2357 | SourceLocation RParen) { |
| 2358 | QualType T = TSInfo->getType(); |
| 2359 | |
Anders Carlsson | 3292d5c | 2009-07-07 19:06:02 +0000 | [diff] [blame] | 2360 | // According to http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html |
| 2361 | // 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] | 2362 | // to be complete, an array of unknown bound, or void. |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2363 | if (UTT != UTT_IsClass && UTT != UTT_IsEnum && UTT != UTT_IsUnion) { |
Sebastian Redl | 607a178 | 2010-09-08 00:48:43 +0000 | [diff] [blame] | 2364 | QualType E = T; |
| 2365 | if (T->isIncompleteArrayType()) |
| 2366 | E = Context.getAsArrayType(T)->getElementType(); |
| 2367 | if (!T->isVoidType() && |
| 2368 | RequireCompleteType(KWLoc, E, |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 2369 | diag::err_incomplete_type_used_in_type_trait_expr)) |
Anders Carlsson | 3292d5c | 2009-07-07 19:06:02 +0000 | [diff] [blame] | 2370 | return ExprError(); |
| 2371 | } |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2372 | |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2373 | bool Value = false; |
| 2374 | if (!T->isDependentType()) |
Sebastian Redl | f8aca86 | 2010-09-14 23:40:14 +0000 | [diff] [blame] | 2375 | Value = EvaluateUnaryTypeTrait(*this, UTT, T, KWLoc); |
Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2376 | |
| 2377 | return Owned(new (Context) UnaryTypeTraitExpr(KWLoc, UTT, TSInfo, Value, |
Anders Carlsson | 3292d5c | 2009-07-07 19:06:02 +0000 | [diff] [blame] | 2378 | RParen, Context.BoolTy)); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2379 | } |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 2380 | |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 2381 | ExprResult Sema::ActOnBinaryTypeTrait(BinaryTypeTrait BTT, |
| 2382 | SourceLocation KWLoc, |
| 2383 | ParsedType LhsTy, |
| 2384 | ParsedType RhsTy, |
| 2385 | SourceLocation RParen) { |
| 2386 | TypeSourceInfo *LhsTSInfo; |
| 2387 | QualType LhsT = GetTypeFromParser(LhsTy, &LhsTSInfo); |
| 2388 | if (!LhsTSInfo) |
| 2389 | LhsTSInfo = Context.getTrivialTypeSourceInfo(LhsT); |
| 2390 | |
| 2391 | TypeSourceInfo *RhsTSInfo; |
| 2392 | QualType RhsT = GetTypeFromParser(RhsTy, &RhsTSInfo); |
| 2393 | if (!RhsTSInfo) |
| 2394 | RhsTSInfo = Context.getTrivialTypeSourceInfo(RhsT); |
| 2395 | |
| 2396 | return BuildBinaryTypeTrait(BTT, KWLoc, LhsTSInfo, RhsTSInfo, RParen); |
| 2397 | } |
| 2398 | |
| 2399 | static bool EvaluateBinaryTypeTrait(Sema &Self, BinaryTypeTrait BTT, |
| 2400 | QualType LhsT, QualType RhsT, |
| 2401 | SourceLocation KeyLoc) { |
| 2402 | assert((!LhsT->isDependentType() || RhsT->isDependentType()) && |
| 2403 | "Cannot evaluate traits for dependent types."); |
| 2404 | |
| 2405 | switch(BTT) { |
| 2406 | case BTT_IsBaseOf: |
| 2407 | // C++0x [meta.rel]p2 |
| 2408 | // Base is a base class of Derived without regard to cv-qualifiers or |
| 2409 | // Base and Derived are not unions and name the same class type without |
| 2410 | // regard to cv-qualifiers. |
| 2411 | if (Self.IsDerivedFrom(RhsT, LhsT) || |
| 2412 | (!LhsT->isUnionType() && !RhsT->isUnionType() |
| 2413 | && LhsT->getAsCXXRecordDecl() == RhsT->getAsCXXRecordDecl())) |
| 2414 | return true; |
| 2415 | |
| 2416 | return false; |
Francois Pichet | f187237 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 2417 | case BTT_TypeCompatible: |
| 2418 | return Self.Context.typesAreCompatible(LhsT.getUnqualifiedType(), |
| 2419 | RhsT.getUnqualifiedType()); |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 2420 | } |
| 2421 | llvm_unreachable("Unknown type trait or not implemented"); |
| 2422 | } |
| 2423 | |
| 2424 | ExprResult Sema::BuildBinaryTypeTrait(BinaryTypeTrait BTT, |
| 2425 | SourceLocation KWLoc, |
| 2426 | TypeSourceInfo *LhsTSInfo, |
| 2427 | TypeSourceInfo *RhsTSInfo, |
| 2428 | SourceLocation RParen) { |
| 2429 | QualType LhsT = LhsTSInfo->getType(); |
| 2430 | QualType RhsT = RhsTSInfo->getType(); |
| 2431 | |
| 2432 | if (BTT == BTT_IsBaseOf) { |
| 2433 | // C++0x [meta.rel]p2 |
| 2434 | // If Base and Derived are class types and are different types |
| 2435 | // (ignoring possible cv-qualifiers) then Derived shall be a complete |
| 2436 | // type. [] |
| 2437 | CXXRecordDecl *LhsDecl = LhsT->getAsCXXRecordDecl(); |
| 2438 | CXXRecordDecl *RhsDecl = RhsT->getAsCXXRecordDecl(); |
| 2439 | if (!LhsT->isDependentType() && !RhsT->isDependentType() && |
| 2440 | LhsDecl && RhsDecl && LhsT != RhsT && |
| 2441 | RequireCompleteType(KWLoc, RhsT, |
| 2442 | diag::err_incomplete_type_used_in_type_trait_expr)) |
| 2443 | return ExprError(); |
Francois Pichet | f187237 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 2444 | } else if (BTT == BTT_TypeCompatible) { |
| 2445 | if (getLangOptions().CPlusPlus) { |
| 2446 | Diag(KWLoc, diag::err_types_compatible_p_in_cplusplus) |
| 2447 | << SourceRange(KWLoc, RParen); |
| 2448 | return ExprError(); |
| 2449 | } |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 2450 | } |
| 2451 | |
| 2452 | bool Value = false; |
| 2453 | if (!LhsT->isDependentType() && !RhsT->isDependentType()) |
| 2454 | Value = EvaluateBinaryTypeTrait(*this, BTT, LhsT, RhsT, KWLoc); |
| 2455 | |
Francois Pichet | f187237 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 2456 | // Select trait result type. |
| 2457 | QualType ResultType; |
| 2458 | switch (BTT) { |
| 2459 | default: llvm_unreachable("Unknown type trait or not implemented"); |
| 2460 | case BTT_IsBaseOf: ResultType = Context.BoolTy; break; |
| 2461 | case BTT_TypeCompatible: ResultType = Context.IntTy; break; |
| 2462 | } |
| 2463 | |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 2464 | return Owned(new (Context) BinaryTypeTraitExpr(KWLoc, BTT, LhsTSInfo, |
| 2465 | RhsTSInfo, Value, RParen, |
Francois Pichet | f187237 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 2466 | ResultType)); |
Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 2467 | } |
| 2468 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2469 | QualType Sema::CheckPointerToMemberOperands(Expr *&lex, Expr *&rex, |
| 2470 | ExprValueKind &VK, |
| 2471 | SourceLocation Loc, |
| 2472 | bool isIndirect) { |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 2473 | const char *OpSpelling = isIndirect ? "->*" : ".*"; |
| 2474 | // C++ 5.5p2 |
| 2475 | // The binary operator .* [p3: ->*] binds its second operand, which shall |
| 2476 | // be of type "pointer to member of T" (where T is a completely-defined |
| 2477 | // class type) [...] |
| 2478 | QualType RType = rex->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2479 | const MemberPointerType *MemPtr = RType->getAs<MemberPointerType>(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 2480 | if (!MemPtr) { |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 2481 | Diag(Loc, diag::err_bad_memptr_rhs) |
| 2482 | << OpSpelling << RType << rex->getSourceRange(); |
| 2483 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2484 | } |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 2485 | |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 2486 | QualType Class(MemPtr->getClass(), 0); |
| 2487 | |
Douglas Gregor | 7d520ba | 2010-10-13 20:41:14 +0000 | [diff] [blame] | 2488 | // Note: C++ [expr.mptr.oper]p2-3 says that the class type into which the |
| 2489 | // member pointer points must be completely-defined. However, there is no |
| 2490 | // reason for this semantic distinction, and the rule is not enforced by |
| 2491 | // other compilers. Therefore, we do not check this property, as it is |
| 2492 | // likely to be considered a defect. |
Sebastian Redl | 59fc269 | 2010-04-10 10:14:54 +0000 | [diff] [blame] | 2493 | |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 2494 | // C++ 5.5p2 |
| 2495 | // [...] to its first operand, which shall be of class T or of a class of |
| 2496 | // which T is an unambiguous and accessible base class. [p3: a pointer to |
| 2497 | // such a class] |
| 2498 | QualType LType = lex->getType(); |
| 2499 | if (isIndirect) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2500 | if (const PointerType *Ptr = LType->getAs<PointerType>()) |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2501 | LType = Ptr->getPointeeType(); |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 2502 | else { |
| 2503 | Diag(Loc, diag::err_bad_memptr_lhs) |
Fariborz Jahanian | ef78ac6 | 2009-10-26 20:45:27 +0000 | [diff] [blame] | 2504 | << OpSpelling << 1 << LType |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2505 | << FixItHint::CreateReplacement(SourceRange(Loc), ".*"); |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 2506 | return QualType(); |
| 2507 | } |
| 2508 | } |
| 2509 | |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2510 | if (!Context.hasSameUnqualifiedType(Class, LType)) { |
Sebastian Redl | 17e1d35 | 2010-04-23 17:18:26 +0000 | [diff] [blame] | 2511 | // If we want to check the hierarchy, we need a complete type. |
| 2512 | if (RequireCompleteType(Loc, LType, PDiag(diag::err_bad_memptr_lhs) |
| 2513 | << OpSpelling << (int)isIndirect)) { |
| 2514 | return QualType(); |
| 2515 | } |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 2516 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 2517 | /*DetectVirtual=*/false); |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2518 | // FIXME: Would it be useful to print full ambiguity paths, or is that |
| 2519 | // overkill? |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 2520 | if (!IsDerivedFrom(LType, Class, Paths) || |
| 2521 | Paths.isAmbiguous(Context.getCanonicalType(Class))) { |
| 2522 | Diag(Loc, diag::err_bad_memptr_lhs) << OpSpelling |
Eli Friedman | 3005efe | 2010-01-16 00:00:48 +0000 | [diff] [blame] | 2523 | << (int)isIndirect << lex->getType(); |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 2524 | return QualType(); |
| 2525 | } |
Eli Friedman | 3005efe | 2010-01-16 00:00:48 +0000 | [diff] [blame] | 2526 | // Cast LHS to type of use. |
| 2527 | QualType UseType = isIndirect ? Context.getPointerType(Class) : Class; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2528 | ExprValueKind VK = |
| 2529 | isIndirect ? VK_RValue : CastCategory(lex); |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2530 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2531 | CXXCastPath BasePath; |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 2532 | BuildBasePathArray(Paths, BasePath); |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2533 | ImpCastExprToType(lex, UseType, CK_DerivedToBase, VK, &BasePath); |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 2534 | } |
| 2535 | |
Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 2536 | if (isa<CXXScalarValueInitExpr>(rex->IgnoreParens())) { |
Fariborz Jahanian | 05ebda9 | 2009-11-18 21:54:48 +0000 | [diff] [blame] | 2537 | // Diagnose use of pointer-to-member type which when used as |
| 2538 | // the functional cast in a pointer-to-member expression. |
| 2539 | Diag(Loc, diag::err_pointer_to_member_type) << isIndirect; |
| 2540 | return QualType(); |
| 2541 | } |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2542 | |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 2543 | // C++ 5.5p2 |
| 2544 | // The result is an object or a function of the type specified by the |
| 2545 | // second operand. |
| 2546 | // The cv qualifiers are the union of those in the pointer and the left side, |
| 2547 | // in accordance with 5.5p5 and 5.2.5. |
| 2548 | // FIXME: This returns a dereferenced member function pointer as a normal |
| 2549 | // function type. However, the only operation valid on such functions is |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2550 | // calling them. There's also a GCC extension to get a function pointer to the |
| 2551 | // thing, which is another complication, because this type - unlike the type |
| 2552 | // 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] | 2553 | // argument. |
| 2554 | // We probably need a "MemberFunctionClosureType" or something like that. |
| 2555 | QualType Result = MemPtr->getPointeeType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2556 | Result = Context.getCVRQualifiedType(Result, LType.getCVRQualifiers()); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2557 | |
| 2558 | // C++ [expr.mptr.oper]p6: |
| 2559 | // The result of a .* expression whose second operand is a pointer |
| 2560 | // to a data member is of the same value category as its |
| 2561 | // first operand. The result of a .* expression whose second |
| 2562 | // operand is a pointer to a member function is a prvalue. The |
| 2563 | // result of an ->* expression is an lvalue if its second operand |
| 2564 | // is a pointer to data member and a prvalue otherwise. |
| 2565 | if (Result->isFunctionType()) |
| 2566 | VK = VK_RValue; |
| 2567 | else if (isIndirect) |
| 2568 | VK = VK_LValue; |
| 2569 | else |
| 2570 | VK = lex->getValueKind(); |
| 2571 | |
Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 2572 | return Result; |
| 2573 | } |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2574 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2575 | /// \brief Try to convert a type to another according to C++0x 5.16p3. |
| 2576 | /// |
| 2577 | /// This is part of the parameter validation for the ? operator. If either |
| 2578 | /// value operand is a class type, the two operands are attempted to be |
| 2579 | /// converted to each other. This function does the conversion in one direction. |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2580 | /// It returns true if the program is ill-formed and has already been diagnosed |
| 2581 | /// as such. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2582 | static bool TryClassUnification(Sema &Self, Expr *From, Expr *To, |
| 2583 | SourceLocation QuestionLoc, |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2584 | bool &HaveConversion, |
| 2585 | QualType &ToType) { |
| 2586 | HaveConversion = false; |
| 2587 | ToType = To->getType(); |
| 2588 | |
| 2589 | InitializationKind Kind = InitializationKind::CreateCopy(To->getLocStart(), |
| 2590 | SourceLocation()); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2591 | // C++0x 5.16p3 |
| 2592 | // The process for determining whether an operand expression E1 of type T1 |
| 2593 | // can be converted to match an operand expression E2 of type T2 is defined |
| 2594 | // as follows: |
| 2595 | // -- If E2 is an lvalue: |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 2596 | bool ToIsLvalue = To->isLValue(); |
Douglas Gregor | 0fd8ff7 | 2010-03-26 20:59:55 +0000 | [diff] [blame] | 2597 | if (ToIsLvalue) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2598 | // E1 can be converted to match E2 if E1 can be implicitly converted to |
| 2599 | // type "lvalue reference to T2", subject to the constraint that in the |
| 2600 | // conversion the reference must bind directly to E1. |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2601 | QualType T = Self.Context.getLValueReferenceType(ToType); |
| 2602 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(T); |
| 2603 | |
| 2604 | InitializationSequence InitSeq(Self, Entity, Kind, &From, 1); |
| 2605 | if (InitSeq.isDirectReferenceBinding()) { |
| 2606 | ToType = T; |
| 2607 | HaveConversion = true; |
| 2608 | return false; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2609 | } |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2610 | |
| 2611 | if (InitSeq.isAmbiguous()) |
| 2612 | return InitSeq.Diagnose(Self, Entity, Kind, &From, 1); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2613 | } |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 2614 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2615 | // -- If E2 is an rvalue, or if the conversion above cannot be done: |
| 2616 | // -- if E1 and E2 have class type, and the underlying class types are |
| 2617 | // the same or one is a base class of the other: |
| 2618 | QualType FTy = From->getType(); |
| 2619 | QualType TTy = To->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2620 | const RecordType *FRec = FTy->getAs<RecordType>(); |
| 2621 | const RecordType *TRec = TTy->getAs<RecordType>(); |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2622 | bool FDerivedFromT = FRec && TRec && FRec != TRec && |
| 2623 | Self.IsDerivedFrom(FTy, TTy); |
| 2624 | if (FRec && TRec && |
| 2625 | (FRec == TRec || FDerivedFromT || Self.IsDerivedFrom(TTy, FTy))) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2626 | // E1 can be converted to match E2 if the class of T2 is the |
| 2627 | // same type as, or a base class of, the class of T1, and |
| 2628 | // [cv2 > cv1]. |
John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 2629 | if (FRec == TRec || FDerivedFromT) { |
| 2630 | if (TTy.isAtLeastAsQualifiedAs(FTy)) { |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2631 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(TTy); |
| 2632 | InitializationSequence InitSeq(Self, Entity, Kind, &From, 1); |
| 2633 | if (InitSeq.getKind() != InitializationSequence::FailedSequence) { |
| 2634 | HaveConversion = true; |
| 2635 | return false; |
| 2636 | } |
| 2637 | |
| 2638 | if (InitSeq.isAmbiguous()) |
| 2639 | return InitSeq.Diagnose(Self, Entity, Kind, &From, 1); |
| 2640 | } |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2641 | } |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2642 | |
| 2643 | return false; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2644 | } |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2645 | |
| 2646 | // -- Otherwise: E1 can be converted to match E2 if E1 can be |
| 2647 | // implicitly converted to the type that expression E2 would have |
Douglas Gregor | 0fd8ff7 | 2010-03-26 20:59:55 +0000 | [diff] [blame] | 2648 | // if E2 were converted to an rvalue (or the type it has, if E2 is |
| 2649 | // an rvalue). |
| 2650 | // |
| 2651 | // This actually refers very narrowly to the lvalue-to-rvalue conversion, not |
| 2652 | // to the array-to-pointer or function-to-pointer conversions. |
| 2653 | if (!TTy->getAs<TagType>()) |
| 2654 | TTy = TTy.getUnqualifiedType(); |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2655 | |
| 2656 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(TTy); |
| 2657 | InitializationSequence InitSeq(Self, Entity, Kind, &From, 1); |
| 2658 | HaveConversion = InitSeq.getKind() != InitializationSequence::FailedSequence; |
| 2659 | ToType = TTy; |
| 2660 | if (InitSeq.isAmbiguous()) |
| 2661 | return InitSeq.Diagnose(Self, Entity, Kind, &From, 1); |
| 2662 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2663 | return false; |
| 2664 | } |
| 2665 | |
| 2666 | /// \brief Try to find a common type for two according to C++0x 5.16p5. |
| 2667 | /// |
| 2668 | /// This is part of the parameter validation for the ? operator. If either |
| 2669 | /// value operand is a class type, overload resolution is used to find a |
| 2670 | /// conversion to a common type. |
| 2671 | static bool FindConditionalOverload(Sema &Self, Expr *&LHS, Expr *&RHS, |
| 2672 | SourceLocation Loc) { |
| 2673 | Expr *Args[2] = { LHS, RHS }; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 2674 | OverloadCandidateSet CandidateSet(Loc); |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 2675 | Self.AddBuiltinOperatorCandidates(OO_Conditional, Loc, Args, 2, CandidateSet); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2676 | |
| 2677 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2678 | switch (CandidateSet.BestViableFunction(Self, Loc, Best)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2679 | case OR_Success: |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2680 | // We found a match. Perform the conversions on the arguments and move on. |
| 2681 | if (Self.PerformImplicitConversion(LHS, Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 2682 | Best->Conversions[0], Sema::AA_Converting) || |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2683 | Self.PerformImplicitConversion(RHS, Best->BuiltinTypes.ParamTypes[1], |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 2684 | Best->Conversions[1], Sema::AA_Converting)) |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2685 | break; |
| 2686 | return false; |
| 2687 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2688 | case OR_No_Viable_Function: |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2689 | Self.Diag(Loc, diag::err_typecheck_cond_incompatible_operands) |
| 2690 | << LHS->getType() << RHS->getType() |
| 2691 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 2692 | return true; |
| 2693 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2694 | case OR_Ambiguous: |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2695 | Self.Diag(Loc, diag::err_conditional_ambiguous_ovl) |
| 2696 | << LHS->getType() << RHS->getType() |
| 2697 | << LHS->getSourceRange() << RHS->getSourceRange(); |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2698 | // FIXME: Print the possible common types by printing the return types of |
| 2699 | // the viable candidates. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2700 | break; |
| 2701 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2702 | case OR_Deleted: |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2703 | assert(false && "Conditional operator has only built-in overloads"); |
| 2704 | break; |
| 2705 | } |
| 2706 | return true; |
| 2707 | } |
| 2708 | |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 2709 | /// \brief Perform an "extended" implicit conversion as returned by |
| 2710 | /// TryClassUnification. |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2711 | static bool ConvertForConditional(Sema &Self, Expr *&E, QualType T) { |
| 2712 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(T); |
| 2713 | InitializationKind Kind = InitializationKind::CreateCopy(E->getLocStart(), |
| 2714 | SourceLocation()); |
| 2715 | InitializationSequence InitSeq(Self, Entity, Kind, &E, 1); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2716 | ExprResult Result = InitSeq.Perform(Self, Entity, Kind, MultiExprArg(&E, 1)); |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2717 | if (Result.isInvalid()) |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 2718 | return true; |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2719 | |
| 2720 | E = Result.takeAs<Expr>(); |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 2721 | return false; |
| 2722 | } |
| 2723 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2724 | /// \brief Check the operands of ?: under C++ semantics. |
| 2725 | /// |
| 2726 | /// See C++ [expr.cond]. Note that LHS is never null, even for the GNU x ?: y |
| 2727 | /// extension. In this case, LHS == Cond. (But they're not aliases.) |
| 2728 | QualType Sema::CXXCheckConditionalOperands(Expr *&Cond, Expr *&LHS, Expr *&RHS, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2729 | Expr *&SAVE, ExprValueKind &VK, |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2730 | ExprObjectKind &OK, |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2731 | SourceLocation QuestionLoc) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2732 | // FIXME: Handle C99's complex types, vector types, block pointers and Obj-C++ |
| 2733 | // interface pointers. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2734 | |
| 2735 | // C++0x 5.16p1 |
| 2736 | // The first expression is contextually converted to bool. |
| 2737 | if (!Cond->isTypeDependent()) { |
Fariborz Jahanian | 1fb019b | 2010-09-18 19:38:38 +0000 | [diff] [blame] | 2738 | if (SAVE && Cond->getType()->isArrayType()) { |
| 2739 | QualType CondTy = Cond->getType(); |
| 2740 | CondTy = Context.getArrayDecayedType(CondTy); |
| 2741 | ImpCastExprToType(Cond, CondTy, CK_ArrayToPointerDecay); |
| 2742 | SAVE = LHS = Cond; |
| 2743 | } |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2744 | if (CheckCXXBooleanCondition(Cond)) |
| 2745 | return QualType(); |
| 2746 | } |
| 2747 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2748 | // Assume r-value. |
| 2749 | VK = VK_RValue; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2750 | OK = OK_Ordinary; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2751 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2752 | // Either of the arguments dependent? |
| 2753 | if (LHS->isTypeDependent() || RHS->isTypeDependent()) |
| 2754 | return Context.DependentTy; |
| 2755 | |
| 2756 | // C++0x 5.16p2 |
| 2757 | // If either the second or the third operand has type (cv) void, ... |
| 2758 | QualType LTy = LHS->getType(); |
| 2759 | QualType RTy = RHS->getType(); |
| 2760 | bool LVoid = LTy->isVoidType(); |
| 2761 | bool RVoid = RTy->isVoidType(); |
| 2762 | if (LVoid || RVoid) { |
| 2763 | // ... then the [l2r] conversions are performed on the second and third |
| 2764 | // operands ... |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 2765 | DefaultFunctionArrayLvalueConversion(LHS); |
| 2766 | DefaultFunctionArrayLvalueConversion(RHS); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2767 | LTy = LHS->getType(); |
| 2768 | RTy = RHS->getType(); |
| 2769 | |
| 2770 | // ... and one of the following shall hold: |
| 2771 | // -- The second or the third operand (but not both) is a throw- |
| 2772 | // expression; the result is of the type of the other and is an rvalue. |
| 2773 | bool LThrow = isa<CXXThrowExpr>(LHS); |
| 2774 | bool RThrow = isa<CXXThrowExpr>(RHS); |
| 2775 | if (LThrow && !RThrow) |
| 2776 | return RTy; |
| 2777 | if (RThrow && !LThrow) |
| 2778 | return LTy; |
| 2779 | |
| 2780 | // -- Both the second and third operands have type void; the result is of |
| 2781 | // type void and is an rvalue. |
| 2782 | if (LVoid && RVoid) |
| 2783 | return Context.VoidTy; |
| 2784 | |
| 2785 | // Neither holds, error. |
| 2786 | Diag(QuestionLoc, diag::err_conditional_void_nonvoid) |
| 2787 | << (LVoid ? RTy : LTy) << (LVoid ? 0 : 1) |
| 2788 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 2789 | return QualType(); |
| 2790 | } |
| 2791 | |
| 2792 | // Neither is void. |
| 2793 | |
| 2794 | // C++0x 5.16p3 |
| 2795 | // Otherwise, if the second and third operand have different types, and |
| 2796 | // either has (cv) class type, and attempt is made to convert each of those |
| 2797 | // operands to the other. |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2798 | if (!Context.hasSameType(LTy, RTy) && |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2799 | (LTy->isRecordType() || RTy->isRecordType())) { |
| 2800 | ImplicitConversionSequence ICSLeftToRight, ICSRightToLeft; |
| 2801 | // These return true if a single direction is already ambiguous. |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2802 | QualType L2RType, R2LType; |
| 2803 | bool HaveL2R, HaveR2L; |
| 2804 | if (TryClassUnification(*this, LHS, RHS, QuestionLoc, HaveL2R, L2RType)) |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2805 | return QualType(); |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2806 | if (TryClassUnification(*this, RHS, LHS, QuestionLoc, HaveR2L, R2LType)) |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2807 | return QualType(); |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2808 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2809 | // If both can be converted, [...] the program is ill-formed. |
| 2810 | if (HaveL2R && HaveR2L) { |
| 2811 | Diag(QuestionLoc, diag::err_conditional_ambiguous) |
| 2812 | << LTy << RTy << LHS->getSourceRange() << RHS->getSourceRange(); |
| 2813 | return QualType(); |
| 2814 | } |
| 2815 | |
| 2816 | // If exactly one conversion is possible, that conversion is applied to |
| 2817 | // the chosen operand and the converted operands are used in place of the |
| 2818 | // original operands for the remainder of this section. |
| 2819 | if (HaveL2R) { |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2820 | if (ConvertForConditional(*this, LHS, L2RType)) |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2821 | return QualType(); |
| 2822 | LTy = LHS->getType(); |
| 2823 | } else if (HaveR2L) { |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2824 | if (ConvertForConditional(*this, RHS, R2LType)) |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2825 | return QualType(); |
| 2826 | RTy = RHS->getType(); |
| 2827 | } |
| 2828 | } |
| 2829 | |
| 2830 | // C++0x 5.16p4 |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2831 | // If the second and third operands are glvalues of the same value |
| 2832 | // category and have the same type, the result is of that type and |
| 2833 | // value category and it is a bit-field if the second or the third |
| 2834 | // operand is a bit-field, or if both are bit-fields. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2835 | // We only extend this to bitfields, not to the crazy other kinds of |
| 2836 | // l-values. |
Douglas Gregor | 1927b1f | 2010-04-01 22:47:07 +0000 | [diff] [blame] | 2837 | bool Same = Context.hasSameType(LTy, RTy); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2838 | if (Same && |
| 2839 | LHS->getValueKind() != VK_RValue && |
| 2840 | LHS->getValueKind() == RHS->getValueKind() && |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2841 | (LHS->getObjectKind() == OK_Ordinary || |
| 2842 | LHS->getObjectKind() == OK_BitField) && |
| 2843 | (RHS->getObjectKind() == OK_Ordinary || |
| 2844 | RHS->getObjectKind() == OK_BitField)) { |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2845 | VK = LHS->getValueKind(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2846 | if (LHS->getObjectKind() == OK_BitField || |
| 2847 | RHS->getObjectKind() == OK_BitField) |
| 2848 | OK = OK_BitField; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2849 | return LTy; |
Fariborz Jahanian | 3911a1a | 2010-09-25 01:08:05 +0000 | [diff] [blame] | 2850 | } |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2851 | |
| 2852 | // C++0x 5.16p5 |
| 2853 | // Otherwise, the result is an rvalue. If the second and third operands |
| 2854 | // do not have the same type, and either has (cv) class type, ... |
| 2855 | if (!Same && (LTy->isRecordType() || RTy->isRecordType())) { |
| 2856 | // ... overload resolution is used to determine the conversions (if any) |
| 2857 | // to be applied to the operands. If the overload resolution fails, the |
| 2858 | // program is ill-formed. |
| 2859 | if (FindConditionalOverload(*this, LHS, RHS, QuestionLoc)) |
| 2860 | return QualType(); |
| 2861 | } |
| 2862 | |
| 2863 | // C++0x 5.16p6 |
| 2864 | // LValue-to-rvalue, array-to-pointer, and function-to-pointer standard |
| 2865 | // conversions are performed on the second and third operands. |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 2866 | DefaultFunctionArrayLvalueConversion(LHS); |
| 2867 | DefaultFunctionArrayLvalueConversion(RHS); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2868 | LTy = LHS->getType(); |
| 2869 | RTy = RHS->getType(); |
| 2870 | |
| 2871 | // After those conversions, one of the following shall hold: |
| 2872 | // -- The second and third operands have the same type; the result |
Douglas Gregor | b65a458 | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 2873 | // is of that type. If the operands have class type, the result |
| 2874 | // is a prvalue temporary of the result type, which is |
| 2875 | // copy-initialized from either the second operand or the third |
| 2876 | // operand depending on the value of the first operand. |
| 2877 | if (Context.getCanonicalType(LTy) == Context.getCanonicalType(RTy)) { |
| 2878 | if (LTy->isRecordType()) { |
| 2879 | // The operands have class type. Make a temporary copy. |
| 2880 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(LTy); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2881 | ExprResult LHSCopy = PerformCopyInitialization(Entity, |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2882 | SourceLocation(), |
| 2883 | Owned(LHS)); |
Douglas Gregor | b65a458 | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 2884 | if (LHSCopy.isInvalid()) |
| 2885 | return QualType(); |
| 2886 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2887 | ExprResult RHSCopy = PerformCopyInitialization(Entity, |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2888 | SourceLocation(), |
| 2889 | Owned(RHS)); |
Douglas Gregor | b65a458 | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 2890 | if (RHSCopy.isInvalid()) |
| 2891 | return QualType(); |
| 2892 | |
| 2893 | LHS = LHSCopy.takeAs<Expr>(); |
| 2894 | RHS = RHSCopy.takeAs<Expr>(); |
| 2895 | } |
| 2896 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2897 | return LTy; |
Douglas Gregor | b65a458 | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 2898 | } |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2899 | |
Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 2900 | // Extension: conditional operator involving vector types. |
| 2901 | if (LTy->isVectorType() || RTy->isVectorType()) |
| 2902 | return CheckVectorOperands(QuestionLoc, LHS, RHS); |
| 2903 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2904 | // -- The second and third operands have arithmetic or enumeration type; |
| 2905 | // the usual arithmetic conversions are performed to bring them to a |
| 2906 | // common type, and the result is of that type. |
| 2907 | if (LTy->isArithmeticType() && RTy->isArithmeticType()) { |
| 2908 | UsualArithmeticConversions(LHS, RHS); |
| 2909 | return LHS->getType(); |
| 2910 | } |
| 2911 | |
| 2912 | // -- The second and third operands have pointer type, or one has pointer |
| 2913 | // type and the other is a null pointer constant; pointer conversions |
| 2914 | // and qualification conversions are performed to bring them to their |
| 2915 | // composite pointer type. The result is of the composite pointer type. |
Eli Friedman | de8ac49 | 2010-01-02 22:56:07 +0000 | [diff] [blame] | 2916 | // -- The second and third operands have pointer to member type, or one has |
| 2917 | // pointer to member type and the other is a null pointer constant; |
| 2918 | // pointer to member conversions and qualification conversions are |
| 2919 | // performed to bring them to a common type, whose cv-qualification |
| 2920 | // shall match the cv-qualification of either the second or the third |
| 2921 | // operand. The result is of the common type. |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 2922 | bool NonStandardCompositeType = false; |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 2923 | QualType Composite = FindCompositePointerType(QuestionLoc, LHS, RHS, |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 2924 | isSFINAEContext()? 0 : &NonStandardCompositeType); |
| 2925 | if (!Composite.isNull()) { |
| 2926 | if (NonStandardCompositeType) |
| 2927 | Diag(QuestionLoc, |
| 2928 | diag::ext_typecheck_cond_incompatible_operands_nonstandard) |
| 2929 | << LTy << RTy << Composite |
| 2930 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 2931 | |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2932 | return Composite; |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 2933 | } |
Fariborz Jahanian | 5501636 | 2009-12-10 20:46:08 +0000 | [diff] [blame] | 2934 | |
Douglas Gregor | 1927b1f | 2010-04-01 22:47:07 +0000 | [diff] [blame] | 2935 | // Similarly, attempt to find composite type of two objective-c pointers. |
Fariborz Jahanian | 5501636 | 2009-12-10 20:46:08 +0000 | [diff] [blame] | 2936 | Composite = FindCompositeObjCPointerType(LHS, RHS, QuestionLoc); |
| 2937 | if (!Composite.isNull()) |
| 2938 | return Composite; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2939 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2940 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
| 2941 | << LHS->getType() << RHS->getType() |
| 2942 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 2943 | return QualType(); |
| 2944 | } |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2945 | |
| 2946 | /// \brief Find a merged pointer type and convert the two expressions to it. |
| 2947 | /// |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 2948 | /// This finds the composite pointer type (or member pointer type) for @p E1 |
| 2949 | /// and @p E2 according to C++0x 5.9p2. It converts both expressions to this |
| 2950 | /// type and returns it. |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2951 | /// It does not emit diagnostics. |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 2952 | /// |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 2953 | /// \param Loc The location of the operator requiring these two expressions to |
| 2954 | /// be converted to the composite pointer type. |
| 2955 | /// |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 2956 | /// If \p NonStandardCompositeType is non-NULL, then we are permitted to find |
| 2957 | /// a non-standard (but still sane) composite type to which both expressions |
| 2958 | /// can be converted. When such a type is chosen, \c *NonStandardCompositeType |
| 2959 | /// will be set true. |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 2960 | QualType Sema::FindCompositePointerType(SourceLocation Loc, |
| 2961 | Expr *&E1, Expr *&E2, |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 2962 | bool *NonStandardCompositeType) { |
| 2963 | if (NonStandardCompositeType) |
| 2964 | *NonStandardCompositeType = false; |
| 2965 | |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2966 | assert(getLangOptions().CPlusPlus && "This function assumes C++"); |
| 2967 | QualType T1 = E1->getType(), T2 = E2->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2968 | |
Fariborz Jahanian | 0cedfbd | 2009-12-08 20:04:24 +0000 | [diff] [blame] | 2969 | if (!T1->isAnyPointerType() && !T1->isMemberPointerType() && |
| 2970 | !T2->isAnyPointerType() && !T2->isMemberPointerType()) |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 2971 | return QualType(); |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2972 | |
| 2973 | // C++0x 5.9p2 |
| 2974 | // Pointer conversions and qualification conversions are performed on |
| 2975 | // pointer operands to bring them to their composite pointer type. If |
| 2976 | // one operand is a null pointer constant, the composite pointer type is |
| 2977 | // the type of the other operand. |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2978 | if (E1->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2979 | if (T2->isMemberPointerType()) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2980 | ImpCastExprToType(E1, T2, CK_NullToMemberPointer); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2981 | else |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 2982 | ImpCastExprToType(E1, T2, CK_NullToPointer); |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2983 | return T2; |
| 2984 | } |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2985 | if (E2->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2986 | if (T1->isMemberPointerType()) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2987 | ImpCastExprToType(E2, T1, CK_NullToMemberPointer); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2988 | else |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 2989 | ImpCastExprToType(E2, T1, CK_NullToPointer); |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2990 | return T1; |
| 2991 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2992 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 2993 | // Now both have to be pointers or member pointers. |
Sebastian Redl | a439e6f | 2009-11-16 21:03:45 +0000 | [diff] [blame] | 2994 | if ((!T1->isPointerType() && !T1->isMemberPointerType()) || |
| 2995 | (!T2->isPointerType() && !T2->isMemberPointerType())) |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 2996 | return QualType(); |
| 2997 | |
| 2998 | // Otherwise, of one of the operands has type "pointer to cv1 void," then |
| 2999 | // the other has type "pointer to cv2 T" and the composite pointer type is |
| 3000 | // "pointer to cv12 void," where cv12 is the union of cv1 and cv2. |
| 3001 | // Otherwise, the composite pointer type is a pointer type similar to the |
| 3002 | // type of one of the operands, with a cv-qualification signature that is |
| 3003 | // the union of the cv-qualification signatures of the operand types. |
| 3004 | // In practice, the first part here is redundant; it's subsumed by the second. |
| 3005 | // What we do here is, we build the two possible composite types, and try the |
| 3006 | // conversions in both directions. If only one works, or if the two composite |
| 3007 | // types are the same, we have succeeded. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3008 | // FIXME: extended qualifiers? |
Sebastian Redl | a439e6f | 2009-11-16 21:03:45 +0000 | [diff] [blame] | 3009 | typedef llvm::SmallVector<unsigned, 4> QualifierVector; |
| 3010 | QualifierVector QualifierUnion; |
| 3011 | typedef llvm::SmallVector<std::pair<const Type *, const Type *>, 4> |
| 3012 | ContainingClassVector; |
| 3013 | ContainingClassVector MemberOfClass; |
| 3014 | QualType Composite1 = Context.getCanonicalType(T1), |
| 3015 | Composite2 = Context.getCanonicalType(T2); |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3016 | unsigned NeedConstBefore = 0; |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3017 | do { |
| 3018 | const PointerType *Ptr1, *Ptr2; |
| 3019 | if ((Ptr1 = Composite1->getAs<PointerType>()) && |
| 3020 | (Ptr2 = Composite2->getAs<PointerType>())) { |
| 3021 | Composite1 = Ptr1->getPointeeType(); |
| 3022 | Composite2 = Ptr2->getPointeeType(); |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3023 | |
| 3024 | // If we're allowed to create a non-standard composite type, keep track |
| 3025 | // of where we need to fill in additional 'const' qualifiers. |
| 3026 | if (NonStandardCompositeType && |
| 3027 | Composite1.getCVRQualifiers() != Composite2.getCVRQualifiers()) |
| 3028 | NeedConstBefore = QualifierUnion.size(); |
| 3029 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3030 | QualifierUnion.push_back( |
| 3031 | Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers()); |
| 3032 | MemberOfClass.push_back(std::make_pair((const Type *)0, (const Type *)0)); |
| 3033 | continue; |
| 3034 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3035 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3036 | const MemberPointerType *MemPtr1, *MemPtr2; |
| 3037 | if ((MemPtr1 = Composite1->getAs<MemberPointerType>()) && |
| 3038 | (MemPtr2 = Composite2->getAs<MemberPointerType>())) { |
| 3039 | Composite1 = MemPtr1->getPointeeType(); |
| 3040 | Composite2 = MemPtr2->getPointeeType(); |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3041 | |
| 3042 | // If we're allowed to create a non-standard composite type, keep track |
| 3043 | // of where we need to fill in additional 'const' qualifiers. |
| 3044 | if (NonStandardCompositeType && |
| 3045 | Composite1.getCVRQualifiers() != Composite2.getCVRQualifiers()) |
| 3046 | NeedConstBefore = QualifierUnion.size(); |
| 3047 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3048 | QualifierUnion.push_back( |
| 3049 | Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers()); |
| 3050 | MemberOfClass.push_back(std::make_pair(MemPtr1->getClass(), |
| 3051 | MemPtr2->getClass())); |
| 3052 | continue; |
| 3053 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3054 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3055 | // FIXME: block pointer types? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3056 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3057 | // Cannot unwrap any more types. |
| 3058 | break; |
| 3059 | } while (true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3060 | |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3061 | if (NeedConstBefore && NonStandardCompositeType) { |
| 3062 | // Extension: Add 'const' to qualifiers that come before the first qualifier |
| 3063 | // mismatch, so that our (non-standard!) composite type meets the |
| 3064 | // requirements of C++ [conv.qual]p4 bullet 3. |
| 3065 | for (unsigned I = 0; I != NeedConstBefore; ++I) { |
| 3066 | if ((QualifierUnion[I] & Qualifiers::Const) == 0) { |
| 3067 | QualifierUnion[I] = QualifierUnion[I] | Qualifiers::Const; |
| 3068 | *NonStandardCompositeType = true; |
| 3069 | } |
| 3070 | } |
| 3071 | } |
| 3072 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3073 | // Rewrap the composites as pointers or member pointers with the union CVRs. |
Sebastian Redl | a439e6f | 2009-11-16 21:03:45 +0000 | [diff] [blame] | 3074 | ContainingClassVector::reverse_iterator MOC |
| 3075 | = MemberOfClass.rbegin(); |
| 3076 | for (QualifierVector::reverse_iterator |
| 3077 | I = QualifierUnion.rbegin(), |
| 3078 | E = QualifierUnion.rend(); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3079 | I != E; (void)++I, ++MOC) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3080 | Qualifiers Quals = Qualifiers::fromCVRMask(*I); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3081 | if (MOC->first && MOC->second) { |
| 3082 | // Rebuild member pointer type |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3083 | Composite1 = Context.getMemberPointerType( |
| 3084 | Context.getQualifiedType(Composite1, Quals), |
| 3085 | MOC->first); |
| 3086 | Composite2 = Context.getMemberPointerType( |
| 3087 | Context.getQualifiedType(Composite2, Quals), |
| 3088 | MOC->second); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3089 | } else { |
| 3090 | // Rebuild pointer type |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3091 | Composite1 |
| 3092 | = Context.getPointerType(Context.getQualifiedType(Composite1, Quals)); |
| 3093 | Composite2 |
| 3094 | = Context.getPointerType(Context.getQualifiedType(Composite2, Quals)); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3095 | } |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 3096 | } |
| 3097 | |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 3098 | // Try to convert to the first composite pointer type. |
| 3099 | InitializedEntity Entity1 |
| 3100 | = InitializedEntity::InitializeTemporary(Composite1); |
| 3101 | InitializationKind Kind |
| 3102 | = InitializationKind::CreateCopy(Loc, SourceLocation()); |
| 3103 | InitializationSequence E1ToC1(*this, Entity1, Kind, &E1, 1); |
| 3104 | InitializationSequence E2ToC1(*this, Entity1, Kind, &E2, 1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3105 | |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 3106 | if (E1ToC1 && E2ToC1) { |
| 3107 | // Conversion to Composite1 is viable. |
| 3108 | if (!Context.hasSameType(Composite1, Composite2)) { |
| 3109 | // Composite2 is a different type from Composite1. Check whether |
| 3110 | // Composite2 is also viable. |
| 3111 | InitializedEntity Entity2 |
| 3112 | = InitializedEntity::InitializeTemporary(Composite2); |
| 3113 | InitializationSequence E1ToC2(*this, Entity2, Kind, &E1, 1); |
| 3114 | InitializationSequence E2ToC2(*this, Entity2, Kind, &E2, 1); |
| 3115 | if (E1ToC2 && E2ToC2) { |
| 3116 | // Both Composite1 and Composite2 are viable and are different; |
| 3117 | // this is an ambiguity. |
| 3118 | return QualType(); |
| 3119 | } |
| 3120 | } |
| 3121 | |
| 3122 | // Convert E1 to Composite1 |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3123 | ExprResult E1Result |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 3124 | = E1ToC1.Perform(*this, Entity1, Kind, MultiExprArg(*this,&E1,1)); |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 3125 | if (E1Result.isInvalid()) |
| 3126 | return QualType(); |
| 3127 | E1 = E1Result.takeAs<Expr>(); |
| 3128 | |
| 3129 | // Convert E2 to Composite1 |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3130 | ExprResult E2Result |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 3131 | = E2ToC1.Perform(*this, Entity1, Kind, MultiExprArg(*this,&E2,1)); |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 3132 | if (E2Result.isInvalid()) |
| 3133 | return QualType(); |
| 3134 | E2 = E2Result.takeAs<Expr>(); |
| 3135 | |
| 3136 | return Composite1; |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 3137 | } |
| 3138 | |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 3139 | // Check whether Composite2 is viable. |
| 3140 | InitializedEntity Entity2 |
| 3141 | = InitializedEntity::InitializeTemporary(Composite2); |
| 3142 | InitializationSequence E1ToC2(*this, Entity2, Kind, &E1, 1); |
| 3143 | InitializationSequence E2ToC2(*this, Entity2, Kind, &E2, 1); |
| 3144 | if (!E1ToC2 || !E2ToC2) |
| 3145 | return QualType(); |
| 3146 | |
| 3147 | // Convert E1 to Composite2 |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3148 | ExprResult E1Result |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 3149 | = E1ToC2.Perform(*this, Entity2, Kind, MultiExprArg(*this, &E1, 1)); |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 3150 | if (E1Result.isInvalid()) |
| 3151 | return QualType(); |
| 3152 | E1 = E1Result.takeAs<Expr>(); |
| 3153 | |
| 3154 | // Convert E2 to Composite2 |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3155 | ExprResult E2Result |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 3156 | = E2ToC2.Perform(*this, Entity2, Kind, MultiExprArg(*this, &E2, 1)); |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 3157 | if (E2Result.isInvalid()) |
| 3158 | return QualType(); |
| 3159 | E2 = E2Result.takeAs<Expr>(); |
| 3160 | |
| 3161 | return Composite2; |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 3162 | } |
Anders Carlsson | 165a0a0 | 2009-05-17 18:41:29 +0000 | [diff] [blame] | 3163 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3164 | ExprResult Sema::MaybeBindToTemporary(Expr *E) { |
Douglas Gregor | 19cc1c7 | 2010-11-01 21:10:29 +0000 | [diff] [blame] | 3165 | if (!E) |
| 3166 | return ExprError(); |
| 3167 | |
Anders Carlsson | 089c260 | 2009-08-15 23:41:35 +0000 | [diff] [blame] | 3168 | if (!Context.getLangOptions().CPlusPlus) |
| 3169 | return Owned(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3170 | |
Douglas Gregor | 5132655 | 2009-12-24 18:51:59 +0000 | [diff] [blame] | 3171 | assert(!isa<CXXBindTemporaryExpr>(E) && "Double-bound temporary?"); |
| 3172 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3173 | const RecordType *RT = E->getType()->getAs<RecordType>(); |
Anders Carlsson | def1199 | 2009-05-30 20:36:53 +0000 | [diff] [blame] | 3174 | if (!RT) |
| 3175 | return Owned(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3176 | |
Anders Carlsson | 0ea4dfd | 2010-07-16 21:18:37 +0000 | [diff] [blame] | 3177 | // If this is the result of a call or an Objective-C message send expression, |
| 3178 | // 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] | 3179 | if (CallExpr *CE = dyn_cast<CallExpr>(E)) { |
Anders Carlsson | 0ea4dfd | 2010-07-16 21:18:37 +0000 | [diff] [blame] | 3180 | if (CE->getCallReturnType()->isReferenceType()) |
Anders Carlsson | 283e4d5 | 2009-09-14 01:30:44 +0000 | [diff] [blame] | 3181 | return Owned(E); |
Anders Carlsson | 0ea4dfd | 2010-07-16 21:18:37 +0000 | [diff] [blame] | 3182 | } else if (ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(E)) { |
| 3183 | if (const ObjCMethodDecl *MD = ME->getMethodDecl()) { |
| 3184 | if (MD->getResultType()->isReferenceType()) |
| 3185 | return Owned(E); |
| 3186 | } |
Anders Carlsson | 283e4d5 | 2009-09-14 01:30:44 +0000 | [diff] [blame] | 3187 | } |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 3188 | |
| 3189 | // That should be enough to guarantee that this type is complete. |
| 3190 | // If it has a trivial destructor, we can avoid the extra copy. |
| 3191 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
John McCall | 507384f | 2010-08-12 02:40:37 +0000 | [diff] [blame] | 3192 | if (RD->isInvalidDecl() || RD->hasTrivialDestructor()) |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 3193 | return Owned(E); |
| 3194 | |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 3195 | CXXTemporary *Temp = CXXTemporary::Create(Context, LookupDestructor(RD)); |
Anders Carlsson | 860306e | 2009-05-30 21:21:49 +0000 | [diff] [blame] | 3196 | ExprTemporaries.push_back(Temp); |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 3197 | if (CXXDestructorDecl *Destructor = LookupDestructor(RD)) { |
Fariborz Jahanian | a83f7ed | 2009-08-03 19:13:25 +0000 | [diff] [blame] | 3198 | MarkDeclarationReferenced(E->getExprLoc(), Destructor); |
John McCall | c91cc66 | 2010-04-07 00:41:46 +0000 | [diff] [blame] | 3199 | CheckDestructorAccess(E->getExprLoc(), Destructor, |
| 3200 | PDiag(diag::err_access_dtor_temp) |
| 3201 | << E->getType()); |
| 3202 | } |
Anders Carlsson | def1199 | 2009-05-30 20:36:53 +0000 | [diff] [blame] | 3203 | // FIXME: Add the temporary to the temporaries vector. |
| 3204 | return Owned(CXXBindTemporaryExpr::Create(Context, Temp, E)); |
| 3205 | } |
| 3206 | |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 3207 | Expr *Sema::MaybeCreateExprWithCleanups(Expr *SubExpr) { |
Anders Carlsson | 99ba36d | 2009-06-05 15:38:08 +0000 | [diff] [blame] | 3208 | assert(SubExpr && "sub expression can't be null!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3209 | |
Douglas Gregor | 1f5f3a4 | 2009-12-03 17:10:37 +0000 | [diff] [blame] | 3210 | unsigned FirstTemporary = ExprEvalContexts.back().NumTemporaries; |
| 3211 | assert(ExprTemporaries.size() >= FirstTemporary); |
| 3212 | if (ExprTemporaries.size() == FirstTemporary) |
Anders Carlsson | 99ba36d | 2009-06-05 15:38:08 +0000 | [diff] [blame] | 3213 | return SubExpr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3214 | |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 3215 | Expr *E = ExprWithCleanups::Create(Context, SubExpr, |
| 3216 | &ExprTemporaries[FirstTemporary], |
| 3217 | ExprTemporaries.size() - FirstTemporary); |
Douglas Gregor | 1f5f3a4 | 2009-12-03 17:10:37 +0000 | [diff] [blame] | 3218 | ExprTemporaries.erase(ExprTemporaries.begin() + FirstTemporary, |
| 3219 | ExprTemporaries.end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3220 | |
Anders Carlsson | 99ba36d | 2009-06-05 15:38:08 +0000 | [diff] [blame] | 3221 | return E; |
| 3222 | } |
| 3223 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3224 | ExprResult |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 3225 | Sema::MaybeCreateExprWithCleanups(ExprResult SubExpr) { |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 3226 | if (SubExpr.isInvalid()) |
| 3227 | return ExprError(); |
| 3228 | |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 3229 | return Owned(MaybeCreateExprWithCleanups(SubExpr.take())); |
Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 3230 | } |
| 3231 | |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 3232 | Stmt *Sema::MaybeCreateStmtWithCleanups(Stmt *SubStmt) { |
Argyrios Kyrtzidis | bf8cafa | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 3233 | assert(SubStmt && "sub statement can't be null!"); |
| 3234 | |
| 3235 | unsigned FirstTemporary = ExprEvalContexts.back().NumTemporaries; |
| 3236 | assert(ExprTemporaries.size() >= FirstTemporary); |
| 3237 | if (ExprTemporaries.size() == FirstTemporary) |
| 3238 | return SubStmt; |
| 3239 | |
| 3240 | // FIXME: In order to attach the temporaries, wrap the statement into |
| 3241 | // a StmtExpr; currently this is only used for asm statements. |
| 3242 | // This is hacky, either create a new CXXStmtWithTemporaries statement or |
| 3243 | // a new AsmStmtWithTemporaries. |
| 3244 | CompoundStmt *CompStmt = new (Context) CompoundStmt(Context, &SubStmt, 1, |
| 3245 | SourceLocation(), |
| 3246 | SourceLocation()); |
| 3247 | Expr *E = new (Context) StmtExpr(CompStmt, Context.VoidTy, SourceLocation(), |
| 3248 | SourceLocation()); |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 3249 | return MaybeCreateExprWithCleanups(E); |
Argyrios Kyrtzidis | bf8cafa | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 3250 | } |
| 3251 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3252 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3253 | Sema::ActOnStartCXXMemberReference(Scope *S, Expr *Base, SourceLocation OpLoc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3254 | tok::TokenKind OpKind, ParsedType &ObjectType, |
Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 3255 | bool &MayBePseudoDestructor) { |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 3256 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3257 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3258 | if (Result.isInvalid()) return ExprError(); |
| 3259 | Base = Result.get(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3260 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3261 | QualType BaseType = Base->getType(); |
Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 3262 | MayBePseudoDestructor = false; |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 3263 | if (BaseType->isDependentType()) { |
Douglas Gregor | 43d8863 | 2009-11-04 22:49:18 +0000 | [diff] [blame] | 3264 | // If we have a pointer to a dependent type and are using the -> operator, |
| 3265 | // the object type is the type that the pointer points to. We might still |
| 3266 | // have enough information about that type to do something useful. |
| 3267 | if (OpKind == tok::arrow) |
| 3268 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) |
| 3269 | BaseType = Ptr->getPointeeType(); |
| 3270 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3271 | ObjectType = ParsedType::make(BaseType); |
Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 3272 | MayBePseudoDestructor = true; |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3273 | return Owned(Base); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 3274 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3275 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 3276 | // C++ [over.match.oper]p8: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3277 | // [...] When operator->returns, the operator-> is applied to the value |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 3278 | // returned, with the original second operand. |
| 3279 | if (OpKind == tok::arrow) { |
John McCall | c4e8321 | 2009-09-30 01:01:30 +0000 | [diff] [blame] | 3280 | // The set of types we've considered so far. |
John McCall | 432887f | 2009-09-30 01:30:54 +0000 | [diff] [blame] | 3281 | llvm::SmallPtrSet<CanQualType,8> CTypes; |
Fariborz Jahanian | 7a8233a | 2009-09-30 17:46:20 +0000 | [diff] [blame] | 3282 | llvm::SmallVector<SourceLocation, 8> Locations; |
John McCall | 432887f | 2009-09-30 01:30:54 +0000 | [diff] [blame] | 3283 | CTypes.insert(Context.getCanonicalType(BaseType)); |
John McCall | c4e8321 | 2009-09-30 01:01:30 +0000 | [diff] [blame] | 3284 | |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 3285 | while (BaseType->isRecordType()) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3286 | Result = BuildOverloadedArrowExpr(S, Base, OpLoc); |
| 3287 | if (Result.isInvalid()) |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 3288 | return ExprError(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3289 | Base = Result.get(); |
| 3290 | if (CXXOperatorCallExpr *OpCall = dyn_cast<CXXOperatorCallExpr>(Base)) |
Anders Carlsson | de699e5 | 2009-10-13 22:55:59 +0000 | [diff] [blame] | 3291 | Locations.push_back(OpCall->getDirectCallee()->getLocation()); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3292 | BaseType = Base->getType(); |
John McCall | c4e8321 | 2009-09-30 01:01:30 +0000 | [diff] [blame] | 3293 | CanQualType CBaseType = Context.getCanonicalType(BaseType); |
John McCall | 432887f | 2009-09-30 01:30:54 +0000 | [diff] [blame] | 3294 | if (!CTypes.insert(CBaseType)) { |
Fariborz Jahanian | 4a4e345 | 2009-09-30 00:19:41 +0000 | [diff] [blame] | 3295 | Diag(OpLoc, diag::err_operator_arrow_circular); |
Fariborz Jahanian | 7a8233a | 2009-09-30 17:46:20 +0000 | [diff] [blame] | 3296 | for (unsigned i = 0; i < Locations.size(); i++) |
| 3297 | Diag(Locations[i], diag::note_declared_at); |
Fariborz Jahanian | 4a4e345 | 2009-09-30 00:19:41 +0000 | [diff] [blame] | 3298 | return ExprError(); |
| 3299 | } |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 3300 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3301 | |
Douglas Gregor | 31658df | 2009-11-20 19:58:21 +0000 | [diff] [blame] | 3302 | if (BaseType->isPointerType()) |
| 3303 | BaseType = BaseType->getPointeeType(); |
| 3304 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3305 | |
| 3306 | // 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] | 3307 | // vector types or Objective-C interfaces. Just return early and let |
| 3308 | // ActOnMemberReferenceExpr do the work. |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 3309 | if (!BaseType->isRecordType()) { |
| 3310 | // C++ [basic.lookup.classref]p2: |
| 3311 | // [...] If the type of the object expression is of pointer to scalar |
| 3312 | // type, the unqualified-id is looked up in the context of the complete |
| 3313 | // postfix-expression. |
Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 3314 | // |
| 3315 | // This also indicates that we should be parsing a |
| 3316 | // pseudo-destructor-name. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3317 | ObjectType = ParsedType(); |
Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 3318 | MayBePseudoDestructor = true; |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3319 | return Owned(Base); |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 3320 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3321 | |
Douglas Gregor | 03c5705 | 2009-11-17 05:17:33 +0000 | [diff] [blame] | 3322 | // The object type must be complete (or dependent). |
| 3323 | if (!BaseType->isDependentType() && |
| 3324 | RequireCompleteType(OpLoc, BaseType, |
| 3325 | PDiag(diag::err_incomplete_member_access))) |
| 3326 | return ExprError(); |
| 3327 | |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 3328 | // C++ [basic.lookup.classref]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3329 | // 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] | 3330 | // 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] | 3331 | // type C (or of pointer to a class type C), the unqualified-id is looked |
| 3332 | // up in the scope of class C. [...] |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3333 | ObjectType = ParsedType::make(BaseType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3334 | return move(Base); |
Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 3335 | } |
| 3336 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3337 | ExprResult Sema::DiagnoseDtorReference(SourceLocation NameLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3338 | Expr *MemExpr) { |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3339 | SourceLocation ExpectedLParenLoc = PP.getLocForEndOfToken(NameLoc); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3340 | Diag(MemExpr->getLocStart(), diag::err_dtor_expr_without_call) |
| 3341 | << isa<CXXPseudoDestructorExpr>(MemExpr) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3342 | << FixItHint::CreateInsertion(ExpectedLParenLoc, "()"); |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3343 | |
| 3344 | return ActOnCallExpr(/*Scope*/ 0, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3345 | MemExpr, |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3346 | /*LPLoc*/ ExpectedLParenLoc, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3347 | MultiExprArg(), |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3348 | /*RPLoc*/ ExpectedLParenLoc); |
| 3349 | } |
Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 3350 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3351 | ExprResult Sema::BuildPseudoDestructorExpr(Expr *Base, |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3352 | SourceLocation OpLoc, |
| 3353 | tok::TokenKind OpKind, |
| 3354 | const CXXScopeSpec &SS, |
Douglas Gregor | 26d4ac9 | 2010-02-24 23:40:28 +0000 | [diff] [blame] | 3355 | TypeSourceInfo *ScopeTypeInfo, |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3356 | SourceLocation CCLoc, |
Douglas Gregor | fce46ee | 2010-02-24 23:50:37 +0000 | [diff] [blame] | 3357 | SourceLocation TildeLoc, |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 3358 | PseudoDestructorTypeStorage Destructed, |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3359 | bool HasTrailingLParen) { |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 3360 | TypeSourceInfo *DestructedTypeInfo = Destructed.getTypeSourceInfo(); |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3361 | |
| 3362 | // C++ [expr.pseudo]p2: |
| 3363 | // The left-hand side of the dot operator shall be of scalar type. The |
| 3364 | // left-hand side of the arrow operator shall be of pointer to scalar type. |
| 3365 | // This scalar type is the object type. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3366 | QualType ObjectType = Base->getType(); |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3367 | if (OpKind == tok::arrow) { |
| 3368 | if (const PointerType *Ptr = ObjectType->getAs<PointerType>()) { |
| 3369 | ObjectType = Ptr->getPointeeType(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3370 | } else if (!Base->isTypeDependent()) { |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3371 | // The user wrote "p->" when she probably meant "p."; fix it. |
| 3372 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
| 3373 | << ObjectType << true |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3374 | << FixItHint::CreateReplacement(OpLoc, "."); |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3375 | if (isSFINAEContext()) |
| 3376 | return ExprError(); |
| 3377 | |
| 3378 | OpKind = tok::period; |
| 3379 | } |
| 3380 | } |
| 3381 | |
| 3382 | if (!ObjectType->isDependentType() && !ObjectType->isScalarType()) { |
| 3383 | Diag(OpLoc, diag::err_pseudo_dtor_base_not_scalar) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3384 | << ObjectType << Base->getSourceRange(); |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3385 | return ExprError(); |
| 3386 | } |
| 3387 | |
| 3388 | // C++ [expr.pseudo]p2: |
| 3389 | // [...] The cv-unqualified versions of the object type and of the type |
| 3390 | // designated by the pseudo-destructor-name shall be the same type. |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 3391 | if (DestructedTypeInfo) { |
| 3392 | QualType DestructedType = DestructedTypeInfo->getType(); |
| 3393 | SourceLocation DestructedTypeStart |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 3394 | = DestructedTypeInfo->getTypeLoc().getLocalSourceRange().getBegin(); |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 3395 | if (!DestructedType->isDependentType() && !ObjectType->isDependentType() && |
| 3396 | !Context.hasSameUnqualifiedType(DestructedType, ObjectType)) { |
| 3397 | Diag(DestructedTypeStart, diag::err_pseudo_dtor_type_mismatch) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3398 | << ObjectType << DestructedType << Base->getSourceRange() |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 3399 | << DestructedTypeInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 3400 | |
| 3401 | // Recover by setting the destructed type to the object type. |
| 3402 | DestructedType = ObjectType; |
| 3403 | DestructedTypeInfo = Context.getTrivialTypeSourceInfo(ObjectType, |
| 3404 | DestructedTypeStart); |
| 3405 | Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo); |
| 3406 | } |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3407 | } |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 3408 | |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3409 | // C++ [expr.pseudo]p2: |
| 3410 | // [...] Furthermore, the two type-names in a pseudo-destructor-name of the |
| 3411 | // form |
| 3412 | // |
| 3413 | // ::[opt] nested-name-specifier[opt] type-name :: ~ type-name |
| 3414 | // |
| 3415 | // shall designate the same scalar type. |
| 3416 | if (ScopeTypeInfo) { |
| 3417 | QualType ScopeType = ScopeTypeInfo->getType(); |
| 3418 | if (!ScopeType->isDependentType() && !ObjectType->isDependentType() && |
John McCall | 81e317a | 2010-06-11 17:36:40 +0000 | [diff] [blame] | 3419 | !Context.hasSameUnqualifiedType(ScopeType, ObjectType)) { |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3420 | |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 3421 | Diag(ScopeTypeInfo->getTypeLoc().getLocalSourceRange().getBegin(), |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3422 | diag::err_pseudo_dtor_type_mismatch) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3423 | << ObjectType << ScopeType << Base->getSourceRange() |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 3424 | << ScopeTypeInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3425 | |
| 3426 | ScopeType = QualType(); |
| 3427 | ScopeTypeInfo = 0; |
| 3428 | } |
| 3429 | } |
| 3430 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3431 | Expr *Result |
| 3432 | = new (Context) CXXPseudoDestructorExpr(Context, Base, |
| 3433 | OpKind == tok::arrow, OpLoc, |
| 3434 | SS.getScopeRep(), SS.getRange(), |
| 3435 | ScopeTypeInfo, |
| 3436 | CCLoc, |
| 3437 | TildeLoc, |
| 3438 | Destructed); |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 3439 | |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3440 | if (HasTrailingLParen) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3441 | return Owned(Result); |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3442 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3443 | return DiagnoseDtorReference(Destructed.getLocation(), Result); |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3444 | } |
| 3445 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3446 | ExprResult Sema::ActOnPseudoDestructorExpr(Scope *S, Expr *Base, |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3447 | SourceLocation OpLoc, |
| 3448 | tok::TokenKind OpKind, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3449 | CXXScopeSpec &SS, |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3450 | UnqualifiedId &FirstTypeName, |
| 3451 | SourceLocation CCLoc, |
| 3452 | SourceLocation TildeLoc, |
| 3453 | UnqualifiedId &SecondTypeName, |
| 3454 | bool HasTrailingLParen) { |
| 3455 | assert((FirstTypeName.getKind() == UnqualifiedId::IK_TemplateId || |
| 3456 | FirstTypeName.getKind() == UnqualifiedId::IK_Identifier) && |
| 3457 | "Invalid first type name in pseudo-destructor"); |
| 3458 | assert((SecondTypeName.getKind() == UnqualifiedId::IK_TemplateId || |
| 3459 | SecondTypeName.getKind() == UnqualifiedId::IK_Identifier) && |
| 3460 | "Invalid second type name in pseudo-destructor"); |
| 3461 | |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3462 | // C++ [expr.pseudo]p2: |
| 3463 | // The left-hand side of the dot operator shall be of scalar type. The |
| 3464 | // left-hand side of the arrow operator shall be of pointer to scalar type. |
| 3465 | // This scalar type is the object type. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3466 | QualType ObjectType = Base->getType(); |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3467 | if (OpKind == tok::arrow) { |
| 3468 | if (const PointerType *Ptr = ObjectType->getAs<PointerType>()) { |
| 3469 | ObjectType = Ptr->getPointeeType(); |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 3470 | } else if (!ObjectType->isDependentType()) { |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3471 | // The user wrote "p->" when she probably meant "p."; fix it. |
| 3472 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 3473 | << ObjectType << true |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3474 | << FixItHint::CreateReplacement(OpLoc, "."); |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3475 | if (isSFINAEContext()) |
| 3476 | return ExprError(); |
| 3477 | |
| 3478 | OpKind = tok::period; |
| 3479 | } |
| 3480 | } |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 3481 | |
| 3482 | // Compute the object type that we should use for name lookup purposes. Only |
| 3483 | // record types and dependent types matter. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3484 | ParsedType ObjectTypePtrForLookup; |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 3485 | if (!SS.isSet()) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3486 | if (const Type *T = ObjectType->getAs<RecordType>()) |
| 3487 | ObjectTypePtrForLookup = ParsedType::make(QualType(T, 0)); |
| 3488 | else if (ObjectType->isDependentType()) |
| 3489 | ObjectTypePtrForLookup = ParsedType::make(Context.DependentTy); |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 3490 | } |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3491 | |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3492 | // Convert the name of the type being destructed (following the ~) into a |
| 3493 | // type (with source-location information). |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3494 | QualType DestructedType; |
| 3495 | TypeSourceInfo *DestructedTypeInfo = 0; |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 3496 | PseudoDestructorTypeStorage Destructed; |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3497 | if (SecondTypeName.getKind() == UnqualifiedId::IK_Identifier) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3498 | ParsedType T = getTypeName(*SecondTypeName.Identifier, |
| 3499 | SecondTypeName.StartLocation, |
| 3500 | S, &SS, true, ObjectTypePtrForLookup); |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 3501 | if (!T && |
| 3502 | ((SS.isSet() && !computeDeclContext(SS, false)) || |
| 3503 | (!SS.isSet() && ObjectType->isDependentType()))) { |
| 3504 | // The name of the type being destroyed is a dependent name, and we |
| 3505 | // couldn't find anything useful in scope. Just store the identifier and |
| 3506 | // it's location, and we'll perform (qualified) name lookup again at |
| 3507 | // template instantiation time. |
| 3508 | Destructed = PseudoDestructorTypeStorage(SecondTypeName.Identifier, |
| 3509 | SecondTypeName.StartLocation); |
| 3510 | } else if (!T) { |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3511 | Diag(SecondTypeName.StartLocation, |
| 3512 | diag::err_pseudo_dtor_destructor_non_type) |
| 3513 | << SecondTypeName.Identifier << ObjectType; |
| 3514 | if (isSFINAEContext()) |
| 3515 | return ExprError(); |
| 3516 | |
| 3517 | // Recover by assuming we had the right type all along. |
| 3518 | DestructedType = ObjectType; |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3519 | } else |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3520 | DestructedType = GetTypeFromParser(T, &DestructedTypeInfo); |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3521 | } else { |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3522 | // Resolve the template-id to a type. |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3523 | TemplateIdAnnotation *TemplateId = SecondTypeName.TemplateId; |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3524 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
| 3525 | TemplateId->getTemplateArgs(), |
| 3526 | TemplateId->NumArgs); |
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 3527 | TypeResult T = ActOnTemplateIdType(TemplateId->Template, |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3528 | TemplateId->TemplateNameLoc, |
| 3529 | TemplateId->LAngleLoc, |
| 3530 | TemplateArgsPtr, |
| 3531 | TemplateId->RAngleLoc); |
| 3532 | if (T.isInvalid() || !T.get()) { |
| 3533 | // Recover by assuming we had the right type all along. |
| 3534 | DestructedType = ObjectType; |
| 3535 | } else |
| 3536 | DestructedType = GetTypeFromParser(T.get(), &DestructedTypeInfo); |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3537 | } |
| 3538 | |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3539 | // If we've performed some kind of recovery, (re-)build the type source |
| 3540 | // information. |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 3541 | if (!DestructedType.isNull()) { |
| 3542 | if (!DestructedTypeInfo) |
| 3543 | DestructedTypeInfo = Context.getTrivialTypeSourceInfo(DestructedType, |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3544 | SecondTypeName.StartLocation); |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 3545 | Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo); |
| 3546 | } |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3547 | |
| 3548 | // Convert the name of the scope type (the type prior to '::') into a type. |
| 3549 | TypeSourceInfo *ScopeTypeInfo = 0; |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3550 | QualType ScopeType; |
| 3551 | if (FirstTypeName.getKind() == UnqualifiedId::IK_TemplateId || |
| 3552 | FirstTypeName.Identifier) { |
| 3553 | if (FirstTypeName.getKind() == UnqualifiedId::IK_Identifier) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3554 | ParsedType T = getTypeName(*FirstTypeName.Identifier, |
| 3555 | FirstTypeName.StartLocation, |
| 3556 | S, &SS, false, ObjectTypePtrForLookup); |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3557 | if (!T) { |
| 3558 | Diag(FirstTypeName.StartLocation, |
| 3559 | diag::err_pseudo_dtor_destructor_non_type) |
| 3560 | << FirstTypeName.Identifier << ObjectType; |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3561 | |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3562 | if (isSFINAEContext()) |
| 3563 | return ExprError(); |
| 3564 | |
| 3565 | // Just drop this type. It's unnecessary anyway. |
| 3566 | ScopeType = QualType(); |
| 3567 | } else |
| 3568 | ScopeType = GetTypeFromParser(T, &ScopeTypeInfo); |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3569 | } else { |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3570 | // Resolve the template-id to a type. |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3571 | TemplateIdAnnotation *TemplateId = FirstTypeName.TemplateId; |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3572 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
| 3573 | TemplateId->getTemplateArgs(), |
| 3574 | TemplateId->NumArgs); |
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 3575 | TypeResult T = ActOnTemplateIdType(TemplateId->Template, |
Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 3576 | TemplateId->TemplateNameLoc, |
| 3577 | TemplateId->LAngleLoc, |
| 3578 | TemplateArgsPtr, |
| 3579 | TemplateId->RAngleLoc); |
| 3580 | if (T.isInvalid() || !T.get()) { |
| 3581 | // Recover by dropping this type. |
| 3582 | ScopeType = QualType(); |
| 3583 | } else |
| 3584 | ScopeType = GetTypeFromParser(T.get(), &ScopeTypeInfo); |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3585 | } |
| 3586 | } |
Douglas Gregor | b4a418f | 2010-02-24 23:02:30 +0000 | [diff] [blame] | 3587 | |
| 3588 | if (!ScopeType.isNull() && !ScopeTypeInfo) |
| 3589 | ScopeTypeInfo = Context.getTrivialTypeSourceInfo(ScopeType, |
| 3590 | FirstTypeName.StartLocation); |
| 3591 | |
| 3592 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3593 | return BuildPseudoDestructorExpr(Base, OpLoc, OpKind, SS, |
Douglas Gregor | fce46ee | 2010-02-24 23:50:37 +0000 | [diff] [blame] | 3594 | ScopeTypeInfo, CCLoc, TildeLoc, |
Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 3595 | Destructed, HasTrailingLParen); |
Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 3596 | } |
| 3597 | |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame^] | 3598 | ExprResult Sema::BuildCXXMemberCallExpr(Expr *Exp, NamedDecl *FoundDecl, |
| 3599 | CXXMethodDecl *Method) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 3600 | if (PerformObjectArgumentInitialization(Exp, /*Qualifier=*/0, |
| 3601 | FoundDecl, Method)) |
Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame^] | 3602 | return true; |
Eli Friedman | 772fffa | 2009-12-09 04:53:56 +0000 | [diff] [blame] | 3603 | |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 3604 | MemberExpr *ME = |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3605 | new (Context) MemberExpr(Exp, /*IsArrow=*/false, Method, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3606 | SourceLocation(), Method->getType(), |
| 3607 | VK_RValue, OK_Ordinary); |
| 3608 | QualType ResultType = Method->getResultType(); |
| 3609 | ExprValueKind VK = Expr::getValueKindForType(ResultType); |
| 3610 | ResultType = ResultType.getNonLValueExprType(Context); |
| 3611 | |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3612 | MarkDeclarationReferenced(Exp->getLocStart(), Method); |
| 3613 | CXXMemberCallExpr *CE = |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3614 | new (Context) CXXMemberCallExpr(Context, ME, 0, 0, ResultType, VK, |
Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 3615 | Exp->getLocEnd()); |
Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 3616 | return CE; |
| 3617 | } |
| 3618 | |
Sebastian Redl | 2e15622 | 2010-09-10 20:55:43 +0000 | [diff] [blame] | 3619 | ExprResult Sema::BuildCXXNoexceptExpr(SourceLocation KeyLoc, Expr *Operand, |
| 3620 | SourceLocation RParen) { |
Sebastian Redl | 2e15622 | 2010-09-10 20:55:43 +0000 | [diff] [blame] | 3621 | return Owned(new (Context) CXXNoexceptExpr(Context.BoolTy, Operand, |
| 3622 | Operand->CanThrow(Context), |
| 3623 | KeyLoc, RParen)); |
| 3624 | } |
| 3625 | |
| 3626 | ExprResult Sema::ActOnNoexceptExpr(SourceLocation KeyLoc, SourceLocation, |
| 3627 | Expr *Operand, SourceLocation RParen) { |
| 3628 | return BuildCXXNoexceptExpr(KeyLoc, Operand, RParen); |
Sebastian Redl | 02bc21a | 2010-09-10 20:55:37 +0000 | [diff] [blame] | 3629 | } |
| 3630 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3631 | /// Perform the conversions required for an expression used in a |
| 3632 | /// context that ignores the result. |
| 3633 | void Sema::IgnoredValueConversions(Expr *&E) { |
John McCall | a878cda | 2010-12-02 02:07:15 +0000 | [diff] [blame] | 3634 | // C99 6.3.2.1: |
| 3635 | // [Except in specific positions,] an lvalue that does not have |
| 3636 | // array type is converted to the value stored in the |
| 3637 | // designated object (and is no longer an lvalue). |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3638 | if (E->isRValue()) return; |
John McCall | a878cda | 2010-12-02 02:07:15 +0000 | [diff] [blame] | 3639 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3640 | // We always want to do this on ObjC property references. |
| 3641 | if (E->getObjectKind() == OK_ObjCProperty) { |
| 3642 | ConvertPropertyForRValue(E); |
| 3643 | if (E->isRValue()) return; |
| 3644 | } |
| 3645 | |
| 3646 | // Otherwise, this rule does not apply in C++, at least not for the moment. |
| 3647 | if (getLangOptions().CPlusPlus) return; |
| 3648 | |
| 3649 | // GCC seems to also exclude expressions of incomplete enum type. |
| 3650 | if (const EnumType *T = E->getType()->getAs<EnumType>()) { |
| 3651 | if (!T->getDecl()->isComplete()) { |
| 3652 | // FIXME: stupid workaround for a codegen bug! |
| 3653 | ImpCastExprToType(E, Context.VoidTy, CK_ToVoid); |
| 3654 | return; |
| 3655 | } |
| 3656 | } |
| 3657 | |
| 3658 | DefaultFunctionArrayLvalueConversion(E); |
John McCall | 85515d6 | 2010-12-04 12:29:11 +0000 | [diff] [blame] | 3659 | if (!E->getType()->isVoidType()) |
| 3660 | RequireCompleteType(E->getExprLoc(), E->getType(), |
| 3661 | diag::err_incomplete_type); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3662 | } |
| 3663 | |
| 3664 | ExprResult Sema::ActOnFinishFullExpr(Expr *FullExpr) { |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 3665 | if (!FullExpr) |
| 3666 | return ExprError(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3667 | |
Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 3668 | if (DiagnoseUnexpandedParameterPack(FullExpr)) |
| 3669 | return ExprError(); |
| 3670 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3671 | IgnoredValueConversions(FullExpr); |
John McCall | b4eb64d | 2010-10-08 02:01:28 +0000 | [diff] [blame] | 3672 | CheckImplicitConversions(FullExpr); |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 3673 | return MaybeCreateExprWithCleanups(FullExpr); |
Anders Carlsson | 165a0a0 | 2009-05-17 18:41:29 +0000 | [diff] [blame] | 3674 | } |
Argyrios Kyrtzidis | bf8cafa | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 3675 | |
| 3676 | StmtResult Sema::ActOnFinishFullStmt(Stmt *FullStmt) { |
| 3677 | if (!FullStmt) return StmtError(); |
| 3678 | |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 3679 | return MaybeCreateStmtWithCleanups(FullStmt); |
Argyrios Kyrtzidis | bf8cafa | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 3680 | } |