Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- SemaType.cpp - Semantic Analysis for Types -----------------------===// |
| 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 type-related semantic analysis. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 36f255c | 2011-06-03 14:28:43 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTMutationListener.h" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 17 | #include "clang/AST/CXXInheritance.h" |
Steve Naroff | 980e508 | 2007-10-01 19:00:59 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclTemplate.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "clang/AST/Expr.h" |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 21 | #include "clang/AST/TypeLoc.h" |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 22 | #include "clang/AST/TypeLocVisitor.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 23 | #include "clang/Basic/OpenCL.h" |
Anders Carlsson | 91a0cc9 | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 24 | #include "clang/Basic/PartialDiagnostic.h" |
Charles Davis | d18f9f9 | 2010-08-16 04:01:50 +0000 | [diff] [blame] | 25 | #include "clang/Basic/TargetInfo.h" |
John McCall | 2792fa5 | 2011-03-08 04:17:03 +0000 | [diff] [blame] | 26 | #include "clang/Lex/Preprocessor.h" |
Eli Friedman | bc1029b | 2012-04-05 22:47:34 +0000 | [diff] [blame] | 27 | #include "clang/Parse/ParseDiagnostic.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 28 | #include "clang/Sema/DeclSpec.h" |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 29 | #include "clang/Sema/DelayedDiagnostic.h" |
Douglas Gregor | d07cc36 | 2012-01-02 17:18:37 +0000 | [diff] [blame] | 30 | #include "clang/Sema/Lookup.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 31 | #include "clang/Sema/ScopeInfo.h" |
| 32 | #include "clang/Sema/Template.h" |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/SmallPtrSet.h" |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 34 | #include "llvm/Support/ErrorHandling.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 35 | using namespace clang; |
| 36 | |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 37 | /// isOmittedBlockReturnType - Return true if this declarator is missing a |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 38 | /// return type because this is a omitted return type on a block literal. |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 39 | static bool isOmittedBlockReturnType(const Declarator &D) { |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 40 | if (D.getContext() != Declarator::BlockLiteralContext || |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 41 | D.getDeclSpec().hasTypeSpecifier()) |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 42 | return false; |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 43 | |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 44 | if (D.getNumTypeObjects() == 0) |
Chris Lattner | a64ef0a | 2009-10-25 22:09:09 +0000 | [diff] [blame] | 45 | return true; // ^{ ... } |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 46 | |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 47 | if (D.getNumTypeObjects() == 1 && |
| 48 | D.getTypeObject(0).Kind == DeclaratorChunk::Function) |
Chris Lattner | a64ef0a | 2009-10-25 22:09:09 +0000 | [diff] [blame] | 49 | return true; // ^(int X, float Y) { ... } |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 50 | |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 51 | return false; |
| 52 | } |
| 53 | |
John McCall | 2792fa5 | 2011-03-08 04:17:03 +0000 | [diff] [blame] | 54 | /// diagnoseBadTypeAttribute - Diagnoses a type attribute which |
| 55 | /// doesn't apply to the given type. |
| 56 | static void diagnoseBadTypeAttribute(Sema &S, const AttributeList &attr, |
| 57 | QualType type) { |
Chandler Carruth | 108f756 | 2011-07-26 05:40:03 +0000 | [diff] [blame] | 58 | bool useExpansionLoc = false; |
John McCall | 2792fa5 | 2011-03-08 04:17:03 +0000 | [diff] [blame] | 59 | |
| 60 | unsigned diagID = 0; |
| 61 | switch (attr.getKind()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 62 | case AttributeList::AT_ObjCGC: |
John McCall | 2792fa5 | 2011-03-08 04:17:03 +0000 | [diff] [blame] | 63 | diagID = diag::warn_pointer_attribute_wrong_type; |
Chandler Carruth | 108f756 | 2011-07-26 05:40:03 +0000 | [diff] [blame] | 64 | useExpansionLoc = true; |
John McCall | 2792fa5 | 2011-03-08 04:17:03 +0000 | [diff] [blame] | 65 | break; |
| 66 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 67 | case AttributeList::AT_ObjCOwnership: |
Argyrios Kyrtzidis | 05d4876 | 2011-07-01 22:23:09 +0000 | [diff] [blame] | 68 | diagID = diag::warn_objc_object_attribute_wrong_type; |
Chandler Carruth | 108f756 | 2011-07-26 05:40:03 +0000 | [diff] [blame] | 69 | useExpansionLoc = true; |
Argyrios Kyrtzidis | 05d4876 | 2011-07-01 22:23:09 +0000 | [diff] [blame] | 70 | break; |
| 71 | |
John McCall | 2792fa5 | 2011-03-08 04:17:03 +0000 | [diff] [blame] | 72 | default: |
| 73 | // Assume everything else was a function attribute. |
| 74 | diagID = diag::warn_function_attribute_wrong_type; |
| 75 | break; |
| 76 | } |
| 77 | |
| 78 | SourceLocation loc = attr.getLoc(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 79 | StringRef name = attr.getName()->getName(); |
John McCall | 2792fa5 | 2011-03-08 04:17:03 +0000 | [diff] [blame] | 80 | |
| 81 | // The GC attributes are usually written with macros; special-case them. |
Chandler Carruth | 108f756 | 2011-07-26 05:40:03 +0000 | [diff] [blame] | 82 | if (useExpansionLoc && loc.isMacroID() && attr.getParameterName()) { |
John McCall | 834e3f6 | 2011-03-08 07:59:04 +0000 | [diff] [blame] | 83 | if (attr.getParameterName()->isStr("strong")) { |
| 84 | if (S.findMacroSpelling(loc, "__strong")) name = "__strong"; |
| 85 | } else if (attr.getParameterName()->isStr("weak")) { |
| 86 | if (S.findMacroSpelling(loc, "__weak")) name = "__weak"; |
John McCall | 2792fa5 | 2011-03-08 04:17:03 +0000 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
| 90 | S.Diag(loc, diagID) << name << type; |
| 91 | } |
| 92 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 93 | // objc_gc applies to Objective-C pointers or, otherwise, to the |
| 94 | // smallest available pointer type (i.e. 'void*' in 'void**'). |
| 95 | #define OBJC_POINTER_TYPE_ATTRS_CASELIST \ |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 96 | case AttributeList::AT_ObjCGC: \ |
| 97 | case AttributeList::AT_ObjCOwnership |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 98 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 99 | // Function type attributes. |
| 100 | #define FUNCTION_TYPE_ATTRS_CASELIST \ |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 101 | case AttributeList::AT_NoReturn: \ |
| 102 | case AttributeList::AT_CDecl: \ |
| 103 | case AttributeList::AT_FastCall: \ |
| 104 | case AttributeList::AT_StdCall: \ |
| 105 | case AttributeList::AT_ThisCall: \ |
| 106 | case AttributeList::AT_Pascal: \ |
| 107 | case AttributeList::AT_Regparm: \ |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 108 | case AttributeList::AT_Pcs: \ |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 109 | case AttributeList::AT_PnaclCall: \ |
| 110 | case AttributeList::AT_IntelOclBicc \ |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 111 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 112 | namespace { |
| 113 | /// An object which stores processing state for the entire |
| 114 | /// GetTypeForDeclarator process. |
| 115 | class TypeProcessingState { |
| 116 | Sema &sema; |
| 117 | |
| 118 | /// The declarator being processed. |
| 119 | Declarator &declarator; |
| 120 | |
| 121 | /// The index of the declarator chunk we're currently processing. |
| 122 | /// May be the total number of valid chunks, indicating the |
| 123 | /// DeclSpec. |
| 124 | unsigned chunkIndex; |
| 125 | |
| 126 | /// Whether there are non-trivial modifications to the decl spec. |
| 127 | bool trivial; |
| 128 | |
John McCall | 7ea2193 | 2011-03-26 01:39:56 +0000 | [diff] [blame] | 129 | /// Whether we saved the attributes in the decl spec. |
| 130 | bool hasSavedAttrs; |
| 131 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 132 | /// The original set of attributes on the DeclSpec. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 133 | SmallVector<AttributeList*, 2> savedAttrs; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 134 | |
| 135 | /// A list of attributes to diagnose the uselessness of when the |
| 136 | /// processing is complete. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 137 | SmallVector<AttributeList*, 2> ignoredTypeAttrs; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 138 | |
| 139 | public: |
| 140 | TypeProcessingState(Sema &sema, Declarator &declarator) |
| 141 | : sema(sema), declarator(declarator), |
| 142 | chunkIndex(declarator.getNumTypeObjects()), |
John McCall | 7ea2193 | 2011-03-26 01:39:56 +0000 | [diff] [blame] | 143 | trivial(true), hasSavedAttrs(false) {} |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 144 | |
| 145 | Sema &getSema() const { |
| 146 | return sema; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 147 | } |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 148 | |
| 149 | Declarator &getDeclarator() const { |
| 150 | return declarator; |
| 151 | } |
| 152 | |
John McCall | b2381b1 | 2013-03-01 07:58:16 +0000 | [diff] [blame] | 153 | bool isProcessingDeclSpec() const { |
| 154 | return chunkIndex == declarator.getNumTypeObjects(); |
| 155 | } |
| 156 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 157 | unsigned getCurrentChunkIndex() const { |
| 158 | return chunkIndex; |
| 159 | } |
| 160 | |
| 161 | void setCurrentChunkIndex(unsigned idx) { |
| 162 | assert(idx <= declarator.getNumTypeObjects()); |
| 163 | chunkIndex = idx; |
| 164 | } |
| 165 | |
| 166 | AttributeList *&getCurrentAttrListRef() const { |
John McCall | b2381b1 | 2013-03-01 07:58:16 +0000 | [diff] [blame] | 167 | if (isProcessingDeclSpec()) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 168 | return getMutableDeclSpec().getAttributes().getListRef(); |
| 169 | return declarator.getTypeObject(chunkIndex).getAttrListRef(); |
| 170 | } |
| 171 | |
| 172 | /// Save the current set of attributes on the DeclSpec. |
| 173 | void saveDeclSpecAttrs() { |
| 174 | // Don't try to save them multiple times. |
John McCall | 7ea2193 | 2011-03-26 01:39:56 +0000 | [diff] [blame] | 175 | if (hasSavedAttrs) return; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 176 | |
| 177 | DeclSpec &spec = getMutableDeclSpec(); |
| 178 | for (AttributeList *attr = spec.getAttributes().getList(); attr; |
| 179 | attr = attr->getNext()) |
| 180 | savedAttrs.push_back(attr); |
| 181 | trivial &= savedAttrs.empty(); |
John McCall | 7ea2193 | 2011-03-26 01:39:56 +0000 | [diff] [blame] | 182 | hasSavedAttrs = true; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | /// Record that we had nowhere to put the given type attribute. |
| 186 | /// We will diagnose such attributes later. |
| 187 | void addIgnoredTypeAttr(AttributeList &attr) { |
| 188 | ignoredTypeAttrs.push_back(&attr); |
| 189 | } |
| 190 | |
| 191 | /// Diagnose all the ignored type attributes, given that the |
| 192 | /// declarator worked out to the given type. |
| 193 | void diagnoseIgnoredTypeAttrs(QualType type) const { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 194 | for (SmallVectorImpl<AttributeList*>::const_iterator |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 195 | i = ignoredTypeAttrs.begin(), e = ignoredTypeAttrs.end(); |
John McCall | 2792fa5 | 2011-03-08 04:17:03 +0000 | [diff] [blame] | 196 | i != e; ++i) |
| 197 | diagnoseBadTypeAttribute(getSema(), **i, type); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | ~TypeProcessingState() { |
| 201 | if (trivial) return; |
| 202 | |
| 203 | restoreDeclSpecAttrs(); |
| 204 | } |
| 205 | |
| 206 | private: |
| 207 | DeclSpec &getMutableDeclSpec() const { |
| 208 | return const_cast<DeclSpec&>(declarator.getDeclSpec()); |
| 209 | } |
| 210 | |
| 211 | void restoreDeclSpecAttrs() { |
John McCall | 7ea2193 | 2011-03-26 01:39:56 +0000 | [diff] [blame] | 212 | assert(hasSavedAttrs); |
| 213 | |
| 214 | if (savedAttrs.empty()) { |
| 215 | getMutableDeclSpec().getAttributes().set(0); |
| 216 | return; |
| 217 | } |
| 218 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 219 | getMutableDeclSpec().getAttributes().set(savedAttrs[0]); |
| 220 | for (unsigned i = 0, e = savedAttrs.size() - 1; i != e; ++i) |
| 221 | savedAttrs[i]->setNext(savedAttrs[i+1]); |
| 222 | savedAttrs.back()->setNext(0); |
| 223 | } |
| 224 | }; |
| 225 | |
| 226 | /// Basically std::pair except that we really want to avoid an |
| 227 | /// implicit operator= for safety concerns. It's also a minor |
| 228 | /// link-time optimization for this to be a private type. |
| 229 | struct AttrAndList { |
| 230 | /// The attribute. |
| 231 | AttributeList &first; |
| 232 | |
| 233 | /// The head of the list the attribute is currently in. |
| 234 | AttributeList *&second; |
| 235 | |
| 236 | AttrAndList(AttributeList &attr, AttributeList *&head) |
| 237 | : first(attr), second(head) {} |
| 238 | }; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 239 | } |
| 240 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 241 | namespace llvm { |
| 242 | template <> struct isPodLike<AttrAndList> { |
| 243 | static const bool value = true; |
| 244 | }; |
| 245 | } |
| 246 | |
| 247 | static void spliceAttrIntoList(AttributeList &attr, AttributeList *&head) { |
| 248 | attr.setNext(head); |
| 249 | head = &attr; |
| 250 | } |
| 251 | |
| 252 | static void spliceAttrOutOfList(AttributeList &attr, AttributeList *&head) { |
| 253 | if (head == &attr) { |
| 254 | head = attr.getNext(); |
| 255 | return; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 256 | } |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 257 | |
| 258 | AttributeList *cur = head; |
| 259 | while (true) { |
| 260 | assert(cur && cur->getNext() && "ran out of attrs?"); |
| 261 | if (cur->getNext() == &attr) { |
| 262 | cur->setNext(attr.getNext()); |
| 263 | return; |
| 264 | } |
| 265 | cur = cur->getNext(); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | static void moveAttrFromListToList(AttributeList &attr, |
| 270 | AttributeList *&fromList, |
| 271 | AttributeList *&toList) { |
| 272 | spliceAttrOutOfList(attr, fromList); |
| 273 | spliceAttrIntoList(attr, toList); |
| 274 | } |
| 275 | |
Richard Smith | f7a0527 | 2013-01-14 07:53:01 +0000 | [diff] [blame] | 276 | /// The location of a type attribute. |
| 277 | enum TypeAttrLocation { |
| 278 | /// The attribute is in the decl-specifier-seq. |
| 279 | TAL_DeclSpec, |
| 280 | /// The attribute is part of a DeclaratorChunk. |
| 281 | TAL_DeclChunk, |
| 282 | /// The attribute is immediately after the declaration's name. |
| 283 | TAL_DeclName |
| 284 | }; |
| 285 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 286 | static void processTypeAttrs(TypeProcessingState &state, |
Richard Smith | f7a0527 | 2013-01-14 07:53:01 +0000 | [diff] [blame] | 287 | QualType &type, TypeAttrLocation TAL, |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 288 | AttributeList *attrs); |
| 289 | |
| 290 | static bool handleFunctionTypeAttr(TypeProcessingState &state, |
| 291 | AttributeList &attr, |
| 292 | QualType &type); |
| 293 | |
| 294 | static bool handleObjCGCTypeAttr(TypeProcessingState &state, |
| 295 | AttributeList &attr, QualType &type); |
| 296 | |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 297 | static bool handleObjCOwnershipTypeAttr(TypeProcessingState &state, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 298 | AttributeList &attr, QualType &type); |
| 299 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 300 | static bool handleObjCPointerTypeAttr(TypeProcessingState &state, |
| 301 | AttributeList &attr, QualType &type) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 302 | if (attr.getKind() == AttributeList::AT_ObjCGC) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 303 | return handleObjCGCTypeAttr(state, attr, type); |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 304 | assert(attr.getKind() == AttributeList::AT_ObjCOwnership); |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 305 | return handleObjCOwnershipTypeAttr(state, attr, type); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 306 | } |
| 307 | |
John McCall | b2381b1 | 2013-03-01 07:58:16 +0000 | [diff] [blame] | 308 | /// Given the index of a declarator chunk, check whether that chunk |
| 309 | /// directly specifies the return type of a function and, if so, find |
| 310 | /// an appropriate place for it. |
| 311 | /// |
| 312 | /// \param i - a notional index which the search will start |
| 313 | /// immediately inside |
| 314 | static DeclaratorChunk *maybeMovePastReturnType(Declarator &declarator, |
| 315 | unsigned i) { |
| 316 | assert(i <= declarator.getNumTypeObjects()); |
| 317 | |
| 318 | DeclaratorChunk *result = 0; |
| 319 | |
| 320 | // First, look inwards past parens for a function declarator. |
| 321 | for (; i != 0; --i) { |
| 322 | DeclaratorChunk &fnChunk = declarator.getTypeObject(i-1); |
| 323 | switch (fnChunk.Kind) { |
| 324 | case DeclaratorChunk::Paren: |
| 325 | continue; |
| 326 | |
| 327 | // If we find anything except a function, bail out. |
| 328 | case DeclaratorChunk::Pointer: |
| 329 | case DeclaratorChunk::BlockPointer: |
| 330 | case DeclaratorChunk::Array: |
| 331 | case DeclaratorChunk::Reference: |
| 332 | case DeclaratorChunk::MemberPointer: |
| 333 | return result; |
| 334 | |
| 335 | // If we do find a function declarator, scan inwards from that, |
| 336 | // looking for a block-pointer declarator. |
| 337 | case DeclaratorChunk::Function: |
| 338 | for (--i; i != 0; --i) { |
| 339 | DeclaratorChunk &blockChunk = declarator.getTypeObject(i-1); |
| 340 | switch (blockChunk.Kind) { |
| 341 | case DeclaratorChunk::Paren: |
| 342 | case DeclaratorChunk::Pointer: |
| 343 | case DeclaratorChunk::Array: |
| 344 | case DeclaratorChunk::Function: |
| 345 | case DeclaratorChunk::Reference: |
| 346 | case DeclaratorChunk::MemberPointer: |
| 347 | continue; |
| 348 | case DeclaratorChunk::BlockPointer: |
| 349 | result = &blockChunk; |
| 350 | goto continue_outer; |
| 351 | } |
| 352 | llvm_unreachable("bad declarator chunk kind"); |
| 353 | } |
| 354 | |
| 355 | // If we run out of declarators doing that, we're done. |
| 356 | return result; |
| 357 | } |
| 358 | llvm_unreachable("bad declarator chunk kind"); |
| 359 | |
| 360 | // Okay, reconsider from our new point. |
| 361 | continue_outer: ; |
| 362 | } |
| 363 | |
| 364 | // Ran out of chunks, bail out. |
| 365 | return result; |
| 366 | } |
| 367 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 368 | /// Given that an objc_gc attribute was written somewhere on a |
| 369 | /// declaration *other* than on the declarator itself (for which, use |
| 370 | /// distributeObjCPointerTypeAttrFromDeclarator), and given that it |
| 371 | /// didn't apply in whatever position it was written in, try to move |
| 372 | /// it to a more appropriate position. |
| 373 | static void distributeObjCPointerTypeAttr(TypeProcessingState &state, |
| 374 | AttributeList &attr, |
| 375 | QualType type) { |
| 376 | Declarator &declarator = state.getDeclarator(); |
John McCall | b2381b1 | 2013-03-01 07:58:16 +0000 | [diff] [blame] | 377 | |
| 378 | // Move it to the outermost normal or block pointer declarator. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 379 | for (unsigned i = state.getCurrentChunkIndex(); i != 0; --i) { |
| 380 | DeclaratorChunk &chunk = declarator.getTypeObject(i-1); |
| 381 | switch (chunk.Kind) { |
| 382 | case DeclaratorChunk::Pointer: |
John McCall | b2381b1 | 2013-03-01 07:58:16 +0000 | [diff] [blame] | 383 | case DeclaratorChunk::BlockPointer: { |
| 384 | // But don't move an ARC ownership attribute to the return type |
| 385 | // of a block. |
| 386 | DeclaratorChunk *destChunk = 0; |
| 387 | if (state.isProcessingDeclSpec() && |
| 388 | attr.getKind() == AttributeList::AT_ObjCOwnership) |
| 389 | destChunk = maybeMovePastReturnType(declarator, i - 1); |
| 390 | if (!destChunk) destChunk = &chunk; |
| 391 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 392 | moveAttrFromListToList(attr, state.getCurrentAttrListRef(), |
John McCall | b2381b1 | 2013-03-01 07:58:16 +0000 | [diff] [blame] | 393 | destChunk->getAttrListRef()); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 394 | return; |
John McCall | b2381b1 | 2013-03-01 07:58:16 +0000 | [diff] [blame] | 395 | } |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 396 | |
| 397 | case DeclaratorChunk::Paren: |
| 398 | case DeclaratorChunk::Array: |
| 399 | continue; |
| 400 | |
John McCall | b2381b1 | 2013-03-01 07:58:16 +0000 | [diff] [blame] | 401 | // We may be starting at the return type of a block. |
| 402 | case DeclaratorChunk::Function: |
| 403 | if (state.isProcessingDeclSpec() && |
| 404 | attr.getKind() == AttributeList::AT_ObjCOwnership) { |
| 405 | if (DeclaratorChunk *dest = maybeMovePastReturnType(declarator, i)) { |
| 406 | moveAttrFromListToList(attr, state.getCurrentAttrListRef(), |
| 407 | dest->getAttrListRef()); |
| 408 | return; |
| 409 | } |
| 410 | } |
| 411 | goto error; |
| 412 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 413 | // Don't walk through these. |
| 414 | case DeclaratorChunk::Reference: |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 415 | case DeclaratorChunk::MemberPointer: |
| 416 | goto error; |
| 417 | } |
| 418 | } |
| 419 | error: |
John McCall | 2792fa5 | 2011-03-08 04:17:03 +0000 | [diff] [blame] | 420 | |
| 421 | diagnoseBadTypeAttribute(state.getSema(), attr, type); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | /// Distribute an objc_gc type attribute that was written on the |
| 425 | /// declarator. |
| 426 | static void |
| 427 | distributeObjCPointerTypeAttrFromDeclarator(TypeProcessingState &state, |
| 428 | AttributeList &attr, |
| 429 | QualType &declSpecType) { |
| 430 | Declarator &declarator = state.getDeclarator(); |
| 431 | |
| 432 | // objc_gc goes on the innermost pointer to something that's not a |
| 433 | // pointer. |
| 434 | unsigned innermost = -1U; |
| 435 | bool considerDeclSpec = true; |
| 436 | for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) { |
| 437 | DeclaratorChunk &chunk = declarator.getTypeObject(i); |
| 438 | switch (chunk.Kind) { |
| 439 | case DeclaratorChunk::Pointer: |
| 440 | case DeclaratorChunk::BlockPointer: |
| 441 | innermost = i; |
John McCall | ae278a3 | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 442 | continue; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 443 | |
| 444 | case DeclaratorChunk::Reference: |
| 445 | case DeclaratorChunk::MemberPointer: |
| 446 | case DeclaratorChunk::Paren: |
| 447 | case DeclaratorChunk::Array: |
| 448 | continue; |
| 449 | |
| 450 | case DeclaratorChunk::Function: |
| 451 | considerDeclSpec = false; |
| 452 | goto done; |
| 453 | } |
| 454 | } |
| 455 | done: |
| 456 | |
| 457 | // That might actually be the decl spec if we weren't blocked by |
| 458 | // anything in the declarator. |
| 459 | if (considerDeclSpec) { |
John McCall | 7ea2193 | 2011-03-26 01:39:56 +0000 | [diff] [blame] | 460 | if (handleObjCPointerTypeAttr(state, attr, declSpecType)) { |
| 461 | // Splice the attribute into the decl spec. Prevents the |
| 462 | // attribute from being applied multiple times and gives |
| 463 | // the source-location-filler something to work with. |
| 464 | state.saveDeclSpecAttrs(); |
| 465 | moveAttrFromListToList(attr, declarator.getAttrListRef(), |
| 466 | declarator.getMutableDeclSpec().getAttributes().getListRef()); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 467 | return; |
John McCall | 7ea2193 | 2011-03-26 01:39:56 +0000 | [diff] [blame] | 468 | } |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | // Otherwise, if we found an appropriate chunk, splice the attribute |
| 472 | // into it. |
| 473 | if (innermost != -1U) { |
| 474 | moveAttrFromListToList(attr, declarator.getAttrListRef(), |
| 475 | declarator.getTypeObject(innermost).getAttrListRef()); |
| 476 | return; |
| 477 | } |
| 478 | |
| 479 | // Otherwise, diagnose when we're done building the type. |
| 480 | spliceAttrOutOfList(attr, declarator.getAttrListRef()); |
| 481 | state.addIgnoredTypeAttr(attr); |
| 482 | } |
| 483 | |
| 484 | /// A function type attribute was written somewhere in a declaration |
| 485 | /// *other* than on the declarator itself or in the decl spec. Given |
| 486 | /// that it didn't apply in whatever position it was written in, try |
| 487 | /// to move it to a more appropriate position. |
| 488 | static void distributeFunctionTypeAttr(TypeProcessingState &state, |
| 489 | AttributeList &attr, |
| 490 | QualType type) { |
| 491 | Declarator &declarator = state.getDeclarator(); |
| 492 | |
| 493 | // Try to push the attribute from the return type of a function to |
| 494 | // the function itself. |
| 495 | for (unsigned i = state.getCurrentChunkIndex(); i != 0; --i) { |
| 496 | DeclaratorChunk &chunk = declarator.getTypeObject(i-1); |
| 497 | switch (chunk.Kind) { |
| 498 | case DeclaratorChunk::Function: |
| 499 | moveAttrFromListToList(attr, state.getCurrentAttrListRef(), |
| 500 | chunk.getAttrListRef()); |
| 501 | return; |
| 502 | |
| 503 | case DeclaratorChunk::Paren: |
| 504 | case DeclaratorChunk::Pointer: |
| 505 | case DeclaratorChunk::BlockPointer: |
| 506 | case DeclaratorChunk::Array: |
| 507 | case DeclaratorChunk::Reference: |
| 508 | case DeclaratorChunk::MemberPointer: |
| 509 | continue; |
| 510 | } |
| 511 | } |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 512 | |
John McCall | 2792fa5 | 2011-03-08 04:17:03 +0000 | [diff] [blame] | 513 | diagnoseBadTypeAttribute(state.getSema(), attr, type); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | /// Try to distribute a function type attribute to the innermost |
| 517 | /// function chunk or type. Returns true if the attribute was |
| 518 | /// distributed, false if no location was found. |
| 519 | static bool |
| 520 | distributeFunctionTypeAttrToInnermost(TypeProcessingState &state, |
| 521 | AttributeList &attr, |
| 522 | AttributeList *&attrList, |
| 523 | QualType &declSpecType) { |
| 524 | Declarator &declarator = state.getDeclarator(); |
| 525 | |
| 526 | // Put it on the innermost function chunk, if there is one. |
| 527 | for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) { |
| 528 | DeclaratorChunk &chunk = declarator.getTypeObject(i); |
| 529 | if (chunk.Kind != DeclaratorChunk::Function) continue; |
| 530 | |
| 531 | moveAttrFromListToList(attr, attrList, chunk.getAttrListRef()); |
| 532 | return true; |
| 533 | } |
| 534 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 535 | if (handleFunctionTypeAttr(state, attr, declSpecType)) { |
| 536 | spliceAttrOutOfList(attr, attrList); |
| 537 | return true; |
| 538 | } |
| 539 | |
| 540 | return false; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | /// A function type attribute was written in the decl spec. Try to |
| 544 | /// apply it somewhere. |
| 545 | static void |
| 546 | distributeFunctionTypeAttrFromDeclSpec(TypeProcessingState &state, |
| 547 | AttributeList &attr, |
| 548 | QualType &declSpecType) { |
| 549 | state.saveDeclSpecAttrs(); |
| 550 | |
Richard Smith | 5c52166 | 2013-01-15 02:48:13 +0000 | [diff] [blame] | 551 | // C++11 attributes before the decl specifiers actually appertain to |
| 552 | // the declarators. Move them straight there. We don't support the |
| 553 | // 'put them wherever you like' semantics we allow for GNU attributes. |
| 554 | if (attr.isCXX11Attribute()) { |
| 555 | moveAttrFromListToList(attr, state.getCurrentAttrListRef(), |
| 556 | state.getDeclarator().getAttrListRef()); |
| 557 | return; |
| 558 | } |
| 559 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 560 | // Try to distribute to the innermost. |
| 561 | if (distributeFunctionTypeAttrToInnermost(state, attr, |
| 562 | state.getCurrentAttrListRef(), |
| 563 | declSpecType)) |
| 564 | return; |
| 565 | |
| 566 | // If that failed, diagnose the bad attribute when the declarator is |
| 567 | // fully built. |
| 568 | state.addIgnoredTypeAttr(attr); |
| 569 | } |
| 570 | |
| 571 | /// A function type attribute was written on the declarator. Try to |
| 572 | /// apply it somewhere. |
| 573 | static void |
| 574 | distributeFunctionTypeAttrFromDeclarator(TypeProcessingState &state, |
| 575 | AttributeList &attr, |
| 576 | QualType &declSpecType) { |
| 577 | Declarator &declarator = state.getDeclarator(); |
| 578 | |
| 579 | // Try to distribute to the innermost. |
| 580 | if (distributeFunctionTypeAttrToInnermost(state, attr, |
| 581 | declarator.getAttrListRef(), |
| 582 | declSpecType)) |
| 583 | return; |
| 584 | |
| 585 | // If that failed, diagnose the bad attribute when the declarator is |
| 586 | // fully built. |
| 587 | spliceAttrOutOfList(attr, declarator.getAttrListRef()); |
| 588 | state.addIgnoredTypeAttr(attr); |
| 589 | } |
| 590 | |
| 591 | /// \brief Given that there are attributes written on the declarator |
| 592 | /// itself, try to distribute any type attributes to the appropriate |
| 593 | /// declarator chunk. |
| 594 | /// |
| 595 | /// These are attributes like the following: |
| 596 | /// int f ATTR; |
| 597 | /// int (f ATTR)(); |
| 598 | /// but not necessarily this: |
| 599 | /// int f() ATTR; |
| 600 | static void distributeTypeAttrsFromDeclarator(TypeProcessingState &state, |
| 601 | QualType &declSpecType) { |
| 602 | // Collect all the type attributes from the declarator itself. |
| 603 | assert(state.getDeclarator().getAttributes() && "declarator has no attrs!"); |
| 604 | AttributeList *attr = state.getDeclarator().getAttributes(); |
| 605 | AttributeList *next; |
| 606 | do { |
| 607 | next = attr->getNext(); |
| 608 | |
Richard Smith | 5c52166 | 2013-01-15 02:48:13 +0000 | [diff] [blame] | 609 | // Do not distribute C++11 attributes. They have strict rules for what |
| 610 | // they appertain to. |
| 611 | if (attr->isCXX11Attribute()) |
| 612 | continue; |
| 613 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 614 | switch (attr->getKind()) { |
| 615 | OBJC_POINTER_TYPE_ATTRS_CASELIST: |
| 616 | distributeObjCPointerTypeAttrFromDeclarator(state, *attr, declSpecType); |
| 617 | break; |
| 618 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 619 | case AttributeList::AT_NSReturnsRetained: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 620 | if (!state.getSema().getLangOpts().ObjCAutoRefCount) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 621 | break; |
| 622 | // fallthrough |
| 623 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 624 | FUNCTION_TYPE_ATTRS_CASELIST: |
| 625 | distributeFunctionTypeAttrFromDeclarator(state, *attr, declSpecType); |
| 626 | break; |
| 627 | |
| 628 | default: |
| 629 | break; |
| 630 | } |
| 631 | } while ((attr = next)); |
| 632 | } |
| 633 | |
| 634 | /// Add a synthetic '()' to a block-literal declarator if it is |
| 635 | /// required, given the return type. |
| 636 | static void maybeSynthesizeBlockSignature(TypeProcessingState &state, |
| 637 | QualType declSpecType) { |
| 638 | Declarator &declarator = state.getDeclarator(); |
| 639 | |
| 640 | // First, check whether the declarator would produce a function, |
| 641 | // i.e. whether the innermost semantic chunk is a function. |
| 642 | if (declarator.isFunctionDeclarator()) { |
| 643 | // If so, make that declarator a prototyped declarator. |
| 644 | declarator.getFunctionTypeInfo().hasPrototype = true; |
| 645 | return; |
| 646 | } |
| 647 | |
John McCall | da26379 | 2011-02-08 01:59:10 +0000 | [diff] [blame] | 648 | // If there are any type objects, the type as written won't name a |
| 649 | // function, regardless of the decl spec type. This is because a |
| 650 | // block signature declarator is always an abstract-declarator, and |
| 651 | // abstract-declarators can't just be parentheses chunks. Therefore |
| 652 | // we need to build a function chunk unless there are no type |
| 653 | // objects and the decl spec type is a function. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 654 | if (!declarator.getNumTypeObjects() && declSpecType->isFunctionType()) |
| 655 | return; |
| 656 | |
John McCall | da26379 | 2011-02-08 01:59:10 +0000 | [diff] [blame] | 657 | // Note that there *are* cases with invalid declarators where |
| 658 | // declarators consist solely of parentheses. In general, these |
| 659 | // occur only in failed efforts to make function declarators, so |
| 660 | // faking up the function chunk is still the right thing to do. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 661 | |
| 662 | // Otherwise, we need to fake up a function declarator. |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 663 | SourceLocation loc = declarator.getLocStart(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 664 | |
| 665 | // ...and *prepend* it to the declarator. |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 666 | SourceLocation NoLoc; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 667 | declarator.AddInnermostTypeInfo(DeclaratorChunk::getFunction( |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 668 | /*HasProto=*/true, |
| 669 | /*IsAmbiguous=*/false, |
| 670 | /*LParenLoc=*/NoLoc, |
| 671 | /*ArgInfo=*/0, |
| 672 | /*NumArgs=*/0, |
| 673 | /*EllipsisLoc=*/NoLoc, |
| 674 | /*RParenLoc=*/NoLoc, |
| 675 | /*TypeQuals=*/0, |
| 676 | /*RefQualifierIsLvalueRef=*/true, |
| 677 | /*RefQualifierLoc=*/NoLoc, |
| 678 | /*ConstQualifierLoc=*/NoLoc, |
| 679 | /*VolatileQualifierLoc=*/NoLoc, |
| 680 | /*MutableLoc=*/NoLoc, |
| 681 | EST_None, |
| 682 | /*ESpecLoc=*/NoLoc, |
| 683 | /*Exceptions=*/0, |
| 684 | /*ExceptionRanges=*/0, |
| 685 | /*NumExceptions=*/0, |
| 686 | /*NoexceptExpr=*/0, |
| 687 | loc, loc, declarator)); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 688 | |
| 689 | // For consistency, make sure the state still has us as processing |
| 690 | // the decl spec. |
| 691 | assert(state.getCurrentChunkIndex() == declarator.getNumTypeObjects() - 1); |
| 692 | state.setCurrentChunkIndex(declarator.getNumTypeObjects()); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 693 | } |
| 694 | |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 695 | /// \brief Convert the specified declspec to the appropriate type |
| 696 | /// object. |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 697 | /// \param state Specifies the declarator containing the declaration specifier |
| 698 | /// to be converted, along with other associated processing state. |
Chris Lattner | 5153ee6 | 2009-04-25 08:47:54 +0000 | [diff] [blame] | 699 | /// \returns The type described by the declaration specifiers. This function |
| 700 | /// never returns null. |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 701 | static QualType ConvertDeclSpecToType(TypeProcessingState &state) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 702 | // FIXME: Should move the logic from DeclSpec::Finish to here for validity |
| 703 | // checking. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 704 | |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 705 | Sema &S = state.getSema(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 706 | Declarator &declarator = state.getDeclarator(); |
| 707 | const DeclSpec &DS = declarator.getDeclSpec(); |
| 708 | SourceLocation DeclLoc = declarator.getIdentifierLoc(); |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 709 | if (DeclLoc.isInvalid()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 710 | DeclLoc = DS.getLocStart(); |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 711 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 712 | ASTContext &Context = S.Context; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 713 | |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 714 | QualType Result; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 715 | switch (DS.getTypeSpecType()) { |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 716 | case DeclSpec::TST_void: |
| 717 | Result = Context.VoidTy; |
| 718 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 719 | case DeclSpec::TST_char: |
| 720 | if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified) |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 721 | Result = Context.CharTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 722 | else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 723 | Result = Context.SignedCharTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 724 | else { |
| 725 | assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned && |
| 726 | "Unknown TSS value"); |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 727 | Result = Context.UnsignedCharTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 728 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 729 | break; |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 730 | case DeclSpec::TST_wchar: |
| 731 | if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified) |
| 732 | Result = Context.WCharTy; |
| 733 | else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 734 | S.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec) |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 735 | << DS.getSpecifierName(DS.getTypeSpecType()); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 736 | Result = Context.getSignedWCharType(); |
| 737 | } else { |
| 738 | assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned && |
| 739 | "Unknown TSS value"); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 740 | S.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec) |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 741 | << DS.getSpecifierName(DS.getTypeSpecType()); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 742 | Result = Context.getUnsignedWCharType(); |
| 743 | } |
| 744 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 745 | case DeclSpec::TST_char16: |
| 746 | assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified && |
| 747 | "Unknown TSS value"); |
| 748 | Result = Context.Char16Ty; |
| 749 | break; |
| 750 | case DeclSpec::TST_char32: |
| 751 | assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified && |
| 752 | "Unknown TSS value"); |
| 753 | Result = Context.Char32Ty; |
| 754 | break; |
Chris Lattner | d658b56 | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 755 | case DeclSpec::TST_unspecified: |
Chris Lattner | 62f5f7f | 2008-07-26 00:46:50 +0000 | [diff] [blame] | 756 | // "<proto1,proto2>" is an objc qualified ID with a missing id. |
Chris Lattner | 097e916 | 2008-10-20 02:01:50 +0000 | [diff] [blame] | 757 | if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 758 | Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy, |
Roman Divacky | 31ba613 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 759 | (ObjCProtocolDecl*const*)PQ, |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 760 | DS.getNumProtocolQualifiers()); |
| 761 | Result = Context.getObjCObjectPointerType(Result); |
Chris Lattner | 62f5f7f | 2008-07-26 00:46:50 +0000 | [diff] [blame] | 762 | break; |
| 763 | } |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 764 | |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 765 | // If this is a missing declspec in a block literal return context, then it |
| 766 | // is inferred from the return statements inside the block. |
Eli Friedman | f88c400 | 2012-01-04 04:41:38 +0000 | [diff] [blame] | 767 | // The declspec is always missing in a lambda expr context; it is either |
| 768 | // specified with a trailing return type or inferred. |
| 769 | if (declarator.getContext() == Declarator::LambdaExprContext || |
| 770 | isOmittedBlockReturnType(declarator)) { |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 771 | Result = Context.DependentTy; |
| 772 | break; |
| 773 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 774 | |
Chris Lattner | d658b56 | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 775 | // Unspecified typespec defaults to int in C90. However, the C90 grammar |
| 776 | // [C90 6.5] only allows a decl-spec if there was *some* type-specifier, |
| 777 | // type-qualifier, or storage-class-specifier. If not, emit an extwarn. |
| 778 | // Note that the one exception to this is function definitions, which are |
| 779 | // allowed to be completely missing a declspec. This is handled in the |
| 780 | // parser already though by it pretending to have seen an 'int' in this |
| 781 | // case. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 782 | if (S.getLangOpts().ImplicitInt) { |
Chris Lattner | 35d276f | 2009-02-27 18:53:28 +0000 | [diff] [blame] | 783 | // In C89 mode, we only warn if there is a completely missing declspec |
| 784 | // when one is not allowed. |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 785 | if (DS.isEmpty()) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 786 | S.Diag(DeclLoc, diag::ext_missing_declspec) |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 787 | << DS.getSourceRange() |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 788 | << FixItHint::CreateInsertion(DS.getLocStart(), "int"); |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 789 | } |
Douglas Gregor | 4310f4e | 2009-02-16 22:38:20 +0000 | [diff] [blame] | 790 | } else if (!DS.hasTypeSpecifier()) { |
Chris Lattner | d658b56 | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 791 | // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says: |
| 792 | // "At least one type specifier shall be given in the declaration |
| 793 | // specifiers in each declaration, and in the specifier-qualifier list in |
| 794 | // each struct declaration and type name." |
Douglas Gregor | 4310f4e | 2009-02-16 22:38:20 +0000 | [diff] [blame] | 795 | // FIXME: Does Microsoft really have the implicit int extension in C++? |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 796 | if (S.getLangOpts().CPlusPlus && |
| 797 | !S.getLangOpts().MicrosoftExt) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 798 | S.Diag(DeclLoc, diag::err_missing_type_specifier) |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 799 | << DS.getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 800 | |
Chris Lattner | b78d833 | 2009-06-26 04:45:06 +0000 | [diff] [blame] | 801 | // When this occurs in C++ code, often something is very broken with the |
| 802 | // value being declared, poison it as invalid so we don't get chains of |
| 803 | // errors. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 804 | declarator.setInvalidType(true); |
Chris Lattner | b78d833 | 2009-06-26 04:45:06 +0000 | [diff] [blame] | 805 | } else { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 806 | S.Diag(DeclLoc, diag::ext_missing_type_specifier) |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 807 | << DS.getSourceRange(); |
Chris Lattner | b78d833 | 2009-06-26 04:45:06 +0000 | [diff] [blame] | 808 | } |
Chris Lattner | d658b56 | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 809 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 810 | |
| 811 | // FALL THROUGH. |
Chris Lattner | 3cbc38b | 2007-08-21 17:02:28 +0000 | [diff] [blame] | 812 | case DeclSpec::TST_int: { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 813 | if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) { |
| 814 | switch (DS.getTypeSpecWidth()) { |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 815 | case DeclSpec::TSW_unspecified: Result = Context.IntTy; break; |
| 816 | case DeclSpec::TSW_short: Result = Context.ShortTy; break; |
| 817 | case DeclSpec::TSW_long: Result = Context.LongTy; break; |
Chris Lattner | 311157f | 2009-10-25 18:25:04 +0000 | [diff] [blame] | 818 | case DeclSpec::TSW_longlong: |
| 819 | Result = Context.LongLongTy; |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 820 | |
Dmitri Gribenko | e3b136b | 2012-09-24 18:19:21 +0000 | [diff] [blame] | 821 | // 'long long' is a C99 or C++11 feature. |
| 822 | if (!S.getLangOpts().C99) { |
| 823 | if (S.getLangOpts().CPlusPlus) |
| 824 | S.Diag(DS.getTypeSpecWidthLoc(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 825 | S.getLangOpts().CPlusPlus11 ? |
Dmitri Gribenko | e3b136b | 2012-09-24 18:19:21 +0000 | [diff] [blame] | 826 | diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong); |
| 827 | else |
| 828 | S.Diag(DS.getTypeSpecWidthLoc(), diag::ext_c99_longlong); |
| 829 | } |
Chris Lattner | 311157f | 2009-10-25 18:25:04 +0000 | [diff] [blame] | 830 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 831 | } |
| 832 | } else { |
| 833 | switch (DS.getTypeSpecWidth()) { |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 834 | case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break; |
| 835 | case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break; |
| 836 | case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break; |
Chris Lattner | 311157f | 2009-10-25 18:25:04 +0000 | [diff] [blame] | 837 | case DeclSpec::TSW_longlong: |
| 838 | Result = Context.UnsignedLongLongTy; |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 839 | |
Dmitri Gribenko | e3b136b | 2012-09-24 18:19:21 +0000 | [diff] [blame] | 840 | // 'long long' is a C99 or C++11 feature. |
| 841 | if (!S.getLangOpts().C99) { |
| 842 | if (S.getLangOpts().CPlusPlus) |
| 843 | S.Diag(DS.getTypeSpecWidthLoc(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 844 | S.getLangOpts().CPlusPlus11 ? |
Dmitri Gribenko | e3b136b | 2012-09-24 18:19:21 +0000 | [diff] [blame] | 845 | diag::warn_cxx98_compat_longlong : diag::ext_cxx11_longlong); |
| 846 | else |
| 847 | S.Diag(DS.getTypeSpecWidthLoc(), diag::ext_c99_longlong); |
| 848 | } |
Chris Lattner | 311157f | 2009-10-25 18:25:04 +0000 | [diff] [blame] | 849 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 850 | } |
| 851 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 852 | break; |
Chris Lattner | 3cbc38b | 2007-08-21 17:02:28 +0000 | [diff] [blame] | 853 | } |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 854 | case DeclSpec::TST_int128: |
Richard Smith | 8426890 | 2012-11-29 05:41:51 +0000 | [diff] [blame] | 855 | if (!S.PP.getTargetInfo().hasInt128Type()) |
| 856 | S.Diag(DS.getTypeSpecTypeLoc(), diag::err_int128_unsupported); |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 857 | if (DS.getTypeSpecSign() == DeclSpec::TSS_unsigned) |
| 858 | Result = Context.UnsignedInt128Ty; |
| 859 | else |
| 860 | Result = Context.Int128Ty; |
| 861 | break; |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 862 | case DeclSpec::TST_half: Result = Context.HalfTy; break; |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 863 | case DeclSpec::TST_float: Result = Context.FloatTy; break; |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 864 | case DeclSpec::TST_double: |
| 865 | if (DS.getTypeSpecWidth() == DeclSpec::TSW_long) |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 866 | Result = Context.LongDoubleTy; |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 867 | else |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 868 | Result = Context.DoubleTy; |
Peter Collingbourne | 39d3e7a | 2011-02-15 19:46:23 +0000 | [diff] [blame] | 869 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 870 | if (S.getLangOpts().OpenCL && !S.getOpenCLOptions().cl_khr_fp64) { |
Peter Collingbourne | 39d3e7a | 2011-02-15 19:46:23 +0000 | [diff] [blame] | 871 | S.Diag(DS.getTypeSpecTypeLoc(), diag::err_double_requires_fp64); |
| 872 | declarator.setInvalidType(true); |
| 873 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 874 | break; |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 875 | case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 876 | case DeclSpec::TST_decimal32: // _Decimal32 |
| 877 | case DeclSpec::TST_decimal64: // _Decimal64 |
| 878 | case DeclSpec::TST_decimal128: // _Decimal128 |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 879 | S.Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported); |
Chris Lattner | 8f12f65 | 2009-05-13 05:02:08 +0000 | [diff] [blame] | 880 | Result = Context.IntTy; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 881 | declarator.setInvalidType(true); |
Chris Lattner | 8f12f65 | 2009-05-13 05:02:08 +0000 | [diff] [blame] | 882 | break; |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 883 | case DeclSpec::TST_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 884 | case DeclSpec::TST_enum: |
| 885 | case DeclSpec::TST_union: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 886 | case DeclSpec::TST_struct: |
| 887 | case DeclSpec::TST_interface: { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 888 | TypeDecl *D = dyn_cast_or_null<TypeDecl>(DS.getRepAsDecl()); |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 889 | if (!D) { |
| 890 | // This can happen in C++ with ambiguous lookups. |
| 891 | Result = Context.IntTy; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 892 | declarator.setInvalidType(true); |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 893 | break; |
| 894 | } |
| 895 | |
Chris Lattner | a64ef0a | 2009-10-25 22:09:09 +0000 | [diff] [blame] | 896 | // If the type is deprecated or unavailable, diagnose it. |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 897 | S.DiagnoseUseOfDecl(D, DS.getTypeSpecTypeNameLoc()); |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 898 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 899 | assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 && |
Chris Lattner | a64ef0a | 2009-10-25 22:09:09 +0000 | [diff] [blame] | 900 | DS.getTypeSpecSign() == 0 && "No qualifiers on tag names!"); |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 901 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 902 | // TypeQuals handled by caller. |
Chris Lattner | a64ef0a | 2009-10-25 22:09:09 +0000 | [diff] [blame] | 903 | Result = Context.getTypeDeclType(D); |
John McCall | 2191b20 | 2009-09-05 06:31:47 +0000 | [diff] [blame] | 904 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 905 | // In both C and C++, make an ElaboratedType. |
| 906 | ElaboratedTypeKeyword Keyword |
| 907 | = ElaboratedType::getKeywordForTypeSpec(DS.getTypeSpecType()); |
| 908 | Result = S.getElaboratedType(Keyword, DS.getTypeSpecScope(), Result); |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 909 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 910 | } |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 911 | case DeclSpec::TST_typename: { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 912 | assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 && |
| 913 | DS.getTypeSpecSign() == 0 && |
| 914 | "Can't handle qualifiers on typedef names yet!"); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 915 | Result = S.GetTypeFromParser(DS.getRepAsType()); |
John McCall | 27940d2 | 2010-07-30 05:17:22 +0000 | [diff] [blame] | 916 | if (Result.isNull()) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 917 | declarator.setInvalidType(true); |
John McCall | 27940d2 | 2010-07-30 05:17:22 +0000 | [diff] [blame] | 918 | else if (DeclSpec::ProtocolQualifierListTy PQ |
| 919 | = DS.getProtocolQualifiers()) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 920 | if (const ObjCObjectType *ObjT = Result->getAs<ObjCObjectType>()) { |
| 921 | // Silently drop any existing protocol qualifiers. |
| 922 | // TODO: determine whether that's the right thing to do. |
| 923 | if (ObjT->getNumProtocols()) |
| 924 | Result = ObjT->getBaseType(); |
| 925 | |
| 926 | if (DS.getNumProtocolQualifiers()) |
| 927 | Result = Context.getObjCObjectType(Result, |
Roman Divacky | 31ba613 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 928 | (ObjCProtocolDecl*const*) PQ, |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 929 | DS.getNumProtocolQualifiers()); |
| 930 | } else if (Result->isObjCIdType()) { |
Chris Lattner | ae4da61 | 2008-07-26 01:53:50 +0000 | [diff] [blame] | 931 | // id<protocol-list> |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 932 | Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy, |
Roman Divacky | 31ba613 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 933 | (ObjCProtocolDecl*const*) PQ, |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 934 | DS.getNumProtocolQualifiers()); |
| 935 | Result = Context.getObjCObjectPointerType(Result); |
| 936 | } else if (Result->isObjCClassType()) { |
Steve Naroff | 4262a07 | 2009-02-23 18:53:24 +0000 | [diff] [blame] | 937 | // Class<protocol-list> |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 938 | Result = Context.getObjCObjectType(Context.ObjCBuiltinClassTy, |
Roman Divacky | 31ba613 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 939 | (ObjCProtocolDecl*const*) PQ, |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 940 | DS.getNumProtocolQualifiers()); |
| 941 | Result = Context.getObjCObjectPointerType(Result); |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 942 | } else { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 943 | S.Diag(DeclLoc, diag::err_invalid_protocol_qualifiers) |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 944 | << DS.getSourceRange(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 945 | declarator.setInvalidType(true); |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 946 | } |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 947 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 948 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 949 | // TypeQuals handled by caller. |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 950 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 951 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 952 | case DeclSpec::TST_typeofType: |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 953 | // FIXME: Preserve type source info. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 954 | Result = S.GetTypeFromParser(DS.getRepAsType()); |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 955 | assert(!Result.isNull() && "Didn't get a type for typeof?"); |
Fariborz Jahanian | 730e175 | 2010-10-06 17:00:02 +0000 | [diff] [blame] | 956 | if (!Result->isDependentType()) |
| 957 | if (const TagType *TT = Result->getAs<TagType>()) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 958 | S.DiagnoseUseOfDecl(TT->getDecl(), DS.getTypeSpecTypeLoc()); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 959 | // TypeQuals handled by caller. |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 960 | Result = Context.getTypeOfType(Result); |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 961 | break; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 962 | case DeclSpec::TST_typeofExpr: { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 963 | Expr *E = DS.getRepAsExpr(); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 964 | assert(E && "Didn't get an expression for typeof?"); |
| 965 | // TypeQuals handled by caller. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 966 | Result = S.BuildTypeofExprType(E, DS.getTypeSpecTypeLoc()); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 967 | if (Result.isNull()) { |
| 968 | Result = Context.IntTy; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 969 | declarator.setInvalidType(true); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 970 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 971 | break; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 972 | } |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 973 | case DeclSpec::TST_decltype: { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 974 | Expr *E = DS.getRepAsExpr(); |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 975 | assert(E && "Didn't get an expression for decltype?"); |
| 976 | // TypeQuals handled by caller. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 977 | Result = S.BuildDecltypeType(E, DS.getTypeSpecTypeLoc()); |
Anders Carlsson | af017e6 | 2009-06-29 22:58:55 +0000 | [diff] [blame] | 978 | if (Result.isNull()) { |
| 979 | Result = Context.IntTy; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 980 | declarator.setInvalidType(true); |
Anders Carlsson | af017e6 | 2009-06-29 22:58:55 +0000 | [diff] [blame] | 981 | } |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 982 | break; |
| 983 | } |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 984 | case DeclSpec::TST_underlyingType: |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 985 | Result = S.GetTypeFromParser(DS.getRepAsType()); |
| 986 | assert(!Result.isNull() && "Didn't get a type for __underlying_type?"); |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 987 | Result = S.BuildUnaryTransformType(Result, |
| 988 | UnaryTransformType::EnumUnderlyingType, |
| 989 | DS.getTypeSpecTypeLoc()); |
| 990 | if (Result.isNull()) { |
| 991 | Result = Context.IntTy; |
| 992 | declarator.setInvalidType(true); |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 993 | } |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 994 | break; |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 995 | |
Anders Carlsson | e89d159 | 2009-06-26 18:41:36 +0000 | [diff] [blame] | 996 | case DeclSpec::TST_auto: { |
| 997 | // TypeQuals handled by caller. |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 998 | Result = Context.getAutoType(QualType()); |
Anders Carlsson | e89d159 | 2009-06-26 18:41:36 +0000 | [diff] [blame] | 999 | break; |
| 1000 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1001 | |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 1002 | case DeclSpec::TST_unknown_anytype: |
| 1003 | Result = Context.UnknownAnyTy; |
| 1004 | break; |
| 1005 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1006 | case DeclSpec::TST_atomic: |
| 1007 | Result = S.GetTypeFromParser(DS.getRepAsType()); |
| 1008 | assert(!Result.isNull() && "Didn't get a type for _Atomic?"); |
| 1009 | Result = S.BuildAtomicType(Result, DS.getTypeSpecTypeLoc()); |
| 1010 | if (Result.isNull()) { |
| 1011 | Result = Context.IntTy; |
| 1012 | declarator.setInvalidType(true); |
| 1013 | } |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 1014 | break; |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1015 | |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 1016 | case DeclSpec::TST_image1d_t: |
| 1017 | Result = Context.OCLImage1dTy; |
| 1018 | break; |
| 1019 | |
| 1020 | case DeclSpec::TST_image1d_array_t: |
| 1021 | Result = Context.OCLImage1dArrayTy; |
| 1022 | break; |
| 1023 | |
| 1024 | case DeclSpec::TST_image1d_buffer_t: |
| 1025 | Result = Context.OCLImage1dBufferTy; |
| 1026 | break; |
| 1027 | |
| 1028 | case DeclSpec::TST_image2d_t: |
| 1029 | Result = Context.OCLImage2dTy; |
| 1030 | break; |
| 1031 | |
| 1032 | case DeclSpec::TST_image2d_array_t: |
| 1033 | Result = Context.OCLImage2dArrayTy; |
| 1034 | break; |
| 1035 | |
| 1036 | case DeclSpec::TST_image3d_t: |
| 1037 | Result = Context.OCLImage3dTy; |
| 1038 | break; |
| 1039 | |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 1040 | case DeclSpec::TST_sampler_t: |
| 1041 | Result = Context.OCLSamplerTy; |
| 1042 | break; |
| 1043 | |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 1044 | case DeclSpec::TST_event_t: |
| 1045 | Result = Context.OCLEventTy; |
| 1046 | break; |
| 1047 | |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 1048 | case DeclSpec::TST_error: |
Chris Lattner | 5153ee6 | 2009-04-25 08:47:54 +0000 | [diff] [blame] | 1049 | Result = Context.IntTy; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1050 | declarator.setInvalidType(true); |
Chris Lattner | 5153ee6 | 2009-04-25 08:47:54 +0000 | [diff] [blame] | 1051 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1052 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1053 | |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 1054 | // Handle complex types. |
Douglas Gregor | f244cd7 | 2009-02-14 21:06:05 +0000 | [diff] [blame] | 1055 | if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1056 | if (S.getLangOpts().Freestanding) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1057 | S.Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex); |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 1058 | Result = Context.getComplexType(Result); |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 1059 | } else if (DS.isTypeAltiVecVector()) { |
| 1060 | unsigned typeSize = static_cast<unsigned>(Context.getTypeSize(Result)); |
| 1061 | assert(typeSize > 0 && "type size for vector must be greater than 0 bits"); |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1062 | VectorType::VectorKind VecKind = VectorType::AltiVecVector; |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1063 | if (DS.isTypeAltiVecPixel()) |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1064 | VecKind = VectorType::AltiVecPixel; |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 1065 | else if (DS.isTypeAltiVecBool()) |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1066 | VecKind = VectorType::AltiVecBool; |
| 1067 | Result = Context.getVectorType(Result, 128/typeSize, VecKind); |
Douglas Gregor | f244cd7 | 2009-02-14 21:06:05 +0000 | [diff] [blame] | 1068 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1069 | |
Argyrios Kyrtzidis | 47423bd | 2010-09-23 09:40:31 +0000 | [diff] [blame] | 1070 | // FIXME: Imaginary. |
| 1071 | if (DS.getTypeSpecComplex() == DeclSpec::TSC_imaginary) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1072 | S.Diag(DS.getTypeSpecComplexLoc(), diag::err_imaginary_not_supported); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1073 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1074 | // Before we process any type attributes, synthesize a block literal |
| 1075 | // function declarator if necessary. |
| 1076 | if (declarator.getContext() == Declarator::BlockLiteralContext) |
| 1077 | maybeSynthesizeBlockSignature(state, Result); |
| 1078 | |
| 1079 | // Apply any type attributes from the decl spec. This may cause the |
| 1080 | // list of type attributes to be temporarily saved while the type |
| 1081 | // attributes are pushed around. |
| 1082 | if (AttributeList *attrs = DS.getAttributes().getList()) |
Richard Smith | f7a0527 | 2013-01-14 07:53:01 +0000 | [diff] [blame] | 1083 | processTypeAttrs(state, Result, TAL_DeclSpec, attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1084 | |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 1085 | // Apply const/volatile/restrict qualifiers to T. |
| 1086 | if (unsigned TypeQuals = DS.getTypeQualifiers()) { |
| 1087 | |
| 1088 | // Enforce C99 6.7.3p2: "Types other than pointer types derived from object |
| 1089 | // or incomplete types shall not be restrict-qualified." C++ also allows |
| 1090 | // restrict-qualified references. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1091 | if (TypeQuals & DeclSpec::TQ_restrict) { |
Fariborz Jahanian | 2b5ff1a | 2009-12-07 18:08:58 +0000 | [diff] [blame] | 1092 | if (Result->isAnyPointerType() || Result->isReferenceType()) { |
| 1093 | QualType EltTy; |
| 1094 | if (Result->isObjCObjectPointerType()) |
| 1095 | EltTy = Result; |
| 1096 | else |
| 1097 | EltTy = Result->isPointerType() ? |
| 1098 | Result->getAs<PointerType>()->getPointeeType() : |
| 1099 | Result->getAs<ReferenceType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1100 | |
Douglas Gregor | bad0e65 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 1101 | // If we have a pointer or reference, the pointee must have an object |
Chris Lattner | bdcd637 | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 1102 | // incomplete type. |
| 1103 | if (!EltTy->isIncompleteOrObjectType()) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1104 | S.Diag(DS.getRestrictSpecLoc(), |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 1105 | diag::err_typecheck_invalid_restrict_invalid_pointee) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 1106 | << EltTy << DS.getSourceRange(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1107 | TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier. |
Chris Lattner | bdcd637 | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 1108 | } |
| 1109 | } else { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1110 | S.Diag(DS.getRestrictSpecLoc(), |
| 1111 | diag::err_typecheck_invalid_restrict_not_pointer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 1112 | << Result << DS.getSourceRange(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1113 | TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier. |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 1114 | } |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 1115 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1116 | |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 1117 | // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification |
| 1118 | // of a function type includes any type qualifiers, the behavior is |
| 1119 | // undefined." |
| 1120 | if (Result->isFunctionType() && TypeQuals) { |
| 1121 | // Get some location to point at, either the C or V location. |
| 1122 | SourceLocation Loc; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1123 | if (TypeQuals & DeclSpec::TQ_const) |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 1124 | Loc = DS.getConstSpecLoc(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1125 | else if (TypeQuals & DeclSpec::TQ_volatile) |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 1126 | Loc = DS.getVolatileSpecLoc(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1127 | else { |
| 1128 | assert((TypeQuals & DeclSpec::TQ_restrict) && |
| 1129 | "Has CVR quals but not C, V, or R?"); |
| 1130 | Loc = DS.getRestrictSpecLoc(); |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 1131 | } |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1132 | S.Diag(Loc, diag::warn_typecheck_function_qualifiers) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 1133 | << Result << DS.getSourceRange(); |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 1134 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1135 | |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 1136 | // C++ [dcl.ref]p1: |
| 1137 | // Cv-qualified references are ill-formed except when the |
| 1138 | // cv-qualifiers are introduced through the use of a typedef |
| 1139 | // (7.1.3) or of a template type argument (14.3), in which |
| 1140 | // case the cv-qualifiers are ignored. |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 1141 | // FIXME: Shouldn't we be checking SCS_typedef here? |
| 1142 | if (DS.getTypeSpecType() == DeclSpec::TST_typename && |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 1143 | TypeQuals && Result->isReferenceType()) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1144 | TypeQuals &= ~DeclSpec::TQ_const; |
| 1145 | TypeQuals &= ~DeclSpec::TQ_volatile; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
Eli Friedman | bc1029b | 2012-04-05 22:47:34 +0000 | [diff] [blame] | 1148 | // C90 6.5.3 constraints: "The same type qualifier shall not appear more |
| 1149 | // than once in the same specifier-list or qualifier-list, either directly |
| 1150 | // or via one or more typedefs." |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 1151 | if (!S.getLangOpts().C99 && !S.getLangOpts().CPlusPlus |
Eli Friedman | bc1029b | 2012-04-05 22:47:34 +0000 | [diff] [blame] | 1152 | && TypeQuals & Result.getCVRQualifiers()) { |
| 1153 | if (TypeQuals & DeclSpec::TQ_const && Result.isConstQualified()) { |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 1154 | S.Diag(DS.getConstSpecLoc(), diag::ext_duplicate_declspec) |
Eli Friedman | bc1029b | 2012-04-05 22:47:34 +0000 | [diff] [blame] | 1155 | << "const"; |
| 1156 | } |
| 1157 | |
| 1158 | if (TypeQuals & DeclSpec::TQ_volatile && Result.isVolatileQualified()) { |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 1159 | S.Diag(DS.getVolatileSpecLoc(), diag::ext_duplicate_declspec) |
Eli Friedman | bc1029b | 2012-04-05 22:47:34 +0000 | [diff] [blame] | 1160 | << "volatile"; |
| 1161 | } |
| 1162 | |
| 1163 | // C90 doesn't have restrict, so it doesn't force us to produce a warning |
| 1164 | // in this case. |
| 1165 | } |
| 1166 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1167 | Qualifiers Quals = Qualifiers::fromCVRMask(TypeQuals); |
| 1168 | Result = Context.getQualifiedType(Result, Quals); |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 1169 | } |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1170 | |
Chris Lattner | f1d705c | 2008-02-21 01:07:18 +0000 | [diff] [blame] | 1171 | return Result; |
| 1172 | } |
| 1173 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1174 | static std::string getPrintableNameForEntity(DeclarationName Entity) { |
| 1175 | if (Entity) |
| 1176 | return Entity.getAsString(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1177 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1178 | return "type name"; |
| 1179 | } |
| 1180 | |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1181 | QualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc, |
| 1182 | Qualifiers Qs) { |
| 1183 | // Enforce C99 6.7.3p2: "Types other than pointer types derived from |
| 1184 | // object or incomplete types shall not be restrict-qualified." |
| 1185 | if (Qs.hasRestrict()) { |
| 1186 | unsigned DiagID = 0; |
| 1187 | QualType ProblemTy; |
| 1188 | |
| 1189 | const Type *Ty = T->getCanonicalTypeInternal().getTypePtr(); |
| 1190 | if (const ReferenceType *RTy = dyn_cast<ReferenceType>(Ty)) { |
| 1191 | if (!RTy->getPointeeType()->isIncompleteOrObjectType()) { |
| 1192 | DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee; |
| 1193 | ProblemTy = T->getAs<ReferenceType>()->getPointeeType(); |
| 1194 | } |
| 1195 | } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) { |
| 1196 | if (!PTy->getPointeeType()->isIncompleteOrObjectType()) { |
| 1197 | DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee; |
| 1198 | ProblemTy = T->getAs<PointerType>()->getPointeeType(); |
| 1199 | } |
| 1200 | } else if (const MemberPointerType *PTy = dyn_cast<MemberPointerType>(Ty)) { |
| 1201 | if (!PTy->getPointeeType()->isIncompleteOrObjectType()) { |
| 1202 | DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee; |
| 1203 | ProblemTy = T->getAs<PointerType>()->getPointeeType(); |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 1204 | } |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1205 | } else if (!Ty->isDependentType()) { |
| 1206 | // FIXME: this deserves a proper diagnostic |
| 1207 | DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee; |
| 1208 | ProblemTy = T; |
| 1209 | } |
| 1210 | |
| 1211 | if (DiagID) { |
| 1212 | Diag(Loc, DiagID) << ProblemTy; |
| 1213 | Qs.removeRestrict(); |
| 1214 | } |
| 1215 | } |
| 1216 | |
| 1217 | return Context.getQualifiedType(T, Qs); |
| 1218 | } |
| 1219 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 1220 | /// \brief Build a paren type including \p T. |
| 1221 | QualType Sema::BuildParenType(QualType T) { |
| 1222 | return Context.getParenType(T); |
| 1223 | } |
| 1224 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1225 | /// Given that we're building a pointer or reference to the given |
| 1226 | static QualType inferARCLifetimeForPointee(Sema &S, QualType type, |
| 1227 | SourceLocation loc, |
| 1228 | bool isReference) { |
| 1229 | // Bail out if retention is unrequired or already specified. |
| 1230 | if (!type->isObjCLifetimeType() || |
| 1231 | type.getObjCLifetime() != Qualifiers::OCL_None) |
| 1232 | return type; |
| 1233 | |
| 1234 | Qualifiers::ObjCLifetime implicitLifetime = Qualifiers::OCL_None; |
| 1235 | |
| 1236 | // If the object type is const-qualified, we can safely use |
| 1237 | // __unsafe_unretained. This is safe (because there are no read |
| 1238 | // barriers), and it'll be safe to coerce anything but __weak* to |
| 1239 | // the resulting type. |
| 1240 | if (type.isConstQualified()) { |
| 1241 | implicitLifetime = Qualifiers::OCL_ExplicitNone; |
| 1242 | |
| 1243 | // Otherwise, check whether the static type does not require |
| 1244 | // retaining. This currently only triggers for Class (possibly |
| 1245 | // protocol-qualifed, and arrays thereof). |
| 1246 | } else if (type->isObjCARCImplicitlyUnretainedType()) { |
| 1247 | implicitLifetime = Qualifiers::OCL_ExplicitNone; |
| 1248 | |
Eli Friedman | ef331b7 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 1249 | // If we are in an unevaluated context, like sizeof, skip adding a |
| 1250 | // qualification. |
David Blaikie | 71f55f7 | 2012-08-06 22:47:24 +0000 | [diff] [blame] | 1251 | } else if (S.isUnevaluatedContext()) { |
Eli Friedman | ef331b7 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 1252 | return type; |
Argyrios Kyrtzidis | 5b76f37 | 2011-09-20 23:49:22 +0000 | [diff] [blame] | 1253 | |
John McCall | e8c904f | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 1254 | // If that failed, give an error and recover using __strong. __strong |
| 1255 | // is the option most likely to prevent spurious second-order diagnostics, |
| 1256 | // like when binding a reference to a field. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1257 | } else { |
| 1258 | // These types can show up in private ivars in system headers, so |
| 1259 | // we need this to not be an error in those cases. Instead we |
| 1260 | // want to delay. |
| 1261 | if (S.DelayedDiagnostics.shouldDelayDiagnostics()) { |
Eli Friedman | ef331b7 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 1262 | S.DelayedDiagnostics.add( |
| 1263 | sema::DelayedDiagnostic::makeForbiddenType(loc, |
| 1264 | diag::err_arc_indirect_no_ownership, type, isReference)); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1265 | } else { |
Eli Friedman | ef331b7 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 1266 | S.Diag(loc, diag::err_arc_indirect_no_ownership) << type << isReference; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1267 | } |
John McCall | e8c904f | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 1268 | implicitLifetime = Qualifiers::OCL_Strong; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1269 | } |
| 1270 | assert(implicitLifetime && "didn't infer any lifetime!"); |
| 1271 | |
| 1272 | Qualifiers qs; |
| 1273 | qs.addObjCLifetime(implicitLifetime); |
| 1274 | return S.Context.getQualifiedType(type, qs); |
| 1275 | } |
| 1276 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1277 | /// \brief Build a pointer type. |
| 1278 | /// |
| 1279 | /// \param T The type to which we'll be building a pointer. |
| 1280 | /// |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1281 | /// \param Loc The location of the entity whose type involves this |
| 1282 | /// pointer type or, if there is no such entity, the location of the |
| 1283 | /// type that will have pointer type. |
| 1284 | /// |
| 1285 | /// \param Entity The name of the entity that involves the pointer |
| 1286 | /// type, if known. |
| 1287 | /// |
| 1288 | /// \returns A suitable pointer type, if there are no |
| 1289 | /// errors. Otherwise, returns a NULL type. |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1290 | QualType Sema::BuildPointerType(QualType T, |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1291 | SourceLocation Loc, DeclarationName Entity) { |
| 1292 | if (T->isReferenceType()) { |
| 1293 | // C++ 8.3.2p4: There shall be no ... pointers to references ... |
| 1294 | Diag(Loc, diag::err_illegal_decl_pointer_to_reference) |
John McCall | ac40605 | 2009-10-30 00:37:20 +0000 | [diff] [blame] | 1295 | << getPrintableNameForEntity(Entity) << T; |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1296 | return QualType(); |
| 1297 | } |
| 1298 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1299 | assert(!T->isObjCObjectType() && "Should build ObjCObjectPointerType"); |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 1300 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1301 | // In ARC, it is forbidden to build pointers to unqualified pointers. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1302 | if (getLangOpts().ObjCAutoRefCount) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1303 | T = inferARCLifetimeForPointee(*this, T, Loc, /*reference*/ false); |
| 1304 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1305 | // Build the pointer type. |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1306 | return Context.getPointerType(T); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
| 1309 | /// \brief Build a reference type. |
| 1310 | /// |
| 1311 | /// \param T The type to which we'll be building a reference. |
| 1312 | /// |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1313 | /// \param Loc The location of the entity whose type involves this |
| 1314 | /// reference type or, if there is no such entity, the location of the |
| 1315 | /// type that will have reference type. |
| 1316 | /// |
| 1317 | /// \param Entity The name of the entity that involves the reference |
| 1318 | /// type, if known. |
| 1319 | /// |
| 1320 | /// \returns A suitable reference type, if there are no |
| 1321 | /// errors. Otherwise, returns a NULL type. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1322 | QualType Sema::BuildReferenceType(QualType T, bool SpelledAsLValue, |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1323 | SourceLocation Loc, |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1324 | DeclarationName Entity) { |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 1325 | assert(Context.getCanonicalType(T) != Context.OverloadTy && |
Douglas Gregor | 9625e44 | 2011-05-21 22:16:50 +0000 | [diff] [blame] | 1326 | "Unresolved overloaded function type"); |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 1327 | |
Douglas Gregor | 69d8316 | 2011-01-20 16:08:06 +0000 | [diff] [blame] | 1328 | // C++0x [dcl.ref]p6: |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 1329 | // If a typedef (7.1.3), a type template-parameter (14.3.1), or a |
| 1330 | // decltype-specifier (7.1.6.2) denotes a type TR that is a reference to a |
| 1331 | // type T, an attempt to create the type "lvalue reference to cv TR" creates |
| 1332 | // the type "lvalue reference to T", while an attempt to create the type |
Douglas Gregor | 69d8316 | 2011-01-20 16:08:06 +0000 | [diff] [blame] | 1333 | // "rvalue reference to cv TR" creates the type TR. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1334 | bool LValueRef = SpelledAsLValue || T->getAs<LValueReferenceType>(); |
| 1335 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1336 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 1337 | // |
| 1338 | // According to C++ DR 106, references to references are only |
| 1339 | // diagnosed when they are written directly (e.g., "int & &"), |
| 1340 | // but not when they happen via a typedef: |
| 1341 | // |
| 1342 | // typedef int& intref; |
| 1343 | // typedef intref& intref2; |
| 1344 | // |
| 1345 | // Parser::ParseDeclaratorInternal diagnoses the case where |
| 1346 | // references are written directly; here, we handle the |
Douglas Gregor | 69d8316 | 2011-01-20 16:08:06 +0000 | [diff] [blame] | 1347 | // collapsing of references-to-references as described in C++0x. |
| 1348 | // DR 106 and 540 introduce reference-collapsing into C++98/03. |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1349 | |
| 1350 | // C++ [dcl.ref]p1: |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 1351 | // A declarator that specifies the type "reference to cv void" |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1352 | // is ill-formed. |
| 1353 | if (T->isVoidType()) { |
| 1354 | Diag(Loc, diag::err_reference_to_void); |
| 1355 | return QualType(); |
| 1356 | } |
| 1357 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1358 | // In ARC, it is forbidden to build references to unqualified pointers. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1359 | if (getLangOpts().ObjCAutoRefCount) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1360 | T = inferARCLifetimeForPointee(*this, T, Loc, /*reference*/ true); |
| 1361 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1362 | // Handle restrict on references. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1363 | if (LValueRef) |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1364 | return Context.getLValueReferenceType(T, SpelledAsLValue); |
| 1365 | return Context.getRValueReferenceType(T); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1366 | } |
| 1367 | |
Chris Lattner | e1eed38 | 2011-06-14 06:38:10 +0000 | [diff] [blame] | 1368 | /// Check whether the specified array size makes the array type a VLA. If so, |
| 1369 | /// return true, if not, return the size of the array in SizeVal. |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 1370 | static bool isArraySizeVLA(Sema &S, Expr *ArraySize, llvm::APSInt &SizeVal) { |
| 1371 | // If the size is an ICE, it certainly isn't a VLA. If we're in a GNU mode |
| 1372 | // (like gnu99, but not c99) accept any evaluatable value as an extension. |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 1373 | class VLADiagnoser : public Sema::VerifyICEDiagnoser { |
| 1374 | public: |
| 1375 | VLADiagnoser() : Sema::VerifyICEDiagnoser(true) {} |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 1376 | |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 1377 | virtual void diagnoseNotICE(Sema &S, SourceLocation Loc, SourceRange SR) { |
| 1378 | } |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 1379 | |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 1380 | virtual void diagnoseFold(Sema &S, SourceLocation Loc, SourceRange SR) { |
| 1381 | S.Diag(Loc, diag::ext_vla_folded_to_constant) << SR; |
| 1382 | } |
| 1383 | } Diagnoser; |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 1384 | |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 1385 | return S.VerifyIntegerConstantExpression(ArraySize, &SizeVal, Diagnoser, |
| 1386 | S.LangOpts.GNUMode).isInvalid(); |
Chris Lattner | e1eed38 | 2011-06-14 06:38:10 +0000 | [diff] [blame] | 1387 | } |
| 1388 | |
| 1389 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1390 | /// \brief Build an array type. |
| 1391 | /// |
| 1392 | /// \param T The type of each element in the array. |
| 1393 | /// |
| 1394 | /// \param ASM C99 array size modifier (e.g., '*', 'static'). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1395 | /// |
| 1396 | /// \param ArraySize Expression describing the size of the array. |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1397 | /// |
James Dennett | efce31f | 2012-06-22 08:10:18 +0000 | [diff] [blame] | 1398 | /// \param Brackets The range from the opening '[' to the closing ']'. |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1399 | /// |
| 1400 | /// \param Entity The name of the entity that involves the array |
| 1401 | /// type, if known. |
| 1402 | /// |
| 1403 | /// \returns A suitable array type, if there are no errors. Otherwise, |
| 1404 | /// returns a NULL type. |
| 1405 | QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM, |
| 1406 | Expr *ArraySize, unsigned Quals, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1407 | SourceRange Brackets, DeclarationName Entity) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1408 | |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1409 | SourceLocation Loc = Brackets.getBegin(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1410 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | 138bb23 | 2010-04-27 19:38:14 +0000 | [diff] [blame] | 1411 | // C++ [dcl.array]p1: |
| 1412 | // T is called the array element type; this type shall not be a reference |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 1413 | // type, the (possibly cv-qualified) type void, a function type or an |
Douglas Gregor | 138bb23 | 2010-04-27 19:38:14 +0000 | [diff] [blame] | 1414 | // abstract class type. |
| 1415 | // |
Richard Smith | bb35151 | 2012-07-07 23:00:31 +0000 | [diff] [blame] | 1416 | // C++ [dcl.array]p3: |
| 1417 | // When several "array of" specifications are adjacent, [...] only the |
| 1418 | // first of the constant expressions that specify the bounds of the arrays |
| 1419 | // may be omitted. |
| 1420 | // |
Douglas Gregor | 138bb23 | 2010-04-27 19:38:14 +0000 | [diff] [blame] | 1421 | // Note: function types are handled in the common path with C. |
| 1422 | if (T->isReferenceType()) { |
| 1423 | Diag(Loc, diag::err_illegal_decl_array_of_references) |
| 1424 | << getPrintableNameForEntity(Entity) << T; |
| 1425 | return QualType(); |
| 1426 | } |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 1427 | |
Richard Smith | bb35151 | 2012-07-07 23:00:31 +0000 | [diff] [blame] | 1428 | if (T->isVoidType() || T->isIncompleteArrayType()) { |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1429 | Diag(Loc, diag::err_illegal_decl_array_incomplete_type) << T; |
| 1430 | return QualType(); |
| 1431 | } |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 1432 | |
| 1433 | if (RequireNonAbstractType(Brackets.getBegin(), T, |
Douglas Gregor | 138bb23 | 2010-04-27 19:38:14 +0000 | [diff] [blame] | 1434 | diag::err_array_of_abstract_type)) |
| 1435 | return QualType(); |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 1436 | |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1437 | } else { |
Douglas Gregor | 138bb23 | 2010-04-27 19:38:14 +0000 | [diff] [blame] | 1438 | // C99 6.7.5.2p1: If the element type is an incomplete or function type, |
| 1439 | // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]()) |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1440 | if (RequireCompleteType(Loc, T, |
| 1441 | diag::err_illegal_decl_array_incomplete_type)) |
| 1442 | return QualType(); |
| 1443 | } |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1444 | |
| 1445 | if (T->isFunctionType()) { |
| 1446 | Diag(Loc, diag::err_illegal_decl_array_of_functions) |
John McCall | ac40605 | 2009-10-30 00:37:20 +0000 | [diff] [blame] | 1447 | << getPrintableNameForEntity(Entity) << T; |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1448 | return QualType(); |
| 1449 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1450 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1451 | if (T->getContainedAutoType()) { |
| 1452 | Diag(Loc, diag::err_illegal_decl_array_of_auto) |
| 1453 | << getPrintableNameForEntity(Entity) << T; |
Anders Carlsson | e7cf07d | 2009-06-26 19:33:28 +0000 | [diff] [blame] | 1454 | return QualType(); |
| 1455 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1456 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1457 | if (const RecordType *EltTy = T->getAs<RecordType>()) { |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1458 | // If the element type is a struct or union that contains a variadic |
| 1459 | // array, accept it as a GNU extension: C99 6.7.2.1p2. |
| 1460 | if (EltTy->getDecl()->hasFlexibleArrayMember()) |
| 1461 | Diag(Loc, diag::ext_flexible_array_in_array) << T; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1462 | } else if (T->isObjCObjectType()) { |
Chris Lattner | c7c11b1 | 2009-04-27 01:55:56 +0000 | [diff] [blame] | 1463 | Diag(Loc, diag::err_objc_array_of_interfaces) << T; |
| 1464 | return QualType(); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1465 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1466 | |
John McCall | 806054d | 2012-01-11 00:14:46 +0000 | [diff] [blame] | 1467 | // Do placeholder conversions on the array size expression. |
| 1468 | if (ArraySize && ArraySize->hasPlaceholderType()) { |
| 1469 | ExprResult Result = CheckPlaceholderExpr(ArraySize); |
| 1470 | if (Result.isInvalid()) return QualType(); |
| 1471 | ArraySize = Result.take(); |
| 1472 | } |
| 1473 | |
John McCall | 5e3c67b | 2010-12-15 04:42:30 +0000 | [diff] [blame] | 1474 | // Do lvalue-to-rvalue conversions on the array size expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1475 | if (ArraySize && !ArraySize->isRValue()) { |
| 1476 | ExprResult Result = DefaultLvalueConversion(ArraySize); |
| 1477 | if (Result.isInvalid()) |
| 1478 | return QualType(); |
| 1479 | |
| 1480 | ArraySize = Result.take(); |
| 1481 | } |
John McCall | 5e3c67b | 2010-12-15 04:42:30 +0000 | [diff] [blame] | 1482 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1483 | // C99 6.7.5.2p1: The size expression shall have integer type. |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 1484 | // C++11 allows contextual conversions to such types. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1485 | if (!getLangOpts().CPlusPlus11 && |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 1486 | ArraySize && !ArraySize->isTypeDependent() && |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1487 | !ArraySize->getType()->isIntegralOrUnscopedEnumerationType()) { |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1488 | Diag(ArraySize->getLocStart(), diag::err_array_size_non_int) |
| 1489 | << ArraySize->getType() << ArraySize->getSourceRange(); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1490 | return QualType(); |
| 1491 | } |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 1492 | |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 1493 | llvm::APSInt ConstVal(Context.getTypeSize(Context.getSizeType())); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1494 | if (!ArraySize) { |
Eli Friedman | f91f5c8 | 2009-04-26 21:57:51 +0000 | [diff] [blame] | 1495 | if (ASM == ArrayType::Star) |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1496 | T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets); |
Eli Friedman | f91f5c8 | 2009-04-26 21:57:51 +0000 | [diff] [blame] | 1497 | else |
| 1498 | T = Context.getIncompleteArrayType(T, ASM, Quals); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1499 | } else if (ArraySize->isTypeDependent() || ArraySize->isValueDependent()) { |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1500 | T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 1501 | } else if ((!T->isDependentType() && !T->isIncompleteType() && |
| 1502 | !T->isConstantSizeType()) || |
| 1503 | isArraySizeVLA(*this, ArraySize, ConstVal)) { |
| 1504 | // Even in C++11, don't allow contextual conversions in the array bound |
| 1505 | // of a VLA. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1506 | if (getLangOpts().CPlusPlus11 && |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 1507 | !ArraySize->getType()->isIntegralOrUnscopedEnumerationType()) { |
| 1508 | Diag(ArraySize->getLocStart(), diag::err_array_size_non_int) |
| 1509 | << ArraySize->getType() << ArraySize->getSourceRange(); |
| 1510 | return QualType(); |
| 1511 | } |
| 1512 | |
Chris Lattner | e1eed38 | 2011-06-14 06:38:10 +0000 | [diff] [blame] | 1513 | // C99: an array with an element type that has a non-constant-size is a VLA. |
Chris Lattner | e1eed38 | 2011-06-14 06:38:10 +0000 | [diff] [blame] | 1514 | // C99: an array with a non-ICE size is a VLA. We accept any expression |
| 1515 | // that we can fold to a non-zero positive value as an extension. |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1516 | T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1517 | } else { |
| 1518 | // C99 6.7.5.2p1: If the expression is a constant expression, it shall |
| 1519 | // have a value greater than zero. |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1520 | if (ConstVal.isSigned() && ConstVal.isNegative()) { |
Chandler Carruth | b2b5cc0 | 2011-01-04 04:44:35 +0000 | [diff] [blame] | 1521 | if (Entity) |
| 1522 | Diag(ArraySize->getLocStart(), diag::err_decl_negative_array_size) |
| 1523 | << getPrintableNameForEntity(Entity) << ArraySize->getSourceRange(); |
| 1524 | else |
| 1525 | Diag(ArraySize->getLocStart(), diag::err_typecheck_negative_array_size) |
| 1526 | << ArraySize->getSourceRange(); |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1527 | return QualType(); |
| 1528 | } |
| 1529 | if (ConstVal == 0) { |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 1530 | // GCC accepts zero sized static arrays. We allow them when |
| 1531 | // we're not in a SFINAE context. |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 1532 | Diag(ArraySize->getLocStart(), |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 1533 | isSFINAEContext()? diag::err_typecheck_zero_array_size |
| 1534 | : diag::ext_typecheck_zero_array_size) |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1535 | << ArraySize->getSourceRange(); |
Peter Collingbourne | 20cdbeb | 2011-10-16 21:17:32 +0000 | [diff] [blame] | 1536 | |
| 1537 | if (ASM == ArrayType::Static) { |
| 1538 | Diag(ArraySize->getLocStart(), |
| 1539 | diag::warn_typecheck_zero_static_array_size) |
| 1540 | << ArraySize->getSourceRange(); |
| 1541 | ASM = ArrayType::Normal; |
| 1542 | } |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 1543 | } else if (!T->isDependentType() && !T->isVariablyModifiedType() && |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 1544 | !T->isIncompleteType()) { |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 1545 | // Is the array too large? |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 1546 | unsigned ActiveSizeBits |
| 1547 | = ConstantArrayType::getNumAddressingBits(Context, T, ConstVal); |
| 1548 | if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context)) |
| 1549 | Diag(ArraySize->getLocStart(), diag::err_array_too_large) |
| 1550 | << ConstVal.toString(10) |
| 1551 | << ArraySize->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1552 | } |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 1553 | |
John McCall | 46a617a | 2009-10-16 00:14:28 +0000 | [diff] [blame] | 1554 | T = Context.getConstantArrayType(T, ConstVal, ASM, Quals); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1555 | } |
Joey Gouly | 617bb31 | 2013-01-17 17:35:00 +0000 | [diff] [blame] | 1556 | |
| 1557 | // OpenCL v1.2 s6.9.d: variable length arrays are not supported. |
| 1558 | if (getLangOpts().OpenCL && T->isVariableArrayType()) { |
| 1559 | Diag(Loc, diag::err_opencl_vla); |
| 1560 | return QualType(); |
| 1561 | } |
David Chisnall | af40776 | 2010-01-11 23:08:08 +0000 | [diff] [blame] | 1562 | // If this is not C99, extwarn about VLA's and C99 array size modifiers. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1563 | if (!getLangOpts().C99) { |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 1564 | if (T->isVariableArrayType()) { |
| 1565 | // Prohibit the use of non-POD types in VLAs. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1566 | QualType BaseT = Context.getBaseElementType(T); |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 1567 | if (!T->isDependentType() && |
Douglas Gregor | 3c7236e | 2013-01-08 00:01:45 +0000 | [diff] [blame] | 1568 | !BaseT.isPODType(Context) && |
| 1569 | !BaseT->isObjCLifetimeType()) { |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 1570 | Diag(Loc, diag::err_vla_non_pod) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1571 | << BaseT; |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 1572 | return QualType(); |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 1573 | } |
Douglas Gregor | a481ec4 | 2010-05-23 19:57:01 +0000 | [diff] [blame] | 1574 | // Prohibit the use of VLAs during template argument deduction. |
| 1575 | else if (isSFINAEContext()) { |
| 1576 | Diag(Loc, diag::err_vla_in_sfinae); |
| 1577 | return QualType(); |
| 1578 | } |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 1579 | // Just extwarn about VLAs. |
| 1580 | else |
| 1581 | Diag(Loc, diag::ext_vla); |
| 1582 | } else if (ASM != ArrayType::Normal || Quals != 0) |
Richard Smith | d7c56e1 | 2011-12-29 21:57:33 +0000 | [diff] [blame] | 1583 | Diag(Loc, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1584 | getLangOpts().CPlusPlus? diag::err_c99_array_usage_cxx |
Richard Smith | d7c56e1 | 2011-12-29 21:57:33 +0000 | [diff] [blame] | 1585 | : diag::ext_c99_array_usage) << ASM; |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
Dmitri Gribenko | 630f4bb | 2013-01-23 20:02:51 +0000 | [diff] [blame] | 1588 | if (T->isVariableArrayType()) { |
| 1589 | // Warn about VLAs for -Wvla. |
| 1590 | Diag(Loc, diag::warn_vla_used); |
| 1591 | } |
| 1592 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1593 | return T; |
| 1594 | } |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1595 | |
| 1596 | /// \brief Build an ext-vector type. |
| 1597 | /// |
| 1598 | /// Run the required checks for the extended vector type. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1599 | QualType Sema::BuildExtVectorType(QualType T, Expr *ArraySize, |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1600 | SourceLocation AttrLoc) { |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1601 | // unlike gcc's vector_size attribute, we do not allow vectors to be defined |
| 1602 | // in conjunction with complex types (pointers, arrays, functions, etc.). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1603 | if (!T->isDependentType() && |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1604 | !T->isIntegerType() && !T->isRealFloatingType()) { |
| 1605 | Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T; |
| 1606 | return QualType(); |
| 1607 | } |
| 1608 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1609 | if (!ArraySize->isTypeDependent() && !ArraySize->isValueDependent()) { |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1610 | llvm::APSInt vecSize(32); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1611 | if (!ArraySize->isIntegerConstantExpr(vecSize, Context)) { |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1612 | Diag(AttrLoc, diag::err_attribute_argument_not_int) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1613 | << "ext_vector_type" << ArraySize->getSourceRange(); |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1614 | return QualType(); |
| 1615 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1616 | |
| 1617 | // unlike gcc's vector_size attribute, the size is specified as the |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1618 | // number of elements, not the number of bytes. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1619 | unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue()); |
| 1620 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1621 | if (vectorSize == 0) { |
| 1622 | Diag(AttrLoc, diag::err_attribute_zero_size) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1623 | << ArraySize->getSourceRange(); |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1624 | return QualType(); |
| 1625 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1626 | |
Douglas Gregor | 4ac0140 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1627 | return Context.getExtVectorType(T, vectorSize); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1628 | } |
| 1629 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1630 | return Context.getDependentSizedExtVectorType(T, ArraySize, AttrLoc); |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1631 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1632 | |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 1633 | /// \brief Build a function type. |
| 1634 | /// |
| 1635 | /// This routine checks the function type according to C++ rules and |
| 1636 | /// under the assumption that the result type and parameter types have |
| 1637 | /// just been instantiated from a template. It therefore duplicates |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1638 | /// some of the behavior of GetTypeForDeclarator, but in a much |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 1639 | /// simpler form that is only suitable for this narrow use case. |
| 1640 | /// |
| 1641 | /// \param T The return type of the function. |
| 1642 | /// |
| 1643 | /// \param ParamTypes The parameter types of the function. This array |
| 1644 | /// will be modified to account for adjustments to the types of the |
| 1645 | /// function parameters. |
| 1646 | /// |
| 1647 | /// \param NumParamTypes The number of parameter types in ParamTypes. |
| 1648 | /// |
| 1649 | /// \param Variadic Whether this is a variadic function type. |
| 1650 | /// |
Richard Smith | eefb3d5 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 1651 | /// \param HasTrailingReturn Whether this function has a trailing return type. |
| 1652 | /// |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 1653 | /// \param Quals The cvr-qualifiers to be applied to the function type. |
| 1654 | /// |
| 1655 | /// \param Loc The location of the entity whose type involves this |
| 1656 | /// function type or, if there is no such entity, the location of the |
| 1657 | /// type that will have function type. |
| 1658 | /// |
| 1659 | /// \param Entity The name of the entity that involves the function |
| 1660 | /// type, if known. |
| 1661 | /// |
| 1662 | /// \returns A suitable function type, if there are no |
| 1663 | /// errors. Otherwise, returns a NULL type. |
| 1664 | QualType Sema::BuildFunctionType(QualType T, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1665 | QualType *ParamTypes, |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 1666 | unsigned NumParamTypes, |
Richard Smith | eefb3d5 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 1667 | bool Variadic, bool HasTrailingReturn, |
| 1668 | unsigned Quals, |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 1669 | RefQualifierKind RefQualifier, |
Eli Friedman | fa86954 | 2010-08-05 02:54:05 +0000 | [diff] [blame] | 1670 | SourceLocation Loc, DeclarationName Entity, |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1671 | FunctionType::ExtInfo Info) { |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 1672 | if (T->isArrayType() || T->isFunctionType()) { |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 1673 | Diag(Loc, diag::err_func_returning_array_function) |
Douglas Gregor | 58408bc | 2010-01-11 18:46:21 +0000 | [diff] [blame] | 1674 | << T->isFunctionType() << T; |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 1675 | return QualType(); |
| 1676 | } |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1677 | |
| 1678 | // Functions cannot return half FP. |
| 1679 | if (T->isHalfType()) { |
| 1680 | Diag(Loc, diag::err_parameters_retval_cannot_have_fp16_type) << 1 << |
| 1681 | FixItHint::CreateInsertion(Loc, "*"); |
| 1682 | return QualType(); |
| 1683 | } |
| 1684 | |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 1685 | bool Invalid = false; |
| 1686 | for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) { |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1687 | // FIXME: Loc is too inprecise here, should use proper locations for args. |
Douglas Gregor | 79e6bd3 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 1688 | QualType ParamType = Context.getAdjustedParameterType(ParamTypes[Idx]); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1689 | if (ParamType->isVoidType()) { |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 1690 | Diag(Loc, diag::err_param_with_void_type); |
| 1691 | Invalid = true; |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1692 | } else if (ParamType->isHalfType()) { |
| 1693 | // Disallow half FP arguments. |
| 1694 | Diag(Loc, diag::err_parameters_retval_cannot_have_fp16_type) << 0 << |
| 1695 | FixItHint::CreateInsertion(Loc, "*"); |
| 1696 | Invalid = true; |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 1697 | } |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1698 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1699 | ParamTypes[Idx] = ParamType; |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 1700 | } |
| 1701 | |
| 1702 | if (Invalid) |
| 1703 | return QualType(); |
| 1704 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1705 | FunctionProtoType::ExtProtoInfo EPI; |
| 1706 | EPI.Variadic = Variadic; |
Richard Smith | eefb3d5 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 1707 | EPI.HasTrailingReturn = HasTrailingReturn; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1708 | EPI.TypeQuals = Quals; |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 1709 | EPI.RefQualifier = RefQualifier; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1710 | EPI.ExtInfo = Info; |
| 1711 | |
| 1712 | return Context.getFunctionType(T, ParamTypes, NumParamTypes, EPI); |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 1713 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1714 | |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 1715 | /// \brief Build a member pointer type \c T Class::*. |
| 1716 | /// |
| 1717 | /// \param T the type to which the member pointer refers. |
| 1718 | /// \param Class the class type into which the member pointer points. |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 1719 | /// \param Loc the location where this type begins |
| 1720 | /// \param Entity the name of the entity that will have this member pointer type |
| 1721 | /// |
| 1722 | /// \returns a member pointer type, if successful, or a NULL type if there was |
| 1723 | /// an error. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1724 | QualType Sema::BuildMemberPointerType(QualType T, QualType Class, |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1725 | SourceLocation Loc, |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 1726 | DeclarationName Entity) { |
| 1727 | // Verify that we're not building a pointer to pointer to function with |
| 1728 | // exception specification. |
| 1729 | if (CheckDistantExceptionSpec(T)) { |
| 1730 | Diag(Loc, diag::err_distant_exception_spec); |
| 1731 | |
| 1732 | // FIXME: If we're doing this as part of template instantiation, |
| 1733 | // we should return immediately. |
| 1734 | |
| 1735 | // Build the type anyway, but use the canonical type so that the |
| 1736 | // exception specifiers are stripped off. |
| 1737 | T = Context.getCanonicalType(T); |
| 1738 | } |
| 1739 | |
Sebastian Redl | 7378012 | 2010-06-09 21:19:43 +0000 | [diff] [blame] | 1740 | // C++ 8.3.3p3: A pointer to member shall not point to ... a member |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 1741 | // with reference type, or "cv void." |
| 1742 | if (T->isReferenceType()) { |
Anders Carlsson | 8d4655d | 2009-06-30 00:06:57 +0000 | [diff] [blame] | 1743 | Diag(Loc, diag::err_illegal_decl_mempointer_to_reference) |
John McCall | ac40605 | 2009-10-30 00:37:20 +0000 | [diff] [blame] | 1744 | << (Entity? Entity.getAsString() : "type name") << T; |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 1745 | return QualType(); |
| 1746 | } |
| 1747 | |
| 1748 | if (T->isVoidType()) { |
| 1749 | Diag(Loc, diag::err_illegal_decl_mempointer_to_void) |
| 1750 | << (Entity? Entity.getAsString() : "type name"); |
| 1751 | return QualType(); |
| 1752 | } |
| 1753 | |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 1754 | if (!Class->isDependentType() && !Class->isRecordType()) { |
| 1755 | Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class; |
| 1756 | return QualType(); |
| 1757 | } |
| 1758 | |
John McCall | b8b2c9d | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 1759 | // C++ allows the class type in a member pointer to be an incomplete type. |
| 1760 | // In the Microsoft ABI, the size of the member pointer can vary |
| 1761 | // according to the class type, which means that we really need a |
| 1762 | // complete type if possible, which means we need to instantiate templates. |
| 1763 | // |
| 1764 | // For now, just require a complete type, which will instantiate |
| 1765 | // templates. This will also error if the type is just forward-declared, |
| 1766 | // which is a bug, but it's a bug that saves us from dealing with some |
| 1767 | // complexities at the moment. |
| 1768 | if (Context.getTargetInfo().getCXXABI().isMicrosoft() && |
Joao Matos | 679fc93 | 2012-09-04 17:18:12 +0000 | [diff] [blame] | 1769 | RequireCompleteType(Loc, Class, diag::err_incomplete_type)) |
| 1770 | return QualType(); |
| 1771 | |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1772 | return Context.getMemberPointerType(T, Class.getTypePtr()); |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 1773 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1774 | |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 1775 | /// \brief Build a block pointer type. |
| 1776 | /// |
| 1777 | /// \param T The type to which we'll be building a block pointer. |
| 1778 | /// |
James Dennett | efce31f | 2012-06-22 08:10:18 +0000 | [diff] [blame] | 1779 | /// \param Loc The source location, used for diagnostics. |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 1780 | /// |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 1781 | /// \param Entity The name of the entity that involves the block pointer |
| 1782 | /// type, if known. |
| 1783 | /// |
| 1784 | /// \returns A suitable block pointer type, if there are no |
| 1785 | /// errors. Otherwise, returns a NULL type. |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 1786 | QualType Sema::BuildBlockPointerType(QualType T, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1787 | SourceLocation Loc, |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 1788 | DeclarationName Entity) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1789 | if (!T->isFunctionType()) { |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 1790 | Diag(Loc, diag::err_nonfunction_block_type); |
| 1791 | return QualType(); |
| 1792 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1793 | |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1794 | return Context.getBlockPointerType(T); |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 1795 | } |
| 1796 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1797 | QualType Sema::GetTypeFromParser(ParsedType Ty, TypeSourceInfo **TInfo) { |
| 1798 | QualType QT = Ty.get(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1799 | if (QT.isNull()) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1800 | if (TInfo) *TInfo = 0; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1801 | return QualType(); |
| 1802 | } |
| 1803 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1804 | TypeSourceInfo *DI = 0; |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1805 | if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) { |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 1806 | QT = LIT->getType(); |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1807 | DI = LIT->getTypeSourceInfo(); |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 1808 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1809 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1810 | if (TInfo) *TInfo = DI; |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 1811 | return QT; |
| 1812 | } |
| 1813 | |
Argyrios Kyrtzidis | a8349f5 | 2011-07-01 22:23:05 +0000 | [diff] [blame] | 1814 | static void transferARCOwnershipToDeclaratorChunk(TypeProcessingState &state, |
| 1815 | Qualifiers::ObjCLifetime ownership, |
| 1816 | unsigned chunkIndex); |
| 1817 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1818 | /// Given that this is the declaration of a parameter under ARC, |
| 1819 | /// attempt to infer attributes and such for pointer-to-whatever |
| 1820 | /// types. |
| 1821 | static void inferARCWriteback(TypeProcessingState &state, |
| 1822 | QualType &declSpecType) { |
| 1823 | Sema &S = state.getSema(); |
| 1824 | Declarator &declarator = state.getDeclarator(); |
| 1825 | |
| 1826 | // TODO: should we care about decl qualifiers? |
| 1827 | |
| 1828 | // Check whether the declarator has the expected form. We walk |
| 1829 | // from the inside out in order to make the block logic work. |
| 1830 | unsigned outermostPointerIndex = 0; |
| 1831 | bool isBlockPointer = false; |
| 1832 | unsigned numPointers = 0; |
| 1833 | for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) { |
| 1834 | unsigned chunkIndex = i; |
| 1835 | DeclaratorChunk &chunk = declarator.getTypeObject(chunkIndex); |
| 1836 | switch (chunk.Kind) { |
| 1837 | case DeclaratorChunk::Paren: |
| 1838 | // Ignore parens. |
| 1839 | break; |
| 1840 | |
| 1841 | case DeclaratorChunk::Reference: |
| 1842 | case DeclaratorChunk::Pointer: |
| 1843 | // Count the number of pointers. Treat references |
| 1844 | // interchangeably as pointers; if they're mis-ordered, normal |
| 1845 | // type building will discover that. |
| 1846 | outermostPointerIndex = chunkIndex; |
| 1847 | numPointers++; |
| 1848 | break; |
| 1849 | |
| 1850 | case DeclaratorChunk::BlockPointer: |
| 1851 | // If we have a pointer to block pointer, that's an acceptable |
| 1852 | // indirect reference; anything else is not an application of |
| 1853 | // the rules. |
| 1854 | if (numPointers != 1) return; |
| 1855 | numPointers++; |
| 1856 | outermostPointerIndex = chunkIndex; |
| 1857 | isBlockPointer = true; |
| 1858 | |
| 1859 | // We don't care about pointer structure in return values here. |
| 1860 | goto done; |
| 1861 | |
| 1862 | case DeclaratorChunk::Array: // suppress if written (id[])? |
| 1863 | case DeclaratorChunk::Function: |
| 1864 | case DeclaratorChunk::MemberPointer: |
| 1865 | return; |
| 1866 | } |
| 1867 | } |
| 1868 | done: |
| 1869 | |
| 1870 | // If we have *one* pointer, then we want to throw the qualifier on |
| 1871 | // the declaration-specifiers, which means that it needs to be a |
| 1872 | // retainable object type. |
| 1873 | if (numPointers == 1) { |
| 1874 | // If it's not a retainable object type, the rule doesn't apply. |
| 1875 | if (!declSpecType->isObjCRetainableType()) return; |
| 1876 | |
| 1877 | // If it already has lifetime, don't do anything. |
| 1878 | if (declSpecType.getObjCLifetime()) return; |
| 1879 | |
| 1880 | // Otherwise, modify the type in-place. |
| 1881 | Qualifiers qs; |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 1882 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1883 | if (declSpecType->isObjCARCImplicitlyUnretainedType()) |
| 1884 | qs.addObjCLifetime(Qualifiers::OCL_ExplicitNone); |
| 1885 | else |
| 1886 | qs.addObjCLifetime(Qualifiers::OCL_Autoreleasing); |
| 1887 | declSpecType = S.Context.getQualifiedType(declSpecType, qs); |
| 1888 | |
| 1889 | // If we have *two* pointers, then we want to throw the qualifier on |
| 1890 | // the outermost pointer. |
| 1891 | } else if (numPointers == 2) { |
| 1892 | // If we don't have a block pointer, we need to check whether the |
| 1893 | // declaration-specifiers gave us something that will turn into a |
| 1894 | // retainable object pointer after we slap the first pointer on it. |
| 1895 | if (!isBlockPointer && !declSpecType->isObjCObjectType()) |
| 1896 | return; |
| 1897 | |
| 1898 | // Look for an explicit lifetime attribute there. |
| 1899 | DeclaratorChunk &chunk = declarator.getTypeObject(outermostPointerIndex); |
Argyrios Kyrtzidis | 1c73dcb | 2011-07-01 22:23:03 +0000 | [diff] [blame] | 1900 | if (chunk.Kind != DeclaratorChunk::Pointer && |
| 1901 | chunk.Kind != DeclaratorChunk::BlockPointer) |
| 1902 | return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1903 | for (const AttributeList *attr = chunk.getAttrs(); attr; |
| 1904 | attr = attr->getNext()) |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 1905 | if (attr->getKind() == AttributeList::AT_ObjCOwnership) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1906 | return; |
| 1907 | |
Argyrios Kyrtzidis | a8349f5 | 2011-07-01 22:23:05 +0000 | [diff] [blame] | 1908 | transferARCOwnershipToDeclaratorChunk(state, Qualifiers::OCL_Autoreleasing, |
| 1909 | outermostPointerIndex); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1910 | |
| 1911 | // Any other number of pointers/references does not trigger the rule. |
| 1912 | } else return; |
| 1913 | |
| 1914 | // TODO: mark whether we did this inference? |
| 1915 | } |
| 1916 | |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 1917 | static void DiagnoseIgnoredQualifiers(unsigned Quals, |
| 1918 | SourceLocation ConstQualLoc, |
| 1919 | SourceLocation VolatileQualLoc, |
| 1920 | SourceLocation RestrictQualLoc, |
| 1921 | Sema& S) { |
| 1922 | std::string QualStr; |
| 1923 | unsigned NumQuals = 0; |
| 1924 | SourceLocation Loc; |
| 1925 | |
| 1926 | FixItHint ConstFixIt; |
| 1927 | FixItHint VolatileFixIt; |
| 1928 | FixItHint RestrictFixIt; |
| 1929 | |
Hans Wennborg | a08fcb8 | 2011-06-03 17:37:26 +0000 | [diff] [blame] | 1930 | const SourceManager &SM = S.getSourceManager(); |
| 1931 | |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 1932 | // FIXME: The locations here are set kind of arbitrarily. It'd be nicer to |
| 1933 | // find a range and grow it to encompass all the qualifiers, regardless of |
| 1934 | // the order in which they textually appear. |
| 1935 | if (Quals & Qualifiers::Const) { |
| 1936 | ConstFixIt = FixItHint::CreateRemoval(ConstQualLoc); |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 1937 | QualStr = "const"; |
Hans Wennborg | a08fcb8 | 2011-06-03 17:37:26 +0000 | [diff] [blame] | 1938 | ++NumQuals; |
| 1939 | if (!Loc.isValid() || SM.isBeforeInTranslationUnit(ConstQualLoc, Loc)) |
| 1940 | Loc = ConstQualLoc; |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 1941 | } |
| 1942 | if (Quals & Qualifiers::Volatile) { |
| 1943 | VolatileFixIt = FixItHint::CreateRemoval(VolatileQualLoc); |
Hans Wennborg | a08fcb8 | 2011-06-03 17:37:26 +0000 | [diff] [blame] | 1944 | QualStr += (NumQuals == 0 ? "volatile" : " volatile"); |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 1945 | ++NumQuals; |
Hans Wennborg | a08fcb8 | 2011-06-03 17:37:26 +0000 | [diff] [blame] | 1946 | if (!Loc.isValid() || SM.isBeforeInTranslationUnit(VolatileQualLoc, Loc)) |
| 1947 | Loc = VolatileQualLoc; |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 1948 | } |
| 1949 | if (Quals & Qualifiers::Restrict) { |
| 1950 | RestrictFixIt = FixItHint::CreateRemoval(RestrictQualLoc); |
Hans Wennborg | a08fcb8 | 2011-06-03 17:37:26 +0000 | [diff] [blame] | 1951 | QualStr += (NumQuals == 0 ? "restrict" : " restrict"); |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 1952 | ++NumQuals; |
Hans Wennborg | a08fcb8 | 2011-06-03 17:37:26 +0000 | [diff] [blame] | 1953 | if (!Loc.isValid() || SM.isBeforeInTranslationUnit(RestrictQualLoc, Loc)) |
| 1954 | Loc = RestrictQualLoc; |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 1955 | } |
| 1956 | |
| 1957 | assert(NumQuals > 0 && "No known qualifiers?"); |
| 1958 | |
| 1959 | S.Diag(Loc, diag::warn_qual_return_type) |
Hans Wennborg | a08fcb8 | 2011-06-03 17:37:26 +0000 | [diff] [blame] | 1960 | << QualStr << NumQuals << ConstFixIt << VolatileFixIt << RestrictFixIt; |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 1961 | } |
| 1962 | |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 1963 | static QualType GetDeclSpecTypeForDeclarator(TypeProcessingState &state, |
| 1964 | TypeSourceInfo *&ReturnTypeInfo) { |
| 1965 | Sema &SemaRef = state.getSema(); |
| 1966 | Declarator &D = state.getDeclarator(); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1967 | QualType T; |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 1968 | ReturnTypeInfo = 0; |
| 1969 | |
| 1970 | // The TagDecl owned by the DeclSpec. |
Argyrios Kyrtzidis | d3880f8 | 2011-06-28 03:01:18 +0000 | [diff] [blame] | 1971 | TagDecl *OwnedTagDecl = 0; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1972 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1973 | switch (D.getName().getKind()) { |
Fariborz Jahanian | 98a5403 | 2011-07-12 17:16:56 +0000 | [diff] [blame] | 1974 | case UnqualifiedId::IK_ImplicitSelfParam: |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1975 | case UnqualifiedId::IK_OperatorFunctionId: |
Sebastian Redl | 8999fe1 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1976 | case UnqualifiedId::IK_Identifier: |
Sean Hunt | 0486d74 | 2009-11-28 04:44:28 +0000 | [diff] [blame] | 1977 | case UnqualifiedId::IK_LiteralOperatorId: |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1978 | case UnqualifiedId::IK_TemplateId: |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 1979 | T = ConvertDeclSpecToType(state); |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 1980 | |
Douglas Gregor | 591bd3c | 2010-02-08 22:07:33 +0000 | [diff] [blame] | 1981 | if (!D.isInvalidType() && D.getDeclSpec().isTypeSpecOwned()) { |
Argyrios Kyrtzidis | d3880f8 | 2011-06-28 03:01:18 +0000 | [diff] [blame] | 1982 | OwnedTagDecl = cast<TagDecl>(D.getDeclSpec().getRepAsDecl()); |
Abramo Bagnara | 1598797 | 2011-03-08 22:33:38 +0000 | [diff] [blame] | 1983 | // Owned declaration is embedded in declarator. |
Argyrios Kyrtzidis | d3880f8 | 2011-06-28 03:01:18 +0000 | [diff] [blame] | 1984 | OwnedTagDecl->setEmbeddedInDeclarator(true); |
Douglas Gregor | 591bd3c | 2010-02-08 22:07:33 +0000 | [diff] [blame] | 1985 | } |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1986 | break; |
| 1987 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1988 | case UnqualifiedId::IK_ConstructorName: |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1989 | case UnqualifiedId::IK_ConstructorTemplateId: |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1990 | case UnqualifiedId::IK_DestructorName: |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1991 | // Constructors and destructors don't have return types. Use |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 1992 | // "void" instead. |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 1993 | T = SemaRef.Context.VoidTy; |
Rafael Espindola | a78a640 | 2012-07-31 01:54:04 +0000 | [diff] [blame] | 1994 | if (AttributeList *attrs = D.getDeclSpec().getAttributes().getList()) |
Richard Smith | f7a0527 | 2013-01-14 07:53:01 +0000 | [diff] [blame] | 1995 | processTypeAttrs(state, T, TAL_DeclSpec, attrs); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1996 | break; |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 1997 | |
| 1998 | case UnqualifiedId::IK_ConversionFunctionId: |
| 1999 | // The result type of a conversion function is the type that it |
| 2000 | // converts to. |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 2001 | T = SemaRef.GetTypeFromParser(D.getName().ConversionFunctionId, |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2002 | &ReturnTypeInfo); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 2003 | break; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 2004 | } |
Douglas Gregor | dab60ad | 2010-10-01 18:44:50 +0000 | [diff] [blame] | 2005 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2006 | if (D.getAttributes()) |
| 2007 | distributeTypeAttrsFromDeclarator(state, T); |
| 2008 | |
Richard Smith | d37b360 | 2012-02-10 11:05:11 +0000 | [diff] [blame] | 2009 | // C++11 [dcl.spec.auto]p5: reject 'auto' if it is not in an allowed context. |
| 2010 | // In C++11, a function declarator using 'auto' must have a trailing return |
Richard Smith | 8110f04 | 2011-02-22 01:22:29 +0000 | [diff] [blame] | 2011 | // type (this is checked later) and we can skip this. In other languages |
| 2012 | // using auto, we need to check regardless. |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2013 | if (D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto && |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2014 | (!SemaRef.getLangOpts().CPlusPlus11 || !D.isFunctionDeclarator())) { |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 2015 | int Error = -1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2016 | |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 2017 | switch (D.getContext()) { |
| 2018 | case Declarator::KNRTypeListContext: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2019 | llvm_unreachable("K&R type lists aren't allowed in C++"); |
Eli Friedman | f88c400 | 2012-01-04 04:41:38 +0000 | [diff] [blame] | 2020 | case Declarator::LambdaExprContext: |
| 2021 | llvm_unreachable("Can't specify a type specifier in lambda grammar"); |
John McCall | cdda47f | 2011-10-01 09:56:14 +0000 | [diff] [blame] | 2022 | case Declarator::ObjCParameterContext: |
| 2023 | case Declarator::ObjCResultContext: |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 2024 | case Declarator::PrototypeContext: |
| 2025 | Error = 0; // Function prototype |
| 2026 | break; |
| 2027 | case Declarator::MemberContext: |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2028 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static) |
| 2029 | break; |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2030 | switch (cast<TagDecl>(SemaRef.CurContext)->getTagKind()) { |
David Blaikie | eb2d1f1 | 2011-09-23 20:26:49 +0000 | [diff] [blame] | 2031 | case TTK_Enum: llvm_unreachable("unhandled tag kind"); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2032 | case TTK_Struct: Error = 1; /* Struct member */ break; |
| 2033 | case TTK_Union: Error = 2; /* Union member */ break; |
| 2034 | case TTK_Class: Error = 3; /* Class member */ break; |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 2035 | case TTK_Interface: Error = 4; /* Interface member */ break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2036 | } |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 2037 | break; |
| 2038 | case Declarator::CXXCatchContext: |
Argyrios Kyrtzidis | 17b6399 | 2011-07-01 22:22:40 +0000 | [diff] [blame] | 2039 | case Declarator::ObjCCatchContext: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 2040 | Error = 5; // Exception declaration |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 2041 | break; |
| 2042 | case Declarator::TemplateParamContext: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 2043 | Error = 6; // Template parameter |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 2044 | break; |
| 2045 | case Declarator::BlockLiteralContext: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 2046 | Error = 7; // Block literal |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2047 | break; |
| 2048 | case Declarator::TemplateTypeArgContext: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 2049 | Error = 8; // Template type argument |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2050 | break; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2051 | case Declarator::AliasDeclContext: |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2052 | case Declarator::AliasTemplateContext: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 2053 | Error = 10; // Type alias |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2054 | break; |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 2055 | case Declarator::TrailingReturnContext: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 2056 | Error = 11; // Function return type |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 2057 | break; |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2058 | case Declarator::TypeNameContext: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 2059 | Error = 12; // Generic |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 2060 | break; |
| 2061 | case Declarator::FileContext: |
| 2062 | case Declarator::BlockContext: |
| 2063 | case Declarator::ForContext: |
| 2064 | case Declarator::ConditionContext: |
Argyrios Kyrtzidis | 0b8c98f | 2011-06-28 03:01:23 +0000 | [diff] [blame] | 2065 | case Declarator::CXXNewContext: |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 2066 | break; |
| 2067 | } |
| 2068 | |
Richard Smith | ddc83f9 | 2011-02-21 23:18:00 +0000 | [diff] [blame] | 2069 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 2070 | Error = 9; |
Richard Smith | ddc83f9 | 2011-02-21 23:18:00 +0000 | [diff] [blame] | 2071 | |
Richard Smith | 8110f04 | 2011-02-22 01:22:29 +0000 | [diff] [blame] | 2072 | // In Objective-C it is an error to use 'auto' on a function declarator. |
| 2073 | if (D.isFunctionDeclarator()) |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 2074 | Error = 11; |
Richard Smith | 8110f04 | 2011-02-22 01:22:29 +0000 | [diff] [blame] | 2075 | |
Richard Smith | d37b360 | 2012-02-10 11:05:11 +0000 | [diff] [blame] | 2076 | // C++11 [dcl.spec.auto]p2: 'auto' is always fine if the declarator |
Richard Smith | e7397c6 | 2011-02-22 00:36:53 +0000 | [diff] [blame] | 2077 | // contains a trailing return type. That is only legal at the outermost |
| 2078 | // level. Check all declarator chunks (outermost first) anyway, to give |
| 2079 | // better diagnostics. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2080 | if (SemaRef.getLangOpts().CPlusPlus11 && Error != -1) { |
Richard Smith | e7397c6 | 2011-02-22 00:36:53 +0000 | [diff] [blame] | 2081 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { |
| 2082 | unsigned chunkIndex = e - i - 1; |
| 2083 | state.setCurrentChunkIndex(chunkIndex); |
| 2084 | DeclaratorChunk &DeclType = D.getTypeObject(chunkIndex); |
| 2085 | if (DeclType.Kind == DeclaratorChunk::Function) { |
| 2086 | const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun; |
Richard Smith | 54655be | 2012-06-12 01:51:59 +0000 | [diff] [blame] | 2087 | if (FTI.hasTrailingReturnType()) { |
Richard Smith | e7397c6 | 2011-02-22 00:36:53 +0000 | [diff] [blame] | 2088 | Error = -1; |
| 2089 | break; |
| 2090 | } |
| 2091 | } |
| 2092 | } |
| 2093 | } |
| 2094 | |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 2095 | if (Error != -1) { |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2096 | SemaRef.Diag(D.getDeclSpec().getTypeSpecTypeLoc(), |
| 2097 | diag::err_auto_not_allowed) |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 2098 | << Error; |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2099 | T = SemaRef.Context.IntTy; |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 2100 | D.setInvalidType(true); |
Richard Smith | 0aa86c0 | 2011-10-15 05:42:01 +0000 | [diff] [blame] | 2101 | } else |
| 2102 | SemaRef.Diag(D.getDeclSpec().getTypeSpecTypeLoc(), |
| 2103 | diag::warn_cxx98_compat_auto_type_specifier); |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 2104 | } |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2105 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2106 | if (SemaRef.getLangOpts().CPlusPlus && |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2107 | OwnedTagDecl && OwnedTagDecl->isCompleteDefinition()) { |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2108 | // Check the contexts where C++ forbids the declaration of a new class |
| 2109 | // or enumeration in a type-specifier-seq. |
| 2110 | switch (D.getContext()) { |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 2111 | case Declarator::TrailingReturnContext: |
| 2112 | // Class and enumeration definitions are syntactically not allowed in |
| 2113 | // trailing return types. |
| 2114 | llvm_unreachable("parser should not have allowed this"); |
| 2115 | break; |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2116 | case Declarator::FileContext: |
| 2117 | case Declarator::MemberContext: |
| 2118 | case Declarator::BlockContext: |
| 2119 | case Declarator::ForContext: |
| 2120 | case Declarator::BlockLiteralContext: |
Eli Friedman | f88c400 | 2012-01-04 04:41:38 +0000 | [diff] [blame] | 2121 | case Declarator::LambdaExprContext: |
Richard Smith | d37b360 | 2012-02-10 11:05:11 +0000 | [diff] [blame] | 2122 | // C++11 [dcl.type]p3: |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2123 | // A type-specifier-seq shall not define a class or enumeration unless |
| 2124 | // it appears in the type-id of an alias-declaration (7.1.3) that is not |
| 2125 | // the declaration of a template-declaration. |
| 2126 | case Declarator::AliasDeclContext: |
| 2127 | break; |
| 2128 | case Declarator::AliasTemplateContext: |
| 2129 | SemaRef.Diag(OwnedTagDecl->getLocation(), |
| 2130 | diag::err_type_defined_in_alias_template) |
| 2131 | << SemaRef.Context.getTypeDeclType(OwnedTagDecl); |
Enea Zaffanella | 601e6e8 | 2013-01-11 14:34:39 +0000 | [diff] [blame] | 2132 | D.setInvalidType(true); |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2133 | break; |
| 2134 | case Declarator::TypeNameContext: |
| 2135 | case Declarator::TemplateParamContext: |
| 2136 | case Declarator::CXXNewContext: |
| 2137 | case Declarator::CXXCatchContext: |
| 2138 | case Declarator::ObjCCatchContext: |
| 2139 | case Declarator::TemplateTypeArgContext: |
| 2140 | SemaRef.Diag(OwnedTagDecl->getLocation(), |
| 2141 | diag::err_type_defined_in_type_specifier) |
| 2142 | << SemaRef.Context.getTypeDeclType(OwnedTagDecl); |
Enea Zaffanella | 601e6e8 | 2013-01-11 14:34:39 +0000 | [diff] [blame] | 2143 | D.setInvalidType(true); |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2144 | break; |
| 2145 | case Declarator::PrototypeContext: |
John McCall | cdda47f | 2011-10-01 09:56:14 +0000 | [diff] [blame] | 2146 | case Declarator::ObjCParameterContext: |
| 2147 | case Declarator::ObjCResultContext: |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2148 | case Declarator::KNRTypeListContext: |
| 2149 | // C++ [dcl.fct]p6: |
| 2150 | // Types shall not be defined in return or parameter types. |
| 2151 | SemaRef.Diag(OwnedTagDecl->getLocation(), |
| 2152 | diag::err_type_defined_in_param_type) |
| 2153 | << SemaRef.Context.getTypeDeclType(OwnedTagDecl); |
Enea Zaffanella | 601e6e8 | 2013-01-11 14:34:39 +0000 | [diff] [blame] | 2154 | D.setInvalidType(true); |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2155 | break; |
| 2156 | case Declarator::ConditionContext: |
| 2157 | // C++ 6.4p2: |
| 2158 | // The type-specifier-seq shall not contain typedef and shall not declare |
| 2159 | // a new class or enumeration. |
| 2160 | SemaRef.Diag(OwnedTagDecl->getLocation(), |
| 2161 | diag::err_type_defined_in_condition); |
Enea Zaffanella | 601e6e8 | 2013-01-11 14:34:39 +0000 | [diff] [blame] | 2162 | D.setInvalidType(true); |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2163 | break; |
| 2164 | } |
| 2165 | } |
| 2166 | |
| 2167 | return T; |
| 2168 | } |
| 2169 | |
Benjamin Kramer | a08c2fb | 2012-02-24 22:19:42 +0000 | [diff] [blame] | 2170 | static std::string getFunctionQualifiersAsString(const FunctionProtoType *FnTy){ |
Richard Smith | d37b360 | 2012-02-10 11:05:11 +0000 | [diff] [blame] | 2171 | std::string Quals = |
| 2172 | Qualifiers::fromCVRMask(FnTy->getTypeQuals()).getAsString(); |
| 2173 | |
| 2174 | switch (FnTy->getRefQualifier()) { |
| 2175 | case RQ_None: |
| 2176 | break; |
| 2177 | |
| 2178 | case RQ_LValue: |
| 2179 | if (!Quals.empty()) |
| 2180 | Quals += ' '; |
| 2181 | Quals += '&'; |
| 2182 | break; |
| 2183 | |
| 2184 | case RQ_RValue: |
| 2185 | if (!Quals.empty()) |
| 2186 | Quals += ' '; |
| 2187 | Quals += "&&"; |
| 2188 | break; |
| 2189 | } |
| 2190 | |
| 2191 | return Quals; |
| 2192 | } |
| 2193 | |
| 2194 | /// Check that the function type T, which has a cv-qualifier or a ref-qualifier, |
| 2195 | /// can be contained within the declarator chunk DeclType, and produce an |
| 2196 | /// appropriate diagnostic if not. |
| 2197 | static void checkQualifiedFunction(Sema &S, QualType T, |
| 2198 | DeclaratorChunk &DeclType) { |
| 2199 | // C++98 [dcl.fct]p4 / C++11 [dcl.fct]p6: a function type with a |
| 2200 | // cv-qualifier or a ref-qualifier can only appear at the topmost level |
| 2201 | // of a type. |
| 2202 | int DiagKind = -1; |
| 2203 | switch (DeclType.Kind) { |
| 2204 | case DeclaratorChunk::Paren: |
| 2205 | case DeclaratorChunk::MemberPointer: |
| 2206 | // These cases are permitted. |
| 2207 | return; |
| 2208 | case DeclaratorChunk::Array: |
| 2209 | case DeclaratorChunk::Function: |
| 2210 | // These cases don't allow function types at all; no need to diagnose the |
| 2211 | // qualifiers separately. |
| 2212 | return; |
| 2213 | case DeclaratorChunk::BlockPointer: |
| 2214 | DiagKind = 0; |
| 2215 | break; |
| 2216 | case DeclaratorChunk::Pointer: |
| 2217 | DiagKind = 1; |
| 2218 | break; |
| 2219 | case DeclaratorChunk::Reference: |
| 2220 | DiagKind = 2; |
| 2221 | break; |
| 2222 | } |
| 2223 | |
| 2224 | assert(DiagKind != -1); |
| 2225 | S.Diag(DeclType.Loc, diag::err_compound_qualified_function_type) |
| 2226 | << DiagKind << isa<FunctionType>(T.IgnoreParens()) << T |
| 2227 | << getFunctionQualifiersAsString(T->castAs<FunctionProtoType>()); |
| 2228 | } |
| 2229 | |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 2230 | /// Produce an approprioate diagnostic for an ambiguity between a function |
| 2231 | /// declarator and a C++ direct-initializer. |
| 2232 | static void warnAboutAmbiguousFunction(Sema &S, Declarator &D, |
| 2233 | DeclaratorChunk &DeclType, QualType RT) { |
| 2234 | const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun; |
| 2235 | assert(FTI.isAmbiguous && "no direct-initializer / function ambiguity"); |
| 2236 | |
| 2237 | // If the return type is void there is no ambiguity. |
| 2238 | if (RT->isVoidType()) |
| 2239 | return; |
| 2240 | |
| 2241 | // An initializer for a non-class type can have at most one argument. |
| 2242 | if (!RT->isRecordType() && FTI.NumArgs > 1) |
| 2243 | return; |
| 2244 | |
| 2245 | // An initializer for a reference must have exactly one argument. |
| 2246 | if (RT->isReferenceType() && FTI.NumArgs != 1) |
| 2247 | return; |
| 2248 | |
| 2249 | // Only warn if this declarator is declaring a function at block scope, and |
| 2250 | // doesn't have a storage class (such as 'extern') specified. |
| 2251 | if (!D.isFunctionDeclarator() || |
| 2252 | D.getFunctionDefinitionKind() != FDK_Declaration || |
| 2253 | !S.CurContext->isFunctionOrMethod() || |
| 2254 | D.getDeclSpec().getStorageClassSpecAsWritten() |
| 2255 | != DeclSpec::SCS_unspecified) |
| 2256 | return; |
| 2257 | |
| 2258 | // Inside a condition, a direct initializer is not permitted. We allow one to |
| 2259 | // be parsed in order to give better diagnostics in condition parsing. |
| 2260 | if (D.getContext() == Declarator::ConditionContext) |
| 2261 | return; |
| 2262 | |
| 2263 | SourceRange ParenRange(DeclType.Loc, DeclType.EndLoc); |
| 2264 | |
Richard Smith | d64effc | 2012-07-30 21:42:05 +0000 | [diff] [blame] | 2265 | S.Diag(DeclType.Loc, |
| 2266 | FTI.NumArgs ? diag::warn_parens_disambiguated_as_function_declaration |
| 2267 | : diag::warn_empty_parens_are_function_decl) |
| 2268 | << ParenRange; |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 2269 | |
Richard Smith | d64effc | 2012-07-30 21:42:05 +0000 | [diff] [blame] | 2270 | // If the declaration looks like: |
| 2271 | // T var1, |
| 2272 | // f(); |
| 2273 | // and name lookup finds a function named 'f', then the ',' was |
| 2274 | // probably intended to be a ';'. |
| 2275 | if (!D.isFirstDeclarator() && D.getIdentifier()) { |
| 2276 | FullSourceLoc Comma(D.getCommaLoc(), S.SourceMgr); |
| 2277 | FullSourceLoc Name(D.getIdentifierLoc(), S.SourceMgr); |
| 2278 | if (Comma.getFileID() != Name.getFileID() || |
| 2279 | Comma.getSpellingLineNumber() != Name.getSpellingLineNumber()) { |
| 2280 | LookupResult Result(S, D.getIdentifier(), SourceLocation(), |
| 2281 | Sema::LookupOrdinaryName); |
| 2282 | if (S.LookupName(Result, S.getCurScope())) |
| 2283 | S.Diag(D.getCommaLoc(), diag::note_empty_parens_function_call) |
| 2284 | << FixItHint::CreateReplacement(D.getCommaLoc(), ";") |
| 2285 | << D.getIdentifier(); |
| 2286 | } |
| 2287 | } |
| 2288 | |
| 2289 | if (FTI.NumArgs > 0) { |
| 2290 | // For a declaration with parameters, eg. "T var(T());", suggest adding parens |
| 2291 | // around the first parameter to turn the declaration into a variable |
| 2292 | // declaration. |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 2293 | SourceRange Range = FTI.ArgInfo[0].Param->getSourceRange(); |
| 2294 | SourceLocation B = Range.getBegin(); |
| 2295 | SourceLocation E = S.PP.getLocForEndOfToken(Range.getEnd()); |
| 2296 | // FIXME: Maybe we should suggest adding braces instead of parens |
| 2297 | // in C++11 for classes that don't have an initializer_list constructor. |
| 2298 | S.Diag(B, diag::note_additional_parens_for_variable_declaration) |
| 2299 | << FixItHint::CreateInsertion(B, "(") |
| 2300 | << FixItHint::CreateInsertion(E, ")"); |
Richard Smith | d64effc | 2012-07-30 21:42:05 +0000 | [diff] [blame] | 2301 | } else { |
| 2302 | // For a declaration without parameters, eg. "T var();", suggest replacing the |
| 2303 | // parens with an initializer to turn the declaration into a variable |
| 2304 | // declaration. |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 2305 | const CXXRecordDecl *RD = RT->getAsCXXRecordDecl(); |
Richard Smith | d64effc | 2012-07-30 21:42:05 +0000 | [diff] [blame] | 2306 | |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 2307 | // Empty parens mean value-initialization, and no parens mean |
| 2308 | // default initialization. These are equivalent if the default |
| 2309 | // constructor is user-provided or if zero-initialization is a |
| 2310 | // no-op. |
| 2311 | if (RD && RD->hasDefinition() && |
| 2312 | (RD->isEmpty() || RD->hasUserProvidedDefaultConstructor())) |
| 2313 | S.Diag(DeclType.Loc, diag::note_empty_parens_default_ctor) |
| 2314 | << FixItHint::CreateRemoval(ParenRange); |
| 2315 | else { |
| 2316 | std::string Init = S.getFixItZeroInitializerForType(RT); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2317 | if (Init.empty() && S.LangOpts.CPlusPlus11) |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 2318 | Init = "{}"; |
| 2319 | if (!Init.empty()) |
| 2320 | S.Diag(DeclType.Loc, diag::note_empty_parens_zero_initialize) |
| 2321 | << FixItHint::CreateReplacement(ParenRange, Init); |
| 2322 | } |
| 2323 | } |
| 2324 | } |
| 2325 | |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2326 | static TypeSourceInfo *GetFullTypeForDeclarator(TypeProcessingState &state, |
| 2327 | QualType declSpecType, |
| 2328 | TypeSourceInfo *TInfo) { |
| 2329 | |
| 2330 | QualType T = declSpecType; |
| 2331 | Declarator &D = state.getDeclarator(); |
| 2332 | Sema &S = state.getSema(); |
| 2333 | ASTContext &Context = S.Context; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2334 | const LangOptions &LangOpts = S.getLangOpts(); |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2335 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 2336 | // The name we're declaring, if any. |
| 2337 | DeclarationName Name; |
| 2338 | if (D.getIdentifier()) |
| 2339 | Name = D.getIdentifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2340 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2341 | // Does this declaration declare a typedef-name? |
| 2342 | bool IsTypedefName = |
| 2343 | D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef || |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2344 | D.getContext() == Declarator::AliasDeclContext || |
| 2345 | D.getContext() == Declarator::AliasTemplateContext; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2346 | |
Richard Smith | d37b360 | 2012-02-10 11:05:11 +0000 | [diff] [blame] | 2347 | // Does T refer to a function type with a cv-qualifier or a ref-qualifier? |
| 2348 | bool IsQualifiedFunction = T->isFunctionProtoType() && |
| 2349 | (T->castAs<FunctionProtoType>()->getTypeQuals() != 0 || |
| 2350 | T->castAs<FunctionProtoType>()->getRefQualifier() != RQ_None); |
| 2351 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 2352 | // Walk the DeclTypeInfo, building the recursive type as we go. |
| 2353 | // DeclTypeInfos are ordered from the identifier out, which is |
| 2354 | // opposite of what we want :). |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 2355 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2356 | unsigned chunkIndex = e - i - 1; |
| 2357 | state.setCurrentChunkIndex(chunkIndex); |
| 2358 | DeclaratorChunk &DeclType = D.getTypeObject(chunkIndex); |
Richard Smith | d37b360 | 2012-02-10 11:05:11 +0000 | [diff] [blame] | 2359 | if (IsQualifiedFunction) { |
| 2360 | checkQualifiedFunction(S, T, DeclType); |
| 2361 | IsQualifiedFunction = DeclType.Kind == DeclaratorChunk::Paren; |
| 2362 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2363 | switch (DeclType.Kind) { |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 2364 | case DeclaratorChunk::Paren: |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2365 | T = S.BuildParenType(T); |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 2366 | break; |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2367 | case DeclaratorChunk::BlockPointer: |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 2368 | // If blocks are disabled, emit an error. |
| 2369 | if (!LangOpts.Blocks) |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2370 | S.Diag(DeclType.Loc, diag::err_blocks_disable); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2371 | |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2372 | T = S.BuildBlockPointerType(T, D.getIdentifierLoc(), Name); |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 2373 | if (DeclType.Cls.TypeQuals) |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2374 | T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Cls.TypeQuals); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2375 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2376 | case DeclaratorChunk::Pointer: |
Sebastian Redl | 6a7330c | 2009-05-29 15:01:05 +0000 | [diff] [blame] | 2377 | // Verify that we're not building a pointer to pointer to function with |
| 2378 | // exception specification. |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2379 | if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) { |
| 2380 | S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec); |
Sebastian Redl | 6a7330c | 2009-05-29 15:01:05 +0000 | [diff] [blame] | 2381 | D.setInvalidType(true); |
| 2382 | // Build the type anyway. |
| 2383 | } |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2384 | if (LangOpts.ObjC1 && T->getAs<ObjCObjectType>()) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2385 | T = Context.getObjCObjectPointerType(T); |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 2386 | if (DeclType.Ptr.TypeQuals) |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2387 | T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2388 | break; |
| 2389 | } |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2390 | T = S.BuildPointerType(T, DeclType.Loc, Name); |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 2391 | if (DeclType.Ptr.TypeQuals) |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2392 | T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2393 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2394 | break; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2395 | case DeclaratorChunk::Reference: { |
Sebastian Redl | 6a7330c | 2009-05-29 15:01:05 +0000 | [diff] [blame] | 2396 | // Verify that we're not building a reference to pointer to function with |
| 2397 | // exception specification. |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2398 | if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) { |
| 2399 | S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec); |
Sebastian Redl | 6a7330c | 2009-05-29 15:01:05 +0000 | [diff] [blame] | 2400 | D.setInvalidType(true); |
| 2401 | // Build the type anyway. |
| 2402 | } |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2403 | T = S.BuildReferenceType(T, DeclType.Ref.LValueRef, DeclType.Loc, Name); |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 2404 | |
| 2405 | Qualifiers Quals; |
| 2406 | if (DeclType.Ref.HasRestrict) |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2407 | T = S.BuildQualifiedType(T, DeclType.Loc, Qualifiers::Restrict); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2408 | break; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2409 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2410 | case DeclaratorChunk::Array: { |
Sebastian Redl | 6a7330c | 2009-05-29 15:01:05 +0000 | [diff] [blame] | 2411 | // Verify that we're not building an array of pointers to function with |
| 2412 | // exception specification. |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2413 | if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) { |
| 2414 | S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec); |
Sebastian Redl | 6a7330c | 2009-05-29 15:01:05 +0000 | [diff] [blame] | 2415 | D.setInvalidType(true); |
| 2416 | // Build the type anyway. |
| 2417 | } |
Chris Lattner | fd89bc8 | 2008-04-02 01:05:10 +0000 | [diff] [blame] | 2418 | DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr; |
Chris Lattner | 94f81fd | 2007-08-28 16:54:00 +0000 | [diff] [blame] | 2419 | Expr *ArraySize = static_cast<Expr*>(ATI.NumElts); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2420 | ArrayType::ArraySizeModifier ASM; |
| 2421 | if (ATI.isStar) |
| 2422 | ASM = ArrayType::Star; |
| 2423 | else if (ATI.hasStatic) |
| 2424 | ASM = ArrayType::Static; |
| 2425 | else |
| 2426 | ASM = ArrayType::Normal; |
John McCall | c05a94b | 2011-03-23 23:43:04 +0000 | [diff] [blame] | 2427 | if (ASM == ArrayType::Star && !D.isPrototypeContext()) { |
Eli Friedman | f91f5c8 | 2009-04-26 21:57:51 +0000 | [diff] [blame] | 2428 | // FIXME: This check isn't quite right: it allows star in prototypes |
| 2429 | // for function definitions, and disallows some edge cases detailed |
| 2430 | // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2431 | S.Diag(DeclType.Loc, diag::err_array_star_outside_prototype); |
Eli Friedman | f91f5c8 | 2009-04-26 21:57:51 +0000 | [diff] [blame] | 2432 | ASM = ArrayType::Normal; |
| 2433 | D.setInvalidType(true); |
| 2434 | } |
Hans Wennborg | 7f397c5 | 2012-08-15 07:42:30 +0000 | [diff] [blame] | 2435 | |
| 2436 | // C99 6.7.5.2p1: The optional type qualifiers and the keyword static |
| 2437 | // shall appear only in a declaration of a function parameter with an |
| 2438 | // array type, ... |
| 2439 | if (ASM == ArrayType::Static || ATI.TypeQuals) { |
Matt Beaumont-Gay | 99570a5 | 2012-08-15 19:53:19 +0000 | [diff] [blame] | 2440 | if (!(D.isPrototypeContext() || |
| 2441 | D.getContext() == Declarator::KNRTypeListContext)) { |
Hans Wennborg | 7f397c5 | 2012-08-15 07:42:30 +0000 | [diff] [blame] | 2442 | S.Diag(DeclType.Loc, diag::err_array_static_outside_prototype) << |
| 2443 | (ASM == ArrayType::Static ? "'static'" : "type qualifier"); |
| 2444 | // Remove the 'static' and the type qualifiers. |
| 2445 | if (ASM == ArrayType::Static) |
| 2446 | ASM = ArrayType::Normal; |
| 2447 | ATI.TypeQuals = 0; |
| 2448 | D.setInvalidType(true); |
| 2449 | } |
| 2450 | |
| 2451 | // C99 6.7.5.2p1: ... and then only in the outermost array type |
| 2452 | // derivation. |
| 2453 | unsigned x = chunkIndex; |
| 2454 | while (x != 0) { |
| 2455 | // Walk outwards along the declarator chunks. |
| 2456 | x--; |
| 2457 | const DeclaratorChunk &DC = D.getTypeObject(x); |
| 2458 | switch (DC.Kind) { |
| 2459 | case DeclaratorChunk::Paren: |
| 2460 | continue; |
| 2461 | case DeclaratorChunk::Array: |
| 2462 | case DeclaratorChunk::Pointer: |
| 2463 | case DeclaratorChunk::Reference: |
| 2464 | case DeclaratorChunk::MemberPointer: |
| 2465 | S.Diag(DeclType.Loc, diag::err_array_static_not_outermost) << |
| 2466 | (ASM == ArrayType::Static ? "'static'" : "type qualifier"); |
| 2467 | if (ASM == ArrayType::Static) |
| 2468 | ASM = ArrayType::Normal; |
| 2469 | ATI.TypeQuals = 0; |
| 2470 | D.setInvalidType(true); |
| 2471 | break; |
| 2472 | case DeclaratorChunk::Function: |
| 2473 | case DeclaratorChunk::BlockPointer: |
| 2474 | // These are invalid anyway, so just ignore. |
| 2475 | break; |
| 2476 | } |
| 2477 | } |
| 2478 | } |
| 2479 | |
Eli Friedman | 8ac2c66 | 2011-11-11 02:00:42 +0000 | [diff] [blame] | 2480 | T = S.BuildArrayType(T, ASM, ArraySize, ATI.TypeQuals, |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2481 | SourceRange(DeclType.Loc, DeclType.EndLoc), Name); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2482 | break; |
| 2483 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2484 | case DeclaratorChunk::Function: { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2485 | // If the function declarator has a prototype (i.e. it is not () and |
| 2486 | // does not have a K&R-style identifier list), then the arguments are part |
| 2487 | // of the type, otherwise the argument list is (). |
| 2488 | const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun; |
Richard Smith | d37b360 | 2012-02-10 11:05:11 +0000 | [diff] [blame] | 2489 | IsQualifiedFunction = FTI.TypeQuals || FTI.hasRefQualifier(); |
Sebastian Redl | 3cc9726 | 2009-05-31 11:47:27 +0000 | [diff] [blame] | 2490 | |
Richard Smith | e7397c6 | 2011-02-22 00:36:53 +0000 | [diff] [blame] | 2491 | // Check for auto functions and trailing return type and adjust the |
| 2492 | // return type accordingly. |
| 2493 | if (!D.isInvalidType()) { |
| 2494 | // trailing-return-type is only required if we're declaring a function, |
| 2495 | // and not, for instance, a pointer to a function. |
| 2496 | if (D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto && |
Richard Smith | 54655be | 2012-06-12 01:51:59 +0000 | [diff] [blame] | 2497 | !FTI.hasTrailingReturnType() && chunkIndex == 0) { |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2498 | S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(), |
Richard Smith | e7397c6 | 2011-02-22 00:36:53 +0000 | [diff] [blame] | 2499 | diag::err_auto_missing_trailing_return); |
| 2500 | T = Context.IntTy; |
| 2501 | D.setInvalidType(true); |
Richard Smith | 54655be | 2012-06-12 01:51:59 +0000 | [diff] [blame] | 2502 | } else if (FTI.hasTrailingReturnType()) { |
Richard Smith | e7397c6 | 2011-02-22 00:36:53 +0000 | [diff] [blame] | 2503 | // T must be exactly 'auto' at this point. See CWG issue 681. |
| 2504 | if (isa<ParenType>(T)) { |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2505 | S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(), |
Richard Smith | e7397c6 | 2011-02-22 00:36:53 +0000 | [diff] [blame] | 2506 | diag::err_trailing_return_in_parens) |
| 2507 | << T << D.getDeclSpec().getSourceRange(); |
| 2508 | D.setInvalidType(true); |
Eli Friedman | f88c400 | 2012-01-04 04:41:38 +0000 | [diff] [blame] | 2509 | } else if (D.getContext() != Declarator::LambdaExprContext && |
| 2510 | (T.hasQualifiers() || !isa<AutoType>(T))) { |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2511 | S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(), |
Richard Smith | e7397c6 | 2011-02-22 00:36:53 +0000 | [diff] [blame] | 2512 | diag::err_trailing_return_without_auto) |
| 2513 | << T << D.getDeclSpec().getSourceRange(); |
| 2514 | D.setInvalidType(true); |
| 2515 | } |
Richard Smith | 54655be | 2012-06-12 01:51:59 +0000 | [diff] [blame] | 2516 | T = S.GetTypeFromParser(FTI.getTrailingReturnType(), &TInfo); |
| 2517 | if (T.isNull()) { |
| 2518 | // An error occurred parsing the trailing return type. |
| 2519 | T = Context.IntTy; |
| 2520 | D.setInvalidType(true); |
| 2521 | } |
Richard Smith | e7397c6 | 2011-02-22 00:36:53 +0000 | [diff] [blame] | 2522 | } |
| 2523 | } |
| 2524 | |
Chris Lattner | cd88129 | 2007-12-19 05:31:29 +0000 | [diff] [blame] | 2525 | // C99 6.7.5.3p1: The return type may not be a function or array type. |
Douglas Gregor | 58408bc | 2010-01-11 18:46:21 +0000 | [diff] [blame] | 2526 | // For conversion functions, we'll diagnose this particular error later. |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 2527 | if ((T->isArrayType() || T->isFunctionType()) && |
| 2528 | (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId)) { |
Argyrios Kyrtzidis | 9865044 | 2011-01-25 23:16:33 +0000 | [diff] [blame] | 2529 | unsigned diagID = diag::err_func_returning_array_function; |
Argyrios Kyrtzidis | a4356ad | 2011-01-26 01:26:44 +0000 | [diff] [blame] | 2530 | // Last processing chunk in block context means this function chunk |
| 2531 | // represents the block. |
| 2532 | if (chunkIndex == 0 && |
| 2533 | D.getContext() == Declarator::BlockLiteralContext) |
Argyrios Kyrtzidis | 9865044 | 2011-01-25 23:16:33 +0000 | [diff] [blame] | 2534 | diagID = diag::err_block_returning_array_function; |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2535 | S.Diag(DeclType.Loc, diagID) << T->isFunctionType() << T; |
Chris Lattner | cd88129 | 2007-12-19 05:31:29 +0000 | [diff] [blame] | 2536 | T = Context.IntTy; |
| 2537 | D.setInvalidType(true); |
| 2538 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2539 | |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 2540 | // Do not allow returning half FP value. |
| 2541 | // FIXME: This really should be in BuildFunctionType. |
| 2542 | if (T->isHalfType()) { |
Joey Gouly | 19dbb20 | 2013-01-23 11:56:20 +0000 | [diff] [blame] | 2543 | if (S.getLangOpts().OpenCL) { |
| 2544 | if (!S.getOpenCLOptions().cl_khr_fp16) { |
| 2545 | S.Diag(D.getIdentifierLoc(), diag::err_opencl_half_return) << T; |
| 2546 | D.setInvalidType(true); |
| 2547 | } |
| 2548 | } else { |
| 2549 | S.Diag(D.getIdentifierLoc(), |
| 2550 | diag::err_parameters_retval_cannot_have_fp16_type) << 1; |
| 2551 | D.setInvalidType(true); |
| 2552 | } |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 2553 | } |
| 2554 | |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 2555 | // cv-qualifiers on return types are pointless except when the type is a |
| 2556 | // class type in C++. |
Douglas Gregor | fff9513 | 2011-03-01 17:04:42 +0000 | [diff] [blame] | 2557 | if (isa<PointerType>(T) && T.getLocalCVRQualifiers() && |
Rafael Espindola | 1e15394 | 2011-03-11 04:56:58 +0000 | [diff] [blame] | 2558 | (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId) && |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2559 | (!LangOpts.CPlusPlus || !T->isDependentType())) { |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 2560 | assert(chunkIndex + 1 < e && "No DeclaratorChunk for the return type?"); |
| 2561 | DeclaratorChunk ReturnTypeChunk = D.getTypeObject(chunkIndex + 1); |
| 2562 | assert(ReturnTypeChunk.Kind == DeclaratorChunk::Pointer); |
| 2563 | |
| 2564 | DeclaratorChunk::PointerTypeInfo &PTI = ReturnTypeChunk.Ptr; |
| 2565 | |
| 2566 | DiagnoseIgnoredQualifiers(PTI.TypeQuals, |
| 2567 | SourceLocation::getFromRawEncoding(PTI.ConstQualLoc), |
| 2568 | SourceLocation::getFromRawEncoding(PTI.VolatileQualLoc), |
| 2569 | SourceLocation::getFromRawEncoding(PTI.RestrictQualLoc), |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2570 | S); |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 2571 | |
| 2572 | } else if (T.getCVRQualifiers() && D.getDeclSpec().getTypeQualifiers() && |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2573 | (!LangOpts.CPlusPlus || |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 2574 | (!T->isDependentType() && !T->isRecordType()))) { |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 2575 | |
| 2576 | DiagnoseIgnoredQualifiers(D.getDeclSpec().getTypeQualifiers(), |
| 2577 | D.getDeclSpec().getConstSpecLoc(), |
| 2578 | D.getDeclSpec().getVolatileSpecLoc(), |
| 2579 | D.getDeclSpec().getRestrictSpecLoc(), |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2580 | S); |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 2581 | } |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 2582 | |
Douglas Gregor | 02dd798 | 2013-01-17 23:36:45 +0000 | [diff] [blame] | 2583 | // Objective-C ARC ownership qualifiers are ignored on the function |
| 2584 | // return type (by type canonicalization). Complain if this attribute |
| 2585 | // was written here. |
| 2586 | if (T.getQualifiers().hasObjCLifetime()) { |
| 2587 | SourceLocation AttrLoc; |
| 2588 | if (chunkIndex + 1 < D.getNumTypeObjects()) { |
| 2589 | DeclaratorChunk ReturnTypeChunk = D.getTypeObject(chunkIndex + 1); |
| 2590 | for (const AttributeList *Attr = ReturnTypeChunk.getAttrs(); |
| 2591 | Attr; Attr = Attr->getNext()) { |
| 2592 | if (Attr->getKind() == AttributeList::AT_ObjCOwnership) { |
| 2593 | AttrLoc = Attr->getLoc(); |
| 2594 | break; |
| 2595 | } |
| 2596 | } |
| 2597 | } |
| 2598 | if (AttrLoc.isInvalid()) { |
| 2599 | for (const AttributeList *Attr |
| 2600 | = D.getDeclSpec().getAttributes().getList(); |
| 2601 | Attr; Attr = Attr->getNext()) { |
| 2602 | if (Attr->getKind() == AttributeList::AT_ObjCOwnership) { |
| 2603 | AttrLoc = Attr->getLoc(); |
| 2604 | break; |
| 2605 | } |
| 2606 | } |
| 2607 | } |
| 2608 | |
| 2609 | if (AttrLoc.isValid()) { |
| 2610 | // The ownership attributes are almost always written via |
| 2611 | // the predefined |
| 2612 | // __strong/__weak/__autoreleasing/__unsafe_unretained. |
| 2613 | if (AttrLoc.isMacroID()) |
| 2614 | AttrLoc = S.SourceMgr.getImmediateExpansionRange(AttrLoc).first; |
| 2615 | |
| 2616 | S.Diag(AttrLoc, diag::warn_arc_lifetime_result_type) |
| 2617 | << T.getQualifiers().getObjCLifetime(); |
| 2618 | } |
| 2619 | } |
| 2620 | |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2621 | if (LangOpts.CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) { |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 2622 | // C++ [dcl.fct]p6: |
| 2623 | // Types shall not be defined in return or parameter types. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2624 | TagDecl *Tag = cast<TagDecl>(D.getDeclSpec().getRepAsDecl()); |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2625 | if (Tag->isCompleteDefinition()) |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2626 | S.Diag(Tag->getLocation(), diag::err_type_defined_in_result_type) |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 2627 | << Context.getTypeDeclType(Tag); |
| 2628 | } |
| 2629 | |
Sebastian Redl | 3cc9726 | 2009-05-31 11:47:27 +0000 | [diff] [blame] | 2630 | // Exception specs are not allowed in typedefs. Complain, but add it |
| 2631 | // anyway. |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2632 | if (IsTypedefName && FTI.getExceptionSpecType()) |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2633 | S.Diag(FTI.getExceptionSpecLoc(), diag::err_exception_spec_in_typedef) |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2634 | << (D.getContext() == Declarator::AliasDeclContext || |
| 2635 | D.getContext() == Declarator::AliasTemplateContext); |
Sebastian Redl | 3cc9726 | 2009-05-31 11:47:27 +0000 | [diff] [blame] | 2636 | |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 2637 | // If we see "T var();" or "T var(T());" at block scope, it is probably |
| 2638 | // an attempt to initialize a variable, not a function declaration. |
| 2639 | if (FTI.isAmbiguous) |
| 2640 | warnAboutAmbiguousFunction(S, D, DeclType, T); |
| 2641 | |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2642 | if (!FTI.NumArgs && !FTI.isVariadic && !LangOpts.CPlusPlus) { |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 2643 | // Simple void foo(), where the incoming T is the result type. |
| 2644 | T = Context.getFunctionNoProtoType(T); |
| 2645 | } else { |
| 2646 | // We allow a zero-parameter variadic function in C if the |
| 2647 | // function is marked with the "overloadable" attribute. Scan |
| 2648 | // for this attribute now. |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2649 | if (!FTI.NumArgs && FTI.isVariadic && !LangOpts.CPlusPlus) { |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 2650 | bool Overloadable = false; |
| 2651 | for (const AttributeList *Attrs = D.getAttributes(); |
| 2652 | Attrs; Attrs = Attrs->getNext()) { |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 2653 | if (Attrs->getKind() == AttributeList::AT_Overloadable) { |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 2654 | Overloadable = true; |
| 2655 | break; |
| 2656 | } |
| 2657 | } |
| 2658 | |
| 2659 | if (!Overloadable) |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2660 | S.Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg); |
Argyrios Kyrtzidis | c6f7345 | 2008-10-16 17:31:08 +0000 | [diff] [blame] | 2661 | } |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 2662 | |
| 2663 | if (FTI.NumArgs && FTI.ArgInfo[0].Param == 0) { |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 2664 | // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function |
| 2665 | // definition. |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2666 | S.Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration); |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 2667 | D.setInvalidType(true); |
Argyrios Kyrtzidis | d5668a2 | 2013-02-22 06:58:32 +0000 | [diff] [blame] | 2668 | // Recover by creating a K&R-style function type. |
| 2669 | T = Context.getFunctionNoProtoType(T); |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 2670 | break; |
| 2671 | } |
| 2672 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2673 | FunctionProtoType::ExtProtoInfo EPI; |
| 2674 | EPI.Variadic = FTI.isVariadic; |
Richard Smith | 54655be | 2012-06-12 01:51:59 +0000 | [diff] [blame] | 2675 | EPI.HasTrailingReturn = FTI.hasTrailingReturnType(); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2676 | EPI.TypeQuals = FTI.TypeQuals; |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 2677 | EPI.RefQualifier = !FTI.hasRefQualifier()? RQ_None |
| 2678 | : FTI.RefQualifierIsLValueRef? RQ_LValue |
| 2679 | : RQ_RValue; |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 2680 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2681 | // Otherwise, we have a function with an argument list that is |
| 2682 | // potentially variadic. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2683 | SmallVector<QualType, 16> ArgTys; |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 2684 | ArgTys.reserve(FTI.NumArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2685 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2686 | SmallVector<bool, 16> ConsumedArguments; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2687 | ConsumedArguments.reserve(FTI.NumArgs); |
| 2688 | bool HasAnyConsumedArguments = false; |
| 2689 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2690 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2691 | ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param); |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 2692 | QualType ArgTy = Param->getType(); |
Chris Lattner | 78c75fb | 2007-07-21 05:30:18 +0000 | [diff] [blame] | 2693 | assert(!ArgTy.isNull() && "Couldn't parse type?"); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 2694 | |
| 2695 | // Adjust the parameter type. |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 2696 | assert((ArgTy == Context.getAdjustedParameterType(ArgTy)) && |
Douglas Gregor | 79e6bd3 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 2697 | "Unadjusted type?"); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 2698 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2699 | // Look for 'void'. void is allowed only as a single argument to a |
| 2700 | // function with no other parameters (C99 6.7.5.3p10). We record |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2701 | // int(void) as a FunctionProtoType with an empty argument list. |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 2702 | if (ArgTy->isVoidType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2703 | // If this is something like 'float(int, void)', reject it. 'void' |
| 2704 | // is an incomplete type (C99 6.2.5p19) and function decls cannot |
| 2705 | // have arguments of incomplete type. |
| 2706 | if (FTI.NumArgs != 1 || FTI.isVariadic) { |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2707 | S.Diag(DeclType.Loc, diag::err_void_only_param); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 2708 | ArgTy = Context.IntTy; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 2709 | Param->setType(ArgTy); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 2710 | } else if (FTI.ArgInfo[i].Ident) { |
| 2711 | // Reject, but continue to parse 'int(void abc)'. |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2712 | S.Diag(FTI.ArgInfo[i].IdentLoc, |
Chris Lattner | 4565d4e | 2007-07-21 05:26:43 +0000 | [diff] [blame] | 2713 | diag::err_param_with_void_type); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 2714 | ArgTy = Context.IntTy; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 2715 | Param->setType(ArgTy); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 2716 | } else { |
| 2717 | // Reject, but continue to parse 'float(const void)'. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2718 | if (ArgTy.hasQualifiers()) |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2719 | S.Diag(DeclType.Loc, diag::err_void_param_qualified); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2720 | |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 2721 | // Do not add 'void' to the ArgTys list. |
| 2722 | break; |
| 2723 | } |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 2724 | } else if (ArgTy->isHalfType()) { |
| 2725 | // Disallow half FP arguments. |
| 2726 | // FIXME: This really should be in BuildFunctionType. |
Joey Gouly | 19dbb20 | 2013-01-23 11:56:20 +0000 | [diff] [blame] | 2727 | if (S.getLangOpts().OpenCL) { |
| 2728 | if (!S.getOpenCLOptions().cl_khr_fp16) { |
| 2729 | S.Diag(Param->getLocation(), |
| 2730 | diag::err_opencl_half_argument) << ArgTy; |
| 2731 | D.setInvalidType(); |
John McCall | 9dd74c5 | 2013-02-12 01:29:43 +0000 | [diff] [blame] | 2732 | Param->setInvalidDecl(); |
Joey Gouly | 19dbb20 | 2013-01-23 11:56:20 +0000 | [diff] [blame] | 2733 | } |
| 2734 | } else { |
| 2735 | S.Diag(Param->getLocation(), |
| 2736 | diag::err_parameters_retval_cannot_have_fp16_type) << 0; |
| 2737 | D.setInvalidType(); |
| 2738 | } |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 2739 | } else if (!FTI.hasPrototype) { |
| 2740 | if (ArgTy->isPromotableIntegerType()) { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 2741 | ArgTy = Context.getPromotedIntegerType(ArgTy); |
John McCall | eecf5fa | 2011-03-09 04:22:44 +0000 | [diff] [blame] | 2742 | Param->setKNRPromoted(true); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2743 | } else if (const BuiltinType* BTy = ArgTy->getAs<BuiltinType>()) { |
John McCall | eecf5fa | 2011-03-09 04:22:44 +0000 | [diff] [blame] | 2744 | if (BTy->getKind() == BuiltinType::Float) { |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 2745 | ArgTy = Context.DoubleTy; |
John McCall | eecf5fa | 2011-03-09 04:22:44 +0000 | [diff] [blame] | 2746 | Param->setKNRPromoted(true); |
| 2747 | } |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 2748 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2749 | } |
Fariborz Jahanian | 56a965c | 2010-09-08 21:36:35 +0000 | [diff] [blame] | 2750 | |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2751 | if (LangOpts.ObjCAutoRefCount) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2752 | bool Consumed = Param->hasAttr<NSConsumedAttr>(); |
| 2753 | ConsumedArguments.push_back(Consumed); |
| 2754 | HasAnyConsumedArguments |= Consumed; |
| 2755 | } |
| 2756 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2757 | ArgTys.push_back(ArgTy); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2758 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2759 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2760 | if (HasAnyConsumedArguments) |
| 2761 | EPI.ConsumedArguments = ConsumedArguments.data(); |
| 2762 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2763 | SmallVector<QualType, 4> Exceptions; |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 2764 | SmallVector<ParsedType, 2> DynamicExceptions; |
| 2765 | SmallVector<SourceRange, 2> DynamicExceptionRanges; |
| 2766 | Expr *NoexceptExpr = 0; |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 2767 | |
Sebastian Redl | 8b5b409 | 2011-03-06 10:52:04 +0000 | [diff] [blame] | 2768 | if (FTI.getExceptionSpecType() == EST_Dynamic) { |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 2769 | // FIXME: It's rather inefficient to have to split into two vectors |
| 2770 | // here. |
| 2771 | unsigned N = FTI.NumExceptions; |
| 2772 | DynamicExceptions.reserve(N); |
| 2773 | DynamicExceptionRanges.reserve(N); |
| 2774 | for (unsigned I = 0; I != N; ++I) { |
| 2775 | DynamicExceptions.push_back(FTI.Exceptions[I].Ty); |
| 2776 | DynamicExceptionRanges.push_back(FTI.Exceptions[I].Range); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2777 | } |
Sebastian Redl | 8b5b409 | 2011-03-06 10:52:04 +0000 | [diff] [blame] | 2778 | } else if (FTI.getExceptionSpecType() == EST_ComputedNoexcept) { |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 2779 | NoexceptExpr = FTI.NoexceptExpr; |
| 2780 | } |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 2781 | |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 2782 | S.checkExceptionSpecification(FTI.getExceptionSpecType(), |
| 2783 | DynamicExceptions, |
| 2784 | DynamicExceptionRanges, |
| 2785 | NoexceptExpr, |
| 2786 | Exceptions, |
| 2787 | EPI); |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 2788 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2789 | T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(), EPI); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2790 | } |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2791 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2792 | break; |
| 2793 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2794 | case DeclaratorChunk::MemberPointer: |
| 2795 | // The scope spec must refer to a class, or be dependent. |
Abramo Bagnara | 7bd0676 | 2010-08-13 12:56:25 +0000 | [diff] [blame] | 2796 | CXXScopeSpec &SS = DeclType.Mem.Scope(); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2797 | QualType ClsType; |
Abramo Bagnara | 7bd0676 | 2010-08-13 12:56:25 +0000 | [diff] [blame] | 2798 | if (SS.isInvalid()) { |
Jeffrey Yasskin | edc2877 | 2010-04-07 23:29:58 +0000 | [diff] [blame] | 2799 | // Avoid emitting extra errors if we already errored on the scope. |
| 2800 | D.setInvalidType(true); |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2801 | } else if (S.isDependentScopeSpecifier(SS) || |
| 2802 | dyn_cast_or_null<CXXRecordDecl>(S.computeDeclContext(SS))) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2803 | NestedNameSpecifier *NNS |
Abramo Bagnara | 7bd0676 | 2010-08-13 12:56:25 +0000 | [diff] [blame] | 2804 | = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 2805 | NestedNameSpecifier *NNSPrefix = NNS->getPrefix(); |
| 2806 | switch (NNS->getKind()) { |
| 2807 | case NestedNameSpecifier::Identifier: |
Abramo Bagnara | 7bd0676 | 2010-08-13 12:56:25 +0000 | [diff] [blame] | 2808 | ClsType = Context.getDependentNameType(ETK_None, NNSPrefix, |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2809 | NNS->getAsIdentifier()); |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 2810 | break; |
| 2811 | |
| 2812 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 2813 | case NestedNameSpecifier::NamespaceAlias: |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 2814 | case NestedNameSpecifier::Global: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2815 | llvm_unreachable("Nested-name-specifier must name a type"); |
Abramo Bagnara | 7bd0676 | 2010-08-13 12:56:25 +0000 | [diff] [blame] | 2816 | |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 2817 | case NestedNameSpecifier::TypeSpec: |
| 2818 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 2819 | ClsType = QualType(NNS->getAsType(), 0); |
Abramo Bagnara | 91ce2c4 | 2011-03-10 10:18:27 +0000 | [diff] [blame] | 2820 | // Note: if the NNS has a prefix and ClsType is a nondependent |
| 2821 | // TemplateSpecializationType, then the NNS prefix is NOT included |
| 2822 | // in ClsType; hence we wrap ClsType into an ElaboratedType. |
| 2823 | // NOTE: in particular, no wrap occurs if ClsType already is an |
| 2824 | // Elaborated, DependentName, or DependentTemplateSpecialization. |
| 2825 | if (NNSPrefix && isa<TemplateSpecializationType>(NNS->getAsType())) |
Abramo Bagnara | 7bd0676 | 2010-08-13 12:56:25 +0000 | [diff] [blame] | 2826 | ClsType = Context.getElaboratedType(ETK_None, NNSPrefix, ClsType); |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 2827 | break; |
| 2828 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2829 | } else { |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2830 | S.Diag(DeclType.Mem.Scope().getBeginLoc(), |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 2831 | diag::err_illegal_decl_mempointer_in_nonclass) |
| 2832 | << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name") |
| 2833 | << DeclType.Mem.Scope().getRange(); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2834 | D.setInvalidType(true); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2835 | } |
| 2836 | |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 2837 | if (!ClsType.isNull()) |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2838 | T = S.BuildMemberPointerType(T, ClsType, DeclType.Loc, D.getIdentifier()); |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 2839 | if (T.isNull()) { |
| 2840 | T = Context.IntTy; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2841 | D.setInvalidType(true); |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 2842 | } else if (DeclType.Mem.TypeQuals) { |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2843 | T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Mem.TypeQuals); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2844 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2845 | break; |
| 2846 | } |
| 2847 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 2848 | if (T.isNull()) { |
| 2849 | D.setInvalidType(true); |
| 2850 | T = Context.IntTy; |
| 2851 | } |
| 2852 | |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 2853 | // See if there are any attributes on this declarator chunk. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2854 | if (AttributeList *attrs = const_cast<AttributeList*>(DeclType.getAttrs())) |
Richard Smith | f7a0527 | 2013-01-14 07:53:01 +0000 | [diff] [blame] | 2855 | processTypeAttrs(state, T, TAL_DeclChunk, attrs); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2856 | } |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2857 | |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2858 | if (LangOpts.CPlusPlus && T->isFunctionType()) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2859 | const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>(); |
Chris Lattner | 778ed74 | 2009-10-25 17:36:50 +0000 | [diff] [blame] | 2860 | assert(FnTy && "Why oh why is there not a FunctionProtoType here?"); |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2861 | |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 2862 | // C++ 8.3.5p4: |
Douglas Gregor | 708f3b8 | 2010-10-14 22:03:51 +0000 | [diff] [blame] | 2863 | // A cv-qualifier-seq shall only be part of the function type |
| 2864 | // for a nonstatic member function, the function type to which a pointer |
| 2865 | // to member refers, or the top-level function type of a function typedef |
| 2866 | // declaration. |
Douglas Gregor | 683a81f | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 2867 | // |
| 2868 | // Core issue 547 also allows cv-qualifiers on function types that are |
| 2869 | // top-level template type arguments. |
John McCall | 613ef3d | 2010-10-19 01:54:45 +0000 | [diff] [blame] | 2870 | bool FreeFunction; |
| 2871 | if (!D.getCXXScopeSpec().isSet()) { |
Eli Friedman | 906a7e1 | 2012-01-06 03:05:34 +0000 | [diff] [blame] | 2872 | FreeFunction = ((D.getContext() != Declarator::MemberContext && |
| 2873 | D.getContext() != Declarator::LambdaExprContext) || |
John McCall | 613ef3d | 2010-10-19 01:54:45 +0000 | [diff] [blame] | 2874 | D.getDeclSpec().isFriendSpecified()); |
| 2875 | } else { |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2876 | DeclContext *DC = S.computeDeclContext(D.getCXXScopeSpec()); |
John McCall | 613ef3d | 2010-10-19 01:54:45 +0000 | [diff] [blame] | 2877 | FreeFunction = (DC && !DC->isRecord()); |
| 2878 | } |
| 2879 | |
Richard Smith | d37b360 | 2012-02-10 11:05:11 +0000 | [diff] [blame] | 2880 | // C++11 [dcl.fct]p6 (w/DR1417): |
| 2881 | // An attempt to specify a function type with a cv-qualifier-seq or a |
| 2882 | // ref-qualifier (including by typedef-name) is ill-formed unless it is: |
| 2883 | // - the function type for a non-static member function, |
| 2884 | // - the function type to which a pointer to member refers, |
| 2885 | // - the top-level function type of a function typedef declaration or |
| 2886 | // alias-declaration, |
| 2887 | // - the type-id in the default argument of a type-parameter, or |
| 2888 | // - the type-id of a template-argument for a type-parameter |
| 2889 | if (IsQualifiedFunction && |
| 2890 | !(!FreeFunction && |
| 2891 | D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_static) && |
| 2892 | !IsTypedefName && |
| 2893 | D.getContext() != Declarator::TemplateTypeArgContext) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2894 | SourceLocation Loc = D.getLocStart(); |
Richard Smith | d37b360 | 2012-02-10 11:05:11 +0000 | [diff] [blame] | 2895 | SourceRange RemovalRange; |
| 2896 | unsigned I; |
| 2897 | if (D.isFunctionDeclarator(I)) { |
| 2898 | SmallVector<SourceLocation, 4> RemovalLocs; |
| 2899 | const DeclaratorChunk &Chunk = D.getTypeObject(I); |
| 2900 | assert(Chunk.Kind == DeclaratorChunk::Function); |
| 2901 | if (Chunk.Fun.hasRefQualifier()) |
| 2902 | RemovalLocs.push_back(Chunk.Fun.getRefQualifierLoc()); |
| 2903 | if (Chunk.Fun.TypeQuals & Qualifiers::Const) |
| 2904 | RemovalLocs.push_back(Chunk.Fun.getConstQualifierLoc()); |
| 2905 | if (Chunk.Fun.TypeQuals & Qualifiers::Volatile) |
| 2906 | RemovalLocs.push_back(Chunk.Fun.getVolatileQualifierLoc()); |
| 2907 | // FIXME: We do not track the location of the __restrict qualifier. |
| 2908 | //if (Chunk.Fun.TypeQuals & Qualifiers::Restrict) |
| 2909 | // RemovalLocs.push_back(Chunk.Fun.getRestrictQualifierLoc()); |
| 2910 | if (!RemovalLocs.empty()) { |
| 2911 | std::sort(RemovalLocs.begin(), RemovalLocs.end(), |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 2912 | BeforeThanCompare<SourceLocation>(S.getSourceManager())); |
Richard Smith | d37b360 | 2012-02-10 11:05:11 +0000 | [diff] [blame] | 2913 | RemovalRange = SourceRange(RemovalLocs.front(), RemovalLocs.back()); |
| 2914 | Loc = RemovalLocs.front(); |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 2915 | } |
| 2916 | } |
Richard Smith | d37b360 | 2012-02-10 11:05:11 +0000 | [diff] [blame] | 2917 | |
| 2918 | S.Diag(Loc, diag::err_invalid_qualified_function_type) |
| 2919 | << FreeFunction << D.isFunctionDeclarator() << T |
| 2920 | << getFunctionQualifiersAsString(FnTy) |
| 2921 | << FixItHint::CreateRemoval(RemovalRange); |
| 2922 | |
| 2923 | // Strip the cv-qualifiers and ref-qualifiers from the type. |
| 2924 | FunctionProtoType::ExtProtoInfo EPI = FnTy->getExtProtoInfo(); |
| 2925 | EPI.TypeQuals = 0; |
| 2926 | EPI.RefQualifier = RQ_None; |
| 2927 | |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 2928 | T = Context.getFunctionType(FnTy->getResultType(), |
Richard Smith | d37b360 | 2012-02-10 11:05:11 +0000 | [diff] [blame] | 2929 | FnTy->arg_type_begin(), |
| 2930 | FnTy->getNumArgs(), EPI); |
Richard Smith | e925322 | 2012-10-24 23:51:56 +0000 | [diff] [blame] | 2931 | // Rebuild any parens around the identifier in the function type. |
| 2932 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { |
| 2933 | if (D.getTypeObject(i).Kind != DeclaratorChunk::Paren) |
| 2934 | break; |
| 2935 | T = S.BuildParenType(T); |
| 2936 | } |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2937 | } |
| 2938 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2939 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2940 | // Apply any undistributed attributes from the declarator. |
| 2941 | if (!T.isNull()) |
| 2942 | if (AttributeList *attrs = D.getAttributes()) |
Richard Smith | f7a0527 | 2013-01-14 07:53:01 +0000 | [diff] [blame] | 2943 | processTypeAttrs(state, T, TAL_DeclName, attrs); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2944 | |
| 2945 | // Diagnose any ignored type attributes. |
| 2946 | if (!T.isNull()) state.diagnoseIgnoredTypeAttrs(T); |
| 2947 | |
Peter Collingbourne | 148f1f7 | 2011-03-20 08:06:45 +0000 | [diff] [blame] | 2948 | // C++0x [dcl.constexpr]p9: |
| 2949 | // A constexpr specifier used in an object declaration declares the object |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 2950 | // as const. |
Peter Collingbourne | 148f1f7 | 2011-03-20 08:06:45 +0000 | [diff] [blame] | 2951 | if (D.getDeclSpec().isConstexprSpecified() && T->isObjectType()) { |
Sebastian Redl | 7378012 | 2010-06-09 21:19:43 +0000 | [diff] [blame] | 2952 | T.addConst(); |
| 2953 | } |
| 2954 | |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 2955 | // If there was an ellipsis in the declarator, the declaration declares a |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 2956 | // parameter pack whose type may be a pack expansion type. |
| 2957 | if (D.hasEllipsis() && !T.isNull()) { |
| 2958 | // C++0x [dcl.fct]p13: |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 2959 | // A declarator-id or abstract-declarator containing an ellipsis shall |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 2960 | // only be used in a parameter-declaration. Such a parameter-declaration |
| 2961 | // is a parameter pack (14.5.3). [...] |
| 2962 | switch (D.getContext()) { |
| 2963 | case Declarator::PrototypeContext: |
| 2964 | // C++0x [dcl.fct]p13: |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 2965 | // [...] When it is part of a parameter-declaration-clause, the |
| 2966 | // parameter pack is a function parameter pack (14.5.3). The type T |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 2967 | // of the declarator-id of the function parameter pack shall contain |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 2968 | // a template parameter pack; each template parameter pack in T is |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 2969 | // expanded by the function parameter pack. |
| 2970 | // |
| 2971 | // We represent function parameter packs as function parameters whose |
| 2972 | // type is a pack expansion. |
| 2973 | if (!T->containsUnexpandedParameterPack()) { |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 2974 | S.Diag(D.getEllipsisLoc(), |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 2975 | diag::err_function_parameter_pack_without_parameter_packs) |
| 2976 | << T << D.getSourceRange(); |
| 2977 | D.setEllipsisLoc(SourceLocation()); |
| 2978 | } else { |
David Blaikie | 66874fb | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 2979 | T = Context.getPackExpansionType(T, None); |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 2980 | } |
| 2981 | break; |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 2982 | |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 2983 | case Declarator::TemplateParamContext: |
| 2984 | // C++0x [temp.param]p15: |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 2985 | // If a template-parameter is a [...] is a parameter-declaration that |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 2986 | // declares a parameter pack (8.3.5), then the template-parameter is a |
| 2987 | // template parameter pack (14.5.3). |
| 2988 | // |
| 2989 | // Note: core issue 778 clarifies that, if there are any unexpanded |
| 2990 | // parameter packs in the type of the non-type template parameter, then |
| 2991 | // it expands those parameter packs. |
| 2992 | if (T->containsUnexpandedParameterPack()) |
David Blaikie | 66874fb | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 2993 | T = Context.getPackExpansionType(T, None); |
Richard Smith | e5acd13 | 2011-10-14 20:31:37 +0000 | [diff] [blame] | 2994 | else |
| 2995 | S.Diag(D.getEllipsisLoc(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2996 | LangOpts.CPlusPlus11 |
Richard Smith | e5acd13 | 2011-10-14 20:31:37 +0000 | [diff] [blame] | 2997 | ? diag::warn_cxx98_compat_variadic_templates |
| 2998 | : diag::ext_variadic_templates); |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 2999 | break; |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 3000 | |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 3001 | case Declarator::FileContext: |
| 3002 | case Declarator::KNRTypeListContext: |
John McCall | cdda47f | 2011-10-01 09:56:14 +0000 | [diff] [blame] | 3003 | case Declarator::ObjCParameterContext: // FIXME: special diagnostic here? |
| 3004 | case Declarator::ObjCResultContext: // FIXME: special diagnostic here? |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 3005 | case Declarator::TypeNameContext: |
Argyrios Kyrtzidis | 0b8c98f | 2011-06-28 03:01:23 +0000 | [diff] [blame] | 3006 | case Declarator::CXXNewContext: |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3007 | case Declarator::AliasDeclContext: |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3008 | case Declarator::AliasTemplateContext: |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 3009 | case Declarator::MemberContext: |
| 3010 | case Declarator::BlockContext: |
| 3011 | case Declarator::ForContext: |
| 3012 | case Declarator::ConditionContext: |
| 3013 | case Declarator::CXXCatchContext: |
Argyrios Kyrtzidis | 17b6399 | 2011-07-01 22:22:40 +0000 | [diff] [blame] | 3014 | case Declarator::ObjCCatchContext: |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 3015 | case Declarator::BlockLiteralContext: |
Eli Friedman | f88c400 | 2012-01-04 04:41:38 +0000 | [diff] [blame] | 3016 | case Declarator::LambdaExprContext: |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3017 | case Declarator::TrailingReturnContext: |
Douglas Gregor | 683a81f | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 3018 | case Declarator::TemplateTypeArgContext: |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 3019 | // FIXME: We may want to allow parameter packs in block-literal contexts |
| 3020 | // in the future. |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 3021 | S.Diag(D.getEllipsisLoc(), diag::err_ellipsis_in_declarator_not_parameter); |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 3022 | D.setEllipsisLoc(SourceLocation()); |
| 3023 | break; |
| 3024 | } |
| 3025 | } |
Richard Smith | e7397c6 | 2011-02-22 00:36:53 +0000 | [diff] [blame] | 3026 | |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 3027 | if (T.isNull()) |
| 3028 | return Context.getNullTypeSourceInfo(); |
| 3029 | else if (D.isInvalidType()) |
| 3030 | return Context.getTrivialTypeSourceInfo(T); |
Argyrios Kyrtzidis | db7abf7 | 2011-06-28 03:01:12 +0000 | [diff] [blame] | 3031 | |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 3032 | return S.GetTypeSourceInfoForDeclarator(D, T, TInfo); |
| 3033 | } |
| 3034 | |
| 3035 | /// GetTypeForDeclarator - Convert the type for the specified |
| 3036 | /// declarator to Type instances. |
| 3037 | /// |
| 3038 | /// The result of this call will never be null, but the associated |
| 3039 | /// type may be a null type if there's an unrecoverable error. |
| 3040 | TypeSourceInfo *Sema::GetTypeForDeclarator(Declarator &D, Scope *S) { |
| 3041 | // Determine the type of the declarator. Not all forms of declarator |
| 3042 | // have a type. |
| 3043 | |
| 3044 | TypeProcessingState state(*this, D); |
| 3045 | |
| 3046 | TypeSourceInfo *ReturnTypeInfo = 0; |
| 3047 | QualType T = GetDeclSpecTypeForDeclarator(state, ReturnTypeInfo); |
| 3048 | if (T.isNull()) |
| 3049 | return Context.getNullTypeSourceInfo(); |
| 3050 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3051 | if (D.isPrototypeContext() && getLangOpts().ObjCAutoRefCount) |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 3052 | inferARCWriteback(state, T); |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 3053 | |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 3054 | return GetFullTypeForDeclarator(state, T, ReturnTypeInfo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3055 | } |
| 3056 | |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 3057 | static void transferARCOwnershipToDeclSpec(Sema &S, |
| 3058 | QualType &declSpecTy, |
| 3059 | Qualifiers::ObjCLifetime ownership) { |
| 3060 | if (declSpecTy->isObjCRetainableType() && |
| 3061 | declSpecTy.getObjCLifetime() == Qualifiers::OCL_None) { |
| 3062 | Qualifiers qs; |
| 3063 | qs.addObjCLifetime(ownership); |
| 3064 | declSpecTy = S.Context.getQualifiedType(declSpecTy, qs); |
| 3065 | } |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 3066 | } |
| 3067 | |
| 3068 | static void transferARCOwnershipToDeclaratorChunk(TypeProcessingState &state, |
| 3069 | Qualifiers::ObjCLifetime ownership, |
| 3070 | unsigned chunkIndex) { |
| 3071 | Sema &S = state.getSema(); |
| 3072 | Declarator &D = state.getDeclarator(); |
| 3073 | |
| 3074 | // Look for an explicit lifetime attribute. |
| 3075 | DeclaratorChunk &chunk = D.getTypeObject(chunkIndex); |
| 3076 | for (const AttributeList *attr = chunk.getAttrs(); attr; |
| 3077 | attr = attr->getNext()) |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3078 | if (attr->getKind() == AttributeList::AT_ObjCOwnership) |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 3079 | return; |
| 3080 | |
| 3081 | const char *attrStr = 0; |
| 3082 | switch (ownership) { |
David Blaikie | 3026348 | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 3083 | case Qualifiers::OCL_None: llvm_unreachable("no ownership!"); |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 3084 | case Qualifiers::OCL_ExplicitNone: attrStr = "none"; break; |
| 3085 | case Qualifiers::OCL_Strong: attrStr = "strong"; break; |
| 3086 | case Qualifiers::OCL_Weak: attrStr = "weak"; break; |
| 3087 | case Qualifiers::OCL_Autoreleasing: attrStr = "autoreleasing"; break; |
| 3088 | } |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 3089 | |
| 3090 | // If there wasn't one, add one (with an invalid source location |
| 3091 | // so that we don't make an AttributedType for it). |
| 3092 | AttributeList *attr = D.getAttributePool() |
| 3093 | .create(&S.Context.Idents.get("objc_ownership"), SourceLocation(), |
| 3094 | /*scope*/ 0, SourceLocation(), |
| 3095 | &S.Context.Idents.get(attrStr), SourceLocation(), |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 3096 | /*args*/ 0, 0, AttributeList::AS_GNU); |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 3097 | spliceAttrIntoList(*attr, chunk.getAttrListRef()); |
| 3098 | |
| 3099 | // TODO: mark whether we did this inference? |
| 3100 | } |
| 3101 | |
Benjamin Kramer | 48d798c | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 3102 | /// \brief Used for transferring ownership in casts resulting in l-values. |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 3103 | static void transferARCOwnership(TypeProcessingState &state, |
| 3104 | QualType &declSpecTy, |
| 3105 | Qualifiers::ObjCLifetime ownership) { |
| 3106 | Sema &S = state.getSema(); |
| 3107 | Declarator &D = state.getDeclarator(); |
| 3108 | |
| 3109 | int inner = -1; |
Argyrios Kyrtzidis | 6ee5492 | 2011-10-28 22:54:28 +0000 | [diff] [blame] | 3110 | bool hasIndirection = false; |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 3111 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { |
| 3112 | DeclaratorChunk &chunk = D.getTypeObject(i); |
| 3113 | switch (chunk.Kind) { |
| 3114 | case DeclaratorChunk::Paren: |
| 3115 | // Ignore parens. |
| 3116 | break; |
| 3117 | |
| 3118 | case DeclaratorChunk::Array: |
| 3119 | case DeclaratorChunk::Reference: |
| 3120 | case DeclaratorChunk::Pointer: |
Argyrios Kyrtzidis | 6ee5492 | 2011-10-28 22:54:28 +0000 | [diff] [blame] | 3121 | if (inner != -1) |
| 3122 | hasIndirection = true; |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 3123 | inner = i; |
| 3124 | break; |
| 3125 | |
| 3126 | case DeclaratorChunk::BlockPointer: |
Argyrios Kyrtzidis | 6ee5492 | 2011-10-28 22:54:28 +0000 | [diff] [blame] | 3127 | if (inner != -1) |
| 3128 | transferARCOwnershipToDeclaratorChunk(state, ownership, i); |
| 3129 | return; |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 3130 | |
| 3131 | case DeclaratorChunk::Function: |
| 3132 | case DeclaratorChunk::MemberPointer: |
| 3133 | return; |
| 3134 | } |
| 3135 | } |
| 3136 | |
| 3137 | if (inner == -1) |
Argyrios Kyrtzidis | 6ee5492 | 2011-10-28 22:54:28 +0000 | [diff] [blame] | 3138 | return; |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 3139 | |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 3140 | DeclaratorChunk &chunk = D.getTypeObject(inner); |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 3141 | if (chunk.Kind == DeclaratorChunk::Pointer) { |
| 3142 | if (declSpecTy->isObjCRetainableType()) |
| 3143 | return transferARCOwnershipToDeclSpec(S, declSpecTy, ownership); |
Argyrios Kyrtzidis | 6ee5492 | 2011-10-28 22:54:28 +0000 | [diff] [blame] | 3144 | if (declSpecTy->isObjCObjectType() && hasIndirection) |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 3145 | return transferARCOwnershipToDeclaratorChunk(state, ownership, inner); |
| 3146 | } else { |
| 3147 | assert(chunk.Kind == DeclaratorChunk::Array || |
| 3148 | chunk.Kind == DeclaratorChunk::Reference); |
| 3149 | return transferARCOwnershipToDeclSpec(S, declSpecTy, ownership); |
| 3150 | } |
| 3151 | } |
| 3152 | |
| 3153 | TypeSourceInfo *Sema::GetTypeForDeclaratorCast(Declarator &D, QualType FromTy) { |
| 3154 | TypeProcessingState state(*this, D); |
| 3155 | |
| 3156 | TypeSourceInfo *ReturnTypeInfo = 0; |
| 3157 | QualType declSpecTy = GetDeclSpecTypeForDeclarator(state, ReturnTypeInfo); |
| 3158 | if (declSpecTy.isNull()) |
| 3159 | return Context.getNullTypeSourceInfo(); |
| 3160 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3161 | if (getLangOpts().ObjCAutoRefCount) { |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 3162 | Qualifiers::ObjCLifetime ownership = Context.getInnerObjCOwnership(FromTy); |
| 3163 | if (ownership != Qualifiers::OCL_None) |
| 3164 | transferARCOwnership(state, declSpecTy, ownership); |
| 3165 | } |
| 3166 | |
| 3167 | return GetFullTypeForDeclarator(state, declSpecTy, ReturnTypeInfo); |
| 3168 | } |
| 3169 | |
John McCall | 14aa217 | 2011-03-04 04:00:19 +0000 | [diff] [blame] | 3170 | /// Map an AttributedType::Kind to an AttributeList::Kind. |
| 3171 | static AttributeList::Kind getAttrListKind(AttributedType::Kind kind) { |
| 3172 | switch (kind) { |
| 3173 | case AttributedType::attr_address_space: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3174 | return AttributeList::AT_AddressSpace; |
John McCall | 14aa217 | 2011-03-04 04:00:19 +0000 | [diff] [blame] | 3175 | case AttributedType::attr_regparm: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3176 | return AttributeList::AT_Regparm; |
John McCall | 14aa217 | 2011-03-04 04:00:19 +0000 | [diff] [blame] | 3177 | case AttributedType::attr_vector_size: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3178 | return AttributeList::AT_VectorSize; |
John McCall | 14aa217 | 2011-03-04 04:00:19 +0000 | [diff] [blame] | 3179 | case AttributedType::attr_neon_vector_type: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3180 | return AttributeList::AT_NeonVectorType; |
John McCall | 14aa217 | 2011-03-04 04:00:19 +0000 | [diff] [blame] | 3181 | case AttributedType::attr_neon_polyvector_type: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3182 | return AttributeList::AT_NeonPolyVectorType; |
John McCall | 14aa217 | 2011-03-04 04:00:19 +0000 | [diff] [blame] | 3183 | case AttributedType::attr_objc_gc: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3184 | return AttributeList::AT_ObjCGC; |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 3185 | case AttributedType::attr_objc_ownership: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3186 | return AttributeList::AT_ObjCOwnership; |
John McCall | 14aa217 | 2011-03-04 04:00:19 +0000 | [diff] [blame] | 3187 | case AttributedType::attr_noreturn: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3188 | return AttributeList::AT_NoReturn; |
John McCall | 14aa217 | 2011-03-04 04:00:19 +0000 | [diff] [blame] | 3189 | case AttributedType::attr_cdecl: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3190 | return AttributeList::AT_CDecl; |
John McCall | 14aa217 | 2011-03-04 04:00:19 +0000 | [diff] [blame] | 3191 | case AttributedType::attr_fastcall: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3192 | return AttributeList::AT_FastCall; |
John McCall | 14aa217 | 2011-03-04 04:00:19 +0000 | [diff] [blame] | 3193 | case AttributedType::attr_stdcall: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3194 | return AttributeList::AT_StdCall; |
John McCall | 14aa217 | 2011-03-04 04:00:19 +0000 | [diff] [blame] | 3195 | case AttributedType::attr_thiscall: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3196 | return AttributeList::AT_ThisCall; |
John McCall | 14aa217 | 2011-03-04 04:00:19 +0000 | [diff] [blame] | 3197 | case AttributedType::attr_pascal: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3198 | return AttributeList::AT_Pascal; |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3199 | case AttributedType::attr_pcs: |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3200 | return AttributeList::AT_Pcs; |
Derek Schuff | 263366f | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3201 | case AttributedType::attr_pnaclcall: |
| 3202 | return AttributeList::AT_PnaclCall; |
Guy Benyei | 3898008 | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3203 | case AttributedType::attr_inteloclbicc: |
| 3204 | return AttributeList::AT_IntelOclBicc; |
John McCall | 14aa217 | 2011-03-04 04:00:19 +0000 | [diff] [blame] | 3205 | } |
| 3206 | llvm_unreachable("unexpected attribute kind!"); |
John McCall | 14aa217 | 2011-03-04 04:00:19 +0000 | [diff] [blame] | 3207 | } |
| 3208 | |
| 3209 | static void fillAttributedTypeLoc(AttributedTypeLoc TL, |
| 3210 | const AttributeList *attrs) { |
| 3211 | AttributedType::Kind kind = TL.getAttrKind(); |
| 3212 | |
| 3213 | assert(attrs && "no type attributes in the expected location!"); |
| 3214 | AttributeList::Kind parsedKind = getAttrListKind(kind); |
| 3215 | while (attrs->getKind() != parsedKind) { |
| 3216 | attrs = attrs->getNext(); |
| 3217 | assert(attrs && "no matching attribute in expected location!"); |
| 3218 | } |
| 3219 | |
| 3220 | TL.setAttrNameLoc(attrs->getLoc()); |
| 3221 | if (TL.hasAttrExprOperand()) |
| 3222 | TL.setAttrExprOperand(attrs->getArg(0)); |
| 3223 | else if (TL.hasAttrEnumOperand()) |
| 3224 | TL.setAttrEnumOperandLoc(attrs->getParameterLoc()); |
| 3225 | |
| 3226 | // FIXME: preserve this information to here. |
| 3227 | if (TL.hasAttrOperand()) |
| 3228 | TL.setAttrOperandParensRange(SourceRange()); |
| 3229 | } |
| 3230 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 3231 | namespace { |
| 3232 | class TypeSpecLocFiller : public TypeLocVisitor<TypeSpecLocFiller> { |
Douglas Gregor | c21c7e9 | 2011-01-25 19:13:18 +0000 | [diff] [blame] | 3233 | ASTContext &Context; |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 3234 | const DeclSpec &DS; |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 3235 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 3236 | public: |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 3237 | TypeSpecLocFiller(ASTContext &Context, const DeclSpec &DS) |
Douglas Gregor | c21c7e9 | 2011-01-25 19:13:18 +0000 | [diff] [blame] | 3238 | : Context(Context), DS(DS) {} |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 3239 | |
John McCall | 14aa217 | 2011-03-04 04:00:19 +0000 | [diff] [blame] | 3240 | void VisitAttributedTypeLoc(AttributedTypeLoc TL) { |
| 3241 | fillAttributedTypeLoc(TL, DS.getAttributes().getList()); |
| 3242 | Visit(TL.getModifiedLoc()); |
| 3243 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 3244 | void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { |
| 3245 | Visit(TL.getUnqualifiedLoc()); |
| 3246 | } |
| 3247 | void VisitTypedefTypeLoc(TypedefTypeLoc TL) { |
| 3248 | TL.setNameLoc(DS.getTypeSpecTypeLoc()); |
| 3249 | } |
| 3250 | void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { |
| 3251 | TL.setNameLoc(DS.getTypeSpecTypeLoc()); |
Fariborz Jahanian | 1de6a6c | 2012-05-09 21:49:29 +0000 | [diff] [blame] | 3252 | // FIXME. We should have DS.getTypeSpecTypeEndLoc(). But, it requires |
| 3253 | // addition field. What we have is good enough for dispay of location |
| 3254 | // of 'fixit' on interface name. |
| 3255 | TL.setNameEndLoc(DS.getLocEnd()); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3256 | } |
| 3257 | void VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { |
| 3258 | // Handle the base type, which might not have been written explicitly. |
| 3259 | if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) { |
| 3260 | TL.setHasBaseTypeAsWritten(false); |
Douglas Gregor | c21c7e9 | 2011-01-25 19:13:18 +0000 | [diff] [blame] | 3261 | TL.getBaseLoc().initialize(Context, SourceLocation()); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3262 | } else { |
| 3263 | TL.setHasBaseTypeAsWritten(true); |
| 3264 | Visit(TL.getBaseLoc()); |
| 3265 | } |
Argyrios Kyrtzidis | eb66759 | 2009-09-29 19:45:22 +0000 | [diff] [blame] | 3266 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3267 | // Protocol qualifiers. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3268 | if (DS.getProtocolQualifiers()) { |
| 3269 | assert(TL.getNumProtocols() > 0); |
| 3270 | assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers()); |
| 3271 | TL.setLAngleLoc(DS.getProtocolLAngleLoc()); |
| 3272 | TL.setRAngleLoc(DS.getSourceRange().getEnd()); |
| 3273 | for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i) |
| 3274 | TL.setProtocolLoc(i, DS.getProtocolLocs()[i]); |
| 3275 | } else { |
| 3276 | assert(TL.getNumProtocols() == 0); |
| 3277 | TL.setLAngleLoc(SourceLocation()); |
| 3278 | TL.setRAngleLoc(SourceLocation()); |
| 3279 | } |
| 3280 | } |
| 3281 | void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3282 | TL.setStarLoc(SourceLocation()); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3283 | Visit(TL.getPointeeLoc()); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 3284 | } |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3285 | void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3286 | TypeSourceInfo *TInfo = 0; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3287 | Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3288 | |
| 3289 | // If we got no declarator info from previous Sema routines, |
| 3290 | // just fill with the typespec loc. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3291 | if (!TInfo) { |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 3292 | TL.initialize(Context, DS.getTypeSpecTypeNameLoc()); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3293 | return; |
| 3294 | } |
| 3295 | |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 3296 | TypeLoc OldTL = TInfo->getTypeLoc(); |
| 3297 | if (TInfo->getType()->getAs<ElaboratedType>()) { |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3298 | ElaboratedTypeLoc ElabTL = OldTL.castAs<ElaboratedTypeLoc>(); |
| 3299 | TemplateSpecializationTypeLoc NamedTL = ElabTL.getNamedTypeLoc() |
| 3300 | .castAs<TemplateSpecializationTypeLoc>(); |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 3301 | TL.copy(NamedTL); |
| 3302 | } |
| 3303 | else |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3304 | TL.copy(OldTL.castAs<TemplateSpecializationTypeLoc>()); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3305 | } |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 3306 | void VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { |
| 3307 | assert(DS.getTypeSpecType() == DeclSpec::TST_typeofExpr); |
| 3308 | TL.setTypeofLoc(DS.getTypeSpecTypeLoc()); |
| 3309 | TL.setParensRange(DS.getTypeofParensRange()); |
| 3310 | } |
| 3311 | void VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { |
| 3312 | assert(DS.getTypeSpecType() == DeclSpec::TST_typeofType); |
| 3313 | TL.setTypeofLoc(DS.getTypeSpecTypeLoc()); |
| 3314 | TL.setParensRange(DS.getTypeofParensRange()); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3315 | assert(DS.getRepAsType()); |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 3316 | TypeSourceInfo *TInfo = 0; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3317 | Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo); |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 3318 | TL.setUnderlyingTInfo(TInfo); |
| 3319 | } |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 3320 | void VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { |
| 3321 | // FIXME: This holds only because we only have one unary transform. |
| 3322 | assert(DS.getTypeSpecType() == DeclSpec::TST_underlyingType); |
| 3323 | TL.setKWLoc(DS.getTypeSpecTypeLoc()); |
| 3324 | TL.setParensRange(DS.getTypeofParensRange()); |
| 3325 | assert(DS.getRepAsType()); |
| 3326 | TypeSourceInfo *TInfo = 0; |
| 3327 | Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo); |
| 3328 | TL.setUnderlyingTInfo(TInfo); |
| 3329 | } |
Douglas Gregor | ddf889a | 2010-01-18 18:04:31 +0000 | [diff] [blame] | 3330 | void VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { |
| 3331 | // By default, use the source location of the type specifier. |
| 3332 | TL.setBuiltinLoc(DS.getTypeSpecTypeLoc()); |
| 3333 | if (TL.needsExtraLocalData()) { |
| 3334 | // Set info for the written builtin specifiers. |
| 3335 | TL.getWrittenBuiltinSpecs() = DS.getWrittenBuiltinSpecs(); |
| 3336 | // Try to have a meaningful source location. |
| 3337 | if (TL.getWrittenSignSpec() != TSS_unspecified) |
| 3338 | // Sign spec loc overrides the others (e.g., 'unsigned long'). |
| 3339 | TL.setBuiltinLoc(DS.getTypeSpecSignLoc()); |
| 3340 | else if (TL.getWrittenWidthSpec() != TSW_unspecified) |
| 3341 | // Width spec loc overrides type spec loc (e.g., 'short int'). |
| 3342 | TL.setBuiltinLoc(DS.getTypeSpecWidthLoc()); |
| 3343 | } |
| 3344 | } |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 3345 | void VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { |
| 3346 | ElaboratedTypeKeyword Keyword |
| 3347 | = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType()); |
Nico Weber | 253e80b | 2010-11-22 10:30:56 +0000 | [diff] [blame] | 3348 | if (DS.getTypeSpecType() == TST_typename) { |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 3349 | TypeSourceInfo *TInfo = 0; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3350 | Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo); |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 3351 | if (TInfo) { |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3352 | TL.copy(TInfo->getTypeLoc().castAs<ElaboratedTypeLoc>()); |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 3353 | return; |
| 3354 | } |
| 3355 | } |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 3356 | TL.setElaboratedKeywordLoc(Keyword != ETK_None |
| 3357 | ? DS.getTypeSpecTypeLoc() |
| 3358 | : SourceLocation()); |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 3359 | const CXXScopeSpec& SS = DS.getTypeSpecScope(); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 3360 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 3361 | Visit(TL.getNextTypeLoc().getUnqualifiedLoc()); |
| 3362 | } |
| 3363 | void VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { |
Abramo Bagnara | 66581d4 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 3364 | assert(DS.getTypeSpecType() == TST_typename); |
| 3365 | TypeSourceInfo *TInfo = 0; |
| 3366 | Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo); |
| 3367 | assert(TInfo); |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3368 | TL.copy(TInfo->getTypeLoc().castAs<DependentNameTypeLoc>()); |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 3369 | } |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3370 | void VisitDependentTemplateSpecializationTypeLoc( |
| 3371 | DependentTemplateSpecializationTypeLoc TL) { |
Abramo Bagnara | 66581d4 | 2012-02-06 22:45:07 +0000 | [diff] [blame] | 3372 | assert(DS.getTypeSpecType() == TST_typename); |
| 3373 | TypeSourceInfo *TInfo = 0; |
| 3374 | Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo); |
| 3375 | assert(TInfo); |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3376 | TL.copy( |
| 3377 | TInfo->getTypeLoc().castAs<DependentTemplateSpecializationTypeLoc>()); |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 3378 | } |
| 3379 | void VisitTagTypeLoc(TagTypeLoc TL) { |
| 3380 | TL.setNameLoc(DS.getTypeSpecTypeNameLoc()); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3381 | } |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3382 | void VisitAtomicTypeLoc(AtomicTypeLoc TL) { |
| 3383 | TL.setKWLoc(DS.getTypeSpecTypeLoc()); |
| 3384 | TL.setParensRange(DS.getTypeofParensRange()); |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 3385 | |
Douglas Gregor | 43fe245 | 2011-10-09 18:45:17 +0000 | [diff] [blame] | 3386 | TypeSourceInfo *TInfo = 0; |
| 3387 | Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo); |
Jordan Rose | 41f3f3a | 2013-03-05 01:27:54 +0000 | [diff] [blame^] | 3388 | assert(TInfo); |
Douglas Gregor | 43fe245 | 2011-10-09 18:45:17 +0000 | [diff] [blame] | 3389 | TL.getValueLoc().initializeFullCopy(TInfo->getTypeLoc()); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3390 | } |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 3391 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 3392 | void VisitTypeLoc(TypeLoc TL) { |
| 3393 | // FIXME: add other typespec types and change this to an assert. |
Douglas Gregor | c21c7e9 | 2011-01-25 19:13:18 +0000 | [diff] [blame] | 3394 | TL.initialize(Context, DS.getTypeSpecTypeLoc()); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 3395 | } |
| 3396 | }; |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 3397 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 3398 | class DeclaratorLocFiller : public TypeLocVisitor<DeclaratorLocFiller> { |
Abramo Bagnara | b6ab6c1 | 2011-03-05 14:42:21 +0000 | [diff] [blame] | 3399 | ASTContext &Context; |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 3400 | const DeclaratorChunk &Chunk; |
| 3401 | |
| 3402 | public: |
Abramo Bagnara | b6ab6c1 | 2011-03-05 14:42:21 +0000 | [diff] [blame] | 3403 | DeclaratorLocFiller(ASTContext &Context, const DeclaratorChunk &Chunk) |
| 3404 | : Context(Context), Chunk(Chunk) {} |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 3405 | |
| 3406 | void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 3407 | llvm_unreachable("qualified type locs not expected here!"); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 3408 | } |
| 3409 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3410 | void VisitAttributedTypeLoc(AttributedTypeLoc TL) { |
| 3411 | fillAttributedTypeLoc(TL, Chunk.getAttrs()); |
| 3412 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 3413 | void VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { |
| 3414 | assert(Chunk.Kind == DeclaratorChunk::BlockPointer); |
| 3415 | TL.setCaretLoc(Chunk.Loc); |
| 3416 | } |
| 3417 | void VisitPointerTypeLoc(PointerTypeLoc TL) { |
| 3418 | assert(Chunk.Kind == DeclaratorChunk::Pointer); |
| 3419 | TL.setStarLoc(Chunk.Loc); |
| 3420 | } |
| 3421 | void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { |
| 3422 | assert(Chunk.Kind == DeclaratorChunk::Pointer); |
| 3423 | TL.setStarLoc(Chunk.Loc); |
| 3424 | } |
| 3425 | void VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { |
| 3426 | assert(Chunk.Kind == DeclaratorChunk::MemberPointer); |
Abramo Bagnara | b6ab6c1 | 2011-03-05 14:42:21 +0000 | [diff] [blame] | 3427 | const CXXScopeSpec& SS = Chunk.Mem.Scope(); |
| 3428 | NestedNameSpecifierLoc NNSLoc = SS.getWithLocInContext(Context); |
| 3429 | |
| 3430 | const Type* ClsTy = TL.getClass(); |
| 3431 | QualType ClsQT = QualType(ClsTy, 0); |
| 3432 | TypeSourceInfo *ClsTInfo = Context.CreateTypeSourceInfo(ClsQT, 0); |
| 3433 | // Now copy source location info into the type loc component. |
| 3434 | TypeLoc ClsTL = ClsTInfo->getTypeLoc(); |
| 3435 | switch (NNSLoc.getNestedNameSpecifier()->getKind()) { |
| 3436 | case NestedNameSpecifier::Identifier: |
| 3437 | assert(isa<DependentNameType>(ClsTy) && "Unexpected TypeLoc"); |
| 3438 | { |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3439 | DependentNameTypeLoc DNTLoc = ClsTL.castAs<DependentNameTypeLoc>(); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 3440 | DNTLoc.setElaboratedKeywordLoc(SourceLocation()); |
Abramo Bagnara | b6ab6c1 | 2011-03-05 14:42:21 +0000 | [diff] [blame] | 3441 | DNTLoc.setQualifierLoc(NNSLoc.getPrefix()); |
| 3442 | DNTLoc.setNameLoc(NNSLoc.getLocalBeginLoc()); |
| 3443 | } |
| 3444 | break; |
| 3445 | |
| 3446 | case NestedNameSpecifier::TypeSpec: |
| 3447 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 3448 | if (isa<ElaboratedType>(ClsTy)) { |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3449 | ElaboratedTypeLoc ETLoc = ClsTL.castAs<ElaboratedTypeLoc>(); |
Abramo Bagnara | 38a4291 | 2012-02-06 19:09:27 +0000 | [diff] [blame] | 3450 | ETLoc.setElaboratedKeywordLoc(SourceLocation()); |
Abramo Bagnara | b6ab6c1 | 2011-03-05 14:42:21 +0000 | [diff] [blame] | 3451 | ETLoc.setQualifierLoc(NNSLoc.getPrefix()); |
| 3452 | TypeLoc NamedTL = ETLoc.getNamedTypeLoc(); |
| 3453 | NamedTL.initializeFullCopy(NNSLoc.getTypeLoc()); |
| 3454 | } else { |
| 3455 | ClsTL.initializeFullCopy(NNSLoc.getTypeLoc()); |
| 3456 | } |
| 3457 | break; |
| 3458 | |
| 3459 | case NestedNameSpecifier::Namespace: |
| 3460 | case NestedNameSpecifier::NamespaceAlias: |
| 3461 | case NestedNameSpecifier::Global: |
| 3462 | llvm_unreachable("Nested-name-specifier must name a type"); |
Abramo Bagnara | b6ab6c1 | 2011-03-05 14:42:21 +0000 | [diff] [blame] | 3463 | } |
| 3464 | |
| 3465 | // Finally fill in MemberPointerLocInfo fields. |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 3466 | TL.setStarLoc(Chunk.Loc); |
Abramo Bagnara | b6ab6c1 | 2011-03-05 14:42:21 +0000 | [diff] [blame] | 3467 | TL.setClassTInfo(ClsTInfo); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 3468 | } |
| 3469 | void VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { |
| 3470 | assert(Chunk.Kind == DeclaratorChunk::Reference); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3471 | // 'Amp' is misleading: this might have been originally |
| 3472 | /// spelled with AmpAmp. |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 3473 | TL.setAmpLoc(Chunk.Loc); |
| 3474 | } |
| 3475 | void VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { |
| 3476 | assert(Chunk.Kind == DeclaratorChunk::Reference); |
| 3477 | assert(!Chunk.Ref.LValueRef); |
| 3478 | TL.setAmpAmpLoc(Chunk.Loc); |
| 3479 | } |
| 3480 | void VisitArrayTypeLoc(ArrayTypeLoc TL) { |
| 3481 | assert(Chunk.Kind == DeclaratorChunk::Array); |
| 3482 | TL.setLBracketLoc(Chunk.Loc); |
| 3483 | TL.setRBracketLoc(Chunk.EndLoc); |
| 3484 | TL.setSizeExpr(static_cast<Expr*>(Chunk.Arr.NumElts)); |
| 3485 | } |
| 3486 | void VisitFunctionTypeLoc(FunctionTypeLoc TL) { |
| 3487 | assert(Chunk.Kind == DeclaratorChunk::Function); |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 3488 | TL.setLocalRangeBegin(Chunk.Loc); |
| 3489 | TL.setLocalRangeEnd(Chunk.EndLoc); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 3490 | |
| 3491 | const DeclaratorChunk::FunctionTypeInfo &FTI = Chunk.Fun; |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 3492 | TL.setLParenLoc(FTI.getLParenLoc()); |
| 3493 | TL.setRParenLoc(FTI.getRParenLoc()); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3494 | for (unsigned i = 0, e = TL.getNumArgs(), tpi = 0; i != e; ++i) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3495 | ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3496 | TL.setArg(tpi++, Param); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 3497 | } |
| 3498 | // FIXME: exception specs |
| 3499 | } |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 3500 | void VisitParenTypeLoc(ParenTypeLoc TL) { |
| 3501 | assert(Chunk.Kind == DeclaratorChunk::Paren); |
| 3502 | TL.setLParenLoc(Chunk.Loc); |
| 3503 | TL.setRParenLoc(Chunk.EndLoc); |
| 3504 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 3505 | |
| 3506 | void VisitTypeLoc(TypeLoc TL) { |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 3507 | llvm_unreachable("unsupported TypeLoc kind in declarator!"); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 3508 | } |
| 3509 | }; |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 3510 | } |
| 3511 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3512 | /// \brief Create and instantiate a TypeSourceInfo with type source information. |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 3513 | /// |
| 3514 | /// \param T QualType referring to the type as written in source code. |
Douglas Gregor | 05baacb | 2010-04-12 23:19:01 +0000 | [diff] [blame] | 3515 | /// |
| 3516 | /// \param ReturnTypeInfo For declarators whose return type does not show |
| 3517 | /// up in the normal place in the declaration specifiers (such as a C++ |
| 3518 | /// conversion function), this pointer will refer to a type source information |
| 3519 | /// for that return type. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3520 | TypeSourceInfo * |
Douglas Gregor | 05baacb | 2010-04-12 23:19:01 +0000 | [diff] [blame] | 3521 | Sema::GetTypeSourceInfoForDeclarator(Declarator &D, QualType T, |
| 3522 | TypeSourceInfo *ReturnTypeInfo) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3523 | TypeSourceInfo *TInfo = Context.CreateTypeSourceInfo(T); |
| 3524 | UnqualTypeLoc CurrTL = TInfo->getTypeLoc().getUnqualifiedLoc(); |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 3525 | |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 3526 | // Handle parameter packs whose type is a pack expansion. |
| 3527 | if (isa<PackExpansionType>(T)) { |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3528 | CurrTL.castAs<PackExpansionTypeLoc>().setEllipsisLoc(D.getEllipsisLoc()); |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 3529 | CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc(); |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 3530 | } |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 3531 | |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 3532 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3533 | while (AttributedTypeLoc TL = CurrTL.getAs<AttributedTypeLoc>()) { |
John McCall | 14aa217 | 2011-03-04 04:00:19 +0000 | [diff] [blame] | 3534 | fillAttributedTypeLoc(TL, D.getTypeObject(i).getAttrs()); |
| 3535 | CurrTL = TL.getNextTypeLoc().getUnqualifiedLoc(); |
| 3536 | } |
| 3537 | |
Abramo Bagnara | b6ab6c1 | 2011-03-05 14:42:21 +0000 | [diff] [blame] | 3538 | DeclaratorLocFiller(Context, D.getTypeObject(i)).Visit(CurrTL); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 3539 | CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc(); |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 3540 | } |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 3541 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3542 | // If we have different source information for the return type, use |
| 3543 | // that. This really only applies to C++ conversion functions. |
| 3544 | if (ReturnTypeInfo) { |
Douglas Gregor | 05baacb | 2010-04-12 23:19:01 +0000 | [diff] [blame] | 3545 | TypeLoc TL = ReturnTypeInfo->getTypeLoc(); |
| 3546 | assert(TL.getFullDataSize() == CurrTL.getFullDataSize()); |
| 3547 | memcpy(CurrTL.getOpaqueData(), TL.getOpaqueData(), TL.getFullDataSize()); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3548 | } else { |
Douglas Gregor | c21c7e9 | 2011-01-25 19:13:18 +0000 | [diff] [blame] | 3549 | TypeSpecLocFiller(Context, D.getDeclSpec()).Visit(CurrTL); |
Douglas Gregor | 05baacb | 2010-04-12 23:19:01 +0000 | [diff] [blame] | 3550 | } |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 3551 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3552 | return TInfo; |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 3553 | } |
| 3554 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3555 | /// \brief Create a LocInfoType to hold the given QualType and TypeSourceInfo. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3556 | ParsedType Sema::CreateParsedType(QualType T, TypeSourceInfo *TInfo) { |
Argyrios Kyrtzidis | 1bb8a45 | 2009-08-19 01:28:17 +0000 | [diff] [blame] | 3557 | // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser |
| 3558 | // and Sema during declaration parsing. Try deallocating/caching them when |
| 3559 | // it's appropriate, instead of allocating them and keeping them around. |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 3560 | LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType), |
Douglas Gregor | eb0eb49 | 2010-12-10 08:12:03 +0000 | [diff] [blame] | 3561 | TypeAlignment); |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3562 | new (LocT) LocInfoType(T, TInfo); |
Argyrios Kyrtzidis | 1bb8a45 | 2009-08-19 01:28:17 +0000 | [diff] [blame] | 3563 | assert(LocT->getTypeClass() != T->getTypeClass() && |
| 3564 | "LocInfoType's TypeClass conflicts with an existing Type class"); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3565 | return ParsedType::make(QualType(LocT, 0)); |
Argyrios Kyrtzidis | 1bb8a45 | 2009-08-19 01:28:17 +0000 | [diff] [blame] | 3566 | } |
| 3567 | |
| 3568 | void LocInfoType::getAsStringInternal(std::string &Str, |
| 3569 | const PrintingPolicy &Policy) const { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3570 | llvm_unreachable("LocInfoType leaked into the type system; an opaque TypeTy*" |
Argyrios Kyrtzidis | 35d44e5 | 2009-08-19 01:46:06 +0000 | [diff] [blame] | 3571 | " was used directly instead of getting the QualType through" |
| 3572 | " GetTypeFromParser"); |
Argyrios Kyrtzidis | 1bb8a45 | 2009-08-19 01:28:17 +0000 | [diff] [blame] | 3573 | } |
| 3574 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3575 | TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3576 | // C99 6.7.6: Type names have no identifier. This is already validated by |
| 3577 | // the parser. |
| 3578 | assert(D.getIdentifier() == 0 && "Type name should have no identifier!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3579 | |
Argyrios Kyrtzidis | d3880f8 | 2011-06-28 03:01:18 +0000 | [diff] [blame] | 3580 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 3581 | QualType T = TInfo->getType(); |
Chris Lattner | 5153ee6 | 2009-04-25 08:47:54 +0000 | [diff] [blame] | 3582 | if (D.isInvalidType()) |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 3583 | return true; |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 3584 | |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 3585 | // Make sure there are no unused decl attributes on the declarator. |
John McCall | cdda47f | 2011-10-01 09:56:14 +0000 | [diff] [blame] | 3586 | // We don't want to do this for ObjC parameters because we're going |
| 3587 | // to apply them to the actual parameter declaration. |
Richard Smith | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 3588 | // Likewise, we don't want to do this for alias declarations, because |
| 3589 | // we are actually going to build a declaration from this eventually. |
| 3590 | if (D.getContext() != Declarator::ObjCParameterContext && |
| 3591 | D.getContext() != Declarator::AliasDeclContext && |
| 3592 | D.getContext() != Declarator::AliasTemplateContext) |
John McCall | cdda47f | 2011-10-01 09:56:14 +0000 | [diff] [blame] | 3593 | checkUnusedDeclAttributes(D); |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 3594 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3595 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 3596 | // Check that there are no default arguments (C++ only). |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 3597 | CheckExtraCXXDefaultArguments(D); |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 3598 | } |
| 3599 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3600 | return CreateParsedType(T, TInfo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3601 | } |
| 3602 | |
Douglas Gregor | e97179c | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 3603 | ParsedType Sema::ActOnObjCInstanceType(SourceLocation Loc) { |
| 3604 | QualType T = Context.getObjCInstanceType(); |
| 3605 | TypeSourceInfo *TInfo = Context.getTrivialTypeSourceInfo(T, Loc); |
| 3606 | return CreateParsedType(T, TInfo); |
| 3607 | } |
| 3608 | |
| 3609 | |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 3610 | //===----------------------------------------------------------------------===// |
| 3611 | // Type Attribute Processing |
| 3612 | //===----------------------------------------------------------------------===// |
| 3613 | |
| 3614 | /// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the |
| 3615 | /// specified type. The attribute contains 1 argument, the id of the address |
| 3616 | /// space for the type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3617 | static void HandleAddressSpaceTypeAttribute(QualType &Type, |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 3618 | const AttributeList &Attr, Sema &S){ |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3619 | |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 3620 | // If this type is already address space qualified, reject it. |
Peter Collingbourne | 29e3ef8 | 2011-07-27 20:29:53 +0000 | [diff] [blame] | 3621 | // ISO/IEC TR 18037 S5.3 (amending C99 6.7.3): "No type shall be qualified by |
| 3622 | // qualifiers for two or more different address spaces." |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 3623 | if (Type.getAddressSpace()) { |
| 3624 | S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3625 | Attr.setInvalid(); |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 3626 | return; |
| 3627 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3628 | |
Peter Collingbourne | 020972d | 2011-07-27 20:30:05 +0000 | [diff] [blame] | 3629 | // ISO/IEC TR 18037 S5.3 (amending C99 6.7.3): "A function type shall not be |
| 3630 | // qualified by an address-space qualifier." |
| 3631 | if (Type->isFunctionType()) { |
| 3632 | S.Diag(Attr.getLoc(), diag::err_attribute_address_function_type); |
| 3633 | Attr.setInvalid(); |
| 3634 | return; |
| 3635 | } |
| 3636 | |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 3637 | // Check the attribute arguments. |
| 3638 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 3639 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3640 | Attr.setInvalid(); |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 3641 | return; |
| 3642 | } |
| 3643 | Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0)); |
| 3644 | llvm::APSInt addrSpace(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 3645 | if (ASArgExpr->isTypeDependent() || ASArgExpr->isValueDependent() || |
| 3646 | !ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) { |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 3647 | S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int) |
| 3648 | << ASArgExpr->getSourceRange(); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3649 | Attr.setInvalid(); |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 3650 | return; |
| 3651 | } |
| 3652 | |
John McCall | efadb77 | 2009-07-28 06:52:18 +0000 | [diff] [blame] | 3653 | // Bounds checking. |
| 3654 | if (addrSpace.isSigned()) { |
| 3655 | if (addrSpace.isNegative()) { |
| 3656 | S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative) |
| 3657 | << ASArgExpr->getSourceRange(); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3658 | Attr.setInvalid(); |
John McCall | efadb77 | 2009-07-28 06:52:18 +0000 | [diff] [blame] | 3659 | return; |
| 3660 | } |
| 3661 | addrSpace.setIsSigned(false); |
| 3662 | } |
| 3663 | llvm::APSInt max(addrSpace.getBitWidth()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3664 | max = Qualifiers::MaxAddressSpace; |
John McCall | efadb77 | 2009-07-28 06:52:18 +0000 | [diff] [blame] | 3665 | if (addrSpace > max) { |
| 3666 | S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3667 | << Qualifiers::MaxAddressSpace << ASArgExpr->getSourceRange(); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3668 | Attr.setInvalid(); |
John McCall | efadb77 | 2009-07-28 06:52:18 +0000 | [diff] [blame] | 3669 | return; |
| 3670 | } |
| 3671 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3672 | unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue()); |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 3673 | Type = S.Context.getAddrSpaceQualType(Type, ASIdx); |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 3674 | } |
| 3675 | |
John McCall | d85bf9d | 2012-02-08 00:46:41 +0000 | [diff] [blame] | 3676 | /// Does this type have a "direct" ownership qualifier? That is, |
| 3677 | /// is it written like "__strong id", as opposed to something like |
| 3678 | /// "typeof(foo)", where that happens to be strong? |
| 3679 | static bool hasDirectOwnershipQualifier(QualType type) { |
| 3680 | // Fast path: no qualifier at all. |
| 3681 | assert(type.getQualifiers().hasObjCLifetime()); |
| 3682 | |
| 3683 | while (true) { |
| 3684 | // __strong id |
| 3685 | if (const AttributedType *attr = dyn_cast<AttributedType>(type)) { |
| 3686 | if (attr->getAttrKind() == AttributedType::attr_objc_ownership) |
| 3687 | return true; |
| 3688 | |
| 3689 | type = attr->getModifiedType(); |
| 3690 | |
| 3691 | // X *__strong (...) |
| 3692 | } else if (const ParenType *paren = dyn_cast<ParenType>(type)) { |
| 3693 | type = paren->getInnerType(); |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 3694 | |
John McCall | d85bf9d | 2012-02-08 00:46:41 +0000 | [diff] [blame] | 3695 | // That's it for things we want to complain about. In particular, |
| 3696 | // we do not want to look through typedefs, typeof(expr), |
| 3697 | // typeof(type), or any other way that the type is somehow |
| 3698 | // abstracted. |
| 3699 | } else { |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 3700 | |
John McCall | d85bf9d | 2012-02-08 00:46:41 +0000 | [diff] [blame] | 3701 | return false; |
| 3702 | } |
| 3703 | } |
| 3704 | } |
| 3705 | |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 3706 | /// handleObjCOwnershipTypeAttr - Process an objc_ownership |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3707 | /// attribute on the specified type. |
| 3708 | /// |
| 3709 | /// Returns 'true' if the attribute was handled. |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 3710 | static bool handleObjCOwnershipTypeAttr(TypeProcessingState &state, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3711 | AttributeList &attr, |
| 3712 | QualType &type) { |
Argyrios Kyrtzidis | e71202e | 2011-11-04 20:37:24 +0000 | [diff] [blame] | 3713 | bool NonObjCPointer = false; |
| 3714 | |
| 3715 | if (!type->isDependentType()) { |
| 3716 | if (const PointerType *ptr = type->getAs<PointerType>()) { |
| 3717 | QualType pointee = ptr->getPointeeType(); |
| 3718 | if (pointee->isObjCRetainableType() || pointee->isPointerType()) |
| 3719 | return false; |
| 3720 | // It is important not to lose the source info that there was an attribute |
| 3721 | // applied to non-objc pointer. We will create an attributed type but |
| 3722 | // its type will be the same as the original type. |
| 3723 | NonObjCPointer = true; |
| 3724 | } else if (!type->isObjCRetainableType()) { |
| 3725 | return false; |
| 3726 | } |
John McCall | b2381b1 | 2013-03-01 07:58:16 +0000 | [diff] [blame] | 3727 | |
| 3728 | // Don't accept an ownership attribute in the declspec if it would |
| 3729 | // just be the return type of a block pointer. |
| 3730 | if (state.isProcessingDeclSpec()) { |
| 3731 | Declarator &D = state.getDeclarator(); |
| 3732 | if (maybeMovePastReturnType(D, D.getNumTypeObjects())) |
| 3733 | return false; |
| 3734 | } |
Argyrios Kyrtzidis | e71202e | 2011-11-04 20:37:24 +0000 | [diff] [blame] | 3735 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3736 | |
| 3737 | Sema &S = state.getSema(); |
Argyrios Kyrtzidis | 440ec2e | 2011-09-28 18:35:06 +0000 | [diff] [blame] | 3738 | SourceLocation AttrLoc = attr.getLoc(); |
| 3739 | if (AttrLoc.isMacroID()) |
| 3740 | AttrLoc = S.getSourceManager().getImmediateExpansionRange(AttrLoc).first; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3741 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3742 | if (!attr.getParameterName()) { |
Argyrios Kyrtzidis | 440ec2e | 2011-09-28 18:35:06 +0000 | [diff] [blame] | 3743 | S.Diag(AttrLoc, diag::err_attribute_argument_n_not_string) |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 3744 | << "objc_ownership" << 1; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3745 | attr.setInvalid(); |
| 3746 | return true; |
| 3747 | } |
| 3748 | |
John McCall | d85bf9d | 2012-02-08 00:46:41 +0000 | [diff] [blame] | 3749 | // Consume lifetime attributes without further comment outside of |
| 3750 | // ARC mode. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3751 | if (!S.getLangOpts().ObjCAutoRefCount) |
John McCall | d85bf9d | 2012-02-08 00:46:41 +0000 | [diff] [blame] | 3752 | return true; |
| 3753 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3754 | Qualifiers::ObjCLifetime lifetime; |
| 3755 | if (attr.getParameterName()->isStr("none")) |
| 3756 | lifetime = Qualifiers::OCL_ExplicitNone; |
| 3757 | else if (attr.getParameterName()->isStr("strong")) |
| 3758 | lifetime = Qualifiers::OCL_Strong; |
| 3759 | else if (attr.getParameterName()->isStr("weak")) |
| 3760 | lifetime = Qualifiers::OCL_Weak; |
| 3761 | else if (attr.getParameterName()->isStr("autoreleasing")) |
| 3762 | lifetime = Qualifiers::OCL_Autoreleasing; |
| 3763 | else { |
Argyrios Kyrtzidis | 440ec2e | 2011-09-28 18:35:06 +0000 | [diff] [blame] | 3764 | S.Diag(AttrLoc, diag::warn_attribute_type_not_supported) |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 3765 | << "objc_ownership" << attr.getParameterName(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3766 | attr.setInvalid(); |
| 3767 | return true; |
| 3768 | } |
| 3769 | |
John McCall | d85bf9d | 2012-02-08 00:46:41 +0000 | [diff] [blame] | 3770 | SplitQualType underlyingType = type.split(); |
| 3771 | |
| 3772 | // Check for redundant/conflicting ownership qualifiers. |
| 3773 | if (Qualifiers::ObjCLifetime previousLifetime |
| 3774 | = type.getQualifiers().getObjCLifetime()) { |
| 3775 | // If it's written directly, that's an error. |
| 3776 | if (hasDirectOwnershipQualifier(type)) { |
| 3777 | S.Diag(AttrLoc, diag::err_attr_objc_ownership_redundant) |
| 3778 | << type; |
| 3779 | return true; |
| 3780 | } |
| 3781 | |
| 3782 | // Otherwise, if the qualifiers actually conflict, pull sugar off |
| 3783 | // until we reach a type that is directly qualified. |
| 3784 | if (previousLifetime != lifetime) { |
| 3785 | // This should always terminate: the canonical type is |
| 3786 | // qualified, so some bit of sugar must be hiding it. |
| 3787 | while (!underlyingType.Quals.hasObjCLifetime()) { |
| 3788 | underlyingType = underlyingType.getSingleStepDesugaredType(); |
| 3789 | } |
| 3790 | underlyingType.Quals.removeObjCLifetime(); |
| 3791 | } |
| 3792 | } |
| 3793 | |
| 3794 | underlyingType.Quals.addObjCLifetime(lifetime); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3795 | |
Argyrios Kyrtzidis | e71202e | 2011-11-04 20:37:24 +0000 | [diff] [blame] | 3796 | if (NonObjCPointer) { |
| 3797 | StringRef name = attr.getName()->getName(); |
| 3798 | switch (lifetime) { |
| 3799 | case Qualifiers::OCL_None: |
| 3800 | case Qualifiers::OCL_ExplicitNone: |
| 3801 | break; |
| 3802 | case Qualifiers::OCL_Strong: name = "__strong"; break; |
| 3803 | case Qualifiers::OCL_Weak: name = "__weak"; break; |
| 3804 | case Qualifiers::OCL_Autoreleasing: name = "__autoreleasing"; break; |
| 3805 | } |
| 3806 | S.Diag(AttrLoc, diag::warn_objc_object_attribute_wrong_type) |
| 3807 | << name << type; |
| 3808 | } |
| 3809 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3810 | QualType origType = type; |
Argyrios Kyrtzidis | e71202e | 2011-11-04 20:37:24 +0000 | [diff] [blame] | 3811 | if (!NonObjCPointer) |
John McCall | d85bf9d | 2012-02-08 00:46:41 +0000 | [diff] [blame] | 3812 | type = S.Context.getQualifiedType(underlyingType); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3813 | |
| 3814 | // If we have a valid source location for the attribute, use an |
| 3815 | // AttributedType instead. |
Argyrios Kyrtzidis | 440ec2e | 2011-09-28 18:35:06 +0000 | [diff] [blame] | 3816 | if (AttrLoc.isValid()) |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 3817 | type = S.Context.getAttributedType(AttributedType::attr_objc_ownership, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3818 | origType, type); |
| 3819 | |
John McCall | 9f084a3 | 2011-07-06 00:26:06 +0000 | [diff] [blame] | 3820 | // Forbid __weak if the runtime doesn't support it. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3821 | if (lifetime == Qualifiers::OCL_Weak && |
John McCall | 0a7dd78 | 2012-08-21 02:47:43 +0000 | [diff] [blame] | 3822 | !S.getLangOpts().ObjCARCWeak && !NonObjCPointer) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3823 | |
| 3824 | // Actually, delay this until we know what we're parsing. |
| 3825 | if (S.DelayedDiagnostics.shouldDelayDiagnostics()) { |
| 3826 | S.DelayedDiagnostics.add( |
Argyrios Kyrtzidis | 440ec2e | 2011-09-28 18:35:06 +0000 | [diff] [blame] | 3827 | sema::DelayedDiagnostic::makeForbiddenType( |
| 3828 | S.getSourceManager().getExpansionLoc(AttrLoc), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3829 | diag::err_arc_weak_no_runtime, type, /*ignored*/ 0)); |
| 3830 | } else { |
Argyrios Kyrtzidis | 440ec2e | 2011-09-28 18:35:06 +0000 | [diff] [blame] | 3831 | S.Diag(AttrLoc, diag::err_arc_weak_no_runtime); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3832 | } |
| 3833 | |
| 3834 | attr.setInvalid(); |
| 3835 | return true; |
| 3836 | } |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 3837 | |
| 3838 | // Forbid __weak for class objects marked as |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 3839 | // objc_arc_weak_reference_unavailable |
| 3840 | if (lifetime == Qualifiers::OCL_Weak) { |
John McCall | b2381b1 | 2013-03-01 07:58:16 +0000 | [diff] [blame] | 3841 | if (const ObjCObjectPointerType *ObjT = |
| 3842 | type->getAs<ObjCObjectPointerType>()) { |
Richard Smith | 4e90bc3 | 2012-08-23 04:53:18 +0000 | [diff] [blame] | 3843 | if (ObjCInterfaceDecl *Class = ObjT->getInterfaceDecl()) { |
| 3844 | if (Class->isArcWeakrefUnavailable()) { |
| 3845 | S.Diag(AttrLoc, diag::err_arc_unsupported_weak_class); |
| 3846 | S.Diag(ObjT->getInterfaceDecl()->getLocation(), |
| 3847 | diag::note_class_declared); |
| 3848 | } |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 3849 | } |
| 3850 | } |
| 3851 | } |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 3852 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3853 | return true; |
| 3854 | } |
| 3855 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3856 | /// handleObjCGCTypeAttr - Process the __attribute__((objc_gc)) type |
| 3857 | /// attribute on the specified type. Returns true to indicate that |
| 3858 | /// the attribute was handled, false to indicate that the type does |
| 3859 | /// not permit the attribute. |
| 3860 | static bool handleObjCGCTypeAttr(TypeProcessingState &state, |
| 3861 | AttributeList &attr, |
| 3862 | QualType &type) { |
| 3863 | Sema &S = state.getSema(); |
| 3864 | |
| 3865 | // Delay if this isn't some kind of pointer. |
| 3866 | if (!type->isPointerType() && |
| 3867 | !type->isObjCObjectPointerType() && |
| 3868 | !type->isBlockPointerType()) |
| 3869 | return false; |
| 3870 | |
| 3871 | if (type.getObjCGCAttr() != Qualifiers::GCNone) { |
| 3872 | S.Diag(attr.getLoc(), diag::err_attribute_multiple_objc_gc); |
| 3873 | attr.setInvalid(); |
| 3874 | return true; |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 3875 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3876 | |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 3877 | // Check the attribute arguments. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3878 | if (!attr.getParameterName()) { |
| 3879 | S.Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Fariborz Jahanian | ba372b8 | 2009-02-18 17:52:36 +0000 | [diff] [blame] | 3880 | << "objc_gc" << 1; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3881 | attr.setInvalid(); |
| 3882 | return true; |
Fariborz Jahanian | ba372b8 | 2009-02-18 17:52:36 +0000 | [diff] [blame] | 3883 | } |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3884 | Qualifiers::GC GCAttr; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3885 | if (attr.getNumArgs() != 0) { |
| 3886 | S.Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 3887 | attr.setInvalid(); |
| 3888 | return true; |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 3889 | } |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3890 | if (attr.getParameterName()->isStr("weak")) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3891 | GCAttr = Qualifiers::Weak; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3892 | else if (attr.getParameterName()->isStr("strong")) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3893 | GCAttr = Qualifiers::Strong; |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 3894 | else { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3895 | S.Diag(attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 3896 | << "objc_gc" << attr.getParameterName(); |
| 3897 | attr.setInvalid(); |
| 3898 | return true; |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 3899 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3900 | |
John McCall | 14aa217 | 2011-03-04 04:00:19 +0000 | [diff] [blame] | 3901 | QualType origType = type; |
| 3902 | type = S.Context.getObjCGCQualType(origType, GCAttr); |
| 3903 | |
| 3904 | // Make an attributed type to preserve the source information. |
| 3905 | if (attr.getLoc().isValid()) |
| 3906 | type = S.Context.getAttributedType(AttributedType::attr_objc_gc, |
| 3907 | origType, type); |
| 3908 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3909 | return true; |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 3910 | } |
| 3911 | |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 3912 | namespace { |
| 3913 | /// A helper class to unwrap a type down to a function for the |
| 3914 | /// purposes of applying attributes there. |
| 3915 | /// |
| 3916 | /// Use: |
| 3917 | /// FunctionTypeUnwrapper unwrapped(SemaRef, T); |
| 3918 | /// if (unwrapped.isFunctionType()) { |
| 3919 | /// const FunctionType *fn = unwrapped.get(); |
| 3920 | /// // change fn somehow |
| 3921 | /// T = unwrapped.wrap(fn); |
| 3922 | /// } |
| 3923 | struct FunctionTypeUnwrapper { |
| 3924 | enum WrapKind { |
| 3925 | Desugar, |
| 3926 | Parens, |
| 3927 | Pointer, |
| 3928 | BlockPointer, |
| 3929 | Reference, |
| 3930 | MemberPointer |
| 3931 | }; |
| 3932 | |
| 3933 | QualType Original; |
| 3934 | const FunctionType *Fn; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3935 | SmallVector<unsigned char /*WrapKind*/, 8> Stack; |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 3936 | |
| 3937 | FunctionTypeUnwrapper(Sema &S, QualType T) : Original(T) { |
| 3938 | while (true) { |
| 3939 | const Type *Ty = T.getTypePtr(); |
| 3940 | if (isa<FunctionType>(Ty)) { |
| 3941 | Fn = cast<FunctionType>(Ty); |
| 3942 | return; |
| 3943 | } else if (isa<ParenType>(Ty)) { |
| 3944 | T = cast<ParenType>(Ty)->getInnerType(); |
| 3945 | Stack.push_back(Parens); |
| 3946 | } else if (isa<PointerType>(Ty)) { |
| 3947 | T = cast<PointerType>(Ty)->getPointeeType(); |
| 3948 | Stack.push_back(Pointer); |
| 3949 | } else if (isa<BlockPointerType>(Ty)) { |
| 3950 | T = cast<BlockPointerType>(Ty)->getPointeeType(); |
| 3951 | Stack.push_back(BlockPointer); |
| 3952 | } else if (isa<MemberPointerType>(Ty)) { |
| 3953 | T = cast<MemberPointerType>(Ty)->getPointeeType(); |
| 3954 | Stack.push_back(MemberPointer); |
| 3955 | } else if (isa<ReferenceType>(Ty)) { |
| 3956 | T = cast<ReferenceType>(Ty)->getPointeeType(); |
| 3957 | Stack.push_back(Reference); |
| 3958 | } else { |
| 3959 | const Type *DTy = Ty->getUnqualifiedDesugaredType(); |
| 3960 | if (Ty == DTy) { |
| 3961 | Fn = 0; |
| 3962 | return; |
| 3963 | } |
| 3964 | |
| 3965 | T = QualType(DTy, 0); |
| 3966 | Stack.push_back(Desugar); |
| 3967 | } |
| 3968 | } |
| 3969 | } |
| 3970 | |
| 3971 | bool isFunctionType() const { return (Fn != 0); } |
| 3972 | const FunctionType *get() const { return Fn; } |
| 3973 | |
| 3974 | QualType wrap(Sema &S, const FunctionType *New) { |
| 3975 | // If T wasn't modified from the unwrapped type, do nothing. |
| 3976 | if (New == get()) return Original; |
| 3977 | |
| 3978 | Fn = New; |
| 3979 | return wrap(S.Context, Original, 0); |
| 3980 | } |
| 3981 | |
| 3982 | private: |
| 3983 | QualType wrap(ASTContext &C, QualType Old, unsigned I) { |
| 3984 | if (I == Stack.size()) |
| 3985 | return C.getQualifiedType(Fn, Old.getQualifiers()); |
| 3986 | |
| 3987 | // Build up the inner type, applying the qualifiers from the old |
| 3988 | // type to the new type. |
| 3989 | SplitQualType SplitOld = Old.split(); |
| 3990 | |
| 3991 | // As a special case, tail-recurse if there are no qualifiers. |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3992 | if (SplitOld.Quals.empty()) |
| 3993 | return wrap(C, SplitOld.Ty, I); |
| 3994 | return C.getQualifiedType(wrap(C, SplitOld.Ty, I), SplitOld.Quals); |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 3995 | } |
| 3996 | |
| 3997 | QualType wrap(ASTContext &C, const Type *Old, unsigned I) { |
| 3998 | if (I == Stack.size()) return QualType(Fn, 0); |
| 3999 | |
| 4000 | switch (static_cast<WrapKind>(Stack[I++])) { |
| 4001 | case Desugar: |
| 4002 | // This is the point at which we potentially lose source |
| 4003 | // information. |
| 4004 | return wrap(C, Old->getUnqualifiedDesugaredType(), I); |
| 4005 | |
| 4006 | case Parens: { |
| 4007 | QualType New = wrap(C, cast<ParenType>(Old)->getInnerType(), I); |
| 4008 | return C.getParenType(New); |
| 4009 | } |
| 4010 | |
| 4011 | case Pointer: { |
| 4012 | QualType New = wrap(C, cast<PointerType>(Old)->getPointeeType(), I); |
| 4013 | return C.getPointerType(New); |
| 4014 | } |
| 4015 | |
| 4016 | case BlockPointer: { |
| 4017 | QualType New = wrap(C, cast<BlockPointerType>(Old)->getPointeeType(),I); |
| 4018 | return C.getBlockPointerType(New); |
| 4019 | } |
| 4020 | |
| 4021 | case MemberPointer: { |
| 4022 | const MemberPointerType *OldMPT = cast<MemberPointerType>(Old); |
| 4023 | QualType New = wrap(C, OldMPT->getPointeeType(), I); |
| 4024 | return C.getMemberPointerType(New, OldMPT->getClass()); |
| 4025 | } |
| 4026 | |
| 4027 | case Reference: { |
| 4028 | const ReferenceType *OldRef = cast<ReferenceType>(Old); |
| 4029 | QualType New = wrap(C, OldRef->getPointeeType(), I); |
| 4030 | if (isa<LValueReferenceType>(OldRef)) |
| 4031 | return C.getLValueReferenceType(New, OldRef->isSpelledAsLValue()); |
| 4032 | else |
| 4033 | return C.getRValueReferenceType(New); |
| 4034 | } |
| 4035 | } |
| 4036 | |
| 4037 | llvm_unreachable("unknown wrapping kind"); |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 4038 | } |
| 4039 | }; |
| 4040 | } |
| 4041 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4042 | /// Process an individual function attribute. Returns true to |
| 4043 | /// indicate that the attribute was handled, false if it wasn't. |
| 4044 | static bool handleFunctionTypeAttr(TypeProcessingState &state, |
| 4045 | AttributeList &attr, |
| 4046 | QualType &type) { |
| 4047 | Sema &S = state.getSema(); |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 4048 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4049 | FunctionTypeUnwrapper unwrapped(S, type); |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 4050 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4051 | if (attr.getKind() == AttributeList::AT_NoReturn) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4052 | if (S.CheckNoReturnAttr(attr)) |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4053 | return true; |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 4054 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4055 | // Delay if this is not a function type. |
| 4056 | if (!unwrapped.isFunctionType()) |
| 4057 | return false; |
| 4058 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4059 | // Otherwise we can process right away. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4060 | FunctionType::ExtInfo EI = unwrapped.get()->getExtInfo().withNoReturn(true); |
| 4061 | type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI)); |
| 4062 | return true; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4063 | } |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 4064 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4065 | // ns_returns_retained is not always a type attribute, but if we got |
| 4066 | // here, we're treating it as one right now. |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4067 | if (attr.getKind() == AttributeList::AT_NSReturnsRetained) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4068 | assert(S.getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4069 | "ns_returns_retained treated as type attribute in non-ARC"); |
| 4070 | if (attr.getNumArgs()) return true; |
| 4071 | |
| 4072 | // Delay if this is not a function type. |
| 4073 | if (!unwrapped.isFunctionType()) |
| 4074 | return false; |
| 4075 | |
| 4076 | FunctionType::ExtInfo EI |
| 4077 | = unwrapped.get()->getExtInfo().withProducesResult(true); |
| 4078 | type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI)); |
| 4079 | return true; |
| 4080 | } |
| 4081 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4082 | if (attr.getKind() == AttributeList::AT_Regparm) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4083 | unsigned value; |
| 4084 | if (S.CheckRegparmAttr(attr, value)) |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 4085 | return true; |
| 4086 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4087 | // Delay if this is not a function type. |
| 4088 | if (!unwrapped.isFunctionType()) |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 4089 | return false; |
| 4090 | |
Argyrios Kyrtzidis | ce95566 | 2011-01-25 23:16:40 +0000 | [diff] [blame] | 4091 | // Diagnose regparm with fastcall. |
| 4092 | const FunctionType *fn = unwrapped.get(); |
| 4093 | CallingConv CC = fn->getCallConv(); |
| 4094 | if (CC == CC_X86FastCall) { |
| 4095 | S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 4096 | << FunctionType::getNameForCallConv(CC) |
| 4097 | << "regparm"; |
| 4098 | attr.setInvalid(); |
| 4099 | return true; |
| 4100 | } |
| 4101 | |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 4102 | FunctionType::ExtInfo EI = |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4103 | unwrapped.get()->getExtInfo().withRegParm(value); |
| 4104 | type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI)); |
| 4105 | return true; |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 4106 | } |
| 4107 | |
Aaron Ballman | 82bfa19 | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 4108 | // Delay if the type didn't work out to a function. |
| 4109 | if (!unwrapped.isFunctionType()) return false; |
| 4110 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4111 | // Otherwise, a calling convention. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4112 | CallingConv CC; |
| 4113 | if (S.CheckCallingConvAttr(attr, CC)) |
| 4114 | return true; |
John McCall | f82b4e8 | 2010-02-04 05:44:44 +0000 | [diff] [blame] | 4115 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4116 | const FunctionType *fn = unwrapped.get(); |
| 4117 | CallingConv CCOld = fn->getCallConv(); |
Charles Davis | 064f7db | 2010-02-23 06:13:55 +0000 | [diff] [blame] | 4118 | if (S.Context.getCanonicalCallConv(CC) == |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4119 | S.Context.getCanonicalCallConv(CCOld)) { |
Argyrios Kyrtzidis | ce95566 | 2011-01-25 23:16:40 +0000 | [diff] [blame] | 4120 | FunctionType::ExtInfo EI= unwrapped.get()->getExtInfo().withCallingConv(CC); |
| 4121 | type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI)); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4122 | return true; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4123 | } |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4124 | |
Roman Divacky | 8e68f1c | 2011-08-05 16:37:22 +0000 | [diff] [blame] | 4125 | if (CCOld != (S.LangOpts.MRTD ? CC_X86StdCall : CC_Default)) { |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4126 | // Should we diagnose reapplications of the same convention? |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4127 | S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible) |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4128 | << FunctionType::getNameForCallConv(CC) |
| 4129 | << FunctionType::getNameForCallConv(CCOld); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4130 | attr.setInvalid(); |
| 4131 | return true; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4132 | } |
| 4133 | |
| 4134 | // Diagnose the use of X86 fastcall on varargs or unprototyped functions. |
| 4135 | if (CC == CC_X86FastCall) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4136 | if (isa<FunctionNoProtoType>(fn)) { |
| 4137 | S.Diag(attr.getLoc(), diag::err_cconv_knr) |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4138 | << FunctionType::getNameForCallConv(CC); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4139 | attr.setInvalid(); |
| 4140 | return true; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4141 | } |
| 4142 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4143 | const FunctionProtoType *FnP = cast<FunctionProtoType>(fn); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4144 | if (FnP->isVariadic()) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4145 | S.Diag(attr.getLoc(), diag::err_cconv_varargs) |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4146 | << FunctionType::getNameForCallConv(CC); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4147 | attr.setInvalid(); |
| 4148 | return true; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4149 | } |
Argyrios Kyrtzidis | ce95566 | 2011-01-25 23:16:40 +0000 | [diff] [blame] | 4150 | |
| 4151 | // Also diagnose fastcall with regparm. |
Eli Friedman | a49218e | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 4152 | if (fn->getHasRegParm()) { |
Argyrios Kyrtzidis | ce95566 | 2011-01-25 23:16:40 +0000 | [diff] [blame] | 4153 | S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 4154 | << "regparm" |
| 4155 | << FunctionType::getNameForCallConv(CC); |
| 4156 | attr.setInvalid(); |
| 4157 | return true; |
| 4158 | } |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4159 | } |
| 4160 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4161 | FunctionType::ExtInfo EI = unwrapped.get()->getExtInfo().withCallingConv(CC); |
| 4162 | type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI)); |
| 4163 | return true; |
John McCall | f82b4e8 | 2010-02-04 05:44:44 +0000 | [diff] [blame] | 4164 | } |
| 4165 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4166 | /// Handle OpenCL image access qualifiers: read_only, write_only, read_write |
| 4167 | static void HandleOpenCLImageAccessAttribute(QualType& CurType, |
| 4168 | const AttributeList &Attr, |
| 4169 | Sema &S) { |
| 4170 | // Check the attribute arguments. |
| 4171 | if (Attr.getNumArgs() != 1) { |
| 4172 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 4173 | Attr.setInvalid(); |
| 4174 | return; |
| 4175 | } |
| 4176 | Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0)); |
| 4177 | llvm::APSInt arg(32); |
| 4178 | if (sizeExpr->isTypeDependent() || sizeExpr->isValueDependent() || |
| 4179 | !sizeExpr->isIntegerConstantExpr(arg, S.Context)) { |
| 4180 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 4181 | << "opencl_image_access" << sizeExpr->getSourceRange(); |
| 4182 | Attr.setInvalid(); |
| 4183 | return; |
| 4184 | } |
| 4185 | unsigned iarg = static_cast<unsigned>(arg.getZExtValue()); |
| 4186 | switch (iarg) { |
| 4187 | case CLIA_read_only: |
| 4188 | case CLIA_write_only: |
| 4189 | case CLIA_read_write: |
| 4190 | // Implemented in a separate patch |
| 4191 | break; |
| 4192 | default: |
| 4193 | // Implemented in a separate patch |
| 4194 | S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size) |
| 4195 | << sizeExpr->getSourceRange(); |
| 4196 | Attr.setInvalid(); |
| 4197 | break; |
| 4198 | } |
| 4199 | } |
| 4200 | |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 4201 | /// HandleVectorSizeAttribute - this attribute is only applicable to integral |
| 4202 | /// and float scalars, although arrays, pointers, and function return values are |
| 4203 | /// allowed in conjunction with this construct. Aggregates with this attribute |
| 4204 | /// are invalid, even if they are of the same size as a corresponding scalar. |
| 4205 | /// The raw attribute should contain precisely 1 argument, the vector size for |
| 4206 | /// the variable, measured in bytes. If curType and rawAttr are well formed, |
| 4207 | /// this routine will return a new vector type. |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 4208 | static void HandleVectorSizeAttr(QualType& CurType, const AttributeList &Attr, |
| 4209 | Sema &S) { |
Bob Wilson | 56affbc | 2010-11-16 00:32:16 +0000 | [diff] [blame] | 4210 | // Check the attribute arguments. |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 4211 | if (Attr.getNumArgs() != 1) { |
| 4212 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4213 | Attr.setInvalid(); |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 4214 | return; |
| 4215 | } |
| 4216 | Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0)); |
| 4217 | llvm::APSInt vecSize(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 4218 | if (sizeExpr->isTypeDependent() || sizeExpr->isValueDependent() || |
| 4219 | !sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) { |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 4220 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 4221 | << "vector_size" << sizeExpr->getSourceRange(); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4222 | Attr.setInvalid(); |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 4223 | return; |
| 4224 | } |
| 4225 | // the base type must be integer or float, and can't already be a vector. |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 4226 | if (!CurType->isIntegerType() && !CurType->isRealFloatingType()) { |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 4227 | S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4228 | Attr.setInvalid(); |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 4229 | return; |
| 4230 | } |
| 4231 | unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType)); |
| 4232 | // vecSize is specified in bytes - convert to bits. |
| 4233 | unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8); |
| 4234 | |
| 4235 | // the vector size needs to be an integral multiple of the type size. |
| 4236 | if (vectorSize % typeSize) { |
| 4237 | S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size) |
| 4238 | << sizeExpr->getSourceRange(); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4239 | Attr.setInvalid(); |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 4240 | return; |
| 4241 | } |
| 4242 | if (vectorSize == 0) { |
| 4243 | S.Diag(Attr.getLoc(), diag::err_attribute_zero_size) |
| 4244 | << sizeExpr->getSourceRange(); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4245 | Attr.setInvalid(); |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 4246 | return; |
| 4247 | } |
| 4248 | |
| 4249 | // Success! Instantiate the vector type, the number of elements is > 0, and |
| 4250 | // not required to be a power of 2, unlike GCC. |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 4251 | CurType = S.Context.getVectorType(CurType, vectorSize/typeSize, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 4252 | VectorType::GenericVector); |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 4253 | } |
| 4254 | |
Douglas Gregor | 4ac0140 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 4255 | /// \brief Process the OpenCL-like ext_vector_type attribute when it occurs on |
| 4256 | /// a type. |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 4257 | static void HandleExtVectorTypeAttr(QualType &CurType, |
| 4258 | const AttributeList &Attr, |
Douglas Gregor | 4ac0140 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 4259 | Sema &S) { |
| 4260 | Expr *sizeExpr; |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 4261 | |
Douglas Gregor | 4ac0140 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 4262 | // Special case where the argument is a template id. |
| 4263 | if (Attr.getParameterName()) { |
| 4264 | CXXScopeSpec SS; |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4265 | SourceLocation TemplateKWLoc; |
Douglas Gregor | 4ac0140 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 4266 | UnqualifiedId id; |
| 4267 | id.setIdentifier(Attr.getParameterName(), Attr.getLoc()); |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4268 | |
| 4269 | ExprResult Size = S.ActOnIdExpression(S.getCurScope(), SS, TemplateKWLoc, |
| 4270 | id, false, false); |
Douglas Gregor | 4ac0140 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 4271 | if (Size.isInvalid()) |
| 4272 | return; |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 4273 | |
Douglas Gregor | 4ac0140 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 4274 | sizeExpr = Size.get(); |
| 4275 | } else { |
| 4276 | // check the attribute arguments. |
| 4277 | if (Attr.getNumArgs() != 1) { |
| 4278 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 4279 | return; |
| 4280 | } |
| 4281 | sizeExpr = Attr.getArg(0); |
| 4282 | } |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 4283 | |
Douglas Gregor | 4ac0140 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 4284 | // Create the vector type. |
| 4285 | QualType T = S.BuildExtVectorType(CurType, sizeExpr, Attr.getLoc()); |
| 4286 | if (!T.isNull()) |
| 4287 | CurType = T; |
| 4288 | } |
| 4289 | |
Bob Wilson | 4211bb6 | 2010-11-16 00:32:24 +0000 | [diff] [blame] | 4290 | /// HandleNeonVectorTypeAttr - The "neon_vector_type" and |
| 4291 | /// "neon_polyvector_type" attributes are used to create vector types that |
| 4292 | /// are mangled according to ARM's ABI. Otherwise, these types are identical |
| 4293 | /// to those created with the "vector_size" attribute. Unlike "vector_size" |
| 4294 | /// the argument to these Neon attributes is the number of vector elements, |
| 4295 | /// not the vector size in bytes. The vector width and element type must |
| 4296 | /// match one of the standard Neon vector types. |
| 4297 | static void HandleNeonVectorTypeAttr(QualType& CurType, |
| 4298 | const AttributeList &Attr, Sema &S, |
| 4299 | VectorType::VectorKind VecKind, |
| 4300 | const char *AttrName) { |
| 4301 | // Check the attribute arguments. |
| 4302 | if (Attr.getNumArgs() != 1) { |
| 4303 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 4304 | Attr.setInvalid(); |
| 4305 | return; |
| 4306 | } |
| 4307 | // The number of elements must be an ICE. |
| 4308 | Expr *numEltsExpr = static_cast<Expr *>(Attr.getArg(0)); |
| 4309 | llvm::APSInt numEltsInt(32); |
| 4310 | if (numEltsExpr->isTypeDependent() || numEltsExpr->isValueDependent() || |
| 4311 | !numEltsExpr->isIntegerConstantExpr(numEltsInt, S.Context)) { |
| 4312 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 4313 | << AttrName << numEltsExpr->getSourceRange(); |
| 4314 | Attr.setInvalid(); |
| 4315 | return; |
| 4316 | } |
| 4317 | // Only certain element types are supported for Neon vectors. |
| 4318 | const BuiltinType* BTy = CurType->getAs<BuiltinType>(); |
| 4319 | if (!BTy || |
| 4320 | (VecKind == VectorType::NeonPolyVector && |
| 4321 | BTy->getKind() != BuiltinType::SChar && |
| 4322 | BTy->getKind() != BuiltinType::Short) || |
| 4323 | (BTy->getKind() != BuiltinType::SChar && |
| 4324 | BTy->getKind() != BuiltinType::UChar && |
| 4325 | BTy->getKind() != BuiltinType::Short && |
| 4326 | BTy->getKind() != BuiltinType::UShort && |
| 4327 | BTy->getKind() != BuiltinType::Int && |
| 4328 | BTy->getKind() != BuiltinType::UInt && |
| 4329 | BTy->getKind() != BuiltinType::LongLong && |
| 4330 | BTy->getKind() != BuiltinType::ULongLong && |
| 4331 | BTy->getKind() != BuiltinType::Float)) { |
| 4332 | S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) <<CurType; |
| 4333 | Attr.setInvalid(); |
| 4334 | return; |
| 4335 | } |
| 4336 | // The total size of the vector must be 64 or 128 bits. |
| 4337 | unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType)); |
| 4338 | unsigned numElts = static_cast<unsigned>(numEltsInt.getZExtValue()); |
| 4339 | unsigned vecSize = typeSize * numElts; |
| 4340 | if (vecSize != 64 && vecSize != 128) { |
| 4341 | S.Diag(Attr.getLoc(), diag::err_attribute_bad_neon_vector_size) << CurType; |
| 4342 | Attr.setInvalid(); |
| 4343 | return; |
| 4344 | } |
| 4345 | |
| 4346 | CurType = S.Context.getVectorType(CurType, numElts, VecKind); |
| 4347 | } |
| 4348 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4349 | static void processTypeAttrs(TypeProcessingState &state, QualType &type, |
Richard Smith | f7a0527 | 2013-01-14 07:53:01 +0000 | [diff] [blame] | 4350 | TypeAttrLocation TAL, AttributeList *attrs) { |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 4351 | // Scan through and apply attributes to this type where it makes sense. Some |
| 4352 | // attributes (such as __address_space__, __vector_size__, etc) apply to the |
| 4353 | // type, but others can be present in the type specifiers even though they |
Chris Lattner | fca0ddd | 2008-06-26 06:27:57 +0000 | [diff] [blame] | 4354 | // apply to the decl. Here we apply type attributes and ignore the rest. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4355 | |
| 4356 | AttributeList *next; |
| 4357 | do { |
| 4358 | AttributeList &attr = *attrs; |
| 4359 | next = attr.getNext(); |
| 4360 | |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4361 | // Skip attributes that were marked to be invalid. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4362 | if (attr.isInvalid()) |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4363 | continue; |
| 4364 | |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4365 | if (attr.isCXX11Attribute()) { |
| 4366 | // [[gnu::...]] attributes are treated as declaration attributes, so may |
| 4367 | // not appertain to a DeclaratorChunk, even if we handle them as type |
| 4368 | // attributes. |
| 4369 | if (attr.getScopeName() && attr.getScopeName()->isStr("gnu")) { |
| 4370 | if (TAL == TAL_DeclChunk) { |
| 4371 | state.getSema().Diag(attr.getLoc(), |
| 4372 | diag::warn_cxx11_gnu_attribute_on_type) |
| 4373 | << attr.getName(); |
| 4374 | continue; |
| 4375 | } |
| 4376 | } else if (TAL != TAL_DeclChunk) { |
| 4377 | // Otherwise, only consider type processing for a C++11 attribute if |
| 4378 | // it's actually been applied to a type. |
| 4379 | continue; |
| 4380 | } |
Richard Smith | f7a0527 | 2013-01-14 07:53:01 +0000 | [diff] [blame] | 4381 | } |
| 4382 | |
Abramo Bagnara | b1f1b26 | 2010-04-30 09:13:03 +0000 | [diff] [blame] | 4383 | // If this is an attribute we can handle, do so now, |
| 4384 | // otherwise, add it to the FnAttrs list for rechaining. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4385 | switch (attr.getKind()) { |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4386 | default: |
| 4387 | // A C++11 attribute on a declarator chunk must appertain to a type. |
Richard Smith | d03de6a | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 4388 | if (attr.isCXX11Attribute() && TAL == TAL_DeclChunk) { |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4389 | state.getSema().Diag(attr.getLoc(), diag::err_attribute_not_type_attr) |
Richard Smith | d03de6a | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 4390 | << attr.getName(); |
| 4391 | attr.setUsedAsTypeAttr(); |
| 4392 | } |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4393 | break; |
| 4394 | |
| 4395 | case AttributeList::UnknownAttribute: |
| 4396 | if (attr.isCXX11Attribute() && TAL == TAL_DeclChunk) |
| 4397 | state.getSema().Diag(attr.getLoc(), |
| 4398 | diag::warn_unknown_attribute_ignored) |
| 4399 | << attr.getName(); |
| 4400 | break; |
| 4401 | |
| 4402 | case AttributeList::IgnoredAttribute: |
| 4403 | break; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4404 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4405 | case AttributeList::AT_MayAlias: |
Chandler Carruth | 682eae2 | 2011-10-07 18:40:27 +0000 | [diff] [blame] | 4406 | // FIXME: This attribute needs to actually be handled, but if we ignore |
| 4407 | // it it breaks large amounts of Linux software. |
| 4408 | attr.setUsedAsTypeAttr(); |
| 4409 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4410 | case AttributeList::AT_AddressSpace: |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4411 | HandleAddressSpaceTypeAttribute(type, attr, state.getSema()); |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4412 | attr.setUsedAsTypeAttr(); |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 4413 | break; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4414 | OBJC_POINTER_TYPE_ATTRS_CASELIST: |
| 4415 | if (!handleObjCPointerTypeAttr(state, attr, type)) |
| 4416 | distributeObjCPointerTypeAttr(state, attr, type); |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4417 | attr.setUsedAsTypeAttr(); |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 4418 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4419 | case AttributeList::AT_VectorSize: |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4420 | HandleVectorSizeAttr(type, attr, state.getSema()); |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4421 | attr.setUsedAsTypeAttr(); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4422 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4423 | case AttributeList::AT_ExtVectorType: |
Richard Smith | a4fa900 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 4424 | HandleExtVectorTypeAttr(type, attr, state.getSema()); |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4425 | attr.setUsedAsTypeAttr(); |
Douglas Gregor | 4ac0140 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 4426 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4427 | case AttributeList::AT_NeonVectorType: |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4428 | HandleNeonVectorTypeAttr(type, attr, state.getSema(), |
| 4429 | VectorType::NeonVector, "neon_vector_type"); |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4430 | attr.setUsedAsTypeAttr(); |
Bob Wilson | 4211bb6 | 2010-11-16 00:32:24 +0000 | [diff] [blame] | 4431 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4432 | case AttributeList::AT_NeonPolyVectorType: |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4433 | HandleNeonVectorTypeAttr(type, attr, state.getSema(), |
| 4434 | VectorType::NeonPolyVector, |
Bob Wilson | 4211bb6 | 2010-11-16 00:32:24 +0000 | [diff] [blame] | 4435 | "neon_polyvector_type"); |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4436 | attr.setUsedAsTypeAttr(); |
Bob Wilson | 4211bb6 | 2010-11-16 00:32:24 +0000 | [diff] [blame] | 4437 | break; |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4438 | case AttributeList::AT_OpenCLImageAccess: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4439 | HandleOpenCLImageAccessAttribute(type, attr, state.getSema()); |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4440 | attr.setUsedAsTypeAttr(); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4441 | break; |
| 4442 | |
Richard Smith | d03de6a | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 4443 | case AttributeList::AT_Win64: |
| 4444 | case AttributeList::AT_Ptr32: |
| 4445 | case AttributeList::AT_Ptr64: |
| 4446 | // FIXME: Don't ignore these. We have partial handling for them as |
| 4447 | // declaration attributes in SemaDeclAttr.cpp; that should be moved here. |
| 4448 | attr.setUsedAsTypeAttr(); |
| 4449 | break; |
| 4450 | |
Sean Hunt | 8e083e7 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4451 | case AttributeList::AT_NSReturnsRetained: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4452 | if (!state.getSema().getLangOpts().ObjCAutoRefCount) |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4453 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4454 | // fallthrough into the function attrs |
| 4455 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4456 | FUNCTION_TYPE_ATTRS_CASELIST: |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4457 | attr.setUsedAsTypeAttr(); |
| 4458 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4459 | // Never process function type attributes as part of the |
| 4460 | // declaration-specifiers. |
Richard Smith | f7a0527 | 2013-01-14 07:53:01 +0000 | [diff] [blame] | 4461 | if (TAL == TAL_DeclSpec) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4462 | distributeFunctionTypeAttrFromDeclSpec(state, attr, type); |
| 4463 | |
| 4464 | // Otherwise, handle the possible delays. |
| 4465 | else if (!handleFunctionTypeAttr(state, attr, type)) |
| 4466 | distributeFunctionTypeAttr(state, attr, type); |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 4467 | break; |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 4468 | } |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4469 | } while ((attrs = next)); |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 4470 | } |
| 4471 | |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 4472 | /// \brief Ensure that the type of the given expression is complete. |
| 4473 | /// |
| 4474 | /// This routine checks whether the expression \p E has a complete type. If the |
| 4475 | /// expression refers to an instantiable construct, that instantiation is |
| 4476 | /// performed as needed to complete its type. Furthermore |
| 4477 | /// Sema::RequireCompleteType is called for the expression's type (or in the |
| 4478 | /// case of a reference type, the referred-to type). |
| 4479 | /// |
| 4480 | /// \param E The expression whose type is required to be complete. |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4481 | /// \param Diagnoser The object that will emit a diagnostic if the type is |
| 4482 | /// incomplete. |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 4483 | /// |
| 4484 | /// \returns \c true if the type of \p E is incomplete and diagnosed, \c false |
| 4485 | /// otherwise. |
Douglas Gregor | f502d8e | 2012-05-04 16:48:41 +0000 | [diff] [blame] | 4486 | bool Sema::RequireCompleteExprType(Expr *E, TypeDiagnoser &Diagnoser){ |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 4487 | QualType T = E->getType(); |
| 4488 | |
| 4489 | // Fast path the case where the type is already complete. |
| 4490 | if (!T->isIncompleteType()) |
| 4491 | return false; |
| 4492 | |
| 4493 | // Incomplete array types may be completed by the initializer attached to |
| 4494 | // their definitions. For static data members of class templates we need to |
| 4495 | // instantiate the definition to get this initializer and complete the type. |
| 4496 | if (T->isIncompleteArrayType()) { |
| 4497 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParens())) { |
| 4498 | if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { |
| 4499 | if (Var->isStaticDataMember() && |
| 4500 | Var->getInstantiatedFromStaticDataMember()) { |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 4501 | |
Douglas Gregor | 36f255c | 2011-06-03 14:28:43 +0000 | [diff] [blame] | 4502 | MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo(); |
| 4503 | assert(MSInfo && "Missing member specialization information?"); |
| 4504 | if (MSInfo->getTemplateSpecializationKind() |
| 4505 | != TSK_ExplicitSpecialization) { |
| 4506 | // If we don't already have a point of instantiation, this is it. |
| 4507 | if (MSInfo->getPointOfInstantiation().isInvalid()) { |
| 4508 | MSInfo->setPointOfInstantiation(E->getLocStart()); |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 4509 | |
| 4510 | // This is a modification of an existing AST node. Notify |
Douglas Gregor | 36f255c | 2011-06-03 14:28:43 +0000 | [diff] [blame] | 4511 | // listeners. |
| 4512 | if (ASTMutationListener *L = getASTMutationListener()) |
| 4513 | L->StaticDataMemberInstantiated(Var); |
| 4514 | } |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 4515 | |
Douglas Gregor | 36f255c | 2011-06-03 14:28:43 +0000 | [diff] [blame] | 4516 | InstantiateStaticDataMemberDefinition(E->getExprLoc(), Var); |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 4517 | |
Douglas Gregor | 36f255c | 2011-06-03 14:28:43 +0000 | [diff] [blame] | 4518 | // Update the type to the newly instantiated definition's type both |
| 4519 | // here and within the expression. |
| 4520 | if (VarDecl *Def = Var->getDefinition()) { |
| 4521 | DRE->setDecl(Def); |
| 4522 | T = Def->getType(); |
| 4523 | DRE->setType(T); |
| 4524 | E->setType(T); |
| 4525 | } |
Douglas Gregor | f15748a | 2011-06-03 03:35:07 +0000 | [diff] [blame] | 4526 | } |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 4527 | |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 4528 | // We still go on to try to complete the type independently, as it |
| 4529 | // may also require instantiations or diagnostics if it remains |
| 4530 | // incomplete. |
| 4531 | } |
| 4532 | } |
| 4533 | } |
| 4534 | } |
| 4535 | |
| 4536 | // FIXME: Are there other cases which require instantiating something other |
| 4537 | // than the type to complete the type of an expression? |
| 4538 | |
| 4539 | // Look through reference types and complete the referred type. |
| 4540 | if (const ReferenceType *Ref = T->getAs<ReferenceType>()) |
| 4541 | T = Ref->getPointeeType(); |
| 4542 | |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4543 | return RequireCompleteType(E->getExprLoc(), T, Diagnoser); |
| 4544 | } |
| 4545 | |
| 4546 | namespace { |
Douglas Gregor | f502d8e | 2012-05-04 16:48:41 +0000 | [diff] [blame] | 4547 | struct TypeDiagnoserDiag : Sema::TypeDiagnoser { |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4548 | unsigned DiagID; |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 4549 | |
Douglas Gregor | f502d8e | 2012-05-04 16:48:41 +0000 | [diff] [blame] | 4550 | TypeDiagnoserDiag(unsigned DiagID) |
| 4551 | : Sema::TypeDiagnoser(DiagID == 0), DiagID(DiagID) {} |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 4552 | |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4553 | virtual void diagnose(Sema &S, SourceLocation Loc, QualType T) { |
| 4554 | if (Suppressed) return; |
| 4555 | S.Diag(Loc, DiagID) << T; |
| 4556 | } |
| 4557 | }; |
| 4558 | } |
| 4559 | |
| 4560 | bool Sema::RequireCompleteExprType(Expr *E, unsigned DiagID) { |
Douglas Gregor | f502d8e | 2012-05-04 16:48:41 +0000 | [diff] [blame] | 4561 | TypeDiagnoserDiag Diagnoser(DiagID); |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4562 | return RequireCompleteExprType(E, Diagnoser); |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 4563 | } |
| 4564 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4565 | /// @brief Ensure that the type T is a complete type. |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4566 | /// |
| 4567 | /// This routine checks whether the type @p T is complete in any |
| 4568 | /// context where a complete type is required. If @p T is a complete |
Douglas Gregor | 86447ec | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 4569 | /// type, returns false. If @p T is a class template specialization, |
| 4570 | /// this routine then attempts to perform class template |
| 4571 | /// instantiation. If instantiation fails, or if @p T is incomplete |
| 4572 | /// and cannot be completed, issues the diagnostic @p diag (giving it |
| 4573 | /// the type @p T) and returns true. |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4574 | /// |
| 4575 | /// @param Loc The location in the source that the incomplete type |
| 4576 | /// diagnostic should refer to. |
| 4577 | /// |
| 4578 | /// @param T The type that this routine is examining for completeness. |
| 4579 | /// |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4580 | /// @returns @c true if @p T is incomplete and a diagnostic was emitted, |
| 4581 | /// @c false otherwise. |
Anders Carlsson | 91a0cc9 | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 4582 | bool Sema::RequireCompleteType(SourceLocation Loc, QualType T, |
Douglas Gregor | f502d8e | 2012-05-04 16:48:41 +0000 | [diff] [blame] | 4583 | TypeDiagnoser &Diagnoser) { |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4584 | // FIXME: Add this assertion to make sure we always get instantiation points. |
| 4585 | // assert(!Loc.isInvalid() && "Invalid location in RequireCompleteType"); |
Douglas Gregor | 690dc7f | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 4586 | // FIXME: Add this assertion to help us flush out problems with |
| 4587 | // checking for dependent types and type-dependent expressions. |
| 4588 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4589 | // assert(!T->isDependentType() && |
Douglas Gregor | 690dc7f | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 4590 | // "Can't ask whether a dependent type is complete"); |
| 4591 | |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4592 | // If we have a complete type, we're done. |
Douglas Gregor | d07cc36 | 2012-01-02 17:18:37 +0000 | [diff] [blame] | 4593 | NamedDecl *Def = 0; |
| 4594 | if (!T->isIncompleteType(&Def)) { |
| 4595 | // If we know about the definition but it is not visible, complain. |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4596 | if (!Diagnoser.Suppressed && Def && !LookupResult::isVisible(Def)) { |
Douglas Gregor | d07cc36 | 2012-01-02 17:18:37 +0000 | [diff] [blame] | 4597 | // Suppress this error outside of a SFINAE context if we've already |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 4598 | // emitted the error once for this type. There's no usefulness in |
Douglas Gregor | d07cc36 | 2012-01-02 17:18:37 +0000 | [diff] [blame] | 4599 | // repeating the diagnostic. |
| 4600 | // FIXME: Add a Fix-It that imports the corresponding module or includes |
| 4601 | // the header. |
Douglas Gregor | ca2ab45 | 2013-01-12 01:29:50 +0000 | [diff] [blame] | 4602 | Module *Owner = Def->getOwningModule(); |
| 4603 | Diag(Loc, diag::err_module_private_definition) |
| 4604 | << T << Owner->getFullModuleName(); |
| 4605 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 4606 | |
| 4607 | if (!isSFINAEContext()) { |
| 4608 | // Recover by implicitly importing this module. |
| 4609 | createImplicitModuleImport(Loc, Owner); |
Douglas Gregor | d07cc36 | 2012-01-02 17:18:37 +0000 | [diff] [blame] | 4610 | } |
| 4611 | } |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 4612 | |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4613 | return false; |
Douglas Gregor | d07cc36 | 2012-01-02 17:18:37 +0000 | [diff] [blame] | 4614 | } |
Eli Friedman | 3c0eb16 | 2008-05-27 03:33:27 +0000 | [diff] [blame] | 4615 | |
Sean Callanan | bd79119 | 2011-12-16 00:20:31 +0000 | [diff] [blame] | 4616 | const TagType *Tag = T->getAs<TagType>(); |
| 4617 | const ObjCInterfaceType *IFace = 0; |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 4618 | |
Sean Callanan | bd79119 | 2011-12-16 00:20:31 +0000 | [diff] [blame] | 4619 | if (Tag) { |
| 4620 | // Avoid diagnosing invalid decls as incomplete. |
| 4621 | if (Tag->getDecl()->isInvalidDecl()) |
| 4622 | return true; |
| 4623 | |
| 4624 | // Give the external AST source a chance to complete the type. |
| 4625 | if (Tag->getDecl()->hasExternalLexicalStorage()) { |
| 4626 | Context.getExternalSource()->CompleteType(Tag->getDecl()); |
| 4627 | if (!Tag->isIncompleteType()) |
| 4628 | return false; |
| 4629 | } |
| 4630 | } |
| 4631 | else if ((IFace = T->getAs<ObjCInterfaceType>())) { |
| 4632 | // Avoid diagnosing invalid decls as incomplete. |
| 4633 | if (IFace->getDecl()->isInvalidDecl()) |
| 4634 | return true; |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 4635 | |
Sean Callanan | bd79119 | 2011-12-16 00:20:31 +0000 | [diff] [blame] | 4636 | // Give the external AST source a chance to complete the type. |
| 4637 | if (IFace->getDecl()->hasExternalLexicalStorage()) { |
| 4638 | Context.getExternalSource()->CompleteType(IFace->getDecl()); |
| 4639 | if (!IFace->isIncompleteType()) |
| 4640 | return false; |
| 4641 | } |
| 4642 | } |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 4643 | |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 4644 | // If we have a class template specialization or a class member of a |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 4645 | // class template specialization, or an array with known size of such, |
| 4646 | // try to instantiate it. |
| 4647 | QualType MaybeTemplate = T; |
Douglas Gregor | e656b83 | 2012-04-23 16:42:52 +0000 | [diff] [blame] | 4648 | while (const ConstantArrayType *Array |
| 4649 | = Context.getAsConstantArrayType(MaybeTemplate)) |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 4650 | MaybeTemplate = Array->getElementType(); |
| 4651 | if (const RecordType *Record = MaybeTemplate->getAs<RecordType>()) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4652 | if (ClassTemplateSpecializationDecl *ClassTemplateSpec |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 4653 | = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) { |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 4654 | if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared) |
| 4655 | return InstantiateClassTemplateSpecialization(Loc, ClassTemplateSpec, |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 4656 | TSK_ImplicitInstantiation, |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4657 | /*Complain=*/!Diagnoser.Suppressed); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4658 | } else if (CXXRecordDecl *Rec |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 4659 | = dyn_cast<CXXRecordDecl>(Record->getDecl())) { |
Richard Smith | 564f4c5 | 2012-03-22 03:35:28 +0000 | [diff] [blame] | 4660 | CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass(); |
| 4661 | if (!Rec->isBeingDefined() && Pattern) { |
| 4662 | MemberSpecializationInfo *MSI = Rec->getMemberSpecializationInfo(); |
| 4663 | assert(MSI && "Missing member specialization information?"); |
Douglas Gregor | 357bbd0 | 2009-08-28 20:50:45 +0000 | [diff] [blame] | 4664 | // This record was instantiated from a class within a template. |
Richard Smith | 564f4c5 | 2012-03-22 03:35:28 +0000 | [diff] [blame] | 4665 | if (MSI->getTemplateSpecializationKind() != TSK_ExplicitSpecialization) |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4666 | return InstantiateClass(Loc, Rec, Pattern, |
| 4667 | getTemplateInstantiationArgs(Rec), |
| 4668 | TSK_ImplicitInstantiation, |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4669 | /*Complain=*/!Diagnoser.Suppressed); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 4670 | } |
| 4671 | } |
| 4672 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4673 | |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4674 | if (Diagnoser.Suppressed) |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4675 | return true; |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4676 | |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4677 | // We have an incomplete type. Produce a diagnostic. |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4678 | Diagnoser.diagnose(*this, Loc, T); |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 4679 | |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4680 | // If the type was a forward declaration of a class/struct/union |
Rafael Espindola | 0162070 | 2010-03-21 22:56:43 +0000 | [diff] [blame] | 4681 | // type, produce a note. |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4682 | if (Tag && !Tag->getDecl()->isInvalidDecl()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4683 | Diag(Tag->getDecl()->getLocation(), |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4684 | Tag->isBeingDefined() ? diag::note_type_being_defined |
| 4685 | : diag::note_forward_declaration) |
Douglas Gregor | b302996 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 4686 | << QualType(Tag, 0); |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 4687 | |
Douglas Gregor | b302996 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 4688 | // If the Objective-C class was a forward declaration, produce a note. |
| 4689 | if (IFace && !IFace->getDecl()->isInvalidDecl()) |
| 4690 | Diag(IFace->getDecl()->getLocation(), diag::note_forward_class); |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4691 | |
| 4692 | return true; |
| 4693 | } |
Douglas Gregor | e625893 | 2009-03-19 00:39:20 +0000 | [diff] [blame] | 4694 | |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4695 | bool Sema::RequireCompleteType(SourceLocation Loc, QualType T, |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 4696 | unsigned DiagID) { |
Douglas Gregor | f502d8e | 2012-05-04 16:48:41 +0000 | [diff] [blame] | 4697 | TypeDiagnoserDiag Diagnoser(DiagID); |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4698 | return RequireCompleteType(Loc, T, Diagnoser); |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4699 | } |
| 4700 | |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 4701 | /// \brief Get diagnostic %select index for tag kind for |
| 4702 | /// literal type diagnostic message. |
| 4703 | /// WARNING: Indexes apply to particular diagnostics only! |
| 4704 | /// |
| 4705 | /// \returns diagnostic %select index. |
Joao Matos | f143ae9 | 2012-09-01 00:13:24 +0000 | [diff] [blame] | 4706 | static unsigned getLiteralDiagFromTagKind(TagTypeKind Tag) { |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 4707 | switch (Tag) { |
Joao Matos | f143ae9 | 2012-09-01 00:13:24 +0000 | [diff] [blame] | 4708 | case TTK_Struct: return 0; |
| 4709 | case TTK_Interface: return 1; |
| 4710 | case TTK_Class: return 2; |
| 4711 | default: llvm_unreachable("Invalid tag kind for literal type diagnostic!"); |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 4712 | } |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 4713 | } |
| 4714 | |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 4715 | /// @brief Ensure that the type T is a literal type. |
| 4716 | /// |
| 4717 | /// This routine checks whether the type @p T is a literal type. If @p T is an |
| 4718 | /// incomplete type, an attempt is made to complete it. If @p T is a literal |
| 4719 | /// type, or @p AllowIncompleteType is true and @p T is an incomplete type, |
| 4720 | /// returns false. Otherwise, this routine issues the diagnostic @p PD (giving |
| 4721 | /// it the type @p T), along with notes explaining why the type is not a |
| 4722 | /// literal type, and returns true. |
| 4723 | /// |
| 4724 | /// @param Loc The location in the source that the non-literal type |
| 4725 | /// diagnostic should refer to. |
| 4726 | /// |
| 4727 | /// @param T The type that this routine is examining for literalness. |
| 4728 | /// |
Douglas Gregor | f502d8e | 2012-05-04 16:48:41 +0000 | [diff] [blame] | 4729 | /// @param Diagnoser Emits a diagnostic if T is not a literal type. |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 4730 | /// |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 4731 | /// @returns @c true if @p T is not a literal type and a diagnostic was emitted, |
| 4732 | /// @c false otherwise. |
| 4733 | bool Sema::RequireLiteralType(SourceLocation Loc, QualType T, |
Douglas Gregor | f502d8e | 2012-05-04 16:48:41 +0000 | [diff] [blame] | 4734 | TypeDiagnoser &Diagnoser) { |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 4735 | assert(!T->isDependentType() && "type should not be dependent"); |
| 4736 | |
Eli Friedman | ee06539 | 2012-02-20 23:58:14 +0000 | [diff] [blame] | 4737 | QualType ElemType = Context.getBaseElementType(T); |
| 4738 | RequireCompleteType(Loc, ElemType, 0); |
| 4739 | |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 4740 | if (T->isLiteralType()) |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 4741 | return false; |
| 4742 | |
Douglas Gregor | f502d8e | 2012-05-04 16:48:41 +0000 | [diff] [blame] | 4743 | if (Diagnoser.Suppressed) |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 4744 | return true; |
| 4745 | |
Douglas Gregor | f502d8e | 2012-05-04 16:48:41 +0000 | [diff] [blame] | 4746 | Diagnoser.diagnose(*this, Loc, T); |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 4747 | |
| 4748 | if (T->isVariableArrayType()) |
| 4749 | return true; |
| 4750 | |
Eli Friedman | ee06539 | 2012-02-20 23:58:14 +0000 | [diff] [blame] | 4751 | const RecordType *RT = ElemType->getAs<RecordType>(); |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 4752 | if (!RT) |
| 4753 | return true; |
| 4754 | |
| 4755 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 4756 | |
Richard Smith | c799a6a | 2012-04-25 23:23:48 +0000 | [diff] [blame] | 4757 | // A partially-defined class type can't be a literal type, because a literal |
| 4758 | // class type must have a trivial destructor (which can't be checked until |
| 4759 | // the class definition is complete). |
| 4760 | if (!RD->isCompleteDefinition()) { |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4761 | RequireCompleteType(Loc, ElemType, diag::note_non_literal_incomplete, T); |
Eli Friedman | ee06539 | 2012-02-20 23:58:14 +0000 | [diff] [blame] | 4762 | return true; |
Richard Smith | c799a6a | 2012-04-25 23:23:48 +0000 | [diff] [blame] | 4763 | } |
Eli Friedman | ee06539 | 2012-02-20 23:58:14 +0000 | [diff] [blame] | 4764 | |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 4765 | // If the class has virtual base classes, then it's not an aggregate, and |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 4766 | // cannot have any constexpr constructors or a trivial default constructor, |
| 4767 | // so is non-literal. This is better to diagnose than the resulting absence |
| 4768 | // of constexpr constructors. |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 4769 | if (RD->getNumVBases()) { |
| 4770 | Diag(RD->getLocation(), diag::note_non_literal_virtual_base) |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 4771 | << getLiteralDiagFromTagKind(RD->getTagKind()) << RD->getNumVBases(); |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 4772 | for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(), |
| 4773 | E = RD->vbases_end(); I != E; ++I) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4774 | Diag(I->getLocStart(), |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 4775 | diag::note_constexpr_virtual_base_here) << I->getSourceRange(); |
Richard Smith | 86c3ae4 | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 4776 | } else if (!RD->isAggregate() && !RD->hasConstexprNonCopyMoveConstructor() && |
| 4777 | !RD->hasTrivialDefaultConstructor()) { |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 4778 | Diag(RD->getLocation(), diag::note_non_literal_no_constexpr_ctors) << RD; |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 4779 | } else if (RD->hasNonLiteralTypeFieldsOrBases()) { |
| 4780 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 4781 | E = RD->bases_end(); I != E; ++I) { |
| 4782 | if (!I->getType()->isLiteralType()) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4783 | Diag(I->getLocStart(), |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 4784 | diag::note_non_literal_base_class) |
| 4785 | << RD << I->getType() << I->getSourceRange(); |
| 4786 | return true; |
| 4787 | } |
| 4788 | } |
| 4789 | for (CXXRecordDecl::field_iterator I = RD->field_begin(), |
| 4790 | E = RD->field_end(); I != E; ++I) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 4791 | if (!I->getType()->isLiteralType() || |
| 4792 | I->getType().isVolatileQualified()) { |
| 4793 | Diag(I->getLocation(), diag::note_non_literal_field) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4794 | << RD << *I << I->getType() |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 4795 | << I->getType().isVolatileQualified(); |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 4796 | return true; |
| 4797 | } |
| 4798 | } |
| 4799 | } else if (!RD->hasTrivialDestructor()) { |
| 4800 | // All fields and bases are of literal types, so have trivial destructors. |
| 4801 | // If this class's destructor is non-trivial it must be user-declared. |
| 4802 | CXXDestructorDecl *Dtor = RD->getDestructor(); |
| 4803 | assert(Dtor && "class has literal fields and bases but no dtor?"); |
| 4804 | if (!Dtor) |
| 4805 | return true; |
| 4806 | |
| 4807 | Diag(Dtor->getLocation(), Dtor->isUserProvided() ? |
| 4808 | diag::note_non_literal_user_provided_dtor : |
| 4809 | diag::note_non_literal_nontrivial_dtor) << RD; |
Richard Smith | ac71351 | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 4810 | if (!Dtor->isUserProvided()) |
| 4811 | SpecialMemberIsTrivial(Dtor, CXXDestructor, /*Diagnose*/true); |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 4812 | } |
| 4813 | |
| 4814 | return true; |
| 4815 | } |
| 4816 | |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 4817 | bool Sema::RequireLiteralType(SourceLocation Loc, QualType T, unsigned DiagID) { |
Douglas Gregor | f502d8e | 2012-05-04 16:48:41 +0000 | [diff] [blame] | 4818 | TypeDiagnoserDiag Diagnoser(DiagID); |
| 4819 | return RequireLiteralType(Loc, T, Diagnoser); |
| 4820 | } |
| 4821 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4822 | /// \brief Retrieve a version of the type 'T' that is elaborated by Keyword |
| 4823 | /// and qualified by the nested-name-specifier contained in SS. |
| 4824 | QualType Sema::getElaboratedType(ElaboratedTypeKeyword Keyword, |
| 4825 | const CXXScopeSpec &SS, QualType T) { |
| 4826 | if (T.isNull()) |
Douglas Gregor | e625893 | 2009-03-19 00:39:20 +0000 | [diff] [blame] | 4827 | return T; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4828 | NestedNameSpecifier *NNS; |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 4829 | if (SS.isValid()) |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4830 | NNS = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 4831 | else { |
| 4832 | if (Keyword == ETK_None) |
| 4833 | return T; |
| 4834 | NNS = 0; |
| 4835 | } |
| 4836 | return Context.getElaboratedType(Keyword, NNS, T); |
Douglas Gregor | e625893 | 2009-03-19 00:39:20 +0000 | [diff] [blame] | 4837 | } |
Anders Carlsson | af017e6 | 2009-06-29 22:58:55 +0000 | [diff] [blame] | 4838 | |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 4839 | QualType Sema::BuildTypeofExprType(Expr *E, SourceLocation Loc) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 4840 | ExprResult ER = CheckPlaceholderExpr(E); |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 4841 | if (ER.isInvalid()) return QualType(); |
| 4842 | E = ER.take(); |
| 4843 | |
Fariborz Jahanian | 2b1d51b | 2010-10-05 23:24:00 +0000 | [diff] [blame] | 4844 | if (!E->isTypeDependent()) { |
| 4845 | QualType T = E->getType(); |
Fariborz Jahanian | aca7f7b | 2010-10-06 00:23:25 +0000 | [diff] [blame] | 4846 | if (const TagType *TT = T->getAs<TagType>()) |
| 4847 | DiagnoseUseOfDecl(TT->getDecl(), E->getExprLoc()); |
Fariborz Jahanian | 2b1d51b | 2010-10-05 23:24:00 +0000 | [diff] [blame] | 4848 | } |
Anders Carlsson | af017e6 | 2009-06-29 22:58:55 +0000 | [diff] [blame] | 4849 | return Context.getTypeOfExprType(E); |
| 4850 | } |
| 4851 | |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 4852 | /// getDecltypeForExpr - Given an expr, will return the decltype for |
| 4853 | /// that expression, according to the rules in C++11 |
| 4854 | /// [dcl.type.simple]p4 and C++11 [expr.lambda.prim]p18. |
| 4855 | static QualType getDecltypeForExpr(Sema &S, Expr *E) { |
| 4856 | if (E->isTypeDependent()) |
| 4857 | return S.Context.DependentTy; |
| 4858 | |
Douglas Gregor | 6d9ef30 | 2012-02-12 18:57:57 +0000 | [diff] [blame] | 4859 | // C++11 [dcl.type.simple]p4: |
| 4860 | // The type denoted by decltype(e) is defined as follows: |
| 4861 | // |
| 4862 | // - if e is an unparenthesized id-expression or an unparenthesized class |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 4863 | // member access (5.2.5), decltype(e) is the type of the entity named |
| 4864 | // by e. If there is no such entity, or if e names a set of overloaded |
Douglas Gregor | 6d9ef30 | 2012-02-12 18:57:57 +0000 | [diff] [blame] | 4865 | // functions, the program is ill-formed; |
Douglas Gregor | 84dd82e | 2012-09-13 23:40:46 +0000 | [diff] [blame] | 4866 | // |
| 4867 | // We apply the same rules for Objective-C ivar and property references. |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 4868 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 4869 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) |
| 4870 | return VD->getType(); |
Douglas Gregor | 84dd82e | 2012-09-13 23:40:46 +0000 | [diff] [blame] | 4871 | } else if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) { |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 4872 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) |
| 4873 | return FD->getType(); |
Douglas Gregor | 84dd82e | 2012-09-13 23:40:46 +0000 | [diff] [blame] | 4874 | } else if (const ObjCIvarRefExpr *IR = dyn_cast<ObjCIvarRefExpr>(E)) { |
| 4875 | return IR->getDecl()->getType(); |
| 4876 | } else if (const ObjCPropertyRefExpr *PR = dyn_cast<ObjCPropertyRefExpr>(E)) { |
| 4877 | if (PR->isExplicitProperty()) |
| 4878 | return PR->getExplicitProperty()->getType(); |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 4879 | } |
Douglas Gregor | 84dd82e | 2012-09-13 23:40:46 +0000 | [diff] [blame] | 4880 | |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 4881 | // C++11 [expr.lambda.prim]p18: |
| 4882 | // Every occurrence of decltype((x)) where x is a possibly |
| 4883 | // parenthesized id-expression that names an entity of automatic |
| 4884 | // storage duration is treated as if x were transformed into an |
| 4885 | // access to a corresponding data member of the closure type that |
| 4886 | // would have been declared if x were an odr-use of the denoted |
| 4887 | // entity. |
| 4888 | using namespace sema; |
| 4889 | if (S.getCurLambda()) { |
| 4890 | if (isa<ParenExpr>(E)) { |
| 4891 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParens())) { |
| 4892 | if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { |
Douglas Gregor | 6893284 | 2012-02-18 05:51:20 +0000 | [diff] [blame] | 4893 | QualType T = S.getCapturedDeclRefType(Var, DRE->getLocation()); |
| 4894 | if (!T.isNull()) |
| 4895 | return S.Context.getLValueReferenceType(T); |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 4896 | } |
| 4897 | } |
| 4898 | } |
| 4899 | } |
| 4900 | |
Douglas Gregor | 6d9ef30 | 2012-02-12 18:57:57 +0000 | [diff] [blame] | 4901 | |
| 4902 | // C++11 [dcl.type.simple]p4: |
| 4903 | // [...] |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 4904 | QualType T = E->getType(); |
Douglas Gregor | 6d9ef30 | 2012-02-12 18:57:57 +0000 | [diff] [blame] | 4905 | switch (E->getValueKind()) { |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 4906 | // - otherwise, if e is an xvalue, decltype(e) is T&&, where T is the |
Douglas Gregor | 6d9ef30 | 2012-02-12 18:57:57 +0000 | [diff] [blame] | 4907 | // type of e; |
| 4908 | case VK_XValue: T = S.Context.getRValueReferenceType(T); break; |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 4909 | // - otherwise, if e is an lvalue, decltype(e) is T&, where T is the |
Douglas Gregor | 6d9ef30 | 2012-02-12 18:57:57 +0000 | [diff] [blame] | 4910 | // type of e; |
| 4911 | case VK_LValue: T = S.Context.getLValueReferenceType(T); break; |
| 4912 | // - otherwise, decltype(e) is the type of e. |
| 4913 | case VK_RValue: break; |
| 4914 | } |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 4915 | |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 4916 | return T; |
| 4917 | } |
| 4918 | |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 4919 | QualType Sema::BuildDecltypeType(Expr *E, SourceLocation Loc) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 4920 | ExprResult ER = CheckPlaceholderExpr(E); |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 4921 | if (ER.isInvalid()) return QualType(); |
| 4922 | E = ER.take(); |
Chad Rosier | 91cbbbf | 2012-06-26 21:41:40 +0000 | [diff] [blame] | 4923 | |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 4924 | return Context.getDecltypeType(E, getDecltypeForExpr(*this, E)); |
Anders Carlsson | af017e6 | 2009-06-29 22:58:55 +0000 | [diff] [blame] | 4925 | } |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 4926 | |
| 4927 | QualType Sema::BuildUnaryTransformType(QualType BaseType, |
| 4928 | UnaryTransformType::UTTKind UKind, |
| 4929 | SourceLocation Loc) { |
| 4930 | switch (UKind) { |
| 4931 | case UnaryTransformType::EnumUnderlyingType: |
| 4932 | if (!BaseType->isDependentType() && !BaseType->isEnumeralType()) { |
| 4933 | Diag(Loc, diag::err_only_enums_have_underlying_types); |
| 4934 | return QualType(); |
| 4935 | } else { |
| 4936 | QualType Underlying = BaseType; |
| 4937 | if (!BaseType->isDependentType()) { |
| 4938 | EnumDecl *ED = BaseType->getAs<EnumType>()->getDecl(); |
| 4939 | assert(ED && "EnumType has no EnumDecl"); |
| 4940 | DiagnoseUseOfDecl(ED, Loc); |
| 4941 | Underlying = ED->getIntegerType(); |
| 4942 | } |
| 4943 | assert(!Underlying.isNull()); |
| 4944 | return Context.getUnaryTransformType(BaseType, Underlying, |
| 4945 | UnaryTransformType::EnumUnderlyingType); |
| 4946 | } |
| 4947 | } |
| 4948 | llvm_unreachable("unknown unary transform type"); |
| 4949 | } |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4950 | |
| 4951 | QualType Sema::BuildAtomicType(QualType T, SourceLocation Loc) { |
| 4952 | if (!T->isDependentType()) { |
Richard Smith | 8327118 | 2012-02-11 18:03:45 +0000 | [diff] [blame] | 4953 | // FIXME: It isn't entirely clear whether incomplete atomic types |
| 4954 | // are allowed or not; for simplicity, ban them for the moment. |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4955 | if (RequireCompleteType(Loc, T, diag::err_atomic_specifier_bad_type, 0)) |
Richard Smith | 8327118 | 2012-02-11 18:03:45 +0000 | [diff] [blame] | 4956 | return QualType(); |
| 4957 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4958 | int DisallowedKind = -1; |
Richard Smith | 8327118 | 2012-02-11 18:03:45 +0000 | [diff] [blame] | 4959 | if (T->isArrayType()) |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4960 | DisallowedKind = 1; |
| 4961 | else if (T->isFunctionType()) |
| 4962 | DisallowedKind = 2; |
| 4963 | else if (T->isReferenceType()) |
| 4964 | DisallowedKind = 3; |
| 4965 | else if (T->isAtomicType()) |
| 4966 | DisallowedKind = 4; |
| 4967 | else if (T.hasQualifiers()) |
| 4968 | DisallowedKind = 5; |
| 4969 | else if (!T.isTriviallyCopyableType(Context)) |
| 4970 | // Some other non-trivially-copyable type (probably a C++ class) |
| 4971 | DisallowedKind = 6; |
| 4972 | |
| 4973 | if (DisallowedKind != -1) { |
| 4974 | Diag(Loc, diag::err_atomic_specifier_bad_type) << DisallowedKind << T; |
| 4975 | return QualType(); |
| 4976 | } |
| 4977 | |
| 4978 | // FIXME: Do we need any handling for ARC here? |
| 4979 | } |
| 4980 | |
| 4981 | // Build the pointer type. |
| 4982 | return Context.getAtomicType(T); |
| 4983 | } |