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