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