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