| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- SemaExprCXX.cpp - Semantic Analysis for Expressions --------------===// | 
|  | 2 | // | 
|  | 3 | //                     The LLVM Compiler Infrastructure | 
|  | 4 | // | 
| Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source | 
|  | 6 | // License. See LICENSE.TXT for details. | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // | 
|  | 8 | //===----------------------------------------------------------------------===// | 
|  | 9 | // | 
|  | 10 | //  This file implements semantic analysis for C++ expressions. | 
|  | 11 | // | 
|  | 12 | //===----------------------------------------------------------------------===// | 
|  | 13 |  | 
| John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" | 
| John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 15 | #include "clang/Sema/DeclSpec.h" | 
| Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 16 | #include "clang/Sema/Initialization.h" | 
|  | 17 | #include "clang/Sema/Lookup.h" | 
| John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 18 | #include "clang/Sema/ParsedTemplate.h" | 
| John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 19 | #include "clang/Sema/ScopeInfo.h" | 
| Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 20 | #include "clang/Sema/Scope.h" | 
| John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 21 | #include "clang/Sema/TemplateDeduction.h" | 
| Steve Naroff | 210679c | 2007-08-25 14:02:58 +0000 | [diff] [blame] | 22 | #include "clang/AST/ASTContext.h" | 
| Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 23 | #include "clang/AST/CXXInheritance.h" | 
| John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 24 | #include "clang/AST/DeclObjC.h" | 
| Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 25 | #include "clang/AST/ExprCXX.h" | 
| Fariborz Jahanian | d426662 | 2010-06-16 18:56:04 +0000 | [diff] [blame] | 26 | #include "clang/AST/ExprObjC.h" | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 27 | #include "clang/AST/TypeLoc.h" | 
| Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 28 | #include "clang/Basic/PartialDiagnostic.h" | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 29 | #include "clang/Basic/TargetInfo.h" | 
| Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 30 | #include "clang/Lex/Preprocessor.h" | 
| Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/STLExtras.h" | 
| Chandler Carruth | 73e0a91 | 2011-05-01 07:23:17 +0000 | [diff] [blame] | 32 | #include "llvm/Support/ErrorHandling.h" | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 33 | using namespace clang; | 
| John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 34 | using namespace sema; | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 35 |  | 
| John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 36 | ParsedType Sema::getDestructorName(SourceLocation TildeLoc, | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 37 | IdentifierInfo &II, | 
| John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 38 | SourceLocation NameLoc, | 
|  | 39 | Scope *S, CXXScopeSpec &SS, | 
|  | 40 | ParsedType ObjectTypePtr, | 
|  | 41 | bool EnteringContext) { | 
| Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 42 | // Determine where to perform name lookup. | 
|  | 43 |  | 
|  | 44 | // FIXME: This area of the standard is very messy, and the current | 
|  | 45 | // wording is rather unclear about which scopes we search for the | 
|  | 46 | // destructor name; see core issues 399 and 555. Issue 399 in | 
|  | 47 | // particular shows where the current description of destructor name | 
|  | 48 | // lookup is completely out of line with existing practice, e.g., | 
|  | 49 | // this appears to be ill-formed: | 
|  | 50 | // | 
|  | 51 | //   namespace N { | 
|  | 52 | //     template <typename T> struct S { | 
|  | 53 | //       ~S(); | 
|  | 54 | //     }; | 
|  | 55 | //   } | 
|  | 56 | // | 
|  | 57 | //   void f(N::S<int>* s) { | 
|  | 58 | //     s->N::S<int>::~S(); | 
|  | 59 | //   } | 
|  | 60 | // | 
| Douglas Gregor | 93649fd | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 61 | // See also PR6358 and PR6359. | 
| Sebastian Redl | c0fee50 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 62 | // For this reason, we're currently only doing the C++03 version of this | 
|  | 63 | // 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] | 64 | QualType SearchType; | 
|  | 65 | DeclContext *LookupCtx = 0; | 
|  | 66 | bool isDependent = false; | 
|  | 67 | bool LookInScope = false; | 
|  | 68 |  | 
|  | 69 | // If we have an object type, it's because we are in a | 
|  | 70 | // pseudo-destructor-expression or a member access expression, and | 
|  | 71 | // we know what type we're looking for. | 
|  | 72 | if (ObjectTypePtr) | 
|  | 73 | SearchType = GetTypeFromParser(ObjectTypePtr); | 
|  | 74 |  | 
|  | 75 | if (SS.isSet()) { | 
| Douglas Gregor | 93649fd | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 76 | NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 77 |  | 
| Douglas Gregor | 93649fd | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 78 | bool AlreadySearched = false; | 
|  | 79 | bool LookAtPrefix = true; | 
| Sebastian Redl | c0fee50 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 80 | // C++ [basic.lookup.qual]p6: | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 81 | //   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] | 82 | //   the type-names are looked up as types in the scope designated by the | 
|  | 83 | //   nested-name-specifier. In a qualified-id of the form: | 
| NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 84 | // | 
|  | 85 | //     ::[opt] nested-name-specifier  ~ class-name | 
| Sebastian Redl | c0fee50 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 86 | // | 
|  | 87 | //   where the nested-name-specifier designates a namespace scope, and in | 
| Chandler Carruth | 5e895a8 | 2010-02-21 10:19:54 +0000 | [diff] [blame] | 88 | //   a qualified-id of the form: | 
| Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 89 | // | 
| NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 90 | //     ::opt nested-name-specifier class-name ::  ~ class-name | 
| Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 91 | // | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 92 | //   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] | 93 | //   the nested-name-specifier. | 
| Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 94 | // | 
| Sebastian Redl | c0fee50 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 95 | // Here, we check the first case (completely) and determine whether the | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 96 | // code below is permitted to look at the prefix of the | 
| Sebastian Redl | c0fee50 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 97 | // nested-name-specifier. | 
|  | 98 | DeclContext *DC = computeDeclContext(SS, EnteringContext); | 
|  | 99 | if (DC && DC->isFileContext()) { | 
|  | 100 | AlreadySearched = true; | 
|  | 101 | LookupCtx = DC; | 
|  | 102 | isDependent = false; | 
|  | 103 | } else if (DC && isa<CXXRecordDecl>(DC)) | 
|  | 104 | LookAtPrefix = false; | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 105 |  | 
| Sebastian Redl | c0fee50 | 2010-07-07 23:17:38 +0000 | [diff] [blame] | 106 | // The second case from the C++03 rules quoted further above. | 
| Douglas Gregor | 93649fd | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 107 | NestedNameSpecifier *Prefix = 0; | 
|  | 108 | if (AlreadySearched) { | 
|  | 109 | // Nothing left to do. | 
|  | 110 | } else if (LookAtPrefix && (Prefix = NNS->getPrefix())) { | 
|  | 111 | CXXScopeSpec PrefixSS; | 
| Douglas Gregor | 7e38494 | 2011-02-25 16:07:42 +0000 | [diff] [blame] | 112 | PrefixSS.Adopt(NestedNameSpecifierLoc(Prefix, SS.location_data())); | 
| Douglas Gregor | 93649fd | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 113 | LookupCtx = computeDeclContext(PrefixSS, EnteringContext); | 
|  | 114 | isDependent = isDependentScopeSpecifier(PrefixSS); | 
| Douglas Gregor | 93649fd | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 115 | } else if (ObjectTypePtr) { | 
| Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 116 | LookupCtx = computeDeclContext(SearchType); | 
|  | 117 | isDependent = SearchType->isDependentType(); | 
|  | 118 | } else { | 
|  | 119 | LookupCtx = computeDeclContext(SS, EnteringContext); | 
| Douglas Gregor | 93649fd | 2010-02-23 00:15:22 +0000 | [diff] [blame] | 120 | isDependent = LookupCtx && LookupCtx->isDependentContext(); | 
| Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 121 | } | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 122 |  | 
| Douglas Gregor | edc9050 | 2010-02-25 04:46:04 +0000 | [diff] [blame] | 123 | LookInScope = false; | 
| Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 124 | } else if (ObjectTypePtr) { | 
|  | 125 | // C++ [basic.lookup.classref]p3: | 
|  | 126 | //   If the unqualified-id is ~type-name, the type-name is looked up | 
|  | 127 | //   in the context of the entire postfix-expression. If the type T | 
|  | 128 | //   of the object expression is of a class type C, the type-name is | 
|  | 129 | //   also looked up in the scope of class C. At least one of the | 
|  | 130 | //   lookups shall find a name that refers to (possibly | 
|  | 131 | //   cv-qualified) T. | 
|  | 132 | LookupCtx = computeDeclContext(SearchType); | 
|  | 133 | isDependent = SearchType->isDependentType(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 134 | assert((isDependent || !SearchType->isIncompleteType()) && | 
| Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 135 | "Caller should have completed object type"); | 
|  | 136 |  | 
|  | 137 | LookInScope = true; | 
|  | 138 | } else { | 
|  | 139 | // Perform lookup into the current scope (only). | 
|  | 140 | LookInScope = true; | 
|  | 141 | } | 
|  | 142 |  | 
| Douglas Gregor | 7ec1873 | 2011-03-04 22:32:08 +0000 | [diff] [blame] | 143 | TypeDecl *NonMatchingTypeDecl = 0; | 
| Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 144 | LookupResult Found(*this, &II, NameLoc, LookupOrdinaryName); | 
|  | 145 | for (unsigned Step = 0; Step != 2; ++Step) { | 
|  | 146 | // Look for the name first in the computed lookup context (if we | 
| Douglas Gregor | 7ec1873 | 2011-03-04 22:32:08 +0000 | [diff] [blame] | 147 | // have one) and, if that fails to find a match, in the scope (if | 
| Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 148 | // we're allowed to look there). | 
|  | 149 | Found.clear(); | 
|  | 150 | if (Step == 0 && LookupCtx) | 
|  | 151 | LookupQualifiedName(Found, LookupCtx); | 
| Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 152 | else if (Step == 1 && LookInScope && S) | 
| Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 153 | LookupName(Found, S); | 
|  | 154 | else | 
|  | 155 | continue; | 
|  | 156 |  | 
|  | 157 | // FIXME: Should we be suppressing ambiguities here? | 
|  | 158 | if (Found.isAmbiguous()) | 
| John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 159 | return ParsedType(); | 
| Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 160 |  | 
|  | 161 | if (TypeDecl *Type = Found.getAsSingle<TypeDecl>()) { | 
|  | 162 | QualType T = Context.getTypeDeclType(Type); | 
| Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 163 |  | 
|  | 164 | if (SearchType.isNull() || SearchType->isDependentType() || | 
|  | 165 | Context.hasSameUnqualifiedType(T, SearchType)) { | 
|  | 166 | // We found our type! | 
|  | 167 |  | 
| John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 168 | return ParsedType::make(T); | 
| Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 169 | } | 
| John Wiegley | 36784e7 | 2011-03-08 08:13:22 +0000 | [diff] [blame] | 170 |  | 
| Douglas Gregor | 7ec1873 | 2011-03-04 22:32:08 +0000 | [diff] [blame] | 171 | if (!SearchType.isNull()) | 
|  | 172 | NonMatchingTypeDecl = Type; | 
| Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 173 | } | 
|  | 174 |  | 
|  | 175 | // If the name that we found is a class template name, and it is | 
|  | 176 | // the same name as the template name in the last part of the | 
|  | 177 | // nested-name-specifier (if present) or the object type, then | 
|  | 178 | // this is the destructor for that class. | 
|  | 179 | // FIXME: This is a workaround until we get real drafting for core | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 180 | // issue 399, for which there isn't even an obvious direction. | 
| Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 181 | if (ClassTemplateDecl *Template = Found.getAsSingle<ClassTemplateDecl>()) { | 
|  | 182 | QualType MemberOfType; | 
|  | 183 | if (SS.isSet()) { | 
|  | 184 | if (DeclContext *Ctx = computeDeclContext(SS, EnteringContext)) { | 
|  | 185 | // Figure out the type of the context, if it has one. | 
| John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 186 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx)) | 
|  | 187 | MemberOfType = Context.getTypeDeclType(Record); | 
| Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 188 | } | 
|  | 189 | } | 
|  | 190 | if (MemberOfType.isNull()) | 
|  | 191 | MemberOfType = SearchType; | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 192 |  | 
| Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 193 | if (MemberOfType.isNull()) | 
|  | 194 | continue; | 
|  | 195 |  | 
|  | 196 | // We're referring into a class template specialization. If the | 
|  | 197 | // class template we found is the same as the template being | 
|  | 198 | // specialized, we found what we are looking for. | 
|  | 199 | if (const RecordType *Record = MemberOfType->getAs<RecordType>()) { | 
|  | 200 | if (ClassTemplateSpecializationDecl *Spec | 
|  | 201 | = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) { | 
|  | 202 | if (Spec->getSpecializedTemplate()->getCanonicalDecl() == | 
|  | 203 | Template->getCanonicalDecl()) | 
| John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 204 | return ParsedType::make(MemberOfType); | 
| Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 205 | } | 
|  | 206 |  | 
|  | 207 | continue; | 
|  | 208 | } | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 209 |  | 
| Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 210 | // We're referring to an unresolved class template | 
|  | 211 | // specialization. Determine whether we class template we found | 
|  | 212 | // is the same as the template being specialized or, if we don't | 
|  | 213 | // know which template is being specialized, that it at least | 
|  | 214 | // has the same name. | 
|  | 215 | if (const TemplateSpecializationType *SpecType | 
|  | 216 | = MemberOfType->getAs<TemplateSpecializationType>()) { | 
|  | 217 | TemplateName SpecName = SpecType->getTemplateName(); | 
|  | 218 |  | 
|  | 219 | // The class template we found is the same template being | 
|  | 220 | // specialized. | 
|  | 221 | if (TemplateDecl *SpecTemplate = SpecName.getAsTemplateDecl()) { | 
|  | 222 | if (SpecTemplate->getCanonicalDecl() == Template->getCanonicalDecl()) | 
| John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 223 | return ParsedType::make(MemberOfType); | 
| Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 224 |  | 
|  | 225 | continue; | 
|  | 226 | } | 
|  | 227 |  | 
|  | 228 | // The class template we found has the same name as the | 
|  | 229 | // (dependent) template name being specialized. | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 230 | if (DependentTemplateName *DepTemplate | 
| Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 231 | = SpecName.getAsDependentTemplateName()) { | 
|  | 232 | if (DepTemplate->isIdentifier() && | 
|  | 233 | DepTemplate->getIdentifier() == Template->getIdentifier()) | 
| John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 234 | return ParsedType::make(MemberOfType); | 
| Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 235 |  | 
|  | 236 | continue; | 
|  | 237 | } | 
|  | 238 | } | 
|  | 239 | } | 
|  | 240 | } | 
|  | 241 |  | 
|  | 242 | if (isDependent) { | 
|  | 243 | // We didn't find our type, but that's okay: it's dependent | 
|  | 244 | // anyway. | 
| Douglas Gregor | e29425b | 2011-02-28 22:42:13 +0000 | [diff] [blame] | 245 |  | 
|  | 246 | // FIXME: What if we have no nested-name-specifier? | 
|  | 247 | QualType T = CheckTypenameType(ETK_None, SourceLocation(), | 
|  | 248 | SS.getWithLocInContext(Context), | 
|  | 249 | II, NameLoc); | 
| John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 250 | return ParsedType::make(T); | 
| Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 251 | } | 
|  | 252 |  | 
| Douglas Gregor | 7ec1873 | 2011-03-04 22:32:08 +0000 | [diff] [blame] | 253 | if (NonMatchingTypeDecl) { | 
|  | 254 | QualType T = Context.getTypeDeclType(NonMatchingTypeDecl); | 
|  | 255 | Diag(NameLoc, diag::err_destructor_expr_type_mismatch) | 
|  | 256 | << T << SearchType; | 
|  | 257 | Diag(NonMatchingTypeDecl->getLocation(), diag::note_destructor_type_here) | 
|  | 258 | << T; | 
|  | 259 | } else if (ObjectTypePtr) | 
|  | 260 | Diag(NameLoc, diag::err_ident_in_dtor_not_a_type) | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 261 | << &II; | 
| Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 262 | else | 
|  | 263 | Diag(NameLoc, diag::err_destructor_class_name); | 
|  | 264 |  | 
| John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 265 | return ParsedType(); | 
| Douglas Gregor | 124b878 | 2010-02-16 19:09:40 +0000 | [diff] [blame] | 266 | } | 
|  | 267 |  | 
| Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 268 | /// \brief Build a C++ typeid expression with a type operand. | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 269 | ExprResult Sema::BuildCXXTypeId(QualType TypeInfoType, | 
| Douglas Gregor | 4eb4f0f | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 270 | SourceLocation TypeidLoc, | 
|  | 271 | TypeSourceInfo *Operand, | 
|  | 272 | SourceLocation RParenLoc) { | 
| Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 273 | // C++ [expr.typeid]p4: | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 274 | //   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] | 275 | //   that is the operand of typeid are always ignored. | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 276 | //   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] | 277 | //   type, the class shall be completely-defined. | 
| Douglas Gregor | d1c1d7b | 2010-06-02 06:16:02 +0000 | [diff] [blame] | 278 | Qualifiers Quals; | 
|  | 279 | QualType T | 
|  | 280 | = Context.getUnqualifiedArrayType(Operand->getType().getNonReferenceType(), | 
|  | 281 | Quals); | 
| Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 282 | if (T->getAs<RecordType>() && | 
|  | 283 | RequireCompleteType(TypeidLoc, T, diag::err_incomplete_typeid)) | 
|  | 284 | return ExprError(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 285 |  | 
| Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 286 | return Owned(new (Context) CXXTypeidExpr(TypeInfoType.withConst(), | 
|  | 287 | Operand, | 
|  | 288 | SourceRange(TypeidLoc, RParenLoc))); | 
|  | 289 | } | 
|  | 290 |  | 
|  | 291 | /// \brief Build a C++ typeid expression with an expression operand. | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 292 | ExprResult Sema::BuildCXXTypeId(QualType TypeInfoType, | 
| Douglas Gregor | 4eb4f0f | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 293 | SourceLocation TypeidLoc, | 
|  | 294 | Expr *E, | 
|  | 295 | SourceLocation RParenLoc) { | 
| Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 296 | bool isUnevaluatedOperand = true; | 
| Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 297 | if (E && !E->isTypeDependent()) { | 
| John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 298 | if (E->getType()->isPlaceholderType()) { | 
|  | 299 | ExprResult result = CheckPlaceholderExpr(E); | 
|  | 300 | if (result.isInvalid()) return ExprError(); | 
|  | 301 | E = result.take(); | 
|  | 302 | } | 
|  | 303 |  | 
| Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 304 | QualType T = E->getType(); | 
|  | 305 | if (const RecordType *RecordT = T->getAs<RecordType>()) { | 
|  | 306 | CXXRecordDecl *RecordD = cast<CXXRecordDecl>(RecordT->getDecl()); | 
|  | 307 | // C++ [expr.typeid]p3: | 
|  | 308 | //   [...] If the type of the expression is a class type, the class | 
|  | 309 | //   shall be completely-defined. | 
|  | 310 | if (RequireCompleteType(TypeidLoc, T, diag::err_incomplete_typeid)) | 
|  | 311 | return ExprError(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 312 |  | 
| Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 313 | // C++ [expr.typeid]p3: | 
| Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 314 | //   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] | 315 | //   polymorphic class type [...] [the] expression is an unevaluated | 
|  | 316 | //   operand. [...] | 
| Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 317 | if (RecordD->isPolymorphic() && E->Classify(Context).isGLValue()) { | 
| Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 318 | isUnevaluatedOperand = false; | 
| Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 319 |  | 
|  | 320 | // We require a vtable to query the type at run time. | 
|  | 321 | MarkVTableUsed(TypeidLoc, RecordD); | 
|  | 322 | } | 
| Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 323 | } | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 324 |  | 
| Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 325 | // C++ [expr.typeid]p4: | 
|  | 326 | //   [...] 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] | 327 | //   cv-qualified type, the result of the typeid expression refers to a | 
|  | 328 | //   std::type_info object representing the cv-unqualified referenced | 
| Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 329 | //   type. | 
| Douglas Gregor | d1c1d7b | 2010-06-02 06:16:02 +0000 | [diff] [blame] | 330 | Qualifiers Quals; | 
|  | 331 | QualType UnqualT = Context.getUnqualifiedArrayType(T, Quals); | 
|  | 332 | if (!Context.hasSameType(T, UnqualT)) { | 
|  | 333 | T = UnqualT; | 
| Eli Friedman | c1c0dfb | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 334 | E = ImpCastExprToType(E, UnqualT, CK_NoOp, E->getValueKind()).take(); | 
| Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 335 | } | 
|  | 336 | } | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 337 |  | 
| Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 338 | // If this is an unevaluated operand, clear out the set of | 
|  | 339 | // declaration references we have been computing and eliminate any | 
|  | 340 | // temporaries introduced in its computation. | 
|  | 341 | if (isUnevaluatedOperand) | 
|  | 342 | ExprEvalContexts.back().Context = Unevaluated; | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 343 |  | 
| Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 344 | return Owned(new (Context) CXXTypeidExpr(TypeInfoType.withConst(), | 
| John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 345 | E, | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 346 | SourceRange(TypeidLoc, RParenLoc))); | 
| Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 347 | } | 
|  | 348 |  | 
|  | 349 | /// ActOnCXXTypeidOfType - Parse typeid( type-id ) or typeid (expression); | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 350 | ExprResult | 
| Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 351 | Sema::ActOnCXXTypeid(SourceLocation OpLoc, SourceLocation LParenLoc, | 
|  | 352 | bool isType, void *TyOrExpr, SourceLocation RParenLoc) { | 
| Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 353 | // Find the std::type_info type. | 
| Sebastian Redl | ce0682f | 2011-03-31 19:29:24 +0000 | [diff] [blame] | 354 | if (!getStdNamespace()) | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 355 | return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid)); | 
| Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 356 |  | 
| Douglas Gregor | 4eb4f0f | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 357 | if (!CXXTypeInfoDecl) { | 
|  | 358 | IdentifierInfo *TypeInfoII = &PP.getIdentifierTable().get("type_info"); | 
|  | 359 | LookupResult R(*this, TypeInfoII, SourceLocation(), LookupTagName); | 
|  | 360 | LookupQualifiedName(R, getStdNamespace()); | 
|  | 361 | CXXTypeInfoDecl = R.getAsSingle<RecordDecl>(); | 
|  | 362 | if (!CXXTypeInfoDecl) | 
|  | 363 | return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid)); | 
|  | 364 | } | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 365 |  | 
| Douglas Gregor | 4eb4f0f | 2010-09-08 23:14:30 +0000 | [diff] [blame] | 366 | QualType TypeInfoType = Context.getTypeDeclType(CXXTypeInfoDecl); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 367 |  | 
| Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 368 | if (isType) { | 
|  | 369 | // The operand is a type; handle it as such. | 
|  | 370 | TypeSourceInfo *TInfo = 0; | 
| John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 371 | QualType T = GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrExpr), | 
|  | 372 | &TInfo); | 
| Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 373 | if (T.isNull()) | 
|  | 374 | return ExprError(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 375 |  | 
| Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 376 | if (!TInfo) | 
|  | 377 | TInfo = Context.getTrivialTypeSourceInfo(T, OpLoc); | 
| Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 378 |  | 
| Douglas Gregor | 57fdc8a | 2010-04-26 22:37:10 +0000 | [diff] [blame] | 379 | return BuildCXXTypeId(TypeInfoType, OpLoc, TInfo, RParenLoc); | 
| Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 380 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 381 |  | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 382 | // The operand is an expression. | 
| John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 383 | return BuildCXXTypeId(TypeInfoType, OpLoc, (Expr*)TyOrExpr, RParenLoc); | 
| Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 384 | } | 
|  | 385 |  | 
| Francois Pichet | 6915c52 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 386 | /// Retrieve the UuidAttr associated with QT. | 
|  | 387 | static UuidAttr *GetUuidAttrOfType(QualType QT) { | 
|  | 388 | // Optionally remove one level of pointer, reference or array indirection. | 
| John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 389 | const Type *Ty = QT.getTypePtr();; | 
| Francois Pichet | 913b7bf | 2010-12-20 03:51:03 +0000 | [diff] [blame] | 390 | if (QT->isPointerType() || QT->isReferenceType()) | 
|  | 391 | Ty = QT->getPointeeType().getTypePtr(); | 
|  | 392 | else if (QT->isArrayType()) | 
|  | 393 | Ty = cast<ArrayType>(QT)->getElementType().getTypePtr(); | 
|  | 394 |  | 
| Francois Pichet | 8db75a2 | 2011-05-08 10:02:20 +0000 | [diff] [blame] | 395 | // Loop all record redeclaration looking for an uuid attribute. | 
| Francois Pichet | 6915c52 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 396 | CXXRecordDecl *RD = Ty->getAsCXXRecordDecl(); | 
| Francois Pichet | 8db75a2 | 2011-05-08 10:02:20 +0000 | [diff] [blame] | 397 | for (CXXRecordDecl::redecl_iterator I = RD->redecls_begin(), | 
|  | 398 | E = RD->redecls_end(); I != E; ++I) { | 
|  | 399 | if (UuidAttr *Uuid = I->getAttr<UuidAttr>()) | 
| Francois Pichet | 6915c52 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 400 | return Uuid; | 
| Francois Pichet | 6915c52 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 401 | } | 
| Francois Pichet | 8db75a2 | 2011-05-08 10:02:20 +0000 | [diff] [blame] | 402 |  | 
| Francois Pichet | 6915c52 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 403 | return 0; | 
| Francois Pichet | 913b7bf | 2010-12-20 03:51:03 +0000 | [diff] [blame] | 404 | } | 
|  | 405 |  | 
| Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 406 | /// \brief Build a Microsoft __uuidof expression with a type operand. | 
|  | 407 | ExprResult Sema::BuildCXXUuidof(QualType TypeInfoType, | 
|  | 408 | SourceLocation TypeidLoc, | 
|  | 409 | TypeSourceInfo *Operand, | 
|  | 410 | SourceLocation RParenLoc) { | 
| Francois Pichet | 6915c52 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 411 | if (!Operand->getType()->isDependentType()) { | 
|  | 412 | if (!GetUuidAttrOfType(Operand->getType())) | 
|  | 413 | return ExprError(Diag(TypeidLoc, diag::err_uuidof_without_guid)); | 
|  | 414 | } | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 415 |  | 
| Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 416 | // FIXME: add __uuidof semantic analysis for type operand. | 
|  | 417 | return Owned(new (Context) CXXUuidofExpr(TypeInfoType.withConst(), | 
|  | 418 | Operand, | 
|  | 419 | SourceRange(TypeidLoc, RParenLoc))); | 
|  | 420 | } | 
|  | 421 |  | 
|  | 422 | /// \brief Build a Microsoft __uuidof expression with an expression operand. | 
|  | 423 | ExprResult Sema::BuildCXXUuidof(QualType TypeInfoType, | 
|  | 424 | SourceLocation TypeidLoc, | 
|  | 425 | Expr *E, | 
|  | 426 | SourceLocation RParenLoc) { | 
| Francois Pichet | 6915c52 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 427 | if (!E->getType()->isDependentType()) { | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 428 | if (!GetUuidAttrOfType(E->getType()) && | 
| Francois Pichet | 6915c52 | 2010-12-27 01:32:00 +0000 | [diff] [blame] | 429 | !E->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) | 
|  | 430 | return ExprError(Diag(TypeidLoc, diag::err_uuidof_without_guid)); | 
|  | 431 | } | 
|  | 432 | // FIXME: add __uuidof semantic analysis for type operand. | 
| Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 433 | return Owned(new (Context) CXXUuidofExpr(TypeInfoType.withConst(), | 
|  | 434 | E, | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 435 | SourceRange(TypeidLoc, RParenLoc))); | 
| Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 436 | } | 
|  | 437 |  | 
|  | 438 | /// ActOnCXXUuidof - Parse __uuidof( type-id ) or __uuidof (expression); | 
|  | 439 | ExprResult | 
|  | 440 | Sema::ActOnCXXUuidof(SourceLocation OpLoc, SourceLocation LParenLoc, | 
|  | 441 | bool isType, void *TyOrExpr, SourceLocation RParenLoc) { | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 442 | // If MSVCGuidDecl has not been cached, do the lookup. | 
| Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 443 | if (!MSVCGuidDecl) { | 
|  | 444 | IdentifierInfo *GuidII = &PP.getIdentifierTable().get("_GUID"); | 
|  | 445 | LookupResult R(*this, GuidII, SourceLocation(), LookupTagName); | 
|  | 446 | LookupQualifiedName(R, Context.getTranslationUnitDecl()); | 
|  | 447 | MSVCGuidDecl = R.getAsSingle<RecordDecl>(); | 
|  | 448 | if (!MSVCGuidDecl) | 
|  | 449 | return ExprError(Diag(OpLoc, diag::err_need_header_before_ms_uuidof)); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 450 | } | 
|  | 451 |  | 
| Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 452 | QualType GuidType = Context.getTypeDeclType(MSVCGuidDecl); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 453 |  | 
| Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 454 | if (isType) { | 
|  | 455 | // The operand is a type; handle it as such. | 
|  | 456 | TypeSourceInfo *TInfo = 0; | 
|  | 457 | QualType T = GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrExpr), | 
|  | 458 | &TInfo); | 
|  | 459 | if (T.isNull()) | 
|  | 460 | return ExprError(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 461 |  | 
| Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 462 | if (!TInfo) | 
|  | 463 | TInfo = Context.getTrivialTypeSourceInfo(T, OpLoc); | 
|  | 464 |  | 
|  | 465 | return BuildCXXUuidof(GuidType, OpLoc, TInfo, RParenLoc); | 
|  | 466 | } | 
|  | 467 |  | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 468 | // The operand is an expression. | 
| Francois Pichet | 01b7c30 | 2010-09-08 12:20:18 +0000 | [diff] [blame] | 469 | return BuildCXXUuidof(GuidType, OpLoc, (Expr*)TyOrExpr, RParenLoc); | 
|  | 470 | } | 
|  | 471 |  | 
| Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 472 | /// ActOnCXXBoolLiteral - Parse {true,false} literals. | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 473 | ExprResult | 
| Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 474 | Sema::ActOnCXXBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) { | 
| Douglas Gregor | 2f639b9 | 2008-10-24 15:36:09 +0000 | [diff] [blame] | 475 | assert((Kind == tok::kw_true || Kind == tok::kw_false) && | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 476 | "Unknown C++ Boolean value!"); | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 477 | return Owned(new (Context) CXXBoolLiteralExpr(Kind == tok::kw_true, | 
|  | 478 | Context.BoolTy, OpLoc)); | 
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 479 | } | 
| Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 480 |  | 
| Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 481 | /// ActOnCXXNullPtrLiteral - Parse 'nullptr'. | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 482 | ExprResult | 
| Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 483 | Sema::ActOnCXXNullPtrLiteral(SourceLocation Loc) { | 
|  | 484 | return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc)); | 
|  | 485 | } | 
|  | 486 |  | 
| Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 487 | /// ActOnCXXThrow - Parse throw expressions. | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 488 | ExprResult | 
| Douglas Gregor | bca01b4 | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 489 | Sema::ActOnCXXThrow(Scope *S, SourceLocation OpLoc, Expr *Ex) { | 
|  | 490 | bool IsThrownVarInScope = false; | 
|  | 491 | if (Ex) { | 
|  | 492 | // C++0x [class.copymove]p31: | 
|  | 493 | //   When certain criteria are met, an implementation is allowed to omit the | 
|  | 494 | //   copy/move construction of a class object [...] | 
|  | 495 | // | 
|  | 496 | //     - in a throw-expression, when the operand is the name of a | 
|  | 497 | //       non-volatile automatic object (other than a function or catch- | 
|  | 498 | //       clause parameter) whose scope does not extend beyond the end of the | 
|  | 499 | //       innermost enclosing try-block (if there is one), the copy/move | 
|  | 500 | //       operation from the operand to the exception object (15.1) can be | 
|  | 501 | //       omitted by constructing the automatic object directly into the | 
|  | 502 | //       exception object | 
|  | 503 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Ex->IgnoreParens())) | 
|  | 504 | if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { | 
|  | 505 | if (Var->hasLocalStorage() && !Var->getType().isVolatileQualified()) { | 
|  | 506 | for( ; S; S = S->getParent()) { | 
|  | 507 | if (S->isDeclScope(Var)) { | 
|  | 508 | IsThrownVarInScope = true; | 
|  | 509 | break; | 
|  | 510 | } | 
|  | 511 |  | 
|  | 512 | if (S->getFlags() & | 
|  | 513 | (Scope::FnScope | Scope::ClassScope | Scope::BlockScope | | 
|  | 514 | Scope::FunctionPrototypeScope | Scope::ObjCMethodScope | | 
|  | 515 | Scope::TryScope)) | 
|  | 516 | break; | 
|  | 517 | } | 
|  | 518 | } | 
|  | 519 | } | 
|  | 520 | } | 
|  | 521 |  | 
|  | 522 | return BuildCXXThrow(OpLoc, Ex, IsThrownVarInScope); | 
|  | 523 | } | 
|  | 524 |  | 
|  | 525 | ExprResult Sema::BuildCXXThrow(SourceLocation OpLoc, Expr *Ex, | 
|  | 526 | bool IsThrownVarInScope) { | 
| Anders Carlsson | 729b853 | 2011-02-23 03:46:46 +0000 | [diff] [blame] | 527 | // Don't report an error if 'throw' is used in system headers. | 
| Anders Carlsson | 15348ae | 2011-02-28 02:27:16 +0000 | [diff] [blame] | 528 | if (!getLangOptions().CXXExceptions && | 
| Anders Carlsson | 729b853 | 2011-02-23 03:46:46 +0000 | [diff] [blame] | 529 | !getSourceManager().isInSystemHeader(OpLoc)) | 
| Anders Carlsson | b1fba31 | 2011-02-19 21:53:09 +0000 | [diff] [blame] | 530 | Diag(OpLoc, diag::err_exceptions_disabled) << "throw"; | 
| Douglas Gregor | bca01b4 | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 531 |  | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 532 | if (Ex && !Ex->isTypeDependent()) { | 
| Douglas Gregor | bca01b4 | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 533 | ExprResult ExRes = CheckCXXThrowOperand(OpLoc, Ex, IsThrownVarInScope); | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 534 | if (ExRes.isInvalid()) | 
|  | 535 | return ExprError(); | 
|  | 536 | Ex = ExRes.take(); | 
|  | 537 | } | 
| Douglas Gregor | bca01b4 | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 538 |  | 
|  | 539 | return Owned(new (Context) CXXThrowExpr(Ex, Context.VoidTy, OpLoc, | 
|  | 540 | IsThrownVarInScope)); | 
| Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 541 | } | 
|  | 542 |  | 
|  | 543 | /// CheckCXXThrowOperand - Validate the operand of a throw. | 
| Douglas Gregor | bca01b4 | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 544 | ExprResult Sema::CheckCXXThrowOperand(SourceLocation ThrowLoc, Expr *E, | 
|  | 545 | bool IsThrownVarInScope) { | 
| Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 546 | // C++ [except.throw]p3: | 
| Douglas Gregor | 154fe98 | 2009-12-23 22:04:40 +0000 | [diff] [blame] | 547 | //   A throw-expression initializes a temporary object, called the exception | 
|  | 548 | //   object, the type of which is determined by removing any top-level | 
|  | 549 | //   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] | 550 | //   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] | 551 | //   or "pointer to function returning T", [...] | 
|  | 552 | if (E->getType().hasQualifiers()) | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 553 | E = ImpCastExprToType(E, E->getType().getUnqualifiedType(), CK_NoOp, | 
| Eli Friedman | c1c0dfb | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 554 | E->getValueKind()).take(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 555 |  | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 556 | ExprResult Res = DefaultFunctionArrayConversion(E); | 
|  | 557 | if (Res.isInvalid()) | 
|  | 558 | return ExprError(); | 
|  | 559 | E = Res.take(); | 
| Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 560 |  | 
|  | 561 | //   If the type of the exception would be an incomplete type or a pointer | 
|  | 562 | //   to an incomplete type other than (cv) void the program is ill-formed. | 
|  | 563 | QualType Ty = E->getType(); | 
| John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 564 | bool isPointer = false; | 
| Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 565 | if (const PointerType* Ptr = Ty->getAs<PointerType>()) { | 
| Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 566 | Ty = Ptr->getPointeeType(); | 
| John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 567 | isPointer = true; | 
| Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 568 | } | 
|  | 569 | if (!isPointer || !Ty->isVoidType()) { | 
|  | 570 | if (RequireCompleteType(ThrowLoc, Ty, | 
| Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 571 | PDiag(isPointer ? diag::err_throw_incomplete_ptr | 
|  | 572 | : diag::err_throw_incomplete) | 
|  | 573 | << E->getSourceRange())) | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 574 | return ExprError(); | 
| Rafael Espindola | 7b9a5aa | 2010-03-02 21:28:26 +0000 | [diff] [blame] | 575 |  | 
| Douglas Gregor | bf422f9 | 2010-04-15 18:05:39 +0000 | [diff] [blame] | 576 | if (RequireNonAbstractType(ThrowLoc, E->getType(), | 
|  | 577 | PDiag(diag::err_throw_abstract_type) | 
|  | 578 | << E->getSourceRange())) | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 579 | return ExprError(); | 
| Sebastian Redl | 972041f | 2009-04-27 20:27:31 +0000 | [diff] [blame] | 580 | } | 
|  | 581 |  | 
| John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 582 | // Initialize the exception result.  This implicitly weeds out | 
|  | 583 | // abstract types or types with inaccessible copy constructors. | 
| Douglas Gregor | bca01b4 | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 584 |  | 
|  | 585 | // C++0x [class.copymove]p31: | 
|  | 586 | //   When certain criteria are met, an implementation is allowed to omit the | 
|  | 587 | //   copy/move construction of a class object [...] | 
|  | 588 | // | 
|  | 589 | //     - in a throw-expression, when the operand is the name of a | 
|  | 590 | //       non-volatile automatic object (other than a function or catch-clause | 
|  | 591 | //       parameter) whose scope does not extend beyond the end of the | 
|  | 592 | //       innermost enclosing try-block (if there is one), the copy/move | 
|  | 593 | //       operation from the operand to the exception object (15.1) can be | 
|  | 594 | //       omitted by constructing the automatic object directly into the | 
|  | 595 | //       exception object | 
|  | 596 | const VarDecl *NRVOVariable = 0; | 
|  | 597 | if (IsThrownVarInScope) | 
|  | 598 | NRVOVariable = getCopyElisionCandidate(QualType(), E, false); | 
|  | 599 |  | 
| John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 600 | InitializedEntity Entity = | 
| Douglas Gregor | 72dfa27 | 2011-01-21 22:46:35 +0000 | [diff] [blame] | 601 | InitializedEntity::InitializeException(ThrowLoc, E->getType(), | 
| Douglas Gregor | bca01b4 | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 602 | /*NRVO=*/NRVOVariable != 0); | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 603 | Res = PerformMoveOrCopyInitialization(Entity, NRVOVariable, | 
| Douglas Gregor | bca01b4 | 2011-07-06 22:04:06 +0000 | [diff] [blame] | 604 | QualType(), E, | 
|  | 605 | IsThrownVarInScope); | 
| John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 606 | if (Res.isInvalid()) | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 607 | return ExprError(); | 
|  | 608 | E = Res.take(); | 
| Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 609 |  | 
| Eli Friedman | 5ed9b93 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 610 | // If the exception has class type, we need additional handling. | 
|  | 611 | const RecordType *RecordTy = Ty->getAs<RecordType>(); | 
|  | 612 | if (!RecordTy) | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 613 | return Owned(E); | 
| Eli Friedman | 5ed9b93 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 614 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl()); | 
|  | 615 |  | 
| Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 616 | // If we are throwing a polymorphic class type or pointer thereof, | 
|  | 617 | // exception handling will make use of the vtable. | 
| Eli Friedman | 5ed9b93 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 618 | MarkVTableUsed(ThrowLoc, RD); | 
|  | 619 |  | 
| Eli Friedman | 98efb9f | 2010-10-12 20:32:36 +0000 | [diff] [blame] | 620 | // If a pointer is thrown, the referenced object will not be destroyed. | 
|  | 621 | if (isPointer) | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 622 | return Owned(E); | 
| Eli Friedman | 98efb9f | 2010-10-12 20:32:36 +0000 | [diff] [blame] | 623 |  | 
| Eli Friedman | 5ed9b93 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 624 | // If the class has a non-trivial destructor, we must be able to call it. | 
|  | 625 | if (RD->hasTrivialDestructor()) | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 626 | return Owned(E); | 
| Eli Friedman | 5ed9b93 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 627 |  | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 628 | CXXDestructorDecl *Destructor | 
| Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 629 | = const_cast<CXXDestructorDecl*>(LookupDestructor(RD)); | 
| Eli Friedman | 5ed9b93 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 630 | if (!Destructor) | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 631 | return Owned(E); | 
| Eli Friedman | 5ed9b93 | 2010-06-03 20:39:03 +0000 | [diff] [blame] | 632 |  | 
|  | 633 | MarkDeclarationReferenced(E->getExprLoc(), Destructor); | 
|  | 634 | CheckDestructorAccess(E->getExprLoc(), Destructor, | 
| Douglas Gregor | ed8abf1 | 2010-07-08 06:14:04 +0000 | [diff] [blame] | 635 | PDiag(diag::err_access_dtor_exception) << Ty); | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 636 | return Owned(E); | 
| Chris Lattner | 50dd289 | 2008-02-26 00:51:44 +0000 | [diff] [blame] | 637 | } | 
| Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 638 |  | 
| Douglas Gregor | 341350e | 2011-10-18 16:47:30 +0000 | [diff] [blame^] | 639 | QualType Sema::getCurrentThisType(bool Capture) { | 
| John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 640 | // Ignore block scopes: we can capture through them. | 
|  | 641 | // Ignore nested enum scopes: we'll diagnose non-constant expressions | 
|  | 642 | // where they're invalid, and other uses are legitimate. | 
|  | 643 | // Don't ignore nested class scopes: you can't use 'this' in a local class. | 
| John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 644 | DeclContext *DC = CurContext; | 
| Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 645 | unsigned NumBlocks = 0; | 
| John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 646 | while (true) { | 
| Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 647 | if (isa<BlockDecl>(DC)) { | 
|  | 648 | DC = cast<BlockDecl>(DC)->getDeclContext(); | 
|  | 649 | ++NumBlocks; | 
|  | 650 | } else if (isa<EnumDecl>(DC)) | 
|  | 651 | DC = cast<EnumDecl>(DC)->getDeclContext(); | 
| John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 652 | else break; | 
|  | 653 | } | 
| Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 654 |  | 
| Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 655 | QualType ThisTy; | 
|  | 656 | if (CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(DC)) { | 
|  | 657 | if (method && method->isInstance()) | 
|  | 658 | ThisTy = method->getThisType(Context); | 
|  | 659 | } else if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC)) { | 
|  | 660 | // C++0x [expr.prim]p4: | 
|  | 661 | //   Otherwise, if a member-declarator declares a non-static data member | 
|  | 662 | // of a class X, the expression this is a prvalue of type "pointer to X" | 
|  | 663 | // within the optional brace-or-equal-initializer. | 
|  | 664 | Scope *S = getScopeForContext(DC); | 
|  | 665 | if (!S || S->getFlags() & Scope::ThisScope) | 
|  | 666 | ThisTy = Context.getPointerType(Context.getRecordType(RD)); | 
|  | 667 | } | 
| John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 668 |  | 
| Douglas Gregor | 341350e | 2011-10-18 16:47:30 +0000 | [diff] [blame^] | 669 | if (!Capture || ThisTy.isNull()) | 
|  | 670 | return ThisTy; | 
|  | 671 |  | 
| Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 672 | // Mark that we're closing on 'this' in all the block scopes we ignored. | 
| Douglas Gregor | 341350e | 2011-10-18 16:47:30 +0000 | [diff] [blame^] | 673 | for (unsigned idx = FunctionScopes.size() - 1; | 
|  | 674 | NumBlocks; --idx, --NumBlocks) | 
|  | 675 | cast<BlockScopeInfo>(FunctionScopes[idx])->CapturesCXXThis = true; | 
| John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 676 |  | 
| Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 677 | return ThisTy; | 
| John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 678 | } | 
|  | 679 |  | 
| Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 680 | ExprResult Sema::ActOnCXXThis(SourceLocation Loc) { | 
| John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 681 | /// C++ 9.3.2: In the body of a non-static member function, the keyword this | 
|  | 682 | /// is a non-lvalue expression whose value is the address of the object for | 
|  | 683 | /// which the function is called. | 
|  | 684 |  | 
| Douglas Gregor | 341350e | 2011-10-18 16:47:30 +0000 | [diff] [blame^] | 685 | QualType ThisTy = getCurrentThisType(); | 
| Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 686 | if (ThisTy.isNull()) return Diag(Loc, diag::err_invalid_this_use); | 
| John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 687 |  | 
| Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 688 | return Owned(new (Context) CXXThisExpr(Loc, ThisTy, /*isImplicit=*/false)); | 
| Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 689 | } | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 690 |  | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 691 | ExprResult | 
| Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 692 | Sema::ActOnCXXTypeConstructExpr(ParsedType TypeRep, | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 693 | SourceLocation LParenLoc, | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 694 | MultiExprArg exprs, | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 695 | SourceLocation RParenLoc) { | 
| Douglas Gregor | ae4c77d | 2010-02-05 19:11:37 +0000 | [diff] [blame] | 696 | if (!TypeRep) | 
|  | 697 | return ExprError(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 698 |  | 
| John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 699 | TypeSourceInfo *TInfo; | 
|  | 700 | QualType Ty = GetTypeFromParser(TypeRep, &TInfo); | 
|  | 701 | if (!TInfo) | 
|  | 702 | TInfo = Context.getTrivialTypeSourceInfo(Ty, SourceLocation()); | 
| Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 703 |  | 
|  | 704 | return BuildCXXTypeConstructExpr(TInfo, LParenLoc, exprs, RParenLoc); | 
|  | 705 | } | 
|  | 706 |  | 
|  | 707 | /// ActOnCXXTypeConstructExpr - Parse construction of a specified type. | 
|  | 708 | /// Can be interpreted either as function-style casting ("int(x)") | 
|  | 709 | /// or class type construction ("ClassType(x,y,z)") | 
|  | 710 | /// or creation of a value-initialized type ("int()"). | 
|  | 711 | ExprResult | 
|  | 712 | Sema::BuildCXXTypeConstructExpr(TypeSourceInfo *TInfo, | 
|  | 713 | SourceLocation LParenLoc, | 
|  | 714 | MultiExprArg exprs, | 
|  | 715 | SourceLocation RParenLoc) { | 
|  | 716 | QualType Ty = TInfo->getType(); | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 717 | unsigned NumExprs = exprs.size(); | 
|  | 718 | Expr **Exprs = (Expr**)exprs.get(); | 
| Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 719 | SourceLocation TyBeginLoc = TInfo->getTypeLoc().getBeginLoc(); | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 720 | SourceRange FullRange = SourceRange(TyBeginLoc, RParenLoc); | 
|  | 721 |  | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 722 | if (Ty->isDependentType() || | 
| Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 723 | CallExpr::hasAnyTypeDependentArguments(Exprs, NumExprs)) { | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 724 | exprs.release(); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 725 |  | 
| Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 726 | return Owned(CXXUnresolvedConstructExpr::Create(Context, TInfo, | 
| Douglas Gregor | d81e6ca | 2009-05-20 18:46:25 +0000 | [diff] [blame] | 727 | LParenLoc, | 
|  | 728 | Exprs, NumExprs, | 
|  | 729 | RParenLoc)); | 
| Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 730 | } | 
|  | 731 |  | 
| Anders Carlsson | bb60a50 | 2009-08-27 03:53:50 +0000 | [diff] [blame] | 732 | if (Ty->isArrayType()) | 
|  | 733 | return ExprError(Diag(TyBeginLoc, | 
|  | 734 | diag::err_value_init_for_array_type) << FullRange); | 
|  | 735 | if (!Ty->isVoidType() && | 
|  | 736 | RequireCompleteType(TyBeginLoc, Ty, | 
|  | 737 | PDiag(diag::err_invalid_incomplete_type_use) | 
|  | 738 | << FullRange)) | 
|  | 739 | return ExprError(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 740 |  | 
| Anders Carlsson | bb60a50 | 2009-08-27 03:53:50 +0000 | [diff] [blame] | 741 | if (RequireNonAbstractType(TyBeginLoc, Ty, | 
|  | 742 | diag::err_allocation_of_abstract_type)) | 
|  | 743 | return ExprError(); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 744 |  | 
|  | 745 |  | 
| Douglas Gregor | 506ae41 | 2009-01-16 18:33:17 +0000 | [diff] [blame] | 746 | // C++ [expr.type.conv]p1: | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 747 | // If the expression list is a single expression, the type conversion | 
|  | 748 | // expression is equivalent (in definedness, and if defined in meaning) to the | 
|  | 749 | // corresponding cast expression. | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 750 | if (NumExprs == 1) { | 
| John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 751 | Expr *Arg = Exprs[0]; | 
| Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 752 | exprs.release(); | 
| John McCall | b45ae25 | 2011-10-05 07:41:44 +0000 | [diff] [blame] | 753 | return BuildCXXFunctionalCastExpr(TInfo, LParenLoc, Arg, RParenLoc); | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 754 | } | 
|  | 755 |  | 
| Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 756 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(TInfo); | 
|  | 757 | InitializationKind Kind | 
|  | 758 | = NumExprs ? InitializationKind::CreateDirect(TyBeginLoc, | 
|  | 759 | LParenLoc, RParenLoc) | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 760 | : InitializationKind::CreateValue(TyBeginLoc, | 
| Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 761 | LParenLoc, RParenLoc); | 
|  | 762 | InitializationSequence InitSeq(*this, Entity, Kind, Exprs, NumExprs); | 
|  | 763 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, move(exprs)); | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 764 |  | 
| Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 765 | // FIXME: Improve AST representation? | 
|  | 766 | return move(Result); | 
| Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 767 | } | 
| Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 768 |  | 
| John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 769 | /// doesUsualArrayDeleteWantSize - Answers whether the usual | 
|  | 770 | /// operator delete[] for the given type has a size_t parameter. | 
|  | 771 | static bool doesUsualArrayDeleteWantSize(Sema &S, SourceLocation loc, | 
|  | 772 | QualType allocType) { | 
|  | 773 | const RecordType *record = | 
|  | 774 | allocType->getBaseElementTypeUnsafe()->getAs<RecordType>(); | 
|  | 775 | if (!record) return false; | 
|  | 776 |  | 
|  | 777 | // Try to find an operator delete[] in class scope. | 
|  | 778 |  | 
|  | 779 | DeclarationName deleteName = | 
|  | 780 | S.Context.DeclarationNames.getCXXOperatorName(OO_Array_Delete); | 
|  | 781 | LookupResult ops(S, deleteName, loc, Sema::LookupOrdinaryName); | 
|  | 782 | S.LookupQualifiedName(ops, record->getDecl()); | 
|  | 783 |  | 
|  | 784 | // We're just doing this for information. | 
|  | 785 | ops.suppressDiagnostics(); | 
|  | 786 |  | 
|  | 787 | // Very likely: there's no operator delete[]. | 
|  | 788 | if (ops.empty()) return false; | 
|  | 789 |  | 
|  | 790 | // If it's ambiguous, it should be illegal to call operator delete[] | 
|  | 791 | // on this thing, so it doesn't matter if we allocate extra space or not. | 
|  | 792 | if (ops.isAmbiguous()) return false; | 
|  | 793 |  | 
|  | 794 | LookupResult::Filter filter = ops.makeFilter(); | 
|  | 795 | while (filter.hasNext()) { | 
|  | 796 | NamedDecl *del = filter.next()->getUnderlyingDecl(); | 
|  | 797 |  | 
|  | 798 | // C++0x [basic.stc.dynamic.deallocation]p2: | 
|  | 799 | //   A template instance is never a usual deallocation function, | 
|  | 800 | //   regardless of its signature. | 
|  | 801 | if (isa<FunctionTemplateDecl>(del)) { | 
|  | 802 | filter.erase(); | 
|  | 803 | continue; | 
|  | 804 | } | 
|  | 805 |  | 
|  | 806 | // C++0x [basic.stc.dynamic.deallocation]p2: | 
|  | 807 | //   If class T does not declare [an operator delete[] with one | 
|  | 808 | //   parameter] but does declare a member deallocation function | 
|  | 809 | //   named operator delete[] with exactly two parameters, the | 
|  | 810 | //   second of which has type std::size_t, then this function | 
|  | 811 | //   is a usual deallocation function. | 
|  | 812 | if (!cast<CXXMethodDecl>(del)->isUsualDeallocationFunction()) { | 
|  | 813 | filter.erase(); | 
|  | 814 | continue; | 
|  | 815 | } | 
|  | 816 | } | 
|  | 817 | filter.done(); | 
|  | 818 |  | 
|  | 819 | if (!ops.isSingleResult()) return false; | 
|  | 820 |  | 
|  | 821 | const FunctionDecl *del = cast<FunctionDecl>(ops.getFoundDecl()); | 
|  | 822 | return (del->getNumParams() == 2); | 
|  | 823 | } | 
| Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 824 |  | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 825 | /// ActOnCXXNew - Parsed a C++ 'new' expression (C++ 5.3.4), as in e.g.: | 
|  | 826 | /// @code new (memory) int[size][4] @endcode | 
|  | 827 | /// or | 
|  | 828 | /// @code ::new Foo(23, "hello") @endcode | 
|  | 829 | /// For the interpretation of this heap of arguments, consult the base version. | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 830 | ExprResult | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 831 | Sema::ActOnCXXNew(SourceLocation StartLoc, bool UseGlobal, | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 832 | SourceLocation PlacementLParen, MultiExprArg PlacementArgs, | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 833 | SourceLocation PlacementRParen, SourceRange TypeIdParens, | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 834 | Declarator &D, SourceLocation ConstructorLParen, | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 835 | MultiExprArg ConstructorArgs, | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 836 | SourceLocation ConstructorRParen) { | 
| Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 837 | bool TypeContainsAuto = D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto; | 
|  | 838 |  | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 839 | Expr *ArraySize = 0; | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 840 | // If the specified type is an array, unwrap it and save the expression. | 
|  | 841 | if (D.getNumTypeObjects() > 0 && | 
|  | 842 | D.getTypeObject(0).Kind == DeclaratorChunk::Array) { | 
|  | 843 | DeclaratorChunk &Chunk = D.getTypeObject(0); | 
| Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 844 | if (TypeContainsAuto) | 
|  | 845 | return ExprError(Diag(Chunk.Loc, diag::err_new_array_of_auto) | 
|  | 846 | << D.getSourceRange()); | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 847 | if (Chunk.Arr.hasStatic) | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 848 | return ExprError(Diag(Chunk.Loc, diag::err_static_illegal_in_new) | 
|  | 849 | << D.getSourceRange()); | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 850 | if (!Chunk.Arr.NumElts) | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 851 | return ExprError(Diag(Chunk.Loc, diag::err_array_new_needs_size) | 
|  | 852 | << D.getSourceRange()); | 
| Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 853 |  | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 854 | ArraySize = static_cast<Expr*>(Chunk.Arr.NumElts); | 
| Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 855 | D.DropFirstTypeObject(); | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 856 | } | 
|  | 857 |  | 
| Douglas Gregor | 043cad2 | 2009-09-11 00:18:58 +0000 | [diff] [blame] | 858 | // Every dimension shall be of constant size. | 
| Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 859 | if (ArraySize) { | 
|  | 860 | for (unsigned I = 0, N = D.getNumTypeObjects(); I < N; ++I) { | 
| Douglas Gregor | 043cad2 | 2009-09-11 00:18:58 +0000 | [diff] [blame] | 861 | if (D.getTypeObject(I).Kind != DeclaratorChunk::Array) | 
|  | 862 | break; | 
|  | 863 |  | 
|  | 864 | DeclaratorChunk::ArrayTypeInfo &Array = D.getTypeObject(I).Arr; | 
|  | 865 | if (Expr *NumElts = (Expr *)Array.NumElts) { | 
|  | 866 | if (!NumElts->isTypeDependent() && !NumElts->isValueDependent() && | 
|  | 867 | !NumElts->isIntegerConstantExpr(Context)) { | 
|  | 868 | Diag(D.getTypeObject(I).Loc, diag::err_new_array_nonconst) | 
|  | 869 | << NumElts->getSourceRange(); | 
|  | 870 | return ExprError(); | 
|  | 871 | } | 
|  | 872 | } | 
|  | 873 | } | 
|  | 874 | } | 
| Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 875 |  | 
| Argyrios Kyrtzidis | 0b8c98f | 2011-06-28 03:01:23 +0000 | [diff] [blame] | 876 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, /*Scope=*/0); | 
| John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 877 | QualType AllocType = TInfo->getType(); | 
| Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 878 | if (D.isInvalidType()) | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 879 | return ExprError(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 880 |  | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 881 | return BuildCXXNew(StartLoc, UseGlobal, | 
| Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 882 | PlacementLParen, | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 883 | move(PlacementArgs), | 
| Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 884 | PlacementRParen, | 
| Douglas Gregor | 4bd4031 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 885 | TypeIdParens, | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 886 | AllocType, | 
| Douglas Gregor | 1bb2a93 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 887 | TInfo, | 
| John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 888 | ArraySize, | 
| Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 889 | ConstructorLParen, | 
|  | 890 | move(ConstructorArgs), | 
| Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 891 | ConstructorRParen, | 
|  | 892 | TypeContainsAuto); | 
| Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 893 | } | 
|  | 894 |  | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 895 | ExprResult | 
| Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 896 | Sema::BuildCXXNew(SourceLocation StartLoc, bool UseGlobal, | 
|  | 897 | SourceLocation PlacementLParen, | 
|  | 898 | MultiExprArg PlacementArgs, | 
|  | 899 | SourceLocation PlacementRParen, | 
| Douglas Gregor | 4bd4031 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 900 | SourceRange TypeIdParens, | 
| Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 901 | QualType AllocType, | 
| Douglas Gregor | 1bb2a93 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 902 | TypeSourceInfo *AllocTypeInfo, | 
| John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 903 | Expr *ArraySize, | 
| Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 904 | SourceLocation ConstructorLParen, | 
|  | 905 | MultiExprArg ConstructorArgs, | 
| Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 906 | SourceLocation ConstructorRParen, | 
|  | 907 | bool TypeMayContainAuto) { | 
| Douglas Gregor | 1bb2a93 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 908 | SourceRange TypeRange = AllocTypeInfo->getTypeLoc().getSourceRange(); | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 909 |  | 
| Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 910 | // C++0x [decl.spec.auto]p6. Deduce the type which 'auto' stands in for. | 
|  | 911 | if (TypeMayContainAuto && AllocType->getContainedAutoType()) { | 
|  | 912 | if (ConstructorArgs.size() == 0) | 
|  | 913 | return ExprError(Diag(StartLoc, diag::err_auto_new_requires_ctor_arg) | 
|  | 914 | << AllocType << TypeRange); | 
|  | 915 | if (ConstructorArgs.size() != 1) { | 
|  | 916 | Expr *FirstBad = ConstructorArgs.get()[1]; | 
|  | 917 | return ExprError(Diag(FirstBad->getSourceRange().getBegin(), | 
|  | 918 | diag::err_auto_new_ctor_multiple_expressions) | 
|  | 919 | << AllocType << TypeRange); | 
|  | 920 | } | 
| Richard Smith | a085da8 | 2011-03-17 16:11:59 +0000 | [diff] [blame] | 921 | TypeSourceInfo *DeducedType = 0; | 
|  | 922 | if (!DeduceAutoType(AllocTypeInfo, ConstructorArgs.get()[0], DeducedType)) | 
| Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 923 | return ExprError(Diag(StartLoc, diag::err_auto_new_deduction_failure) | 
|  | 924 | << AllocType | 
|  | 925 | << ConstructorArgs.get()[0]->getType() | 
|  | 926 | << TypeRange | 
|  | 927 | << ConstructorArgs.get()[0]->getSourceRange()); | 
| Richard Smith | a085da8 | 2011-03-17 16:11:59 +0000 | [diff] [blame] | 928 | if (!DeducedType) | 
|  | 929 | return ExprError(); | 
| Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 930 |  | 
| Richard Smith | a085da8 | 2011-03-17 16:11:59 +0000 | [diff] [blame] | 931 | AllocTypeInfo = DeducedType; | 
|  | 932 | AllocType = AllocTypeInfo->getType(); | 
| Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 933 | } | 
|  | 934 |  | 
| Douglas Gregor | 3caf04e | 2010-05-16 16:01:03 +0000 | [diff] [blame] | 935 | // Per C++0x [expr.new]p5, the type being constructed may be a | 
|  | 936 | // typedef of an array type. | 
| John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 937 | if (!ArraySize) { | 
| Douglas Gregor | 3caf04e | 2010-05-16 16:01:03 +0000 | [diff] [blame] | 938 | if (const ConstantArrayType *Array | 
|  | 939 | = Context.getAsConstantArrayType(AllocType)) { | 
| Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 940 | ArraySize = IntegerLiteral::Create(Context, Array->getSize(), | 
|  | 941 | Context.getSizeType(), | 
|  | 942 | TypeRange.getEnd()); | 
| Douglas Gregor | 3caf04e | 2010-05-16 16:01:03 +0000 | [diff] [blame] | 943 | AllocType = Array->getElementType(); | 
|  | 944 | } | 
|  | 945 | } | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 946 |  | 
| Douglas Gregor | a075076 | 2010-10-06 16:00:31 +0000 | [diff] [blame] | 947 | if (CheckAllocatedType(AllocType, TypeRange.getBegin(), TypeRange)) | 
|  | 948 | return ExprError(); | 
|  | 949 |  | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 950 | // In ARC, infer 'retaining' for the allocated | 
|  | 951 | if (getLangOptions().ObjCAutoRefCount && | 
|  | 952 | AllocType.getObjCLifetime() == Qualifiers::OCL_None && | 
|  | 953 | AllocType->isObjCLifetimeType()) { | 
|  | 954 | AllocType = Context.getLifetimeQualifiedType(AllocType, | 
|  | 955 | AllocType->getObjCARCImplicitLifetime()); | 
|  | 956 | } | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 957 |  | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 958 | QualType ResultType = Context.getPointerType(AllocType); | 
|  | 959 |  | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 960 | // C++ 5.3.4p6: "The expression in a direct-new-declarator shall have integral | 
|  | 961 | //   or enumeration type with a non-negative value." | 
| Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 962 | if (ArraySize && !ArraySize->isTypeDependent()) { | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 963 |  | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 964 | QualType SizeType = ArraySize->getType(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 965 |  | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 966 | ExprResult ConvertedSize | 
| John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 967 | = ConvertToIntegralOrEnumerationType(StartLoc, ArraySize, | 
| Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 968 | PDiag(diag::err_array_size_not_integral), | 
|  | 969 | PDiag(diag::err_array_size_incomplete_type) | 
|  | 970 | << ArraySize->getSourceRange(), | 
|  | 971 | PDiag(diag::err_array_size_explicit_conversion), | 
|  | 972 | PDiag(diag::note_array_size_conversion), | 
|  | 973 | PDiag(diag::err_array_size_ambiguous_conversion), | 
|  | 974 | PDiag(diag::note_array_size_conversion), | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 975 | PDiag(getLangOptions().CPlusPlus0x? 0 | 
| Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 976 | : diag::ext_array_size_conversion)); | 
|  | 977 | if (ConvertedSize.isInvalid()) | 
|  | 978 | return ExprError(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 979 |  | 
| John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 980 | ArraySize = ConvertedSize.take(); | 
| Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 981 | SizeType = ArraySize->getType(); | 
| Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 982 | if (!SizeType->isIntegralOrUnscopedEnumerationType()) | 
| Douglas Gregor | 6bc574d | 2010-06-30 00:20:43 +0000 | [diff] [blame] | 983 | return ExprError(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 984 |  | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 985 | // Let's see if this is a constant < 0. If so, we reject it out of hand. | 
|  | 986 | // We don't care about special rules, so we tell the machinery it's not | 
|  | 987 | // evaluated - it gives us a result in more cases. | 
| Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 988 | if (!ArraySize->isValueDependent()) { | 
|  | 989 | llvm::APSInt Value; | 
|  | 990 | if (ArraySize->isIntegerConstantExpr(Value, Context, 0, false)) { | 
|  | 991 | if (Value < llvm::APSInt( | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 992 | llvm::APInt::getNullValue(Value.getBitWidth()), | 
| Anders Carlsson | ac18b2e | 2009-09-23 00:37:25 +0000 | [diff] [blame] | 993 | Value.isUnsigned())) | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 994 | return ExprError(Diag(ArraySize->getSourceRange().getBegin(), | 
| Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 995 | diag::err_typecheck_negative_array_size) | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 996 | << ArraySize->getSourceRange()); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 997 |  | 
| Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 998 | if (!AllocType->isDependentType()) { | 
|  | 999 | unsigned ActiveSizeBits | 
|  | 1000 | = ConstantArrayType::getNumAddressingBits(Context, AllocType, Value); | 
|  | 1001 | if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context)) { | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1002 | Diag(ArraySize->getSourceRange().getBegin(), | 
| Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 1003 | diag::err_array_too_large) | 
|  | 1004 | << Value.toString(10) | 
|  | 1005 | << ArraySize->getSourceRange(); | 
|  | 1006 | return ExprError(); | 
|  | 1007 | } | 
|  | 1008 | } | 
| Douglas Gregor | 4bd4031 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 1009 | } else if (TypeIdParens.isValid()) { | 
|  | 1010 | // Can't have dynamic array size when the type-id is in parentheses. | 
|  | 1011 | Diag(ArraySize->getLocStart(), diag::ext_new_paren_array_nonconst) | 
|  | 1012 | << ArraySize->getSourceRange() | 
|  | 1013 | << FixItHint::CreateRemoval(TypeIdParens.getBegin()) | 
|  | 1014 | << FixItHint::CreateRemoval(TypeIdParens.getEnd()); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1015 |  | 
| Douglas Gregor | 4bd4031 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 1016 | TypeIdParens = SourceRange(); | 
| Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1017 | } | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1018 | } | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1019 |  | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1020 | // ARC: warn about ABI issues. | 
|  | 1021 | if (getLangOptions().ObjCAutoRefCount) { | 
|  | 1022 | QualType BaseAllocType = Context.getBaseElementType(AllocType); | 
|  | 1023 | if (BaseAllocType.hasStrongOrWeakObjCLifetime()) | 
|  | 1024 | Diag(StartLoc, diag::warn_err_new_delete_object_array) | 
|  | 1025 | << 0 << BaseAllocType; | 
|  | 1026 | } | 
|  | 1027 |  | 
| John McCall | 7d16627 | 2011-05-15 07:14:44 +0000 | [diff] [blame] | 1028 | // Note that we do *not* convert the argument in any way.  It can | 
|  | 1029 | // be signed, larger than size_t, whatever. | 
| Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1030 | } | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1031 |  | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1032 | FunctionDecl *OperatorNew = 0; | 
|  | 1033 | FunctionDecl *OperatorDelete = 0; | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1034 | Expr **PlaceArgs = (Expr**)PlacementArgs.get(); | 
|  | 1035 | unsigned NumPlaceArgs = PlacementArgs.size(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1036 |  | 
| Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1037 | if (!AllocType->isDependentType() && | 
|  | 1038 | !Expr::hasAnyTypeDependentArguments(PlaceArgs, NumPlaceArgs) && | 
|  | 1039 | FindAllocationFunctions(StartLoc, | 
| Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 1040 | SourceRange(PlacementLParen, PlacementRParen), | 
|  | 1041 | UseGlobal, AllocType, ArraySize, PlaceArgs, | 
|  | 1042 | NumPlaceArgs, OperatorNew, OperatorDelete)) | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1043 | return ExprError(); | 
| John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1044 |  | 
|  | 1045 | // If this is an array allocation, compute whether the usual array | 
|  | 1046 | // deallocation function for the type has a size_t parameter. | 
|  | 1047 | bool UsualArrayDeleteWantsSize = false; | 
|  | 1048 | if (ArraySize && !AllocType->isDependentType()) | 
|  | 1049 | UsualArrayDeleteWantsSize | 
|  | 1050 | = doesUsualArrayDeleteWantSize(*this, StartLoc, AllocType); | 
|  | 1051 |  | 
| Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1052 | SmallVector<Expr *, 8> AllPlaceArgs; | 
| Fariborz Jahanian | 498429f | 2009-11-19 18:39:40 +0000 | [diff] [blame] | 1053 | if (OperatorNew) { | 
|  | 1054 | // Add default arguments, if any. | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1055 | const FunctionProtoType *Proto = | 
| Fariborz Jahanian | 498429f | 2009-11-19 18:39:40 +0000 | [diff] [blame] | 1056 | OperatorNew->getType()->getAs<FunctionProtoType>(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1057 | VariadicCallType CallType = | 
| Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 1058 | Proto->isVariadic() ? VariadicFunction : VariadicDoesNotApply; | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1059 |  | 
| Anders Carlsson | 28e9483 | 2010-05-03 02:07:56 +0000 | [diff] [blame] | 1060 | if (GatherArgumentsForCall(PlacementLParen, OperatorNew, | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1061 | Proto, 1, PlaceArgs, NumPlaceArgs, | 
| Anders Carlsson | 28e9483 | 2010-05-03 02:07:56 +0000 | [diff] [blame] | 1062 | AllPlaceArgs, CallType)) | 
| Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 1063 | return ExprError(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1064 |  | 
| Fariborz Jahanian | 498429f | 2009-11-19 18:39:40 +0000 | [diff] [blame] | 1065 | NumPlaceArgs = AllPlaceArgs.size(); | 
|  | 1066 | if (NumPlaceArgs > 0) | 
|  | 1067 | PlaceArgs = &AllPlaceArgs[0]; | 
|  | 1068 | } | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1069 |  | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1070 | bool Init = ConstructorLParen.isValid(); | 
|  | 1071 | // --- Choosing a constructor --- | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1072 | CXXConstructorDecl *Constructor = 0; | 
| Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 1073 | bool HadMultipleCandidates = false; | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1074 | Expr **ConsArgs = (Expr**)ConstructorArgs.get(); | 
|  | 1075 | unsigned NumConsArgs = ConstructorArgs.size(); | 
| John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 1076 | ASTOwningVector<Expr*> ConvertedConstructorArgs(*this); | 
| Eli Friedman | a8ce9ec | 2009-11-08 22:15:39 +0000 | [diff] [blame] | 1077 |  | 
| Anders Carlsson | 48c9501 | 2010-05-03 15:45:23 +0000 | [diff] [blame] | 1078 | // Array 'new' can't have any initializers. | 
| Anders Carlsson | 55cbd6e | 2010-05-16 16:24:20 +0000 | [diff] [blame] | 1079 | if (NumConsArgs && (ResultType->isArrayType() || ArraySize)) { | 
| Anders Carlsson | 48c9501 | 2010-05-03 15:45:23 +0000 | [diff] [blame] | 1080 | SourceRange InitRange(ConsArgs[0]->getLocStart(), | 
|  | 1081 | ConsArgs[NumConsArgs - 1]->getLocEnd()); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1082 |  | 
| Anders Carlsson | 48c9501 | 2010-05-03 15:45:23 +0000 | [diff] [blame] | 1083 | Diag(StartLoc, diag::err_new_array_init_args) << InitRange; | 
|  | 1084 | return ExprError(); | 
|  | 1085 | } | 
|  | 1086 |  | 
| Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1087 | if (!AllocType->isDependentType() && | 
|  | 1088 | !Expr::hasAnyTypeDependentArguments(ConsArgs, NumConsArgs)) { | 
|  | 1089 | // C++0x [expr.new]p15: | 
|  | 1090 | //   A new-expression that creates an object of type T initializes that | 
|  | 1091 | //   object as follows: | 
|  | 1092 | InitializationKind Kind | 
|  | 1093 | //     - If the new-initializer is omitted, the object is default- | 
|  | 1094 | //       initialized (8.5); if no initialization is performed, | 
|  | 1095 | //       the object has indeterminate value | 
| Douglas Gregor | 1bb2a93 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 1096 | = !Init? InitializationKind::CreateDefault(TypeRange.getBegin()) | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1097 | //     - Otherwise, the new-initializer is interpreted according to the | 
| Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1098 | //       initialization rules of 8.5 for direct-initialization. | 
| Douglas Gregor | 1bb2a93 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 1099 | : InitializationKind::CreateDirect(TypeRange.getBegin(), | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1100 | ConstructorLParen, | 
| Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1101 | ConstructorRParen); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1102 |  | 
| Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1103 | InitializedEntity Entity | 
| Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 1104 | = InitializedEntity::InitializeNew(StartLoc, AllocType); | 
| Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1105 | InitializationSequence InitSeq(*this, Entity, Kind, ConsArgs, NumConsArgs); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1106 | ExprResult FullInit = InitSeq.Perform(*this, Entity, Kind, | 
| Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1107 | move(ConstructorArgs)); | 
|  | 1108 | if (FullInit.isInvalid()) | 
|  | 1109 | return ExprError(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1110 |  | 
|  | 1111 | // 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] | 1112 | // constructor call, which CXXNewExpr handles directly. | 
|  | 1113 | if (Expr *FullInitExpr = (Expr *)FullInit.get()) { | 
|  | 1114 | if (CXXBindTemporaryExpr *Binder | 
|  | 1115 | = dyn_cast<CXXBindTemporaryExpr>(FullInitExpr)) | 
|  | 1116 | FullInitExpr = Binder->getSubExpr(); | 
|  | 1117 | if (CXXConstructExpr *Construct | 
|  | 1118 | = dyn_cast<CXXConstructExpr>(FullInitExpr)) { | 
|  | 1119 | Constructor = Construct->getConstructor(); | 
| Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 1120 | HadMultipleCandidates = Construct->hadMultipleCandidates(); | 
| Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1121 | for (CXXConstructExpr::arg_iterator A = Construct->arg_begin(), | 
|  | 1122 | AEnd = Construct->arg_end(); | 
|  | 1123 | A != AEnd; ++A) | 
| John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 1124 | ConvertedConstructorArgs.push_back(*A); | 
| Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1125 | } else { | 
|  | 1126 | // Take the converted initializer. | 
|  | 1127 | ConvertedConstructorArgs.push_back(FullInit.release()); | 
|  | 1128 | } | 
|  | 1129 | } else { | 
|  | 1130 | // No initialization required. | 
|  | 1131 | } | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1132 |  | 
| Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 1133 | // Take the converted arguments and use them for the new expression. | 
| Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 1134 | NumConsArgs = ConvertedConstructorArgs.size(); | 
|  | 1135 | ConsArgs = (Expr **)ConvertedConstructorArgs.take(); | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1136 | } | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1137 |  | 
| Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1138 | // Mark the new and delete operators as referenced. | 
|  | 1139 | if (OperatorNew) | 
|  | 1140 | MarkDeclarationReferenced(StartLoc, OperatorNew); | 
|  | 1141 | if (OperatorDelete) | 
|  | 1142 | MarkDeclarationReferenced(StartLoc, OperatorDelete); | 
|  | 1143 |  | 
| John McCall | 84ff0fc | 2011-07-13 20:12:57 +0000 | [diff] [blame] | 1144 | // C++0x [expr.new]p17: | 
|  | 1145 | //   If the new expression creates an array of objects of class type, | 
|  | 1146 | //   access and ambiguity control are done for the destructor. | 
|  | 1147 | if (ArraySize && Constructor) { | 
|  | 1148 | if (CXXDestructorDecl *dtor = LookupDestructor(Constructor->getParent())) { | 
|  | 1149 | MarkDeclarationReferenced(StartLoc, dtor); | 
|  | 1150 | CheckDestructorAccess(StartLoc, dtor, | 
|  | 1151 | PDiag(diag::err_access_dtor) | 
|  | 1152 | << Context.getBaseElementType(AllocType)); | 
|  | 1153 | } | 
|  | 1154 | } | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1155 |  | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1156 | PlacementArgs.release(); | 
|  | 1157 | ConstructorArgs.release(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1158 |  | 
| Ted Kremenek | ad7fe86 | 2010-02-11 22:51:03 +0000 | [diff] [blame] | 1159 | return Owned(new (Context) CXXNewExpr(Context, UseGlobal, OperatorNew, | 
| Douglas Gregor | 4bd4031 | 2010-07-13 15:54:32 +0000 | [diff] [blame] | 1160 | PlaceArgs, NumPlaceArgs, TypeIdParens, | 
| Ted Kremenek | ad7fe86 | 2010-02-11 22:51:03 +0000 | [diff] [blame] | 1161 | ArraySize, Constructor, Init, | 
| Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 1162 | ConsArgs, NumConsArgs, | 
|  | 1163 | HadMultipleCandidates, | 
|  | 1164 | OperatorDelete, | 
| John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1165 | UsualArrayDeleteWantsSize, | 
| Douglas Gregor | 1bb2a93 | 2010-09-07 21:49:58 +0000 | [diff] [blame] | 1166 | ResultType, AllocTypeInfo, | 
|  | 1167 | StartLoc, | 
| Ted Kremenek | ad7fe86 | 2010-02-11 22:51:03 +0000 | [diff] [blame] | 1168 | Init ? ConstructorRParen : | 
| Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 1169 | TypeRange.getEnd(), | 
|  | 1170 | ConstructorLParen, ConstructorRParen)); | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1171 | } | 
|  | 1172 |  | 
|  | 1173 | /// CheckAllocatedType - Checks that a type is suitable as the allocated type | 
|  | 1174 | /// in a new-expression. | 
|  | 1175 | /// dimension off and stores the size expression in ArraySize. | 
| Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1176 | bool Sema::CheckAllocatedType(QualType AllocType, SourceLocation Loc, | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1177 | SourceRange R) { | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1178 | // C++ 5.3.4p1: "[The] type shall be a complete object type, but not an | 
|  | 1179 | //   abstract class type or array thereof. | 
| Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1180 | if (AllocType->isFunctionType()) | 
| Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1181 | return Diag(Loc, diag::err_bad_new_type) | 
|  | 1182 | << AllocType << 0 << R; | 
| Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1183 | else if (AllocType->isReferenceType()) | 
| Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1184 | return Diag(Loc, diag::err_bad_new_type) | 
|  | 1185 | << AllocType << 1 << R; | 
| Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1186 | else if (!AllocType->isDependentType() && | 
| Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1187 | RequireCompleteType(Loc, AllocType, | 
| Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 1188 | PDiag(diag::err_new_incomplete_type) | 
|  | 1189 | << R)) | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1190 | return true; | 
| Douglas Gregor | 3433cf7 | 2009-05-21 00:00:09 +0000 | [diff] [blame] | 1191 | else if (RequireNonAbstractType(Loc, AllocType, | 
| Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1192 | diag::err_allocation_of_abstract_type)) | 
|  | 1193 | return true; | 
| Douglas Gregor | a075076 | 2010-10-06 16:00:31 +0000 | [diff] [blame] | 1194 | else if (AllocType->isVariablyModifiedType()) | 
|  | 1195 | return Diag(Loc, diag::err_variably_modified_new_type) | 
|  | 1196 | << AllocType; | 
| Douglas Gregor | 5666d36 | 2011-04-15 19:46:20 +0000 | [diff] [blame] | 1197 | else if (unsigned AddressSpace = AllocType.getAddressSpace()) | 
|  | 1198 | return Diag(Loc, diag::err_address_space_qualified_new) | 
|  | 1199 | << AllocType.getUnqualifiedType() << AddressSpace; | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1200 | else if (getLangOptions().ObjCAutoRefCount) { | 
|  | 1201 | if (const ArrayType *AT = Context.getAsArrayType(AllocType)) { | 
|  | 1202 | QualType BaseAllocType = Context.getBaseElementType(AT); | 
|  | 1203 | if (BaseAllocType.getObjCLifetime() == Qualifiers::OCL_None && | 
|  | 1204 | BaseAllocType->isObjCLifetimeType()) | 
| Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 1205 | return Diag(Loc, diag::err_arc_new_array_without_ownership) | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1206 | << BaseAllocType; | 
|  | 1207 | } | 
|  | 1208 | } | 
| Douglas Gregor | 5666d36 | 2011-04-15 19:46:20 +0000 | [diff] [blame] | 1209 |  | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1210 | return false; | 
|  | 1211 | } | 
|  | 1212 |  | 
| Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1213 | /// \brief Determine whether the given function is a non-placement | 
|  | 1214 | /// deallocation function. | 
|  | 1215 | static bool isNonPlacementDeallocationFunction(FunctionDecl *FD) { | 
|  | 1216 | if (FD->isInvalidDecl()) | 
|  | 1217 | return false; | 
|  | 1218 |  | 
|  | 1219 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FD)) | 
|  | 1220 | return Method->isUsualDeallocationFunction(); | 
|  | 1221 |  | 
|  | 1222 | return ((FD->getOverloadedOperator() == OO_Delete || | 
|  | 1223 | FD->getOverloadedOperator() == OO_Array_Delete) && | 
|  | 1224 | FD->getNumParams() == 1); | 
|  | 1225 | } | 
|  | 1226 |  | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1227 | /// FindAllocationFunctions - Finds the overloads of operator new and delete | 
|  | 1228 | /// that are appropriate for the allocation. | 
| Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 1229 | bool Sema::FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range, | 
|  | 1230 | bool UseGlobal, QualType AllocType, | 
|  | 1231 | bool IsArray, Expr **PlaceArgs, | 
|  | 1232 | unsigned NumPlaceArgs, | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1233 | FunctionDecl *&OperatorNew, | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1234 | FunctionDecl *&OperatorDelete) { | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1235 | // --- Choosing an allocation function --- | 
|  | 1236 | // C++ 5.3.4p8 - 14 & 18 | 
|  | 1237 | // 1) If UseGlobal is true, only look in the global scope. Else, also look | 
|  | 1238 | //   in the scope of the allocated class. | 
|  | 1239 | // 2) If an array size is given, look for operator new[], else look for | 
|  | 1240 | //   operator new. | 
|  | 1241 | // 3) The first argument is always size_t. Append the arguments from the | 
|  | 1242 | //   placement form. | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1243 |  | 
| Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1244 | SmallVector<Expr*, 8> AllocArgs(1 + NumPlaceArgs); | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1245 | // We don't care about the actual value of this argument. | 
|  | 1246 | // FIXME: Should the Sema create the expression and embed it in the syntax | 
|  | 1247 | // tree? Or should the consumer just recalculate the value? | 
| Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 1248 | IntegerLiteral Size(Context, llvm::APInt::getNullValue( | 
| Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1249 | Context.getTargetInfo().getPointerWidth(0)), | 
| Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1250 | Context.getSizeType(), | 
|  | 1251 | SourceLocation()); | 
|  | 1252 | AllocArgs[0] = &Size; | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1253 | std::copy(PlaceArgs, PlaceArgs + NumPlaceArgs, AllocArgs.begin() + 1); | 
|  | 1254 |  | 
| Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1255 | // C++ [expr.new]p8: | 
|  | 1256 | //   If the allocated type is a non-array type, the allocation | 
| NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 1257 | //   function's name is operator new and the deallocation function's | 
| Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1258 | //   name is operator delete. If the allocated type is an array | 
| NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 1259 | //   type, the allocation function's name is operator new[] and the | 
|  | 1260 | //   deallocation function's name is operator delete[]. | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1261 | DeclarationName NewName = Context.DeclarationNames.getCXXOperatorName( | 
|  | 1262 | IsArray ? OO_Array_New : OO_New); | 
| Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1263 | DeclarationName DeleteName = Context.DeclarationNames.getCXXOperatorName( | 
|  | 1264 | IsArray ? OO_Array_Delete : OO_Delete); | 
|  | 1265 |  | 
| Argyrios Kyrtzidis | d293298 | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 1266 | QualType AllocElemType = Context.getBaseElementType(AllocType); | 
|  | 1267 |  | 
|  | 1268 | if (AllocElemType->isRecordType() && !UseGlobal) { | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1269 | CXXRecordDecl *Record | 
| Argyrios Kyrtzidis | d293298 | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 1270 | = cast<CXXRecordDecl>(AllocElemType->getAs<RecordType>()->getDecl()); | 
| Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 1271 | if (FindAllocationOverload(StartLoc, Range, NewName, &AllocArgs[0], | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1272 | AllocArgs.size(), Record, /*AllowMissing=*/true, | 
|  | 1273 | OperatorNew)) | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1274 | return true; | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1275 | } | 
|  | 1276 | if (!OperatorNew) { | 
|  | 1277 | // Didn't find a member overload. Look for a global one. | 
|  | 1278 | DeclareGlobalNewDelete(); | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1279 | DeclContext *TUDecl = Context.getTranslationUnitDecl(); | 
| Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 1280 | if (FindAllocationOverload(StartLoc, Range, NewName, &AllocArgs[0], | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1281 | AllocArgs.size(), TUDecl, /*AllowMissing=*/false, | 
|  | 1282 | OperatorNew)) | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1283 | return true; | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1284 | } | 
|  | 1285 |  | 
| John McCall | 9c82afc | 2010-04-20 02:18:25 +0000 | [diff] [blame] | 1286 | // We don't need an operator delete if we're running under | 
|  | 1287 | // -fno-exceptions. | 
|  | 1288 | if (!getLangOptions().Exceptions) { | 
|  | 1289 | OperatorDelete = 0; | 
|  | 1290 | return false; | 
|  | 1291 | } | 
|  | 1292 |  | 
| Anders Carlsson | d958389 | 2009-05-31 20:26:12 +0000 | [diff] [blame] | 1293 | // FindAllocationOverload can change the passed in arguments, so we need to | 
|  | 1294 | // copy them back. | 
|  | 1295 | if (NumPlaceArgs > 0) | 
|  | 1296 | std::copy(&AllocArgs[1], AllocArgs.end(), PlaceArgs); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1297 |  | 
| Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1298 | // C++ [expr.new]p19: | 
|  | 1299 | // | 
|  | 1300 | //   If the new-expression begins with a unary :: operator, the | 
| NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 1301 | //   deallocation function's name is looked up in the global | 
| Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1302 | //   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] | 1303 | //   array thereof, the deallocation function's name is looked up in | 
| Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1304 | //   the scope of T. If this lookup fails to find the name, or if | 
|  | 1305 | //   the allocated type is not a class type or array thereof, the | 
| NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 1306 | //   deallocation function's name is looked up in the global scope. | 
| Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1307 | LookupResult FoundDelete(*this, DeleteName, StartLoc, LookupOrdinaryName); | 
| Argyrios Kyrtzidis | d293298 | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 1308 | if (AllocElemType->isRecordType() && !UseGlobal) { | 
| Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1309 | CXXRecordDecl *RD | 
| Argyrios Kyrtzidis | d293298 | 2010-08-25 23:14:56 +0000 | [diff] [blame] | 1310 | = cast<CXXRecordDecl>(AllocElemType->getAs<RecordType>()->getDecl()); | 
| Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1311 | LookupQualifiedName(FoundDelete, RD); | 
|  | 1312 | } | 
| John McCall | 90c8c57 | 2010-03-18 08:19:33 +0000 | [diff] [blame] | 1313 | if (FoundDelete.isAmbiguous()) | 
|  | 1314 | return true; // FIXME: clean up expressions? | 
| Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1315 |  | 
|  | 1316 | if (FoundDelete.empty()) { | 
|  | 1317 | DeclareGlobalNewDelete(); | 
|  | 1318 | LookupQualifiedName(FoundDelete, Context.getTranslationUnitDecl()); | 
|  | 1319 | } | 
|  | 1320 |  | 
|  | 1321 | FoundDelete.suppressDiagnostics(); | 
| John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1322 |  | 
| Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1323 | SmallVector<std::pair<DeclAccessPair,FunctionDecl*>, 2> Matches; | 
| John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1324 |  | 
| John McCall | edeb6c9 | 2010-09-14 21:34:24 +0000 | [diff] [blame] | 1325 | // Whether we're looking for a placement operator delete is dictated | 
|  | 1326 | // by whether we selected a placement operator new, not by whether | 
|  | 1327 | // we had explicit placement arguments.  This matters for things like | 
|  | 1328 | //   struct A { void *operator new(size_t, int = 0); ... }; | 
|  | 1329 | //   A *a = new A() | 
|  | 1330 | bool isPlacementNew = (NumPlaceArgs > 0 || OperatorNew->param_size() != 1); | 
|  | 1331 |  | 
|  | 1332 | if (isPlacementNew) { | 
| Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1333 | // C++ [expr.new]p20: | 
|  | 1334 | //   A declaration of a placement deallocation function matches the | 
|  | 1335 | //   declaration of a placement allocation function if it has the | 
|  | 1336 | //   same number of parameters and, after parameter transformations | 
|  | 1337 | //   (8.3.5), all parameter types except the first are | 
|  | 1338 | //   identical. [...] | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1339 | // | 
| Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1340 | // To perform this comparison, we compute the function type that | 
|  | 1341 | // the deallocation function should have, and use that type both | 
|  | 1342 | // for template argument deduction and for comparison purposes. | 
| John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1343 | // | 
|  | 1344 | // FIXME: this comparison should ignore CC and the like. | 
| Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1345 | QualType ExpectedFunctionType; | 
|  | 1346 | { | 
|  | 1347 | const FunctionProtoType *Proto | 
|  | 1348 | = OperatorNew->getType()->getAs<FunctionProtoType>(); | 
| John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1349 |  | 
| Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1350 | SmallVector<QualType, 4> ArgTypes; | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1351 | ArgTypes.push_back(Context.VoidPtrTy); | 
| Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1352 | for (unsigned I = 1, N = Proto->getNumArgs(); I < N; ++I) | 
|  | 1353 | ArgTypes.push_back(Proto->getArgType(I)); | 
|  | 1354 |  | 
| John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1355 | FunctionProtoType::ExtProtoInfo EPI; | 
|  | 1356 | EPI.Variadic = Proto->isVariadic(); | 
|  | 1357 |  | 
| Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1358 | ExpectedFunctionType | 
|  | 1359 | = Context.getFunctionType(Context.VoidTy, ArgTypes.data(), | 
| John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1360 | ArgTypes.size(), EPI); | 
| Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1361 | } | 
|  | 1362 |  | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1363 | for (LookupResult::iterator D = FoundDelete.begin(), | 
| Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1364 | DEnd = FoundDelete.end(); | 
|  | 1365 | D != DEnd; ++D) { | 
|  | 1366 | FunctionDecl *Fn = 0; | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1367 | if (FunctionTemplateDecl *FnTmpl | 
| Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1368 | = dyn_cast<FunctionTemplateDecl>((*D)->getUnderlyingDecl())) { | 
|  | 1369 | // Perform template argument deduction to try to match the | 
|  | 1370 | // expected function type. | 
|  | 1371 | TemplateDeductionInfo Info(Context, StartLoc); | 
|  | 1372 | if (DeduceTemplateArguments(FnTmpl, 0, ExpectedFunctionType, Fn, Info)) | 
|  | 1373 | continue; | 
|  | 1374 | } else | 
|  | 1375 | Fn = cast<FunctionDecl>((*D)->getUnderlyingDecl()); | 
|  | 1376 |  | 
|  | 1377 | if (Context.hasSameType(Fn->getType(), ExpectedFunctionType)) | 
| John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1378 | Matches.push_back(std::make_pair(D.getPair(), Fn)); | 
| Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1379 | } | 
|  | 1380 | } else { | 
|  | 1381 | // C++ [expr.new]p20: | 
|  | 1382 | //   [...] Any non-placement deallocation function matches a | 
|  | 1383 | //   non-placement allocation function. [...] | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1384 | for (LookupResult::iterator D = FoundDelete.begin(), | 
| Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1385 | DEnd = FoundDelete.end(); | 
|  | 1386 | D != DEnd; ++D) { | 
|  | 1387 | if (FunctionDecl *Fn = dyn_cast<FunctionDecl>((*D)->getUnderlyingDecl())) | 
|  | 1388 | if (isNonPlacementDeallocationFunction(Fn)) | 
| John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1389 | Matches.push_back(std::make_pair(D.getPair(), Fn)); | 
| Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1390 | } | 
|  | 1391 | } | 
|  | 1392 |  | 
|  | 1393 | // C++ [expr.new]p20: | 
|  | 1394 | //   [...] If the lookup finds a single matching deallocation | 
|  | 1395 | //   function, that function will be called; otherwise, no | 
|  | 1396 | //   deallocation function will be called. | 
|  | 1397 | if (Matches.size() == 1) { | 
| John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1398 | OperatorDelete = Matches[0].second; | 
| Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1399 |  | 
|  | 1400 | // C++0x [expr.new]p20: | 
|  | 1401 | //   If the lookup finds the two-parameter form of a usual | 
|  | 1402 | //   deallocation function (3.7.4.2) and that function, considered | 
|  | 1403 | //   as a placement deallocation function, would have been | 
|  | 1404 | //   selected as a match for the allocation function, the program | 
|  | 1405 | //   is ill-formed. | 
|  | 1406 | if (NumPlaceArgs && getLangOptions().CPlusPlus0x && | 
|  | 1407 | isNonPlacementDeallocationFunction(OperatorDelete)) { | 
|  | 1408 | Diag(StartLoc, diag::err_placement_new_non_placement_delete) | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1409 | << SourceRange(PlaceArgs[0]->getLocStart(), | 
| Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1410 | PlaceArgs[NumPlaceArgs - 1]->getLocEnd()); | 
|  | 1411 | Diag(OperatorDelete->getLocation(), diag::note_previous_decl) | 
|  | 1412 | << DeleteName; | 
| John McCall | 90c8c57 | 2010-03-18 08:19:33 +0000 | [diff] [blame] | 1413 | } else { | 
|  | 1414 | CheckAllocationAccess(StartLoc, Range, FoundDelete.getNamingClass(), | 
| John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1415 | Matches[0].first); | 
| Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1416 | } | 
|  | 1417 | } | 
|  | 1418 |  | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1419 | return false; | 
|  | 1420 | } | 
|  | 1421 |  | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1422 | /// FindAllocationOverload - Find an fitting overload for the allocation | 
|  | 1423 | /// function in the specified scope. | 
| Sebastian Redl | 00e68e2 | 2009-02-09 18:24:27 +0000 | [diff] [blame] | 1424 | bool Sema::FindAllocationOverload(SourceLocation StartLoc, SourceRange Range, | 
|  | 1425 | DeclarationName Name, Expr** Args, | 
|  | 1426 | unsigned NumArgs, DeclContext *Ctx, | 
| Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1427 | bool AllowMissing, FunctionDecl *&Operator, | 
|  | 1428 | bool Diagnose) { | 
| John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1429 | LookupResult R(*this, Name, StartLoc, LookupOrdinaryName); | 
|  | 1430 | LookupQualifiedName(R, Ctx); | 
| John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1431 | if (R.empty()) { | 
| Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1432 | if (AllowMissing || !Diagnose) | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1433 | return false; | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1434 | return Diag(StartLoc, diag::err_ovl_no_viable_function_in_call) | 
| Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 1435 | << Name << Range; | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1436 | } | 
|  | 1437 |  | 
| John McCall | 90c8c57 | 2010-03-18 08:19:33 +0000 | [diff] [blame] | 1438 | if (R.isAmbiguous()) | 
|  | 1439 | return true; | 
|  | 1440 |  | 
|  | 1441 | R.suppressDiagnostics(); | 
| John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1442 |  | 
| John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1443 | OverloadCandidateSet Candidates(StartLoc); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1444 | for (LookupResult::iterator Alloc = R.begin(), AllocEnd = R.end(); | 
| Douglas Gregor | 5d64e5b | 2009-09-30 00:03:47 +0000 | [diff] [blame] | 1445 | Alloc != AllocEnd; ++Alloc) { | 
| Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 1446 | // Even member operator new/delete are implicitly treated as | 
|  | 1447 | // static, so don't use AddMemberCandidate. | 
| John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1448 | NamedDecl *D = (*Alloc)->getUnderlyingDecl(); | 
| Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1449 |  | 
| John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1450 | if (FunctionTemplateDecl *FnTemplate = dyn_cast<FunctionTemplateDecl>(D)) { | 
|  | 1451 | AddTemplateOverloadCandidate(FnTemplate, Alloc.getPair(), | 
| Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1452 | /*ExplicitTemplateArgs=*/0, Args, NumArgs, | 
|  | 1453 | Candidates, | 
|  | 1454 | /*SuppressUserConversions=*/false); | 
| Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 1455 | continue; | 
| Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1456 | } | 
|  | 1457 |  | 
| John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 1458 | FunctionDecl *Fn = cast<FunctionDecl>(D); | 
|  | 1459 | AddOverloadCandidate(Fn, Alloc.getPair(), Args, NumArgs, Candidates, | 
| Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1460 | /*SuppressUserConversions=*/false); | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1461 | } | 
|  | 1462 |  | 
|  | 1463 | // Do the resolution. | 
|  | 1464 | OverloadCandidateSet::iterator Best; | 
| John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 1465 | switch (Candidates.BestViableFunction(*this, StartLoc, Best)) { | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1466 | case OR_Success: { | 
|  | 1467 | // Got one! | 
|  | 1468 | FunctionDecl *FnDecl = Best->Function; | 
| Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 1469 | MarkDeclarationReferenced(StartLoc, FnDecl); | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1470 | // The first argument is size_t, and the first parameter must be size_t, | 
|  | 1471 | // too. This is checked on declaration and can be assumed. (It can't be | 
|  | 1472 | // asserted on, though, since invalid decls are left in there.) | 
| John McCall | 90c8c57 | 2010-03-18 08:19:33 +0000 | [diff] [blame] | 1473 | // Watch out for variadic allocator function. | 
| Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 1474 | unsigned NumArgsInFnDecl = FnDecl->getNumParams(); | 
|  | 1475 | for (unsigned i = 0; (i < NumArgs && i < NumArgsInFnDecl); ++i) { | 
| Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1476 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, | 
|  | 1477 | FnDecl->getParamDecl(i)); | 
|  | 1478 |  | 
|  | 1479 | if (!Diagnose && !CanPerformCopyInitialization(Entity, Owned(Args[i]))) | 
|  | 1480 | return true; | 
|  | 1481 |  | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1482 | ExprResult Result | 
| Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1483 | = PerformCopyInitialization(Entity, SourceLocation(), Owned(Args[i])); | 
| Douglas Gregor | 29ecaba | 2010-03-26 20:35:59 +0000 | [diff] [blame] | 1484 | if (Result.isInvalid()) | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1485 | return true; | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1486 |  | 
| Douglas Gregor | 29ecaba | 2010-03-26 20:35:59 +0000 | [diff] [blame] | 1487 | Args[i] = Result.takeAs<Expr>(); | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1488 | } | 
|  | 1489 | Operator = FnDecl; | 
| Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1490 | CheckAllocationAccess(StartLoc, Range, R.getNamingClass(), Best->FoundDecl, | 
|  | 1491 | Diagnose); | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1492 | return false; | 
|  | 1493 | } | 
|  | 1494 |  | 
|  | 1495 | case OR_No_Viable_Function: | 
| Chandler Carruth | 361d380 | 2011-06-08 10:26:03 +0000 | [diff] [blame] | 1496 | if (Diagnose) { | 
| Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1497 | Diag(StartLoc, diag::err_ovl_no_viable_function_in_call) | 
|  | 1498 | << Name << Range; | 
| Chandler Carruth | 361d380 | 2011-06-08 10:26:03 +0000 | [diff] [blame] | 1499 | Candidates.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); | 
|  | 1500 | } | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1501 | return true; | 
|  | 1502 |  | 
|  | 1503 | case OR_Ambiguous: | 
| Chandler Carruth | 361d380 | 2011-06-08 10:26:03 +0000 | [diff] [blame] | 1504 | if (Diagnose) { | 
| Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1505 | Diag(StartLoc, diag::err_ovl_ambiguous_call) | 
|  | 1506 | << Name << Range; | 
| Chandler Carruth | 361d380 | 2011-06-08 10:26:03 +0000 | [diff] [blame] | 1507 | Candidates.NoteCandidates(*this, OCD_ViableCandidates, Args, NumArgs); | 
|  | 1508 | } | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1509 | return true; | 
| Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1510 |  | 
| Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1511 | case OR_Deleted: { | 
| Chandler Carruth | 361d380 | 2011-06-08 10:26:03 +0000 | [diff] [blame] | 1512 | if (Diagnose) { | 
| Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1513 | Diag(StartLoc, diag::err_ovl_deleted_call) | 
|  | 1514 | << Best->Function->isDeleted() | 
|  | 1515 | << Name | 
|  | 1516 | << getDeletedOrUnavailableSuffix(Best->Function) | 
|  | 1517 | << Range; | 
| Chandler Carruth | 361d380 | 2011-06-08 10:26:03 +0000 | [diff] [blame] | 1518 | Candidates.NoteCandidates(*this, OCD_AllCandidates, Args, NumArgs); | 
|  | 1519 | } | 
| Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1520 | return true; | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1521 | } | 
| Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1522 | } | 
| David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1523 | llvm_unreachable("Unreachable, bad result from BestViableFunction"); | 
| Sebastian Redl | 7f66239 | 2008-12-04 22:20:51 +0000 | [diff] [blame] | 1524 | } | 
|  | 1525 |  | 
|  | 1526 |  | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1527 | /// DeclareGlobalNewDelete - Declare the global forms of operator new and | 
|  | 1528 | /// delete. These are: | 
|  | 1529 | /// @code | 
| Sebastian Redl | 8999fe1 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1530 | ///   // C++03: | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1531 | ///   void* operator new(std::size_t) throw(std::bad_alloc); | 
|  | 1532 | ///   void* operator new[](std::size_t) throw(std::bad_alloc); | 
|  | 1533 | ///   void operator delete(void *) throw(); | 
|  | 1534 | ///   void operator delete[](void *) throw(); | 
| Sebastian Redl | 8999fe1 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1535 | ///   // C++0x: | 
|  | 1536 | ///   void* operator new(std::size_t); | 
|  | 1537 | ///   void* operator new[](std::size_t); | 
|  | 1538 | ///   void operator delete(void *); | 
|  | 1539 | ///   void operator delete[](void *); | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1540 | /// @endcode | 
| Sebastian Redl | 8999fe1 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1541 | /// C++0x operator delete is implicitly noexcept. | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1542 | /// Note that the placement and nothrow forms of new are *not* implicitly | 
|  | 1543 | /// declared. Their use requires including \<new\>. | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1544 | void Sema::DeclareGlobalNewDelete() { | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1545 | if (GlobalNewDeleteDeclared) | 
|  | 1546 | return; | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1547 |  | 
| Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1548 | // C++ [basic.std.dynamic]p2: | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1549 | //   [...] The following allocation and deallocation functions (18.4) are | 
|  | 1550 | //   implicitly declared in global scope in each translation unit of a | 
| Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1551 | //   program | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1552 | // | 
| Sebastian Redl | 8999fe1 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1553 | //     C++03: | 
| Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1554 | //     void* operator new(std::size_t) throw(std::bad_alloc); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1555 | //     void* operator new[](std::size_t) throw(std::bad_alloc); | 
|  | 1556 | //     void  operator delete(void*) throw(); | 
| Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1557 | //     void  operator delete[](void*) throw(); | 
| Sebastian Redl | 8999fe1 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1558 | //     C++0x: | 
|  | 1559 | //     void* operator new(std::size_t); | 
|  | 1560 | //     void* operator new[](std::size_t); | 
|  | 1561 | //     void  operator delete(void*); | 
|  | 1562 | //     void  operator delete[](void*); | 
| Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1563 | // | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1564 | //   These implicit declarations introduce only the function names operator | 
| Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1565 | //   new, operator new[], operator delete, operator delete[]. | 
|  | 1566 | // | 
|  | 1567 | // Here, we need to refer to std::bad_alloc, so we will implicitly declare | 
|  | 1568 | // "std" or "bad_alloc" as necessary to form the exception specification. | 
|  | 1569 | // However, we do not make these implicit declarations visible to name | 
|  | 1570 | // lookup. | 
| Sebastian Redl | 8999fe1 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1571 | // Note that the C++0x versions of operator delete are deallocation functions, | 
|  | 1572 | // and thus are implicitly noexcept. | 
|  | 1573 | if (!StdBadAlloc && !getLangOptions().CPlusPlus0x) { | 
| Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1574 | // The "std::bad_alloc" class has not yet been declared, so build it | 
|  | 1575 | // implicitly. | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1576 | StdBadAlloc = CXXRecordDecl::Create(Context, TTK_Class, | 
|  | 1577 | getOrCreateStdNamespace(), | 
| Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1578 | SourceLocation(), SourceLocation(), | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1579 | &PP.getIdentifierTable().get("bad_alloc"), | 
| Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 1580 | 0); | 
| Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 1581 | getStdBadAlloc()->setImplicit(true); | 
| Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1582 | } | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1583 |  | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1584 | GlobalNewDeleteDeclared = true; | 
|  | 1585 |  | 
|  | 1586 | QualType VoidPtr = Context.getPointerType(Context.VoidTy); | 
|  | 1587 | QualType SizeT = Context.getSizeType(); | 
| Nuno Lopes | fc28448 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 1588 | bool AssumeSaneOperatorNew = getLangOptions().AssumeSaneOperatorNew; | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1589 |  | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1590 | DeclareGlobalAllocationFunction( | 
|  | 1591 | Context.DeclarationNames.getCXXOperatorName(OO_New), | 
| Nuno Lopes | fc28448 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 1592 | VoidPtr, SizeT, AssumeSaneOperatorNew); | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1593 | DeclareGlobalAllocationFunction( | 
|  | 1594 | Context.DeclarationNames.getCXXOperatorName(OO_Array_New), | 
| Nuno Lopes | fc28448 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 1595 | VoidPtr, SizeT, AssumeSaneOperatorNew); | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1596 | DeclareGlobalAllocationFunction( | 
|  | 1597 | Context.DeclarationNames.getCXXOperatorName(OO_Delete), | 
|  | 1598 | Context.VoidTy, VoidPtr); | 
|  | 1599 | DeclareGlobalAllocationFunction( | 
|  | 1600 | Context.DeclarationNames.getCXXOperatorName(OO_Array_Delete), | 
|  | 1601 | Context.VoidTy, VoidPtr); | 
|  | 1602 | } | 
|  | 1603 |  | 
|  | 1604 | /// DeclareGlobalAllocationFunction - Declares a single implicit global | 
|  | 1605 | /// allocation function if it doesn't already exist. | 
|  | 1606 | void Sema::DeclareGlobalAllocationFunction(DeclarationName Name, | 
| Nuno Lopes | fc28448 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 1607 | QualType Return, QualType Argument, | 
|  | 1608 | bool AddMallocAttr) { | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1609 | DeclContext *GlobalCtx = Context.getTranslationUnitDecl(); | 
|  | 1610 |  | 
|  | 1611 | // Check if this function is already declared. | 
| Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1612 | { | 
| Douglas Gregor | 5cc3709 | 2008-12-23 22:05:29 +0000 | [diff] [blame] | 1613 | DeclContext::lookup_iterator Alloc, AllocEnd; | 
| Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1614 | for (llvm::tie(Alloc, AllocEnd) = GlobalCtx->lookup(Name); | 
| Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1615 | Alloc != AllocEnd; ++Alloc) { | 
| Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1616 | // Only look at non-template functions, as it is the predefined, | 
|  | 1617 | // non-templated allocation function we are trying to declare here. | 
|  | 1618 | if (FunctionDecl *Func = dyn_cast<FunctionDecl>(*Alloc)) { | 
|  | 1619 | QualType InitialParamType = | 
| Douglas Gregor | 6e790ab | 2009-12-22 23:42:49 +0000 | [diff] [blame] | 1620 | Context.getCanonicalType( | 
| Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1621 | Func->getParamDecl(0)->getType().getUnqualifiedType()); | 
|  | 1622 | // FIXME: Do we need to check for default arguments here? | 
| Douglas Gregor | 7b86862 | 2010-08-18 15:06:25 +0000 | [diff] [blame] | 1623 | if (Func->getNumParams() == 1 && InitialParamType == Argument) { | 
|  | 1624 | if(AddMallocAttr && !Func->hasAttr<MallocAttr>()) | 
| Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1625 | Func->addAttr(::new (Context) MallocAttr(SourceLocation(), Context)); | 
| Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1626 | return; | 
| Douglas Gregor | 7b86862 | 2010-08-18 15:06:25 +0000 | [diff] [blame] | 1627 | } | 
| Chandler Carruth | 4a73ea9 | 2010-02-03 11:02:14 +0000 | [diff] [blame] | 1628 | } | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1629 | } | 
|  | 1630 | } | 
|  | 1631 |  | 
| Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1632 | QualType BadAllocType; | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1633 | bool HasBadAllocExceptionSpec | 
| Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1634 | = (Name.getCXXOverloadedOperator() == OO_New || | 
|  | 1635 | Name.getCXXOverloadedOperator() == OO_Array_New); | 
| Sebastian Redl | 8999fe1 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1636 | if (HasBadAllocExceptionSpec && !getLangOptions().CPlusPlus0x) { | 
| Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1637 | assert(StdBadAlloc && "Must have std::bad_alloc declared"); | 
| Argyrios Kyrtzidis | 76c38d3 | 2010-08-02 07:14:54 +0000 | [diff] [blame] | 1638 | BadAllocType = Context.getTypeDeclType(getStdBadAlloc()); | 
| Douglas Gregor | 7adb10f | 2009-09-15 22:30:29 +0000 | [diff] [blame] | 1639 | } | 
| John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1640 |  | 
|  | 1641 | FunctionProtoType::ExtProtoInfo EPI; | 
| John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1642 | if (HasBadAllocExceptionSpec) { | 
| Sebastian Redl | 8999fe1 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1643 | if (!getLangOptions().CPlusPlus0x) { | 
|  | 1644 | EPI.ExceptionSpecType = EST_Dynamic; | 
|  | 1645 | EPI.NumExceptions = 1; | 
|  | 1646 | EPI.Exceptions = &BadAllocType; | 
|  | 1647 | } | 
| Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 1648 | } else { | 
| Sebastian Redl | 8999fe1 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1649 | EPI.ExceptionSpecType = getLangOptions().CPlusPlus0x ? | 
|  | 1650 | EST_BasicNoexcept : EST_DynamicNone; | 
| John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1651 | } | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1652 |  | 
| John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1653 | QualType FnType = Context.getFunctionType(Return, &Argument, 1, EPI); | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1654 | FunctionDecl *Alloc = | 
| Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1655 | FunctionDecl::Create(Context, GlobalCtx, SourceLocation(), | 
|  | 1656 | SourceLocation(), Name, | 
| John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1657 | FnType, /*TInfo=*/0, SC_None, | 
|  | 1658 | SC_None, false, true); | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1659 | Alloc->setImplicit(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1660 |  | 
| Nuno Lopes | fc28448 | 2009-12-16 16:59:22 +0000 | [diff] [blame] | 1661 | if (AddMallocAttr) | 
| Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1662 | Alloc->addAttr(::new (Context) MallocAttr(SourceLocation(), Context)); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1663 |  | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1664 | ParmVarDecl *Param = ParmVarDecl::Create(Context, Alloc, SourceLocation(), | 
| Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1665 | SourceLocation(), 0, | 
|  | 1666 | Argument, /*TInfo=*/0, | 
|  | 1667 | SC_None, SC_None, 0); | 
| David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 1668 | Alloc->setParams(Param); | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1669 |  | 
| Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1670 | // FIXME: Also add this declaration to the IdentifierResolver, but | 
|  | 1671 | // make sure it is at the end of the chain to coincide with the | 
|  | 1672 | // global scope. | 
| John McCall | 5f1e094 | 2010-08-24 08:50:51 +0000 | [diff] [blame] | 1673 | Context.getTranslationUnitDecl()->addDecl(Alloc); | 
| Sebastian Redl | b5a57a6 | 2008-12-03 20:26:15 +0000 | [diff] [blame] | 1674 | } | 
|  | 1675 |  | 
| Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1676 | bool Sema::FindDeallocationFunction(SourceLocation StartLoc, CXXRecordDecl *RD, | 
|  | 1677 | DeclarationName Name, | 
| Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1678 | FunctionDecl* &Operator, bool Diagnose) { | 
| John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1679 | LookupResult Found(*this, Name, StartLoc, LookupOrdinaryName); | 
| Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1680 | // Try to find operator delete/operator delete[] in class scope. | 
| John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1681 | LookupQualifiedName(Found, RD); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1682 |  | 
| John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 1683 | if (Found.isAmbiguous()) | 
| Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1684 | return true; | 
| Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1685 |  | 
| Chandler Carruth | 2389324 | 2010-06-28 00:30:51 +0000 | [diff] [blame] | 1686 | Found.suppressDiagnostics(); | 
|  | 1687 |  | 
| Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1688 | SmallVector<DeclAccessPair,4> Matches; | 
| Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1689 | for (LookupResult::iterator F = Found.begin(), FEnd = Found.end(); | 
|  | 1690 | F != FEnd; ++F) { | 
| Chandler Carruth | 09556fd | 2010-08-08 07:04:00 +0000 | [diff] [blame] | 1691 | NamedDecl *ND = (*F)->getUnderlyingDecl(); | 
|  | 1692 |  | 
|  | 1693 | // Ignore template operator delete members from the check for a usual | 
|  | 1694 | // deallocation function. | 
|  | 1695 | if (isa<FunctionTemplateDecl>(ND)) | 
|  | 1696 | continue; | 
|  | 1697 |  | 
|  | 1698 | if (cast<CXXMethodDecl>(ND)->isUsualDeallocationFunction()) | 
| John McCall | 046a746 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 1699 | Matches.push_back(F.getPair()); | 
|  | 1700 | } | 
|  | 1701 |  | 
|  | 1702 | // There's exactly one suitable operator;  pick it. | 
|  | 1703 | if (Matches.size() == 1) { | 
|  | 1704 | Operator = cast<CXXMethodDecl>(Matches[0]->getUnderlyingDecl()); | 
| Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1705 |  | 
|  | 1706 | if (Operator->isDeleted()) { | 
|  | 1707 | if (Diagnose) { | 
|  | 1708 | Diag(StartLoc, diag::err_deleted_function_use); | 
|  | 1709 | Diag(Operator->getLocation(), diag::note_unavailable_here) << true; | 
|  | 1710 | } | 
|  | 1711 | return true; | 
|  | 1712 | } | 
|  | 1713 |  | 
| John McCall | 046a746 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 1714 | CheckAllocationAccess(StartLoc, SourceRange(), Found.getNamingClass(), | 
| Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1715 | Matches[0], Diagnose); | 
| John McCall | 046a746 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 1716 | return false; | 
|  | 1717 |  | 
|  | 1718 | // We found multiple suitable operators;  complain about the ambiguity. | 
|  | 1719 | } else if (!Matches.empty()) { | 
| Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1720 | if (Diagnose) { | 
| Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 1721 | Diag(StartLoc, diag::err_ambiguous_suitable_delete_member_function_found) | 
|  | 1722 | << Name << RD; | 
| John McCall | 046a746 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 1723 |  | 
| Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1724 | for (SmallVectorImpl<DeclAccessPair>::iterator | 
| Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 1725 | F = Matches.begin(), FEnd = Matches.end(); F != FEnd; ++F) | 
|  | 1726 | Diag((*F)->getUnderlyingDecl()->getLocation(), | 
|  | 1727 | diag::note_member_declared_here) << Name; | 
|  | 1728 | } | 
| John McCall | 046a746 | 2010-08-04 00:31:26 +0000 | [diff] [blame] | 1729 | return true; | 
| Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1730 | } | 
|  | 1731 |  | 
|  | 1732 | // We did find operator delete/operator delete[] declarations, but | 
|  | 1733 | // none of them were suitable. | 
|  | 1734 | if (!Found.empty()) { | 
| Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1735 | if (Diagnose) { | 
| Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 1736 | Diag(StartLoc, diag::err_no_suitable_delete_member_function_found) | 
|  | 1737 | << Name << RD; | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1738 |  | 
| Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 1739 | for (LookupResult::iterator F = Found.begin(), FEnd = Found.end(); | 
|  | 1740 | F != FEnd; ++F) | 
|  | 1741 | Diag((*F)->getUnderlyingDecl()->getLocation(), | 
|  | 1742 | diag::note_member_declared_here) << Name; | 
|  | 1743 | } | 
| Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1744 | return true; | 
|  | 1745 | } | 
|  | 1746 |  | 
|  | 1747 | // Look for a global declaration. | 
|  | 1748 | DeclareGlobalNewDelete(); | 
|  | 1749 | DeclContext *TUDecl = Context.getTranslationUnitDecl(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1750 |  | 
| Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1751 | CXXNullPtrLiteralExpr Null(Context.VoidPtrTy, SourceLocation()); | 
|  | 1752 | Expr* DeallocArgs[1]; | 
|  | 1753 | DeallocArgs[0] = &Null; | 
|  | 1754 | if (FindAllocationOverload(StartLoc, SourceRange(), Name, | 
| Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 1755 | DeallocArgs, 1, TUDecl, !Diagnose, | 
|  | 1756 | Operator, Diagnose)) | 
| Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1757 | return true; | 
|  | 1758 |  | 
|  | 1759 | assert(Operator && "Did not find a deallocation function!"); | 
|  | 1760 | return false; | 
|  | 1761 | } | 
|  | 1762 |  | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1763 | /// ActOnCXXDelete - Parsed a C++ 'delete' expression (C++ 5.3.5), as in: | 
|  | 1764 | /// @code ::delete ptr; @endcode | 
|  | 1765 | /// or | 
|  | 1766 | /// @code delete [] ptr; @endcode | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1767 | ExprResult | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1768 | Sema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal, | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1769 | bool ArrayForm, Expr *ExE) { | 
| Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1770 | // C++ [expr.delete]p1: | 
|  | 1771 | //   The operand shall have a pointer type, or a class type having a single | 
|  | 1772 | //   conversion function to a pointer type. The result has type void. | 
|  | 1773 | // | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1774 | // DR599 amends "pointer type" to "pointer to object type" in both cases. | 
|  | 1775 |  | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1776 | ExprResult Ex = Owned(ExE); | 
| Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1777 | FunctionDecl *OperatorDelete = 0; | 
| Argyrios Kyrtzidis | 4076dac | 2010-09-13 20:15:54 +0000 | [diff] [blame] | 1778 | bool ArrayFormAsWritten = ArrayForm; | 
| John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1779 | bool UsualArrayDeleteWantsSize = false; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1780 |  | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1781 | if (!Ex.get()->isTypeDependent()) { | 
|  | 1782 | QualType Type = Ex.get()->getType(); | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1783 |  | 
| Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1784 | if (const RecordType *Record = Type->getAs<RecordType>()) { | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1785 | if (RequireCompleteType(StartLoc, Type, | 
| Douglas Gregor | 254a942 | 2010-07-29 14:44:35 +0000 | [diff] [blame] | 1786 | PDiag(diag::err_delete_incomplete_class_type))) | 
|  | 1787 | return ExprError(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1788 |  | 
| Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1789 | SmallVector<CXXConversionDecl*, 4> ObjectPtrConversions; | 
| John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1790 |  | 
| Fariborz Jahanian | 5346278 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 1791 | CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl()); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1792 | const UnresolvedSetImpl *Conversions = RD->getVisibleConversionFunctions(); | 
| John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 1793 | for (UnresolvedSetImpl::iterator I = Conversions->begin(), | 
| John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1794 | E = Conversions->end(); I != E; ++I) { | 
| John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1795 | NamedDecl *D = I.getDecl(); | 
|  | 1796 | if (isa<UsingShadowDecl>(D)) | 
|  | 1797 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); | 
|  | 1798 |  | 
| Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1799 | // Skip over templated conversion functions; they aren't considered. | 
| John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1800 | if (isa<FunctionTemplateDecl>(D)) | 
| Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1801 | continue; | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1802 |  | 
| John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1803 | CXXConversionDecl *Conv = cast<CXXConversionDecl>(D); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1804 |  | 
| Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1805 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); | 
|  | 1806 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) | 
| Eli Friedman | 1357869 | 2010-08-05 02:49:48 +0000 | [diff] [blame] | 1807 | if (ConvPtrType->getPointeeType()->isIncompleteOrObjectType()) | 
| Fariborz Jahanian | 8b915e7 | 2009-09-15 22:15:23 +0000 | [diff] [blame] | 1808 | ObjectPtrConversions.push_back(Conv); | 
| Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1809 | } | 
| Fariborz Jahanian | 8b915e7 | 2009-09-15 22:15:23 +0000 | [diff] [blame] | 1810 | if (ObjectPtrConversions.size() == 1) { | 
|  | 1811 | // We have a single conversion to a pointer-to-object type. Perform | 
|  | 1812 | // that conversion. | 
| John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1813 | // TODO: don't redo the conversion calculation. | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1814 | ExprResult Res = | 
|  | 1815 | PerformImplicitConversion(Ex.get(), | 
| John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1816 | ObjectPtrConversions.front()->getConversionType(), | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1817 | AA_Converting); | 
|  | 1818 | if (Res.isUsable()) { | 
|  | 1819 | Ex = move(Res); | 
|  | 1820 | Type = Ex.get()->getType(); | 
| Fariborz Jahanian | 8b915e7 | 2009-09-15 22:15:23 +0000 | [diff] [blame] | 1821 | } | 
|  | 1822 | } | 
|  | 1823 | else if (ObjectPtrConversions.size() > 1) { | 
|  | 1824 | Diag(StartLoc, diag::err_ambiguous_delete_operand) | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1825 | << Type << Ex.get()->getSourceRange(); | 
| John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1826 | for (unsigned i= 0; i < ObjectPtrConversions.size(); i++) | 
|  | 1827 | NoteOverloadCandidate(ObjectPtrConversions[i]); | 
| Fariborz Jahanian | 8b915e7 | 2009-09-15 22:15:23 +0000 | [diff] [blame] | 1828 | return ExprError(); | 
| Douglas Gregor | 9cd9f3f | 2009-09-09 23:39:55 +0000 | [diff] [blame] | 1829 | } | 
| Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1830 | } | 
|  | 1831 |  | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1832 | if (!Type->isPointerType()) | 
|  | 1833 | return ExprError(Diag(StartLoc, diag::err_delete_operand) | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1834 | << Type << Ex.get()->getSourceRange()); | 
| Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1835 |  | 
| Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1836 | QualType Pointee = Type->getAs<PointerType>()->getPointeeType(); | 
| Eli Friedman | e52c914 | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 1837 | QualType PointeeElem = Context.getBaseElementType(Pointee); | 
|  | 1838 |  | 
|  | 1839 | if (unsigned AddressSpace = Pointee.getAddressSpace()) | 
|  | 1840 | return Diag(Ex.get()->getLocStart(), | 
|  | 1841 | diag::err_address_space_qualified_delete) | 
|  | 1842 | << Pointee.getUnqualifiedType() << AddressSpace; | 
|  | 1843 |  | 
|  | 1844 | CXXRecordDecl *PointeeRD = 0; | 
| Douglas Gregor | 94a6157 | 2010-05-24 17:01:56 +0000 | [diff] [blame] | 1845 | if (Pointee->isVoidType() && !isSFINAEContext()) { | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1846 | // 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] | 1847 | // effectively bans deletion of "void*". However, most compilers support | 
|  | 1848 | // this, so we treat it as a warning unless we're in a SFINAE context. | 
|  | 1849 | Diag(StartLoc, diag::ext_delete_void_ptr_operand) | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1850 | << Type << Ex.get()->getSourceRange(); | 
| Eli Friedman | e52c914 | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 1851 | } else if (Pointee->isFunctionType() || Pointee->isVoidType()) { | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1852 | return ExprError(Diag(StartLoc, diag::err_delete_operand) | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1853 | << Type << Ex.get()->getSourceRange()); | 
| Eli Friedman | e52c914 | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 1854 | } else if (!Pointee->isDependentType()) { | 
|  | 1855 | if (!RequireCompleteType(StartLoc, Pointee, | 
|  | 1856 | PDiag(diag::warn_delete_incomplete) | 
|  | 1857 | << Ex.get()->getSourceRange())) { | 
|  | 1858 | if (const RecordType *RT = PointeeElem->getAs<RecordType>()) | 
|  | 1859 | PointeeRD = cast<CXXRecordDecl>(RT->getDecl()); | 
|  | 1860 | } | 
|  | 1861 | } | 
|  | 1862 |  | 
| Douglas Gregor | 1070c9f | 2009-09-29 21:38:53 +0000 | [diff] [blame] | 1863 | // C++ [expr.delete]p2: | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1864 | //   [Note: a pointer to a const type can be the operand of a | 
|  | 1865 | //   delete-expression; it is not necessary to cast away the constness | 
|  | 1866 | //   (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] | 1867 | //   of the delete-expression. ] | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1868 | if (!Context.hasSameType(Ex.get()->getType(), Context.VoidPtrTy)) | 
|  | 1869 | Ex = Owned(ImplicitCastExpr::Create(Context, Context.VoidPtrTy, CK_NoOp, | 
|  | 1870 | Ex.take(), 0, VK_RValue)); | 
| Argyrios Kyrtzidis | 4076dac | 2010-09-13 20:15:54 +0000 | [diff] [blame] | 1871 |  | 
|  | 1872 | if (Pointee->isArrayType() && !ArrayForm) { | 
|  | 1873 | Diag(StartLoc, diag::warn_delete_array_type) | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1874 | << Type << Ex.get()->getSourceRange() | 
| Argyrios Kyrtzidis | 4076dac | 2010-09-13 20:15:54 +0000 | [diff] [blame] | 1875 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(StartLoc), "[]"); | 
|  | 1876 | ArrayForm = true; | 
|  | 1877 | } | 
|  | 1878 |  | 
| Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1879 | DeclarationName DeleteName = Context.DeclarationNames.getCXXOperatorName( | 
|  | 1880 | ArrayForm ? OO_Array_Delete : OO_Delete); | 
|  | 1881 |  | 
| Eli Friedman | e52c914 | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 1882 | if (PointeeRD) { | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1883 | if (!UseGlobal && | 
| Eli Friedman | e52c914 | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 1884 | FindDeallocationFunction(StartLoc, PointeeRD, DeleteName, | 
|  | 1885 | OperatorDelete)) | 
| Anders Carlsson | 0ba63ea | 2009-11-14 03:17:38 +0000 | [diff] [blame] | 1886 | return ExprError(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1887 |  | 
| John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1888 | // If we're allocating an array of records, check whether the | 
|  | 1889 | // usual operator delete[] has a size_t parameter. | 
|  | 1890 | if (ArrayForm) { | 
|  | 1891 | // If the user specifically asked to use the global allocator, | 
|  | 1892 | // we'll need to do the lookup into the class. | 
|  | 1893 | if (UseGlobal) | 
|  | 1894 | UsualArrayDeleteWantsSize = | 
|  | 1895 | doesUsualArrayDeleteWantSize(*this, StartLoc, PointeeElem); | 
|  | 1896 |  | 
|  | 1897 | // Otherwise, the usual operator delete[] should be the | 
|  | 1898 | // function we just found. | 
|  | 1899 | else if (isa<CXXMethodDecl>(OperatorDelete)) | 
|  | 1900 | UsualArrayDeleteWantsSize = (OperatorDelete->getNumParams() == 2); | 
|  | 1901 | } | 
|  | 1902 |  | 
| Eli Friedman | e52c914 | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 1903 | if (!PointeeRD->hasTrivialDestructor()) | 
|  | 1904 | if (CXXDestructorDecl *Dtor = LookupDestructor(PointeeRD)) { | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1905 | MarkDeclarationReferenced(StartLoc, | 
| Fariborz Jahanian | 34374e6 | 2009-09-03 23:18:17 +0000 | [diff] [blame] | 1906 | const_cast<CXXDestructorDecl*>(Dtor)); | 
| Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 1907 | DiagnoseUseOfDecl(Dtor, StartLoc); | 
|  | 1908 | } | 
| Argyrios Kyrtzidis | 6f0074a | 2011-05-24 19:53:26 +0000 | [diff] [blame] | 1909 |  | 
|  | 1910 | // C++ [expr.delete]p3: | 
|  | 1911 | //   In the first alternative (delete object), if the static type of the | 
|  | 1912 | //   object to be deleted is different from its dynamic type, the static | 
|  | 1913 | //   type shall be a base class of the dynamic type of the object to be | 
|  | 1914 | //   deleted and the static type shall have a virtual destructor or the | 
|  | 1915 | //   behavior is undefined. | 
|  | 1916 | // | 
|  | 1917 | // Note: a final class cannot be derived from, no issue there | 
| Eli Friedman | ef8c79c | 2011-07-26 23:27:24 +0000 | [diff] [blame] | 1918 | if (PointeeRD->isPolymorphic() && !PointeeRD->hasAttr<FinalAttr>()) { | 
| Eli Friedman | e52c914 | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 1919 | CXXDestructorDecl *dtor = PointeeRD->getDestructor(); | 
| Eli Friedman | ef8c79c | 2011-07-26 23:27:24 +0000 | [diff] [blame] | 1920 | if (dtor && !dtor->isVirtual()) { | 
|  | 1921 | if (PointeeRD->isAbstract()) { | 
|  | 1922 | // If the class is abstract, we warn by default, because we're | 
|  | 1923 | // sure the code has undefined behavior. | 
|  | 1924 | Diag(StartLoc, diag::warn_delete_abstract_non_virtual_dtor) | 
|  | 1925 | << PointeeElem; | 
|  | 1926 | } else if (!ArrayForm) { | 
|  | 1927 | // Otherwise, if this is not an array delete, it's a bit suspect, | 
|  | 1928 | // but not necessarily wrong. | 
|  | 1929 | Diag(StartLoc, diag::warn_delete_non_virtual_dtor) << PointeeElem; | 
|  | 1930 | } | 
|  | 1931 | } | 
| Argyrios Kyrtzidis | 6f0074a | 2011-05-24 19:53:26 +0000 | [diff] [blame] | 1932 | } | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1933 |  | 
|  | 1934 | } else if (getLangOptions().ObjCAutoRefCount && | 
|  | 1935 | PointeeElem->isObjCLifetimeType() && | 
|  | 1936 | (PointeeElem.getObjCLifetime() == Qualifiers::OCL_Strong || | 
|  | 1937 | PointeeElem.getObjCLifetime() == Qualifiers::OCL_Weak) && | 
|  | 1938 | ArrayForm) { | 
|  | 1939 | Diag(StartLoc, diag::warn_err_new_delete_object_array) | 
|  | 1940 | << 1 << PointeeElem; | 
| Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1941 | } | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1942 |  | 
| Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1943 | if (!OperatorDelete) { | 
| Anders Carlsson | 78f7455 | 2009-11-15 18:45:20 +0000 | [diff] [blame] | 1944 | // Look for a global declaration. | 
| Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1945 | DeclareGlobalNewDelete(); | 
|  | 1946 | DeclContext *TUDecl = Context.getTranslationUnitDecl(); | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1947 | Expr *Arg = Ex.get(); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1948 | if (FindAllocationOverload(StartLoc, SourceRange(), DeleteName, | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1949 | &Arg, 1, TUDecl, /*AllowMissing=*/false, | 
| Anders Carlsson | d67c4c3 | 2009-08-16 20:29:29 +0000 | [diff] [blame] | 1950 | OperatorDelete)) | 
|  | 1951 | return ExprError(); | 
|  | 1952 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1953 |  | 
| John McCall | 9c82afc | 2010-04-20 02:18:25 +0000 | [diff] [blame] | 1954 | MarkDeclarationReferenced(StartLoc, OperatorDelete); | 
| John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1955 |  | 
| Douglas Gregor | d880f52 | 2011-02-01 15:50:11 +0000 | [diff] [blame] | 1956 | // Check access and ambiguity of operator delete and destructor. | 
| Eli Friedman | e52c914 | 2011-07-26 22:25:31 +0000 | [diff] [blame] | 1957 | if (PointeeRD) { | 
|  | 1958 | if (CXXDestructorDecl *Dtor = LookupDestructor(PointeeRD)) { | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1959 | CheckDestructorAccess(Ex.get()->getExprLoc(), Dtor, | 
| Douglas Gregor | d880f52 | 2011-02-01 15:50:11 +0000 | [diff] [blame] | 1960 | PDiag(diag::err_access_dtor) << PointeeElem); | 
|  | 1961 | } | 
|  | 1962 | } | 
|  | 1963 |  | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1964 | } | 
|  | 1965 |  | 
| Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 1966 | return Owned(new (Context) CXXDeleteExpr(Context.VoidTy, UseGlobal, ArrayForm, | 
| John McCall | 6ec278d | 2011-01-27 09:37:56 +0000 | [diff] [blame] | 1967 | ArrayFormAsWritten, | 
|  | 1968 | UsualArrayDeleteWantsSize, | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1969 | OperatorDelete, Ex.take(), StartLoc)); | 
| Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1970 | } | 
|  | 1971 |  | 
| Douglas Gregor | 8cfe5a7 | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 1972 | /// \brief Check the use of the given variable as a C++ condition in an if, | 
|  | 1973 | /// while, do-while, or switch statement. | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1974 | ExprResult Sema::CheckConditionVariable(VarDecl *ConditionVar, | 
| John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1975 | SourceLocation StmtLoc, | 
|  | 1976 | bool ConvertToBoolean) { | 
| Douglas Gregor | 8cfe5a7 | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 1977 | QualType T = ConditionVar->getType(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1978 |  | 
| Douglas Gregor | 8cfe5a7 | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 1979 | // C++ [stmt.select]p2: | 
|  | 1980 | //   The declarator shall not specify a function or an array. | 
|  | 1981 | if (T->isFunctionType()) | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1982 | return ExprError(Diag(ConditionVar->getLocation(), | 
| Douglas Gregor | 8cfe5a7 | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 1983 | diag::err_invalid_use_of_function_type) | 
|  | 1984 | << ConditionVar->getSourceRange()); | 
|  | 1985 | else if (T->isArrayType()) | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1986 | return ExprError(Diag(ConditionVar->getLocation(), | 
| Douglas Gregor | 8cfe5a7 | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 1987 | diag::err_invalid_use_of_array_type) | 
|  | 1988 | << ConditionVar->getSourceRange()); | 
| Douglas Gregor | a7605db | 2009-11-24 16:07:02 +0000 | [diff] [blame] | 1989 |  | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1990 | ExprResult Condition = | 
|  | 1991 | Owned(DeclRefExpr::Create(Context, NestedNameSpecifierLoc(), | 
| Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 1992 | ConditionVar, | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1993 | ConditionVar->getLocation(), | 
| John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1994 | ConditionVar->getType().getNonReferenceType(), | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1995 | VK_LValue)); | 
|  | 1996 | if (ConvertToBoolean) { | 
|  | 1997 | Condition = CheckBooleanCondition(Condition.take(), StmtLoc); | 
|  | 1998 | if (Condition.isInvalid()) | 
|  | 1999 | return ExprError(); | 
|  | 2000 | } | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2001 |  | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2002 | return move(Condition); | 
| Douglas Gregor | 8cfe5a7 | 2009-11-23 23:44:04 +0000 | [diff] [blame] | 2003 | } | 
|  | 2004 |  | 
| Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 2005 | /// CheckCXXBooleanCondition - Returns true if a conversion to bool is invalid. | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2006 | ExprResult Sema::CheckCXXBooleanCondition(Expr *CondExpr) { | 
| Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 2007 | // C++ 6.4p4: | 
|  | 2008 | // The value of a condition that is an initialized declaration in a statement | 
|  | 2009 | // other than a switch statement is the value of the declared variable | 
|  | 2010 | // implicitly converted to type bool. If that conversion is ill-formed, the | 
|  | 2011 | // program is ill-formed. | 
|  | 2012 | // The value of a condition that is an expression is the value of the | 
|  | 2013 | // expression, implicitly converted to bool. | 
|  | 2014 | // | 
| Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2015 | return PerformContextuallyConvertToBool(CondExpr); | 
| Argyrios Kyrtzidis | 5921093 | 2008-09-10 02:17:11 +0000 | [diff] [blame] | 2016 | } | 
| Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 2017 |  | 
|  | 2018 | /// Helper function to determine whether this is the (deprecated) C++ | 
|  | 2019 | /// conversion from a string literal to a pointer to non-const char or | 
|  | 2020 | /// non-const wchar_t (for narrow and wide string literals, | 
|  | 2021 | /// respectively). | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2022 | bool | 
| Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 2023 | Sema::IsStringLiteralToNonConstPointerConversion(Expr *From, QualType ToType) { | 
|  | 2024 | // Look inside the implicit cast, if it exists. | 
|  | 2025 | if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(From)) | 
|  | 2026 | From = Cast->getSubExpr(); | 
|  | 2027 |  | 
|  | 2028 | // A string literal (2.13.4) that is not a wide string literal can | 
|  | 2029 | // be converted to an rvalue of type "pointer to char"; a wide | 
|  | 2030 | // string literal can be converted to an rvalue of type "pointer | 
|  | 2031 | // to wchar_t" (C++ 4.2p2). | 
| Douglas Gregor | 1984eb9 | 2010-06-22 23:47:37 +0000 | [diff] [blame] | 2032 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(From->IgnoreParens())) | 
| Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2033 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2034 | if (const BuiltinType *ToPointeeType | 
| John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2035 | = ToPtrType->getPointeeType()->getAs<BuiltinType>()) { | 
| Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 2036 | // This conversion is considered only when there is an | 
|  | 2037 | // explicit appropriate pointer target type (C++ 4.2p2). | 
| Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2038 | if (!ToPtrType->getPointeeType().hasQualifiers()) { | 
|  | 2039 | switch (StrLit->getKind()) { | 
|  | 2040 | case StringLiteral::UTF8: | 
|  | 2041 | case StringLiteral::UTF16: | 
|  | 2042 | case StringLiteral::UTF32: | 
|  | 2043 | // We don't allow UTF literals to be implicitly converted | 
|  | 2044 | break; | 
|  | 2045 | case StringLiteral::Ascii: | 
|  | 2046 | return (ToPointeeType->getKind() == BuiltinType::Char_U || | 
|  | 2047 | ToPointeeType->getKind() == BuiltinType::Char_S); | 
|  | 2048 | case StringLiteral::Wide: | 
|  | 2049 | return ToPointeeType->isWideCharType(); | 
|  | 2050 | } | 
|  | 2051 | } | 
| Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 2052 | } | 
|  | 2053 |  | 
|  | 2054 | return false; | 
|  | 2055 | } | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2056 |  | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2057 | static ExprResult BuildCXXCastArgument(Sema &S, | 
| John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2058 | SourceLocation CastLoc, | 
|  | 2059 | QualType Ty, | 
|  | 2060 | CastKind Kind, | 
|  | 2061 | CXXMethodDecl *Method, | 
| John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 2062 | DeclAccessPair FoundDecl, | 
| Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 2063 | bool HadMultipleCandidates, | 
| John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2064 | Expr *From) { | 
| Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2065 | switch (Kind) { | 
| David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2066 | default: llvm_unreachable("Unhandled cast kind!"); | 
| John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2067 | case CK_ConstructorConversion: { | 
| Douglas Gregor | 13e1bca | 2011-10-10 22:41:00 +0000 | [diff] [blame] | 2068 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Method); | 
| John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 2069 | ASTOwningVector<Expr*> ConstructorArgs(S); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2070 |  | 
| Douglas Gregor | 13e1bca | 2011-10-10 22:41:00 +0000 | [diff] [blame] | 2071 | if (S.CompleteConstructorCall(Constructor, | 
| John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2072 | MultiExprArg(&From, 1), | 
| Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2073 | CastLoc, ConstructorArgs)) | 
| John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2074 | return ExprError(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2075 |  | 
| Douglas Gregor | 13e1bca | 2011-10-10 22:41:00 +0000 | [diff] [blame] | 2076 | S.CheckConstructorAccess(CastLoc, Constructor, Constructor->getAccess(), | 
|  | 2077 | S.PDiag(diag::err_access_ctor)); | 
|  | 2078 |  | 
|  | 2079 | ExprResult Result | 
|  | 2080 | = S.BuildCXXConstructExpr(CastLoc, Ty, cast<CXXConstructorDecl>(Method), | 
|  | 2081 | move_arg(ConstructorArgs), | 
|  | 2082 | HadMultipleCandidates, /*ZeroInit*/ false, | 
|  | 2083 | CXXConstructExpr::CK_Complete, SourceRange()); | 
| Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2084 | if (Result.isInvalid()) | 
| John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2085 | return ExprError(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2086 |  | 
| Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2087 | return S.MaybeBindToTemporary(Result.takeAs<Expr>()); | 
|  | 2088 | } | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2089 |  | 
| John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2090 | case CK_UserDefinedConversion: { | 
| Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2091 | assert(!From->getType()->isPointerType() && "Arg can't have pointer type!"); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2092 |  | 
| Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2093 | // Create an implicit call expr that calls it. | 
| Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 2094 | ExprResult Result = S.BuildCXXMemberCallExpr(From, FoundDecl, Method, | 
|  | 2095 | HadMultipleCandidates); | 
| Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 2096 | if (Result.isInvalid()) | 
|  | 2097 | return ExprError(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2098 |  | 
| John McCall | ca82a82 | 2011-09-21 08:36:56 +0000 | [diff] [blame] | 2099 | S.CheckMemberOperatorAccess(CastLoc, From, /*arg*/ 0, FoundDecl); | 
|  | 2100 |  | 
| Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 2101 | return S.MaybeBindToTemporary(Result.get()); | 
| Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2102 | } | 
|  | 2103 | } | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2104 | } | 
| Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2105 |  | 
| Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2106 | /// PerformImplicitConversion - Perform an implicit conversion of the | 
|  | 2107 | /// expression From to the type ToType using the pre-computed implicit | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2108 | /// conversion sequence ICS. Returns the converted | 
| Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 2109 | /// expression. Action is the kind of conversion we're performing, | 
| Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2110 | /// used in the error message. | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2111 | ExprResult | 
|  | 2112 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, | 
| Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2113 | const ImplicitConversionSequence &ICS, | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2114 | AssignmentAction Action, | 
|  | 2115 | CheckedConversionKind CCK) { | 
| John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2116 | switch (ICS.getKind()) { | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2117 | case ImplicitConversionSequence::StandardConversion: { | 
|  | 2118 | ExprResult Res = PerformImplicitConversion(From, ToType, ICS.Standard, | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2119 | Action, CCK); | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2120 | if (Res.isInvalid()) | 
|  | 2121 | return ExprError(); | 
|  | 2122 | From = Res.take(); | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2123 | break; | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2124 | } | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2125 |  | 
| Anders Carlsson | f6c213a | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 2126 | case ImplicitConversionSequence::UserDefinedConversion: { | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2127 |  | 
| Fariborz Jahanian | 7fe5d72 | 2009-08-28 22:04:50 +0000 | [diff] [blame] | 2128 | FunctionDecl *FD = ICS.UserDefined.ConversionFunction; | 
| John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2129 | CastKind CastKind; | 
| Anders Carlsson | f6c213a | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 2130 | QualType BeforeToType; | 
|  | 2131 | if (const CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(FD)) { | 
| John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2132 | CastKind = CK_UserDefinedConversion; | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2133 |  | 
| Anders Carlsson | f6c213a | 2009-09-15 06:28:28 +0000 | [diff] [blame] | 2134 | // If the user-defined conversion is specified by a conversion function, | 
|  | 2135 | // the initial standard conversion sequence converts the source type to | 
|  | 2136 | // the implicit object parameter of the conversion function. | 
|  | 2137 | BeforeToType = Context.getTagDeclType(Conv->getParent()); | 
| John McCall | 9ec9445 | 2010-12-04 09:57:16 +0000 | [diff] [blame] | 2138 | } else { | 
|  | 2139 | const CXXConstructorDecl *Ctor = cast<CXXConstructorDecl>(FD); | 
| John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2140 | CastKind = CK_ConstructorConversion; | 
| Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 2141 | // Do no conversion if dealing with ... for the first conversion. | 
| Douglas Gregor | e44201a | 2009-11-20 02:31:03 +0000 | [diff] [blame] | 2142 | if (!ICS.UserDefined.EllipsisConversion) { | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2143 | // If the user-defined conversion is specified by a constructor, the | 
| Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 2144 | // initial standard conversion sequence converts the source type to the | 
|  | 2145 | // type required by the argument of the constructor | 
| Douglas Gregor | e44201a | 2009-11-20 02:31:03 +0000 | [diff] [blame] | 2146 | BeforeToType = Ctor->getParamDecl(0)->getType().getNonReferenceType(); | 
|  | 2147 | } | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2148 | } | 
| Douglas Gregor | a3998bd | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 2149 | // Watch out for elipsis conversion. | 
| Fariborz Jahanian | 4c0cea2 | 2009-11-06 00:55:14 +0000 | [diff] [blame] | 2150 | if (!ICS.UserDefined.EllipsisConversion) { | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2151 | ExprResult Res = | 
|  | 2152 | PerformImplicitConversion(From, BeforeToType, | 
|  | 2153 | ICS.UserDefined.Before, AA_Converting, | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2154 | CCK); | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2155 | if (Res.isInvalid()) | 
|  | 2156 | return ExprError(); | 
|  | 2157 | From = Res.take(); | 
| Fariborz Jahanian | 966256a | 2009-11-06 00:23:08 +0000 | [diff] [blame] | 2158 | } | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2159 |  | 
|  | 2160 | ExprResult CastArg | 
| Douglas Gregor | ba70ab6 | 2010-04-16 22:17:36 +0000 | [diff] [blame] | 2161 | = BuildCXXCastArgument(*this, | 
|  | 2162 | From->getLocStart(), | 
| Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 2163 | ToType.getNonReferenceType(), | 
| Douglas Gregor | 83eecbe | 2011-01-20 01:32:05 +0000 | [diff] [blame] | 2164 | CastKind, cast<CXXMethodDecl>(FD), | 
|  | 2165 | ICS.UserDefined.FoundConversionFunction, | 
| Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 2166 | ICS.UserDefined.HadMultipleCandidates, | 
| John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2167 | From); | 
| Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 2168 |  | 
|  | 2169 | if (CastArg.isInvalid()) | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2170 | return ExprError(); | 
| Eli Friedman | d888962 | 2009-11-27 04:41:50 +0000 | [diff] [blame] | 2171 |  | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2172 | From = CastArg.take(); | 
| Eli Friedman | d888962 | 2009-11-27 04:41:50 +0000 | [diff] [blame] | 2173 |  | 
| Eli Friedman | d888962 | 2009-11-27 04:41:50 +0000 | [diff] [blame] | 2174 | return PerformImplicitConversion(From, ToType, ICS.UserDefined.After, | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2175 | AA_Converting, CCK); | 
| Fariborz Jahanian | 93034ca | 2009-10-16 19:20:59 +0000 | [diff] [blame] | 2176 | } | 
| John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2177 |  | 
|  | 2178 | case ImplicitConversionSequence::AmbiguousConversion: | 
| John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 2179 | ICS.DiagnoseAmbiguousConversion(*this, From->getExprLoc(), | 
| John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 2180 | PDiag(diag::err_typecheck_ambiguous_condition) | 
|  | 2181 | << From->getSourceRange()); | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2182 | return ExprError(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2183 |  | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2184 | case ImplicitConversionSequence::EllipsisConversion: | 
| David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2185 | llvm_unreachable("Cannot perform an ellipsis conversion"); | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2186 |  | 
|  | 2187 | case ImplicitConversionSequence::BadConversion: | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2188 | return ExprError(); | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2189 | } | 
|  | 2190 |  | 
|  | 2191 | // Everything went well. | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2192 | return Owned(From); | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2193 | } | 
|  | 2194 |  | 
|  | 2195 | /// PerformImplicitConversion - Perform an implicit conversion of the | 
|  | 2196 | /// expression From to the type ToType by following the standard | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2197 | /// conversion sequence SCS. Returns the converted | 
| Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 2198 | /// expression. Flavor is the context in which we're performing this | 
|  | 2199 | /// conversion, for use in error messages. | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2200 | ExprResult | 
|  | 2201 | Sema::PerformImplicitConversion(Expr *From, QualType ToType, | 
| Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 2202 | const StandardConversionSequence& SCS, | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2203 | AssignmentAction Action, | 
|  | 2204 | CheckedConversionKind CCK) { | 
|  | 2205 | bool CStyle = (CCK == CCK_CStyleCast || CCK == CCK_FunctionalCast); | 
|  | 2206 |  | 
| Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2207 | // Overall FIXME: we are recomputing too many types here and doing far too | 
|  | 2208 | // much extra work. What this means is that we need to keep track of more | 
|  | 2209 | // information that is computed when we try the implicit conversion initially, | 
|  | 2210 | // so that we don't need to recompute anything here. | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2211 | QualType FromType = From->getType(); | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2212 |  | 
| Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2213 | if (SCS.CopyConstructor) { | 
| Anders Carlsson | 7c3e8a1 | 2009-05-19 04:45:15 +0000 | [diff] [blame] | 2214 | // FIXME: When can ToType be a reference type? | 
|  | 2215 | assert(!ToType->isReferenceType()); | 
| Fariborz Jahanian | b3c4774 | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 2216 | if (SCS.Second == ICK_Derived_To_Base) { | 
| John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 2217 | ASTOwningVector<Expr*> ConstructorArgs(*this); | 
| Fariborz Jahanian | b3c4774 | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 2218 | if (CompleteConstructorCall(cast<CXXConstructorDecl>(SCS.CopyConstructor), | 
| John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 2219 | MultiExprArg(*this, &From, 1), | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2220 | /*FIXME:ConstructLoc*/SourceLocation(), | 
| Fariborz Jahanian | b3c4774 | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 2221 | ConstructorArgs)) | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2222 | return ExprError(); | 
|  | 2223 | return BuildCXXConstructExpr(/*FIXME:ConstructLoc*/SourceLocation(), | 
|  | 2224 | ToType, SCS.CopyConstructor, | 
|  | 2225 | move_arg(ConstructorArgs), | 
| Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 2226 | /*HadMultipleCandidates*/ false, | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2227 | /*ZeroInit*/ false, | 
|  | 2228 | CXXConstructExpr::CK_Complete, | 
|  | 2229 | SourceRange()); | 
| Fariborz Jahanian | b3c4774 | 2009-09-25 18:59:21 +0000 | [diff] [blame] | 2230 | } | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2231 | return BuildCXXConstructExpr(/*FIXME:ConstructLoc*/SourceLocation(), | 
|  | 2232 | ToType, SCS.CopyConstructor, | 
|  | 2233 | MultiExprArg(*this, &From, 1), | 
| Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 2234 | /*HadMultipleCandidates*/ false, | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2235 | /*ZeroInit*/ false, | 
|  | 2236 | CXXConstructExpr::CK_Complete, | 
|  | 2237 | SourceRange()); | 
| Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2238 | } | 
|  | 2239 |  | 
| Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 2240 | // Resolve overloaded function references. | 
|  | 2241 | if (Context.hasSameType(FromType, Context.OverloadTy)) { | 
|  | 2242 | DeclAccessPair Found; | 
|  | 2243 | FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(From, ToType, | 
|  | 2244 | true, Found); | 
|  | 2245 | if (!Fn) | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2246 | return ExprError(); | 
| Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 2247 |  | 
|  | 2248 | if (DiagnoseUseOfDecl(Fn, From->getSourceRange().getBegin())) | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2249 | return ExprError(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2250 |  | 
| Douglas Gregor | ad4e02f | 2010-04-29 18:24:40 +0000 | [diff] [blame] | 2251 | From = FixOverloadedFunctionReference(From, Found, Fn); | 
|  | 2252 | FromType = From->getType(); | 
|  | 2253 | } | 
|  | 2254 |  | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2255 | // Perform the first implicit conversion. | 
|  | 2256 | switch (SCS.First) { | 
|  | 2257 | case ICK_Identity: | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2258 | // Nothing to do. | 
|  | 2259 | break; | 
|  | 2260 |  | 
| John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2261 | case ICK_Lvalue_To_Rvalue: | 
|  | 2262 | // Should this get its own ICK? | 
|  | 2263 | if (From->getObjectKind() == OK_ObjCProperty) { | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2264 | ExprResult FromRes = ConvertPropertyForRValue(From); | 
|  | 2265 | if (FromRes.isInvalid()) | 
|  | 2266 | return ExprError(); | 
|  | 2267 | From = FromRes.take(); | 
| John McCall | 241d558 | 2010-12-07 22:54:16 +0000 | [diff] [blame] | 2268 | if (!From->isGLValue()) break; | 
| John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2269 | } | 
|  | 2270 |  | 
|  | 2271 | FromType = FromType.getUnqualifiedType(); | 
|  | 2272 | From = ImplicitCastExpr::Create(Context, FromType, CK_LValueToRValue, | 
|  | 2273 | From, 0, VK_RValue); | 
|  | 2274 | break; | 
|  | 2275 |  | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2276 | case ICK_Array_To_Pointer: | 
| Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2277 | FromType = Context.getArrayDecayedType(FromType); | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2278 | From = ImpCastExprToType(From, FromType, CK_ArrayToPointerDecay, | 
|  | 2279 | VK_RValue, /*BasePath=*/0, CCK).take(); | 
| Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2280 | break; | 
|  | 2281 |  | 
|  | 2282 | case ICK_Function_To_Pointer: | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2283 | FromType = Context.getPointerType(FromType); | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2284 | From = ImpCastExprToType(From, FromType, CK_FunctionToPointerDecay, | 
|  | 2285 | VK_RValue, /*BasePath=*/0, CCK).take(); | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2286 | break; | 
|  | 2287 |  | 
|  | 2288 | default: | 
| David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2289 | llvm_unreachable("Improper first standard conversion"); | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2290 | } | 
|  | 2291 |  | 
|  | 2292 | // Perform the second implicit conversion | 
|  | 2293 | switch (SCS.Second) { | 
|  | 2294 | case ICK_Identity: | 
| Sebastian Redl | 2c7588f | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 2295 | // If both sides are functions (or pointers/references to them), there could | 
|  | 2296 | // be incompatible exception declarations. | 
|  | 2297 | if (CheckExceptionSpecCompatibility(From, ToType)) | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2298 | return ExprError(); | 
| Sebastian Redl | 2c7588f | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 2299 | // Nothing else to do. | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2300 | break; | 
|  | 2301 |  | 
| Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 2302 | case ICK_NoReturn_Adjustment: | 
|  | 2303 | // If both sides are functions (or pointers/references to them), there could | 
|  | 2304 | // be incompatible exception declarations. | 
|  | 2305 | if (CheckExceptionSpecCompatibility(From, ToType)) | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2306 | return ExprError(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2307 |  | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2308 | From = ImpCastExprToType(From, ToType, CK_NoOp, | 
|  | 2309 | VK_RValue, /*BasePath=*/0, CCK).take(); | 
| Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 2310 | break; | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2311 |  | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2312 | case ICK_Integral_Promotion: | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2313 | case ICK_Integral_Conversion: | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2314 | From = ImpCastExprToType(From, ToType, CK_IntegralCast, | 
|  | 2315 | VK_RValue, /*BasePath=*/0, CCK).take(); | 
| Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2316 | break; | 
|  | 2317 |  | 
|  | 2318 | case ICK_Floating_Promotion: | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2319 | case ICK_Floating_Conversion: | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2320 | From = ImpCastExprToType(From, ToType, CK_FloatingCast, | 
|  | 2321 | VK_RValue, /*BasePath=*/0, CCK).take(); | 
| Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2322 | break; | 
|  | 2323 |  | 
|  | 2324 | case ICK_Complex_Promotion: | 
| John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2325 | case ICK_Complex_Conversion: { | 
|  | 2326 | QualType FromEl = From->getType()->getAs<ComplexType>()->getElementType(); | 
|  | 2327 | QualType ToEl = ToType->getAs<ComplexType>()->getElementType(); | 
|  | 2328 | CastKind CK; | 
|  | 2329 | if (FromEl->isRealFloatingType()) { | 
|  | 2330 | if (ToEl->isRealFloatingType()) | 
|  | 2331 | CK = CK_FloatingComplexCast; | 
|  | 2332 | else | 
|  | 2333 | CK = CK_FloatingComplexToIntegralComplex; | 
|  | 2334 | } else if (ToEl->isRealFloatingType()) { | 
|  | 2335 | CK = CK_IntegralComplexToFloatingComplex; | 
|  | 2336 | } else { | 
|  | 2337 | CK = CK_IntegralComplexCast; | 
|  | 2338 | } | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2339 | From = ImpCastExprToType(From, ToType, CK, | 
|  | 2340 | VK_RValue, /*BasePath=*/0, CCK).take(); | 
| Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2341 | break; | 
| John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2342 | } | 
| Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2343 |  | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2344 | case ICK_Floating_Integral: | 
| Douglas Gregor | 0c293ea | 2010-06-22 23:07:26 +0000 | [diff] [blame] | 2345 | if (ToType->isRealFloatingType()) | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2346 | From = ImpCastExprToType(From, ToType, CK_IntegralToFloating, | 
|  | 2347 | VK_RValue, /*BasePath=*/0, CCK).take(); | 
| Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2348 | else | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2349 | From = ImpCastExprToType(From, ToType, CK_FloatingToIntegral, | 
|  | 2350 | VK_RValue, /*BasePath=*/0, CCK).take(); | 
| Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2351 | break; | 
|  | 2352 |  | 
| Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2353 | case ICK_Compatible_Conversion: | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2354 | From = ImpCastExprToType(From, ToType, CK_NoOp, | 
|  | 2355 | VK_RValue, /*BasePath=*/0, CCK).take(); | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2356 | break; | 
|  | 2357 |  | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2358 | case ICK_Writeback_Conversion: | 
| Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2359 | case ICK_Pointer_Conversion: { | 
| Douglas Gregor | a3998bd | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 2360 | if (SCS.IncompatibleObjC && Action != AA_Casting) { | 
| Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 2361 | // Diagnose incompatible Objective-C conversions | 
| Douglas Gregor | 8cf0d22 | 2011-06-11 04:42:12 +0000 | [diff] [blame] | 2362 | if (Action == AA_Initializing || Action == AA_Assigning) | 
| Fariborz Jahanian | 84950c7 | 2011-03-21 19:08:42 +0000 | [diff] [blame] | 2363 | Diag(From->getSourceRange().getBegin(), | 
|  | 2364 | diag::ext_typecheck_convert_incompatible_pointer) | 
|  | 2365 | << ToType << From->getType() << Action | 
| Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 2366 | << From->getSourceRange() << 0; | 
| Fariborz Jahanian | 84950c7 | 2011-03-21 19:08:42 +0000 | [diff] [blame] | 2367 | else | 
|  | 2368 | Diag(From->getSourceRange().getBegin(), | 
|  | 2369 | diag::ext_typecheck_convert_incompatible_pointer) | 
|  | 2370 | << From->getType() << ToType << Action | 
| Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 2371 | << From->getSourceRange() << 0; | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2372 |  | 
| Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 2373 | if (From->getType()->isObjCObjectPointerType() && | 
|  | 2374 | ToType->isObjCObjectPointerType()) | 
|  | 2375 | EmitRelatedResultTypeNote(From); | 
| Fariborz Jahanian | 82007c3 | 2011-07-08 17:41:42 +0000 | [diff] [blame] | 2376 | } | 
|  | 2377 | else if (getLangOptions().ObjCAutoRefCount && | 
|  | 2378 | !CheckObjCARCUnavailableWeakConversion(ToType, | 
|  | 2379 | From->getType())) { | 
| John McCall | 7f3a6d3 | 2011-09-09 06:12:06 +0000 | [diff] [blame] | 2380 | if (Action == AA_Initializing) | 
|  | 2381 | Diag(From->getSourceRange().getBegin(), | 
|  | 2382 | diag::err_arc_weak_unavailable_assign); | 
|  | 2383 | else | 
|  | 2384 | Diag(From->getSourceRange().getBegin(), | 
|  | 2385 | diag::err_arc_convesion_of_weak_unavailable) | 
|  | 2386 | << (Action == AA_Casting) << From->getType() << ToType | 
|  | 2387 | << From->getSourceRange(); | 
|  | 2388 | } | 
| Fariborz Jahanian | 82007c3 | 2011-07-08 17:41:42 +0000 | [diff] [blame] | 2389 |  | 
| John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2390 | CastKind Kind = CK_Invalid; | 
| John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2391 | CXXCastPath BasePath; | 
| Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2392 | if (CheckPointerConversion(From, ToType, Kind, BasePath, CStyle)) | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2393 | return ExprError(); | 
| John McCall | dc05b11 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 2394 |  | 
|  | 2395 | // Make sure we extend blocks if necessary. | 
|  | 2396 | // FIXME: doing this here is really ugly. | 
|  | 2397 | if (Kind == CK_BlockPointerToObjCPointerCast) { | 
|  | 2398 | ExprResult E = From; | 
|  | 2399 | (void) PrepareCastToObjCObjectPointer(E); | 
|  | 2400 | From = E.take(); | 
|  | 2401 | } | 
|  | 2402 |  | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2403 | From = ImpCastExprToType(From, ToType, Kind, VK_RValue, &BasePath, CCK) | 
|  | 2404 | .take(); | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2405 | break; | 
| Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2406 | } | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2407 |  | 
| Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2408 | case ICK_Pointer_Member: { | 
| John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2409 | CastKind Kind = CK_Invalid; | 
| John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2410 | CXXCastPath BasePath; | 
| Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2411 | if (CheckMemberPointerConversion(From, ToType, Kind, BasePath, CStyle)) | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2412 | return ExprError(); | 
| Sebastian Redl | 2c7588f | 2009-10-10 12:04:10 +0000 | [diff] [blame] | 2413 | if (CheckExceptionSpecCompatibility(From, ToType)) | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2414 | return ExprError(); | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2415 | From = ImpCastExprToType(From, ToType, Kind, VK_RValue, &BasePath, CCK) | 
|  | 2416 | .take(); | 
| Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 2417 | break; | 
|  | 2418 | } | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2419 |  | 
| Abramo Bagnara | 737d544 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 2420 | case ICK_Boolean_Conversion: | 
| Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 2421 | // Perform half-to-boolean conversion via float. | 
|  | 2422 | if (From->getType()->isHalfType()) { | 
|  | 2423 | From = ImpCastExprToType(From, Context.FloatTy, CK_FloatingCast).take(); | 
|  | 2424 | FromType = Context.FloatTy; | 
|  | 2425 | } | 
|  | 2426 |  | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2427 | From = ImpCastExprToType(From, Context.BoolTy, | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2428 | ScalarTypeToBooleanCastKind(FromType), | 
|  | 2429 | VK_RValue, /*BasePath=*/0, CCK).take(); | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2430 | break; | 
|  | 2431 |  | 
| Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 2432 | case ICK_Derived_To_Base: { | 
| John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2433 | CXXCastPath BasePath; | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2434 | if (CheckDerivedToBaseConversion(From->getType(), | 
| Douglas Gregor | b7a86f5 | 2009-11-06 01:02:41 +0000 | [diff] [blame] | 2435 | ToType.getNonReferenceType(), | 
|  | 2436 | From->getLocStart(), | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2437 | From->getSourceRange(), | 
| Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 2438 | &BasePath, | 
| Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 2439 | CStyle)) | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2440 | return ExprError(); | 
| Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 2441 |  | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2442 | From = ImpCastExprToType(From, ToType.getNonReferenceType(), | 
| Eli Friedman | c1c0dfb | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 2443 | CK_DerivedToBase, From->getValueKind(), | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2444 | &BasePath, CCK).take(); | 
| Douglas Gregor | b7a86f5 | 2009-11-06 01:02:41 +0000 | [diff] [blame] | 2445 | break; | 
| Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 2446 | } | 
|  | 2447 |  | 
| Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 2448 | case ICK_Vector_Conversion: | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2449 | From = ImpCastExprToType(From, ToType, CK_BitCast, | 
|  | 2450 | VK_RValue, /*BasePath=*/0, CCK).take(); | 
| Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 2451 | break; | 
|  | 2452 |  | 
|  | 2453 | case ICK_Vector_Splat: | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2454 | From = ImpCastExprToType(From, ToType, CK_VectorSplat, | 
|  | 2455 | VK_RValue, /*BasePath=*/0, CCK).take(); | 
| Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 2456 | break; | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2457 |  | 
| Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 2458 | case ICK_Complex_Real: | 
| John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2459 | // Case 1.  x -> _Complex y | 
|  | 2460 | if (const ComplexType *ToComplex = ToType->getAs<ComplexType>()) { | 
|  | 2461 | QualType ElType = ToComplex->getElementType(); | 
|  | 2462 | bool isFloatingComplex = ElType->isRealFloatingType(); | 
|  | 2463 |  | 
|  | 2464 | // x -> y | 
|  | 2465 | if (Context.hasSameUnqualifiedType(ElType, From->getType())) { | 
|  | 2466 | // do nothing | 
|  | 2467 | } else if (From->getType()->isRealFloatingType()) { | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2468 | From = ImpCastExprToType(From, ElType, | 
|  | 2469 | isFloatingComplex ? CK_FloatingCast : CK_FloatingToIntegral).take(); | 
| John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2470 | } else { | 
|  | 2471 | assert(From->getType()->isIntegerType()); | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2472 | From = ImpCastExprToType(From, ElType, | 
|  | 2473 | isFloatingComplex ? CK_IntegralToFloating : CK_IntegralCast).take(); | 
| John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2474 | } | 
|  | 2475 | // y -> _Complex y | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2476 | From = ImpCastExprToType(From, ToType, | 
| John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2477 | isFloatingComplex ? CK_FloatingRealToComplex | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2478 | : CK_IntegralRealToComplex).take(); | 
| John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2479 |  | 
|  | 2480 | // Case 2.  _Complex x -> y | 
|  | 2481 | } else { | 
|  | 2482 | const ComplexType *FromComplex = From->getType()->getAs<ComplexType>(); | 
|  | 2483 | assert(FromComplex); | 
|  | 2484 |  | 
|  | 2485 | QualType ElType = FromComplex->getElementType(); | 
|  | 2486 | bool isFloatingComplex = ElType->isRealFloatingType(); | 
|  | 2487 |  | 
|  | 2488 | // _Complex x -> x | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2489 | From = ImpCastExprToType(From, ElType, | 
| John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2490 | isFloatingComplex ? CK_FloatingComplexToReal | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2491 | : CK_IntegralComplexToReal, | 
|  | 2492 | VK_RValue, /*BasePath=*/0, CCK).take(); | 
| John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2493 |  | 
|  | 2494 | // x -> y | 
|  | 2495 | if (Context.hasSameUnqualifiedType(ElType, ToType)) { | 
|  | 2496 | // do nothing | 
|  | 2497 | } else if (ToType->isRealFloatingType()) { | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2498 | From = ImpCastExprToType(From, ToType, | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2499 | isFloatingComplex ? CK_FloatingCast : CK_IntegralToFloating, | 
|  | 2500 | VK_RValue, /*BasePath=*/0, CCK).take(); | 
| John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2501 | } else { | 
|  | 2502 | assert(ToType->isIntegerType()); | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2503 | From = ImpCastExprToType(From, ToType, | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2504 | isFloatingComplex ? CK_FloatingToIntegral : CK_IntegralCast, | 
|  | 2505 | VK_RValue, /*BasePath=*/0, CCK).take(); | 
| John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 2506 | } | 
|  | 2507 | } | 
| Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 2508 | break; | 
| Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2509 |  | 
|  | 2510 | case ICK_Block_Pointer_Conversion: { | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2511 | From = ImpCastExprToType(From, ToType.getUnqualifiedType(), CK_BitCast, | 
|  | 2512 | VK_RValue, /*BasePath=*/0, CCK).take(); | 
|  | 2513 | break; | 
|  | 2514 | } | 
| Fariborz Jahanian | e3c8c64 | 2011-02-12 19:07:46 +0000 | [diff] [blame] | 2515 |  | 
| Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 2516 | case ICK_TransparentUnionConversion: { | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2517 | ExprResult FromRes = Owned(From); | 
| Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 2518 | Sema::AssignConvertType ConvTy = | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2519 | CheckTransparentUnionArgumentConstraints(ToType, FromRes); | 
|  | 2520 | if (FromRes.isInvalid()) | 
|  | 2521 | return ExprError(); | 
|  | 2522 | From = FromRes.take(); | 
| Fariborz Jahanian | d97f558 | 2011-03-23 19:50:54 +0000 | [diff] [blame] | 2523 | assert ((ConvTy == Sema::Compatible) && | 
|  | 2524 | "Improper transparent union conversion"); | 
|  | 2525 | (void)ConvTy; | 
|  | 2526 | break; | 
|  | 2527 | } | 
|  | 2528 |  | 
| Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 2529 | case ICK_Lvalue_To_Rvalue: | 
|  | 2530 | case ICK_Array_To_Pointer: | 
|  | 2531 | case ICK_Function_To_Pointer: | 
|  | 2532 | case ICK_Qualification: | 
|  | 2533 | case ICK_Num_Conversion_Kinds: | 
| David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2534 | llvm_unreachable("Improper second standard conversion"); | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2535 | } | 
|  | 2536 |  | 
|  | 2537 | switch (SCS.Third) { | 
|  | 2538 | case ICK_Identity: | 
|  | 2539 | // Nothing to do. | 
|  | 2540 | break; | 
|  | 2541 |  | 
| Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2542 | case ICK_Qualification: { | 
|  | 2543 | // The qualification keeps the category of the inner expression, unless the | 
|  | 2544 | // target type isn't a reference. | 
| John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2545 | ExprValueKind VK = ToType->isReferenceType() ? | 
| Eli Friedman | c1c0dfb | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 2546 | From->getValueKind() : VK_RValue; | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2547 | From = ImpCastExprToType(From, ToType.getNonLValueExprType(Context), | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2548 | CK_NoOp, VK, /*BasePath=*/0, CCK).take(); | 
| Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 2549 |  | 
| Douglas Gregor | 069a6da | 2011-03-14 16:13:32 +0000 | [diff] [blame] | 2550 | if (SCS.DeprecatedStringLiteralToCharPtr && | 
|  | 2551 | !getLangOptions().WritableStrings) | 
| Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 2552 | Diag(From->getLocStart(), diag::warn_deprecated_string_literal_conversion) | 
|  | 2553 | << ToType.getNonReferenceType(); | 
|  | 2554 |  | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2555 | break; | 
| Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2556 | } | 
|  | 2557 |  | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2558 | default: | 
| David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2559 | llvm_unreachable("Improper third standard conversion"); | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2560 | } | 
|  | 2561 |  | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2562 | return Owned(From); | 
| Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 2563 | } | 
|  | 2564 |  | 
| Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2565 | ExprResult Sema::ActOnUnaryTypeTrait(UnaryTypeTrait UTT, | 
| Douglas Gregor | 3d37c0a | 2010-09-09 16:14:44 +0000 | [diff] [blame] | 2566 | SourceLocation KWLoc, | 
|  | 2567 | ParsedType Ty, | 
|  | 2568 | SourceLocation RParen) { | 
|  | 2569 | TypeSourceInfo *TSInfo; | 
|  | 2570 | QualType T = GetTypeFromParser(Ty, &TSInfo); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2571 |  | 
| Douglas Gregor | 3d37c0a | 2010-09-09 16:14:44 +0000 | [diff] [blame] | 2572 | if (!TSInfo) | 
|  | 2573 | TSInfo = Context.getTrivialTypeSourceInfo(T); | 
| Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2574 | return BuildUnaryTypeTrait(UTT, KWLoc, TSInfo, RParen); | 
| Douglas Gregor | 3d37c0a | 2010-09-09 16:14:44 +0000 | [diff] [blame] | 2575 | } | 
|  | 2576 |  | 
| Chandler Carruth | ccb4ecf | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2577 | /// \brief Check the completeness of a type in a unary type trait. | 
|  | 2578 | /// | 
|  | 2579 | /// If the particular type trait requires a complete type, tries to complete | 
|  | 2580 | /// it. If completing the type fails, a diagnostic is emitted and false | 
|  | 2581 | /// returned. If completing the type succeeds or no completion was required, | 
|  | 2582 | /// returns true. | 
|  | 2583 | static bool CheckUnaryTypeTraitTypeCompleteness(Sema &S, | 
|  | 2584 | UnaryTypeTrait UTT, | 
|  | 2585 | SourceLocation Loc, | 
|  | 2586 | QualType ArgTy) { | 
|  | 2587 | // C++0x [meta.unary.prop]p3: | 
|  | 2588 | //   For all of the class templates X declared in this Clause, instantiating | 
|  | 2589 | //   that template with a template argument that is a class template | 
|  | 2590 | //   specialization may result in the implicit instantiation of the template | 
|  | 2591 | //   argument if and only if the semantics of X require that the argument | 
|  | 2592 | //   must be a complete type. | 
|  | 2593 | // We apply this rule to all the type trait expressions used to implement | 
|  | 2594 | // these class templates. We also try to follow any GCC documented behavior | 
|  | 2595 | // in these expressions to ensure portability of standard libraries. | 
|  | 2596 | switch (UTT) { | 
| Chandler Carruth | ccb4ecf | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2597 | // is_complete_type somewhat obviously cannot require a complete type. | 
|  | 2598 | case UTT_IsCompleteType: | 
| Chandler Carruth | d6efe9b | 2011-05-01 19:18:02 +0000 | [diff] [blame] | 2599 | // Fall-through | 
| Chandler Carruth | ccb4ecf | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2600 |  | 
|  | 2601 | // These traits are modeled on the type predicates in C++0x | 
|  | 2602 | // [meta.unary.cat] and [meta.unary.comp]. They are not specified as | 
|  | 2603 | // requiring a complete type, as whether or not they return true cannot be | 
|  | 2604 | // impacted by the completeness of the type. | 
|  | 2605 | case UTT_IsVoid: | 
|  | 2606 | case UTT_IsIntegral: | 
|  | 2607 | case UTT_IsFloatingPoint: | 
|  | 2608 | case UTT_IsArray: | 
|  | 2609 | case UTT_IsPointer: | 
|  | 2610 | case UTT_IsLvalueReference: | 
|  | 2611 | case UTT_IsRvalueReference: | 
|  | 2612 | case UTT_IsMemberFunctionPointer: | 
|  | 2613 | case UTT_IsMemberObjectPointer: | 
|  | 2614 | case UTT_IsEnum: | 
|  | 2615 | case UTT_IsUnion: | 
|  | 2616 | case UTT_IsClass: | 
|  | 2617 | case UTT_IsFunction: | 
|  | 2618 | case UTT_IsReference: | 
|  | 2619 | case UTT_IsArithmetic: | 
|  | 2620 | case UTT_IsFundamental: | 
|  | 2621 | case UTT_IsObject: | 
|  | 2622 | case UTT_IsScalar: | 
|  | 2623 | case UTT_IsCompound: | 
|  | 2624 | case UTT_IsMemberPointer: | 
| Chandler Carruth | d6efe9b | 2011-05-01 19:18:02 +0000 | [diff] [blame] | 2625 | // Fall-through | 
| Chandler Carruth | ccb4ecf | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2626 |  | 
|  | 2627 | // These traits are modeled on type predicates in C++0x [meta.unary.prop] | 
|  | 2628 | // which requires some of its traits to have the complete type. However, | 
|  | 2629 | // the completeness of the type cannot impact these traits' semantics, and | 
|  | 2630 | // so they don't require it. This matches the comments on these traits in | 
|  | 2631 | // Table 49. | 
|  | 2632 | case UTT_IsConst: | 
|  | 2633 | case UTT_IsVolatile: | 
|  | 2634 | case UTT_IsSigned: | 
|  | 2635 | case UTT_IsUnsigned: | 
|  | 2636 | return true; | 
|  | 2637 |  | 
|  | 2638 | // C++0x [meta.unary.prop] Table 49 requires the following traits to be | 
| Chandler Carruth | d6efe9b | 2011-05-01 19:18:02 +0000 | [diff] [blame] | 2639 | // applied to a complete type. | 
| Chandler Carruth | ccb4ecf | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2640 | case UTT_IsTrivial: | 
| Sean Hunt | feb375d | 2011-05-13 00:31:07 +0000 | [diff] [blame] | 2641 | case UTT_IsTriviallyCopyable: | 
| Chandler Carruth | ccb4ecf | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2642 | case UTT_IsStandardLayout: | 
|  | 2643 | case UTT_IsPOD: | 
|  | 2644 | case UTT_IsLiteral: | 
|  | 2645 | case UTT_IsEmpty: | 
|  | 2646 | case UTT_IsPolymorphic: | 
|  | 2647 | case UTT_IsAbstract: | 
| Chandler Carruth | d6efe9b | 2011-05-01 19:18:02 +0000 | [diff] [blame] | 2648 | // Fall-through | 
| Chandler Carruth | ccb4ecf | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2649 |  | 
| Chandler Carruth | d6efe9b | 2011-05-01 19:18:02 +0000 | [diff] [blame] | 2650 | // These trait expressions are designed to help implement predicates in | 
| Chandler Carruth | ccb4ecf | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2651 | // [meta.unary.prop] despite not being named the same. They are specified | 
|  | 2652 | // by both GCC and the Embarcadero C++ compiler, and require the complete | 
|  | 2653 | // type due to the overarching C++0x type predicates being implemented | 
|  | 2654 | // requiring the complete type. | 
|  | 2655 | case UTT_HasNothrowAssign: | 
|  | 2656 | case UTT_HasNothrowConstructor: | 
|  | 2657 | case UTT_HasNothrowCopy: | 
|  | 2658 | case UTT_HasTrivialAssign: | 
| Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 2659 | case UTT_HasTrivialDefaultConstructor: | 
| Chandler Carruth | ccb4ecf | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2660 | case UTT_HasTrivialCopy: | 
|  | 2661 | case UTT_HasTrivialDestructor: | 
|  | 2662 | case UTT_HasVirtualDestructor: | 
|  | 2663 | // Arrays of unknown bound are expressly allowed. | 
|  | 2664 | QualType ElTy = ArgTy; | 
|  | 2665 | if (ArgTy->isIncompleteArrayType()) | 
|  | 2666 | ElTy = S.Context.getAsArrayType(ArgTy)->getElementType(); | 
|  | 2667 |  | 
|  | 2668 | // The void type is expressly allowed. | 
|  | 2669 | if (ElTy->isVoidType()) | 
|  | 2670 | return true; | 
|  | 2671 |  | 
|  | 2672 | return !S.RequireCompleteType( | 
|  | 2673 | Loc, ElTy, diag::err_incomplete_type_used_in_type_trait_expr); | 
| John Wiegley | cf56641 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 2674 | } | 
| Chandler Carruth | 73e0a91 | 2011-05-01 07:23:17 +0000 | [diff] [blame] | 2675 | llvm_unreachable("Type trait not handled by switch"); | 
| Chandler Carruth | ccb4ecf | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 2676 | } | 
|  | 2677 |  | 
|  | 2678 | static bool EvaluateUnaryTypeTrait(Sema &Self, UnaryTypeTrait UTT, | 
|  | 2679 | SourceLocation KeyLoc, QualType T) { | 
| Chandler Carruth | d064c70 | 2011-05-01 08:41:10 +0000 | [diff] [blame] | 2680 | assert(!T->isDependentType() && "Cannot evaluate traits of dependent type"); | 
| John Wiegley | cf56641 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 2681 |  | 
| Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2682 | ASTContext &C = Self.Context; | 
|  | 2683 | switch(UTT) { | 
| Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2684 | // Type trait expressions corresponding to the primary type category | 
|  | 2685 | // predicates in C++0x [meta.unary.cat]. | 
|  | 2686 | case UTT_IsVoid: | 
|  | 2687 | return T->isVoidType(); | 
|  | 2688 | case UTT_IsIntegral: | 
|  | 2689 | return T->isIntegralType(C); | 
|  | 2690 | case UTT_IsFloatingPoint: | 
|  | 2691 | return T->isFloatingType(); | 
|  | 2692 | case UTT_IsArray: | 
|  | 2693 | return T->isArrayType(); | 
|  | 2694 | case UTT_IsPointer: | 
|  | 2695 | return T->isPointerType(); | 
|  | 2696 | case UTT_IsLvalueReference: | 
|  | 2697 | return T->isLValueReferenceType(); | 
|  | 2698 | case UTT_IsRvalueReference: | 
|  | 2699 | return T->isRValueReferenceType(); | 
|  | 2700 | case UTT_IsMemberFunctionPointer: | 
|  | 2701 | return T->isMemberFunctionPointerType(); | 
|  | 2702 | case UTT_IsMemberObjectPointer: | 
|  | 2703 | return T->isMemberDataPointerType(); | 
|  | 2704 | case UTT_IsEnum: | 
|  | 2705 | return T->isEnumeralType(); | 
| Chandler Carruth | 28eeb38 | 2011-05-01 06:11:03 +0000 | [diff] [blame] | 2706 | case UTT_IsUnion: | 
| Chandler Carruth | aaf147b | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 2707 | return T->isUnionType(); | 
| Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2708 | case UTT_IsClass: | 
| Chandler Carruth | aaf147b | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 2709 | return T->isClassType() || T->isStructureType(); | 
| Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2710 | case UTT_IsFunction: | 
|  | 2711 | return T->isFunctionType(); | 
|  | 2712 |  | 
|  | 2713 | // Type trait expressions which correspond to the convenient composition | 
|  | 2714 | // predicates in C++0x [meta.unary.comp]. | 
|  | 2715 | case UTT_IsReference: | 
|  | 2716 | return T->isReferenceType(); | 
|  | 2717 | case UTT_IsArithmetic: | 
| Chandler Carruth | aaf147b | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 2718 | return T->isArithmeticType() && !T->isEnumeralType(); | 
| Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2719 | case UTT_IsFundamental: | 
| Chandler Carruth | aaf147b | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 2720 | return T->isFundamentalType(); | 
| Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2721 | case UTT_IsObject: | 
| Chandler Carruth | aaf147b | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 2722 | return T->isObjectType(); | 
| Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2723 | case UTT_IsScalar: | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2724 | // Note: semantic analysis depends on Objective-C lifetime types to be | 
|  | 2725 | // considered scalar types. However, such types do not actually behave | 
|  | 2726 | // like scalar types at run time (since they may require retain/release | 
|  | 2727 | // operations), so we report them as non-scalar. | 
|  | 2728 | if (T->isObjCLifetimeType()) { | 
|  | 2729 | switch (T.getObjCLifetime()) { | 
|  | 2730 | case Qualifiers::OCL_None: | 
|  | 2731 | case Qualifiers::OCL_ExplicitNone: | 
|  | 2732 | return true; | 
|  | 2733 |  | 
|  | 2734 | case Qualifiers::OCL_Strong: | 
|  | 2735 | case Qualifiers::OCL_Weak: | 
|  | 2736 | case Qualifiers::OCL_Autoreleasing: | 
|  | 2737 | return false; | 
|  | 2738 | } | 
|  | 2739 | } | 
|  | 2740 |  | 
| Chandler Carruth | cec0ced | 2011-05-01 09:29:55 +0000 | [diff] [blame] | 2741 | return T->isScalarType(); | 
| Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2742 | case UTT_IsCompound: | 
| Chandler Carruth | aaf147b | 2011-05-01 09:29:58 +0000 | [diff] [blame] | 2743 | return T->isCompoundType(); | 
| Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2744 | case UTT_IsMemberPointer: | 
|  | 2745 | return T->isMemberPointerType(); | 
|  | 2746 |  | 
|  | 2747 | // Type trait expressions which correspond to the type property predicates | 
|  | 2748 | // in C++0x [meta.unary.prop]. | 
|  | 2749 | case UTT_IsConst: | 
|  | 2750 | return T.isConstQualified(); | 
|  | 2751 | case UTT_IsVolatile: | 
|  | 2752 | return T.isVolatileQualified(); | 
|  | 2753 | case UTT_IsTrivial: | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2754 | return T.isTrivialType(Self.Context); | 
| Sean Hunt | feb375d | 2011-05-13 00:31:07 +0000 | [diff] [blame] | 2755 | case UTT_IsTriviallyCopyable: | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2756 | return T.isTriviallyCopyableType(Self.Context); | 
| Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2757 | case UTT_IsStandardLayout: | 
|  | 2758 | return T->isStandardLayoutType(); | 
|  | 2759 | case UTT_IsPOD: | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2760 | return T.isPODType(Self.Context); | 
| Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2761 | case UTT_IsLiteral: | 
|  | 2762 | return T->isLiteralType(); | 
|  | 2763 | case UTT_IsEmpty: | 
|  | 2764 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) | 
|  | 2765 | return !RD->isUnion() && RD->isEmpty(); | 
|  | 2766 | return false; | 
| Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2767 | case UTT_IsPolymorphic: | 
| Chandler Carruth | 28eeb38 | 2011-05-01 06:11:03 +0000 | [diff] [blame] | 2768 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) | 
|  | 2769 | return RD->isPolymorphic(); | 
| Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2770 | return false; | 
|  | 2771 | case UTT_IsAbstract: | 
| Chandler Carruth | 28eeb38 | 2011-05-01 06:11:03 +0000 | [diff] [blame] | 2772 | if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl()) | 
|  | 2773 | return RD->isAbstract(); | 
| Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2774 | return false; | 
| John Wiegley | 20c0da7 | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 2775 | case UTT_IsSigned: | 
|  | 2776 | return T->isSignedIntegerType(); | 
| John Wiegley | 20c0da7 | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 2777 | case UTT_IsUnsigned: | 
|  | 2778 | return T->isUnsignedIntegerType(); | 
| Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2779 |  | 
|  | 2780 | // Type trait expressions which query classes regarding their construction, | 
|  | 2781 | // destruction, and copying. Rather than being based directly on the | 
|  | 2782 | // related type predicates in the standard, they are specified by both | 
|  | 2783 | // GCC[1] and the Embarcadero C++ compiler[2], and Clang implements those | 
|  | 2784 | // specifications. | 
|  | 2785 | // | 
|  | 2786 | //   1: http://gcc.gnu/.org/onlinedocs/gcc/Type-Traits.html | 
|  | 2787 | //   2: http://docwiki.embarcadero.com/RADStudio/XE/en/Type_Trait_Functions_(C%2B%2B0x)_Index | 
| Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 2788 | case UTT_HasTrivialDefaultConstructor: | 
| Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2789 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: | 
|  | 2790 | //   If __is_pod (type) is true then the trait is true, else if type is | 
|  | 2791 | //   a cv class or union type (or array thereof) with a trivial default | 
|  | 2792 | //   constructor ([class.ctor]) then the trait is true, else it is false. | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2793 | if (T.isPODType(Self.Context)) | 
| Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2794 | return true; | 
|  | 2795 | if (const RecordType *RT = | 
|  | 2796 | C.getBaseElementType(T)->getAs<RecordType>()) | 
| Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 2797 | return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialDefaultConstructor(); | 
| Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2798 | return false; | 
|  | 2799 | case UTT_HasTrivialCopy: | 
|  | 2800 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: | 
|  | 2801 | //   If __is_pod (type) is true or type is a reference type then | 
|  | 2802 | //   the trait is true, else if type is a cv class or union type | 
|  | 2803 | //   with a trivial copy constructor ([class.copy]) then the trait | 
|  | 2804 | //   is true, else it is false. | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2805 | if (T.isPODType(Self.Context) || T->isReferenceType()) | 
| Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2806 | return true; | 
|  | 2807 | if (const RecordType *RT = T->getAs<RecordType>()) | 
|  | 2808 | return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialCopyConstructor(); | 
|  | 2809 | return false; | 
|  | 2810 | case UTT_HasTrivialAssign: | 
|  | 2811 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: | 
|  | 2812 | //   If type is const qualified or is a reference type then the | 
|  | 2813 | //   trait is false. Otherwise if __is_pod (type) is true then the | 
|  | 2814 | //   trait is true, else if type is a cv class or union type with | 
|  | 2815 | //   a trivial copy assignment ([class.copy]) then the trait is | 
|  | 2816 | //   true, else it is false. | 
|  | 2817 | // Note: the const and reference restrictions are interesting, | 
|  | 2818 | // given that const and reference members don't prevent a class | 
|  | 2819 | // from having a trivial copy assignment operator (but do cause | 
|  | 2820 | // errors if the copy assignment operator is actually used, q.v. | 
|  | 2821 | // [class.copy]p12). | 
|  | 2822 |  | 
|  | 2823 | if (C.getBaseElementType(T).isConstQualified()) | 
|  | 2824 | return false; | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2825 | if (T.isPODType(Self.Context)) | 
| Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2826 | return true; | 
|  | 2827 | if (const RecordType *RT = T->getAs<RecordType>()) | 
|  | 2828 | return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialCopyAssignment(); | 
|  | 2829 | return false; | 
|  | 2830 | case UTT_HasTrivialDestructor: | 
|  | 2831 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: | 
|  | 2832 | //   If __is_pod (type) is true or type is a reference type | 
|  | 2833 | //   then the trait is true, else if type is a cv class or union | 
|  | 2834 | //   type (or array thereof) with a trivial destructor | 
|  | 2835 | //   ([class.dtor]) then the trait is true, else it is | 
|  | 2836 | //   false. | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2837 | if (T.isPODType(Self.Context) || T->isReferenceType()) | 
| Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2838 | return true; | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2839 |  | 
|  | 2840 | // Objective-C++ ARC: autorelease types don't require destruction. | 
|  | 2841 | if (T->isObjCLifetimeType() && | 
|  | 2842 | T.getObjCLifetime() == Qualifiers::OCL_Autoreleasing) | 
|  | 2843 | return true; | 
|  | 2844 |  | 
| Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2845 | if (const RecordType *RT = | 
|  | 2846 | C.getBaseElementType(T)->getAs<RecordType>()) | 
|  | 2847 | return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialDestructor(); | 
|  | 2848 | return false; | 
|  | 2849 | // TODO: Propagate nothrowness for implicitly declared special members. | 
|  | 2850 | case UTT_HasNothrowAssign: | 
|  | 2851 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: | 
|  | 2852 | //   If type is const qualified or is a reference type then the | 
|  | 2853 | //   trait is false. Otherwise if __has_trivial_assign (type) | 
|  | 2854 | //   is true then the trait is true, else if type is a cv class | 
|  | 2855 | //   or union type with copy assignment operators that are known | 
|  | 2856 | //   not to throw an exception then the trait is true, else it is | 
|  | 2857 | //   false. | 
|  | 2858 | if (C.getBaseElementType(T).isConstQualified()) | 
|  | 2859 | return false; | 
|  | 2860 | if (T->isReferenceType()) | 
|  | 2861 | return false; | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2862 | if (T.isPODType(Self.Context) || T->isObjCLifetimeType()) | 
|  | 2863 | return true; | 
| Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2864 | if (const RecordType *RT = T->getAs<RecordType>()) { | 
|  | 2865 | CXXRecordDecl* RD = cast<CXXRecordDecl>(RT->getDecl()); | 
|  | 2866 | if (RD->hasTrivialCopyAssignment()) | 
|  | 2867 | return true; | 
|  | 2868 |  | 
|  | 2869 | bool FoundAssign = false; | 
| Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2870 | DeclarationName Name = C.DeclarationNames.getCXXOperatorName(OO_Equal); | 
| Sebastian Redl | f8aca86 | 2010-09-14 23:40:14 +0000 | [diff] [blame] | 2871 | LookupResult Res(Self, DeclarationNameInfo(Name, KeyLoc), | 
|  | 2872 | Sema::LookupOrdinaryName); | 
|  | 2873 | if (Self.LookupQualifiedName(Res, RD)) { | 
| Douglas Gregor | d41679d | 2011-10-12 15:40:49 +0000 | [diff] [blame] | 2874 | Res.suppressDiagnostics(); | 
| Sebastian Redl | f8aca86 | 2010-09-14 23:40:14 +0000 | [diff] [blame] | 2875 | for (LookupResult::iterator Op = Res.begin(), OpEnd = Res.end(); | 
|  | 2876 | Op != OpEnd; ++Op) { | 
| Douglas Gregor | d41679d | 2011-10-12 15:40:49 +0000 | [diff] [blame] | 2877 | if (isa<FunctionTemplateDecl>(*Op)) | 
|  | 2878 | continue; | 
|  | 2879 |  | 
| Sebastian Redl | f8aca86 | 2010-09-14 23:40:14 +0000 | [diff] [blame] | 2880 | CXXMethodDecl *Operator = cast<CXXMethodDecl>(*Op); | 
|  | 2881 | if (Operator->isCopyAssignmentOperator()) { | 
|  | 2882 | FoundAssign = true; | 
|  | 2883 | const FunctionProtoType *CPT | 
|  | 2884 | = Operator->getType()->getAs<FunctionProtoType>(); | 
| Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2885 | if (CPT->getExceptionSpecType() == EST_Delayed) | 
|  | 2886 | return false; | 
|  | 2887 | if (!CPT->isNothrow(Self.Context)) | 
|  | 2888 | return false; | 
| Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2889 | } | 
|  | 2890 | } | 
|  | 2891 | } | 
| Douglas Gregor | d41679d | 2011-10-12 15:40:49 +0000 | [diff] [blame] | 2892 |  | 
| Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2893 | return FoundAssign; | 
| Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2894 | } | 
|  | 2895 | return false; | 
|  | 2896 | case UTT_HasNothrowCopy: | 
|  | 2897 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: | 
|  | 2898 | //   If __has_trivial_copy (type) is true then the trait is true, else | 
|  | 2899 | //   if type is a cv class or union type with copy constructors that are | 
|  | 2900 | //   known not to throw an exception then the trait is true, else it is | 
|  | 2901 | //   false. | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2902 | if (T.isPODType(C) || T->isReferenceType() || T->isObjCLifetimeType()) | 
| Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2903 | return true; | 
|  | 2904 | if (const RecordType *RT = T->getAs<RecordType>()) { | 
|  | 2905 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); | 
|  | 2906 | if (RD->hasTrivialCopyConstructor()) | 
|  | 2907 | return true; | 
|  | 2908 |  | 
|  | 2909 | bool FoundConstructor = false; | 
| Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2910 | unsigned FoundTQs; | 
| Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2911 | DeclContext::lookup_const_iterator Con, ConEnd; | 
| Sebastian Redl | 5f4e899 | 2010-09-13 21:10:20 +0000 | [diff] [blame] | 2912 | for (llvm::tie(Con, ConEnd) = Self.LookupConstructors(RD); | 
| Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2913 | Con != ConEnd; ++Con) { | 
| Sebastian Redl | 08295a5 | 2010-09-13 22:18:28 +0000 | [diff] [blame] | 2914 | // A template constructor is never a copy constructor. | 
|  | 2915 | // FIXME: However, it may actually be selected at the actual overload | 
|  | 2916 | // resolution point. | 
|  | 2917 | if (isa<FunctionTemplateDecl>(*Con)) | 
|  | 2918 | continue; | 
| Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2919 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con); | 
|  | 2920 | if (Constructor->isCopyConstructor(FoundTQs)) { | 
|  | 2921 | FoundConstructor = true; | 
|  | 2922 | const FunctionProtoType *CPT | 
|  | 2923 | = Constructor->getType()->getAs<FunctionProtoType>(); | 
| Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2924 | if (CPT->getExceptionSpecType() == EST_Delayed) | 
|  | 2925 | return false; | 
| Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2926 | // FIXME: check whether evaluating default arguments can throw. | 
| Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 2927 | // For now, we'll be conservative and assume that they can throw. | 
| Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2928 | if (!CPT->isNothrow(Self.Context) || CPT->getNumArgs() > 1) | 
|  | 2929 | return false; | 
| Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2930 | } | 
|  | 2931 | } | 
|  | 2932 |  | 
| Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2933 | return FoundConstructor; | 
| Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2934 | } | 
|  | 2935 | return false; | 
|  | 2936 | case UTT_HasNothrowConstructor: | 
|  | 2937 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: | 
|  | 2938 | //   If __has_trivial_constructor (type) is true then the trait is | 
|  | 2939 | //   true, else if type is a cv class or union type (or array | 
|  | 2940 | //   thereof) with a default constructor that is known not to | 
|  | 2941 | //   throw an exception then the trait is true, else it is false. | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2942 | if (T.isPODType(C) || T->isObjCLifetimeType()) | 
| Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2943 | return true; | 
|  | 2944 | if (const RecordType *RT = C.getBaseElementType(T)->getAs<RecordType>()) { | 
|  | 2945 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); | 
| Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 2946 | if (RD->hasTrivialDefaultConstructor()) | 
| Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2947 | return true; | 
|  | 2948 |  | 
| Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 2949 | DeclContext::lookup_const_iterator Con, ConEnd; | 
|  | 2950 | for (llvm::tie(Con, ConEnd) = Self.LookupConstructors(RD); | 
|  | 2951 | Con != ConEnd; ++Con) { | 
| Sebastian Redl | 08295a5 | 2010-09-13 22:18:28 +0000 | [diff] [blame] | 2952 | // FIXME: In C++0x, a constructor template can be a default constructor. | 
|  | 2953 | if (isa<FunctionTemplateDecl>(*Con)) | 
|  | 2954 | continue; | 
| Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 2955 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con); | 
|  | 2956 | if (Constructor->isDefaultConstructor()) { | 
|  | 2957 | const FunctionProtoType *CPT | 
|  | 2958 | = Constructor->getType()->getAs<FunctionProtoType>(); | 
| Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2959 | if (CPT->getExceptionSpecType() == EST_Delayed) | 
|  | 2960 | return false; | 
| Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 2961 | // TODO: check whether evaluating default arguments can throw. | 
|  | 2962 | // For now, we'll be conservative and assume that they can throw. | 
| Sebastian Redl | 8026f6d | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 2963 | return CPT->isNothrow(Self.Context) && CPT->getNumArgs() == 0; | 
| Sebastian Redl | 751025d | 2010-09-13 22:02:47 +0000 | [diff] [blame] | 2964 | } | 
| Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2965 | } | 
|  | 2966 | } | 
|  | 2967 | return false; | 
|  | 2968 | case UTT_HasVirtualDestructor: | 
|  | 2969 | // http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html: | 
|  | 2970 | //   If type is a class type with a virtual destructor ([class.dtor]) | 
|  | 2971 | //   then the trait is true, else it is false. | 
|  | 2972 | if (const RecordType *Record = T->getAs<RecordType>()) { | 
|  | 2973 | CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl()); | 
| Sebastian Redl | f8aca86 | 2010-09-14 23:40:14 +0000 | [diff] [blame] | 2974 | if (CXXDestructorDecl *Destructor = Self.LookupDestructor(RD)) | 
| Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2975 | return Destructor->isVirtual(); | 
|  | 2976 | } | 
|  | 2977 | return false; | 
| Chandler Carruth | c41d6b5 | 2011-05-01 06:11:07 +0000 | [diff] [blame] | 2978 |  | 
|  | 2979 | // These type trait expressions are modeled on the specifications for the | 
|  | 2980 | // Embarcadero C++0x type trait functions: | 
|  | 2981 | //   http://docwiki.embarcadero.com/RADStudio/XE/en/Type_Trait_Functions_(C%2B%2B0x)_Index | 
|  | 2982 | case UTT_IsCompleteType: | 
|  | 2983 | // http://docwiki.embarcadero.com/RADStudio/XE/en/Is_complete_type_(typename_T_): | 
|  | 2984 | //   Returns True if and only if T is a complete type at the point of the | 
|  | 2985 | //   function call. | 
|  | 2986 | return !T->isIncompleteType(); | 
| Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2987 | } | 
| Chandler Carruth | 83f563c | 2011-05-01 07:44:17 +0000 | [diff] [blame] | 2988 | llvm_unreachable("Type trait not covered by switch"); | 
| Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2989 | } | 
|  | 2990 |  | 
|  | 2991 | ExprResult Sema::BuildUnaryTypeTrait(UnaryTypeTrait UTT, | 
| Douglas Gregor | 3d37c0a | 2010-09-09 16:14:44 +0000 | [diff] [blame] | 2992 | SourceLocation KWLoc, | 
|  | 2993 | TypeSourceInfo *TSInfo, | 
|  | 2994 | SourceLocation RParen) { | 
|  | 2995 | QualType T = TSInfo->getType(); | 
| Chandler Carruth | eb65a10 | 2011-04-30 10:07:32 +0000 | [diff] [blame] | 2996 | if (!CheckUnaryTypeTraitTypeCompleteness(*this, UTT, KWLoc, T)) | 
|  | 2997 | return ExprError(); | 
| Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 2998 |  | 
| Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 2999 | bool Value = false; | 
|  | 3000 | if (!T->isDependentType()) | 
| Chandler Carruth | ccb4ecf | 2011-05-01 06:51:22 +0000 | [diff] [blame] | 3001 | Value = EvaluateUnaryTypeTrait(*this, UTT, KWLoc, T); | 
| Sebastian Redl | 0dfd848 | 2010-09-13 20:56:31 +0000 | [diff] [blame] | 3002 |  | 
|  | 3003 | return Owned(new (Context) UnaryTypeTraitExpr(KWLoc, UTT, TSInfo, Value, | 
| Anders Carlsson | 3292d5c | 2009-07-07 19:06:02 +0000 | [diff] [blame] | 3004 | RParen, Context.BoolTy)); | 
| Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 3005 | } | 
| Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3006 |  | 
| Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3007 | ExprResult Sema::ActOnBinaryTypeTrait(BinaryTypeTrait BTT, | 
|  | 3008 | SourceLocation KWLoc, | 
|  | 3009 | ParsedType LhsTy, | 
|  | 3010 | ParsedType RhsTy, | 
|  | 3011 | SourceLocation RParen) { | 
|  | 3012 | TypeSourceInfo *LhsTSInfo; | 
|  | 3013 | QualType LhsT = GetTypeFromParser(LhsTy, &LhsTSInfo); | 
|  | 3014 | if (!LhsTSInfo) | 
|  | 3015 | LhsTSInfo = Context.getTrivialTypeSourceInfo(LhsT); | 
|  | 3016 |  | 
|  | 3017 | TypeSourceInfo *RhsTSInfo; | 
|  | 3018 | QualType RhsT = GetTypeFromParser(RhsTy, &RhsTSInfo); | 
|  | 3019 | if (!RhsTSInfo) | 
|  | 3020 | RhsTSInfo = Context.getTrivialTypeSourceInfo(RhsT); | 
|  | 3021 |  | 
|  | 3022 | return BuildBinaryTypeTrait(BTT, KWLoc, LhsTSInfo, RhsTSInfo, RParen); | 
|  | 3023 | } | 
|  | 3024 |  | 
|  | 3025 | static bool EvaluateBinaryTypeTrait(Sema &Self, BinaryTypeTrait BTT, | 
|  | 3026 | QualType LhsT, QualType RhsT, | 
|  | 3027 | SourceLocation KeyLoc) { | 
| Chandler Carruth | d064c70 | 2011-05-01 08:41:10 +0000 | [diff] [blame] | 3028 | assert(!LhsT->isDependentType() && !RhsT->isDependentType() && | 
|  | 3029 | "Cannot evaluate traits of dependent types"); | 
| Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3030 |  | 
|  | 3031 | switch(BTT) { | 
| John McCall | d89d30f | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 3032 | case BTT_IsBaseOf: { | 
| Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3033 | // C++0x [meta.rel]p2 | 
| John McCall | d89d30f | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 3034 | // Base is a base class of Derived without regard to cv-qualifiers or | 
| Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3035 | // Base and Derived are not unions and name the same class type without | 
|  | 3036 | // regard to cv-qualifiers. | 
| Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3037 |  | 
| John McCall | d89d30f | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 3038 | const RecordType *lhsRecord = LhsT->getAs<RecordType>(); | 
|  | 3039 | if (!lhsRecord) return false; | 
|  | 3040 |  | 
|  | 3041 | const RecordType *rhsRecord = RhsT->getAs<RecordType>(); | 
|  | 3042 | if (!rhsRecord) return false; | 
|  | 3043 |  | 
|  | 3044 | assert(Self.Context.hasSameUnqualifiedType(LhsT, RhsT) | 
|  | 3045 | == (lhsRecord == rhsRecord)); | 
|  | 3046 |  | 
|  | 3047 | if (lhsRecord == rhsRecord) | 
|  | 3048 | return !lhsRecord->getDecl()->isUnion(); | 
|  | 3049 |  | 
|  | 3050 | // C++0x [meta.rel]p2: | 
|  | 3051 | //   If Base and Derived are class types and are different types | 
|  | 3052 | //   (ignoring possible cv-qualifiers) then Derived shall be a | 
|  | 3053 | //   complete type. | 
|  | 3054 | if (Self.RequireCompleteType(KeyLoc, RhsT, | 
|  | 3055 | diag::err_incomplete_type_used_in_type_trait_expr)) | 
|  | 3056 | return false; | 
|  | 3057 |  | 
|  | 3058 | return cast<CXXRecordDecl>(rhsRecord->getDecl()) | 
|  | 3059 | ->isDerivedFrom(cast<CXXRecordDecl>(lhsRecord->getDecl())); | 
|  | 3060 | } | 
| John Wiegley | 20c0da7 | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 3061 | case BTT_IsSame: | 
|  | 3062 | return Self.Context.hasSameType(LhsT, RhsT); | 
| Francois Pichet | f187237 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 3063 | case BTT_TypeCompatible: | 
|  | 3064 | return Self.Context.typesAreCompatible(LhsT.getUnqualifiedType(), | 
|  | 3065 | RhsT.getUnqualifiedType()); | 
| John Wiegley | 20c0da7 | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 3066 | case BTT_IsConvertible: | 
| Douglas Gregor | 9f36113 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 3067 | case BTT_IsConvertibleTo: { | 
|  | 3068 | // C++0x [meta.rel]p4: | 
|  | 3069 | //   Given the following function prototype: | 
|  | 3070 | // | 
|  | 3071 | //     template <class T> | 
|  | 3072 | //       typename add_rvalue_reference<T>::type create(); | 
|  | 3073 | // | 
|  | 3074 | //   the predicate condition for a template specialization | 
|  | 3075 | //   is_convertible<From, To> shall be satisfied if and only if | 
|  | 3076 | //   the return expression in the following code would be | 
|  | 3077 | //   well-formed, including any implicit conversions to the return | 
|  | 3078 | //   type of the function: | 
|  | 3079 | // | 
|  | 3080 | //     To test() { | 
|  | 3081 | //       return create<From>(); | 
|  | 3082 | //     } | 
|  | 3083 | // | 
|  | 3084 | //   Access checking is performed as if in a context unrelated to To and | 
|  | 3085 | //   From. Only the validity of the immediate context of the expression | 
|  | 3086 | //   of the return-statement (including conversions to the return type) | 
|  | 3087 | //   is considered. | 
|  | 3088 | // | 
|  | 3089 | // We model the initialization as a copy-initialization of a temporary | 
|  | 3090 | // of the appropriate type, which for this expression is identical to the | 
|  | 3091 | // return statement (since NRVO doesn't apply). | 
|  | 3092 | if (LhsT->isObjectType() || LhsT->isFunctionType()) | 
|  | 3093 | LhsT = Self.Context.getRValueReferenceType(LhsT); | 
|  | 3094 |  | 
|  | 3095 | InitializedEntity To(InitializedEntity::InitializeTemporary(RhsT)); | 
| Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 3096 | OpaqueValueExpr From(KeyLoc, LhsT.getNonLValueExprType(Self.Context), | 
| Douglas Gregor | 9f36113 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 3097 | Expr::getValueKindForType(LhsT)); | 
|  | 3098 | Expr *FromPtr = &From; | 
|  | 3099 | InitializationKind Kind(InitializationKind::CreateCopy(KeyLoc, | 
|  | 3100 | SourceLocation())); | 
|  | 3101 |  | 
| Douglas Gregor | 1eee5dc | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 3102 | // Perform the initialization within a SFINAE trap at translation unit | 
|  | 3103 | // scope. | 
|  | 3104 | Sema::SFINAETrap SFINAE(Self, /*AccessCheckingSFINAE=*/true); | 
|  | 3105 | Sema::ContextRAII TUContext(Self, Self.Context.getTranslationUnitDecl()); | 
| Douglas Gregor | 9f36113 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 3106 | InitializationSequence Init(Self, To, Kind, &FromPtr, 1); | 
| Sebastian Redl | 383616c | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 3107 | if (Init.Failed()) | 
| Douglas Gregor | 9f36113 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 3108 | return false; | 
| Douglas Gregor | 1eee5dc | 2011-01-27 22:31:44 +0000 | [diff] [blame] | 3109 |  | 
| Douglas Gregor | 9f36113 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 3110 | ExprResult Result = Init.Perform(Self, To, Kind, MultiExprArg(&FromPtr, 1)); | 
|  | 3111 | return !Result.isInvalid() && !SFINAE.hasErrorOccurred(); | 
|  | 3112 | } | 
| Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3113 | } | 
|  | 3114 | llvm_unreachable("Unknown type trait or not implemented"); | 
|  | 3115 | } | 
|  | 3116 |  | 
|  | 3117 | ExprResult Sema::BuildBinaryTypeTrait(BinaryTypeTrait BTT, | 
|  | 3118 | SourceLocation KWLoc, | 
|  | 3119 | TypeSourceInfo *LhsTSInfo, | 
|  | 3120 | TypeSourceInfo *RhsTSInfo, | 
|  | 3121 | SourceLocation RParen) { | 
|  | 3122 | QualType LhsT = LhsTSInfo->getType(); | 
|  | 3123 | QualType RhsT = RhsTSInfo->getType(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3124 |  | 
| John McCall | d89d30f | 2011-01-28 22:02:36 +0000 | [diff] [blame] | 3125 | if (BTT == BTT_TypeCompatible) { | 
| Francois Pichet | f187237 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 3126 | if (getLangOptions().CPlusPlus) { | 
|  | 3127 | Diag(KWLoc, diag::err_types_compatible_p_in_cplusplus) | 
|  | 3128 | << SourceRange(KWLoc, RParen); | 
|  | 3129 | return ExprError(); | 
|  | 3130 | } | 
| Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3131 | } | 
|  | 3132 |  | 
|  | 3133 | bool Value = false; | 
|  | 3134 | if (!LhsT->isDependentType() && !RhsT->isDependentType()) | 
|  | 3135 | Value = EvaluateBinaryTypeTrait(*this, BTT, LhsT, RhsT, KWLoc); | 
|  | 3136 |  | 
| Francois Pichet | f187237 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 3137 | // Select trait result type. | 
|  | 3138 | QualType ResultType; | 
|  | 3139 | switch (BTT) { | 
| Francois Pichet | f187237 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 3140 | case BTT_IsBaseOf:       ResultType = Context.BoolTy; break; | 
| John Wiegley | 20c0da7 | 2011-04-27 23:09:49 +0000 | [diff] [blame] | 3141 | case BTT_IsConvertible:  ResultType = Context.BoolTy; break; | 
|  | 3142 | case BTT_IsSame:         ResultType = Context.BoolTy; break; | 
| Francois Pichet | f187237 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 3143 | case BTT_TypeCompatible: ResultType = Context.IntTy; break; | 
| Douglas Gregor | 9f36113 | 2011-01-27 20:28:01 +0000 | [diff] [blame] | 3144 | case BTT_IsConvertibleTo: ResultType = Context.BoolTy; break; | 
| Francois Pichet | f187237 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 3145 | } | 
|  | 3146 |  | 
| Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3147 | return Owned(new (Context) BinaryTypeTraitExpr(KWLoc, BTT, LhsTSInfo, | 
|  | 3148 | RhsTSInfo, Value, RParen, | 
| Francois Pichet | f187237 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 3149 | ResultType)); | 
| Francois Pichet | 6ad6f28 | 2010-12-07 00:08:36 +0000 | [diff] [blame] | 3150 | } | 
|  | 3151 |  | 
| John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3152 | ExprResult Sema::ActOnArrayTypeTrait(ArrayTypeTrait ATT, | 
|  | 3153 | SourceLocation KWLoc, | 
|  | 3154 | ParsedType Ty, | 
|  | 3155 | Expr* DimExpr, | 
|  | 3156 | SourceLocation RParen) { | 
|  | 3157 | TypeSourceInfo *TSInfo; | 
|  | 3158 | QualType T = GetTypeFromParser(Ty, &TSInfo); | 
|  | 3159 | if (!TSInfo) | 
|  | 3160 | TSInfo = Context.getTrivialTypeSourceInfo(T); | 
|  | 3161 |  | 
|  | 3162 | return BuildArrayTypeTrait(ATT, KWLoc, TSInfo, DimExpr, RParen); | 
|  | 3163 | } | 
|  | 3164 |  | 
|  | 3165 | static uint64_t EvaluateArrayTypeTrait(Sema &Self, ArrayTypeTrait ATT, | 
|  | 3166 | QualType T, Expr *DimExpr, | 
|  | 3167 | SourceLocation KeyLoc) { | 
| Chandler Carruth | d064c70 | 2011-05-01 08:41:10 +0000 | [diff] [blame] | 3168 | assert(!T->isDependentType() && "Cannot evaluate traits of dependent type"); | 
| John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3169 |  | 
|  | 3170 | switch(ATT) { | 
|  | 3171 | case ATT_ArrayRank: | 
|  | 3172 | if (T->isArrayType()) { | 
|  | 3173 | unsigned Dim = 0; | 
|  | 3174 | while (const ArrayType *AT = Self.Context.getAsArrayType(T)) { | 
|  | 3175 | ++Dim; | 
|  | 3176 | T = AT->getElementType(); | 
|  | 3177 | } | 
|  | 3178 | return Dim; | 
| John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3179 | } | 
| John Wiegley | cf56641 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 3180 | return 0; | 
|  | 3181 |  | 
| John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3182 | case ATT_ArrayExtent: { | 
|  | 3183 | llvm::APSInt Value; | 
|  | 3184 | uint64_t Dim; | 
| John Wiegley | cf56641 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 3185 | if (DimExpr->isIntegerConstantExpr(Value, Self.Context, 0, false)) { | 
|  | 3186 | if (Value < llvm::APSInt(Value.getBitWidth(), Value.isUnsigned())) { | 
|  | 3187 | Self.Diag(KeyLoc, diag::err_dimension_expr_not_constant_integer) << | 
|  | 3188 | DimExpr->getSourceRange(); | 
|  | 3189 | return false; | 
|  | 3190 | } | 
| John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3191 | Dim = Value.getLimitedValue(); | 
| John Wiegley | cf56641 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 3192 | } else { | 
|  | 3193 | Self.Diag(KeyLoc, diag::err_dimension_expr_not_constant_integer) << | 
|  | 3194 | DimExpr->getSourceRange(); | 
|  | 3195 | return false; | 
|  | 3196 | } | 
| John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3197 |  | 
|  | 3198 | if (T->isArrayType()) { | 
|  | 3199 | unsigned D = 0; | 
|  | 3200 | bool Matched = false; | 
|  | 3201 | while (const ArrayType *AT = Self.Context.getAsArrayType(T)) { | 
|  | 3202 | if (Dim == D) { | 
|  | 3203 | Matched = true; | 
|  | 3204 | break; | 
|  | 3205 | } | 
|  | 3206 | ++D; | 
|  | 3207 | T = AT->getElementType(); | 
|  | 3208 | } | 
|  | 3209 |  | 
| John Wiegley | cf56641 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 3210 | if (Matched && T->isArrayType()) { | 
|  | 3211 | if (const ConstantArrayType *CAT = Self.Context.getAsConstantArrayType(T)) | 
|  | 3212 | return CAT->getSize().getLimitedValue(); | 
|  | 3213 | } | 
| John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3214 | } | 
| John Wiegley | cf56641 | 2011-04-28 02:06:46 +0000 | [diff] [blame] | 3215 | return 0; | 
| John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3216 | } | 
|  | 3217 | } | 
|  | 3218 | llvm_unreachable("Unknown type trait or not implemented"); | 
|  | 3219 | } | 
|  | 3220 |  | 
|  | 3221 | ExprResult Sema::BuildArrayTypeTrait(ArrayTypeTrait ATT, | 
|  | 3222 | SourceLocation KWLoc, | 
|  | 3223 | TypeSourceInfo *TSInfo, | 
|  | 3224 | Expr* DimExpr, | 
|  | 3225 | SourceLocation RParen) { | 
|  | 3226 | QualType T = TSInfo->getType(); | 
| John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3227 |  | 
| Chandler Carruth | af5a3c6 | 2011-05-01 08:48:21 +0000 | [diff] [blame] | 3228 | // FIXME: This should likely be tracked as an APInt to remove any host | 
|  | 3229 | // assumptions about the width of size_t on the target. | 
| Chandler Carruth | d064c70 | 2011-05-01 08:41:10 +0000 | [diff] [blame] | 3230 | uint64_t Value = 0; | 
|  | 3231 | if (!T->isDependentType()) | 
|  | 3232 | Value = EvaluateArrayTypeTrait(*this, ATT, T, DimExpr, KWLoc); | 
|  | 3233 |  | 
| Chandler Carruth | af5a3c6 | 2011-05-01 08:48:21 +0000 | [diff] [blame] | 3234 | // While the specification for these traits from the Embarcadero C++ | 
|  | 3235 | // compiler's documentation says the return type is 'unsigned int', Clang | 
|  | 3236 | // returns 'size_t'. On Windows, the primary platform for the Embarcadero | 
|  | 3237 | // compiler, there is no difference. On several other platforms this is an | 
|  | 3238 | // important distinction. | 
| John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3239 | return Owned(new (Context) ArrayTypeTraitExpr(KWLoc, ATT, TSInfo, Value, | 
| Chandler Carruth | 06207f6 | 2011-05-01 07:49:26 +0000 | [diff] [blame] | 3240 | DimExpr, RParen, | 
| Chandler Carruth | af5a3c6 | 2011-05-01 08:48:21 +0000 | [diff] [blame] | 3241 | Context.getSizeType())); | 
| John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 3242 | } | 
|  | 3243 |  | 
| John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 3244 | ExprResult Sema::ActOnExpressionTrait(ExpressionTrait ET, | 
| Chandler Carruth | 4aa0af3 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 3245 | SourceLocation KWLoc, | 
|  | 3246 | Expr *Queried, | 
|  | 3247 | SourceLocation RParen) { | 
| John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 3248 | // If error parsing the expression, ignore. | 
|  | 3249 | if (!Queried) | 
| Chandler Carruth | 4aa0af3 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 3250 | return ExprError(); | 
| John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 3251 |  | 
| Chandler Carruth | 4aa0af3 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 3252 | ExprResult Result = BuildExpressionTrait(ET, KWLoc, Queried, RParen); | 
| John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 3253 |  | 
|  | 3254 | return move(Result); | 
|  | 3255 | } | 
|  | 3256 |  | 
| Chandler Carruth | 4aa0af3 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 3257 | static bool EvaluateExpressionTrait(ExpressionTrait ET, Expr *E) { | 
|  | 3258 | switch (ET) { | 
|  | 3259 | case ET_IsLValueExpr: return E->isLValue(); | 
|  | 3260 | case ET_IsRValueExpr: return E->isRValue(); | 
|  | 3261 | } | 
|  | 3262 | llvm_unreachable("Expression trait not covered by switch"); | 
|  | 3263 | } | 
|  | 3264 |  | 
| John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 3265 | ExprResult Sema::BuildExpressionTrait(ExpressionTrait ET, | 
| Chandler Carruth | 4aa0af3 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 3266 | SourceLocation KWLoc, | 
|  | 3267 | Expr *Queried, | 
|  | 3268 | SourceLocation RParen) { | 
| John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 3269 | if (Queried->isTypeDependent()) { | 
|  | 3270 | // Delay type-checking for type-dependent expressions. | 
|  | 3271 | } else if (Queried->getType()->isPlaceholderType()) { | 
|  | 3272 | ExprResult PE = CheckPlaceholderExpr(Queried); | 
|  | 3273 | if (PE.isInvalid()) return ExprError(); | 
|  | 3274 | return BuildExpressionTrait(ET, KWLoc, PE.take(), RParen); | 
|  | 3275 | } | 
|  | 3276 |  | 
| Chandler Carruth | 4aa0af3 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 3277 | bool Value = EvaluateExpressionTrait(ET, Queried); | 
| Chandler Carruth | f7ef000 | 2011-05-01 08:48:19 +0000 | [diff] [blame] | 3278 |  | 
| Chandler Carruth | 4aa0af3 | 2011-05-01 07:44:20 +0000 | [diff] [blame] | 3279 | return Owned(new (Context) ExpressionTraitExpr(KWLoc, ET, Queried, Value, | 
|  | 3280 | RParen, Context.BoolTy)); | 
| John Wiegley | 5526220 | 2011-04-25 06:54:41 +0000 | [diff] [blame] | 3281 | } | 
|  | 3282 |  | 
| Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3283 | QualType Sema::CheckPointerToMemberOperands(ExprResult &LHS, ExprResult &RHS, | 
| John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3284 | ExprValueKind &VK, | 
|  | 3285 | SourceLocation Loc, | 
|  | 3286 | bool isIndirect) { | 
| Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3287 | assert(!LHS.get()->getType()->isPlaceholderType() && | 
|  | 3288 | !RHS.get()->getType()->isPlaceholderType() && | 
| John McCall | ea4aba0 | 2011-06-30 17:15:34 +0000 | [diff] [blame] | 3289 | "placeholders should have been weeded out by now"); | 
|  | 3290 |  | 
|  | 3291 | // The LHS undergoes lvalue conversions if this is ->*. | 
|  | 3292 | if (isIndirect) { | 
| Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3293 | LHS = DefaultLvalueConversion(LHS.take()); | 
|  | 3294 | if (LHS.isInvalid()) return QualType(); | 
| John McCall | ea4aba0 | 2011-06-30 17:15:34 +0000 | [diff] [blame] | 3295 | } | 
|  | 3296 |  | 
|  | 3297 | // The RHS always undergoes lvalue conversions. | 
| Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3298 | RHS = DefaultLvalueConversion(RHS.take()); | 
|  | 3299 | if (RHS.isInvalid()) return QualType(); | 
| John McCall | ea4aba0 | 2011-06-30 17:15:34 +0000 | [diff] [blame] | 3300 |  | 
| Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3301 | const char *OpSpelling = isIndirect ? "->*" : ".*"; | 
|  | 3302 | // C++ 5.5p2 | 
|  | 3303 | //   The binary operator .* [p3: ->*] binds its second operand, which shall | 
|  | 3304 | //   be of type "pointer to member of T" (where T is a completely-defined | 
|  | 3305 | //   class type) [...] | 
| Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3306 | QualType RHSType = RHS.get()->getType(); | 
|  | 3307 | const MemberPointerType *MemPtr = RHSType->getAs<MemberPointerType>(); | 
| Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3308 | if (!MemPtr) { | 
| Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3309 | Diag(Loc, diag::err_bad_memptr_rhs) | 
| Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3310 | << OpSpelling << RHSType << RHS.get()->getSourceRange(); | 
| Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3311 | return QualType(); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3312 | } | 
| Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3313 |  | 
| Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3314 | QualType Class(MemPtr->getClass(), 0); | 
|  | 3315 |  | 
| Douglas Gregor | 7d520ba | 2010-10-13 20:41:14 +0000 | [diff] [blame] | 3316 | // Note: C++ [expr.mptr.oper]p2-3 says that the class type into which the | 
|  | 3317 | // member pointer points must be completely-defined. However, there is no | 
|  | 3318 | // reason for this semantic distinction, and the rule is not enforced by | 
|  | 3319 | // other compilers. Therefore, we do not check this property, as it is | 
|  | 3320 | // likely to be considered a defect. | 
| Sebastian Redl | 59fc269 | 2010-04-10 10:14:54 +0000 | [diff] [blame] | 3321 |  | 
| Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3322 | // C++ 5.5p2 | 
|  | 3323 | //   [...] to its first operand, which shall be of class T or of a class of | 
|  | 3324 | //   which T is an unambiguous and accessible base class. [p3: a pointer to | 
|  | 3325 | //   such a class] | 
| Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3326 | QualType LHSType = LHS.get()->getType(); | 
| Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3327 | if (isIndirect) { | 
| Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3328 | if (const PointerType *Ptr = LHSType->getAs<PointerType>()) | 
|  | 3329 | LHSType = Ptr->getPointeeType(); | 
| Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3330 | else { | 
|  | 3331 | Diag(Loc, diag::err_bad_memptr_lhs) | 
| Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3332 | << OpSpelling << 1 << LHSType | 
| Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3333 | << FixItHint::CreateReplacement(SourceRange(Loc), ".*"); | 
| Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3334 | return QualType(); | 
|  | 3335 | } | 
|  | 3336 | } | 
|  | 3337 |  | 
| Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3338 | if (!Context.hasSameUnqualifiedType(Class, LHSType)) { | 
| Sebastian Redl | 17e1d35 | 2010-04-23 17:18:26 +0000 | [diff] [blame] | 3339 | // If we want to check the hierarchy, we need a complete type. | 
| Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3340 | if (RequireCompleteType(Loc, LHSType, PDiag(diag::err_bad_memptr_lhs) | 
| Sebastian Redl | 17e1d35 | 2010-04-23 17:18:26 +0000 | [diff] [blame] | 3341 | << OpSpelling << (int)isIndirect)) { | 
|  | 3342 | return QualType(); | 
|  | 3343 | } | 
| Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 3344 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, | 
| Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 3345 | /*DetectVirtual=*/false); | 
| Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3346 | // FIXME: Would it be useful to print full ambiguity paths, or is that | 
|  | 3347 | // overkill? | 
| Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3348 | if (!IsDerivedFrom(LHSType, Class, Paths) || | 
| Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3349 | Paths.isAmbiguous(Context.getCanonicalType(Class))) { | 
|  | 3350 | Diag(Loc, diag::err_bad_memptr_lhs) << OpSpelling | 
| Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3351 | << (int)isIndirect << LHS.get()->getType(); | 
| Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3352 | return QualType(); | 
|  | 3353 | } | 
| Eli Friedman | 3005efe | 2010-01-16 00:00:48 +0000 | [diff] [blame] | 3354 | // Cast LHS to type of use. | 
|  | 3355 | QualType UseType = isIndirect ? Context.getPointerType(Class) : Class; | 
| Eli Friedman | c1c0dfb | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 3356 | ExprValueKind VK = isIndirect ? VK_RValue : LHS.get()->getValueKind(); | 
| Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3357 |  | 
| John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 3358 | CXXCastPath BasePath; | 
| Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 3359 | BuildBasePathArray(Paths, BasePath); | 
| Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3360 | LHS = ImpCastExprToType(LHS.take(), UseType, CK_DerivedToBase, VK, | 
|  | 3361 | &BasePath); | 
| Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3362 | } | 
|  | 3363 |  | 
| Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3364 | if (isa<CXXScalarValueInitExpr>(RHS.get()->IgnoreParens())) { | 
| Fariborz Jahanian | 05ebda9 | 2009-11-18 21:54:48 +0000 | [diff] [blame] | 3365 | // Diagnose use of pointer-to-member type which when used as | 
|  | 3366 | // the functional cast in a pointer-to-member expression. | 
|  | 3367 | Diag(Loc, diag::err_pointer_to_member_type) << isIndirect; | 
|  | 3368 | return QualType(); | 
|  | 3369 | } | 
| John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3370 |  | 
| Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3371 | // C++ 5.5p2 | 
|  | 3372 | //   The result is an object or a function of the type specified by the | 
|  | 3373 | //   second operand. | 
|  | 3374 | // The cv qualifiers are the union of those in the pointer and the left side, | 
|  | 3375 | // in accordance with 5.5p5 and 5.2.5. | 
| Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3376 | QualType Result = MemPtr->getPointeeType(); | 
| Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3377 | Result = Context.getCVRQualifiedType(Result, LHSType.getCVRQualifiers()); | 
| John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3378 |  | 
| Douglas Gregor | 6b4df91 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 3379 | // C++0x [expr.mptr.oper]p6: | 
|  | 3380 | //   In a .* expression whose object expression is an rvalue, the program is | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3381 | //   ill-formed if the second operand is a pointer to member function with | 
|  | 3382 | //   ref-qualifier &. In a ->* expression or in a .* expression whose object | 
|  | 3383 | //   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] | 3384 | //   is a pointer to member function with ref-qualifier &&. | 
|  | 3385 | if (const FunctionProtoType *Proto = Result->getAs<FunctionProtoType>()) { | 
|  | 3386 | switch (Proto->getRefQualifier()) { | 
|  | 3387 | case RQ_None: | 
|  | 3388 | // Do nothing | 
|  | 3389 | break; | 
|  | 3390 |  | 
|  | 3391 | case RQ_LValue: | 
| Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3392 | if (!isIndirect && !LHS.get()->Classify(Context).isLValue()) | 
| Douglas Gregor | 6b4df91 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 3393 | Diag(Loc, diag::err_pointer_to_member_oper_value_classify) | 
| Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3394 | << RHSType << 1 << LHS.get()->getSourceRange(); | 
| Douglas Gregor | 6b4df91 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 3395 | break; | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3396 |  | 
| Douglas Gregor | 6b4df91 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 3397 | case RQ_RValue: | 
| Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3398 | if (isIndirect || !LHS.get()->Classify(Context).isRValue()) | 
| Douglas Gregor | 6b4df91 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 3399 | Diag(Loc, diag::err_pointer_to_member_oper_value_classify) | 
| Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3400 | << RHSType << 0 << LHS.get()->getSourceRange(); | 
| Douglas Gregor | 6b4df91 | 2011-01-26 16:40:18 +0000 | [diff] [blame] | 3401 | break; | 
|  | 3402 | } | 
|  | 3403 | } | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3404 |  | 
| John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3405 | // C++ [expr.mptr.oper]p6: | 
|  | 3406 | //   The result of a .* expression whose second operand is a pointer | 
|  | 3407 | //   to a data member is of the same value category as its | 
|  | 3408 | //   first operand. The result of a .* expression whose second | 
|  | 3409 | //   operand is a pointer to a member function is a prvalue. The | 
|  | 3410 | //   result of an ->* expression is an lvalue if its second operand | 
|  | 3411 | //   is a pointer to data member and a prvalue otherwise. | 
| John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3412 | if (Result->isFunctionType()) { | 
| John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3413 | VK = VK_RValue; | 
| John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3414 | return Context.BoundMemberTy; | 
|  | 3415 | } else if (isIndirect) { | 
| John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3416 | VK = VK_LValue; | 
| John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3417 | } else { | 
| Richard Trieu | dd22509 | 2011-09-15 21:56:47 +0000 | [diff] [blame] | 3418 | VK = LHS.get()->getValueKind(); | 
| John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3419 | } | 
| John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3420 |  | 
| Sebastian Redl | 7c8bd60 | 2009-02-07 20:10:22 +0000 | [diff] [blame] | 3421 | return Result; | 
|  | 3422 | } | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3423 |  | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3424 | /// \brief Try to convert a type to another according to C++0x 5.16p3. | 
|  | 3425 | /// | 
|  | 3426 | /// This is part of the parameter validation for the ? operator. If either | 
|  | 3427 | /// value operand is a class type, the two operands are attempted to be | 
|  | 3428 | /// converted to each other. This function does the conversion in one direction. | 
| Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3429 | /// It returns true if the program is ill-formed and has already been diagnosed | 
|  | 3430 | /// as such. | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3431 | static bool TryClassUnification(Sema &Self, Expr *From, Expr *To, | 
|  | 3432 | SourceLocation QuestionLoc, | 
| Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3433 | bool &HaveConversion, | 
|  | 3434 | QualType &ToType) { | 
|  | 3435 | HaveConversion = false; | 
|  | 3436 | ToType = To->getType(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3437 |  | 
|  | 3438 | InitializationKind Kind = InitializationKind::CreateCopy(To->getLocStart(), | 
| Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3439 | SourceLocation()); | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3440 | // C++0x 5.16p3 | 
|  | 3441 | //   The process for determining whether an operand expression E1 of type T1 | 
|  | 3442 | //   can be converted to match an operand expression E2 of type T2 is defined | 
|  | 3443 | //   as follows: | 
|  | 3444 | //   -- If E2 is an lvalue: | 
| John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 3445 | bool ToIsLvalue = To->isLValue(); | 
| Douglas Gregor | 0fd8ff7 | 2010-03-26 20:59:55 +0000 | [diff] [blame] | 3446 | if (ToIsLvalue) { | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3447 | //   E1 can be converted to match E2 if E1 can be implicitly converted to | 
|  | 3448 | //   type "lvalue reference to T2", subject to the constraint that in the | 
|  | 3449 | //   conversion the reference must bind directly to E1. | 
| Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3450 | QualType T = Self.Context.getLValueReferenceType(ToType); | 
|  | 3451 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(T); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3452 |  | 
| Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3453 | InitializationSequence InitSeq(Self, Entity, Kind, &From, 1); | 
|  | 3454 | if (InitSeq.isDirectReferenceBinding()) { | 
|  | 3455 | ToType = T; | 
|  | 3456 | HaveConversion = true; | 
|  | 3457 | return false; | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3458 | } | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3459 |  | 
| Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3460 | if (InitSeq.isAmbiguous()) | 
|  | 3461 | return InitSeq.Diagnose(Self, Entity, Kind, &From, 1); | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3462 | } | 
| John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3463 |  | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3464 | //   -- If E2 is an rvalue, or if the conversion above cannot be done: | 
|  | 3465 | //      -- if E1 and E2 have class type, and the underlying class types are | 
|  | 3466 | //         the same or one is a base class of the other: | 
|  | 3467 | QualType FTy = From->getType(); | 
|  | 3468 | QualType TTy = To->getType(); | 
| Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3469 | const RecordType *FRec = FTy->getAs<RecordType>(); | 
|  | 3470 | const RecordType *TRec = TTy->getAs<RecordType>(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3471 | bool FDerivedFromT = FRec && TRec && FRec != TRec && | 
| Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3472 | Self.IsDerivedFrom(FTy, TTy); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3473 | if (FRec && TRec && | 
| Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3474 | (FRec == TRec || FDerivedFromT || Self.IsDerivedFrom(TTy, FTy))) { | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3475 | //         E1 can be converted to match E2 if the class of T2 is the | 
|  | 3476 | //         same type as, or a base class of, the class of T1, and | 
|  | 3477 | //         [cv2 > cv1]. | 
| John McCall | b1bdc62 | 2010-02-25 01:37:24 +0000 | [diff] [blame] | 3478 | if (FRec == TRec || FDerivedFromT) { | 
|  | 3479 | if (TTy.isAtLeastAsQualifiedAs(FTy)) { | 
| Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3480 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(TTy); | 
|  | 3481 | InitializationSequence InitSeq(Self, Entity, Kind, &From, 1); | 
| Sebastian Redl | 383616c | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 3482 | if (InitSeq) { | 
| Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3483 | HaveConversion = true; | 
|  | 3484 | return false; | 
|  | 3485 | } | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3486 |  | 
| Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3487 | if (InitSeq.isAmbiguous()) | 
|  | 3488 | return InitSeq.Diagnose(Self, Entity, Kind, &From, 1); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3489 | } | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3490 | } | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3491 |  | 
| Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3492 | return false; | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3493 | } | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3494 |  | 
| Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3495 | //     -- Otherwise: E1 can be converted to match E2 if E1 can be | 
|  | 3496 | //        implicitly converted to the type that expression E2 would have | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3497 | //        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] | 3498 | //        an rvalue). | 
|  | 3499 | // | 
|  | 3500 | // This actually refers very narrowly to the lvalue-to-rvalue conversion, not | 
|  | 3501 | // to the array-to-pointer or function-to-pointer conversions. | 
|  | 3502 | if (!TTy->getAs<TagType>()) | 
|  | 3503 | TTy = TTy.getUnqualifiedType(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3504 |  | 
| Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3505 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(TTy); | 
|  | 3506 | InitializationSequence InitSeq(Self, Entity, Kind, &From, 1); | 
| Sebastian Redl | 383616c | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 3507 | HaveConversion = !InitSeq.Failed(); | 
| Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3508 | ToType = TTy; | 
|  | 3509 | if (InitSeq.isAmbiguous()) | 
|  | 3510 | return InitSeq.Diagnose(Self, Entity, Kind, &From, 1); | 
|  | 3511 |  | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3512 | return false; | 
|  | 3513 | } | 
|  | 3514 |  | 
|  | 3515 | /// \brief Try to find a common type for two according to C++0x 5.16p5. | 
|  | 3516 | /// | 
|  | 3517 | /// This is part of the parameter validation for the ? operator. If either | 
|  | 3518 | /// value operand is a class type, overload resolution is used to find a | 
|  | 3519 | /// conversion to a common type. | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3520 | static bool FindConditionalOverload(Sema &Self, ExprResult &LHS, ExprResult &RHS, | 
| Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 3521 | SourceLocation QuestionLoc) { | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3522 | Expr *Args[2] = { LHS.get(), RHS.get() }; | 
| Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 3523 | OverloadCandidateSet CandidateSet(QuestionLoc); | 
|  | 3524 | Self.AddBuiltinOperatorCandidates(OO_Conditional, QuestionLoc, Args, 2, | 
|  | 3525 | CandidateSet); | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3526 |  | 
|  | 3527 | OverloadCandidateSet::iterator Best; | 
| Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 3528 | switch (CandidateSet.BestViableFunction(Self, QuestionLoc, Best)) { | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3529 | case OR_Success: { | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3530 | // We found a match. Perform the conversions on the arguments and move on. | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3531 | ExprResult LHSRes = | 
|  | 3532 | Self.PerformImplicitConversion(LHS.get(), Best->BuiltinTypes.ParamTypes[0], | 
|  | 3533 | Best->Conversions[0], Sema::AA_Converting); | 
|  | 3534 | if (LHSRes.isInvalid()) | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3535 | break; | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3536 | LHS = move(LHSRes); | 
|  | 3537 |  | 
|  | 3538 | ExprResult RHSRes = | 
|  | 3539 | Self.PerformImplicitConversion(RHS.get(), Best->BuiltinTypes.ParamTypes[1], | 
|  | 3540 | Best->Conversions[1], Sema::AA_Converting); | 
|  | 3541 | if (RHSRes.isInvalid()) | 
|  | 3542 | break; | 
|  | 3543 | RHS = move(RHSRes); | 
| Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3544 | if (Best->Function) | 
|  | 3545 | Self.MarkDeclarationReferenced(QuestionLoc, Best->Function); | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3546 | return false; | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3547 | } | 
|  | 3548 |  | 
| Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3549 | case OR_No_Viable_Function: | 
| Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 3550 |  | 
|  | 3551 | // Emit a better diagnostic if one of the expressions is a null pointer | 
|  | 3552 | // constant and the other is a pointer type. In this case, the user most | 
|  | 3553 | // likely forgot to take the address of the other expression. | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3554 | if (Self.DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc)) | 
| Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 3555 | return true; | 
|  | 3556 |  | 
|  | 3557 | Self.Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3558 | << LHS.get()->getType() << RHS.get()->getType() | 
|  | 3559 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3560 | return true; | 
|  | 3561 |  | 
| Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3562 | case OR_Ambiguous: | 
| Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 3563 | Self.Diag(QuestionLoc, diag::err_conditional_ambiguous_ovl) | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3564 | << LHS.get()->getType() << RHS.get()->getType() | 
|  | 3565 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); | 
| Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3566 | // FIXME: Print the possible common types by printing the return types of | 
|  | 3567 | // the viable candidates. | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3568 | break; | 
|  | 3569 |  | 
| Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3570 | case OR_Deleted: | 
| David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3571 | llvm_unreachable("Conditional operator has only built-in overloads"); | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3572 | } | 
|  | 3573 | return true; | 
|  | 3574 | } | 
|  | 3575 |  | 
| Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 3576 | /// \brief Perform an "extended" implicit conversion as returned by | 
|  | 3577 | /// TryClassUnification. | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3578 | static bool ConvertForConditional(Sema &Self, ExprResult &E, QualType T) { | 
| Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3579 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(T); | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3580 | InitializationKind Kind = InitializationKind::CreateCopy(E.get()->getLocStart(), | 
| Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3581 | SourceLocation()); | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3582 | Expr *Arg = E.take(); | 
|  | 3583 | InitializationSequence InitSeq(Self, Entity, Kind, &Arg, 1); | 
|  | 3584 | ExprResult Result = InitSeq.Perform(Self, Entity, Kind, MultiExprArg(&Arg, 1)); | 
| Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3585 | if (Result.isInvalid()) | 
| Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 3586 | return true; | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3587 |  | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3588 | E = Result; | 
| Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 3589 | return false; | 
|  | 3590 | } | 
|  | 3591 |  | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3592 | /// \brief Check the operands of ?: under C++ semantics. | 
|  | 3593 | /// | 
|  | 3594 | /// See C++ [expr.cond]. Note that LHS is never null, even for the GNU x ?: y | 
|  | 3595 | /// extension. In this case, LHS == Cond. (But they're not aliases.) | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3596 | QualType Sema::CXXCheckConditionalOperands(ExprResult &Cond, ExprResult &LHS, ExprResult &RHS, | 
| John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 3597 | ExprValueKind &VK, ExprObjectKind &OK, | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3598 | SourceLocation QuestionLoc) { | 
| Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3599 | // FIXME: Handle C99's complex types, vector types, block pointers and Obj-C++ | 
|  | 3600 | // interface pointers. | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3601 |  | 
|  | 3602 | // C++0x 5.16p1 | 
|  | 3603 | //   The first expression is contextually converted to bool. | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3604 | if (!Cond.get()->isTypeDependent()) { | 
|  | 3605 | ExprResult CondRes = CheckCXXBooleanCondition(Cond.take()); | 
|  | 3606 | if (CondRes.isInvalid()) | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3607 | return QualType(); | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3608 | Cond = move(CondRes); | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3609 | } | 
|  | 3610 |  | 
| John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3611 | // Assume r-value. | 
|  | 3612 | VK = VK_RValue; | 
| John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3613 | OK = OK_Ordinary; | 
| John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3614 |  | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3615 | // Either of the arguments dependent? | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3616 | if (LHS.get()->isTypeDependent() || RHS.get()->isTypeDependent()) | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3617 | return Context.DependentTy; | 
|  | 3618 |  | 
|  | 3619 | // C++0x 5.16p2 | 
|  | 3620 | //   If either the second or the third operand has type (cv) void, ... | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3621 | QualType LTy = LHS.get()->getType(); | 
|  | 3622 | QualType RTy = RHS.get()->getType(); | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3623 | bool LVoid = LTy->isVoidType(); | 
|  | 3624 | bool RVoid = RTy->isVoidType(); | 
|  | 3625 | if (LVoid || RVoid) { | 
|  | 3626 | //   ... then the [l2r] conversions are performed on the second and third | 
|  | 3627 | //   operands ... | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3628 | LHS = DefaultFunctionArrayLvalueConversion(LHS.take()); | 
|  | 3629 | RHS = DefaultFunctionArrayLvalueConversion(RHS.take()); | 
|  | 3630 | if (LHS.isInvalid() || RHS.isInvalid()) | 
|  | 3631 | return QualType(); | 
|  | 3632 | LTy = LHS.get()->getType(); | 
|  | 3633 | RTy = RHS.get()->getType(); | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3634 |  | 
|  | 3635 | //   ... and one of the following shall hold: | 
|  | 3636 | //   -- The second or the third operand (but not both) is a throw- | 
|  | 3637 | //      expression; the result is of the type of the other and is an rvalue. | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3638 | bool LThrow = isa<CXXThrowExpr>(LHS.get()); | 
|  | 3639 | bool RThrow = isa<CXXThrowExpr>(RHS.get()); | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3640 | if (LThrow && !RThrow) | 
|  | 3641 | return RTy; | 
|  | 3642 | if (RThrow && !LThrow) | 
|  | 3643 | return LTy; | 
|  | 3644 |  | 
|  | 3645 | //   -- Both the second and third operands have type void; the result is of | 
|  | 3646 | //      type void and is an rvalue. | 
|  | 3647 | if (LVoid && RVoid) | 
|  | 3648 | return Context.VoidTy; | 
|  | 3649 |  | 
|  | 3650 | // Neither holds, error. | 
|  | 3651 | Diag(QuestionLoc, diag::err_conditional_void_nonvoid) | 
|  | 3652 | << (LVoid ? RTy : LTy) << (LVoid ? 0 : 1) | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3653 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3654 | return QualType(); | 
|  | 3655 | } | 
|  | 3656 |  | 
|  | 3657 | // Neither is void. | 
|  | 3658 |  | 
|  | 3659 | // C++0x 5.16p3 | 
|  | 3660 | //   Otherwise, if the second and third operand have different types, and | 
|  | 3661 | //   either has (cv) class type, and attempt is made to convert each of those | 
|  | 3662 | //   operands to the other. | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3663 | if (!Context.hasSameType(LTy, RTy) && | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3664 | (LTy->isRecordType() || RTy->isRecordType())) { | 
|  | 3665 | ImplicitConversionSequence ICSLeftToRight, ICSRightToLeft; | 
|  | 3666 | // These return true if a single direction is already ambiguous. | 
| Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 3667 | QualType L2RType, R2LType; | 
|  | 3668 | bool HaveL2R, HaveR2L; | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3669 | if (TryClassUnification(*this, LHS.get(), RHS.get(), QuestionLoc, HaveL2R, L2RType)) | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3670 | return QualType(); | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3671 | if (TryClassUnification(*this, RHS.get(), LHS.get(), QuestionLoc, HaveR2L, R2LType)) | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3672 | return QualType(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3673 |  | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3674 | //   If both can be converted, [...] the program is ill-formed. | 
|  | 3675 | if (HaveL2R && HaveR2L) { | 
|  | 3676 | Diag(QuestionLoc, diag::err_conditional_ambiguous) | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3677 | << LTy << RTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3678 | return QualType(); | 
|  | 3679 | } | 
|  | 3680 |  | 
|  | 3681 | //   If exactly one conversion is possible, that conversion is applied to | 
|  | 3682 | //   the chosen operand and the converted operands are used in place of the | 
|  | 3683 | //   original operands for the remainder of this section. | 
|  | 3684 | if (HaveL2R) { | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3685 | if (ConvertForConditional(*this, LHS, L2RType) || LHS.isInvalid()) | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3686 | return QualType(); | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3687 | LTy = LHS.get()->getType(); | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3688 | } else if (HaveR2L) { | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3689 | if (ConvertForConditional(*this, RHS, R2LType) || RHS.isInvalid()) | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3690 | return QualType(); | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3691 | RTy = RHS.get()->getType(); | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3692 | } | 
|  | 3693 | } | 
|  | 3694 |  | 
|  | 3695 | // C++0x 5.16p4 | 
| John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3696 | //   If the second and third operands are glvalues of the same value | 
|  | 3697 | //   category and have the same type, the result is of that type and | 
|  | 3698 | //   value category and it is a bit-field if the second or the third | 
|  | 3699 | //   operand is a bit-field, or if both are bit-fields. | 
| John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3700 | // We only extend this to bitfields, not to the crazy other kinds of | 
|  | 3701 | // l-values. | 
| Douglas Gregor | 1927b1f | 2010-04-01 22:47:07 +0000 | [diff] [blame] | 3702 | bool Same = Context.hasSameType(LTy, RTy); | 
| John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3703 | if (Same && | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3704 | LHS.get()->isGLValue() && | 
|  | 3705 | LHS.get()->getValueKind() == RHS.get()->getValueKind() && | 
|  | 3706 | LHS.get()->isOrdinaryOrBitFieldObject() && | 
|  | 3707 | RHS.get()->isOrdinaryOrBitFieldObject()) { | 
|  | 3708 | VK = LHS.get()->getValueKind(); | 
|  | 3709 | if (LHS.get()->getObjectKind() == OK_BitField || | 
|  | 3710 | RHS.get()->getObjectKind() == OK_BitField) | 
| John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3711 | OK = OK_BitField; | 
| John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3712 | return LTy; | 
| Fariborz Jahanian | 3911a1a | 2010-09-25 01:08:05 +0000 | [diff] [blame] | 3713 | } | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3714 |  | 
|  | 3715 | // C++0x 5.16p5 | 
|  | 3716 | //   Otherwise, the result is an rvalue. If the second and third operands | 
|  | 3717 | //   do not have the same type, and either has (cv) class type, ... | 
|  | 3718 | if (!Same && (LTy->isRecordType() || RTy->isRecordType())) { | 
|  | 3719 | //   ... overload resolution is used to determine the conversions (if any) | 
|  | 3720 | //   to be applied to the operands. If the overload resolution fails, the | 
|  | 3721 | //   program is ill-formed. | 
|  | 3722 | if (FindConditionalOverload(*this, LHS, RHS, QuestionLoc)) | 
|  | 3723 | return QualType(); | 
|  | 3724 | } | 
|  | 3725 |  | 
|  | 3726 | // C++0x 5.16p6 | 
|  | 3727 | //   LValue-to-rvalue, array-to-pointer, and function-to-pointer standard | 
|  | 3728 | //   conversions are performed on the second and third operands. | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3729 | LHS = DefaultFunctionArrayLvalueConversion(LHS.take()); | 
|  | 3730 | RHS = DefaultFunctionArrayLvalueConversion(RHS.take()); | 
|  | 3731 | if (LHS.isInvalid() || RHS.isInvalid()) | 
|  | 3732 | return QualType(); | 
|  | 3733 | LTy = LHS.get()->getType(); | 
|  | 3734 | RTy = RHS.get()->getType(); | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3735 |  | 
|  | 3736 | //   After those conversions, one of the following shall hold: | 
|  | 3737 | //   -- The second and third operands have the same type; the result | 
| Douglas Gregor | b65a458 | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 3738 | //      is of that type. If the operands have class type, the result | 
|  | 3739 | //      is a prvalue temporary of the result type, which is | 
|  | 3740 | //      copy-initialized from either the second operand or the third | 
|  | 3741 | //      operand depending on the value of the first operand. | 
|  | 3742 | if (Context.getCanonicalType(LTy) == Context.getCanonicalType(RTy)) { | 
|  | 3743 | if (LTy->isRecordType()) { | 
|  | 3744 | // The operands have class type. Make a temporary copy. | 
|  | 3745 | InitializedEntity Entity = InitializedEntity::InitializeTemporary(LTy); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3746 | ExprResult LHSCopy = PerformCopyInitialization(Entity, | 
|  | 3747 | SourceLocation(), | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3748 | LHS); | 
| Douglas Gregor | b65a458 | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 3749 | if (LHSCopy.isInvalid()) | 
|  | 3750 | return QualType(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3751 |  | 
|  | 3752 | ExprResult RHSCopy = PerformCopyInitialization(Entity, | 
|  | 3753 | SourceLocation(), | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3754 | RHS); | 
| Douglas Gregor | b65a458 | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 3755 | if (RHSCopy.isInvalid()) | 
|  | 3756 | return QualType(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3757 |  | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3758 | LHS = LHSCopy; | 
|  | 3759 | RHS = RHSCopy; | 
| Douglas Gregor | b65a458 | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 3760 | } | 
|  | 3761 |  | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3762 | return LTy; | 
| Douglas Gregor | b65a458 | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 3763 | } | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3764 |  | 
| Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 3765 | // Extension: conditional operator involving vector types. | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3766 | if (LTy->isVectorType() || RTy->isVectorType()) | 
| Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 3767 | return CheckVectorOperands(LHS, RHS, QuestionLoc, /*isCompAssign*/false); | 
| Douglas Gregor | fb4a543 | 2010-05-18 22:42:18 +0000 | [diff] [blame] | 3768 |  | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3769 | //   -- The second and third operands have arithmetic or enumeration type; | 
|  | 3770 | //      the usual arithmetic conversions are performed to bring them to a | 
|  | 3771 | //      common type, and the result is of that type. | 
|  | 3772 | if (LTy->isArithmeticType() && RTy->isArithmeticType()) { | 
|  | 3773 | UsualArithmeticConversions(LHS, RHS); | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3774 | if (LHS.isInvalid() || RHS.isInvalid()) | 
|  | 3775 | return QualType(); | 
|  | 3776 | return LHS.get()->getType(); | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3777 | } | 
|  | 3778 |  | 
|  | 3779 | //   -- The second and third operands have pointer type, or one has pointer | 
|  | 3780 | //      type and the other is a null pointer constant; pointer conversions | 
|  | 3781 | //      and qualification conversions are performed to bring them to their | 
|  | 3782 | //      composite pointer type. The result is of the composite pointer type. | 
| Eli Friedman | de8ac49 | 2010-01-02 22:56:07 +0000 | [diff] [blame] | 3783 | //   -- The second and third operands have pointer to member type, or one has | 
|  | 3784 | //      pointer to member type and the other is a null pointer constant; | 
|  | 3785 | //      pointer to member conversions and qualification conversions are | 
|  | 3786 | //      performed to bring them to a common type, whose cv-qualification | 
|  | 3787 | //      shall match the cv-qualification of either the second or the third | 
|  | 3788 | //      operand. The result is of the common type. | 
| Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3789 | bool NonStandardCompositeType = false; | 
| Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 3790 | QualType Composite = FindCompositePointerType(QuestionLoc, LHS, RHS, | 
| Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3791 | isSFINAEContext()? 0 : &NonStandardCompositeType); | 
|  | 3792 | if (!Composite.isNull()) { | 
|  | 3793 | if (NonStandardCompositeType) | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3794 | Diag(QuestionLoc, | 
| Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3795 | diag::ext_typecheck_cond_incompatible_operands_nonstandard) | 
|  | 3796 | << LTy << RTy << Composite | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3797 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3798 |  | 
| Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 3799 | return Composite; | 
| Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3800 | } | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3801 |  | 
| Douglas Gregor | 1927b1f | 2010-04-01 22:47:07 +0000 | [diff] [blame] | 3802 | // Similarly, attempt to find composite type of two objective-c pointers. | 
| Fariborz Jahanian | 5501636 | 2009-12-10 20:46:08 +0000 | [diff] [blame] | 3803 | Composite = FindCompositeObjCPointerType(LHS, RHS, QuestionLoc); | 
|  | 3804 | if (!Composite.isNull()) | 
|  | 3805 | return Composite; | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3806 |  | 
| Chandler Carruth | 7ef9324 | 2011-02-19 00:13:59 +0000 | [diff] [blame] | 3807 | // Check if we are using a null with a non-pointer type. | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3808 | if (DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc)) | 
| Chandler Carruth | 7ef9324 | 2011-02-19 00:13:59 +0000 | [diff] [blame] | 3809 | return QualType(); | 
|  | 3810 |  | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3811 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3812 | << LHS.get()->getType() << RHS.get()->getType() | 
|  | 3813 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); | 
| Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3814 | return QualType(); | 
|  | 3815 | } | 
| Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 3816 |  | 
|  | 3817 | /// \brief Find a merged pointer type and convert the two expressions to it. | 
|  | 3818 | /// | 
| Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3819 | /// This finds the composite pointer type (or member pointer type) for @p E1 | 
|  | 3820 | /// and @p E2 according to C++0x 5.9p2. It converts both expressions to this | 
|  | 3821 | /// type and returns it. | 
| Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 3822 | /// It does not emit diagnostics. | 
| Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3823 | /// | 
| Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 3824 | /// \param Loc The location of the operator requiring these two expressions to | 
|  | 3825 | /// be converted to the composite pointer type. | 
|  | 3826 | /// | 
| Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3827 | /// If \p NonStandardCompositeType is non-NULL, then we are permitted to find | 
|  | 3828 | /// a non-standard (but still sane) composite type to which both expressions | 
|  | 3829 | /// can be converted. When such a type is chosen, \c *NonStandardCompositeType | 
|  | 3830 | /// will be set true. | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3831 | QualType Sema::FindCompositePointerType(SourceLocation Loc, | 
| Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 3832 | Expr *&E1, Expr *&E2, | 
| Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3833 | bool *NonStandardCompositeType) { | 
|  | 3834 | if (NonStandardCompositeType) | 
|  | 3835 | *NonStandardCompositeType = false; | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3836 |  | 
| Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 3837 | assert(getLangOptions().CPlusPlus && "This function assumes C++"); | 
|  | 3838 | QualType T1 = E1->getType(), T2 = E2->getType(); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3839 |  | 
| Fariborz Jahanian | 0cedfbd | 2009-12-08 20:04:24 +0000 | [diff] [blame] | 3840 | if (!T1->isAnyPointerType() && !T1->isMemberPointerType() && | 
|  | 3841 | !T2->isAnyPointerType() && !T2->isMemberPointerType()) | 
| Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3842 | return QualType(); | 
| Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 3843 |  | 
|  | 3844 | // C++0x 5.9p2 | 
|  | 3845 | //   Pointer conversions and qualification conversions are performed on | 
|  | 3846 | //   pointer operands to bring them to their composite pointer type. If | 
|  | 3847 | //   one operand is a null pointer constant, the composite pointer type is | 
|  | 3848 | //   the type of the other operand. | 
| Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 3849 | if (E1->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { | 
| Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3850 | if (T2->isMemberPointerType()) | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3851 | E1 = ImpCastExprToType(E1, T2, CK_NullToMemberPointer).take(); | 
| Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3852 | else | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3853 | E1 = ImpCastExprToType(E1, T2, CK_NullToPointer).take(); | 
| Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 3854 | return T2; | 
|  | 3855 | } | 
| Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 3856 | if (E2->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { | 
| Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3857 | if (T1->isMemberPointerType()) | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3858 | E2 = ImpCastExprToType(E2, T1, CK_NullToMemberPointer).take(); | 
| Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3859 | else | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3860 | E2 = ImpCastExprToType(E2, T1, CK_NullToPointer).take(); | 
| Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 3861 | return T1; | 
|  | 3862 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3863 |  | 
| Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3864 | // Now both have to be pointers or member pointers. | 
| Sebastian Redl | a439e6f | 2009-11-16 21:03:45 +0000 | [diff] [blame] | 3865 | if ((!T1->isPointerType() && !T1->isMemberPointerType()) || | 
|  | 3866 | (!T2->isPointerType() && !T2->isMemberPointerType())) | 
| Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 3867 | return QualType(); | 
|  | 3868 |  | 
|  | 3869 | //   Otherwise, of one of the operands has type "pointer to cv1 void," then | 
|  | 3870 | //   the other has type "pointer to cv2 T" and the composite pointer type is | 
|  | 3871 | //   "pointer to cv12 void," where cv12 is the union of cv1 and cv2. | 
|  | 3872 | //   Otherwise, the composite pointer type is a pointer type similar to the | 
|  | 3873 | //   type of one of the operands, with a cv-qualification signature that is | 
|  | 3874 | //   the union of the cv-qualification signatures of the operand types. | 
|  | 3875 | // In practice, the first part here is redundant; it's subsumed by the second. | 
|  | 3876 | // What we do here is, we build the two possible composite types, and try the | 
|  | 3877 | // conversions in both directions. If only one works, or if the two composite | 
|  | 3878 | // types are the same, we have succeeded. | 
| John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3879 | // FIXME: extended qualifiers? | 
| Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3880 | typedef SmallVector<unsigned, 4> QualifierVector; | 
| Sebastian Redl | a439e6f | 2009-11-16 21:03:45 +0000 | [diff] [blame] | 3881 | QualifierVector QualifierUnion; | 
| Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3882 | typedef SmallVector<std::pair<const Type *, const Type *>, 4> | 
| Sebastian Redl | a439e6f | 2009-11-16 21:03:45 +0000 | [diff] [blame] | 3883 | ContainingClassVector; | 
|  | 3884 | ContainingClassVector MemberOfClass; | 
|  | 3885 | QualType Composite1 = Context.getCanonicalType(T1), | 
|  | 3886 | Composite2 = Context.getCanonicalType(T2); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3887 | unsigned NeedConstBefore = 0; | 
| Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3888 | do { | 
|  | 3889 | const PointerType *Ptr1, *Ptr2; | 
|  | 3890 | if ((Ptr1 = Composite1->getAs<PointerType>()) && | 
|  | 3891 | (Ptr2 = Composite2->getAs<PointerType>())) { | 
|  | 3892 | Composite1 = Ptr1->getPointeeType(); | 
|  | 3893 | Composite2 = Ptr2->getPointeeType(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3894 |  | 
| Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3895 | // 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] | 3896 | // of where we need to fill in additional 'const' qualifiers. | 
| Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3897 | if (NonStandardCompositeType && | 
|  | 3898 | Composite1.getCVRQualifiers() != Composite2.getCVRQualifiers()) | 
|  | 3899 | NeedConstBefore = QualifierUnion.size(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3900 |  | 
| Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3901 | QualifierUnion.push_back( | 
|  | 3902 | Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers()); | 
|  | 3903 | MemberOfClass.push_back(std::make_pair((const Type *)0, (const Type *)0)); | 
|  | 3904 | continue; | 
|  | 3905 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3906 |  | 
| Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3907 | const MemberPointerType *MemPtr1, *MemPtr2; | 
|  | 3908 | if ((MemPtr1 = Composite1->getAs<MemberPointerType>()) && | 
|  | 3909 | (MemPtr2 = Composite2->getAs<MemberPointerType>())) { | 
|  | 3910 | Composite1 = MemPtr1->getPointeeType(); | 
|  | 3911 | Composite2 = MemPtr2->getPointeeType(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3912 |  | 
| Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3913 | // 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] | 3914 | // of where we need to fill in additional 'const' qualifiers. | 
| Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3915 | if (NonStandardCompositeType && | 
|  | 3916 | Composite1.getCVRQualifiers() != Composite2.getCVRQualifiers()) | 
|  | 3917 | NeedConstBefore = QualifierUnion.size(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3918 |  | 
| Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3919 | QualifierUnion.push_back( | 
|  | 3920 | Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers()); | 
|  | 3921 | MemberOfClass.push_back(std::make_pair(MemPtr1->getClass(), | 
|  | 3922 | MemPtr2->getClass())); | 
|  | 3923 | continue; | 
|  | 3924 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3925 |  | 
| Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3926 | // FIXME: block pointer types? | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3927 |  | 
| Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3928 | // Cannot unwrap any more types. | 
|  | 3929 | break; | 
|  | 3930 | } while (true); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3931 |  | 
| Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3932 | if (NeedConstBefore && NonStandardCompositeType) { | 
|  | 3933 | // Extension: Add 'const' to qualifiers that come before the first qualifier | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3934 | // mismatch, so that our (non-standard!) composite type meets the | 
| Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 3935 | // requirements of C++ [conv.qual]p4 bullet 3. | 
|  | 3936 | for (unsigned I = 0; I != NeedConstBefore; ++I) { | 
|  | 3937 | if ((QualifierUnion[I] & Qualifiers::Const) == 0) { | 
|  | 3938 | QualifierUnion[I] = QualifierUnion[I] | Qualifiers::Const; | 
|  | 3939 | *NonStandardCompositeType = true; | 
|  | 3940 | } | 
|  | 3941 | } | 
|  | 3942 | } | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3943 |  | 
| Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3944 | // Rewrap the composites as pointers or member pointers with the union CVRs. | 
| Sebastian Redl | a439e6f | 2009-11-16 21:03:45 +0000 | [diff] [blame] | 3945 | ContainingClassVector::reverse_iterator MOC | 
|  | 3946 | = MemberOfClass.rbegin(); | 
|  | 3947 | for (QualifierVector::reverse_iterator | 
|  | 3948 | I = QualifierUnion.rbegin(), | 
|  | 3949 | E = QualifierUnion.rend(); | 
| Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3950 | I != E; (void)++I, ++MOC) { | 
| John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3951 | Qualifiers Quals = Qualifiers::fromCVRMask(*I); | 
| Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3952 | if (MOC->first && MOC->second) { | 
|  | 3953 | // Rebuild member pointer type | 
| John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3954 | Composite1 = Context.getMemberPointerType( | 
|  | 3955 | Context.getQualifiedType(Composite1, Quals), | 
|  | 3956 | MOC->first); | 
|  | 3957 | Composite2 = Context.getMemberPointerType( | 
|  | 3958 | Context.getQualifiedType(Composite2, Quals), | 
|  | 3959 | MOC->second); | 
| Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3960 | } else { | 
|  | 3961 | // Rebuild pointer type | 
| John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3962 | Composite1 | 
|  | 3963 | = Context.getPointerType(Context.getQualifiedType(Composite1, Quals)); | 
|  | 3964 | Composite2 | 
|  | 3965 | = Context.getPointerType(Context.getQualifiedType(Composite2, Quals)); | 
| Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 3966 | } | 
| Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 3967 | } | 
|  | 3968 |  | 
| Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 3969 | // Try to convert to the first composite pointer type. | 
|  | 3970 | InitializedEntity Entity1 | 
|  | 3971 | = InitializedEntity::InitializeTemporary(Composite1); | 
|  | 3972 | InitializationKind Kind | 
|  | 3973 | = InitializationKind::CreateCopy(Loc, SourceLocation()); | 
|  | 3974 | InitializationSequence E1ToC1(*this, Entity1, Kind, &E1, 1); | 
|  | 3975 | InitializationSequence E2ToC1(*this, Entity1, Kind, &E2, 1); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3976 |  | 
| Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 3977 | if (E1ToC1 && E2ToC1) { | 
|  | 3978 | // Conversion to Composite1 is viable. | 
|  | 3979 | if (!Context.hasSameType(Composite1, Composite2)) { | 
|  | 3980 | // Composite2 is a different type from Composite1. Check whether | 
|  | 3981 | // Composite2 is also viable. | 
|  | 3982 | InitializedEntity Entity2 | 
|  | 3983 | = InitializedEntity::InitializeTemporary(Composite2); | 
|  | 3984 | InitializationSequence E1ToC2(*this, Entity2, Kind, &E1, 1); | 
|  | 3985 | InitializationSequence E2ToC2(*this, Entity2, Kind, &E2, 1); | 
|  | 3986 | if (E1ToC2 && E2ToC2) { | 
|  | 3987 | // Both Composite1 and Composite2 are viable and are different; | 
|  | 3988 | // this is an ambiguity. | 
|  | 3989 | return QualType(); | 
|  | 3990 | } | 
|  | 3991 | } | 
|  | 3992 |  | 
|  | 3993 | // Convert E1 to Composite1 | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3994 | ExprResult E1Result | 
| John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 3995 | = E1ToC1.Perform(*this, Entity1, Kind, MultiExprArg(*this,&E1,1)); | 
| Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 3996 | if (E1Result.isInvalid()) | 
|  | 3997 | return QualType(); | 
|  | 3998 | E1 = E1Result.takeAs<Expr>(); | 
|  | 3999 |  | 
|  | 4000 | // Convert E2 to Composite1 | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4001 | ExprResult E2Result | 
| John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4002 | = E2ToC1.Perform(*this, Entity1, Kind, MultiExprArg(*this,&E2,1)); | 
| Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 4003 | if (E2Result.isInvalid()) | 
|  | 4004 | return QualType(); | 
|  | 4005 | E2 = E2Result.takeAs<Expr>(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4006 |  | 
| Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 4007 | return Composite1; | 
| Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 4008 | } | 
|  | 4009 |  | 
| Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 4010 | // Check whether Composite2 is viable. | 
|  | 4011 | InitializedEntity Entity2 | 
|  | 4012 | = InitializedEntity::InitializeTemporary(Composite2); | 
|  | 4013 | InitializationSequence E1ToC2(*this, Entity2, Kind, &E1, 1); | 
|  | 4014 | InitializationSequence E2ToC2(*this, Entity2, Kind, &E2, 1); | 
|  | 4015 | if (!E1ToC2 || !E2ToC2) | 
|  | 4016 | return QualType(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4017 |  | 
| Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 4018 | // Convert E1 to Composite2 | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4019 | ExprResult E1Result | 
| John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4020 | = E1ToC2.Perform(*this, Entity2, Kind, MultiExprArg(*this, &E1, 1)); | 
| Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 4021 | if (E1Result.isInvalid()) | 
|  | 4022 | return QualType(); | 
|  | 4023 | E1 = E1Result.takeAs<Expr>(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4024 |  | 
| Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 4025 | // Convert E2 to Composite2 | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4026 | ExprResult E2Result | 
| John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4027 | = E2ToC2.Perform(*this, Entity2, Kind, MultiExprArg(*this, &E2, 1)); | 
| Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 4028 | if (E2Result.isInvalid()) | 
|  | 4029 | return QualType(); | 
|  | 4030 | E2 = E2Result.takeAs<Expr>(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4031 |  | 
| Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 4032 | return Composite2; | 
| Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 4033 | } | 
| Anders Carlsson | 165a0a0 | 2009-05-17 18:41:29 +0000 | [diff] [blame] | 4034 |  | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4035 | ExprResult Sema::MaybeBindToTemporary(Expr *E) { | 
| Douglas Gregor | 19cc1c7 | 2010-11-01 21:10:29 +0000 | [diff] [blame] | 4036 | if (!E) | 
|  | 4037 | return ExprError(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4038 |  | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4039 | assert(!isa<CXXBindTemporaryExpr>(E) && "Double-bound temporary?"); | 
|  | 4040 |  | 
|  | 4041 | // If the result is a glvalue, we shouldn't bind it. | 
|  | 4042 | if (!E->isRValue()) | 
| Anders Carlsson | 089c260 | 2009-08-15 23:41:35 +0000 | [diff] [blame] | 4043 | return Owned(E); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4044 |  | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4045 | // In ARC, calls that return a retainable type can return retained, | 
|  | 4046 | // in which case we have to insert a consuming cast. | 
|  | 4047 | if (getLangOptions().ObjCAutoRefCount && | 
|  | 4048 | E->getType()->isObjCRetainableType()) { | 
|  | 4049 |  | 
|  | 4050 | bool ReturnsRetained; | 
|  | 4051 |  | 
|  | 4052 | // For actual calls, we compute this by examining the type of the | 
|  | 4053 | // called value. | 
|  | 4054 | if (CallExpr *Call = dyn_cast<CallExpr>(E)) { | 
|  | 4055 | Expr *Callee = Call->getCallee()->IgnoreParens(); | 
|  | 4056 | QualType T = Callee->getType(); | 
|  | 4057 |  | 
|  | 4058 | if (T == Context.BoundMemberTy) { | 
|  | 4059 | // Handle pointer-to-members. | 
|  | 4060 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(Callee)) | 
|  | 4061 | T = BinOp->getRHS()->getType(); | 
|  | 4062 | else if (MemberExpr *Mem = dyn_cast<MemberExpr>(Callee)) | 
|  | 4063 | T = Mem->getMemberDecl()->getType(); | 
|  | 4064 | } | 
|  | 4065 |  | 
|  | 4066 | if (const PointerType *Ptr = T->getAs<PointerType>()) | 
|  | 4067 | T = Ptr->getPointeeType(); | 
|  | 4068 | else if (const BlockPointerType *Ptr = T->getAs<BlockPointerType>()) | 
|  | 4069 | T = Ptr->getPointeeType(); | 
|  | 4070 | else if (const MemberPointerType *MemPtr = T->getAs<MemberPointerType>()) | 
|  | 4071 | T = MemPtr->getPointeeType(); | 
|  | 4072 |  | 
|  | 4073 | const FunctionType *FTy = T->getAs<FunctionType>(); | 
|  | 4074 | assert(FTy && "call to value not of function type?"); | 
|  | 4075 | ReturnsRetained = FTy->getExtInfo().getProducesResult(); | 
|  | 4076 |  | 
|  | 4077 | // ActOnStmtExpr arranges things so that StmtExprs of retainable | 
|  | 4078 | // type always produce a +1 object. | 
|  | 4079 | } else if (isa<StmtExpr>(E)) { | 
|  | 4080 | ReturnsRetained = true; | 
|  | 4081 |  | 
|  | 4082 | // For message sends and property references, we try to find an | 
|  | 4083 | // actual method.  FIXME: we should infer retention by selector in | 
|  | 4084 | // cases where we don't have an actual method. | 
|  | 4085 | } else { | 
| John McCall | fc4b191 | 2011-08-03 07:02:44 +0000 | [diff] [blame] | 4086 | ObjCMethodDecl *D = 0; | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4087 | if (ObjCMessageExpr *Send = dyn_cast<ObjCMessageExpr>(E)) { | 
|  | 4088 | D = Send->getMethodDecl(); | 
|  | 4089 | } else { | 
|  | 4090 | CastExpr *CE = cast<CastExpr>(E); | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4091 | assert(CE->getCastKind() == CK_GetObjCProperty); | 
|  | 4092 | const ObjCPropertyRefExpr *PRE = CE->getSubExpr()->getObjCProperty(); | 
|  | 4093 | D = (PRE->isImplicitProperty() ? PRE->getImplicitPropertyGetter() : 0); | 
|  | 4094 | } | 
|  | 4095 |  | 
|  | 4096 | ReturnsRetained = (D && D->hasAttr<NSReturnsRetainedAttr>()); | 
| John McCall | fc4b191 | 2011-08-03 07:02:44 +0000 | [diff] [blame] | 4097 |  | 
|  | 4098 | // Don't do reclaims on performSelector calls; despite their | 
|  | 4099 | // return type, the invoked method doesn't necessarily actually | 
|  | 4100 | // return an object. | 
|  | 4101 | if (!ReturnsRetained && | 
|  | 4102 | D && D->getMethodFamily() == OMF_performSelector) | 
|  | 4103 | return Owned(E); | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4104 | } | 
|  | 4105 |  | 
| John McCall | 7e5e5f4 | 2011-07-07 06:58:02 +0000 | [diff] [blame] | 4106 | ExprNeedsCleanups = true; | 
|  | 4107 |  | 
| John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 4108 | CastKind ck = (ReturnsRetained ? CK_ARCConsumeObject | 
|  | 4109 | : CK_ARCReclaimReturnedObject); | 
| John McCall | 7e5e5f4 | 2011-07-07 06:58:02 +0000 | [diff] [blame] | 4110 | return Owned(ImplicitCastExpr::Create(Context, E->getType(), ck, E, 0, | 
|  | 4111 | VK_RValue)); | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4112 | } | 
|  | 4113 |  | 
|  | 4114 | if (!getLangOptions().CPlusPlus) | 
|  | 4115 | return Owned(E); | 
| Douglas Gregor | 5132655 | 2009-12-24 18:51:59 +0000 | [diff] [blame] | 4116 |  | 
| Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4117 | const RecordType *RT = E->getType()->getAs<RecordType>(); | 
| Anders Carlsson | def1199 | 2009-05-30 20:36:53 +0000 | [diff] [blame] | 4118 | if (!RT) | 
|  | 4119 | return Owned(E); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4120 |  | 
| John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 4121 | // That should be enough to guarantee that this type is complete. | 
|  | 4122 | // If it has a trivial destructor, we can avoid the extra copy. | 
| Jeffrey Yasskin | b7ee2e5 | 2011-01-27 19:17:54 +0000 | [diff] [blame] | 4123 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); | 
| John McCall | 507384f | 2010-08-12 02:40:37 +0000 | [diff] [blame] | 4124 | if (RD->isInvalidDecl() || RD->hasTrivialDestructor()) | 
| John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 4125 | return Owned(E); | 
|  | 4126 |  | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4127 | CXXDestructorDecl *Destructor = LookupDestructor(RD); | 
|  | 4128 |  | 
|  | 4129 | CXXTemporary *Temp = CXXTemporary::Create(Context, Destructor); | 
|  | 4130 | if (Destructor) { | 
| Fariborz Jahanian | a83f7ed | 2009-08-03 19:13:25 +0000 | [diff] [blame] | 4131 | MarkDeclarationReferenced(E->getExprLoc(), Destructor); | 
| John McCall | c91cc66 | 2010-04-07 00:41:46 +0000 | [diff] [blame] | 4132 | CheckDestructorAccess(E->getExprLoc(), Destructor, | 
|  | 4133 | PDiag(diag::err_access_dtor_temp) | 
|  | 4134 | << E->getType()); | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4135 |  | 
|  | 4136 | ExprTemporaries.push_back(Temp); | 
|  | 4137 | ExprNeedsCleanups = true; | 
| John McCall | c91cc66 | 2010-04-07 00:41:46 +0000 | [diff] [blame] | 4138 | } | 
| Anders Carlsson | def1199 | 2009-05-30 20:36:53 +0000 | [diff] [blame] | 4139 | return Owned(CXXBindTemporaryExpr::Create(Context, Temp, E)); | 
|  | 4140 | } | 
|  | 4141 |  | 
| John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 4142 | Expr *Sema::MaybeCreateExprWithCleanups(Expr *SubExpr) { | 
| Anders Carlsson | 99ba36d | 2009-06-05 15:38:08 +0000 | [diff] [blame] | 4143 | assert(SubExpr && "sub expression can't be null!"); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4144 |  | 
| Douglas Gregor | 1f5f3a4 | 2009-12-03 17:10:37 +0000 | [diff] [blame] | 4145 | unsigned FirstTemporary = ExprEvalContexts.back().NumTemporaries; | 
|  | 4146 | assert(ExprTemporaries.size() >= FirstTemporary); | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4147 | assert(ExprNeedsCleanups || ExprTemporaries.size() == FirstTemporary); | 
|  | 4148 | if (!ExprNeedsCleanups) | 
| Anders Carlsson | 99ba36d | 2009-06-05 15:38:08 +0000 | [diff] [blame] | 4149 | return SubExpr; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4150 |  | 
| John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 4151 | Expr *E = ExprWithCleanups::Create(Context, SubExpr, | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4152 | ExprTemporaries.begin() + FirstTemporary, | 
| John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 4153 | ExprTemporaries.size() - FirstTemporary); | 
| Douglas Gregor | 1f5f3a4 | 2009-12-03 17:10:37 +0000 | [diff] [blame] | 4154 | ExprTemporaries.erase(ExprTemporaries.begin() + FirstTemporary, | 
|  | 4155 | ExprTemporaries.end()); | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4156 | ExprNeedsCleanups = false; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4157 |  | 
| Anders Carlsson | 99ba36d | 2009-06-05 15:38:08 +0000 | [diff] [blame] | 4158 | return E; | 
|  | 4159 | } | 
|  | 4160 |  | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4161 | ExprResult | 
| John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 4162 | Sema::MaybeCreateExprWithCleanups(ExprResult SubExpr) { | 
| Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 4163 | if (SubExpr.isInvalid()) | 
|  | 4164 | return ExprError(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4165 |  | 
| John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 4166 | return Owned(MaybeCreateExprWithCleanups(SubExpr.take())); | 
| Douglas Gregor | 90f9382 | 2009-12-22 22:17:25 +0000 | [diff] [blame] | 4167 | } | 
|  | 4168 |  | 
| John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 4169 | Stmt *Sema::MaybeCreateStmtWithCleanups(Stmt *SubStmt) { | 
| Argyrios Kyrtzidis | bf8cafa | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 4170 | assert(SubStmt && "sub statement can't be null!"); | 
|  | 4171 |  | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4172 | if (!ExprNeedsCleanups) | 
| Argyrios Kyrtzidis | bf8cafa | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 4173 | return SubStmt; | 
|  | 4174 |  | 
|  | 4175 | // FIXME: In order to attach the temporaries, wrap the statement into | 
|  | 4176 | // a StmtExpr; currently this is only used for asm statements. | 
|  | 4177 | // This is hacky, either create a new CXXStmtWithTemporaries statement or | 
|  | 4178 | // a new AsmStmtWithTemporaries. | 
|  | 4179 | CompoundStmt *CompStmt = new (Context) CompoundStmt(Context, &SubStmt, 1, | 
|  | 4180 | SourceLocation(), | 
|  | 4181 | SourceLocation()); | 
|  | 4182 | Expr *E = new (Context) StmtExpr(CompStmt, Context.VoidTy, SourceLocation(), | 
|  | 4183 | SourceLocation()); | 
| John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 4184 | return MaybeCreateExprWithCleanups(E); | 
| Argyrios Kyrtzidis | bf8cafa | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 4185 | } | 
|  | 4186 |  | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4187 | ExprResult | 
| John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4188 | Sema::ActOnStartCXXMemberReference(Scope *S, Expr *Base, SourceLocation OpLoc, | 
| John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4189 | tok::TokenKind OpKind, ParsedType &ObjectType, | 
| Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 4190 | bool &MayBePseudoDestructor) { | 
| Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4191 | // Since this might be a postfix expression, get rid of ParenListExprs. | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4192 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); | 
| John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4193 | if (Result.isInvalid()) return ExprError(); | 
|  | 4194 | Base = Result.get(); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4195 |  | 
| John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4196 | QualType BaseType = Base->getType(); | 
| Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 4197 | MayBePseudoDestructor = false; | 
| Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4198 | if (BaseType->isDependentType()) { | 
| Douglas Gregor | 43d8863 | 2009-11-04 22:49:18 +0000 | [diff] [blame] | 4199 | // If we have a pointer to a dependent type and are using the -> operator, | 
|  | 4200 | // the object type is the type that the pointer points to. We might still | 
|  | 4201 | // have enough information about that type to do something useful. | 
|  | 4202 | if (OpKind == tok::arrow) | 
|  | 4203 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) | 
|  | 4204 | BaseType = Ptr->getPointeeType(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4205 |  | 
| John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4206 | ObjectType = ParsedType::make(BaseType); | 
| Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 4207 | MayBePseudoDestructor = true; | 
| John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4208 | return Owned(Base); | 
| Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4209 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4210 |  | 
| Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4211 | // C++ [over.match.oper]p8: | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4212 | //   [...] When operator->returns, the operator-> is applied  to the value | 
| Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4213 | //   returned, with the original second operand. | 
|  | 4214 | if (OpKind == tok::arrow) { | 
| John McCall | c4e8321 | 2009-09-30 01:01:30 +0000 | [diff] [blame] | 4215 | // The set of types we've considered so far. | 
| John McCall | 432887f | 2009-09-30 01:30:54 +0000 | [diff] [blame] | 4216 | llvm::SmallPtrSet<CanQualType,8> CTypes; | 
| Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4217 | SmallVector<SourceLocation, 8> Locations; | 
| John McCall | 432887f | 2009-09-30 01:30:54 +0000 | [diff] [blame] | 4218 | CTypes.insert(Context.getCanonicalType(BaseType)); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4219 |  | 
| Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4220 | while (BaseType->isRecordType()) { | 
| John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4221 | Result = BuildOverloadedArrowExpr(S, Base, OpLoc); | 
|  | 4222 | if (Result.isInvalid()) | 
| Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4223 | return ExprError(); | 
| John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4224 | Base = Result.get(); | 
|  | 4225 | if (CXXOperatorCallExpr *OpCall = dyn_cast<CXXOperatorCallExpr>(Base)) | 
| Anders Carlsson | de699e5 | 2009-10-13 22:55:59 +0000 | [diff] [blame] | 4226 | Locations.push_back(OpCall->getDirectCallee()->getLocation()); | 
| John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4227 | BaseType = Base->getType(); | 
| John McCall | c4e8321 | 2009-09-30 01:01:30 +0000 | [diff] [blame] | 4228 | CanQualType CBaseType = Context.getCanonicalType(BaseType); | 
| John McCall | 432887f | 2009-09-30 01:30:54 +0000 | [diff] [blame] | 4229 | if (!CTypes.insert(CBaseType)) { | 
| Fariborz Jahanian | 4a4e345 | 2009-09-30 00:19:41 +0000 | [diff] [blame] | 4230 | Diag(OpLoc, diag::err_operator_arrow_circular); | 
| Fariborz Jahanian | 7a8233a | 2009-09-30 17:46:20 +0000 | [diff] [blame] | 4231 | for (unsigned i = 0; i < Locations.size(); i++) | 
|  | 4232 | Diag(Locations[i], diag::note_declared_at); | 
| Fariborz Jahanian | 4a4e345 | 2009-09-30 00:19:41 +0000 | [diff] [blame] | 4233 | return ExprError(); | 
|  | 4234 | } | 
| Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4235 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4236 |  | 
| Douglas Gregor | 31658df | 2009-11-20 19:58:21 +0000 | [diff] [blame] | 4237 | if (BaseType->isPointerType()) | 
|  | 4238 | BaseType = BaseType->getPointeeType(); | 
|  | 4239 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4240 |  | 
|  | 4241 | // 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] | 4242 | // vector types or Objective-C interfaces. Just return early and let | 
|  | 4243 | // ActOnMemberReferenceExpr do the work. | 
| Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 4244 | if (!BaseType->isRecordType()) { | 
|  | 4245 | // C++ [basic.lookup.classref]p2: | 
|  | 4246 | //   [...] If the type of the object expression is of pointer to scalar | 
|  | 4247 | //   type, the unqualified-id is looked up in the context of the complete | 
|  | 4248 | //   postfix-expression. | 
| Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 4249 | // | 
|  | 4250 | // This also indicates that we should be parsing a | 
|  | 4251 | // pseudo-destructor-name. | 
| John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4252 | ObjectType = ParsedType(); | 
| Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 4253 | MayBePseudoDestructor = true; | 
| John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4254 | return Owned(Base); | 
| Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 4255 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4256 |  | 
| Douglas Gregor | 03c5705 | 2009-11-17 05:17:33 +0000 | [diff] [blame] | 4257 | // The object type must be complete (or dependent). | 
|  | 4258 | if (!BaseType->isDependentType() && | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4259 | RequireCompleteType(OpLoc, BaseType, | 
| Douglas Gregor | 03c5705 | 2009-11-17 05:17:33 +0000 | [diff] [blame] | 4260 | PDiag(diag::err_incomplete_member_access))) | 
|  | 4261 | return ExprError(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4262 |  | 
| Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 4263 | // C++ [basic.lookup.classref]p2: | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4264 | //   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] | 4265 | //   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] | 4266 | //   type C (or of pointer to a class type C), the unqualified-id is looked | 
|  | 4267 | //   up in the scope of class C. [...] | 
| John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4268 | ObjectType = ParsedType::make(BaseType); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4269 | return move(Base); | 
| Douglas Gregor | 2dd078a | 2009-09-02 22:59:36 +0000 | [diff] [blame] | 4270 | } | 
|  | 4271 |  | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4272 | ExprResult Sema::DiagnoseDtorReference(SourceLocation NameLoc, | 
| John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4273 | Expr *MemExpr) { | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4274 | SourceLocation ExpectedLParenLoc = PP.getLocForEndOfToken(NameLoc); | 
| John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4275 | Diag(MemExpr->getLocStart(), diag::err_dtor_expr_without_call) | 
|  | 4276 | << isa<CXXPseudoDestructorExpr>(MemExpr) | 
| Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4277 | << FixItHint::CreateInsertion(ExpectedLParenLoc, "()"); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4278 |  | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4279 | return ActOnCallExpr(/*Scope*/ 0, | 
| John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4280 | MemExpr, | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4281 | /*LPLoc*/ ExpectedLParenLoc, | 
| John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4282 | MultiExprArg(), | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4283 | /*RPLoc*/ ExpectedLParenLoc); | 
|  | 4284 | } | 
| Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 4285 |  | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4286 | ExprResult Sema::BuildPseudoDestructorExpr(Expr *Base, | 
| John McCall | 2d9f5fa | 2011-02-25 05:21:17 +0000 | [diff] [blame] | 4287 | SourceLocation OpLoc, | 
|  | 4288 | tok::TokenKind OpKind, | 
|  | 4289 | const CXXScopeSpec &SS, | 
|  | 4290 | TypeSourceInfo *ScopeTypeInfo, | 
|  | 4291 | SourceLocation CCLoc, | 
|  | 4292 | SourceLocation TildeLoc, | 
| Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4293 | PseudoDestructorTypeStorage Destructed, | 
| John McCall | 2d9f5fa | 2011-02-25 05:21:17 +0000 | [diff] [blame] | 4294 | bool HasTrailingLParen) { | 
| Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4295 | TypeSourceInfo *DestructedTypeInfo = Destructed.getTypeSourceInfo(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4296 |  | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4297 | // C++ [expr.pseudo]p2: | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4298 | //   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] | 4299 | //   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] | 4300 | //   This scalar type is the object type. | 
| John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4301 | QualType ObjectType = Base->getType(); | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4302 | if (OpKind == tok::arrow) { | 
|  | 4303 | if (const PointerType *Ptr = ObjectType->getAs<PointerType>()) { | 
|  | 4304 | ObjectType = Ptr->getPointeeType(); | 
| John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4305 | } else if (!Base->isTypeDependent()) { | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4306 | // The user wrote "p->" when she probably meant "p."; fix it. | 
|  | 4307 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) | 
|  | 4308 | << ObjectType << true | 
| Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4309 | << FixItHint::CreateReplacement(OpLoc, "."); | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4310 | if (isSFINAEContext()) | 
|  | 4311 | return ExprError(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4312 |  | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4313 | OpKind = tok::period; | 
|  | 4314 | } | 
|  | 4315 | } | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4316 |  | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4317 | if (!ObjectType->isDependentType() && !ObjectType->isScalarType()) { | 
|  | 4318 | Diag(OpLoc, diag::err_pseudo_dtor_base_not_scalar) | 
| John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4319 | << ObjectType << Base->getSourceRange(); | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4320 | return ExprError(); | 
|  | 4321 | } | 
|  | 4322 |  | 
|  | 4323 | // C++ [expr.pseudo]p2: | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4324 | //   [...] The cv-unqualified versions of the object type and of the type | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4325 | //   designated by the pseudo-destructor-name shall be the same type. | 
| Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4326 | if (DestructedTypeInfo) { | 
|  | 4327 | QualType DestructedType = DestructedTypeInfo->getType(); | 
|  | 4328 | SourceLocation DestructedTypeStart | 
| Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 4329 | = DestructedTypeInfo->getTypeLoc().getLocalSourceRange().getBegin(); | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4330 | if (!DestructedType->isDependentType() && !ObjectType->isDependentType()) { | 
|  | 4331 | if (!Context.hasSameUnqualifiedType(DestructedType, ObjectType)) { | 
|  | 4332 | Diag(DestructedTypeStart, diag::err_pseudo_dtor_type_mismatch) | 
|  | 4333 | << ObjectType << DestructedType << Base->getSourceRange() | 
|  | 4334 | << DestructedTypeInfo->getTypeLoc().getLocalSourceRange(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4335 |  | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4336 | // Recover by setting the destructed type to the object type. | 
|  | 4337 | DestructedType = ObjectType; | 
|  | 4338 | DestructedTypeInfo = Context.getTrivialTypeSourceInfo(ObjectType, | 
| Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4339 | DestructedTypeStart); | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4340 | Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo); | 
|  | 4341 | } else if (DestructedType.getObjCLifetime() != | 
|  | 4342 | ObjectType.getObjCLifetime()) { | 
|  | 4343 |  | 
|  | 4344 | if (DestructedType.getObjCLifetime() == Qualifiers::OCL_None) { | 
|  | 4345 | // Okay: just pretend that the user provided the correctly-qualified | 
|  | 4346 | // type. | 
|  | 4347 | } else { | 
|  | 4348 | Diag(DestructedTypeStart, diag::err_arc_pseudo_dtor_inconstant_quals) | 
|  | 4349 | << ObjectType << DestructedType << Base->getSourceRange() | 
|  | 4350 | << DestructedTypeInfo->getTypeLoc().getLocalSourceRange(); | 
|  | 4351 | } | 
|  | 4352 |  | 
|  | 4353 | // Recover by setting the destructed type to the object type. | 
|  | 4354 | DestructedType = ObjectType; | 
|  | 4355 | DestructedTypeInfo = Context.getTrivialTypeSourceInfo(ObjectType, | 
|  | 4356 | DestructedTypeStart); | 
|  | 4357 | Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo); | 
|  | 4358 | } | 
| Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4359 | } | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4360 | } | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4361 |  | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4362 | // C++ [expr.pseudo]p2: | 
|  | 4363 | //   [...] Furthermore, the two type-names in a pseudo-destructor-name of the | 
|  | 4364 | //   form | 
|  | 4365 | // | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4366 | //     ::[opt] nested-name-specifier[opt] type-name :: ~ type-name | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4367 | // | 
|  | 4368 | //   shall designate the same scalar type. | 
|  | 4369 | if (ScopeTypeInfo) { | 
|  | 4370 | QualType ScopeType = ScopeTypeInfo->getType(); | 
|  | 4371 | if (!ScopeType->isDependentType() && !ObjectType->isDependentType() && | 
| John McCall | 81e317a | 2010-06-11 17:36:40 +0000 | [diff] [blame] | 4372 | !Context.hasSameUnqualifiedType(ScopeType, ObjectType)) { | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4373 |  | 
| Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 4374 | Diag(ScopeTypeInfo->getTypeLoc().getLocalSourceRange().getBegin(), | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4375 | diag::err_pseudo_dtor_type_mismatch) | 
| John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4376 | << ObjectType << ScopeType << Base->getSourceRange() | 
| Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 4377 | << ScopeTypeInfo->getTypeLoc().getLocalSourceRange(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4378 |  | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4379 | ScopeType = QualType(); | 
|  | 4380 | ScopeTypeInfo = 0; | 
|  | 4381 | } | 
|  | 4382 | } | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4383 |  | 
| John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4384 | Expr *Result | 
|  | 4385 | = new (Context) CXXPseudoDestructorExpr(Context, Base, | 
|  | 4386 | OpKind == tok::arrow, OpLoc, | 
| Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 4387 | SS.getWithLocInContext(Context), | 
| John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4388 | ScopeTypeInfo, | 
|  | 4389 | CCLoc, | 
|  | 4390 | TildeLoc, | 
|  | 4391 | Destructed); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4392 |  | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4393 | if (HasTrailingLParen) | 
| John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4394 | return Owned(Result); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4395 |  | 
| John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4396 | return DiagnoseDtorReference(Destructed.getLocation(), Result); | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4397 | } | 
|  | 4398 |  | 
| John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4399 | ExprResult Sema::ActOnPseudoDestructorExpr(Scope *S, Expr *Base, | 
| John McCall | 2d9f5fa | 2011-02-25 05:21:17 +0000 | [diff] [blame] | 4400 | SourceLocation OpLoc, | 
|  | 4401 | tok::TokenKind OpKind, | 
|  | 4402 | CXXScopeSpec &SS, | 
|  | 4403 | UnqualifiedId &FirstTypeName, | 
|  | 4404 | SourceLocation CCLoc, | 
|  | 4405 | SourceLocation TildeLoc, | 
|  | 4406 | UnqualifiedId &SecondTypeName, | 
|  | 4407 | bool HasTrailingLParen) { | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4408 | assert((FirstTypeName.getKind() == UnqualifiedId::IK_TemplateId || | 
|  | 4409 | FirstTypeName.getKind() == UnqualifiedId::IK_Identifier) && | 
|  | 4410 | "Invalid first type name in pseudo-destructor"); | 
|  | 4411 | assert((SecondTypeName.getKind() == UnqualifiedId::IK_TemplateId || | 
|  | 4412 | SecondTypeName.getKind() == UnqualifiedId::IK_Identifier) && | 
|  | 4413 | "Invalid second type name in pseudo-destructor"); | 
|  | 4414 |  | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4415 | // C++ [expr.pseudo]p2: | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4416 | //   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] | 4417 | //   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] | 4418 | //   This scalar type is the object type. | 
| John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4419 | QualType ObjectType = Base->getType(); | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4420 | if (OpKind == tok::arrow) { | 
|  | 4421 | if (const PointerType *Ptr = ObjectType->getAs<PointerType>()) { | 
|  | 4422 | ObjectType = Ptr->getPointeeType(); | 
| Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4423 | } else if (!ObjectType->isDependentType()) { | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4424 | // The user wrote "p->" when she probably meant "p."; fix it. | 
|  | 4425 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) | 
| Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4426 | << ObjectType << true | 
| Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4427 | << FixItHint::CreateReplacement(OpLoc, "."); | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4428 | if (isSFINAEContext()) | 
|  | 4429 | return ExprError(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4430 |  | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4431 | OpKind = tok::period; | 
|  | 4432 | } | 
|  | 4433 | } | 
| Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4434 |  | 
|  | 4435 | // Compute the object type that we should use for name lookup purposes. Only | 
|  | 4436 | // record types and dependent types matter. | 
| John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4437 | ParsedType ObjectTypePtrForLookup; | 
| Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4438 | if (!SS.isSet()) { | 
| John McCall | 2d9f5fa | 2011-02-25 05:21:17 +0000 | [diff] [blame] | 4439 | if (ObjectType->isRecordType()) | 
|  | 4440 | ObjectTypePtrForLookup = ParsedType::make(ObjectType); | 
| John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4441 | else if (ObjectType->isDependentType()) | 
|  | 4442 | ObjectTypePtrForLookup = ParsedType::make(Context.DependentTy); | 
| Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4443 | } | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4444 |  | 
|  | 4445 | // Convert the name of the type being destructed (following the ~) into a | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4446 | // type (with source-location information). | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4447 | QualType DestructedType; | 
|  | 4448 | TypeSourceInfo *DestructedTypeInfo = 0; | 
| Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4449 | PseudoDestructorTypeStorage Destructed; | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4450 | if (SecondTypeName.getKind() == UnqualifiedId::IK_Identifier) { | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4451 | ParsedType T = getTypeName(*SecondTypeName.Identifier, | 
| John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4452 | SecondTypeName.StartLocation, | 
| Fariborz Jahanian | 1e52dfc | 2011-02-08 18:05:59 +0000 | [diff] [blame] | 4453 | S, &SS, true, false, ObjectTypePtrForLookup); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4454 | if (!T && | 
| Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4455 | ((SS.isSet() && !computeDeclContext(SS, false)) || | 
|  | 4456 | (!SS.isSet() && ObjectType->isDependentType()))) { | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4457 | // 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] | 4458 | // couldn't find anything useful in scope. Just store the identifier and | 
|  | 4459 | // it's location, and we'll perform (qualified) name lookup again at | 
|  | 4460 | // template instantiation time. | 
|  | 4461 | Destructed = PseudoDestructorTypeStorage(SecondTypeName.Identifier, | 
|  | 4462 | SecondTypeName.StartLocation); | 
|  | 4463 | } else if (!T) { | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4464 | Diag(SecondTypeName.StartLocation, | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4465 | diag::err_pseudo_dtor_destructor_non_type) | 
|  | 4466 | << SecondTypeName.Identifier << ObjectType; | 
|  | 4467 | if (isSFINAEContext()) | 
|  | 4468 | return ExprError(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4469 |  | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4470 | // Recover by assuming we had the right type all along. | 
|  | 4471 | DestructedType = ObjectType; | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4472 | } else | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4473 | DestructedType = GetTypeFromParser(T, &DestructedTypeInfo); | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4474 | } else { | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4475 | // Resolve the template-id to a type. | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4476 | TemplateIdAnnotation *TemplateId = SecondTypeName.TemplateId; | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4477 | ASTTemplateArgsPtr TemplateArgsPtr(*this, | 
|  | 4478 | TemplateId->getTemplateArgs(), | 
|  | 4479 | TemplateId->NumArgs); | 
| Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 4480 | TypeResult T = ActOnTemplateIdType(TemplateId->SS, | 
|  | 4481 | TemplateId->Template, | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4482 | TemplateId->TemplateNameLoc, | 
|  | 4483 | TemplateId->LAngleLoc, | 
|  | 4484 | TemplateArgsPtr, | 
|  | 4485 | TemplateId->RAngleLoc); | 
|  | 4486 | if (T.isInvalid() || !T.get()) { | 
|  | 4487 | // Recover by assuming we had the right type all along. | 
|  | 4488 | DestructedType = ObjectType; | 
|  | 4489 | } else | 
|  | 4490 | DestructedType = GetTypeFromParser(T.get(), &DestructedTypeInfo); | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4491 | } | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4492 |  | 
|  | 4493 | // 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] | 4494 | // information. | 
| Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4495 | if (!DestructedType.isNull()) { | 
|  | 4496 | if (!DestructedTypeInfo) | 
|  | 4497 | DestructedTypeInfo = Context.getTrivialTypeSourceInfo(DestructedType, | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4498 | SecondTypeName.StartLocation); | 
| Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4499 | Destructed = PseudoDestructorTypeStorage(DestructedTypeInfo); | 
|  | 4500 | } | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4501 |  | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4502 | // Convert the name of the scope type (the type prior to '::') into a type. | 
|  | 4503 | TypeSourceInfo *ScopeTypeInfo = 0; | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4504 | QualType ScopeType; | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4505 | if (FirstTypeName.getKind() == UnqualifiedId::IK_TemplateId || | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4506 | FirstTypeName.Identifier) { | 
|  | 4507 | if (FirstTypeName.getKind() == UnqualifiedId::IK_Identifier) { | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4508 | ParsedType T = getTypeName(*FirstTypeName.Identifier, | 
| John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4509 | FirstTypeName.StartLocation, | 
| Douglas Gregor | f3db29f | 2011-02-25 18:19:59 +0000 | [diff] [blame] | 4510 | S, &SS, true, false, ObjectTypePtrForLookup); | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4511 | if (!T) { | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4512 | Diag(FirstTypeName.StartLocation, | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4513 | diag::err_pseudo_dtor_destructor_non_type) | 
|  | 4514 | << FirstTypeName.Identifier << ObjectType; | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4515 |  | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4516 | if (isSFINAEContext()) | 
|  | 4517 | return ExprError(); | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4518 |  | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4519 | // Just drop this type. It's unnecessary anyway. | 
|  | 4520 | ScopeType = QualType(); | 
|  | 4521 | } else | 
|  | 4522 | ScopeType = GetTypeFromParser(T, &ScopeTypeInfo); | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4523 | } else { | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4524 | // Resolve the template-id to a type. | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4525 | TemplateIdAnnotation *TemplateId = FirstTypeName.TemplateId; | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4526 | ASTTemplateArgsPtr TemplateArgsPtr(*this, | 
|  | 4527 | TemplateId->getTemplateArgs(), | 
|  | 4528 | TemplateId->NumArgs); | 
| Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 4529 | TypeResult T = ActOnTemplateIdType(TemplateId->SS, | 
|  | 4530 | TemplateId->Template, | 
| Douglas Gregor | b57fb49 | 2010-02-24 22:38:50 +0000 | [diff] [blame] | 4531 | TemplateId->TemplateNameLoc, | 
|  | 4532 | TemplateId->LAngleLoc, | 
|  | 4533 | TemplateArgsPtr, | 
|  | 4534 | TemplateId->RAngleLoc); | 
|  | 4535 | if (T.isInvalid() || !T.get()) { | 
|  | 4536 | // Recover by dropping this type. | 
|  | 4537 | ScopeType = QualType(); | 
|  | 4538 | } else | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4539 | ScopeType = GetTypeFromParser(T.get(), &ScopeTypeInfo); | 
| Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 4540 | } | 
|  | 4541 | } | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4542 |  | 
| Douglas Gregor | b4a418f | 2010-02-24 23:02:30 +0000 | [diff] [blame] | 4543 | if (!ScopeType.isNull() && !ScopeTypeInfo) | 
|  | 4544 | ScopeTypeInfo = Context.getTrivialTypeSourceInfo(ScopeType, | 
|  | 4545 | FirstTypeName.StartLocation); | 
|  | 4546 |  | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4547 |  | 
| John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4548 | return BuildPseudoDestructorExpr(Base, OpLoc, OpKind, SS, | 
| Douglas Gregor | fce46ee | 2010-02-24 23:50:37 +0000 | [diff] [blame] | 4549 | ScopeTypeInfo, CCLoc, TildeLoc, | 
| Douglas Gregor | a2e7dd2 | 2010-02-25 01:56:36 +0000 | [diff] [blame] | 4550 | Destructed, HasTrailingLParen); | 
| Douglas Gregor | d4dca08 | 2010-02-24 18:44:31 +0000 | [diff] [blame] | 4551 | } | 
|  | 4552 |  | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4553 | ExprResult Sema::BuildCXXMemberCallExpr(Expr *E, NamedDecl *FoundDecl, | 
| Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4554 | CXXMethodDecl *Method, | 
|  | 4555 | bool HadMultipleCandidates) { | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4556 | ExprResult Exp = PerformObjectArgumentInitialization(E, /*Qualifier=*/0, | 
|  | 4557 | FoundDecl, Method); | 
|  | 4558 | if (Exp.isInvalid()) | 
| Douglas Gregor | f2ae526 | 2011-01-20 00:18:04 +0000 | [diff] [blame] | 4559 | return true; | 
| Eli Friedman | 772fffa | 2009-12-09 04:53:56 +0000 | [diff] [blame] | 4560 |  | 
| NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4561 | MemberExpr *ME = | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4562 | new (Context) MemberExpr(Exp.take(), /*IsArrow=*/false, Method, | 
| John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4563 | SourceLocation(), Method->getType(), | 
|  | 4564 | VK_RValue, OK_Ordinary); | 
| Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4565 | if (HadMultipleCandidates) | 
|  | 4566 | ME->setHadMultipleCandidates(true); | 
|  | 4567 |  | 
| John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4568 | QualType ResultType = Method->getResultType(); | 
|  | 4569 | ExprValueKind VK = Expr::getValueKindForType(ResultType); | 
|  | 4570 | ResultType = ResultType.getNonLValueExprType(Context); | 
|  | 4571 |  | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4572 | MarkDeclarationReferenced(Exp.get()->getLocStart(), Method); | 
| Douglas Gregor | 7edfb69 | 2009-11-23 12:27:39 +0000 | [diff] [blame] | 4573 | CXXMemberCallExpr *CE = | 
| John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4574 | new (Context) CXXMemberCallExpr(Context, ME, 0, 0, ResultType, VK, | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4575 | Exp.get()->getLocEnd()); | 
| Fariborz Jahanian | b740023 | 2009-09-28 23:23:40 +0000 | [diff] [blame] | 4576 | return CE; | 
|  | 4577 | } | 
|  | 4578 |  | 
| Sebastian Redl | 2e15622 | 2010-09-10 20:55:43 +0000 | [diff] [blame] | 4579 | ExprResult Sema::BuildCXXNoexceptExpr(SourceLocation KeyLoc, Expr *Operand, | 
|  | 4580 | SourceLocation RParen) { | 
| Sebastian Redl | 2e15622 | 2010-09-10 20:55:43 +0000 | [diff] [blame] | 4581 | return Owned(new (Context) CXXNoexceptExpr(Context.BoolTy, Operand, | 
|  | 4582 | Operand->CanThrow(Context), | 
|  | 4583 | KeyLoc, RParen)); | 
|  | 4584 | } | 
|  | 4585 |  | 
|  | 4586 | ExprResult Sema::ActOnNoexceptExpr(SourceLocation KeyLoc, SourceLocation, | 
|  | 4587 | Expr *Operand, SourceLocation RParen) { | 
|  | 4588 | return BuildCXXNoexceptExpr(KeyLoc, Operand, RParen); | 
| Sebastian Redl | 02bc21a | 2010-09-10 20:55:37 +0000 | [diff] [blame] | 4589 | } | 
|  | 4590 |  | 
| John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4591 | /// Perform the conversions required for an expression used in a | 
|  | 4592 | /// context that ignores the result. | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4593 | ExprResult Sema::IgnoredValueConversions(Expr *E) { | 
| John McCall | a878cda | 2010-12-02 02:07:15 +0000 | [diff] [blame] | 4594 | // C99 6.3.2.1: | 
|  | 4595 | //   [Except in specific positions,] an lvalue that does not have | 
|  | 4596 | //   array type is converted to the value stored in the | 
|  | 4597 | //   designated object (and is no longer an lvalue). | 
| John McCall | e6d134b | 2011-06-27 21:24:11 +0000 | [diff] [blame] | 4598 | if (E->isRValue()) { | 
|  | 4599 | // In C, function designators (i.e. expressions of function type) | 
|  | 4600 | // are r-values, but we still want to do function-to-pointer decay | 
|  | 4601 | // on them.  This is both technically correct and convenient for | 
|  | 4602 | // some clients. | 
|  | 4603 | if (!getLangOptions().CPlusPlus && E->getType()->isFunctionType()) | 
|  | 4604 | return DefaultFunctionArrayConversion(E); | 
|  | 4605 |  | 
|  | 4606 | return Owned(E); | 
|  | 4607 | } | 
| John McCall | a878cda | 2010-12-02 02:07:15 +0000 | [diff] [blame] | 4608 |  | 
| John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4609 | // We always want to do this on ObjC property references. | 
|  | 4610 | if (E->getObjectKind() == OK_ObjCProperty) { | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4611 | ExprResult Res = ConvertPropertyForRValue(E); | 
|  | 4612 | if (Res.isInvalid()) return Owned(E); | 
|  | 4613 | E = Res.take(); | 
|  | 4614 | if (E->isRValue()) return Owned(E); | 
| John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4615 | } | 
|  | 4616 |  | 
|  | 4617 | // Otherwise, this rule does not apply in C++, at least not for the moment. | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4618 | if (getLangOptions().CPlusPlus) return Owned(E); | 
| John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4619 |  | 
|  | 4620 | // GCC seems to also exclude expressions of incomplete enum type. | 
|  | 4621 | if (const EnumType *T = E->getType()->getAs<EnumType>()) { | 
|  | 4622 | if (!T->getDecl()->isComplete()) { | 
|  | 4623 | // FIXME: stupid workaround for a codegen bug! | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4624 | E = ImpCastExprToType(E, Context.VoidTy, CK_ToVoid).take(); | 
|  | 4625 | return Owned(E); | 
| John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4626 | } | 
|  | 4627 | } | 
|  | 4628 |  | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4629 | ExprResult Res = DefaultFunctionArrayLvalueConversion(E); | 
|  | 4630 | if (Res.isInvalid()) | 
|  | 4631 | return Owned(E); | 
|  | 4632 | E = Res.take(); | 
|  | 4633 |  | 
| John McCall | 85515d6 | 2010-12-04 12:29:11 +0000 | [diff] [blame] | 4634 | if (!E->getType()->isVoidType()) | 
|  | 4635 | RequireCompleteType(E->getExprLoc(), E->getType(), | 
|  | 4636 | diag::err_incomplete_type); | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4637 | return Owned(E); | 
| John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4638 | } | 
|  | 4639 |  | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4640 | ExprResult Sema::ActOnFinishFullExpr(Expr *FE) { | 
|  | 4641 | ExprResult FullExpr = Owned(FE); | 
|  | 4642 |  | 
|  | 4643 | if (!FullExpr.get()) | 
| Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 4644 | return ExprError(); | 
| John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4645 |  | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4646 | if (DiagnoseUnexpandedParameterPack(FullExpr.get())) | 
| Douglas Gregor | d093722 | 2010-12-13 22:49:22 +0000 | [diff] [blame] | 4647 | return ExprError(); | 
|  | 4648 |  | 
| John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 4649 | FullExpr = CheckPlaceholderExpr(FullExpr.take()); | 
|  | 4650 | if (FullExpr.isInvalid()) | 
|  | 4651 | return ExprError(); | 
| Douglas Gregor | 353ee24 | 2011-03-07 02:05:23 +0000 | [diff] [blame] | 4652 |  | 
| John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4653 | FullExpr = IgnoredValueConversions(FullExpr.take()); | 
|  | 4654 | if (FullExpr.isInvalid()) | 
|  | 4655 | return ExprError(); | 
|  | 4656 |  | 
| Richard Trieu | f1f8b1a | 2011-09-23 20:10:00 +0000 | [diff] [blame] | 4657 | CheckImplicitConversions(FullExpr.get(), FullExpr.get()->getExprLoc()); | 
| John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 4658 | return MaybeCreateExprWithCleanups(FullExpr); | 
| Anders Carlsson | 165a0a0 | 2009-05-17 18:41:29 +0000 | [diff] [blame] | 4659 | } | 
| Argyrios Kyrtzidis | bf8cafa | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 4660 |  | 
|  | 4661 | StmtResult Sema::ActOnFinishFullStmt(Stmt *FullStmt) { | 
|  | 4662 | if (!FullStmt) return StmtError(); | 
|  | 4663 |  | 
| John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 4664 | return MaybeCreateStmtWithCleanups(FullStmt); | 
| Argyrios Kyrtzidis | bf8cafa | 2010-11-02 02:33:08 +0000 | [diff] [blame] | 4665 | } | 
| Francois Pichet | 1e86269 | 2011-05-06 20:48:22 +0000 | [diff] [blame] | 4666 |  | 
|  | 4667 | bool Sema::CheckMicrosoftIfExistsSymbol(CXXScopeSpec &SS, | 
|  | 4668 | UnqualifiedId &Name) { | 
|  | 4669 | DeclarationNameInfo TargetNameInfo = GetNameFromUnqualifiedId(Name); | 
|  | 4670 | DeclarationName TargetName = TargetNameInfo.getName(); | 
|  | 4671 | if (!TargetName) | 
|  | 4672 | return false; | 
|  | 4673 |  | 
|  | 4674 | // Do the redeclaration lookup in the current scope. | 
|  | 4675 | LookupResult R(*this, TargetNameInfo, Sema::LookupAnyName, | 
|  | 4676 | Sema::NotForRedeclaration); | 
|  | 4677 | R.suppressDiagnostics(); | 
|  | 4678 | LookupParsedName(R, getCurScope(), &SS); | 
|  | 4679 | return !R.empty(); | 
|  | 4680 | } |