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