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" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 15 | #include "clang/Sema/Template.h" |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 16 | #include "clang/Basic/OpenCL.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 36f255c | 2011-06-03 14:28:43 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTMutationListener.h" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 19 | #include "clang/AST/CXXInheritance.h" |
Steve Naroff | 980e508 | 2007-10-01 19:00:59 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclTemplate.h" |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 22 | #include "clang/AST/TypeLoc.h" |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 23 | #include "clang/AST/TypeLocVisitor.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 24 | #include "clang/AST/Expr.h" |
Anders Carlsson | 91a0cc9 | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 25 | #include "clang/Basic/PartialDiagnostic.h" |
Charles Davis | d18f9f9 | 2010-08-16 04:01:50 +0000 | [diff] [blame] | 26 | #include "clang/Basic/TargetInfo.h" |
John McCall | 2792fa5 | 2011-03-08 04:17:03 +0000 | [diff] [blame] | 27 | #include "clang/Lex/Preprocessor.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" |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/SmallPtrSet.h" |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 32 | #include "llvm/Support/ErrorHandling.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 33 | using namespace clang; |
| 34 | |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 35 | /// isOmittedBlockReturnType - Return true if this declarator is missing a |
| 36 | /// 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] | 37 | static bool isOmittedBlockReturnType(const Declarator &D) { |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 38 | if (D.getContext() != Declarator::BlockLiteralContext || |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 39 | D.getDeclSpec().hasTypeSpecifier()) |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 40 | return false; |
| 41 | |
| 42 | if (D.getNumTypeObjects() == 0) |
Chris Lattner | a64ef0a | 2009-10-25 22:09:09 +0000 | [diff] [blame] | 43 | return true; // ^{ ... } |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 44 | |
| 45 | if (D.getNumTypeObjects() == 1 && |
| 46 | D.getTypeObject(0).Kind == DeclaratorChunk::Function) |
Chris Lattner | a64ef0a | 2009-10-25 22:09:09 +0000 | [diff] [blame] | 47 | return true; // ^(int X, float Y) { ... } |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 48 | |
| 49 | return false; |
| 50 | } |
| 51 | |
John McCall | 2792fa5 | 2011-03-08 04:17:03 +0000 | [diff] [blame] | 52 | /// diagnoseBadTypeAttribute - Diagnoses a type attribute which |
| 53 | /// doesn't apply to the given type. |
| 54 | static void diagnoseBadTypeAttribute(Sema &S, const AttributeList &attr, |
| 55 | QualType type) { |
Chandler Carruth | 108f756 | 2011-07-26 05:40:03 +0000 | [diff] [blame] | 56 | bool useExpansionLoc = false; |
John McCall | 2792fa5 | 2011-03-08 04:17:03 +0000 | [diff] [blame] | 57 | |
| 58 | unsigned diagID = 0; |
| 59 | switch (attr.getKind()) { |
| 60 | case AttributeList::AT_objc_gc: |
| 61 | diagID = diag::warn_pointer_attribute_wrong_type; |
Chandler Carruth | 108f756 | 2011-07-26 05:40:03 +0000 | [diff] [blame] | 62 | useExpansionLoc = true; |
John McCall | 2792fa5 | 2011-03-08 04:17:03 +0000 | [diff] [blame] | 63 | break; |
| 64 | |
Argyrios Kyrtzidis | 05d4876 | 2011-07-01 22:23:09 +0000 | [diff] [blame] | 65 | case AttributeList::AT_objc_ownership: |
| 66 | diagID = diag::warn_objc_object_attribute_wrong_type; |
Chandler Carruth | 108f756 | 2011-07-26 05:40:03 +0000 | [diff] [blame] | 67 | useExpansionLoc = true; |
Argyrios Kyrtzidis | 05d4876 | 2011-07-01 22:23:09 +0000 | [diff] [blame] | 68 | break; |
| 69 | |
John McCall | 2792fa5 | 2011-03-08 04:17:03 +0000 | [diff] [blame] | 70 | default: |
| 71 | // Assume everything else was a function attribute. |
| 72 | diagID = diag::warn_function_attribute_wrong_type; |
| 73 | break; |
| 74 | } |
| 75 | |
| 76 | SourceLocation loc = attr.getLoc(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 77 | StringRef name = attr.getName()->getName(); |
John McCall | 2792fa5 | 2011-03-08 04:17:03 +0000 | [diff] [blame] | 78 | |
| 79 | // The GC attributes are usually written with macros; special-case them. |
Chandler Carruth | 108f756 | 2011-07-26 05:40:03 +0000 | [diff] [blame] | 80 | if (useExpansionLoc && loc.isMacroID() && attr.getParameterName()) { |
John McCall | 834e3f6 | 2011-03-08 07:59:04 +0000 | [diff] [blame] | 81 | if (attr.getParameterName()->isStr("strong")) { |
| 82 | if (S.findMacroSpelling(loc, "__strong")) name = "__strong"; |
| 83 | } else if (attr.getParameterName()->isStr("weak")) { |
| 84 | if (S.findMacroSpelling(loc, "__weak")) name = "__weak"; |
John McCall | 2792fa5 | 2011-03-08 04:17:03 +0000 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
| 88 | S.Diag(loc, diagID) << name << type; |
| 89 | } |
| 90 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 91 | // objc_gc applies to Objective-C pointers or, otherwise, to the |
| 92 | // smallest available pointer type (i.e. 'void*' in 'void**'). |
| 93 | #define OBJC_POINTER_TYPE_ATTRS_CASELIST \ |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 94 | case AttributeList::AT_objc_gc: \ |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 95 | case AttributeList::AT_objc_ownership |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 96 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 97 | // Function type attributes. |
| 98 | #define FUNCTION_TYPE_ATTRS_CASELIST \ |
| 99 | case AttributeList::AT_noreturn: \ |
| 100 | case AttributeList::AT_cdecl: \ |
| 101 | case AttributeList::AT_fastcall: \ |
| 102 | case AttributeList::AT_stdcall: \ |
| 103 | case AttributeList::AT_thiscall: \ |
| 104 | case AttributeList::AT_pascal: \ |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 105 | case AttributeList::AT_regparm: \ |
| 106 | case AttributeList::AT_pcs \ |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 107 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 108 | namespace { |
| 109 | /// An object which stores processing state for the entire |
| 110 | /// GetTypeForDeclarator process. |
| 111 | class TypeProcessingState { |
| 112 | Sema &sema; |
| 113 | |
| 114 | /// The declarator being processed. |
| 115 | Declarator &declarator; |
| 116 | |
| 117 | /// The index of the declarator chunk we're currently processing. |
| 118 | /// May be the total number of valid chunks, indicating the |
| 119 | /// DeclSpec. |
| 120 | unsigned chunkIndex; |
| 121 | |
| 122 | /// Whether there are non-trivial modifications to the decl spec. |
| 123 | bool trivial; |
| 124 | |
John McCall | 7ea2193 | 2011-03-26 01:39:56 +0000 | [diff] [blame] | 125 | /// Whether we saved the attributes in the decl spec. |
| 126 | bool hasSavedAttrs; |
| 127 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 128 | /// The original set of attributes on the DeclSpec. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 129 | SmallVector<AttributeList*, 2> savedAttrs; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 130 | |
| 131 | /// A list of attributes to diagnose the uselessness of when the |
| 132 | /// processing is complete. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 133 | SmallVector<AttributeList*, 2> ignoredTypeAttrs; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 134 | |
| 135 | public: |
| 136 | TypeProcessingState(Sema &sema, Declarator &declarator) |
| 137 | : sema(sema), declarator(declarator), |
| 138 | chunkIndex(declarator.getNumTypeObjects()), |
John McCall | 7ea2193 | 2011-03-26 01:39:56 +0000 | [diff] [blame] | 139 | trivial(true), hasSavedAttrs(false) {} |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 140 | |
| 141 | Sema &getSema() const { |
| 142 | return sema; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 143 | } |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 144 | |
| 145 | Declarator &getDeclarator() const { |
| 146 | return declarator; |
| 147 | } |
| 148 | |
| 149 | unsigned getCurrentChunkIndex() const { |
| 150 | return chunkIndex; |
| 151 | } |
| 152 | |
| 153 | void setCurrentChunkIndex(unsigned idx) { |
| 154 | assert(idx <= declarator.getNumTypeObjects()); |
| 155 | chunkIndex = idx; |
| 156 | } |
| 157 | |
| 158 | AttributeList *&getCurrentAttrListRef() const { |
| 159 | assert(chunkIndex <= declarator.getNumTypeObjects()); |
| 160 | if (chunkIndex == declarator.getNumTypeObjects()) |
| 161 | return getMutableDeclSpec().getAttributes().getListRef(); |
| 162 | return declarator.getTypeObject(chunkIndex).getAttrListRef(); |
| 163 | } |
| 164 | |
| 165 | /// Save the current set of attributes on the DeclSpec. |
| 166 | void saveDeclSpecAttrs() { |
| 167 | // Don't try to save them multiple times. |
John McCall | 7ea2193 | 2011-03-26 01:39:56 +0000 | [diff] [blame] | 168 | if (hasSavedAttrs) return; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 169 | |
| 170 | DeclSpec &spec = getMutableDeclSpec(); |
| 171 | for (AttributeList *attr = spec.getAttributes().getList(); attr; |
| 172 | attr = attr->getNext()) |
| 173 | savedAttrs.push_back(attr); |
| 174 | trivial &= savedAttrs.empty(); |
John McCall | 7ea2193 | 2011-03-26 01:39:56 +0000 | [diff] [blame] | 175 | hasSavedAttrs = true; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /// Record that we had nowhere to put the given type attribute. |
| 179 | /// We will diagnose such attributes later. |
| 180 | void addIgnoredTypeAttr(AttributeList &attr) { |
| 181 | ignoredTypeAttrs.push_back(&attr); |
| 182 | } |
| 183 | |
| 184 | /// Diagnose all the ignored type attributes, given that the |
| 185 | /// declarator worked out to the given type. |
| 186 | void diagnoseIgnoredTypeAttrs(QualType type) const { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 187 | for (SmallVectorImpl<AttributeList*>::const_iterator |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 188 | i = ignoredTypeAttrs.begin(), e = ignoredTypeAttrs.end(); |
John McCall | 2792fa5 | 2011-03-08 04:17:03 +0000 | [diff] [blame] | 189 | i != e; ++i) |
| 190 | diagnoseBadTypeAttribute(getSema(), **i, type); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | ~TypeProcessingState() { |
| 194 | if (trivial) return; |
| 195 | |
| 196 | restoreDeclSpecAttrs(); |
| 197 | } |
| 198 | |
| 199 | private: |
| 200 | DeclSpec &getMutableDeclSpec() const { |
| 201 | return const_cast<DeclSpec&>(declarator.getDeclSpec()); |
| 202 | } |
| 203 | |
| 204 | void restoreDeclSpecAttrs() { |
John McCall | 7ea2193 | 2011-03-26 01:39:56 +0000 | [diff] [blame] | 205 | assert(hasSavedAttrs); |
| 206 | |
| 207 | if (savedAttrs.empty()) { |
| 208 | getMutableDeclSpec().getAttributes().set(0); |
| 209 | return; |
| 210 | } |
| 211 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 212 | getMutableDeclSpec().getAttributes().set(savedAttrs[0]); |
| 213 | for (unsigned i = 0, e = savedAttrs.size() - 1; i != e; ++i) |
| 214 | savedAttrs[i]->setNext(savedAttrs[i+1]); |
| 215 | savedAttrs.back()->setNext(0); |
| 216 | } |
| 217 | }; |
| 218 | |
| 219 | /// Basically std::pair except that we really want to avoid an |
| 220 | /// implicit operator= for safety concerns. It's also a minor |
| 221 | /// link-time optimization for this to be a private type. |
| 222 | struct AttrAndList { |
| 223 | /// The attribute. |
| 224 | AttributeList &first; |
| 225 | |
| 226 | /// The head of the list the attribute is currently in. |
| 227 | AttributeList *&second; |
| 228 | |
| 229 | AttrAndList(AttributeList &attr, AttributeList *&head) |
| 230 | : first(attr), second(head) {} |
| 231 | }; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 232 | } |
| 233 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 234 | namespace llvm { |
| 235 | template <> struct isPodLike<AttrAndList> { |
| 236 | static const bool value = true; |
| 237 | }; |
| 238 | } |
| 239 | |
| 240 | static void spliceAttrIntoList(AttributeList &attr, AttributeList *&head) { |
| 241 | attr.setNext(head); |
| 242 | head = &attr; |
| 243 | } |
| 244 | |
| 245 | static void spliceAttrOutOfList(AttributeList &attr, AttributeList *&head) { |
| 246 | if (head == &attr) { |
| 247 | head = attr.getNext(); |
| 248 | return; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 249 | } |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 250 | |
| 251 | AttributeList *cur = head; |
| 252 | while (true) { |
| 253 | assert(cur && cur->getNext() && "ran out of attrs?"); |
| 254 | if (cur->getNext() == &attr) { |
| 255 | cur->setNext(attr.getNext()); |
| 256 | return; |
| 257 | } |
| 258 | cur = cur->getNext(); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | static void moveAttrFromListToList(AttributeList &attr, |
| 263 | AttributeList *&fromList, |
| 264 | AttributeList *&toList) { |
| 265 | spliceAttrOutOfList(attr, fromList); |
| 266 | spliceAttrIntoList(attr, toList); |
| 267 | } |
| 268 | |
| 269 | static void processTypeAttrs(TypeProcessingState &state, |
| 270 | QualType &type, bool isDeclSpec, |
| 271 | AttributeList *attrs); |
| 272 | |
| 273 | static bool handleFunctionTypeAttr(TypeProcessingState &state, |
| 274 | AttributeList &attr, |
| 275 | QualType &type); |
| 276 | |
| 277 | static bool handleObjCGCTypeAttr(TypeProcessingState &state, |
| 278 | AttributeList &attr, QualType &type); |
| 279 | |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 280 | static bool handleObjCOwnershipTypeAttr(TypeProcessingState &state, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 281 | AttributeList &attr, QualType &type); |
| 282 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 283 | static bool handleObjCPointerTypeAttr(TypeProcessingState &state, |
| 284 | AttributeList &attr, QualType &type) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 285 | if (attr.getKind() == AttributeList::AT_objc_gc) |
| 286 | return handleObjCGCTypeAttr(state, attr, type); |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 287 | assert(attr.getKind() == AttributeList::AT_objc_ownership); |
| 288 | return handleObjCOwnershipTypeAttr(state, attr, type); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | /// Given that an objc_gc attribute was written somewhere on a |
| 292 | /// declaration *other* than on the declarator itself (for which, use |
| 293 | /// distributeObjCPointerTypeAttrFromDeclarator), and given that it |
| 294 | /// didn't apply in whatever position it was written in, try to move |
| 295 | /// it to a more appropriate position. |
| 296 | static void distributeObjCPointerTypeAttr(TypeProcessingState &state, |
| 297 | AttributeList &attr, |
| 298 | QualType type) { |
| 299 | Declarator &declarator = state.getDeclarator(); |
| 300 | for (unsigned i = state.getCurrentChunkIndex(); i != 0; --i) { |
| 301 | DeclaratorChunk &chunk = declarator.getTypeObject(i-1); |
| 302 | switch (chunk.Kind) { |
| 303 | case DeclaratorChunk::Pointer: |
| 304 | case DeclaratorChunk::BlockPointer: |
| 305 | moveAttrFromListToList(attr, state.getCurrentAttrListRef(), |
| 306 | chunk.getAttrListRef()); |
| 307 | return; |
| 308 | |
| 309 | case DeclaratorChunk::Paren: |
| 310 | case DeclaratorChunk::Array: |
| 311 | continue; |
| 312 | |
| 313 | // Don't walk through these. |
| 314 | case DeclaratorChunk::Reference: |
| 315 | case DeclaratorChunk::Function: |
| 316 | case DeclaratorChunk::MemberPointer: |
| 317 | goto error; |
| 318 | } |
| 319 | } |
| 320 | error: |
John McCall | 2792fa5 | 2011-03-08 04:17:03 +0000 | [diff] [blame] | 321 | |
| 322 | diagnoseBadTypeAttribute(state.getSema(), attr, type); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | /// Distribute an objc_gc type attribute that was written on the |
| 326 | /// declarator. |
| 327 | static void |
| 328 | distributeObjCPointerTypeAttrFromDeclarator(TypeProcessingState &state, |
| 329 | AttributeList &attr, |
| 330 | QualType &declSpecType) { |
| 331 | Declarator &declarator = state.getDeclarator(); |
| 332 | |
| 333 | // objc_gc goes on the innermost pointer to something that's not a |
| 334 | // pointer. |
| 335 | unsigned innermost = -1U; |
| 336 | bool considerDeclSpec = true; |
| 337 | for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) { |
| 338 | DeclaratorChunk &chunk = declarator.getTypeObject(i); |
| 339 | switch (chunk.Kind) { |
| 340 | case DeclaratorChunk::Pointer: |
| 341 | case DeclaratorChunk::BlockPointer: |
| 342 | innermost = i; |
John McCall | ae278a3 | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 343 | continue; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 344 | |
| 345 | case DeclaratorChunk::Reference: |
| 346 | case DeclaratorChunk::MemberPointer: |
| 347 | case DeclaratorChunk::Paren: |
| 348 | case DeclaratorChunk::Array: |
| 349 | continue; |
| 350 | |
| 351 | case DeclaratorChunk::Function: |
| 352 | considerDeclSpec = false; |
| 353 | goto done; |
| 354 | } |
| 355 | } |
| 356 | done: |
| 357 | |
| 358 | // That might actually be the decl spec if we weren't blocked by |
| 359 | // anything in the declarator. |
| 360 | if (considerDeclSpec) { |
John McCall | 7ea2193 | 2011-03-26 01:39:56 +0000 | [diff] [blame] | 361 | if (handleObjCPointerTypeAttr(state, attr, declSpecType)) { |
| 362 | // Splice the attribute into the decl spec. Prevents the |
| 363 | // attribute from being applied multiple times and gives |
| 364 | // the source-location-filler something to work with. |
| 365 | state.saveDeclSpecAttrs(); |
| 366 | moveAttrFromListToList(attr, declarator.getAttrListRef(), |
| 367 | declarator.getMutableDeclSpec().getAttributes().getListRef()); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 368 | return; |
John McCall | 7ea2193 | 2011-03-26 01:39:56 +0000 | [diff] [blame] | 369 | } |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | // Otherwise, if we found an appropriate chunk, splice the attribute |
| 373 | // into it. |
| 374 | if (innermost != -1U) { |
| 375 | moveAttrFromListToList(attr, declarator.getAttrListRef(), |
| 376 | declarator.getTypeObject(innermost).getAttrListRef()); |
| 377 | return; |
| 378 | } |
| 379 | |
| 380 | // Otherwise, diagnose when we're done building the type. |
| 381 | spliceAttrOutOfList(attr, declarator.getAttrListRef()); |
| 382 | state.addIgnoredTypeAttr(attr); |
| 383 | } |
| 384 | |
| 385 | /// A function type attribute was written somewhere in a declaration |
| 386 | /// *other* than on the declarator itself or in the decl spec. Given |
| 387 | /// that it didn't apply in whatever position it was written in, try |
| 388 | /// to move it to a more appropriate position. |
| 389 | static void distributeFunctionTypeAttr(TypeProcessingState &state, |
| 390 | AttributeList &attr, |
| 391 | QualType type) { |
| 392 | Declarator &declarator = state.getDeclarator(); |
| 393 | |
| 394 | // Try to push the attribute from the return type of a function to |
| 395 | // the function itself. |
| 396 | for (unsigned i = state.getCurrentChunkIndex(); i != 0; --i) { |
| 397 | DeclaratorChunk &chunk = declarator.getTypeObject(i-1); |
| 398 | switch (chunk.Kind) { |
| 399 | case DeclaratorChunk::Function: |
| 400 | moveAttrFromListToList(attr, state.getCurrentAttrListRef(), |
| 401 | chunk.getAttrListRef()); |
| 402 | return; |
| 403 | |
| 404 | case DeclaratorChunk::Paren: |
| 405 | case DeclaratorChunk::Pointer: |
| 406 | case DeclaratorChunk::BlockPointer: |
| 407 | case DeclaratorChunk::Array: |
| 408 | case DeclaratorChunk::Reference: |
| 409 | case DeclaratorChunk::MemberPointer: |
| 410 | continue; |
| 411 | } |
| 412 | } |
| 413 | |
John McCall | 2792fa5 | 2011-03-08 04:17:03 +0000 | [diff] [blame] | 414 | diagnoseBadTypeAttribute(state.getSema(), attr, type); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | /// Try to distribute a function type attribute to the innermost |
| 418 | /// function chunk or type. Returns true if the attribute was |
| 419 | /// distributed, false if no location was found. |
| 420 | static bool |
| 421 | distributeFunctionTypeAttrToInnermost(TypeProcessingState &state, |
| 422 | AttributeList &attr, |
| 423 | AttributeList *&attrList, |
| 424 | QualType &declSpecType) { |
| 425 | Declarator &declarator = state.getDeclarator(); |
| 426 | |
| 427 | // Put it on the innermost function chunk, if there is one. |
| 428 | for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) { |
| 429 | DeclaratorChunk &chunk = declarator.getTypeObject(i); |
| 430 | if (chunk.Kind != DeclaratorChunk::Function) continue; |
| 431 | |
| 432 | moveAttrFromListToList(attr, attrList, chunk.getAttrListRef()); |
| 433 | return true; |
| 434 | } |
| 435 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 436 | if (handleFunctionTypeAttr(state, attr, declSpecType)) { |
| 437 | spliceAttrOutOfList(attr, attrList); |
| 438 | return true; |
| 439 | } |
| 440 | |
| 441 | return false; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | /// A function type attribute was written in the decl spec. Try to |
| 445 | /// apply it somewhere. |
| 446 | static void |
| 447 | distributeFunctionTypeAttrFromDeclSpec(TypeProcessingState &state, |
| 448 | AttributeList &attr, |
| 449 | QualType &declSpecType) { |
| 450 | state.saveDeclSpecAttrs(); |
| 451 | |
| 452 | // Try to distribute to the innermost. |
| 453 | if (distributeFunctionTypeAttrToInnermost(state, attr, |
| 454 | state.getCurrentAttrListRef(), |
| 455 | declSpecType)) |
| 456 | return; |
| 457 | |
| 458 | // If that failed, diagnose the bad attribute when the declarator is |
| 459 | // fully built. |
| 460 | state.addIgnoredTypeAttr(attr); |
| 461 | } |
| 462 | |
| 463 | /// A function type attribute was written on the declarator. Try to |
| 464 | /// apply it somewhere. |
| 465 | static void |
| 466 | distributeFunctionTypeAttrFromDeclarator(TypeProcessingState &state, |
| 467 | AttributeList &attr, |
| 468 | QualType &declSpecType) { |
| 469 | Declarator &declarator = state.getDeclarator(); |
| 470 | |
| 471 | // Try to distribute to the innermost. |
| 472 | if (distributeFunctionTypeAttrToInnermost(state, attr, |
| 473 | declarator.getAttrListRef(), |
| 474 | declSpecType)) |
| 475 | return; |
| 476 | |
| 477 | // If that failed, diagnose the bad attribute when the declarator is |
| 478 | // fully built. |
| 479 | spliceAttrOutOfList(attr, declarator.getAttrListRef()); |
| 480 | state.addIgnoredTypeAttr(attr); |
| 481 | } |
| 482 | |
| 483 | /// \brief Given that there are attributes written on the declarator |
| 484 | /// itself, try to distribute any type attributes to the appropriate |
| 485 | /// declarator chunk. |
| 486 | /// |
| 487 | /// These are attributes like the following: |
| 488 | /// int f ATTR; |
| 489 | /// int (f ATTR)(); |
| 490 | /// but not necessarily this: |
| 491 | /// int f() ATTR; |
| 492 | static void distributeTypeAttrsFromDeclarator(TypeProcessingState &state, |
| 493 | QualType &declSpecType) { |
| 494 | // Collect all the type attributes from the declarator itself. |
| 495 | assert(state.getDeclarator().getAttributes() && "declarator has no attrs!"); |
| 496 | AttributeList *attr = state.getDeclarator().getAttributes(); |
| 497 | AttributeList *next; |
| 498 | do { |
| 499 | next = attr->getNext(); |
| 500 | |
| 501 | switch (attr->getKind()) { |
| 502 | OBJC_POINTER_TYPE_ATTRS_CASELIST: |
| 503 | distributeObjCPointerTypeAttrFromDeclarator(state, *attr, declSpecType); |
| 504 | break; |
| 505 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 506 | case AttributeList::AT_ns_returns_retained: |
| 507 | if (!state.getSema().getLangOptions().ObjCAutoRefCount) |
| 508 | break; |
| 509 | // fallthrough |
| 510 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 511 | FUNCTION_TYPE_ATTRS_CASELIST: |
| 512 | distributeFunctionTypeAttrFromDeclarator(state, *attr, declSpecType); |
| 513 | break; |
| 514 | |
| 515 | default: |
| 516 | break; |
| 517 | } |
| 518 | } while ((attr = next)); |
| 519 | } |
| 520 | |
| 521 | /// Add a synthetic '()' to a block-literal declarator if it is |
| 522 | /// required, given the return type. |
| 523 | static void maybeSynthesizeBlockSignature(TypeProcessingState &state, |
| 524 | QualType declSpecType) { |
| 525 | Declarator &declarator = state.getDeclarator(); |
| 526 | |
| 527 | // First, check whether the declarator would produce a function, |
| 528 | // i.e. whether the innermost semantic chunk is a function. |
| 529 | if (declarator.isFunctionDeclarator()) { |
| 530 | // If so, make that declarator a prototyped declarator. |
| 531 | declarator.getFunctionTypeInfo().hasPrototype = true; |
| 532 | return; |
| 533 | } |
| 534 | |
John McCall | da26379 | 2011-02-08 01:59:10 +0000 | [diff] [blame] | 535 | // If there are any type objects, the type as written won't name a |
| 536 | // function, regardless of the decl spec type. This is because a |
| 537 | // block signature declarator is always an abstract-declarator, and |
| 538 | // abstract-declarators can't just be parentheses chunks. Therefore |
| 539 | // we need to build a function chunk unless there are no type |
| 540 | // objects and the decl spec type is a function. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 541 | if (!declarator.getNumTypeObjects() && declSpecType->isFunctionType()) |
| 542 | return; |
| 543 | |
John McCall | da26379 | 2011-02-08 01:59:10 +0000 | [diff] [blame] | 544 | // Note that there *are* cases with invalid declarators where |
| 545 | // declarators consist solely of parentheses. In general, these |
| 546 | // occur only in failed efforts to make function declarators, so |
| 547 | // faking up the function chunk is still the right thing to do. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 548 | |
| 549 | // Otherwise, we need to fake up a function declarator. |
| 550 | SourceLocation loc = declarator.getSourceRange().getBegin(); |
| 551 | |
| 552 | // ...and *prepend* it to the declarator. |
| 553 | declarator.AddInnermostTypeInfo(DeclaratorChunk::getFunction( |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 554 | /*proto*/ true, |
| 555 | /*variadic*/ false, SourceLocation(), |
| 556 | /*args*/ 0, 0, |
| 557 | /*type quals*/ 0, |
Douglas Gregor | 83f5172 | 2011-01-26 03:43:54 +0000 | [diff] [blame] | 558 | /*ref-qualifier*/true, SourceLocation(), |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 559 | /*const qualifier*/SourceLocation(), |
| 560 | /*volatile qualifier*/SourceLocation(), |
Douglas Gregor | 90ebed0 | 2011-07-13 21:47:47 +0000 | [diff] [blame] | 561 | /*mutable qualifier*/SourceLocation(), |
Sebastian Redl | 6e5d319 | 2011-03-05 22:42:13 +0000 | [diff] [blame] | 562 | /*EH*/ EST_None, SourceLocation(), 0, 0, 0, 0, |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 563 | /*parens*/ loc, loc, |
| 564 | declarator)); |
| 565 | |
| 566 | // For consistency, make sure the state still has us as processing |
| 567 | // the decl spec. |
| 568 | assert(state.getCurrentChunkIndex() == declarator.getNumTypeObjects() - 1); |
| 569 | state.setCurrentChunkIndex(declarator.getNumTypeObjects()); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 572 | /// \brief Convert the specified declspec to the appropriate type |
| 573 | /// object. |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 574 | /// \param D the declarator containing the declaration specifier. |
Chris Lattner | 5153ee6 | 2009-04-25 08:47:54 +0000 | [diff] [blame] | 575 | /// \returns The type described by the declaration specifiers. This function |
| 576 | /// never returns null. |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 577 | static QualType ConvertDeclSpecToType(TypeProcessingState &state) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 578 | // FIXME: Should move the logic from DeclSpec::Finish to here for validity |
| 579 | // checking. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 580 | |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 581 | Sema &S = state.getSema(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 582 | Declarator &declarator = state.getDeclarator(); |
| 583 | const DeclSpec &DS = declarator.getDeclSpec(); |
| 584 | SourceLocation DeclLoc = declarator.getIdentifierLoc(); |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 585 | if (DeclLoc.isInvalid()) |
| 586 | DeclLoc = DS.getSourceRange().getBegin(); |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame] | 587 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 588 | ASTContext &Context = S.Context; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 589 | |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 590 | QualType Result; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 591 | switch (DS.getTypeSpecType()) { |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 592 | case DeclSpec::TST_void: |
| 593 | Result = Context.VoidTy; |
| 594 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 595 | case DeclSpec::TST_char: |
| 596 | if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified) |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 597 | Result = Context.CharTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 598 | else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 599 | Result = Context.SignedCharTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 600 | else { |
| 601 | assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned && |
| 602 | "Unknown TSS value"); |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 603 | Result = Context.UnsignedCharTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 604 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 605 | break; |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 606 | case DeclSpec::TST_wchar: |
| 607 | if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified) |
| 608 | Result = Context.WCharTy; |
| 609 | else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 610 | S.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec) |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 611 | << DS.getSpecifierName(DS.getTypeSpecType()); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 612 | Result = Context.getSignedWCharType(); |
| 613 | } else { |
| 614 | assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned && |
| 615 | "Unknown TSS value"); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 616 | S.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec) |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 617 | << DS.getSpecifierName(DS.getTypeSpecType()); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 618 | Result = Context.getUnsignedWCharType(); |
| 619 | } |
| 620 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 621 | case DeclSpec::TST_char16: |
| 622 | assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified && |
| 623 | "Unknown TSS value"); |
| 624 | Result = Context.Char16Ty; |
| 625 | break; |
| 626 | case DeclSpec::TST_char32: |
| 627 | assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified && |
| 628 | "Unknown TSS value"); |
| 629 | Result = Context.Char32Ty; |
| 630 | break; |
Chris Lattner | d658b56 | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 631 | case DeclSpec::TST_unspecified: |
Chris Lattner | 62f5f7f | 2008-07-26 00:46:50 +0000 | [diff] [blame] | 632 | // "<proto1,proto2>" is an objc qualified ID with a missing id. |
Chris Lattner | 097e916 | 2008-10-20 02:01:50 +0000 | [diff] [blame] | 633 | if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 634 | Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy, |
| 635 | (ObjCProtocolDecl**)PQ, |
| 636 | DS.getNumProtocolQualifiers()); |
| 637 | Result = Context.getObjCObjectPointerType(Result); |
Chris Lattner | 62f5f7f | 2008-07-26 00:46:50 +0000 | [diff] [blame] | 638 | break; |
| 639 | } |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 640 | |
| 641 | // If this is a missing declspec in a block literal return context, then it |
| 642 | // is inferred from the return statements inside the block. |
Eli Friedman | f88c400 | 2012-01-04 04:41:38 +0000 | [diff] [blame] | 643 | // The declspec is always missing in a lambda expr context; it is either |
| 644 | // specified with a trailing return type or inferred. |
| 645 | if (declarator.getContext() == Declarator::LambdaExprContext || |
| 646 | isOmittedBlockReturnType(declarator)) { |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 647 | Result = Context.DependentTy; |
| 648 | break; |
| 649 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 650 | |
Chris Lattner | d658b56 | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 651 | // Unspecified typespec defaults to int in C90. However, the C90 grammar |
| 652 | // [C90 6.5] only allows a decl-spec if there was *some* type-specifier, |
| 653 | // type-qualifier, or storage-class-specifier. If not, emit an extwarn. |
| 654 | // Note that the one exception to this is function definitions, which are |
| 655 | // allowed to be completely missing a declspec. This is handled in the |
| 656 | // parser already though by it pretending to have seen an 'int' in this |
| 657 | // case. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 658 | if (S.getLangOptions().ImplicitInt) { |
Chris Lattner | 35d276f | 2009-02-27 18:53:28 +0000 | [diff] [blame] | 659 | // In C89 mode, we only warn if there is a completely missing declspec |
| 660 | // when one is not allowed. |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 661 | if (DS.isEmpty()) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 662 | S.Diag(DeclLoc, diag::ext_missing_declspec) |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 663 | << DS.getSourceRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 664 | << FixItHint::CreateInsertion(DS.getSourceRange().getBegin(), "int"); |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 665 | } |
Douglas Gregor | 4310f4e | 2009-02-16 22:38:20 +0000 | [diff] [blame] | 666 | } else if (!DS.hasTypeSpecifier()) { |
Chris Lattner | d658b56 | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 667 | // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says: |
| 668 | // "At least one type specifier shall be given in the declaration |
| 669 | // specifiers in each declaration, and in the specifier-qualifier list in |
| 670 | // each struct declaration and type name." |
Douglas Gregor | 4310f4e | 2009-02-16 22:38:20 +0000 | [diff] [blame] | 671 | // FIXME: Does Microsoft really have the implicit int extension in C++? |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 672 | if (S.getLangOptions().CPlusPlus && |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 673 | !S.getLangOptions().MicrosoftExt) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 674 | S.Diag(DeclLoc, diag::err_missing_type_specifier) |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 675 | << DS.getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 676 | |
Chris Lattner | b78d833 | 2009-06-26 04:45:06 +0000 | [diff] [blame] | 677 | // When this occurs in C++ code, often something is very broken with the |
| 678 | // value being declared, poison it as invalid so we don't get chains of |
| 679 | // errors. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 680 | declarator.setInvalidType(true); |
Chris Lattner | b78d833 | 2009-06-26 04:45:06 +0000 | [diff] [blame] | 681 | } else { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 682 | S.Diag(DeclLoc, diag::ext_missing_type_specifier) |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 683 | << DS.getSourceRange(); |
Chris Lattner | b78d833 | 2009-06-26 04:45:06 +0000 | [diff] [blame] | 684 | } |
Chris Lattner | d658b56 | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 685 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 686 | |
| 687 | // FALL THROUGH. |
Chris Lattner | 3cbc38b | 2007-08-21 17:02:28 +0000 | [diff] [blame] | 688 | case DeclSpec::TST_int: { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 689 | if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) { |
| 690 | switch (DS.getTypeSpecWidth()) { |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 691 | case DeclSpec::TSW_unspecified: Result = Context.IntTy; break; |
| 692 | case DeclSpec::TSW_short: Result = Context.ShortTy; break; |
| 693 | case DeclSpec::TSW_long: Result = Context.LongTy; break; |
Chris Lattner | 311157f | 2009-10-25 18:25:04 +0000 | [diff] [blame] | 694 | case DeclSpec::TSW_longlong: |
| 695 | Result = Context.LongLongTy; |
| 696 | |
| 697 | // long long is a C99 feature. |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 698 | if (!S.getLangOptions().C99) |
| 699 | S.Diag(DS.getTypeSpecWidthLoc(), |
| 700 | S.getLangOptions().CPlusPlus0x ? |
| 701 | diag::warn_cxx98_compat_longlong : diag::ext_longlong); |
Chris Lattner | 311157f | 2009-10-25 18:25:04 +0000 | [diff] [blame] | 702 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 703 | } |
| 704 | } else { |
| 705 | switch (DS.getTypeSpecWidth()) { |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 706 | case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break; |
| 707 | case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break; |
| 708 | case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break; |
Chris Lattner | 311157f | 2009-10-25 18:25:04 +0000 | [diff] [blame] | 709 | case DeclSpec::TSW_longlong: |
| 710 | Result = Context.UnsignedLongLongTy; |
| 711 | |
| 712 | // long long is a C99 feature. |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 713 | if (!S.getLangOptions().C99) |
| 714 | S.Diag(DS.getTypeSpecWidthLoc(), |
| 715 | S.getLangOptions().CPlusPlus0x ? |
| 716 | diag::warn_cxx98_compat_longlong : diag::ext_longlong); |
Chris Lattner | 311157f | 2009-10-25 18:25:04 +0000 | [diff] [blame] | 717 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 718 | } |
| 719 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 720 | break; |
Chris Lattner | 3cbc38b | 2007-08-21 17:02:28 +0000 | [diff] [blame] | 721 | } |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 722 | case DeclSpec::TST_half: Result = Context.HalfTy; break; |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 723 | case DeclSpec::TST_float: Result = Context.FloatTy; break; |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 724 | case DeclSpec::TST_double: |
| 725 | if (DS.getTypeSpecWidth() == DeclSpec::TSW_long) |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 726 | Result = Context.LongDoubleTy; |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 727 | else |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 728 | Result = Context.DoubleTy; |
Peter Collingbourne | 39d3e7a | 2011-02-15 19:46:23 +0000 | [diff] [blame] | 729 | |
| 730 | if (S.getLangOptions().OpenCL && !S.getOpenCLOptions().cl_khr_fp64) { |
| 731 | S.Diag(DS.getTypeSpecTypeLoc(), diag::err_double_requires_fp64); |
| 732 | declarator.setInvalidType(true); |
| 733 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 734 | break; |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 735 | case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 736 | case DeclSpec::TST_decimal32: // _Decimal32 |
| 737 | case DeclSpec::TST_decimal64: // _Decimal64 |
| 738 | case DeclSpec::TST_decimal128: // _Decimal128 |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 739 | S.Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported); |
Chris Lattner | 8f12f65 | 2009-05-13 05:02:08 +0000 | [diff] [blame] | 740 | Result = Context.IntTy; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 741 | declarator.setInvalidType(true); |
Chris Lattner | 8f12f65 | 2009-05-13 05:02:08 +0000 | [diff] [blame] | 742 | break; |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 743 | case DeclSpec::TST_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 744 | case DeclSpec::TST_enum: |
| 745 | case DeclSpec::TST_union: |
| 746 | case DeclSpec::TST_struct: { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 747 | TypeDecl *D = dyn_cast_or_null<TypeDecl>(DS.getRepAsDecl()); |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 748 | if (!D) { |
| 749 | // This can happen in C++ with ambiguous lookups. |
| 750 | Result = Context.IntTy; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 751 | declarator.setInvalidType(true); |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 752 | break; |
| 753 | } |
| 754 | |
Chris Lattner | a64ef0a | 2009-10-25 22:09:09 +0000 | [diff] [blame] | 755 | // If the type is deprecated or unavailable, diagnose it. |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 756 | S.DiagnoseUseOfDecl(D, DS.getTypeSpecTypeNameLoc()); |
Chris Lattner | a64ef0a | 2009-10-25 22:09:09 +0000 | [diff] [blame] | 757 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 758 | assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 && |
Chris Lattner | a64ef0a | 2009-10-25 22:09:09 +0000 | [diff] [blame] | 759 | DS.getTypeSpecSign() == 0 && "No qualifiers on tag names!"); |
| 760 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 761 | // TypeQuals handled by caller. |
Chris Lattner | a64ef0a | 2009-10-25 22:09:09 +0000 | [diff] [blame] | 762 | Result = Context.getTypeDeclType(D); |
John McCall | 2191b20 | 2009-09-05 06:31:47 +0000 | [diff] [blame] | 763 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 764 | // In both C and C++, make an ElaboratedType. |
| 765 | ElaboratedTypeKeyword Keyword |
| 766 | = ElaboratedType::getKeywordForTypeSpec(DS.getTypeSpecType()); |
| 767 | Result = S.getElaboratedType(Keyword, DS.getTypeSpecScope(), Result); |
| 768 | |
Chris Lattner | 5153ee6 | 2009-04-25 08:47:54 +0000 | [diff] [blame] | 769 | if (D->isInvalidDecl()) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 770 | declarator.setInvalidType(true); |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 771 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 772 | } |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 773 | case DeclSpec::TST_typename: { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 774 | assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 && |
| 775 | DS.getTypeSpecSign() == 0 && |
| 776 | "Can't handle qualifiers on typedef names yet!"); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 777 | Result = S.GetTypeFromParser(DS.getRepAsType()); |
John McCall | 27940d2 | 2010-07-30 05:17:22 +0000 | [diff] [blame] | 778 | if (Result.isNull()) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 779 | declarator.setInvalidType(true); |
John McCall | 27940d2 | 2010-07-30 05:17:22 +0000 | [diff] [blame] | 780 | else if (DeclSpec::ProtocolQualifierListTy PQ |
| 781 | = DS.getProtocolQualifiers()) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 782 | if (const ObjCObjectType *ObjT = Result->getAs<ObjCObjectType>()) { |
| 783 | // Silently drop any existing protocol qualifiers. |
| 784 | // TODO: determine whether that's the right thing to do. |
| 785 | if (ObjT->getNumProtocols()) |
| 786 | Result = ObjT->getBaseType(); |
| 787 | |
| 788 | if (DS.getNumProtocolQualifiers()) |
| 789 | Result = Context.getObjCObjectType(Result, |
| 790 | (ObjCProtocolDecl**) PQ, |
| 791 | DS.getNumProtocolQualifiers()); |
| 792 | } else if (Result->isObjCIdType()) { |
Chris Lattner | ae4da61 | 2008-07-26 01:53:50 +0000 | [diff] [blame] | 793 | // id<protocol-list> |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 794 | Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy, |
| 795 | (ObjCProtocolDecl**) PQ, |
| 796 | DS.getNumProtocolQualifiers()); |
| 797 | Result = Context.getObjCObjectPointerType(Result); |
| 798 | } else if (Result->isObjCClassType()) { |
Steve Naroff | 4262a07 | 2009-02-23 18:53:24 +0000 | [diff] [blame] | 799 | // Class<protocol-list> |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 800 | Result = Context.getObjCObjectType(Context.ObjCBuiltinClassTy, |
| 801 | (ObjCProtocolDecl**) PQ, |
| 802 | DS.getNumProtocolQualifiers()); |
| 803 | Result = Context.getObjCObjectPointerType(Result); |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 804 | } else { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 805 | S.Diag(DeclLoc, diag::err_invalid_protocol_qualifiers) |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 806 | << DS.getSourceRange(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 807 | declarator.setInvalidType(true); |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 808 | } |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 809 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 810 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 811 | // TypeQuals handled by caller. |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 812 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 813 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 814 | case DeclSpec::TST_typeofType: |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 815 | // FIXME: Preserve type source info. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 816 | Result = S.GetTypeFromParser(DS.getRepAsType()); |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 817 | assert(!Result.isNull() && "Didn't get a type for typeof?"); |
Fariborz Jahanian | 730e175 | 2010-10-06 17:00:02 +0000 | [diff] [blame] | 818 | if (!Result->isDependentType()) |
| 819 | if (const TagType *TT = Result->getAs<TagType>()) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 820 | S.DiagnoseUseOfDecl(TT->getDecl(), DS.getTypeSpecTypeLoc()); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 821 | // TypeQuals handled by caller. |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 822 | Result = Context.getTypeOfType(Result); |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 823 | break; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 824 | case DeclSpec::TST_typeofExpr: { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 825 | Expr *E = DS.getRepAsExpr(); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 826 | assert(E && "Didn't get an expression for typeof?"); |
| 827 | // TypeQuals handled by caller. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 828 | Result = S.BuildTypeofExprType(E, DS.getTypeSpecTypeLoc()); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 829 | if (Result.isNull()) { |
| 830 | Result = Context.IntTy; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 831 | declarator.setInvalidType(true); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 832 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 833 | break; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 834 | } |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 835 | case DeclSpec::TST_decltype: { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 836 | Expr *E = DS.getRepAsExpr(); |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 837 | assert(E && "Didn't get an expression for decltype?"); |
| 838 | // TypeQuals handled by caller. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 839 | Result = S.BuildDecltypeType(E, DS.getTypeSpecTypeLoc()); |
Anders Carlsson | af017e6 | 2009-06-29 22:58:55 +0000 | [diff] [blame] | 840 | if (Result.isNull()) { |
| 841 | Result = Context.IntTy; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 842 | declarator.setInvalidType(true); |
Anders Carlsson | af017e6 | 2009-06-29 22:58:55 +0000 | [diff] [blame] | 843 | } |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 844 | break; |
| 845 | } |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 846 | case DeclSpec::TST_underlyingType: |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 847 | Result = S.GetTypeFromParser(DS.getRepAsType()); |
| 848 | assert(!Result.isNull() && "Didn't get a type for __underlying_type?"); |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 849 | Result = S.BuildUnaryTransformType(Result, |
| 850 | UnaryTransformType::EnumUnderlyingType, |
| 851 | DS.getTypeSpecTypeLoc()); |
| 852 | if (Result.isNull()) { |
| 853 | Result = Context.IntTy; |
| 854 | declarator.setInvalidType(true); |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 855 | } |
| 856 | break; |
| 857 | |
Anders Carlsson | e89d159 | 2009-06-26 18:41:36 +0000 | [diff] [blame] | 858 | case DeclSpec::TST_auto: { |
| 859 | // TypeQuals handled by caller. |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 860 | Result = Context.getAutoType(QualType()); |
Anders Carlsson | e89d159 | 2009-06-26 18:41:36 +0000 | [diff] [blame] | 861 | break; |
| 862 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 863 | |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 864 | case DeclSpec::TST_unknown_anytype: |
| 865 | Result = Context.UnknownAnyTy; |
| 866 | break; |
| 867 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 868 | case DeclSpec::TST_atomic: |
| 869 | Result = S.GetTypeFromParser(DS.getRepAsType()); |
| 870 | assert(!Result.isNull() && "Didn't get a type for _Atomic?"); |
| 871 | Result = S.BuildAtomicType(Result, DS.getTypeSpecTypeLoc()); |
| 872 | if (Result.isNull()) { |
| 873 | Result = Context.IntTy; |
| 874 | declarator.setInvalidType(true); |
| 875 | } |
| 876 | break; |
| 877 | |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 878 | case DeclSpec::TST_error: |
Chris Lattner | 5153ee6 | 2009-04-25 08:47:54 +0000 | [diff] [blame] | 879 | Result = Context.IntTy; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 880 | declarator.setInvalidType(true); |
Chris Lattner | 5153ee6 | 2009-04-25 08:47:54 +0000 | [diff] [blame] | 881 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 882 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 883 | |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 884 | // Handle complex types. |
Douglas Gregor | f244cd7 | 2009-02-14 21:06:05 +0000 | [diff] [blame] | 885 | if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 886 | if (S.getLangOptions().Freestanding) |
| 887 | S.Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex); |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 888 | Result = Context.getComplexType(Result); |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 889 | } else if (DS.isTypeAltiVecVector()) { |
| 890 | unsigned typeSize = static_cast<unsigned>(Context.getTypeSize(Result)); |
| 891 | 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] | 892 | VectorType::VectorKind VecKind = VectorType::AltiVecVector; |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 893 | if (DS.isTypeAltiVecPixel()) |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 894 | VecKind = VectorType::AltiVecPixel; |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 895 | else if (DS.isTypeAltiVecBool()) |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 896 | VecKind = VectorType::AltiVecBool; |
| 897 | Result = Context.getVectorType(Result, 128/typeSize, VecKind); |
Douglas Gregor | f244cd7 | 2009-02-14 21:06:05 +0000 | [diff] [blame] | 898 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 899 | |
Argyrios Kyrtzidis | 47423bd | 2010-09-23 09:40:31 +0000 | [diff] [blame] | 900 | // FIXME: Imaginary. |
| 901 | if (DS.getTypeSpecComplex() == DeclSpec::TSC_imaginary) |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 902 | S.Diag(DS.getTypeSpecComplexLoc(), diag::err_imaginary_not_supported); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 903 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 904 | // Before we process any type attributes, synthesize a block literal |
| 905 | // function declarator if necessary. |
| 906 | if (declarator.getContext() == Declarator::BlockLiteralContext) |
| 907 | maybeSynthesizeBlockSignature(state, Result); |
| 908 | |
| 909 | // Apply any type attributes from the decl spec. This may cause the |
| 910 | // list of type attributes to be temporarily saved while the type |
| 911 | // attributes are pushed around. |
| 912 | if (AttributeList *attrs = DS.getAttributes().getList()) |
| 913 | processTypeAttrs(state, Result, true, attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 914 | |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 915 | // Apply const/volatile/restrict qualifiers to T. |
| 916 | if (unsigned TypeQuals = DS.getTypeQualifiers()) { |
| 917 | |
| 918 | // Enforce C99 6.7.3p2: "Types other than pointer types derived from object |
| 919 | // or incomplete types shall not be restrict-qualified." C++ also allows |
| 920 | // restrict-qualified references. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 921 | if (TypeQuals & DeclSpec::TQ_restrict) { |
Fariborz Jahanian | 2b5ff1a | 2009-12-07 18:08:58 +0000 | [diff] [blame] | 922 | if (Result->isAnyPointerType() || Result->isReferenceType()) { |
| 923 | QualType EltTy; |
| 924 | if (Result->isObjCObjectPointerType()) |
| 925 | EltTy = Result; |
| 926 | else |
| 927 | EltTy = Result->isPointerType() ? |
| 928 | Result->getAs<PointerType>()->getPointeeType() : |
| 929 | Result->getAs<ReferenceType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 930 | |
Douglas Gregor | bad0e65 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 931 | // 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] | 932 | // incomplete type. |
| 933 | if (!EltTy->isIncompleteOrObjectType()) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 934 | S.Diag(DS.getRestrictSpecLoc(), |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 935 | diag::err_typecheck_invalid_restrict_invalid_pointee) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 936 | << EltTy << DS.getSourceRange(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 937 | TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier. |
Chris Lattner | bdcd637 | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 938 | } |
| 939 | } else { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 940 | S.Diag(DS.getRestrictSpecLoc(), |
| 941 | diag::err_typecheck_invalid_restrict_not_pointer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 942 | << Result << DS.getSourceRange(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 943 | TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier. |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 944 | } |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 945 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 946 | |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 947 | // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification |
| 948 | // of a function type includes any type qualifiers, the behavior is |
| 949 | // undefined." |
| 950 | if (Result->isFunctionType() && TypeQuals) { |
| 951 | // Get some location to point at, either the C or V location. |
| 952 | SourceLocation Loc; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 953 | if (TypeQuals & DeclSpec::TQ_const) |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 954 | Loc = DS.getConstSpecLoc(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 955 | else if (TypeQuals & DeclSpec::TQ_volatile) |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 956 | Loc = DS.getVolatileSpecLoc(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 957 | else { |
| 958 | assert((TypeQuals & DeclSpec::TQ_restrict) && |
| 959 | "Has CVR quals but not C, V, or R?"); |
| 960 | Loc = DS.getRestrictSpecLoc(); |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 961 | } |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 962 | S.Diag(Loc, diag::warn_typecheck_function_qualifiers) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 963 | << Result << DS.getSourceRange(); |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 964 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 965 | |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 966 | // C++ [dcl.ref]p1: |
| 967 | // Cv-qualified references are ill-formed except when the |
| 968 | // cv-qualifiers are introduced through the use of a typedef |
| 969 | // (7.1.3) or of a template type argument (14.3), in which |
| 970 | // case the cv-qualifiers are ignored. |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 971 | // FIXME: Shouldn't we be checking SCS_typedef here? |
| 972 | if (DS.getTypeSpecType() == DeclSpec::TST_typename && |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 973 | TypeQuals && Result->isReferenceType()) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 974 | TypeQuals &= ~DeclSpec::TQ_const; |
| 975 | TypeQuals &= ~DeclSpec::TQ_volatile; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 976 | } |
| 977 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 978 | Qualifiers Quals = Qualifiers::fromCVRMask(TypeQuals); |
| 979 | Result = Context.getQualifiedType(Result, Quals); |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 980 | } |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 981 | |
Chris Lattner | f1d705c | 2008-02-21 01:07:18 +0000 | [diff] [blame] | 982 | return Result; |
| 983 | } |
| 984 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 985 | static std::string getPrintableNameForEntity(DeclarationName Entity) { |
| 986 | if (Entity) |
| 987 | return Entity.getAsString(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 988 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 989 | return "type name"; |
| 990 | } |
| 991 | |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 992 | QualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc, |
| 993 | Qualifiers Qs) { |
| 994 | // Enforce C99 6.7.3p2: "Types other than pointer types derived from |
| 995 | // object or incomplete types shall not be restrict-qualified." |
| 996 | if (Qs.hasRestrict()) { |
| 997 | unsigned DiagID = 0; |
| 998 | QualType ProblemTy; |
| 999 | |
| 1000 | const Type *Ty = T->getCanonicalTypeInternal().getTypePtr(); |
| 1001 | if (const ReferenceType *RTy = dyn_cast<ReferenceType>(Ty)) { |
| 1002 | if (!RTy->getPointeeType()->isIncompleteOrObjectType()) { |
| 1003 | DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee; |
| 1004 | ProblemTy = T->getAs<ReferenceType>()->getPointeeType(); |
| 1005 | } |
| 1006 | } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) { |
| 1007 | if (!PTy->getPointeeType()->isIncompleteOrObjectType()) { |
| 1008 | DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee; |
| 1009 | ProblemTy = T->getAs<PointerType>()->getPointeeType(); |
| 1010 | } |
| 1011 | } else if (const MemberPointerType *PTy = dyn_cast<MemberPointerType>(Ty)) { |
| 1012 | if (!PTy->getPointeeType()->isIncompleteOrObjectType()) { |
| 1013 | DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee; |
| 1014 | ProblemTy = T->getAs<PointerType>()->getPointeeType(); |
| 1015 | } |
| 1016 | } else if (!Ty->isDependentType()) { |
| 1017 | // FIXME: this deserves a proper diagnostic |
| 1018 | DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee; |
| 1019 | ProblemTy = T; |
| 1020 | } |
| 1021 | |
| 1022 | if (DiagID) { |
| 1023 | Diag(Loc, DiagID) << ProblemTy; |
| 1024 | Qs.removeRestrict(); |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | return Context.getQualifiedType(T, Qs); |
| 1029 | } |
| 1030 | |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 1031 | /// \brief Build a paren type including \p T. |
| 1032 | QualType Sema::BuildParenType(QualType T) { |
| 1033 | return Context.getParenType(T); |
| 1034 | } |
| 1035 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1036 | /// Given that we're building a pointer or reference to the given |
| 1037 | static QualType inferARCLifetimeForPointee(Sema &S, QualType type, |
| 1038 | SourceLocation loc, |
| 1039 | bool isReference) { |
| 1040 | // Bail out if retention is unrequired or already specified. |
| 1041 | if (!type->isObjCLifetimeType() || |
| 1042 | type.getObjCLifetime() != Qualifiers::OCL_None) |
| 1043 | return type; |
| 1044 | |
| 1045 | Qualifiers::ObjCLifetime implicitLifetime = Qualifiers::OCL_None; |
| 1046 | |
| 1047 | // If the object type is const-qualified, we can safely use |
| 1048 | // __unsafe_unretained. This is safe (because there are no read |
| 1049 | // barriers), and it'll be safe to coerce anything but __weak* to |
| 1050 | // the resulting type. |
| 1051 | if (type.isConstQualified()) { |
| 1052 | implicitLifetime = Qualifiers::OCL_ExplicitNone; |
| 1053 | |
| 1054 | // Otherwise, check whether the static type does not require |
| 1055 | // retaining. This currently only triggers for Class (possibly |
| 1056 | // protocol-qualifed, and arrays thereof). |
| 1057 | } else if (type->isObjCARCImplicitlyUnretainedType()) { |
| 1058 | implicitLifetime = Qualifiers::OCL_ExplicitNone; |
| 1059 | |
Eli Friedman | ef331b7 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 1060 | // If we are in an unevaluated context, like sizeof, skip adding a |
| 1061 | // qualification. |
Eli Friedman | 78a5424 | 2012-01-21 04:44:06 +0000 | [diff] [blame] | 1062 | } else if (S.ExprEvalContexts.back().Context == Sema::Unevaluated) { |
Eli Friedman | ef331b7 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 1063 | return type; |
Argyrios Kyrtzidis | 5b76f37 | 2011-09-20 23:49:22 +0000 | [diff] [blame] | 1064 | |
John McCall | e8c904f | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 1065 | // If that failed, give an error and recover using __strong. __strong |
| 1066 | // is the option most likely to prevent spurious second-order diagnostics, |
| 1067 | // like when binding a reference to a field. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1068 | } else { |
| 1069 | // These types can show up in private ivars in system headers, so |
| 1070 | // we need this to not be an error in those cases. Instead we |
| 1071 | // want to delay. |
| 1072 | if (S.DelayedDiagnostics.shouldDelayDiagnostics()) { |
Eli Friedman | ef331b7 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 1073 | S.DelayedDiagnostics.add( |
| 1074 | sema::DelayedDiagnostic::makeForbiddenType(loc, |
| 1075 | diag::err_arc_indirect_no_ownership, type, isReference)); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1076 | } else { |
Eli Friedman | ef331b7 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 1077 | S.Diag(loc, diag::err_arc_indirect_no_ownership) << type << isReference; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1078 | } |
John McCall | e8c904f | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 1079 | implicitLifetime = Qualifiers::OCL_Strong; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1080 | } |
| 1081 | assert(implicitLifetime && "didn't infer any lifetime!"); |
| 1082 | |
| 1083 | Qualifiers qs; |
| 1084 | qs.addObjCLifetime(implicitLifetime); |
| 1085 | return S.Context.getQualifiedType(type, qs); |
| 1086 | } |
| 1087 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1088 | /// \brief Build a pointer type. |
| 1089 | /// |
| 1090 | /// \param T The type to which we'll be building a pointer. |
| 1091 | /// |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1092 | /// \param Loc The location of the entity whose type involves this |
| 1093 | /// pointer type or, if there is no such entity, the location of the |
| 1094 | /// type that will have pointer type. |
| 1095 | /// |
| 1096 | /// \param Entity The name of the entity that involves the pointer |
| 1097 | /// type, if known. |
| 1098 | /// |
| 1099 | /// \returns A suitable pointer type, if there are no |
| 1100 | /// errors. Otherwise, returns a NULL type. |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1101 | QualType Sema::BuildPointerType(QualType T, |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1102 | SourceLocation Loc, DeclarationName Entity) { |
| 1103 | if (T->isReferenceType()) { |
| 1104 | // C++ 8.3.2p4: There shall be no ... pointers to references ... |
| 1105 | Diag(Loc, diag::err_illegal_decl_pointer_to_reference) |
John McCall | ac40605 | 2009-10-30 00:37:20 +0000 | [diff] [blame] | 1106 | << getPrintableNameForEntity(Entity) << T; |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1107 | return QualType(); |
| 1108 | } |
| 1109 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1110 | assert(!T->isObjCObjectType() && "Should build ObjCObjectPointerType"); |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 1111 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1112 | // In ARC, it is forbidden to build pointers to unqualified pointers. |
| 1113 | if (getLangOptions().ObjCAutoRefCount) |
| 1114 | T = inferARCLifetimeForPointee(*this, T, Loc, /*reference*/ false); |
| 1115 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1116 | // Build the pointer type. |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1117 | return Context.getPointerType(T); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | /// \brief Build a reference type. |
| 1121 | /// |
| 1122 | /// \param T The type to which we'll be building a reference. |
| 1123 | /// |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1124 | /// \param Loc The location of the entity whose type involves this |
| 1125 | /// reference type or, if there is no such entity, the location of the |
| 1126 | /// type that will have reference type. |
| 1127 | /// |
| 1128 | /// \param Entity The name of the entity that involves the reference |
| 1129 | /// type, if known. |
| 1130 | /// |
| 1131 | /// \returns A suitable reference type, if there are no |
| 1132 | /// errors. Otherwise, returns a NULL type. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1133 | QualType Sema::BuildReferenceType(QualType T, bool SpelledAsLValue, |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1134 | SourceLocation Loc, |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1135 | DeclarationName Entity) { |
Douglas Gregor | 9625e44 | 2011-05-21 22:16:50 +0000 | [diff] [blame] | 1136 | assert(Context.getCanonicalType(T) != Context.OverloadTy && |
| 1137 | "Unresolved overloaded function type"); |
| 1138 | |
Douglas Gregor | 69d8316 | 2011-01-20 16:08:06 +0000 | [diff] [blame] | 1139 | // C++0x [dcl.ref]p6: |
| 1140 | // If a typedef (7.1.3), a type template-parameter (14.3.1), or a |
| 1141 | // decltype-specifier (7.1.6.2) denotes a type TR that is a reference to a |
| 1142 | // type T, an attempt to create the type "lvalue reference to cv TR" creates |
| 1143 | // the type "lvalue reference to T", while an attempt to create the type |
| 1144 | // "rvalue reference to cv TR" creates the type TR. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1145 | bool LValueRef = SpelledAsLValue || T->getAs<LValueReferenceType>(); |
| 1146 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1147 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 1148 | // |
| 1149 | // According to C++ DR 106, references to references are only |
| 1150 | // diagnosed when they are written directly (e.g., "int & &"), |
| 1151 | // but not when they happen via a typedef: |
| 1152 | // |
| 1153 | // typedef int& intref; |
| 1154 | // typedef intref& intref2; |
| 1155 | // |
| 1156 | // Parser::ParseDeclaratorInternal diagnoses the case where |
| 1157 | // references are written directly; here, we handle the |
Douglas Gregor | 69d8316 | 2011-01-20 16:08:06 +0000 | [diff] [blame] | 1158 | // collapsing of references-to-references as described in C++0x. |
| 1159 | // DR 106 and 540 introduce reference-collapsing into C++98/03. |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1160 | |
| 1161 | // C++ [dcl.ref]p1: |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 1162 | // A declarator that specifies the type "reference to cv void" |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1163 | // is ill-formed. |
| 1164 | if (T->isVoidType()) { |
| 1165 | Diag(Loc, diag::err_reference_to_void); |
| 1166 | return QualType(); |
| 1167 | } |
| 1168 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1169 | // In ARC, it is forbidden to build references to unqualified pointers. |
| 1170 | if (getLangOptions().ObjCAutoRefCount) |
| 1171 | T = inferARCLifetimeForPointee(*this, T, Loc, /*reference*/ true); |
| 1172 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1173 | // Handle restrict on references. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1174 | if (LValueRef) |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1175 | return Context.getLValueReferenceType(T, SpelledAsLValue); |
| 1176 | return Context.getRValueReferenceType(T); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
Chris Lattner | e1eed38 | 2011-06-14 06:38:10 +0000 | [diff] [blame] | 1179 | /// Check whether the specified array size makes the array type a VLA. If so, |
| 1180 | /// return true, if not, return the size of the array in SizeVal. |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 1181 | static bool isArraySizeVLA(Sema &S, Expr *ArraySize, llvm::APSInt &SizeVal) { |
| 1182 | // If the size is an ICE, it certainly isn't a VLA. If we're in a GNU mode |
| 1183 | // (like gnu99, but not c99) accept any evaluatable value as an extension. |
| 1184 | return S.VerifyIntegerConstantExpression( |
| 1185 | ArraySize, &SizeVal, S.PDiag(), S.LangOpts.GNUMode, |
| 1186 | S.PDiag(diag::ext_vla_folded_to_constant)).isInvalid(); |
Chris Lattner | e1eed38 | 2011-06-14 06:38:10 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1190 | /// \brief Build an array type. |
| 1191 | /// |
| 1192 | /// \param T The type of each element in the array. |
| 1193 | /// |
| 1194 | /// \param ASM C99 array size modifier (e.g., '*', 'static'). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1195 | /// |
| 1196 | /// \param ArraySize Expression describing the size of the array. |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1197 | /// |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1198 | /// \param Loc The location of the entity whose type involves this |
| 1199 | /// array type or, if there is no such entity, the location of the |
| 1200 | /// type that will have array type. |
| 1201 | /// |
| 1202 | /// \param Entity The name of the entity that involves the array |
| 1203 | /// type, if known. |
| 1204 | /// |
| 1205 | /// \returns A suitable array type, if there are no errors. Otherwise, |
| 1206 | /// returns a NULL type. |
| 1207 | QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM, |
| 1208 | Expr *ArraySize, unsigned Quals, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1209 | SourceRange Brackets, DeclarationName Entity) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1210 | |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1211 | SourceLocation Loc = Brackets.getBegin(); |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1212 | if (getLangOptions().CPlusPlus) { |
Douglas Gregor | 138bb23 | 2010-04-27 19:38:14 +0000 | [diff] [blame] | 1213 | // C++ [dcl.array]p1: |
| 1214 | // T is called the array element type; this type shall not be a reference |
| 1215 | // type, the (possibly cv-qualified) type void, a function type or an |
| 1216 | // abstract class type. |
| 1217 | // |
| 1218 | // Note: function types are handled in the common path with C. |
| 1219 | if (T->isReferenceType()) { |
| 1220 | Diag(Loc, diag::err_illegal_decl_array_of_references) |
| 1221 | << getPrintableNameForEntity(Entity) << T; |
| 1222 | return QualType(); |
| 1223 | } |
| 1224 | |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1225 | if (T->isVoidType()) { |
| 1226 | Diag(Loc, diag::err_illegal_decl_array_incomplete_type) << T; |
| 1227 | return QualType(); |
| 1228 | } |
Douglas Gregor | 138bb23 | 2010-04-27 19:38:14 +0000 | [diff] [blame] | 1229 | |
| 1230 | if (RequireNonAbstractType(Brackets.getBegin(), T, |
| 1231 | diag::err_array_of_abstract_type)) |
| 1232 | return QualType(); |
| 1233 | |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1234 | } else { |
Douglas Gregor | 138bb23 | 2010-04-27 19:38:14 +0000 | [diff] [blame] | 1235 | // C99 6.7.5.2p1: If the element type is an incomplete or function type, |
| 1236 | // 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] | 1237 | if (RequireCompleteType(Loc, T, |
| 1238 | diag::err_illegal_decl_array_incomplete_type)) |
| 1239 | return QualType(); |
| 1240 | } |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1241 | |
| 1242 | if (T->isFunctionType()) { |
| 1243 | Diag(Loc, diag::err_illegal_decl_array_of_functions) |
John McCall | ac40605 | 2009-10-30 00:37:20 +0000 | [diff] [blame] | 1244 | << getPrintableNameForEntity(Entity) << T; |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1245 | return QualType(); |
| 1246 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1247 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1248 | if (T->getContainedAutoType()) { |
| 1249 | Diag(Loc, diag::err_illegal_decl_array_of_auto) |
| 1250 | << getPrintableNameForEntity(Entity) << T; |
Anders Carlsson | e7cf07d | 2009-06-26 19:33:28 +0000 | [diff] [blame] | 1251 | return QualType(); |
| 1252 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1253 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1254 | if (const RecordType *EltTy = T->getAs<RecordType>()) { |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1255 | // If the element type is a struct or union that contains a variadic |
| 1256 | // array, accept it as a GNU extension: C99 6.7.2.1p2. |
| 1257 | if (EltTy->getDecl()->hasFlexibleArrayMember()) |
| 1258 | Diag(Loc, diag::ext_flexible_array_in_array) << T; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1259 | } else if (T->isObjCObjectType()) { |
Chris Lattner | c7c11b1 | 2009-04-27 01:55:56 +0000 | [diff] [blame] | 1260 | Diag(Loc, diag::err_objc_array_of_interfaces) << T; |
| 1261 | return QualType(); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1262 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1263 | |
John McCall | 806054d | 2012-01-11 00:14:46 +0000 | [diff] [blame] | 1264 | // Do placeholder conversions on the array size expression. |
| 1265 | if (ArraySize && ArraySize->hasPlaceholderType()) { |
| 1266 | ExprResult Result = CheckPlaceholderExpr(ArraySize); |
| 1267 | if (Result.isInvalid()) return QualType(); |
| 1268 | ArraySize = Result.take(); |
| 1269 | } |
| 1270 | |
John McCall | 5e3c67b | 2010-12-15 04:42:30 +0000 | [diff] [blame] | 1271 | // Do lvalue-to-rvalue conversions on the array size expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1272 | if (ArraySize && !ArraySize->isRValue()) { |
| 1273 | ExprResult Result = DefaultLvalueConversion(ArraySize); |
| 1274 | if (Result.isInvalid()) |
| 1275 | return QualType(); |
| 1276 | |
| 1277 | ArraySize = Result.take(); |
| 1278 | } |
John McCall | 5e3c67b | 2010-12-15 04:42:30 +0000 | [diff] [blame] | 1279 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1280 | // C99 6.7.5.2p1: The size expression shall have integer type. |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 1281 | // C++11 allows contextual conversions to such types. |
| 1282 | if (!getLangOptions().CPlusPlus0x && |
| 1283 | ArraySize && !ArraySize->isTypeDependent() && |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1284 | !ArraySize->getType()->isIntegralOrUnscopedEnumerationType()) { |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1285 | Diag(ArraySize->getLocStart(), diag::err_array_size_non_int) |
| 1286 | << ArraySize->getType() << ArraySize->getSourceRange(); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1287 | return QualType(); |
| 1288 | } |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 1289 | |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 1290 | llvm::APSInt ConstVal(Context.getTypeSize(Context.getSizeType())); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1291 | if (!ArraySize) { |
Eli Friedman | f91f5c8 | 2009-04-26 21:57:51 +0000 | [diff] [blame] | 1292 | if (ASM == ArrayType::Star) |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1293 | T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets); |
Eli Friedman | f91f5c8 | 2009-04-26 21:57:51 +0000 | [diff] [blame] | 1294 | else |
| 1295 | T = Context.getIncompleteArrayType(T, ASM, Quals); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1296 | } else if (ArraySize->isTypeDependent() || ArraySize->isValueDependent()) { |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1297 | T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 1298 | } else if ((!T->isDependentType() && !T->isIncompleteType() && |
| 1299 | !T->isConstantSizeType()) || |
| 1300 | isArraySizeVLA(*this, ArraySize, ConstVal)) { |
| 1301 | // Even in C++11, don't allow contextual conversions in the array bound |
| 1302 | // of a VLA. |
| 1303 | if (getLangOptions().CPlusPlus0x && |
| 1304 | !ArraySize->getType()->isIntegralOrUnscopedEnumerationType()) { |
| 1305 | Diag(ArraySize->getLocStart(), diag::err_array_size_non_int) |
| 1306 | << ArraySize->getType() << ArraySize->getSourceRange(); |
| 1307 | return QualType(); |
| 1308 | } |
| 1309 | |
Chris Lattner | e1eed38 | 2011-06-14 06:38:10 +0000 | [diff] [blame] | 1310 | // 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] | 1311 | // C99: an array with a non-ICE size is a VLA. We accept any expression |
| 1312 | // 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] | 1313 | T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1314 | } else { |
| 1315 | // C99 6.7.5.2p1: If the expression is a constant expression, it shall |
| 1316 | // have a value greater than zero. |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1317 | if (ConstVal.isSigned() && ConstVal.isNegative()) { |
Chandler Carruth | b2b5cc0 | 2011-01-04 04:44:35 +0000 | [diff] [blame] | 1318 | if (Entity) |
| 1319 | Diag(ArraySize->getLocStart(), diag::err_decl_negative_array_size) |
| 1320 | << getPrintableNameForEntity(Entity) << ArraySize->getSourceRange(); |
| 1321 | else |
| 1322 | Diag(ArraySize->getLocStart(), diag::err_typecheck_negative_array_size) |
| 1323 | << ArraySize->getSourceRange(); |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1324 | return QualType(); |
| 1325 | } |
| 1326 | if (ConstVal == 0) { |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 1327 | // GCC accepts zero sized static arrays. We allow them when |
| 1328 | // we're not in a SFINAE context. |
| 1329 | Diag(ArraySize->getLocStart(), |
| 1330 | isSFINAEContext()? diag::err_typecheck_zero_array_size |
| 1331 | : diag::ext_typecheck_zero_array_size) |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1332 | << ArraySize->getSourceRange(); |
Peter Collingbourne | 20cdbeb | 2011-10-16 21:17:32 +0000 | [diff] [blame] | 1333 | |
| 1334 | if (ASM == ArrayType::Static) { |
| 1335 | Diag(ArraySize->getLocStart(), |
| 1336 | diag::warn_typecheck_zero_static_array_size) |
| 1337 | << ArraySize->getSourceRange(); |
| 1338 | ASM = ArrayType::Normal; |
| 1339 | } |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 1340 | } else if (!T->isDependentType() && !T->isVariablyModifiedType() && |
| 1341 | !T->isIncompleteType()) { |
| 1342 | // Is the array too large? |
| 1343 | unsigned ActiveSizeBits |
| 1344 | = ConstantArrayType::getNumAddressingBits(Context, T, ConstVal); |
| 1345 | if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context)) |
| 1346 | Diag(ArraySize->getLocStart(), diag::err_array_too_large) |
| 1347 | << ConstVal.toString(10) |
| 1348 | << ArraySize->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1349 | } |
Douglas Gregor | 2767ce2 | 2010-08-18 00:39:00 +0000 | [diff] [blame] | 1350 | |
John McCall | 46a617a | 2009-10-16 00:14:28 +0000 | [diff] [blame] | 1351 | T = Context.getConstantArrayType(T, ConstVal, ASM, Quals); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1352 | } |
David Chisnall | af40776 | 2010-01-11 23:08:08 +0000 | [diff] [blame] | 1353 | // If this is not C99, extwarn about VLA's and C99 array size modifiers. |
| 1354 | if (!getLangOptions().C99) { |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 1355 | if (T->isVariableArrayType()) { |
| 1356 | // Prohibit the use of non-POD types in VLAs. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1357 | QualType BaseT = Context.getBaseElementType(T); |
Douglas Gregor | 204ce17 | 2010-05-24 20:42:30 +0000 | [diff] [blame] | 1358 | if (!T->isDependentType() && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1359 | !BaseT.isPODType(Context) && |
| 1360 | !BaseT->isObjCLifetimeType()) { |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 1361 | Diag(Loc, diag::err_vla_non_pod) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1362 | << BaseT; |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 1363 | return QualType(); |
| 1364 | } |
Douglas Gregor | a481ec4 | 2010-05-23 19:57:01 +0000 | [diff] [blame] | 1365 | // Prohibit the use of VLAs during template argument deduction. |
| 1366 | else if (isSFINAEContext()) { |
| 1367 | Diag(Loc, diag::err_vla_in_sfinae); |
| 1368 | return QualType(); |
| 1369 | } |
Douglas Gregor | 0fddb97 | 2010-05-22 16:17:30 +0000 | [diff] [blame] | 1370 | // Just extwarn about VLAs. |
| 1371 | else |
| 1372 | Diag(Loc, diag::ext_vla); |
| 1373 | } else if (ASM != ArrayType::Normal || Quals != 0) |
Richard Smith | d7c56e1 | 2011-12-29 21:57:33 +0000 | [diff] [blame] | 1374 | Diag(Loc, |
Douglas Gregor | 043cad2 | 2009-09-11 00:18:58 +0000 | [diff] [blame] | 1375 | getLangOptions().CPlusPlus? diag::err_c99_array_usage_cxx |
Richard Smith | d7c56e1 | 2011-12-29 21:57:33 +0000 | [diff] [blame] | 1376 | : diag::ext_c99_array_usage) << ASM; |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
| 1379 | return T; |
| 1380 | } |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1381 | |
| 1382 | /// \brief Build an ext-vector type. |
| 1383 | /// |
| 1384 | /// Run the required checks for the extended vector type. |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1385 | QualType Sema::BuildExtVectorType(QualType T, Expr *ArraySize, |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1386 | SourceLocation AttrLoc) { |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1387 | // unlike gcc's vector_size attribute, we do not allow vectors to be defined |
| 1388 | // in conjunction with complex types (pointers, arrays, functions, etc.). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1389 | if (!T->isDependentType() && |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1390 | !T->isIntegerType() && !T->isRealFloatingType()) { |
| 1391 | Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T; |
| 1392 | return QualType(); |
| 1393 | } |
| 1394 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1395 | if (!ArraySize->isTypeDependent() && !ArraySize->isValueDependent()) { |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1396 | llvm::APSInt vecSize(32); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1397 | if (!ArraySize->isIntegerConstantExpr(vecSize, Context)) { |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1398 | Diag(AttrLoc, diag::err_attribute_argument_not_int) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1399 | << "ext_vector_type" << ArraySize->getSourceRange(); |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1400 | return QualType(); |
| 1401 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1402 | |
| 1403 | // unlike gcc's vector_size attribute, the size is specified as the |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1404 | // number of elements, not the number of bytes. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1405 | unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue()); |
| 1406 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1407 | if (vectorSize == 0) { |
| 1408 | Diag(AttrLoc, diag::err_attribute_zero_size) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1409 | << ArraySize->getSourceRange(); |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1410 | return QualType(); |
| 1411 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1412 | |
Douglas Gregor | 4ac0140 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1413 | return Context.getExtVectorType(T, vectorSize); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1416 | return Context.getDependentSizedExtVectorType(T, ArraySize, AttrLoc); |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1417 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1418 | |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 1419 | /// \brief Build a function type. |
| 1420 | /// |
| 1421 | /// This routine checks the function type according to C++ rules and |
| 1422 | /// under the assumption that the result type and parameter types have |
| 1423 | /// just been instantiated from a template. It therefore duplicates |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1424 | /// some of the behavior of GetTypeForDeclarator, but in a much |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 1425 | /// simpler form that is only suitable for this narrow use case. |
| 1426 | /// |
| 1427 | /// \param T The return type of the function. |
| 1428 | /// |
| 1429 | /// \param ParamTypes The parameter types of the function. This array |
| 1430 | /// will be modified to account for adjustments to the types of the |
| 1431 | /// function parameters. |
| 1432 | /// |
| 1433 | /// \param NumParamTypes The number of parameter types in ParamTypes. |
| 1434 | /// |
| 1435 | /// \param Variadic Whether this is a variadic function type. |
| 1436 | /// |
| 1437 | /// \param Quals The cvr-qualifiers to be applied to the function type. |
| 1438 | /// |
| 1439 | /// \param Loc The location of the entity whose type involves this |
| 1440 | /// function type or, if there is no such entity, the location of the |
| 1441 | /// type that will have function type. |
| 1442 | /// |
| 1443 | /// \param Entity The name of the entity that involves the function |
| 1444 | /// type, if known. |
| 1445 | /// |
| 1446 | /// \returns A suitable function type, if there are no |
| 1447 | /// errors. Otherwise, returns a NULL type. |
| 1448 | QualType Sema::BuildFunctionType(QualType T, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1449 | QualType *ParamTypes, |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 1450 | unsigned NumParamTypes, |
| 1451 | bool Variadic, unsigned Quals, |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 1452 | RefQualifierKind RefQualifier, |
Eli Friedman | fa86954 | 2010-08-05 02:54:05 +0000 | [diff] [blame] | 1453 | SourceLocation Loc, DeclarationName Entity, |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1454 | FunctionType::ExtInfo Info) { |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 1455 | if (T->isArrayType() || T->isFunctionType()) { |
Douglas Gregor | 58408bc | 2010-01-11 18:46:21 +0000 | [diff] [blame] | 1456 | Diag(Loc, diag::err_func_returning_array_function) |
| 1457 | << T->isFunctionType() << T; |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 1458 | return QualType(); |
| 1459 | } |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1460 | |
| 1461 | // Functions cannot return half FP. |
| 1462 | if (T->isHalfType()) { |
| 1463 | Diag(Loc, diag::err_parameters_retval_cannot_have_fp16_type) << 1 << |
| 1464 | FixItHint::CreateInsertion(Loc, "*"); |
| 1465 | return QualType(); |
| 1466 | } |
| 1467 | |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 1468 | bool Invalid = false; |
| 1469 | for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) { |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1470 | // FIXME: Loc is too inprecise here, should use proper locations for args. |
Douglas Gregor | 79e6bd3 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 1471 | QualType ParamType = Context.getAdjustedParameterType(ParamTypes[Idx]); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1472 | if (ParamType->isVoidType()) { |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 1473 | Diag(Loc, diag::err_param_with_void_type); |
| 1474 | Invalid = true; |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1475 | } else if (ParamType->isHalfType()) { |
| 1476 | // Disallow half FP arguments. |
| 1477 | Diag(Loc, diag::err_parameters_retval_cannot_have_fp16_type) << 0 << |
| 1478 | FixItHint::CreateInsertion(Loc, "*"); |
| 1479 | Invalid = true; |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 1480 | } |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1481 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1482 | ParamTypes[Idx] = ParamType; |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 1483 | } |
| 1484 | |
| 1485 | if (Invalid) |
| 1486 | return QualType(); |
| 1487 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1488 | FunctionProtoType::ExtProtoInfo EPI; |
| 1489 | EPI.Variadic = Variadic; |
| 1490 | EPI.TypeQuals = Quals; |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 1491 | EPI.RefQualifier = RefQualifier; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 1492 | EPI.ExtInfo = Info; |
| 1493 | |
| 1494 | return Context.getFunctionType(T, ParamTypes, NumParamTypes, EPI); |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 1495 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1496 | |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 1497 | /// \brief Build a member pointer type \c T Class::*. |
| 1498 | /// |
| 1499 | /// \param T the type to which the member pointer refers. |
| 1500 | /// \param Class the class type into which the member pointer points. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1501 | /// \param CVR Qualifiers applied to the member pointer type |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 1502 | /// \param Loc the location where this type begins |
| 1503 | /// \param Entity the name of the entity that will have this member pointer type |
| 1504 | /// |
| 1505 | /// \returns a member pointer type, if successful, or a NULL type if there was |
| 1506 | /// an error. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1507 | QualType Sema::BuildMemberPointerType(QualType T, QualType Class, |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1508 | SourceLocation Loc, |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 1509 | DeclarationName Entity) { |
| 1510 | // Verify that we're not building a pointer to pointer to function with |
| 1511 | // exception specification. |
| 1512 | if (CheckDistantExceptionSpec(T)) { |
| 1513 | Diag(Loc, diag::err_distant_exception_spec); |
| 1514 | |
| 1515 | // FIXME: If we're doing this as part of template instantiation, |
| 1516 | // we should return immediately. |
| 1517 | |
| 1518 | // Build the type anyway, but use the canonical type so that the |
| 1519 | // exception specifiers are stripped off. |
| 1520 | T = Context.getCanonicalType(T); |
| 1521 | } |
| 1522 | |
Sebastian Redl | 7378012 | 2010-06-09 21:19:43 +0000 | [diff] [blame] | 1523 | // 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] | 1524 | // with reference type, or "cv void." |
| 1525 | if (T->isReferenceType()) { |
Anders Carlsson | 8d4655d | 2009-06-30 00:06:57 +0000 | [diff] [blame] | 1526 | Diag(Loc, diag::err_illegal_decl_mempointer_to_reference) |
John McCall | ac40605 | 2009-10-30 00:37:20 +0000 | [diff] [blame] | 1527 | << (Entity? Entity.getAsString() : "type name") << T; |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 1528 | return QualType(); |
| 1529 | } |
| 1530 | |
| 1531 | if (T->isVoidType()) { |
| 1532 | Diag(Loc, diag::err_illegal_decl_mempointer_to_void) |
| 1533 | << (Entity? Entity.getAsString() : "type name"); |
| 1534 | return QualType(); |
| 1535 | } |
| 1536 | |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 1537 | if (!Class->isDependentType() && !Class->isRecordType()) { |
| 1538 | Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class; |
| 1539 | return QualType(); |
| 1540 | } |
| 1541 | |
Charles Davis | d18f9f9 | 2010-08-16 04:01:50 +0000 | [diff] [blame] | 1542 | // In the Microsoft ABI, the class is allowed to be an incomplete |
| 1543 | // type. In such cases, the compiler makes a worst-case assumption. |
| 1544 | // We make no such assumption right now, so emit an error if the |
| 1545 | // class isn't a complete type. |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1546 | if (Context.getTargetInfo().getCXXABI() == CXXABI_Microsoft && |
Charles Davis | d18f9f9 | 2010-08-16 04:01:50 +0000 | [diff] [blame] | 1547 | RequireCompleteType(Loc, Class, diag::err_incomplete_type)) |
| 1548 | return QualType(); |
| 1549 | |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1550 | return Context.getMemberPointerType(T, Class.getTypePtr()); |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 1551 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1552 | |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 1553 | /// \brief Build a block pointer type. |
| 1554 | /// |
| 1555 | /// \param T The type to which we'll be building a block pointer. |
| 1556 | /// |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1557 | /// \param CVR The cvr-qualifiers to be applied to the block pointer type. |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 1558 | /// |
| 1559 | /// \param Loc The location of the entity whose type involves this |
| 1560 | /// block pointer type or, if there is no such entity, the location of the |
| 1561 | /// type that will have block pointer type. |
| 1562 | /// |
| 1563 | /// \param Entity The name of the entity that involves the block pointer |
| 1564 | /// type, if known. |
| 1565 | /// |
| 1566 | /// \returns A suitable block pointer type, if there are no |
| 1567 | /// errors. Otherwise, returns a NULL type. |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1568 | QualType Sema::BuildBlockPointerType(QualType T, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1569 | SourceLocation Loc, |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 1570 | DeclarationName Entity) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1571 | if (!T->isFunctionType()) { |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 1572 | Diag(Loc, diag::err_nonfunction_block_type); |
| 1573 | return QualType(); |
| 1574 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1575 | |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1576 | return Context.getBlockPointerType(T); |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 1577 | } |
| 1578 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1579 | QualType Sema::GetTypeFromParser(ParsedType Ty, TypeSourceInfo **TInfo) { |
| 1580 | QualType QT = Ty.get(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1581 | if (QT.isNull()) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1582 | if (TInfo) *TInfo = 0; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1583 | return QualType(); |
| 1584 | } |
| 1585 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1586 | TypeSourceInfo *DI = 0; |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 1587 | if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) { |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 1588 | QT = LIT->getType(); |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1589 | DI = LIT->getTypeSourceInfo(); |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 1590 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1591 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1592 | if (TInfo) *TInfo = DI; |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 1593 | return QT; |
| 1594 | } |
| 1595 | |
Argyrios Kyrtzidis | a8349f5 | 2011-07-01 22:23:05 +0000 | [diff] [blame] | 1596 | static void transferARCOwnershipToDeclaratorChunk(TypeProcessingState &state, |
| 1597 | Qualifiers::ObjCLifetime ownership, |
| 1598 | unsigned chunkIndex); |
| 1599 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1600 | /// Given that this is the declaration of a parameter under ARC, |
| 1601 | /// attempt to infer attributes and such for pointer-to-whatever |
| 1602 | /// types. |
| 1603 | static void inferARCWriteback(TypeProcessingState &state, |
| 1604 | QualType &declSpecType) { |
| 1605 | Sema &S = state.getSema(); |
| 1606 | Declarator &declarator = state.getDeclarator(); |
| 1607 | |
| 1608 | // TODO: should we care about decl qualifiers? |
| 1609 | |
| 1610 | // Check whether the declarator has the expected form. We walk |
| 1611 | // from the inside out in order to make the block logic work. |
| 1612 | unsigned outermostPointerIndex = 0; |
| 1613 | bool isBlockPointer = false; |
| 1614 | unsigned numPointers = 0; |
| 1615 | for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) { |
| 1616 | unsigned chunkIndex = i; |
| 1617 | DeclaratorChunk &chunk = declarator.getTypeObject(chunkIndex); |
| 1618 | switch (chunk.Kind) { |
| 1619 | case DeclaratorChunk::Paren: |
| 1620 | // Ignore parens. |
| 1621 | break; |
| 1622 | |
| 1623 | case DeclaratorChunk::Reference: |
| 1624 | case DeclaratorChunk::Pointer: |
| 1625 | // Count the number of pointers. Treat references |
| 1626 | // interchangeably as pointers; if they're mis-ordered, normal |
| 1627 | // type building will discover that. |
| 1628 | outermostPointerIndex = chunkIndex; |
| 1629 | numPointers++; |
| 1630 | break; |
| 1631 | |
| 1632 | case DeclaratorChunk::BlockPointer: |
| 1633 | // If we have a pointer to block pointer, that's an acceptable |
| 1634 | // indirect reference; anything else is not an application of |
| 1635 | // the rules. |
| 1636 | if (numPointers != 1) return; |
| 1637 | numPointers++; |
| 1638 | outermostPointerIndex = chunkIndex; |
| 1639 | isBlockPointer = true; |
| 1640 | |
| 1641 | // We don't care about pointer structure in return values here. |
| 1642 | goto done; |
| 1643 | |
| 1644 | case DeclaratorChunk::Array: // suppress if written (id[])? |
| 1645 | case DeclaratorChunk::Function: |
| 1646 | case DeclaratorChunk::MemberPointer: |
| 1647 | return; |
| 1648 | } |
| 1649 | } |
| 1650 | done: |
| 1651 | |
| 1652 | // If we have *one* pointer, then we want to throw the qualifier on |
| 1653 | // the declaration-specifiers, which means that it needs to be a |
| 1654 | // retainable object type. |
| 1655 | if (numPointers == 1) { |
| 1656 | // If it's not a retainable object type, the rule doesn't apply. |
| 1657 | if (!declSpecType->isObjCRetainableType()) return; |
| 1658 | |
| 1659 | // If it already has lifetime, don't do anything. |
| 1660 | if (declSpecType.getObjCLifetime()) return; |
| 1661 | |
| 1662 | // Otherwise, modify the type in-place. |
| 1663 | Qualifiers qs; |
| 1664 | |
| 1665 | if (declSpecType->isObjCARCImplicitlyUnretainedType()) |
| 1666 | qs.addObjCLifetime(Qualifiers::OCL_ExplicitNone); |
| 1667 | else |
| 1668 | qs.addObjCLifetime(Qualifiers::OCL_Autoreleasing); |
| 1669 | declSpecType = S.Context.getQualifiedType(declSpecType, qs); |
| 1670 | |
| 1671 | // If we have *two* pointers, then we want to throw the qualifier on |
| 1672 | // the outermost pointer. |
| 1673 | } else if (numPointers == 2) { |
| 1674 | // If we don't have a block pointer, we need to check whether the |
| 1675 | // declaration-specifiers gave us something that will turn into a |
| 1676 | // retainable object pointer after we slap the first pointer on it. |
| 1677 | if (!isBlockPointer && !declSpecType->isObjCObjectType()) |
| 1678 | return; |
| 1679 | |
| 1680 | // Look for an explicit lifetime attribute there. |
| 1681 | DeclaratorChunk &chunk = declarator.getTypeObject(outermostPointerIndex); |
Argyrios Kyrtzidis | 1c73dcb | 2011-07-01 22:23:03 +0000 | [diff] [blame] | 1682 | if (chunk.Kind != DeclaratorChunk::Pointer && |
| 1683 | chunk.Kind != DeclaratorChunk::BlockPointer) |
| 1684 | return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1685 | for (const AttributeList *attr = chunk.getAttrs(); attr; |
| 1686 | attr = attr->getNext()) |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 1687 | if (attr->getKind() == AttributeList::AT_objc_ownership) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1688 | return; |
| 1689 | |
Argyrios Kyrtzidis | a8349f5 | 2011-07-01 22:23:05 +0000 | [diff] [blame] | 1690 | transferARCOwnershipToDeclaratorChunk(state, Qualifiers::OCL_Autoreleasing, |
| 1691 | outermostPointerIndex); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1692 | |
| 1693 | // Any other number of pointers/references does not trigger the rule. |
| 1694 | } else return; |
| 1695 | |
| 1696 | // TODO: mark whether we did this inference? |
| 1697 | } |
| 1698 | |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 1699 | static void DiagnoseIgnoredQualifiers(unsigned Quals, |
| 1700 | SourceLocation ConstQualLoc, |
| 1701 | SourceLocation VolatileQualLoc, |
| 1702 | SourceLocation RestrictQualLoc, |
| 1703 | Sema& S) { |
| 1704 | std::string QualStr; |
| 1705 | unsigned NumQuals = 0; |
| 1706 | SourceLocation Loc; |
| 1707 | |
| 1708 | FixItHint ConstFixIt; |
| 1709 | FixItHint VolatileFixIt; |
| 1710 | FixItHint RestrictFixIt; |
| 1711 | |
Hans Wennborg | a08fcb8 | 2011-06-03 17:37:26 +0000 | [diff] [blame] | 1712 | const SourceManager &SM = S.getSourceManager(); |
| 1713 | |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 1714 | // FIXME: The locations here are set kind of arbitrarily. It'd be nicer to |
| 1715 | // find a range and grow it to encompass all the qualifiers, regardless of |
| 1716 | // the order in which they textually appear. |
| 1717 | if (Quals & Qualifiers::Const) { |
| 1718 | ConstFixIt = FixItHint::CreateRemoval(ConstQualLoc); |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 1719 | QualStr = "const"; |
Hans Wennborg | a08fcb8 | 2011-06-03 17:37:26 +0000 | [diff] [blame] | 1720 | ++NumQuals; |
| 1721 | if (!Loc.isValid() || SM.isBeforeInTranslationUnit(ConstQualLoc, Loc)) |
| 1722 | Loc = ConstQualLoc; |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 1723 | } |
| 1724 | if (Quals & Qualifiers::Volatile) { |
| 1725 | VolatileFixIt = FixItHint::CreateRemoval(VolatileQualLoc); |
Hans Wennborg | a08fcb8 | 2011-06-03 17:37:26 +0000 | [diff] [blame] | 1726 | QualStr += (NumQuals == 0 ? "volatile" : " volatile"); |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 1727 | ++NumQuals; |
Hans Wennborg | a08fcb8 | 2011-06-03 17:37:26 +0000 | [diff] [blame] | 1728 | if (!Loc.isValid() || SM.isBeforeInTranslationUnit(VolatileQualLoc, Loc)) |
| 1729 | Loc = VolatileQualLoc; |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 1730 | } |
| 1731 | if (Quals & Qualifiers::Restrict) { |
| 1732 | RestrictFixIt = FixItHint::CreateRemoval(RestrictQualLoc); |
Hans Wennborg | a08fcb8 | 2011-06-03 17:37:26 +0000 | [diff] [blame] | 1733 | QualStr += (NumQuals == 0 ? "restrict" : " restrict"); |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 1734 | ++NumQuals; |
Hans Wennborg | a08fcb8 | 2011-06-03 17:37:26 +0000 | [diff] [blame] | 1735 | if (!Loc.isValid() || SM.isBeforeInTranslationUnit(RestrictQualLoc, Loc)) |
| 1736 | Loc = RestrictQualLoc; |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 1737 | } |
| 1738 | |
| 1739 | assert(NumQuals > 0 && "No known qualifiers?"); |
| 1740 | |
| 1741 | S.Diag(Loc, diag::warn_qual_return_type) |
Hans Wennborg | a08fcb8 | 2011-06-03 17:37:26 +0000 | [diff] [blame] | 1742 | << QualStr << NumQuals << ConstFixIt << VolatileFixIt << RestrictFixIt; |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 1743 | } |
| 1744 | |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 1745 | static QualType GetDeclSpecTypeForDeclarator(TypeProcessingState &state, |
| 1746 | TypeSourceInfo *&ReturnTypeInfo) { |
| 1747 | Sema &SemaRef = state.getSema(); |
| 1748 | Declarator &D = state.getDeclarator(); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1749 | QualType T; |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 1750 | ReturnTypeInfo = 0; |
| 1751 | |
| 1752 | // The TagDecl owned by the DeclSpec. |
Argyrios Kyrtzidis | d3880f8 | 2011-06-28 03:01:18 +0000 | [diff] [blame] | 1753 | TagDecl *OwnedTagDecl = 0; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1754 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1755 | switch (D.getName().getKind()) { |
Fariborz Jahanian | 98a5403 | 2011-07-12 17:16:56 +0000 | [diff] [blame] | 1756 | case UnqualifiedId::IK_ImplicitSelfParam: |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1757 | case UnqualifiedId::IK_OperatorFunctionId: |
Sebastian Redl | 8999fe1 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 1758 | case UnqualifiedId::IK_Identifier: |
Sean Hunt | 0486d74 | 2009-11-28 04:44:28 +0000 | [diff] [blame] | 1759 | case UnqualifiedId::IK_LiteralOperatorId: |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1760 | case UnqualifiedId::IK_TemplateId: |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 1761 | T = ConvertDeclSpecToType(state); |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 1762 | |
Douglas Gregor | 591bd3c | 2010-02-08 22:07:33 +0000 | [diff] [blame] | 1763 | if (!D.isInvalidType() && D.getDeclSpec().isTypeSpecOwned()) { |
Argyrios Kyrtzidis | d3880f8 | 2011-06-28 03:01:18 +0000 | [diff] [blame] | 1764 | OwnedTagDecl = cast<TagDecl>(D.getDeclSpec().getRepAsDecl()); |
Abramo Bagnara | 1598797 | 2011-03-08 22:33:38 +0000 | [diff] [blame] | 1765 | // Owned declaration is embedded in declarator. |
Argyrios Kyrtzidis | d3880f8 | 2011-06-28 03:01:18 +0000 | [diff] [blame] | 1766 | OwnedTagDecl->setEmbeddedInDeclarator(true); |
Douglas Gregor | 591bd3c | 2010-02-08 22:07:33 +0000 | [diff] [blame] | 1767 | } |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1768 | break; |
| 1769 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1770 | case UnqualifiedId::IK_ConstructorName: |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1771 | case UnqualifiedId::IK_ConstructorTemplateId: |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 1772 | case UnqualifiedId::IK_DestructorName: |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1773 | // Constructors and destructors don't have return types. Use |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 1774 | // "void" instead. |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 1775 | T = SemaRef.Context.VoidTy; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1776 | break; |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 1777 | |
| 1778 | case UnqualifiedId::IK_ConversionFunctionId: |
| 1779 | // The result type of a conversion function is the type that it |
| 1780 | // converts to. |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 1781 | T = SemaRef.GetTypeFromParser(D.getName().ConversionFunctionId, |
| 1782 | &ReturnTypeInfo); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 1783 | break; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1784 | } |
Douglas Gregor | dab60ad | 2010-10-01 18:44:50 +0000 | [diff] [blame] | 1785 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1786 | if (D.getAttributes()) |
| 1787 | distributeTypeAttrsFromDeclarator(state, T); |
| 1788 | |
Richard Smith | e7397c6 | 2011-02-22 00:36:53 +0000 | [diff] [blame] | 1789 | // C++0x [dcl.spec.auto]p5: reject 'auto' if it is not in an allowed context. |
Richard Smith | 8110f04 | 2011-02-22 01:22:29 +0000 | [diff] [blame] | 1790 | // In C++0x, a function declarator using 'auto' must have a trailing return |
| 1791 | // type (this is checked later) and we can skip this. In other languages |
| 1792 | // using auto, we need to check regardless. |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1793 | if (D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto && |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 1794 | (!SemaRef.getLangOptions().CPlusPlus0x || !D.isFunctionDeclarator())) { |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 1795 | int Error = -1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1796 | |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 1797 | switch (D.getContext()) { |
| 1798 | case Declarator::KNRTypeListContext: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1799 | llvm_unreachable("K&R type lists aren't allowed in C++"); |
Eli Friedman | f88c400 | 2012-01-04 04:41:38 +0000 | [diff] [blame] | 1800 | case Declarator::LambdaExprContext: |
| 1801 | llvm_unreachable("Can't specify a type specifier in lambda grammar"); |
John McCall | cdda47f | 2011-10-01 09:56:14 +0000 | [diff] [blame] | 1802 | case Declarator::ObjCParameterContext: |
| 1803 | case Declarator::ObjCResultContext: |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 1804 | case Declarator::PrototypeContext: |
| 1805 | Error = 0; // Function prototype |
| 1806 | break; |
| 1807 | case Declarator::MemberContext: |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1808 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static) |
| 1809 | break; |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 1810 | switch (cast<TagDecl>(SemaRef.CurContext)->getTagKind()) { |
David Blaikie | eb2d1f1 | 2011-09-23 20:26:49 +0000 | [diff] [blame] | 1811 | case TTK_Enum: llvm_unreachable("unhandled tag kind"); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1812 | case TTK_Struct: Error = 1; /* Struct member */ break; |
| 1813 | case TTK_Union: Error = 2; /* Union member */ break; |
| 1814 | case TTK_Class: Error = 3; /* Class member */ break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1815 | } |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 1816 | break; |
| 1817 | case Declarator::CXXCatchContext: |
Argyrios Kyrtzidis | 17b6399 | 2011-07-01 22:22:40 +0000 | [diff] [blame] | 1818 | case Declarator::ObjCCatchContext: |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 1819 | Error = 4; // Exception declaration |
| 1820 | break; |
| 1821 | case Declarator::TemplateParamContext: |
| 1822 | Error = 5; // Template parameter |
| 1823 | break; |
| 1824 | case Declarator::BlockLiteralContext: |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1825 | Error = 6; // Block literal |
| 1826 | break; |
| 1827 | case Declarator::TemplateTypeArgContext: |
| 1828 | Error = 7; // Template type argument |
| 1829 | break; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1830 | case Declarator::AliasDeclContext: |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1831 | case Declarator::AliasTemplateContext: |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1832 | Error = 9; // Type alias |
| 1833 | break; |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1834 | case Declarator::TypeNameContext: |
Argyrios Kyrtzidis | 0b8c98f | 2011-06-28 03:01:23 +0000 | [diff] [blame] | 1835 | Error = 11; // Generic |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 1836 | break; |
| 1837 | case Declarator::FileContext: |
| 1838 | case Declarator::BlockContext: |
| 1839 | case Declarator::ForContext: |
| 1840 | case Declarator::ConditionContext: |
Argyrios Kyrtzidis | 0b8c98f | 2011-06-28 03:01:23 +0000 | [diff] [blame] | 1841 | case Declarator::CXXNewContext: |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 1842 | break; |
| 1843 | } |
| 1844 | |
Richard Smith | ddc83f9 | 2011-02-21 23:18:00 +0000 | [diff] [blame] | 1845 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) |
| 1846 | Error = 8; |
| 1847 | |
Richard Smith | 8110f04 | 2011-02-22 01:22:29 +0000 | [diff] [blame] | 1848 | // In Objective-C it is an error to use 'auto' on a function declarator. |
| 1849 | if (D.isFunctionDeclarator()) |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1850 | Error = 10; |
Richard Smith | 8110f04 | 2011-02-22 01:22:29 +0000 | [diff] [blame] | 1851 | |
Richard Smith | e7397c6 | 2011-02-22 00:36:53 +0000 | [diff] [blame] | 1852 | // C++0x [dcl.spec.auto]p2: 'auto' is always fine if the declarator |
| 1853 | // contains a trailing return type. That is only legal at the outermost |
| 1854 | // level. Check all declarator chunks (outermost first) anyway, to give |
| 1855 | // better diagnostics. |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 1856 | if (SemaRef.getLangOptions().CPlusPlus0x && Error != -1) { |
Richard Smith | e7397c6 | 2011-02-22 00:36:53 +0000 | [diff] [blame] | 1857 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { |
| 1858 | unsigned chunkIndex = e - i - 1; |
| 1859 | state.setCurrentChunkIndex(chunkIndex); |
| 1860 | DeclaratorChunk &DeclType = D.getTypeObject(chunkIndex); |
| 1861 | if (DeclType.Kind == DeclaratorChunk::Function) { |
| 1862 | const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun; |
| 1863 | if (FTI.TrailingReturnType) { |
| 1864 | Error = -1; |
| 1865 | break; |
| 1866 | } |
| 1867 | } |
| 1868 | } |
| 1869 | } |
| 1870 | |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 1871 | if (Error != -1) { |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 1872 | SemaRef.Diag(D.getDeclSpec().getTypeSpecTypeLoc(), |
| 1873 | diag::err_auto_not_allowed) |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 1874 | << Error; |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 1875 | T = SemaRef.Context.IntTy; |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 1876 | D.setInvalidType(true); |
Richard Smith | 0aa86c0 | 2011-10-15 05:42:01 +0000 | [diff] [blame] | 1877 | } else |
| 1878 | SemaRef.Diag(D.getDeclSpec().getTypeSpecTypeLoc(), |
| 1879 | diag::warn_cxx98_compat_auto_type_specifier); |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 1880 | } |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 1881 | |
| 1882 | if (SemaRef.getLangOptions().CPlusPlus && |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 1883 | OwnedTagDecl && OwnedTagDecl->isCompleteDefinition()) { |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 1884 | // Check the contexts where C++ forbids the declaration of a new class |
| 1885 | // or enumeration in a type-specifier-seq. |
| 1886 | switch (D.getContext()) { |
| 1887 | case Declarator::FileContext: |
| 1888 | case Declarator::MemberContext: |
| 1889 | case Declarator::BlockContext: |
| 1890 | case Declarator::ForContext: |
| 1891 | case Declarator::BlockLiteralContext: |
Eli Friedman | f88c400 | 2012-01-04 04:41:38 +0000 | [diff] [blame] | 1892 | case Declarator::LambdaExprContext: |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 1893 | // C++0x [dcl.type]p3: |
| 1894 | // A type-specifier-seq shall not define a class or enumeration unless |
| 1895 | // it appears in the type-id of an alias-declaration (7.1.3) that is not |
| 1896 | // the declaration of a template-declaration. |
| 1897 | case Declarator::AliasDeclContext: |
| 1898 | break; |
| 1899 | case Declarator::AliasTemplateContext: |
| 1900 | SemaRef.Diag(OwnedTagDecl->getLocation(), |
| 1901 | diag::err_type_defined_in_alias_template) |
| 1902 | << SemaRef.Context.getTypeDeclType(OwnedTagDecl); |
| 1903 | break; |
| 1904 | case Declarator::TypeNameContext: |
| 1905 | case Declarator::TemplateParamContext: |
| 1906 | case Declarator::CXXNewContext: |
| 1907 | case Declarator::CXXCatchContext: |
| 1908 | case Declarator::ObjCCatchContext: |
| 1909 | case Declarator::TemplateTypeArgContext: |
| 1910 | SemaRef.Diag(OwnedTagDecl->getLocation(), |
| 1911 | diag::err_type_defined_in_type_specifier) |
| 1912 | << SemaRef.Context.getTypeDeclType(OwnedTagDecl); |
| 1913 | break; |
| 1914 | case Declarator::PrototypeContext: |
John McCall | cdda47f | 2011-10-01 09:56:14 +0000 | [diff] [blame] | 1915 | case Declarator::ObjCParameterContext: |
| 1916 | case Declarator::ObjCResultContext: |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 1917 | case Declarator::KNRTypeListContext: |
| 1918 | // C++ [dcl.fct]p6: |
| 1919 | // Types shall not be defined in return or parameter types. |
| 1920 | SemaRef.Diag(OwnedTagDecl->getLocation(), |
| 1921 | diag::err_type_defined_in_param_type) |
| 1922 | << SemaRef.Context.getTypeDeclType(OwnedTagDecl); |
| 1923 | break; |
| 1924 | case Declarator::ConditionContext: |
| 1925 | // C++ 6.4p2: |
| 1926 | // The type-specifier-seq shall not contain typedef and shall not declare |
| 1927 | // a new class or enumeration. |
| 1928 | SemaRef.Diag(OwnedTagDecl->getLocation(), |
| 1929 | diag::err_type_defined_in_condition); |
| 1930 | break; |
| 1931 | } |
| 1932 | } |
| 1933 | |
| 1934 | return T; |
| 1935 | } |
| 1936 | |
| 1937 | static TypeSourceInfo *GetFullTypeForDeclarator(TypeProcessingState &state, |
| 1938 | QualType declSpecType, |
| 1939 | TypeSourceInfo *TInfo) { |
| 1940 | |
| 1941 | QualType T = declSpecType; |
| 1942 | Declarator &D = state.getDeclarator(); |
| 1943 | Sema &S = state.getSema(); |
| 1944 | ASTContext &Context = S.Context; |
| 1945 | const LangOptions &LangOpts = S.getLangOptions(); |
| 1946 | |
| 1947 | bool ImplicitlyNoexcept = false; |
| 1948 | if (D.getName().getKind() == UnqualifiedId::IK_OperatorFunctionId && |
| 1949 | LangOpts.CPlusPlus0x) { |
| 1950 | OverloadedOperatorKind OO = D.getName().OperatorFunctionId.Operator; |
| 1951 | /// In C++0x, deallocation functions (normal and array operator delete) |
| 1952 | /// are implicitly noexcept. |
| 1953 | if (OO == OO_Delete || OO == OO_Array_Delete) |
| 1954 | ImplicitlyNoexcept = true; |
| 1955 | } |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1956 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1957 | // The name we're declaring, if any. |
| 1958 | DeclarationName Name; |
| 1959 | if (D.getIdentifier()) |
| 1960 | Name = D.getIdentifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1961 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1962 | // Does this declaration declare a typedef-name? |
| 1963 | bool IsTypedefName = |
| 1964 | D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef || |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1965 | D.getContext() == Declarator::AliasDeclContext || |
| 1966 | D.getContext() == Declarator::AliasTemplateContext; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1967 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 1968 | // Walk the DeclTypeInfo, building the recursive type as we go. |
| 1969 | // DeclTypeInfos are ordered from the identifier out, which is |
| 1970 | // opposite of what we want :). |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 1971 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1972 | unsigned chunkIndex = e - i - 1; |
| 1973 | state.setCurrentChunkIndex(chunkIndex); |
| 1974 | DeclaratorChunk &DeclType = D.getTypeObject(chunkIndex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1975 | switch (DeclType.Kind) { |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 1976 | case DeclaratorChunk::Paren: |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 1977 | T = S.BuildParenType(T); |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 1978 | break; |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1979 | case DeclaratorChunk::BlockPointer: |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 1980 | // If blocks are disabled, emit an error. |
| 1981 | if (!LangOpts.Blocks) |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 1982 | S.Diag(DeclType.Loc, diag::err_blocks_disable); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1983 | |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 1984 | T = S.BuildBlockPointerType(T, D.getIdentifierLoc(), Name); |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1985 | if (DeclType.Cls.TypeQuals) |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 1986 | T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Cls.TypeQuals); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1987 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1988 | case DeclaratorChunk::Pointer: |
Sebastian Redl | 6a7330c | 2009-05-29 15:01:05 +0000 | [diff] [blame] | 1989 | // Verify that we're not building a pointer to pointer to function with |
| 1990 | // exception specification. |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 1991 | if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) { |
| 1992 | S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec); |
Sebastian Redl | 6a7330c | 2009-05-29 15:01:05 +0000 | [diff] [blame] | 1993 | D.setInvalidType(true); |
| 1994 | // Build the type anyway. |
| 1995 | } |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 1996 | if (LangOpts.ObjC1 && T->getAs<ObjCObjectType>()) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1997 | T = Context.getObjCObjectPointerType(T); |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 1998 | if (DeclType.Ptr.TypeQuals) |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 1999 | T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2000 | break; |
| 2001 | } |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2002 | T = S.BuildPointerType(T, DeclType.Loc, Name); |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 2003 | if (DeclType.Ptr.TypeQuals) |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2004 | T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2005 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2006 | break; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2007 | case DeclaratorChunk::Reference: { |
Sebastian Redl | 6a7330c | 2009-05-29 15:01:05 +0000 | [diff] [blame] | 2008 | // Verify that we're not building a reference to pointer to function with |
| 2009 | // exception specification. |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2010 | if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) { |
| 2011 | S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec); |
Sebastian Redl | 6a7330c | 2009-05-29 15:01:05 +0000 | [diff] [blame] | 2012 | D.setInvalidType(true); |
| 2013 | // Build the type anyway. |
| 2014 | } |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2015 | T = S.BuildReferenceType(T, DeclType.Ref.LValueRef, DeclType.Loc, Name); |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 2016 | |
| 2017 | Qualifiers Quals; |
| 2018 | if (DeclType.Ref.HasRestrict) |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2019 | T = S.BuildQualifiedType(T, DeclType.Loc, Qualifiers::Restrict); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2020 | break; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2021 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2022 | case DeclaratorChunk::Array: { |
Sebastian Redl | 6a7330c | 2009-05-29 15:01:05 +0000 | [diff] [blame] | 2023 | // Verify that we're not building an array of pointers to function with |
| 2024 | // exception specification. |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2025 | if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) { |
| 2026 | S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec); |
Sebastian Redl | 6a7330c | 2009-05-29 15:01:05 +0000 | [diff] [blame] | 2027 | D.setInvalidType(true); |
| 2028 | // Build the type anyway. |
| 2029 | } |
Chris Lattner | fd89bc8 | 2008-04-02 01:05:10 +0000 | [diff] [blame] | 2030 | DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr; |
Chris Lattner | 94f81fd | 2007-08-28 16:54:00 +0000 | [diff] [blame] | 2031 | Expr *ArraySize = static_cast<Expr*>(ATI.NumElts); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2032 | ArrayType::ArraySizeModifier ASM; |
| 2033 | if (ATI.isStar) |
| 2034 | ASM = ArrayType::Star; |
| 2035 | else if (ATI.hasStatic) |
| 2036 | ASM = ArrayType::Static; |
| 2037 | else |
| 2038 | ASM = ArrayType::Normal; |
John McCall | c05a94b | 2011-03-23 23:43:04 +0000 | [diff] [blame] | 2039 | if (ASM == ArrayType::Star && !D.isPrototypeContext()) { |
Eli Friedman | f91f5c8 | 2009-04-26 21:57:51 +0000 | [diff] [blame] | 2040 | // FIXME: This check isn't quite right: it allows star in prototypes |
| 2041 | // for function definitions, and disallows some edge cases detailed |
| 2042 | // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2043 | S.Diag(DeclType.Loc, diag::err_array_star_outside_prototype); |
Eli Friedman | f91f5c8 | 2009-04-26 21:57:51 +0000 | [diff] [blame] | 2044 | ASM = ArrayType::Normal; |
| 2045 | D.setInvalidType(true); |
| 2046 | } |
Eli Friedman | 8ac2c66 | 2011-11-11 02:00:42 +0000 | [diff] [blame] | 2047 | T = S.BuildArrayType(T, ASM, ArraySize, ATI.TypeQuals, |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2048 | SourceRange(DeclType.Loc, DeclType.EndLoc), Name); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2049 | break; |
| 2050 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2051 | case DeclaratorChunk::Function: { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2052 | // If the function declarator has a prototype (i.e. it is not () and |
| 2053 | // does not have a K&R-style identifier list), then the arguments are part |
| 2054 | // of the type, otherwise the argument list is (). |
| 2055 | const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun; |
Sebastian Redl | 3cc9726 | 2009-05-31 11:47:27 +0000 | [diff] [blame] | 2056 | |
Richard Smith | e7397c6 | 2011-02-22 00:36:53 +0000 | [diff] [blame] | 2057 | // Check for auto functions and trailing return type and adjust the |
| 2058 | // return type accordingly. |
| 2059 | if (!D.isInvalidType()) { |
| 2060 | // trailing-return-type is only required if we're declaring a function, |
| 2061 | // and not, for instance, a pointer to a function. |
| 2062 | if (D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto && |
| 2063 | !FTI.TrailingReturnType && chunkIndex == 0) { |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2064 | S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(), |
Richard Smith | e7397c6 | 2011-02-22 00:36:53 +0000 | [diff] [blame] | 2065 | diag::err_auto_missing_trailing_return); |
| 2066 | T = Context.IntTy; |
| 2067 | D.setInvalidType(true); |
| 2068 | } else if (FTI.TrailingReturnType) { |
| 2069 | // T must be exactly 'auto' at this point. See CWG issue 681. |
| 2070 | if (isa<ParenType>(T)) { |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2071 | S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(), |
Richard Smith | e7397c6 | 2011-02-22 00:36:53 +0000 | [diff] [blame] | 2072 | diag::err_trailing_return_in_parens) |
| 2073 | << T << D.getDeclSpec().getSourceRange(); |
| 2074 | D.setInvalidType(true); |
Eli Friedman | f88c400 | 2012-01-04 04:41:38 +0000 | [diff] [blame] | 2075 | } else if (D.getContext() != Declarator::LambdaExprContext && |
| 2076 | (T.hasQualifiers() || !isa<AutoType>(T))) { |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2077 | S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(), |
Richard Smith | e7397c6 | 2011-02-22 00:36:53 +0000 | [diff] [blame] | 2078 | diag::err_trailing_return_without_auto) |
| 2079 | << T << D.getDeclSpec().getSourceRange(); |
| 2080 | D.setInvalidType(true); |
| 2081 | } |
| 2082 | |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2083 | T = S.GetTypeFromParser( |
Richard Smith | e7397c6 | 2011-02-22 00:36:53 +0000 | [diff] [blame] | 2084 | ParsedType::getFromOpaquePtr(FTI.TrailingReturnType), |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2085 | &TInfo); |
Richard Smith | e7397c6 | 2011-02-22 00:36:53 +0000 | [diff] [blame] | 2086 | } |
| 2087 | } |
| 2088 | |
Chris Lattner | cd88129 | 2007-12-19 05:31:29 +0000 | [diff] [blame] | 2089 | // 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] | 2090 | // For conversion functions, we'll diagnose this particular error later. |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 2091 | if ((T->isArrayType() || T->isFunctionType()) && |
| 2092 | (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId)) { |
Argyrios Kyrtzidis | 9865044 | 2011-01-25 23:16:33 +0000 | [diff] [blame] | 2093 | unsigned diagID = diag::err_func_returning_array_function; |
Argyrios Kyrtzidis | a4356ad | 2011-01-26 01:26:44 +0000 | [diff] [blame] | 2094 | // Last processing chunk in block context means this function chunk |
| 2095 | // represents the block. |
| 2096 | if (chunkIndex == 0 && |
| 2097 | D.getContext() == Declarator::BlockLiteralContext) |
Argyrios Kyrtzidis | 9865044 | 2011-01-25 23:16:33 +0000 | [diff] [blame] | 2098 | diagID = diag::err_block_returning_array_function; |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2099 | S.Diag(DeclType.Loc, diagID) << T->isFunctionType() << T; |
Chris Lattner | cd88129 | 2007-12-19 05:31:29 +0000 | [diff] [blame] | 2100 | T = Context.IntTy; |
| 2101 | D.setInvalidType(true); |
| 2102 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2103 | |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 2104 | // Do not allow returning half FP value. |
| 2105 | // FIXME: This really should be in BuildFunctionType. |
| 2106 | if (T->isHalfType()) { |
| 2107 | S.Diag(D.getIdentifierLoc(), |
| 2108 | diag::err_parameters_retval_cannot_have_fp16_type) << 1 |
| 2109 | << FixItHint::CreateInsertion(D.getIdentifierLoc(), "*"); |
| 2110 | D.setInvalidType(true); |
| 2111 | } |
| 2112 | |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 2113 | // cv-qualifiers on return types are pointless except when the type is a |
| 2114 | // class type in C++. |
Douglas Gregor | fff9513 | 2011-03-01 17:04:42 +0000 | [diff] [blame] | 2115 | if (isa<PointerType>(T) && T.getLocalCVRQualifiers() && |
Rafael Espindola | 1e15394 | 2011-03-11 04:56:58 +0000 | [diff] [blame] | 2116 | (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId) && |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2117 | (!LangOpts.CPlusPlus || !T->isDependentType())) { |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 2118 | assert(chunkIndex + 1 < e && "No DeclaratorChunk for the return type?"); |
| 2119 | DeclaratorChunk ReturnTypeChunk = D.getTypeObject(chunkIndex + 1); |
| 2120 | assert(ReturnTypeChunk.Kind == DeclaratorChunk::Pointer); |
| 2121 | |
| 2122 | DeclaratorChunk::PointerTypeInfo &PTI = ReturnTypeChunk.Ptr; |
| 2123 | |
| 2124 | DiagnoseIgnoredQualifiers(PTI.TypeQuals, |
| 2125 | SourceLocation::getFromRawEncoding(PTI.ConstQualLoc), |
| 2126 | SourceLocation::getFromRawEncoding(PTI.VolatileQualLoc), |
| 2127 | SourceLocation::getFromRawEncoding(PTI.RestrictQualLoc), |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2128 | S); |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 2129 | |
| 2130 | } else if (T.getCVRQualifiers() && D.getDeclSpec().getTypeQualifiers() && |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2131 | (!LangOpts.CPlusPlus || |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 2132 | (!T->isDependentType() && !T->isRecordType()))) { |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 2133 | |
| 2134 | DiagnoseIgnoredQualifiers(D.getDeclSpec().getTypeQualifiers(), |
| 2135 | D.getDeclSpec().getConstSpecLoc(), |
| 2136 | D.getDeclSpec().getVolatileSpecLoc(), |
| 2137 | D.getDeclSpec().getRestrictSpecLoc(), |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2138 | S); |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 2139 | } |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 2140 | |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2141 | if (LangOpts.CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) { |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 2142 | // C++ [dcl.fct]p6: |
| 2143 | // Types shall not be defined in return or parameter types. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2144 | TagDecl *Tag = cast<TagDecl>(D.getDeclSpec().getRepAsDecl()); |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2145 | if (Tag->isCompleteDefinition()) |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2146 | S.Diag(Tag->getLocation(), diag::err_type_defined_in_result_type) |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 2147 | << Context.getTypeDeclType(Tag); |
| 2148 | } |
| 2149 | |
Sebastian Redl | 3cc9726 | 2009-05-31 11:47:27 +0000 | [diff] [blame] | 2150 | // Exception specs are not allowed in typedefs. Complain, but add it |
| 2151 | // anyway. |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2152 | if (IsTypedefName && FTI.getExceptionSpecType()) |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2153 | S.Diag(FTI.getExceptionSpecLoc(), diag::err_exception_spec_in_typedef) |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2154 | << (D.getContext() == Declarator::AliasDeclContext || |
| 2155 | D.getContext() == Declarator::AliasTemplateContext); |
Sebastian Redl | 3cc9726 | 2009-05-31 11:47:27 +0000 | [diff] [blame] | 2156 | |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2157 | if (!FTI.NumArgs && !FTI.isVariadic && !LangOpts.CPlusPlus) { |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 2158 | // Simple void foo(), where the incoming T is the result type. |
| 2159 | T = Context.getFunctionNoProtoType(T); |
| 2160 | } else { |
| 2161 | // We allow a zero-parameter variadic function in C if the |
| 2162 | // function is marked with the "overloadable" attribute. Scan |
| 2163 | // for this attribute now. |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2164 | if (!FTI.NumArgs && FTI.isVariadic && !LangOpts.CPlusPlus) { |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 2165 | bool Overloadable = false; |
| 2166 | for (const AttributeList *Attrs = D.getAttributes(); |
| 2167 | Attrs; Attrs = Attrs->getNext()) { |
| 2168 | if (Attrs->getKind() == AttributeList::AT_overloadable) { |
| 2169 | Overloadable = true; |
| 2170 | break; |
| 2171 | } |
| 2172 | } |
| 2173 | |
| 2174 | if (!Overloadable) |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2175 | S.Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg); |
Argyrios Kyrtzidis | c6f7345 | 2008-10-16 17:31:08 +0000 | [diff] [blame] | 2176 | } |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 2177 | |
| 2178 | if (FTI.NumArgs && FTI.ArgInfo[0].Param == 0) { |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 2179 | // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function |
| 2180 | // definition. |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2181 | 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] | 2182 | D.setInvalidType(true); |
| 2183 | break; |
| 2184 | } |
| 2185 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2186 | FunctionProtoType::ExtProtoInfo EPI; |
| 2187 | EPI.Variadic = FTI.isVariadic; |
| 2188 | EPI.TypeQuals = FTI.TypeQuals; |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 2189 | EPI.RefQualifier = !FTI.hasRefQualifier()? RQ_None |
| 2190 | : FTI.RefQualifierIsLValueRef? RQ_LValue |
| 2191 | : RQ_RValue; |
| 2192 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2193 | // Otherwise, we have a function with an argument list that is |
| 2194 | // potentially variadic. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2195 | SmallVector<QualType, 16> ArgTys; |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 2196 | ArgTys.reserve(FTI.NumArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2197 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2198 | SmallVector<bool, 16> ConsumedArguments; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2199 | ConsumedArguments.reserve(FTI.NumArgs); |
| 2200 | bool HasAnyConsumedArguments = false; |
| 2201 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2202 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2203 | ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param); |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 2204 | QualType ArgTy = Param->getType(); |
Chris Lattner | 78c75fb | 2007-07-21 05:30:18 +0000 | [diff] [blame] | 2205 | assert(!ArgTy.isNull() && "Couldn't parse type?"); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 2206 | |
| 2207 | // Adjust the parameter type. |
Douglas Gregor | 79e6bd3 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 2208 | assert((ArgTy == Context.getAdjustedParameterType(ArgTy)) && |
| 2209 | "Unadjusted type?"); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 2210 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2211 | // Look for 'void'. void is allowed only as a single argument to a |
| 2212 | // function with no other parameters (C99 6.7.5.3p10). We record |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2213 | // int(void) as a FunctionProtoType with an empty argument list. |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 2214 | if (ArgTy->isVoidType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2215 | // If this is something like 'float(int, void)', reject it. 'void' |
| 2216 | // is an incomplete type (C99 6.2.5p19) and function decls cannot |
| 2217 | // have arguments of incomplete type. |
| 2218 | if (FTI.NumArgs != 1 || FTI.isVariadic) { |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2219 | S.Diag(DeclType.Loc, diag::err_void_only_param); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 2220 | ArgTy = Context.IntTy; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 2221 | Param->setType(ArgTy); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 2222 | } else if (FTI.ArgInfo[i].Ident) { |
| 2223 | // Reject, but continue to parse 'int(void abc)'. |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2224 | S.Diag(FTI.ArgInfo[i].IdentLoc, |
Chris Lattner | 4565d4e | 2007-07-21 05:26:43 +0000 | [diff] [blame] | 2225 | diag::err_param_with_void_type); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 2226 | ArgTy = Context.IntTy; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 2227 | Param->setType(ArgTy); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 2228 | } else { |
| 2229 | // Reject, but continue to parse 'float(const void)'. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2230 | if (ArgTy.hasQualifiers()) |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2231 | S.Diag(DeclType.Loc, diag::err_void_param_qualified); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2232 | |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 2233 | // Do not add 'void' to the ArgTys list. |
| 2234 | break; |
| 2235 | } |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 2236 | } else if (ArgTy->isHalfType()) { |
| 2237 | // Disallow half FP arguments. |
| 2238 | // FIXME: This really should be in BuildFunctionType. |
| 2239 | S.Diag(Param->getLocation(), |
| 2240 | diag::err_parameters_retval_cannot_have_fp16_type) << 0 |
| 2241 | << FixItHint::CreateInsertion(Param->getLocation(), "*"); |
| 2242 | D.setInvalidType(); |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 2243 | } else if (!FTI.hasPrototype) { |
| 2244 | if (ArgTy->isPromotableIntegerType()) { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 2245 | ArgTy = Context.getPromotedIntegerType(ArgTy); |
John McCall | eecf5fa | 2011-03-09 04:22:44 +0000 | [diff] [blame] | 2246 | Param->setKNRPromoted(true); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2247 | } else if (const BuiltinType* BTy = ArgTy->getAs<BuiltinType>()) { |
John McCall | eecf5fa | 2011-03-09 04:22:44 +0000 | [diff] [blame] | 2248 | if (BTy->getKind() == BuiltinType::Float) { |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 2249 | ArgTy = Context.DoubleTy; |
John McCall | eecf5fa | 2011-03-09 04:22:44 +0000 | [diff] [blame] | 2250 | Param->setKNRPromoted(true); |
| 2251 | } |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 2252 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2253 | } |
Fariborz Jahanian | 56a965c | 2010-09-08 21:36:35 +0000 | [diff] [blame] | 2254 | |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2255 | if (LangOpts.ObjCAutoRefCount) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2256 | bool Consumed = Param->hasAttr<NSConsumedAttr>(); |
| 2257 | ConsumedArguments.push_back(Consumed); |
| 2258 | HasAnyConsumedArguments |= Consumed; |
| 2259 | } |
| 2260 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2261 | ArgTys.push_back(ArgTy); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2262 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2263 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2264 | if (HasAnyConsumedArguments) |
| 2265 | EPI.ConsumedArguments = ConsumedArguments.data(); |
| 2266 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2267 | SmallVector<QualType, 4> Exceptions; |
Sebastian Redl | 8b5b409 | 2011-03-06 10:52:04 +0000 | [diff] [blame] | 2268 | EPI.ExceptionSpecType = FTI.getExceptionSpecType(); |
| 2269 | if (FTI.getExceptionSpecType() == EST_Dynamic) { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2270 | Exceptions.reserve(FTI.NumExceptions); |
| 2271 | for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) { |
| 2272 | // FIXME: Preserve type source info. |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2273 | QualType ET = S.GetTypeFromParser(FTI.Exceptions[ei].Ty); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2274 | // Check that the type is valid for an exception spec, and |
| 2275 | // drop it if not. |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2276 | if (!S.CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range)) |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2277 | Exceptions.push_back(ET); |
| 2278 | } |
John McCall | 373920b | 2010-12-14 16:45:57 +0000 | [diff] [blame] | 2279 | EPI.NumExceptions = Exceptions.size(); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2280 | EPI.Exceptions = Exceptions.data(); |
Sebastian Redl | 8b5b409 | 2011-03-06 10:52:04 +0000 | [diff] [blame] | 2281 | } else if (FTI.getExceptionSpecType() == EST_ComputedNoexcept) { |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2282 | // If an error occurred, there's no expression here. |
| 2283 | if (Expr *NoexceptExpr = FTI.NoexceptExpr) { |
| 2284 | assert((NoexceptExpr->isTypeDependent() || |
| 2285 | NoexceptExpr->getType()->getCanonicalTypeUnqualified() == |
| 2286 | Context.BoolTy) && |
| 2287 | "Parser should have made sure that the expression is boolean"); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2288 | if (!NoexceptExpr->isValueDependent()) |
| 2289 | NoexceptExpr = S.VerifyIntegerConstantExpression(NoexceptExpr, 0, |
| 2290 | S.PDiag(diag::err_noexcept_needs_constant_expression), |
| 2291 | /*AllowFold*/ false).take(); |
| 2292 | EPI.NoexceptExpr = NoexceptExpr; |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2293 | } |
Sebastian Redl | 8999fe1 | 2011-03-14 18:08:30 +0000 | [diff] [blame] | 2294 | } else if (FTI.getExceptionSpecType() == EST_None && |
| 2295 | ImplicitlyNoexcept && chunkIndex == 0) { |
| 2296 | // Only the outermost chunk is marked noexcept, of course. |
| 2297 | EPI.ExceptionSpecType = EST_BasicNoexcept; |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 2298 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2299 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2300 | T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(), EPI); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2301 | } |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2302 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2303 | break; |
| 2304 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2305 | case DeclaratorChunk::MemberPointer: |
| 2306 | // The scope spec must refer to a class, or be dependent. |
Abramo Bagnara | 7bd0676 | 2010-08-13 12:56:25 +0000 | [diff] [blame] | 2307 | CXXScopeSpec &SS = DeclType.Mem.Scope(); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2308 | QualType ClsType; |
Abramo Bagnara | 7bd0676 | 2010-08-13 12:56:25 +0000 | [diff] [blame] | 2309 | if (SS.isInvalid()) { |
Jeffrey Yasskin | edc2877 | 2010-04-07 23:29:58 +0000 | [diff] [blame] | 2310 | // Avoid emitting extra errors if we already errored on the scope. |
| 2311 | D.setInvalidType(true); |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2312 | } else if (S.isDependentScopeSpecifier(SS) || |
| 2313 | dyn_cast_or_null<CXXRecordDecl>(S.computeDeclContext(SS))) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2314 | NestedNameSpecifier *NNS |
Abramo Bagnara | 7bd0676 | 2010-08-13 12:56:25 +0000 | [diff] [blame] | 2315 | = static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 2316 | NestedNameSpecifier *NNSPrefix = NNS->getPrefix(); |
| 2317 | switch (NNS->getKind()) { |
| 2318 | case NestedNameSpecifier::Identifier: |
Abramo Bagnara | 7bd0676 | 2010-08-13 12:56:25 +0000 | [diff] [blame] | 2319 | ClsType = Context.getDependentNameType(ETK_None, NNSPrefix, |
Douglas Gregor | 4a2023f | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2320 | NNS->getAsIdentifier()); |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 2321 | break; |
| 2322 | |
| 2323 | case NestedNameSpecifier::Namespace: |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 2324 | case NestedNameSpecifier::NamespaceAlias: |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 2325 | case NestedNameSpecifier::Global: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2326 | llvm_unreachable("Nested-name-specifier must name a type"); |
Abramo Bagnara | 7bd0676 | 2010-08-13 12:56:25 +0000 | [diff] [blame] | 2327 | |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 2328 | case NestedNameSpecifier::TypeSpec: |
| 2329 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 2330 | ClsType = QualType(NNS->getAsType(), 0); |
Abramo Bagnara | 91ce2c4 | 2011-03-10 10:18:27 +0000 | [diff] [blame] | 2331 | // Note: if the NNS has a prefix and ClsType is a nondependent |
| 2332 | // TemplateSpecializationType, then the NNS prefix is NOT included |
| 2333 | // in ClsType; hence we wrap ClsType into an ElaboratedType. |
| 2334 | // NOTE: in particular, no wrap occurs if ClsType already is an |
| 2335 | // Elaborated, DependentName, or DependentTemplateSpecialization. |
| 2336 | if (NNSPrefix && isa<TemplateSpecializationType>(NNS->getAsType())) |
Abramo Bagnara | 7bd0676 | 2010-08-13 12:56:25 +0000 | [diff] [blame] | 2337 | ClsType = Context.getElaboratedType(ETK_None, NNSPrefix, ClsType); |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 2338 | break; |
| 2339 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2340 | } else { |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2341 | S.Diag(DeclType.Mem.Scope().getBeginLoc(), |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 2342 | diag::err_illegal_decl_mempointer_in_nonclass) |
| 2343 | << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name") |
| 2344 | << DeclType.Mem.Scope().getRange(); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2345 | D.setInvalidType(true); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2346 | } |
| 2347 | |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 2348 | if (!ClsType.isNull()) |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2349 | T = S.BuildMemberPointerType(T, ClsType, DeclType.Loc, D.getIdentifier()); |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 2350 | if (T.isNull()) { |
| 2351 | T = Context.IntTy; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2352 | D.setInvalidType(true); |
John McCall | 2865474 | 2010-06-05 06:41:15 +0000 | [diff] [blame] | 2353 | } else if (DeclType.Mem.TypeQuals) { |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2354 | T = S.BuildQualifiedType(T, DeclType.Loc, DeclType.Mem.TypeQuals); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2355 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2356 | break; |
| 2357 | } |
| 2358 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 2359 | if (T.isNull()) { |
| 2360 | D.setInvalidType(true); |
| 2361 | T = Context.IntTy; |
| 2362 | } |
| 2363 | |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 2364 | // See if there are any attributes on this declarator chunk. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2365 | if (AttributeList *attrs = const_cast<AttributeList*>(DeclType.getAttrs())) |
| 2366 | processTypeAttrs(state, T, false, attrs); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2367 | } |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2368 | |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2369 | if (LangOpts.CPlusPlus && T->isFunctionType()) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2370 | const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>(); |
Chris Lattner | 778ed74 | 2009-10-25 17:36:50 +0000 | [diff] [blame] | 2371 | assert(FnTy && "Why oh why is there not a FunctionProtoType here?"); |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2372 | |
Douglas Gregor | 708f3b8 | 2010-10-14 22:03:51 +0000 | [diff] [blame] | 2373 | // C++ 8.3.5p4: |
| 2374 | // A cv-qualifier-seq shall only be part of the function type |
| 2375 | // for a nonstatic member function, the function type to which a pointer |
| 2376 | // to member refers, or the top-level function type of a function typedef |
| 2377 | // declaration. |
Douglas Gregor | 683a81f | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 2378 | // |
| 2379 | // Core issue 547 also allows cv-qualifiers on function types that are |
| 2380 | // top-level template type arguments. |
John McCall | 613ef3d | 2010-10-19 01:54:45 +0000 | [diff] [blame] | 2381 | bool FreeFunction; |
| 2382 | if (!D.getCXXScopeSpec().isSet()) { |
Eli Friedman | 906a7e1 | 2012-01-06 03:05:34 +0000 | [diff] [blame] | 2383 | FreeFunction = ((D.getContext() != Declarator::MemberContext && |
| 2384 | D.getContext() != Declarator::LambdaExprContext) || |
John McCall | 613ef3d | 2010-10-19 01:54:45 +0000 | [diff] [blame] | 2385 | D.getDeclSpec().isFriendSpecified()); |
| 2386 | } else { |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2387 | DeclContext *DC = S.computeDeclContext(D.getCXXScopeSpec()); |
John McCall | 613ef3d | 2010-10-19 01:54:45 +0000 | [diff] [blame] | 2388 | FreeFunction = (DC && !DC->isRecord()); |
| 2389 | } |
| 2390 | |
Richard Smith | 55dec86 | 2011-09-30 00:45:47 +0000 | [diff] [blame] | 2391 | // C++0x [dcl.constexpr]p8: A constexpr specifier for a non-static member |
| 2392 | // function that is not a constructor declares that function to be const. |
| 2393 | if (D.getDeclSpec().isConstexprSpecified() && !FreeFunction && |
Richard Smith | 1bf9a9e | 2011-11-12 22:28:03 +0000 | [diff] [blame] | 2394 | D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_static && |
Richard Smith | 55dec86 | 2011-09-30 00:45:47 +0000 | [diff] [blame] | 2395 | D.getName().getKind() != UnqualifiedId::IK_ConstructorName && |
| 2396 | D.getName().getKind() != UnqualifiedId::IK_ConstructorTemplateId && |
| 2397 | !(FnTy->getTypeQuals() & DeclSpec::TQ_const)) { |
| 2398 | // Rebuild function type adding a 'const' qualifier. |
| 2399 | FunctionProtoType::ExtProtoInfo EPI = FnTy->getExtProtoInfo(); |
| 2400 | EPI.TypeQuals |= DeclSpec::TQ_const; |
| 2401 | T = Context.getFunctionType(FnTy->getResultType(), |
| 2402 | FnTy->arg_type_begin(), |
| 2403 | FnTy->getNumArgs(), EPI); |
| 2404 | } |
| 2405 | |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 2406 | // C++0x [dcl.fct]p6: |
| 2407 | // A ref-qualifier shall only be part of the function type for a |
| 2408 | // non-static member function, the function type to which a pointer to |
| 2409 | // member refers, or the top-level function type of a function typedef |
| 2410 | // declaration. |
| 2411 | if ((FnTy->getTypeQuals() != 0 || FnTy->getRefQualifier()) && |
Douglas Gregor | 683a81f | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 2412 | !(D.getContext() == Declarator::TemplateTypeArgContext && |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2413 | !D.isFunctionDeclarator()) && !IsTypedefName && |
Sebastian Redl | c61bb20 | 2010-07-09 21:26:08 +0000 | [diff] [blame] | 2414 | (FreeFunction || |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2415 | D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) { |
Douglas Gregor | 683a81f | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 2416 | if (D.getContext() == Declarator::TemplateTypeArgContext) { |
| 2417 | // Accept qualified function types as template type arguments as a GNU |
| 2418 | // extension. This is also the subject of C++ core issue 547. |
| 2419 | std::string Quals; |
| 2420 | if (FnTy->getTypeQuals() != 0) |
| 2421 | Quals = Qualifiers::fromCVRMask(FnTy->getTypeQuals()).getAsString(); |
| 2422 | |
| 2423 | switch (FnTy->getRefQualifier()) { |
| 2424 | case RQ_None: |
| 2425 | break; |
| 2426 | |
| 2427 | case RQ_LValue: |
| 2428 | if (!Quals.empty()) |
| 2429 | Quals += ' '; |
| 2430 | Quals += '&'; |
| 2431 | break; |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 2432 | |
Douglas Gregor | 683a81f | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 2433 | case RQ_RValue: |
| 2434 | if (!Quals.empty()) |
| 2435 | Quals += ' '; |
| 2436 | Quals += "&&"; |
| 2437 | break; |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 2438 | } |
Douglas Gregor | 683a81f | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 2439 | |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2440 | S.Diag(D.getIdentifierLoc(), |
Douglas Gregor | 683a81f | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 2441 | diag::ext_qualified_function_type_template_arg) |
| 2442 | << Quals; |
| 2443 | } else { |
| 2444 | if (FnTy->getTypeQuals() != 0) { |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 2445 | if (D.isFunctionDeclarator()) { |
| 2446 | SourceRange Range = D.getIdentifierLoc(); |
| 2447 | for (unsigned I = 0, N = D.getNumTypeObjects(); I != N; ++I) { |
| 2448 | const DeclaratorChunk &Chunk = D.getTypeObject(N-I-1); |
| 2449 | if (Chunk.Kind == DeclaratorChunk::Function && |
| 2450 | Chunk.Fun.TypeQuals != 0) { |
| 2451 | switch (Chunk.Fun.TypeQuals) { |
| 2452 | case Qualifiers::Const: |
| 2453 | Range = Chunk.Fun.getConstQualifierLoc(); |
| 2454 | break; |
| 2455 | case Qualifiers::Volatile: |
| 2456 | Range = Chunk.Fun.getVolatileQualifierLoc(); |
| 2457 | break; |
| 2458 | case Qualifiers::Const | Qualifiers::Volatile: { |
| 2459 | SourceLocation CLoc = Chunk.Fun.getConstQualifierLoc(); |
| 2460 | SourceLocation VLoc = Chunk.Fun.getVolatileQualifierLoc(); |
| 2461 | if (S.getSourceManager() |
| 2462 | .isBeforeInTranslationUnit(CLoc, VLoc)) { |
| 2463 | Range = SourceRange(CLoc, VLoc); |
| 2464 | } else { |
| 2465 | Range = SourceRange(VLoc, CLoc); |
| 2466 | } |
| 2467 | } |
| 2468 | break; |
| 2469 | } |
| 2470 | break; |
| 2471 | } |
| 2472 | } |
| 2473 | S.Diag(Range.getBegin(), diag::err_invalid_qualified_function_type) |
| 2474 | << FixItHint::CreateRemoval(Range); |
| 2475 | } else |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2476 | S.Diag(D.getIdentifierLoc(), |
Douglas Gregor | 683a81f | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 2477 | diag::err_invalid_qualified_typedef_function_type_use) |
| 2478 | << FreeFunction; |
| 2479 | } |
| 2480 | |
| 2481 | if (FnTy->getRefQualifier()) { |
| 2482 | if (D.isFunctionDeclarator()) { |
| 2483 | SourceLocation Loc = D.getIdentifierLoc(); |
| 2484 | for (unsigned I = 0, N = D.getNumTypeObjects(); I != N; ++I) { |
| 2485 | const DeclaratorChunk &Chunk = D.getTypeObject(N-I-1); |
| 2486 | if (Chunk.Kind == DeclaratorChunk::Function && |
| 2487 | Chunk.Fun.hasRefQualifier()) { |
| 2488 | Loc = Chunk.Fun.getRefQualifierLoc(); |
| 2489 | break; |
| 2490 | } |
| 2491 | } |
| 2492 | |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2493 | S.Diag(Loc, diag::err_invalid_ref_qualifier_function_type) |
Douglas Gregor | 683a81f | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 2494 | << (FnTy->getRefQualifier() == RQ_LValue) |
| 2495 | << FixItHint::CreateRemoval(Loc); |
| 2496 | } else { |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2497 | S.Diag(D.getIdentifierLoc(), |
Douglas Gregor | 683a81f | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 2498 | diag::err_invalid_ref_qualifier_typedef_function_type_use) |
| 2499 | << FreeFunction |
| 2500 | << (FnTy->getRefQualifier() == RQ_LValue); |
| 2501 | } |
| 2502 | } |
| 2503 | |
| 2504 | // Strip the cv-qualifiers and ref-qualifiers from the type. |
| 2505 | FunctionProtoType::ExtProtoInfo EPI = FnTy->getExtProtoInfo(); |
| 2506 | EPI.TypeQuals = 0; |
| 2507 | EPI.RefQualifier = RQ_None; |
| 2508 | |
| 2509 | T = Context.getFunctionType(FnTy->getResultType(), |
| 2510 | FnTy->arg_type_begin(), |
| 2511 | FnTy->getNumArgs(), EPI); |
Douglas Gregor | c938c16 | 2011-01-26 05:01:58 +0000 | [diff] [blame] | 2512 | } |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 2513 | } |
| 2514 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2515 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2516 | // Apply any undistributed attributes from the declarator. |
| 2517 | if (!T.isNull()) |
| 2518 | if (AttributeList *attrs = D.getAttributes()) |
| 2519 | processTypeAttrs(state, T, false, attrs); |
| 2520 | |
| 2521 | // Diagnose any ignored type attributes. |
| 2522 | if (!T.isNull()) state.diagnoseIgnoredTypeAttrs(T); |
| 2523 | |
Peter Collingbourne | 148f1f7 | 2011-03-20 08:06:45 +0000 | [diff] [blame] | 2524 | // C++0x [dcl.constexpr]p9: |
| 2525 | // A constexpr specifier used in an object declaration declares the object |
| 2526 | // as const. |
| 2527 | if (D.getDeclSpec().isConstexprSpecified() && T->isObjectType()) { |
Sebastian Redl | 7378012 | 2010-06-09 21:19:43 +0000 | [diff] [blame] | 2528 | T.addConst(); |
| 2529 | } |
| 2530 | |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 2531 | // If there was an ellipsis in the declarator, the declaration declares a |
| 2532 | // parameter pack whose type may be a pack expansion type. |
| 2533 | if (D.hasEllipsis() && !T.isNull()) { |
| 2534 | // C++0x [dcl.fct]p13: |
| 2535 | // A declarator-id or abstract-declarator containing an ellipsis shall |
| 2536 | // only be used in a parameter-declaration. Such a parameter-declaration |
| 2537 | // is a parameter pack (14.5.3). [...] |
| 2538 | switch (D.getContext()) { |
| 2539 | case Declarator::PrototypeContext: |
| 2540 | // C++0x [dcl.fct]p13: |
| 2541 | // [...] When it is part of a parameter-declaration-clause, the |
| 2542 | // parameter pack is a function parameter pack (14.5.3). The type T |
| 2543 | // of the declarator-id of the function parameter pack shall contain |
| 2544 | // a template parameter pack; each template parameter pack in T is |
| 2545 | // expanded by the function parameter pack. |
| 2546 | // |
| 2547 | // We represent function parameter packs as function parameters whose |
| 2548 | // type is a pack expansion. |
| 2549 | if (!T->containsUnexpandedParameterPack()) { |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2550 | S.Diag(D.getEllipsisLoc(), |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 2551 | diag::err_function_parameter_pack_without_parameter_packs) |
| 2552 | << T << D.getSourceRange(); |
| 2553 | D.setEllipsisLoc(SourceLocation()); |
| 2554 | } else { |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2555 | T = Context.getPackExpansionType(T, llvm::Optional<unsigned>()); |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 2556 | } |
| 2557 | break; |
| 2558 | |
| 2559 | case Declarator::TemplateParamContext: |
| 2560 | // C++0x [temp.param]p15: |
| 2561 | // If a template-parameter is a [...] is a parameter-declaration that |
| 2562 | // declares a parameter pack (8.3.5), then the template-parameter is a |
| 2563 | // template parameter pack (14.5.3). |
| 2564 | // |
| 2565 | // Note: core issue 778 clarifies that, if there are any unexpanded |
| 2566 | // parameter packs in the type of the non-type template parameter, then |
| 2567 | // it expands those parameter packs. |
| 2568 | if (T->containsUnexpandedParameterPack()) |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2569 | T = Context.getPackExpansionType(T, llvm::Optional<unsigned>()); |
Richard Smith | e5acd13 | 2011-10-14 20:31:37 +0000 | [diff] [blame] | 2570 | else |
| 2571 | S.Diag(D.getEllipsisLoc(), |
| 2572 | LangOpts.CPlusPlus0x |
| 2573 | ? diag::warn_cxx98_compat_variadic_templates |
| 2574 | : diag::ext_variadic_templates); |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 2575 | break; |
| 2576 | |
| 2577 | case Declarator::FileContext: |
| 2578 | case Declarator::KNRTypeListContext: |
John McCall | cdda47f | 2011-10-01 09:56:14 +0000 | [diff] [blame] | 2579 | case Declarator::ObjCParameterContext: // FIXME: special diagnostic here? |
| 2580 | case Declarator::ObjCResultContext: // FIXME: special diagnostic here? |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 2581 | case Declarator::TypeNameContext: |
Argyrios Kyrtzidis | 0b8c98f | 2011-06-28 03:01:23 +0000 | [diff] [blame] | 2582 | case Declarator::CXXNewContext: |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2583 | case Declarator::AliasDeclContext: |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2584 | case Declarator::AliasTemplateContext: |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 2585 | case Declarator::MemberContext: |
| 2586 | case Declarator::BlockContext: |
| 2587 | case Declarator::ForContext: |
| 2588 | case Declarator::ConditionContext: |
| 2589 | case Declarator::CXXCatchContext: |
Argyrios Kyrtzidis | 17b6399 | 2011-07-01 22:22:40 +0000 | [diff] [blame] | 2590 | case Declarator::ObjCCatchContext: |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 2591 | case Declarator::BlockLiteralContext: |
Eli Friedman | f88c400 | 2012-01-04 04:41:38 +0000 | [diff] [blame] | 2592 | case Declarator::LambdaExprContext: |
Douglas Gregor | 683a81f | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 2593 | case Declarator::TemplateTypeArgContext: |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 2594 | // FIXME: We may want to allow parameter packs in block-literal contexts |
| 2595 | // in the future. |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2596 | S.Diag(D.getEllipsisLoc(), diag::err_ellipsis_in_declarator_not_parameter); |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 2597 | D.setEllipsisLoc(SourceLocation()); |
| 2598 | break; |
| 2599 | } |
| 2600 | } |
Richard Smith | e7397c6 | 2011-02-22 00:36:53 +0000 | [diff] [blame] | 2601 | |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 2602 | if (T.isNull()) |
| 2603 | return Context.getNullTypeSourceInfo(); |
| 2604 | else if (D.isInvalidType()) |
| 2605 | return Context.getTrivialTypeSourceInfo(T); |
Argyrios Kyrtzidis | db7abf7 | 2011-06-28 03:01:12 +0000 | [diff] [blame] | 2606 | |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2607 | return S.GetTypeSourceInfoForDeclarator(D, T, TInfo); |
| 2608 | } |
| 2609 | |
| 2610 | /// GetTypeForDeclarator - Convert the type for the specified |
| 2611 | /// declarator to Type instances. |
| 2612 | /// |
| 2613 | /// The result of this call will never be null, but the associated |
| 2614 | /// type may be a null type if there's an unrecoverable error. |
| 2615 | TypeSourceInfo *Sema::GetTypeForDeclarator(Declarator &D, Scope *S) { |
| 2616 | // Determine the type of the declarator. Not all forms of declarator |
| 2617 | // have a type. |
| 2618 | |
| 2619 | TypeProcessingState state(*this, D); |
| 2620 | |
| 2621 | TypeSourceInfo *ReturnTypeInfo = 0; |
| 2622 | QualType T = GetDeclSpecTypeForDeclarator(state, ReturnTypeInfo); |
| 2623 | if (T.isNull()) |
| 2624 | return Context.getNullTypeSourceInfo(); |
| 2625 | |
| 2626 | if (D.isPrototypeContext() && getLangOptions().ObjCAutoRefCount) |
| 2627 | inferARCWriteback(state, T); |
Argyrios Kyrtzidis | db7abf7 | 2011-06-28 03:01:12 +0000 | [diff] [blame] | 2628 | |
Argyrios Kyrtzidis | 8cfa57b | 2011-07-01 22:22:42 +0000 | [diff] [blame] | 2629 | return GetFullTypeForDeclarator(state, T, ReturnTypeInfo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2630 | } |
| 2631 | |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 2632 | static void transferARCOwnershipToDeclSpec(Sema &S, |
| 2633 | QualType &declSpecTy, |
| 2634 | Qualifiers::ObjCLifetime ownership) { |
| 2635 | if (declSpecTy->isObjCRetainableType() && |
| 2636 | declSpecTy.getObjCLifetime() == Qualifiers::OCL_None) { |
| 2637 | Qualifiers qs; |
| 2638 | qs.addObjCLifetime(ownership); |
| 2639 | declSpecTy = S.Context.getQualifiedType(declSpecTy, qs); |
| 2640 | } |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 2641 | } |
| 2642 | |
| 2643 | static void transferARCOwnershipToDeclaratorChunk(TypeProcessingState &state, |
| 2644 | Qualifiers::ObjCLifetime ownership, |
| 2645 | unsigned chunkIndex) { |
| 2646 | Sema &S = state.getSema(); |
| 2647 | Declarator &D = state.getDeclarator(); |
| 2648 | |
| 2649 | // Look for an explicit lifetime attribute. |
| 2650 | DeclaratorChunk &chunk = D.getTypeObject(chunkIndex); |
| 2651 | for (const AttributeList *attr = chunk.getAttrs(); attr; |
| 2652 | attr = attr->getNext()) |
| 2653 | if (attr->getKind() == AttributeList::AT_objc_ownership) |
| 2654 | return; |
| 2655 | |
| 2656 | const char *attrStr = 0; |
| 2657 | switch (ownership) { |
David Blaikie | 3026348 | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 2658 | case Qualifiers::OCL_None: llvm_unreachable("no ownership!"); |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 2659 | case Qualifiers::OCL_ExplicitNone: attrStr = "none"; break; |
| 2660 | case Qualifiers::OCL_Strong: attrStr = "strong"; break; |
| 2661 | case Qualifiers::OCL_Weak: attrStr = "weak"; break; |
| 2662 | case Qualifiers::OCL_Autoreleasing: attrStr = "autoreleasing"; break; |
| 2663 | } |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 2664 | |
| 2665 | // If there wasn't one, add one (with an invalid source location |
| 2666 | // so that we don't make an AttributedType for it). |
| 2667 | AttributeList *attr = D.getAttributePool() |
| 2668 | .create(&S.Context.Idents.get("objc_ownership"), SourceLocation(), |
| 2669 | /*scope*/ 0, SourceLocation(), |
| 2670 | &S.Context.Idents.get(attrStr), SourceLocation(), |
| 2671 | /*args*/ 0, 0, |
| 2672 | /*declspec*/ false, /*C++0x*/ false); |
| 2673 | spliceAttrIntoList(*attr, chunk.getAttrListRef()); |
| 2674 | |
| 2675 | // TODO: mark whether we did this inference? |
| 2676 | } |
| 2677 | |
Argyrios Kyrtzidis | 6ee5492 | 2011-10-28 22:54:28 +0000 | [diff] [blame] | 2678 | /// \brief Used for transfering ownership in casts resulting in l-values. |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 2679 | static void transferARCOwnership(TypeProcessingState &state, |
| 2680 | QualType &declSpecTy, |
| 2681 | Qualifiers::ObjCLifetime ownership) { |
| 2682 | Sema &S = state.getSema(); |
| 2683 | Declarator &D = state.getDeclarator(); |
| 2684 | |
| 2685 | int inner = -1; |
Argyrios Kyrtzidis | 6ee5492 | 2011-10-28 22:54:28 +0000 | [diff] [blame] | 2686 | bool hasIndirection = false; |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 2687 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { |
| 2688 | DeclaratorChunk &chunk = D.getTypeObject(i); |
| 2689 | switch (chunk.Kind) { |
| 2690 | case DeclaratorChunk::Paren: |
| 2691 | // Ignore parens. |
| 2692 | break; |
| 2693 | |
| 2694 | case DeclaratorChunk::Array: |
| 2695 | case DeclaratorChunk::Reference: |
| 2696 | case DeclaratorChunk::Pointer: |
Argyrios Kyrtzidis | 6ee5492 | 2011-10-28 22:54:28 +0000 | [diff] [blame] | 2697 | if (inner != -1) |
| 2698 | hasIndirection = true; |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 2699 | inner = i; |
| 2700 | break; |
| 2701 | |
| 2702 | case DeclaratorChunk::BlockPointer: |
Argyrios Kyrtzidis | 6ee5492 | 2011-10-28 22:54:28 +0000 | [diff] [blame] | 2703 | if (inner != -1) |
| 2704 | transferARCOwnershipToDeclaratorChunk(state, ownership, i); |
| 2705 | return; |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 2706 | |
| 2707 | case DeclaratorChunk::Function: |
| 2708 | case DeclaratorChunk::MemberPointer: |
| 2709 | return; |
| 2710 | } |
| 2711 | } |
| 2712 | |
| 2713 | if (inner == -1) |
Argyrios Kyrtzidis | 6ee5492 | 2011-10-28 22:54:28 +0000 | [diff] [blame] | 2714 | return; |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 2715 | |
| 2716 | DeclaratorChunk &chunk = D.getTypeObject(inner); |
| 2717 | if (chunk.Kind == DeclaratorChunk::Pointer) { |
| 2718 | if (declSpecTy->isObjCRetainableType()) |
| 2719 | return transferARCOwnershipToDeclSpec(S, declSpecTy, ownership); |
Argyrios Kyrtzidis | 6ee5492 | 2011-10-28 22:54:28 +0000 | [diff] [blame] | 2720 | if (declSpecTy->isObjCObjectType() && hasIndirection) |
Argyrios Kyrtzidis | 31862ba | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 2721 | return transferARCOwnershipToDeclaratorChunk(state, ownership, inner); |
| 2722 | } else { |
| 2723 | assert(chunk.Kind == DeclaratorChunk::Array || |
| 2724 | chunk.Kind == DeclaratorChunk::Reference); |
| 2725 | return transferARCOwnershipToDeclSpec(S, declSpecTy, ownership); |
| 2726 | } |
| 2727 | } |
| 2728 | |
| 2729 | TypeSourceInfo *Sema::GetTypeForDeclaratorCast(Declarator &D, QualType FromTy) { |
| 2730 | TypeProcessingState state(*this, D); |
| 2731 | |
| 2732 | TypeSourceInfo *ReturnTypeInfo = 0; |
| 2733 | QualType declSpecTy = GetDeclSpecTypeForDeclarator(state, ReturnTypeInfo); |
| 2734 | if (declSpecTy.isNull()) |
| 2735 | return Context.getNullTypeSourceInfo(); |
| 2736 | |
| 2737 | if (getLangOptions().ObjCAutoRefCount) { |
| 2738 | Qualifiers::ObjCLifetime ownership = Context.getInnerObjCOwnership(FromTy); |
| 2739 | if (ownership != Qualifiers::OCL_None) |
| 2740 | transferARCOwnership(state, declSpecTy, ownership); |
| 2741 | } |
| 2742 | |
| 2743 | return GetFullTypeForDeclarator(state, declSpecTy, ReturnTypeInfo); |
| 2744 | } |
| 2745 | |
John McCall | 14aa217 | 2011-03-04 04:00:19 +0000 | [diff] [blame] | 2746 | /// Map an AttributedType::Kind to an AttributeList::Kind. |
| 2747 | static AttributeList::Kind getAttrListKind(AttributedType::Kind kind) { |
| 2748 | switch (kind) { |
| 2749 | case AttributedType::attr_address_space: |
| 2750 | return AttributeList::AT_address_space; |
| 2751 | case AttributedType::attr_regparm: |
| 2752 | return AttributeList::AT_regparm; |
| 2753 | case AttributedType::attr_vector_size: |
| 2754 | return AttributeList::AT_vector_size; |
| 2755 | case AttributedType::attr_neon_vector_type: |
| 2756 | return AttributeList::AT_neon_vector_type; |
| 2757 | case AttributedType::attr_neon_polyvector_type: |
| 2758 | return AttributeList::AT_neon_polyvector_type; |
| 2759 | case AttributedType::attr_objc_gc: |
| 2760 | return AttributeList::AT_objc_gc; |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 2761 | case AttributedType::attr_objc_ownership: |
| 2762 | return AttributeList::AT_objc_ownership; |
John McCall | 14aa217 | 2011-03-04 04:00:19 +0000 | [diff] [blame] | 2763 | case AttributedType::attr_noreturn: |
| 2764 | return AttributeList::AT_noreturn; |
| 2765 | case AttributedType::attr_cdecl: |
| 2766 | return AttributeList::AT_cdecl; |
| 2767 | case AttributedType::attr_fastcall: |
| 2768 | return AttributeList::AT_fastcall; |
| 2769 | case AttributedType::attr_stdcall: |
| 2770 | return AttributeList::AT_stdcall; |
| 2771 | case AttributedType::attr_thiscall: |
| 2772 | return AttributeList::AT_thiscall; |
| 2773 | case AttributedType::attr_pascal: |
| 2774 | return AttributeList::AT_pascal; |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 2775 | case AttributedType::attr_pcs: |
| 2776 | return AttributeList::AT_pcs; |
John McCall | 14aa217 | 2011-03-04 04:00:19 +0000 | [diff] [blame] | 2777 | } |
| 2778 | llvm_unreachable("unexpected attribute kind!"); |
John McCall | 14aa217 | 2011-03-04 04:00:19 +0000 | [diff] [blame] | 2779 | } |
| 2780 | |
| 2781 | static void fillAttributedTypeLoc(AttributedTypeLoc TL, |
| 2782 | const AttributeList *attrs) { |
| 2783 | AttributedType::Kind kind = TL.getAttrKind(); |
| 2784 | |
| 2785 | assert(attrs && "no type attributes in the expected location!"); |
| 2786 | AttributeList::Kind parsedKind = getAttrListKind(kind); |
| 2787 | while (attrs->getKind() != parsedKind) { |
| 2788 | attrs = attrs->getNext(); |
| 2789 | assert(attrs && "no matching attribute in expected location!"); |
| 2790 | } |
| 2791 | |
| 2792 | TL.setAttrNameLoc(attrs->getLoc()); |
| 2793 | if (TL.hasAttrExprOperand()) |
| 2794 | TL.setAttrExprOperand(attrs->getArg(0)); |
| 2795 | else if (TL.hasAttrEnumOperand()) |
| 2796 | TL.setAttrEnumOperandLoc(attrs->getParameterLoc()); |
| 2797 | |
| 2798 | // FIXME: preserve this information to here. |
| 2799 | if (TL.hasAttrOperand()) |
| 2800 | TL.setAttrOperandParensRange(SourceRange()); |
| 2801 | } |
| 2802 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2803 | namespace { |
| 2804 | class TypeSpecLocFiller : public TypeLocVisitor<TypeSpecLocFiller> { |
Douglas Gregor | c21c7e9 | 2011-01-25 19:13:18 +0000 | [diff] [blame] | 2805 | ASTContext &Context; |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2806 | const DeclSpec &DS; |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 2807 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2808 | public: |
Douglas Gregor | c21c7e9 | 2011-01-25 19:13:18 +0000 | [diff] [blame] | 2809 | TypeSpecLocFiller(ASTContext &Context, const DeclSpec &DS) |
| 2810 | : Context(Context), DS(DS) {} |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 2811 | |
John McCall | 14aa217 | 2011-03-04 04:00:19 +0000 | [diff] [blame] | 2812 | void VisitAttributedTypeLoc(AttributedTypeLoc TL) { |
| 2813 | fillAttributedTypeLoc(TL, DS.getAttributes().getList()); |
| 2814 | Visit(TL.getModifiedLoc()); |
| 2815 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2816 | void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { |
| 2817 | Visit(TL.getUnqualifiedLoc()); |
| 2818 | } |
| 2819 | void VisitTypedefTypeLoc(TypedefTypeLoc TL) { |
| 2820 | TL.setNameLoc(DS.getTypeSpecTypeLoc()); |
| 2821 | } |
| 2822 | void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { |
| 2823 | TL.setNameLoc(DS.getTypeSpecTypeLoc()); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2824 | } |
| 2825 | void VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) { |
| 2826 | // Handle the base type, which might not have been written explicitly. |
| 2827 | if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) { |
| 2828 | TL.setHasBaseTypeAsWritten(false); |
Douglas Gregor | c21c7e9 | 2011-01-25 19:13:18 +0000 | [diff] [blame] | 2829 | TL.getBaseLoc().initialize(Context, SourceLocation()); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2830 | } else { |
| 2831 | TL.setHasBaseTypeAsWritten(true); |
| 2832 | Visit(TL.getBaseLoc()); |
| 2833 | } |
Argyrios Kyrtzidis | eb66759 | 2009-09-29 19:45:22 +0000 | [diff] [blame] | 2834 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2835 | // Protocol qualifiers. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2836 | if (DS.getProtocolQualifiers()) { |
| 2837 | assert(TL.getNumProtocols() > 0); |
| 2838 | assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers()); |
| 2839 | TL.setLAngleLoc(DS.getProtocolLAngleLoc()); |
| 2840 | TL.setRAngleLoc(DS.getSourceRange().getEnd()); |
| 2841 | for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i) |
| 2842 | TL.setProtocolLoc(i, DS.getProtocolLocs()[i]); |
| 2843 | } else { |
| 2844 | assert(TL.getNumProtocols() == 0); |
| 2845 | TL.setLAngleLoc(SourceLocation()); |
| 2846 | TL.setRAngleLoc(SourceLocation()); |
| 2847 | } |
| 2848 | } |
| 2849 | void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2850 | TL.setStarLoc(SourceLocation()); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2851 | Visit(TL.getPointeeLoc()); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2852 | } |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2853 | void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2854 | TypeSourceInfo *TInfo = 0; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2855 | Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2856 | |
| 2857 | // If we got no declarator info from previous Sema routines, |
| 2858 | // just fill with the typespec loc. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2859 | if (!TInfo) { |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 2860 | TL.initialize(Context, DS.getTypeSpecTypeNameLoc()); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2861 | return; |
| 2862 | } |
| 2863 | |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 2864 | TypeLoc OldTL = TInfo->getTypeLoc(); |
| 2865 | if (TInfo->getType()->getAs<ElaboratedType>()) { |
| 2866 | ElaboratedTypeLoc ElabTL = cast<ElaboratedTypeLoc>(OldTL); |
| 2867 | TemplateSpecializationTypeLoc NamedTL = |
| 2868 | cast<TemplateSpecializationTypeLoc>(ElabTL.getNamedTypeLoc()); |
| 2869 | TL.copy(NamedTL); |
| 2870 | } |
| 2871 | else |
| 2872 | TL.copy(cast<TemplateSpecializationTypeLoc>(OldTL)); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2873 | } |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 2874 | void VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { |
| 2875 | assert(DS.getTypeSpecType() == DeclSpec::TST_typeofExpr); |
| 2876 | TL.setTypeofLoc(DS.getTypeSpecTypeLoc()); |
| 2877 | TL.setParensRange(DS.getTypeofParensRange()); |
| 2878 | } |
| 2879 | void VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { |
| 2880 | assert(DS.getTypeSpecType() == DeclSpec::TST_typeofType); |
| 2881 | TL.setTypeofLoc(DS.getTypeSpecTypeLoc()); |
| 2882 | TL.setParensRange(DS.getTypeofParensRange()); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2883 | assert(DS.getRepAsType()); |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 2884 | TypeSourceInfo *TInfo = 0; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2885 | Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo); |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 2886 | TL.setUnderlyingTInfo(TInfo); |
| 2887 | } |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 2888 | void VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) { |
| 2889 | // FIXME: This holds only because we only have one unary transform. |
| 2890 | assert(DS.getTypeSpecType() == DeclSpec::TST_underlyingType); |
| 2891 | TL.setKWLoc(DS.getTypeSpecTypeLoc()); |
| 2892 | TL.setParensRange(DS.getTypeofParensRange()); |
| 2893 | assert(DS.getRepAsType()); |
| 2894 | TypeSourceInfo *TInfo = 0; |
| 2895 | Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo); |
| 2896 | TL.setUnderlyingTInfo(TInfo); |
| 2897 | } |
Douglas Gregor | ddf889a | 2010-01-18 18:04:31 +0000 | [diff] [blame] | 2898 | void VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { |
| 2899 | // By default, use the source location of the type specifier. |
| 2900 | TL.setBuiltinLoc(DS.getTypeSpecTypeLoc()); |
| 2901 | if (TL.needsExtraLocalData()) { |
| 2902 | // Set info for the written builtin specifiers. |
| 2903 | TL.getWrittenBuiltinSpecs() = DS.getWrittenBuiltinSpecs(); |
| 2904 | // Try to have a meaningful source location. |
| 2905 | if (TL.getWrittenSignSpec() != TSS_unspecified) |
| 2906 | // Sign spec loc overrides the others (e.g., 'unsigned long'). |
| 2907 | TL.setBuiltinLoc(DS.getTypeSpecSignLoc()); |
| 2908 | else if (TL.getWrittenWidthSpec() != TSW_unspecified) |
| 2909 | // Width spec loc overrides type spec loc (e.g., 'short int'). |
| 2910 | TL.setBuiltinLoc(DS.getTypeSpecWidthLoc()); |
| 2911 | } |
| 2912 | } |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 2913 | void VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { |
| 2914 | ElaboratedTypeKeyword Keyword |
| 2915 | = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType()); |
Nico Weber | 253e80b | 2010-11-22 10:30:56 +0000 | [diff] [blame] | 2916 | if (DS.getTypeSpecType() == TST_typename) { |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 2917 | TypeSourceInfo *TInfo = 0; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2918 | Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo); |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 2919 | if (TInfo) { |
| 2920 | TL.copy(cast<ElaboratedTypeLoc>(TInfo->getTypeLoc())); |
| 2921 | return; |
| 2922 | } |
| 2923 | } |
| 2924 | TL.setKeywordLoc(Keyword != ETK_None |
| 2925 | ? DS.getTypeSpecTypeLoc() |
| 2926 | : SourceLocation()); |
| 2927 | const CXXScopeSpec& SS = DS.getTypeSpecScope(); |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 2928 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 2929 | Visit(TL.getNextTypeLoc().getUnqualifiedLoc()); |
| 2930 | } |
| 2931 | void VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { |
| 2932 | ElaboratedTypeKeyword Keyword |
| 2933 | = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType()); |
Nico Weber | 253e80b | 2010-11-22 10:30:56 +0000 | [diff] [blame] | 2934 | if (DS.getTypeSpecType() == TST_typename) { |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 2935 | TypeSourceInfo *TInfo = 0; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2936 | Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo); |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 2937 | if (TInfo) { |
| 2938 | TL.copy(cast<DependentNameTypeLoc>(TInfo->getTypeLoc())); |
| 2939 | return; |
| 2940 | } |
| 2941 | } |
| 2942 | TL.setKeywordLoc(Keyword != ETK_None |
| 2943 | ? DS.getTypeSpecTypeLoc() |
| 2944 | : SourceLocation()); |
| 2945 | const CXXScopeSpec& SS = DS.getTypeSpecScope(); |
Douglas Gregor | 2494dd0 | 2011-03-01 01:34:45 +0000 | [diff] [blame] | 2946 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 2947 | TL.setNameLoc(DS.getTypeSpecTypeNameLoc()); |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 2948 | } |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2949 | void VisitDependentTemplateSpecializationTypeLoc( |
| 2950 | DependentTemplateSpecializationTypeLoc TL) { |
| 2951 | ElaboratedTypeKeyword Keyword |
| 2952 | = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType()); |
| 2953 | if (Keyword == ETK_Typename) { |
| 2954 | TypeSourceInfo *TInfo = 0; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2955 | Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2956 | if (TInfo) { |
| 2957 | TL.copy(cast<DependentTemplateSpecializationTypeLoc>( |
| 2958 | TInfo->getTypeLoc())); |
| 2959 | return; |
| 2960 | } |
| 2961 | } |
Douglas Gregor | c21c7e9 | 2011-01-25 19:13:18 +0000 | [diff] [blame] | 2962 | TL.initializeLocal(Context, SourceLocation()); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2963 | TL.setKeywordLoc(Keyword != ETK_None |
| 2964 | ? DS.getTypeSpecTypeLoc() |
| 2965 | : SourceLocation()); |
| 2966 | const CXXScopeSpec& SS = DS.getTypeSpecScope(); |
Douglas Gregor | 94fdffa | 2011-03-01 20:11:18 +0000 | [diff] [blame] | 2967 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 2968 | TL.setNameLoc(DS.getTypeSpecTypeNameLoc()); |
| 2969 | } |
| 2970 | void VisitTagTypeLoc(TagTypeLoc TL) { |
| 2971 | TL.setNameLoc(DS.getTypeSpecTypeNameLoc()); |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2972 | } |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 2973 | void VisitAtomicTypeLoc(AtomicTypeLoc TL) { |
| 2974 | TL.setKWLoc(DS.getTypeSpecTypeLoc()); |
| 2975 | TL.setParensRange(DS.getTypeofParensRange()); |
Douglas Gregor | 43fe245 | 2011-10-09 18:45:17 +0000 | [diff] [blame] | 2976 | |
| 2977 | TypeSourceInfo *TInfo = 0; |
| 2978 | Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo); |
| 2979 | TL.getValueLoc().initializeFullCopy(TInfo->getTypeLoc()); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 2980 | } |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 2981 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2982 | void VisitTypeLoc(TypeLoc TL) { |
| 2983 | // FIXME: add other typespec types and change this to an assert. |
Douglas Gregor | c21c7e9 | 2011-01-25 19:13:18 +0000 | [diff] [blame] | 2984 | TL.initialize(Context, DS.getTypeSpecTypeLoc()); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2985 | } |
| 2986 | }; |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 2987 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2988 | class DeclaratorLocFiller : public TypeLocVisitor<DeclaratorLocFiller> { |
Abramo Bagnara | b6ab6c1 | 2011-03-05 14:42:21 +0000 | [diff] [blame] | 2989 | ASTContext &Context; |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2990 | const DeclaratorChunk &Chunk; |
| 2991 | |
| 2992 | public: |
Abramo Bagnara | b6ab6c1 | 2011-03-05 14:42:21 +0000 | [diff] [blame] | 2993 | DeclaratorLocFiller(ASTContext &Context, const DeclaratorChunk &Chunk) |
| 2994 | : Context(Context), Chunk(Chunk) {} |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2995 | |
| 2996 | void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 2997 | llvm_unreachable("qualified type locs not expected here!"); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 2998 | } |
| 2999 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3000 | void VisitAttributedTypeLoc(AttributedTypeLoc TL) { |
| 3001 | fillAttributedTypeLoc(TL, Chunk.getAttrs()); |
| 3002 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 3003 | void VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { |
| 3004 | assert(Chunk.Kind == DeclaratorChunk::BlockPointer); |
| 3005 | TL.setCaretLoc(Chunk.Loc); |
| 3006 | } |
| 3007 | void VisitPointerTypeLoc(PointerTypeLoc TL) { |
| 3008 | assert(Chunk.Kind == DeclaratorChunk::Pointer); |
| 3009 | TL.setStarLoc(Chunk.Loc); |
| 3010 | } |
| 3011 | void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { |
| 3012 | assert(Chunk.Kind == DeclaratorChunk::Pointer); |
| 3013 | TL.setStarLoc(Chunk.Loc); |
| 3014 | } |
| 3015 | void VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { |
| 3016 | assert(Chunk.Kind == DeclaratorChunk::MemberPointer); |
Abramo Bagnara | b6ab6c1 | 2011-03-05 14:42:21 +0000 | [diff] [blame] | 3017 | const CXXScopeSpec& SS = Chunk.Mem.Scope(); |
| 3018 | NestedNameSpecifierLoc NNSLoc = SS.getWithLocInContext(Context); |
| 3019 | |
| 3020 | const Type* ClsTy = TL.getClass(); |
| 3021 | QualType ClsQT = QualType(ClsTy, 0); |
| 3022 | TypeSourceInfo *ClsTInfo = Context.CreateTypeSourceInfo(ClsQT, 0); |
| 3023 | // Now copy source location info into the type loc component. |
| 3024 | TypeLoc ClsTL = ClsTInfo->getTypeLoc(); |
| 3025 | switch (NNSLoc.getNestedNameSpecifier()->getKind()) { |
| 3026 | case NestedNameSpecifier::Identifier: |
| 3027 | assert(isa<DependentNameType>(ClsTy) && "Unexpected TypeLoc"); |
| 3028 | { |
Abramo Bagnara | fd9c42e | 2011-03-06 22:48:24 +0000 | [diff] [blame] | 3029 | DependentNameTypeLoc DNTLoc = cast<DependentNameTypeLoc>(ClsTL); |
Abramo Bagnara | b6ab6c1 | 2011-03-05 14:42:21 +0000 | [diff] [blame] | 3030 | DNTLoc.setKeywordLoc(SourceLocation()); |
| 3031 | DNTLoc.setQualifierLoc(NNSLoc.getPrefix()); |
| 3032 | DNTLoc.setNameLoc(NNSLoc.getLocalBeginLoc()); |
| 3033 | } |
| 3034 | break; |
| 3035 | |
| 3036 | case NestedNameSpecifier::TypeSpec: |
| 3037 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 3038 | if (isa<ElaboratedType>(ClsTy)) { |
| 3039 | ElaboratedTypeLoc ETLoc = *cast<ElaboratedTypeLoc>(&ClsTL); |
| 3040 | ETLoc.setKeywordLoc(SourceLocation()); |
| 3041 | ETLoc.setQualifierLoc(NNSLoc.getPrefix()); |
| 3042 | TypeLoc NamedTL = ETLoc.getNamedTypeLoc(); |
| 3043 | NamedTL.initializeFullCopy(NNSLoc.getTypeLoc()); |
| 3044 | } else { |
| 3045 | ClsTL.initializeFullCopy(NNSLoc.getTypeLoc()); |
| 3046 | } |
| 3047 | break; |
| 3048 | |
| 3049 | case NestedNameSpecifier::Namespace: |
| 3050 | case NestedNameSpecifier::NamespaceAlias: |
| 3051 | case NestedNameSpecifier::Global: |
| 3052 | llvm_unreachable("Nested-name-specifier must name a type"); |
Abramo Bagnara | b6ab6c1 | 2011-03-05 14:42:21 +0000 | [diff] [blame] | 3053 | } |
| 3054 | |
| 3055 | // Finally fill in MemberPointerLocInfo fields. |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 3056 | TL.setStarLoc(Chunk.Loc); |
Abramo Bagnara | b6ab6c1 | 2011-03-05 14:42:21 +0000 | [diff] [blame] | 3057 | TL.setClassTInfo(ClsTInfo); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 3058 | } |
| 3059 | void VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { |
| 3060 | assert(Chunk.Kind == DeclaratorChunk::Reference); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3061 | // 'Amp' is misleading: this might have been originally |
| 3062 | /// spelled with AmpAmp. |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 3063 | TL.setAmpLoc(Chunk.Loc); |
| 3064 | } |
| 3065 | void VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { |
| 3066 | assert(Chunk.Kind == DeclaratorChunk::Reference); |
| 3067 | assert(!Chunk.Ref.LValueRef); |
| 3068 | TL.setAmpAmpLoc(Chunk.Loc); |
| 3069 | } |
| 3070 | void VisitArrayTypeLoc(ArrayTypeLoc TL) { |
| 3071 | assert(Chunk.Kind == DeclaratorChunk::Array); |
| 3072 | TL.setLBracketLoc(Chunk.Loc); |
| 3073 | TL.setRBracketLoc(Chunk.EndLoc); |
| 3074 | TL.setSizeExpr(static_cast<Expr*>(Chunk.Arr.NumElts)); |
| 3075 | } |
| 3076 | void VisitFunctionTypeLoc(FunctionTypeLoc TL) { |
| 3077 | assert(Chunk.Kind == DeclaratorChunk::Function); |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 3078 | TL.setLocalRangeBegin(Chunk.Loc); |
| 3079 | TL.setLocalRangeEnd(Chunk.EndLoc); |
Douglas Gregor | dab60ad | 2010-10-01 18:44:50 +0000 | [diff] [blame] | 3080 | TL.setTrailingReturn(!!Chunk.Fun.TrailingReturnType); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 3081 | |
| 3082 | const DeclaratorChunk::FunctionTypeInfo &FTI = Chunk.Fun; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3083 | for (unsigned i = 0, e = TL.getNumArgs(), tpi = 0; i != e; ++i) { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3084 | ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3085 | TL.setArg(tpi++, Param); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 3086 | } |
| 3087 | // FIXME: exception specs |
| 3088 | } |
Abramo Bagnara | 075f8f1 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 3089 | void VisitParenTypeLoc(ParenTypeLoc TL) { |
| 3090 | assert(Chunk.Kind == DeclaratorChunk::Paren); |
| 3091 | TL.setLParenLoc(Chunk.Loc); |
| 3092 | TL.setRParenLoc(Chunk.EndLoc); |
| 3093 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 3094 | |
| 3095 | void VisitTypeLoc(TypeLoc TL) { |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 3096 | llvm_unreachable("unsupported TypeLoc kind in declarator!"); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 3097 | } |
| 3098 | }; |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 3099 | } |
| 3100 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3101 | /// \brief Create and instantiate a TypeSourceInfo with type source information. |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 3102 | /// |
| 3103 | /// \param T QualType referring to the type as written in source code. |
Douglas Gregor | 05baacb | 2010-04-12 23:19:01 +0000 | [diff] [blame] | 3104 | /// |
| 3105 | /// \param ReturnTypeInfo For declarators whose return type does not show |
| 3106 | /// up in the normal place in the declaration specifiers (such as a C++ |
| 3107 | /// conversion function), this pointer will refer to a type source information |
| 3108 | /// for that return type. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3109 | TypeSourceInfo * |
Douglas Gregor | 05baacb | 2010-04-12 23:19:01 +0000 | [diff] [blame] | 3110 | Sema::GetTypeSourceInfoForDeclarator(Declarator &D, QualType T, |
| 3111 | TypeSourceInfo *ReturnTypeInfo) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3112 | TypeSourceInfo *TInfo = Context.CreateTypeSourceInfo(T); |
| 3113 | UnqualTypeLoc CurrTL = TInfo->getTypeLoc().getUnqualifiedLoc(); |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 3114 | |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 3115 | // Handle parameter packs whose type is a pack expansion. |
| 3116 | if (isa<PackExpansionType>(T)) { |
| 3117 | cast<PackExpansionTypeLoc>(CurrTL).setEllipsisLoc(D.getEllipsisLoc()); |
| 3118 | CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc(); |
| 3119 | } |
| 3120 | |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 3121 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { |
John McCall | 14aa217 | 2011-03-04 04:00:19 +0000 | [diff] [blame] | 3122 | while (isa<AttributedTypeLoc>(CurrTL)) { |
| 3123 | AttributedTypeLoc TL = cast<AttributedTypeLoc>(CurrTL); |
| 3124 | fillAttributedTypeLoc(TL, D.getTypeObject(i).getAttrs()); |
| 3125 | CurrTL = TL.getNextTypeLoc().getUnqualifiedLoc(); |
| 3126 | } |
| 3127 | |
Abramo Bagnara | b6ab6c1 | 2011-03-05 14:42:21 +0000 | [diff] [blame] | 3128 | DeclaratorLocFiller(Context, D.getTypeObject(i)).Visit(CurrTL); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 3129 | CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc(); |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 3130 | } |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 3131 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3132 | // If we have different source information for the return type, use |
| 3133 | // that. This really only applies to C++ conversion functions. |
| 3134 | if (ReturnTypeInfo) { |
Douglas Gregor | 05baacb | 2010-04-12 23:19:01 +0000 | [diff] [blame] | 3135 | TypeLoc TL = ReturnTypeInfo->getTypeLoc(); |
| 3136 | assert(TL.getFullDataSize() == CurrTL.getFullDataSize()); |
| 3137 | memcpy(CurrTL.getOpaqueData(), TL.getOpaqueData(), TL.getFullDataSize()); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3138 | } else { |
Douglas Gregor | c21c7e9 | 2011-01-25 19:13:18 +0000 | [diff] [blame] | 3139 | TypeSpecLocFiller(Context, D.getDeclSpec()).Visit(CurrTL); |
Douglas Gregor | 05baacb | 2010-04-12 23:19:01 +0000 | [diff] [blame] | 3140 | } |
| 3141 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3142 | return TInfo; |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 3143 | } |
| 3144 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3145 | /// \brief Create a LocInfoType to hold the given QualType and TypeSourceInfo. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3146 | ParsedType Sema::CreateParsedType(QualType T, TypeSourceInfo *TInfo) { |
Argyrios Kyrtzidis | 1bb8a45 | 2009-08-19 01:28:17 +0000 | [diff] [blame] | 3147 | // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser |
| 3148 | // and Sema during declaration parsing. Try deallocating/caching them when |
| 3149 | // it's appropriate, instead of allocating them and keeping them around. |
Douglas Gregor | eb0eb49 | 2010-12-10 08:12:03 +0000 | [diff] [blame] | 3150 | LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType), |
| 3151 | TypeAlignment); |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3152 | new (LocT) LocInfoType(T, TInfo); |
Argyrios Kyrtzidis | 1bb8a45 | 2009-08-19 01:28:17 +0000 | [diff] [blame] | 3153 | assert(LocT->getTypeClass() != T->getTypeClass() && |
| 3154 | "LocInfoType's TypeClass conflicts with an existing Type class"); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3155 | return ParsedType::make(QualType(LocT, 0)); |
Argyrios Kyrtzidis | 1bb8a45 | 2009-08-19 01:28:17 +0000 | [diff] [blame] | 3156 | } |
| 3157 | |
| 3158 | void LocInfoType::getAsStringInternal(std::string &Str, |
| 3159 | const PrintingPolicy &Policy) const { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3160 | llvm_unreachable("LocInfoType leaked into the type system; an opaque TypeTy*" |
Argyrios Kyrtzidis | 35d44e5 | 2009-08-19 01:46:06 +0000 | [diff] [blame] | 3161 | " was used directly instead of getting the QualType through" |
| 3162 | " GetTypeFromParser"); |
Argyrios Kyrtzidis | 1bb8a45 | 2009-08-19 01:28:17 +0000 | [diff] [blame] | 3163 | } |
| 3164 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3165 | TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3166 | // C99 6.7.6: Type names have no identifier. This is already validated by |
| 3167 | // the parser. |
| 3168 | assert(D.getIdentifier() == 0 && "Type name should have no identifier!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3169 | |
Argyrios Kyrtzidis | d3880f8 | 2011-06-28 03:01:18 +0000 | [diff] [blame] | 3170 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 3171 | QualType T = TInfo->getType(); |
Chris Lattner | 5153ee6 | 2009-04-25 08:47:54 +0000 | [diff] [blame] | 3172 | if (D.isInvalidType()) |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 3173 | return true; |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 3174 | |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 3175 | // Make sure there are no unused decl attributes on the declarator. |
John McCall | cdda47f | 2011-10-01 09:56:14 +0000 | [diff] [blame] | 3176 | // We don't want to do this for ObjC parameters because we're going |
| 3177 | // to apply them to the actual parameter declaration. |
| 3178 | if (D.getContext() != Declarator::ObjCParameterContext) |
| 3179 | checkUnusedDeclAttributes(D); |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 3180 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 3181 | if (getLangOptions().CPlusPlus) { |
| 3182 | // Check that there are no default arguments (C++ only). |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 3183 | CheckExtraCXXDefaultArguments(D); |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 3184 | } |
| 3185 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3186 | return CreateParsedType(T, TInfo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3187 | } |
| 3188 | |
Douglas Gregor | e97179c | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 3189 | ParsedType Sema::ActOnObjCInstanceType(SourceLocation Loc) { |
| 3190 | QualType T = Context.getObjCInstanceType(); |
| 3191 | TypeSourceInfo *TInfo = Context.getTrivialTypeSourceInfo(T, Loc); |
| 3192 | return CreateParsedType(T, TInfo); |
| 3193 | } |
| 3194 | |
| 3195 | |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 3196 | //===----------------------------------------------------------------------===// |
| 3197 | // Type Attribute Processing |
| 3198 | //===----------------------------------------------------------------------===// |
| 3199 | |
| 3200 | /// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the |
| 3201 | /// specified type. The attribute contains 1 argument, the id of the address |
| 3202 | /// space for the type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3203 | static void HandleAddressSpaceTypeAttribute(QualType &Type, |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 3204 | const AttributeList &Attr, Sema &S){ |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3205 | |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 3206 | // If this type is already address space qualified, reject it. |
Peter Collingbourne | 29e3ef8 | 2011-07-27 20:29:53 +0000 | [diff] [blame] | 3207 | // ISO/IEC TR 18037 S5.3 (amending C99 6.7.3): "No type shall be qualified by |
| 3208 | // qualifiers for two or more different address spaces." |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 3209 | if (Type.getAddressSpace()) { |
| 3210 | S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3211 | Attr.setInvalid(); |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 3212 | return; |
| 3213 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3214 | |
Peter Collingbourne | 020972d | 2011-07-27 20:30:05 +0000 | [diff] [blame] | 3215 | // ISO/IEC TR 18037 S5.3 (amending C99 6.7.3): "A function type shall not be |
| 3216 | // qualified by an address-space qualifier." |
| 3217 | if (Type->isFunctionType()) { |
| 3218 | S.Diag(Attr.getLoc(), diag::err_attribute_address_function_type); |
| 3219 | Attr.setInvalid(); |
| 3220 | return; |
| 3221 | } |
| 3222 | |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 3223 | // Check the attribute arguments. |
| 3224 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 3225 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3226 | Attr.setInvalid(); |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 3227 | return; |
| 3228 | } |
| 3229 | Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0)); |
| 3230 | llvm::APSInt addrSpace(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 3231 | if (ASArgExpr->isTypeDependent() || ASArgExpr->isValueDependent() || |
| 3232 | !ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) { |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 3233 | S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int) |
| 3234 | << ASArgExpr->getSourceRange(); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3235 | Attr.setInvalid(); |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 3236 | return; |
| 3237 | } |
| 3238 | |
John McCall | efadb77 | 2009-07-28 06:52:18 +0000 | [diff] [blame] | 3239 | // Bounds checking. |
| 3240 | if (addrSpace.isSigned()) { |
| 3241 | if (addrSpace.isNegative()) { |
| 3242 | S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative) |
| 3243 | << ASArgExpr->getSourceRange(); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3244 | Attr.setInvalid(); |
John McCall | efadb77 | 2009-07-28 06:52:18 +0000 | [diff] [blame] | 3245 | return; |
| 3246 | } |
| 3247 | addrSpace.setIsSigned(false); |
| 3248 | } |
| 3249 | llvm::APSInt max(addrSpace.getBitWidth()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3250 | max = Qualifiers::MaxAddressSpace; |
John McCall | efadb77 | 2009-07-28 06:52:18 +0000 | [diff] [blame] | 3251 | if (addrSpace > max) { |
| 3252 | S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3253 | << Qualifiers::MaxAddressSpace << ASArgExpr->getSourceRange(); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3254 | Attr.setInvalid(); |
John McCall | efadb77 | 2009-07-28 06:52:18 +0000 | [diff] [blame] | 3255 | return; |
| 3256 | } |
| 3257 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3258 | unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue()); |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 3259 | Type = S.Context.getAddrSpaceQualType(Type, ASIdx); |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 3260 | } |
| 3261 | |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 3262 | /// handleObjCOwnershipTypeAttr - Process an objc_ownership |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3263 | /// attribute on the specified type. |
| 3264 | /// |
| 3265 | /// Returns 'true' if the attribute was handled. |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 3266 | static bool handleObjCOwnershipTypeAttr(TypeProcessingState &state, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3267 | AttributeList &attr, |
| 3268 | QualType &type) { |
Argyrios Kyrtzidis | e71202e | 2011-11-04 20:37:24 +0000 | [diff] [blame] | 3269 | bool NonObjCPointer = false; |
| 3270 | |
| 3271 | if (!type->isDependentType()) { |
| 3272 | if (const PointerType *ptr = type->getAs<PointerType>()) { |
| 3273 | QualType pointee = ptr->getPointeeType(); |
| 3274 | if (pointee->isObjCRetainableType() || pointee->isPointerType()) |
| 3275 | return false; |
| 3276 | // It is important not to lose the source info that there was an attribute |
| 3277 | // applied to non-objc pointer. We will create an attributed type but |
| 3278 | // its type will be the same as the original type. |
| 3279 | NonObjCPointer = true; |
| 3280 | } else if (!type->isObjCRetainableType()) { |
| 3281 | return false; |
| 3282 | } |
| 3283 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3284 | |
| 3285 | Sema &S = state.getSema(); |
Argyrios Kyrtzidis | 440ec2e | 2011-09-28 18:35:06 +0000 | [diff] [blame] | 3286 | SourceLocation AttrLoc = attr.getLoc(); |
| 3287 | if (AttrLoc.isMacroID()) |
| 3288 | AttrLoc = S.getSourceManager().getImmediateExpansionRange(AttrLoc).first; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3289 | |
| 3290 | if (type.getQualifiers().getObjCLifetime()) { |
Argyrios Kyrtzidis | 440ec2e | 2011-09-28 18:35:06 +0000 | [diff] [blame] | 3291 | S.Diag(AttrLoc, diag::err_attr_objc_ownership_redundant) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3292 | << type; |
| 3293 | return true; |
| 3294 | } |
| 3295 | |
| 3296 | if (!attr.getParameterName()) { |
Argyrios Kyrtzidis | 440ec2e | 2011-09-28 18:35:06 +0000 | [diff] [blame] | 3297 | S.Diag(AttrLoc, diag::err_attribute_argument_n_not_string) |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 3298 | << "objc_ownership" << 1; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3299 | attr.setInvalid(); |
| 3300 | return true; |
| 3301 | } |
| 3302 | |
| 3303 | Qualifiers::ObjCLifetime lifetime; |
| 3304 | if (attr.getParameterName()->isStr("none")) |
| 3305 | lifetime = Qualifiers::OCL_ExplicitNone; |
| 3306 | else if (attr.getParameterName()->isStr("strong")) |
| 3307 | lifetime = Qualifiers::OCL_Strong; |
| 3308 | else if (attr.getParameterName()->isStr("weak")) |
| 3309 | lifetime = Qualifiers::OCL_Weak; |
| 3310 | else if (attr.getParameterName()->isStr("autoreleasing")) |
| 3311 | lifetime = Qualifiers::OCL_Autoreleasing; |
| 3312 | else { |
Argyrios Kyrtzidis | 440ec2e | 2011-09-28 18:35:06 +0000 | [diff] [blame] | 3313 | S.Diag(AttrLoc, diag::warn_attribute_type_not_supported) |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 3314 | << "objc_ownership" << attr.getParameterName(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3315 | attr.setInvalid(); |
| 3316 | return true; |
| 3317 | } |
| 3318 | |
| 3319 | // Consume lifetime attributes without further comment outside of |
| 3320 | // ARC mode. |
| 3321 | if (!S.getLangOptions().ObjCAutoRefCount) |
| 3322 | return true; |
| 3323 | |
Argyrios Kyrtzidis | e71202e | 2011-11-04 20:37:24 +0000 | [diff] [blame] | 3324 | if (NonObjCPointer) { |
| 3325 | StringRef name = attr.getName()->getName(); |
| 3326 | switch (lifetime) { |
| 3327 | case Qualifiers::OCL_None: |
| 3328 | case Qualifiers::OCL_ExplicitNone: |
| 3329 | break; |
| 3330 | case Qualifiers::OCL_Strong: name = "__strong"; break; |
| 3331 | case Qualifiers::OCL_Weak: name = "__weak"; break; |
| 3332 | case Qualifiers::OCL_Autoreleasing: name = "__autoreleasing"; break; |
| 3333 | } |
| 3334 | S.Diag(AttrLoc, diag::warn_objc_object_attribute_wrong_type) |
| 3335 | << name << type; |
| 3336 | } |
| 3337 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3338 | Qualifiers qs; |
| 3339 | qs.setObjCLifetime(lifetime); |
| 3340 | QualType origType = type; |
Argyrios Kyrtzidis | e71202e | 2011-11-04 20:37:24 +0000 | [diff] [blame] | 3341 | if (!NonObjCPointer) |
| 3342 | type = S.Context.getQualifiedType(type, qs); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3343 | |
| 3344 | // If we have a valid source location for the attribute, use an |
| 3345 | // AttributedType instead. |
Argyrios Kyrtzidis | 440ec2e | 2011-09-28 18:35:06 +0000 | [diff] [blame] | 3346 | if (AttrLoc.isValid()) |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 3347 | type = S.Context.getAttributedType(AttributedType::attr_objc_ownership, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3348 | origType, type); |
| 3349 | |
John McCall | 9f084a3 | 2011-07-06 00:26:06 +0000 | [diff] [blame] | 3350 | // Forbid __weak if the runtime doesn't support it. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3351 | if (lifetime == Qualifiers::OCL_Weak && |
Argyrios Kyrtzidis | 5cad822 | 2011-11-07 18:40:21 +0000 | [diff] [blame] | 3352 | !S.getLangOptions().ObjCRuntimeHasWeak && !NonObjCPointer) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3353 | |
| 3354 | // Actually, delay this until we know what we're parsing. |
| 3355 | if (S.DelayedDiagnostics.shouldDelayDiagnostics()) { |
| 3356 | S.DelayedDiagnostics.add( |
Argyrios Kyrtzidis | 440ec2e | 2011-09-28 18:35:06 +0000 | [diff] [blame] | 3357 | sema::DelayedDiagnostic::makeForbiddenType( |
| 3358 | S.getSourceManager().getExpansionLoc(AttrLoc), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3359 | diag::err_arc_weak_no_runtime, type, /*ignored*/ 0)); |
| 3360 | } else { |
Argyrios Kyrtzidis | 440ec2e | 2011-09-28 18:35:06 +0000 | [diff] [blame] | 3361 | S.Diag(AttrLoc, diag::err_arc_weak_no_runtime); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3362 | } |
| 3363 | |
| 3364 | attr.setInvalid(); |
| 3365 | return true; |
| 3366 | } |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 3367 | |
| 3368 | // Forbid __weak for class objects marked as |
| 3369 | // objc_arc_weak_reference_unavailable |
| 3370 | if (lifetime == Qualifiers::OCL_Weak) { |
| 3371 | QualType T = type; |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 3372 | while (const PointerType *ptr = T->getAs<PointerType>()) |
| 3373 | T = ptr->getPointeeType(); |
| 3374 | if (const ObjCObjectPointerType *ObjT = T->getAs<ObjCObjectPointerType>()) { |
| 3375 | ObjCInterfaceDecl *Class = ObjT->getInterfaceDecl(); |
Fariborz Jahanian | 7263fee | 2011-07-06 20:48:48 +0000 | [diff] [blame] | 3376 | if (Class->isArcWeakrefUnavailable()) { |
Argyrios Kyrtzidis | 440ec2e | 2011-09-28 18:35:06 +0000 | [diff] [blame] | 3377 | S.Diag(AttrLoc, diag::err_arc_unsupported_weak_class); |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 3378 | S.Diag(ObjT->getInterfaceDecl()->getLocation(), |
| 3379 | diag::note_class_declared); |
Fariborz Jahanian | 742352a | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 3380 | } |
| 3381 | } |
| 3382 | } |
| 3383 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3384 | return true; |
| 3385 | } |
| 3386 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3387 | /// handleObjCGCTypeAttr - Process the __attribute__((objc_gc)) type |
| 3388 | /// attribute on the specified type. Returns true to indicate that |
| 3389 | /// the attribute was handled, false to indicate that the type does |
| 3390 | /// not permit the attribute. |
| 3391 | static bool handleObjCGCTypeAttr(TypeProcessingState &state, |
| 3392 | AttributeList &attr, |
| 3393 | QualType &type) { |
| 3394 | Sema &S = state.getSema(); |
| 3395 | |
| 3396 | // Delay if this isn't some kind of pointer. |
| 3397 | if (!type->isPointerType() && |
| 3398 | !type->isObjCObjectPointerType() && |
| 3399 | !type->isBlockPointerType()) |
| 3400 | return false; |
| 3401 | |
| 3402 | if (type.getObjCGCAttr() != Qualifiers::GCNone) { |
| 3403 | S.Diag(attr.getLoc(), diag::err_attribute_multiple_objc_gc); |
| 3404 | attr.setInvalid(); |
| 3405 | return true; |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 3406 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3407 | |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 3408 | // Check the attribute arguments. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3409 | if (!attr.getParameterName()) { |
| 3410 | S.Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Fariborz Jahanian | ba372b8 | 2009-02-18 17:52:36 +0000 | [diff] [blame] | 3411 | << "objc_gc" << 1; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3412 | attr.setInvalid(); |
| 3413 | return true; |
Fariborz Jahanian | ba372b8 | 2009-02-18 17:52:36 +0000 | [diff] [blame] | 3414 | } |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3415 | Qualifiers::GC GCAttr; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3416 | if (attr.getNumArgs() != 0) { |
| 3417 | S.Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 3418 | attr.setInvalid(); |
| 3419 | return true; |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 3420 | } |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3421 | if (attr.getParameterName()->isStr("weak")) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3422 | GCAttr = Qualifiers::Weak; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3423 | else if (attr.getParameterName()->isStr("strong")) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3424 | GCAttr = Qualifiers::Strong; |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 3425 | else { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3426 | S.Diag(attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 3427 | << "objc_gc" << attr.getParameterName(); |
| 3428 | attr.setInvalid(); |
| 3429 | return true; |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 3430 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3431 | |
John McCall | 14aa217 | 2011-03-04 04:00:19 +0000 | [diff] [blame] | 3432 | QualType origType = type; |
| 3433 | type = S.Context.getObjCGCQualType(origType, GCAttr); |
| 3434 | |
| 3435 | // Make an attributed type to preserve the source information. |
| 3436 | if (attr.getLoc().isValid()) |
| 3437 | type = S.Context.getAttributedType(AttributedType::attr_objc_gc, |
| 3438 | origType, type); |
| 3439 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3440 | return true; |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 3441 | } |
| 3442 | |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 3443 | namespace { |
| 3444 | /// A helper class to unwrap a type down to a function for the |
| 3445 | /// purposes of applying attributes there. |
| 3446 | /// |
| 3447 | /// Use: |
| 3448 | /// FunctionTypeUnwrapper unwrapped(SemaRef, T); |
| 3449 | /// if (unwrapped.isFunctionType()) { |
| 3450 | /// const FunctionType *fn = unwrapped.get(); |
| 3451 | /// // change fn somehow |
| 3452 | /// T = unwrapped.wrap(fn); |
| 3453 | /// } |
| 3454 | struct FunctionTypeUnwrapper { |
| 3455 | enum WrapKind { |
| 3456 | Desugar, |
| 3457 | Parens, |
| 3458 | Pointer, |
| 3459 | BlockPointer, |
| 3460 | Reference, |
| 3461 | MemberPointer |
| 3462 | }; |
| 3463 | |
| 3464 | QualType Original; |
| 3465 | const FunctionType *Fn; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3466 | SmallVector<unsigned char /*WrapKind*/, 8> Stack; |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 3467 | |
| 3468 | FunctionTypeUnwrapper(Sema &S, QualType T) : Original(T) { |
| 3469 | while (true) { |
| 3470 | const Type *Ty = T.getTypePtr(); |
| 3471 | if (isa<FunctionType>(Ty)) { |
| 3472 | Fn = cast<FunctionType>(Ty); |
| 3473 | return; |
| 3474 | } else if (isa<ParenType>(Ty)) { |
| 3475 | T = cast<ParenType>(Ty)->getInnerType(); |
| 3476 | Stack.push_back(Parens); |
| 3477 | } else if (isa<PointerType>(Ty)) { |
| 3478 | T = cast<PointerType>(Ty)->getPointeeType(); |
| 3479 | Stack.push_back(Pointer); |
| 3480 | } else if (isa<BlockPointerType>(Ty)) { |
| 3481 | T = cast<BlockPointerType>(Ty)->getPointeeType(); |
| 3482 | Stack.push_back(BlockPointer); |
| 3483 | } else if (isa<MemberPointerType>(Ty)) { |
| 3484 | T = cast<MemberPointerType>(Ty)->getPointeeType(); |
| 3485 | Stack.push_back(MemberPointer); |
| 3486 | } else if (isa<ReferenceType>(Ty)) { |
| 3487 | T = cast<ReferenceType>(Ty)->getPointeeType(); |
| 3488 | Stack.push_back(Reference); |
| 3489 | } else { |
| 3490 | const Type *DTy = Ty->getUnqualifiedDesugaredType(); |
| 3491 | if (Ty == DTy) { |
| 3492 | Fn = 0; |
| 3493 | return; |
| 3494 | } |
| 3495 | |
| 3496 | T = QualType(DTy, 0); |
| 3497 | Stack.push_back(Desugar); |
| 3498 | } |
| 3499 | } |
| 3500 | } |
| 3501 | |
| 3502 | bool isFunctionType() const { return (Fn != 0); } |
| 3503 | const FunctionType *get() const { return Fn; } |
| 3504 | |
| 3505 | QualType wrap(Sema &S, const FunctionType *New) { |
| 3506 | // If T wasn't modified from the unwrapped type, do nothing. |
| 3507 | if (New == get()) return Original; |
| 3508 | |
| 3509 | Fn = New; |
| 3510 | return wrap(S.Context, Original, 0); |
| 3511 | } |
| 3512 | |
| 3513 | private: |
| 3514 | QualType wrap(ASTContext &C, QualType Old, unsigned I) { |
| 3515 | if (I == Stack.size()) |
| 3516 | return C.getQualifiedType(Fn, Old.getQualifiers()); |
| 3517 | |
| 3518 | // Build up the inner type, applying the qualifiers from the old |
| 3519 | // type to the new type. |
| 3520 | SplitQualType SplitOld = Old.split(); |
| 3521 | |
| 3522 | // As a special case, tail-recurse if there are no qualifiers. |
| 3523 | if (SplitOld.second.empty()) |
| 3524 | return wrap(C, SplitOld.first, I); |
| 3525 | return C.getQualifiedType(wrap(C, SplitOld.first, I), SplitOld.second); |
| 3526 | } |
| 3527 | |
| 3528 | QualType wrap(ASTContext &C, const Type *Old, unsigned I) { |
| 3529 | if (I == Stack.size()) return QualType(Fn, 0); |
| 3530 | |
| 3531 | switch (static_cast<WrapKind>(Stack[I++])) { |
| 3532 | case Desugar: |
| 3533 | // This is the point at which we potentially lose source |
| 3534 | // information. |
| 3535 | return wrap(C, Old->getUnqualifiedDesugaredType(), I); |
| 3536 | |
| 3537 | case Parens: { |
| 3538 | QualType New = wrap(C, cast<ParenType>(Old)->getInnerType(), I); |
| 3539 | return C.getParenType(New); |
| 3540 | } |
| 3541 | |
| 3542 | case Pointer: { |
| 3543 | QualType New = wrap(C, cast<PointerType>(Old)->getPointeeType(), I); |
| 3544 | return C.getPointerType(New); |
| 3545 | } |
| 3546 | |
| 3547 | case BlockPointer: { |
| 3548 | QualType New = wrap(C, cast<BlockPointerType>(Old)->getPointeeType(),I); |
| 3549 | return C.getBlockPointerType(New); |
| 3550 | } |
| 3551 | |
| 3552 | case MemberPointer: { |
| 3553 | const MemberPointerType *OldMPT = cast<MemberPointerType>(Old); |
| 3554 | QualType New = wrap(C, OldMPT->getPointeeType(), I); |
| 3555 | return C.getMemberPointerType(New, OldMPT->getClass()); |
| 3556 | } |
| 3557 | |
| 3558 | case Reference: { |
| 3559 | const ReferenceType *OldRef = cast<ReferenceType>(Old); |
| 3560 | QualType New = wrap(C, OldRef->getPointeeType(), I); |
| 3561 | if (isa<LValueReferenceType>(OldRef)) |
| 3562 | return C.getLValueReferenceType(New, OldRef->isSpelledAsLValue()); |
| 3563 | else |
| 3564 | return C.getRValueReferenceType(New); |
| 3565 | } |
| 3566 | } |
| 3567 | |
| 3568 | llvm_unreachable("unknown wrapping kind"); |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 3569 | } |
| 3570 | }; |
| 3571 | } |
| 3572 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3573 | /// Process an individual function attribute. Returns true to |
| 3574 | /// indicate that the attribute was handled, false if it wasn't. |
| 3575 | static bool handleFunctionTypeAttr(TypeProcessingState &state, |
| 3576 | AttributeList &attr, |
| 3577 | QualType &type) { |
| 3578 | Sema &S = state.getSema(); |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 3579 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3580 | FunctionTypeUnwrapper unwrapped(S, type); |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 3581 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3582 | if (attr.getKind() == AttributeList::AT_noreturn) { |
| 3583 | if (S.CheckNoReturnAttr(attr)) |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 3584 | return true; |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 3585 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3586 | // Delay if this is not a function type. |
| 3587 | if (!unwrapped.isFunctionType()) |
| 3588 | return false; |
| 3589 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 3590 | // Otherwise we can process right away. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3591 | FunctionType::ExtInfo EI = unwrapped.get()->getExtInfo().withNoReturn(true); |
| 3592 | type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI)); |
| 3593 | return true; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 3594 | } |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 3595 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3596 | // ns_returns_retained is not always a type attribute, but if we got |
| 3597 | // here, we're treating it as one right now. |
| 3598 | if (attr.getKind() == AttributeList::AT_ns_returns_retained) { |
| 3599 | assert(S.getLangOptions().ObjCAutoRefCount && |
| 3600 | "ns_returns_retained treated as type attribute in non-ARC"); |
| 3601 | if (attr.getNumArgs()) return true; |
| 3602 | |
| 3603 | // Delay if this is not a function type. |
| 3604 | if (!unwrapped.isFunctionType()) |
| 3605 | return false; |
| 3606 | |
| 3607 | FunctionType::ExtInfo EI |
| 3608 | = unwrapped.get()->getExtInfo().withProducesResult(true); |
| 3609 | type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI)); |
| 3610 | return true; |
| 3611 | } |
| 3612 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3613 | if (attr.getKind() == AttributeList::AT_regparm) { |
| 3614 | unsigned value; |
| 3615 | if (S.CheckRegparmAttr(attr, value)) |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 3616 | return true; |
| 3617 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3618 | // Delay if this is not a function type. |
| 3619 | if (!unwrapped.isFunctionType()) |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 3620 | return false; |
| 3621 | |
Argyrios Kyrtzidis | ce95566 | 2011-01-25 23:16:40 +0000 | [diff] [blame] | 3622 | // Diagnose regparm with fastcall. |
| 3623 | const FunctionType *fn = unwrapped.get(); |
| 3624 | CallingConv CC = fn->getCallConv(); |
| 3625 | if (CC == CC_X86FastCall) { |
| 3626 | S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 3627 | << FunctionType::getNameForCallConv(CC) |
| 3628 | << "regparm"; |
| 3629 | attr.setInvalid(); |
| 3630 | return true; |
| 3631 | } |
| 3632 | |
John McCall | e6a365d | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 3633 | FunctionType::ExtInfo EI = |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3634 | unwrapped.get()->getExtInfo().withRegParm(value); |
| 3635 | type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI)); |
| 3636 | return true; |
Rafael Espindola | 425ef72 | 2010-03-30 22:15:11 +0000 | [diff] [blame] | 3637 | } |
| 3638 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 3639 | // Otherwise, a calling convention. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3640 | CallingConv CC; |
| 3641 | if (S.CheckCallingConvAttr(attr, CC)) |
| 3642 | return true; |
John McCall | f82b4e8 | 2010-02-04 05:44:44 +0000 | [diff] [blame] | 3643 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 3644 | // Delay if the type didn't work out to a function. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3645 | if (!unwrapped.isFunctionType()) return false; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 3646 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3647 | const FunctionType *fn = unwrapped.get(); |
| 3648 | CallingConv CCOld = fn->getCallConv(); |
Charles Davis | 064f7db | 2010-02-23 06:13:55 +0000 | [diff] [blame] | 3649 | if (S.Context.getCanonicalCallConv(CC) == |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3650 | S.Context.getCanonicalCallConv(CCOld)) { |
Argyrios Kyrtzidis | ce95566 | 2011-01-25 23:16:40 +0000 | [diff] [blame] | 3651 | FunctionType::ExtInfo EI= unwrapped.get()->getExtInfo().withCallingConv(CC); |
| 3652 | type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI)); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3653 | return true; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3654 | } |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 3655 | |
Roman Divacky | 8e68f1c | 2011-08-05 16:37:22 +0000 | [diff] [blame] | 3656 | if (CCOld != (S.LangOpts.MRTD ? CC_X86StdCall : CC_Default)) { |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 3657 | // Should we diagnose reapplications of the same convention? |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3658 | S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible) |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 3659 | << FunctionType::getNameForCallConv(CC) |
| 3660 | << FunctionType::getNameForCallConv(CCOld); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3661 | attr.setInvalid(); |
| 3662 | return true; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 3663 | } |
| 3664 | |
| 3665 | // Diagnose the use of X86 fastcall on varargs or unprototyped functions. |
| 3666 | if (CC == CC_X86FastCall) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3667 | if (isa<FunctionNoProtoType>(fn)) { |
| 3668 | S.Diag(attr.getLoc(), diag::err_cconv_knr) |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 3669 | << FunctionType::getNameForCallConv(CC); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3670 | attr.setInvalid(); |
| 3671 | return true; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 3672 | } |
| 3673 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3674 | const FunctionProtoType *FnP = cast<FunctionProtoType>(fn); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 3675 | if (FnP->isVariadic()) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3676 | S.Diag(attr.getLoc(), diag::err_cconv_varargs) |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 3677 | << FunctionType::getNameForCallConv(CC); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3678 | attr.setInvalid(); |
| 3679 | return true; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 3680 | } |
Argyrios Kyrtzidis | ce95566 | 2011-01-25 23:16:40 +0000 | [diff] [blame] | 3681 | |
| 3682 | // Also diagnose fastcall with regparm. |
Eli Friedman | a49218e | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 3683 | if (fn->getHasRegParm()) { |
Argyrios Kyrtzidis | ce95566 | 2011-01-25 23:16:40 +0000 | [diff] [blame] | 3684 | S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 3685 | << "regparm" |
| 3686 | << FunctionType::getNameForCallConv(CC); |
| 3687 | attr.setInvalid(); |
| 3688 | return true; |
| 3689 | } |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 3690 | } |
| 3691 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3692 | FunctionType::ExtInfo EI = unwrapped.get()->getExtInfo().withCallingConv(CC); |
| 3693 | type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI)); |
| 3694 | return true; |
John McCall | f82b4e8 | 2010-02-04 05:44:44 +0000 | [diff] [blame] | 3695 | } |
| 3696 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3697 | /// Handle OpenCL image access qualifiers: read_only, write_only, read_write |
| 3698 | static void HandleOpenCLImageAccessAttribute(QualType& CurType, |
| 3699 | const AttributeList &Attr, |
| 3700 | Sema &S) { |
| 3701 | // Check the attribute arguments. |
| 3702 | if (Attr.getNumArgs() != 1) { |
| 3703 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 3704 | Attr.setInvalid(); |
| 3705 | return; |
| 3706 | } |
| 3707 | Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0)); |
| 3708 | llvm::APSInt arg(32); |
| 3709 | if (sizeExpr->isTypeDependent() || sizeExpr->isValueDependent() || |
| 3710 | !sizeExpr->isIntegerConstantExpr(arg, S.Context)) { |
| 3711 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 3712 | << "opencl_image_access" << sizeExpr->getSourceRange(); |
| 3713 | Attr.setInvalid(); |
| 3714 | return; |
| 3715 | } |
| 3716 | unsigned iarg = static_cast<unsigned>(arg.getZExtValue()); |
| 3717 | switch (iarg) { |
| 3718 | case CLIA_read_only: |
| 3719 | case CLIA_write_only: |
| 3720 | case CLIA_read_write: |
| 3721 | // Implemented in a separate patch |
| 3722 | break; |
| 3723 | default: |
| 3724 | // Implemented in a separate patch |
| 3725 | S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size) |
| 3726 | << sizeExpr->getSourceRange(); |
| 3727 | Attr.setInvalid(); |
| 3728 | break; |
| 3729 | } |
| 3730 | } |
| 3731 | |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 3732 | /// HandleVectorSizeAttribute - this attribute is only applicable to integral |
| 3733 | /// and float scalars, although arrays, pointers, and function return values are |
| 3734 | /// allowed in conjunction with this construct. Aggregates with this attribute |
| 3735 | /// are invalid, even if they are of the same size as a corresponding scalar. |
| 3736 | /// The raw attribute should contain precisely 1 argument, the vector size for |
| 3737 | /// the variable, measured in bytes. If curType and rawAttr are well formed, |
| 3738 | /// this routine will return a new vector type. |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 3739 | static void HandleVectorSizeAttr(QualType& CurType, const AttributeList &Attr, |
| 3740 | Sema &S) { |
Bob Wilson | 56affbc | 2010-11-16 00:32:16 +0000 | [diff] [blame] | 3741 | // Check the attribute arguments. |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 3742 | if (Attr.getNumArgs() != 1) { |
| 3743 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3744 | Attr.setInvalid(); |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 3745 | return; |
| 3746 | } |
| 3747 | Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0)); |
| 3748 | llvm::APSInt vecSize(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 3749 | if (sizeExpr->isTypeDependent() || sizeExpr->isValueDependent() || |
| 3750 | !sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) { |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 3751 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 3752 | << "vector_size" << sizeExpr->getSourceRange(); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3753 | Attr.setInvalid(); |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 3754 | return; |
| 3755 | } |
| 3756 | // 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] | 3757 | if (!CurType->isIntegerType() && !CurType->isRealFloatingType()) { |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 3758 | S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3759 | Attr.setInvalid(); |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 3760 | return; |
| 3761 | } |
| 3762 | unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType)); |
| 3763 | // vecSize is specified in bytes - convert to bits. |
| 3764 | unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8); |
| 3765 | |
| 3766 | // the vector size needs to be an integral multiple of the type size. |
| 3767 | if (vectorSize % typeSize) { |
| 3768 | S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size) |
| 3769 | << sizeExpr->getSourceRange(); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3770 | Attr.setInvalid(); |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 3771 | return; |
| 3772 | } |
| 3773 | if (vectorSize == 0) { |
| 3774 | S.Diag(Attr.getLoc(), diag::err_attribute_zero_size) |
| 3775 | << sizeExpr->getSourceRange(); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3776 | Attr.setInvalid(); |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 3777 | return; |
| 3778 | } |
| 3779 | |
| 3780 | // Success! Instantiate the vector type, the number of elements is > 0, and |
| 3781 | // not required to be a power of 2, unlike GCC. |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 3782 | CurType = S.Context.getVectorType(CurType, vectorSize/typeSize, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 3783 | VectorType::GenericVector); |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 3784 | } |
| 3785 | |
Douglas Gregor | 4ac0140 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 3786 | /// \brief Process the OpenCL-like ext_vector_type attribute when it occurs on |
| 3787 | /// a type. |
| 3788 | static void HandleExtVectorTypeAttr(QualType &CurType, |
| 3789 | const AttributeList &Attr, |
| 3790 | Sema &S) { |
| 3791 | Expr *sizeExpr; |
| 3792 | |
| 3793 | // Special case where the argument is a template id. |
| 3794 | if (Attr.getParameterName()) { |
| 3795 | CXXScopeSpec SS; |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3796 | SourceLocation TemplateKWLoc; |
Douglas Gregor | 4ac0140 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 3797 | UnqualifiedId id; |
| 3798 | id.setIdentifier(Attr.getParameterName(), Attr.getLoc()); |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3799 | |
| 3800 | ExprResult Size = S.ActOnIdExpression(S.getCurScope(), SS, TemplateKWLoc, |
| 3801 | id, false, false); |
Douglas Gregor | 4ac0140 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 3802 | if (Size.isInvalid()) |
| 3803 | return; |
| 3804 | |
| 3805 | sizeExpr = Size.get(); |
| 3806 | } else { |
| 3807 | // check the attribute arguments. |
| 3808 | if (Attr.getNumArgs() != 1) { |
| 3809 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 3810 | return; |
| 3811 | } |
| 3812 | sizeExpr = Attr.getArg(0); |
| 3813 | } |
| 3814 | |
| 3815 | // Create the vector type. |
| 3816 | QualType T = S.BuildExtVectorType(CurType, sizeExpr, Attr.getLoc()); |
| 3817 | if (!T.isNull()) |
| 3818 | CurType = T; |
| 3819 | } |
| 3820 | |
Bob Wilson | 4211bb6 | 2010-11-16 00:32:24 +0000 | [diff] [blame] | 3821 | /// HandleNeonVectorTypeAttr - The "neon_vector_type" and |
| 3822 | /// "neon_polyvector_type" attributes are used to create vector types that |
| 3823 | /// are mangled according to ARM's ABI. Otherwise, these types are identical |
| 3824 | /// to those created with the "vector_size" attribute. Unlike "vector_size" |
| 3825 | /// the argument to these Neon attributes is the number of vector elements, |
| 3826 | /// not the vector size in bytes. The vector width and element type must |
| 3827 | /// match one of the standard Neon vector types. |
| 3828 | static void HandleNeonVectorTypeAttr(QualType& CurType, |
| 3829 | const AttributeList &Attr, Sema &S, |
| 3830 | VectorType::VectorKind VecKind, |
| 3831 | const char *AttrName) { |
| 3832 | // Check the attribute arguments. |
| 3833 | if (Attr.getNumArgs() != 1) { |
| 3834 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 3835 | Attr.setInvalid(); |
| 3836 | return; |
| 3837 | } |
| 3838 | // The number of elements must be an ICE. |
| 3839 | Expr *numEltsExpr = static_cast<Expr *>(Attr.getArg(0)); |
| 3840 | llvm::APSInt numEltsInt(32); |
| 3841 | if (numEltsExpr->isTypeDependent() || numEltsExpr->isValueDependent() || |
| 3842 | !numEltsExpr->isIntegerConstantExpr(numEltsInt, S.Context)) { |
| 3843 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 3844 | << AttrName << numEltsExpr->getSourceRange(); |
| 3845 | Attr.setInvalid(); |
| 3846 | return; |
| 3847 | } |
| 3848 | // Only certain element types are supported for Neon vectors. |
| 3849 | const BuiltinType* BTy = CurType->getAs<BuiltinType>(); |
| 3850 | if (!BTy || |
| 3851 | (VecKind == VectorType::NeonPolyVector && |
| 3852 | BTy->getKind() != BuiltinType::SChar && |
| 3853 | BTy->getKind() != BuiltinType::Short) || |
| 3854 | (BTy->getKind() != BuiltinType::SChar && |
| 3855 | BTy->getKind() != BuiltinType::UChar && |
| 3856 | BTy->getKind() != BuiltinType::Short && |
| 3857 | BTy->getKind() != BuiltinType::UShort && |
| 3858 | BTy->getKind() != BuiltinType::Int && |
| 3859 | BTy->getKind() != BuiltinType::UInt && |
| 3860 | BTy->getKind() != BuiltinType::LongLong && |
| 3861 | BTy->getKind() != BuiltinType::ULongLong && |
| 3862 | BTy->getKind() != BuiltinType::Float)) { |
| 3863 | S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) <<CurType; |
| 3864 | Attr.setInvalid(); |
| 3865 | return; |
| 3866 | } |
| 3867 | // The total size of the vector must be 64 or 128 bits. |
| 3868 | unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType)); |
| 3869 | unsigned numElts = static_cast<unsigned>(numEltsInt.getZExtValue()); |
| 3870 | unsigned vecSize = typeSize * numElts; |
| 3871 | if (vecSize != 64 && vecSize != 128) { |
| 3872 | S.Diag(Attr.getLoc(), diag::err_attribute_bad_neon_vector_size) << CurType; |
| 3873 | Attr.setInvalid(); |
| 3874 | return; |
| 3875 | } |
| 3876 | |
| 3877 | CurType = S.Context.getVectorType(CurType, numElts, VecKind); |
| 3878 | } |
| 3879 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3880 | static void processTypeAttrs(TypeProcessingState &state, QualType &type, |
| 3881 | bool isDeclSpec, AttributeList *attrs) { |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 3882 | // Scan through and apply attributes to this type where it makes sense. Some |
| 3883 | // attributes (such as __address_space__, __vector_size__, etc) apply to the |
| 3884 | // 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] | 3885 | // 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] | 3886 | |
| 3887 | AttributeList *next; |
| 3888 | do { |
| 3889 | AttributeList &attr = *attrs; |
| 3890 | next = attr.getNext(); |
| 3891 | |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3892 | // Skip attributes that were marked to be invalid. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3893 | if (attr.isInvalid()) |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3894 | continue; |
| 3895 | |
Abramo Bagnara | b1f1b26 | 2010-04-30 09:13:03 +0000 | [diff] [blame] | 3896 | // If this is an attribute we can handle, do so now, |
| 3897 | // otherwise, add it to the FnAttrs list for rechaining. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3898 | switch (attr.getKind()) { |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 3899 | default: break; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 3900 | |
Chandler Carruth | 682eae2 | 2011-10-07 18:40:27 +0000 | [diff] [blame] | 3901 | case AttributeList::AT_may_alias: |
| 3902 | // FIXME: This attribute needs to actually be handled, but if we ignore |
| 3903 | // it it breaks large amounts of Linux software. |
| 3904 | attr.setUsedAsTypeAttr(); |
| 3905 | break; |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 3906 | case AttributeList::AT_address_space: |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3907 | HandleAddressSpaceTypeAttribute(type, attr, state.getSema()); |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 3908 | attr.setUsedAsTypeAttr(); |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 3909 | break; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3910 | OBJC_POINTER_TYPE_ATTRS_CASELIST: |
| 3911 | if (!handleObjCPointerTypeAttr(state, attr, type)) |
| 3912 | distributeObjCPointerTypeAttr(state, attr, type); |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 3913 | attr.setUsedAsTypeAttr(); |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 3914 | break; |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 3915 | case AttributeList::AT_vector_size: |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3916 | HandleVectorSizeAttr(type, attr, state.getSema()); |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 3917 | attr.setUsedAsTypeAttr(); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 3918 | break; |
Douglas Gregor | 4ac0140 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 3919 | case AttributeList::AT_ext_vector_type: |
| 3920 | if (state.getDeclarator().getDeclSpec().getStorageClassSpec() |
| 3921 | != DeclSpec::SCS_typedef) |
| 3922 | HandleExtVectorTypeAttr(type, attr, state.getSema()); |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 3923 | attr.setUsedAsTypeAttr(); |
Douglas Gregor | 4ac0140 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 3924 | break; |
Bob Wilson | 4211bb6 | 2010-11-16 00:32:24 +0000 | [diff] [blame] | 3925 | case AttributeList::AT_neon_vector_type: |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3926 | HandleNeonVectorTypeAttr(type, attr, state.getSema(), |
| 3927 | VectorType::NeonVector, "neon_vector_type"); |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 3928 | attr.setUsedAsTypeAttr(); |
Bob Wilson | 4211bb6 | 2010-11-16 00:32:24 +0000 | [diff] [blame] | 3929 | break; |
| 3930 | case AttributeList::AT_neon_polyvector_type: |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3931 | HandleNeonVectorTypeAttr(type, attr, state.getSema(), |
| 3932 | VectorType::NeonPolyVector, |
Bob Wilson | 4211bb6 | 2010-11-16 00:32:24 +0000 | [diff] [blame] | 3933 | "neon_polyvector_type"); |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 3934 | attr.setUsedAsTypeAttr(); |
Bob Wilson | 4211bb6 | 2010-11-16 00:32:24 +0000 | [diff] [blame] | 3935 | break; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3936 | case AttributeList::AT_opencl_image_access: |
| 3937 | HandleOpenCLImageAccessAttribute(type, attr, state.getSema()); |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 3938 | attr.setUsedAsTypeAttr(); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3939 | break; |
| 3940 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3941 | case AttributeList::AT_ns_returns_retained: |
| 3942 | if (!state.getSema().getLangOptions().ObjCAutoRefCount) |
| 3943 | break; |
| 3944 | // fallthrough into the function attrs |
| 3945 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3946 | FUNCTION_TYPE_ATTRS_CASELIST: |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 3947 | attr.setUsedAsTypeAttr(); |
| 3948 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3949 | // Never process function type attributes as part of the |
| 3950 | // declaration-specifiers. |
| 3951 | if (isDeclSpec) |
| 3952 | distributeFunctionTypeAttrFromDeclSpec(state, attr, type); |
| 3953 | |
| 3954 | // Otherwise, handle the possible delays. |
| 3955 | else if (!handleFunctionTypeAttr(state, attr, type)) |
| 3956 | distributeFunctionTypeAttr(state, attr, type); |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 3957 | break; |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 3958 | } |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3959 | } while ((attrs = next)); |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 3960 | } |
| 3961 | |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 3962 | /// \brief Ensure that the type of the given expression is complete. |
| 3963 | /// |
| 3964 | /// This routine checks whether the expression \p E has a complete type. If the |
| 3965 | /// expression refers to an instantiable construct, that instantiation is |
| 3966 | /// performed as needed to complete its type. Furthermore |
| 3967 | /// Sema::RequireCompleteType is called for the expression's type (or in the |
| 3968 | /// case of a reference type, the referred-to type). |
| 3969 | /// |
| 3970 | /// \param E The expression whose type is required to be complete. |
| 3971 | /// \param PD The partial diagnostic that will be printed out if the type cannot |
| 3972 | /// be completed. |
| 3973 | /// |
| 3974 | /// \returns \c true if the type of \p E is incomplete and diagnosed, \c false |
| 3975 | /// otherwise. |
| 3976 | bool Sema::RequireCompleteExprType(Expr *E, const PartialDiagnostic &PD, |
| 3977 | std::pair<SourceLocation, |
| 3978 | PartialDiagnostic> Note) { |
| 3979 | QualType T = E->getType(); |
| 3980 | |
| 3981 | // Fast path the case where the type is already complete. |
| 3982 | if (!T->isIncompleteType()) |
| 3983 | return false; |
| 3984 | |
| 3985 | // Incomplete array types may be completed by the initializer attached to |
| 3986 | // their definitions. For static data members of class templates we need to |
| 3987 | // instantiate the definition to get this initializer and complete the type. |
| 3988 | if (T->isIncompleteArrayType()) { |
| 3989 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParens())) { |
| 3990 | if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) { |
| 3991 | if (Var->isStaticDataMember() && |
| 3992 | Var->getInstantiatedFromStaticDataMember()) { |
Douglas Gregor | 36f255c | 2011-06-03 14:28:43 +0000 | [diff] [blame] | 3993 | |
| 3994 | MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo(); |
| 3995 | assert(MSInfo && "Missing member specialization information?"); |
| 3996 | if (MSInfo->getTemplateSpecializationKind() |
| 3997 | != TSK_ExplicitSpecialization) { |
| 3998 | // If we don't already have a point of instantiation, this is it. |
| 3999 | if (MSInfo->getPointOfInstantiation().isInvalid()) { |
| 4000 | MSInfo->setPointOfInstantiation(E->getLocStart()); |
| 4001 | |
| 4002 | // This is a modification of an existing AST node. Notify |
| 4003 | // listeners. |
| 4004 | if (ASTMutationListener *L = getASTMutationListener()) |
| 4005 | L->StaticDataMemberInstantiated(Var); |
| 4006 | } |
| 4007 | |
| 4008 | InstantiateStaticDataMemberDefinition(E->getExprLoc(), Var); |
| 4009 | |
| 4010 | // Update the type to the newly instantiated definition's type both |
| 4011 | // here and within the expression. |
| 4012 | if (VarDecl *Def = Var->getDefinition()) { |
| 4013 | DRE->setDecl(Def); |
| 4014 | T = Def->getType(); |
| 4015 | DRE->setType(T); |
| 4016 | E->setType(T); |
| 4017 | } |
Douglas Gregor | f15748a | 2011-06-03 03:35:07 +0000 | [diff] [blame] | 4018 | } |
| 4019 | |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 4020 | // We still go on to try to complete the type independently, as it |
| 4021 | // may also require instantiations or diagnostics if it remains |
| 4022 | // incomplete. |
| 4023 | } |
| 4024 | } |
| 4025 | } |
| 4026 | } |
| 4027 | |
| 4028 | // FIXME: Are there other cases which require instantiating something other |
| 4029 | // than the type to complete the type of an expression? |
| 4030 | |
| 4031 | // Look through reference types and complete the referred type. |
| 4032 | if (const ReferenceType *Ref = T->getAs<ReferenceType>()) |
| 4033 | T = Ref->getPointeeType(); |
| 4034 | |
| 4035 | return RequireCompleteType(E->getExprLoc(), T, PD, Note); |
| 4036 | } |
| 4037 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4038 | /// @brief Ensure that the type T is a complete type. |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4039 | /// |
| 4040 | /// This routine checks whether the type @p T is complete in any |
| 4041 | /// 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] | 4042 | /// type, returns false. If @p T is a class template specialization, |
| 4043 | /// this routine then attempts to perform class template |
| 4044 | /// instantiation. If instantiation fails, or if @p T is incomplete |
| 4045 | /// and cannot be completed, issues the diagnostic @p diag (giving it |
| 4046 | /// the type @p T) and returns true. |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4047 | /// |
| 4048 | /// @param Loc The location in the source that the incomplete type |
| 4049 | /// diagnostic should refer to. |
| 4050 | /// |
| 4051 | /// @param T The type that this routine is examining for completeness. |
| 4052 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4053 | /// @param PD The partial diagnostic that will be printed out if T is not a |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 4054 | /// complete type. |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4055 | /// |
| 4056 | /// @returns @c true if @p T is incomplete and a diagnostic was emitted, |
| 4057 | /// @c false otherwise. |
Anders Carlsson | 91a0cc9 | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 4058 | bool Sema::RequireCompleteType(SourceLocation Loc, QualType T, |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 4059 | const PartialDiagnostic &PD, |
| 4060 | std::pair<SourceLocation, |
| 4061 | PartialDiagnostic> Note) { |
Anders Carlsson | 91a0cc9 | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 4062 | unsigned diag = PD.getDiagID(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4063 | |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 4064 | // FIXME: Add this assertion to make sure we always get instantiation points. |
| 4065 | // assert(!Loc.isInvalid() && "Invalid location in RequireCompleteType"); |
Douglas Gregor | 690dc7f | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 4066 | // FIXME: Add this assertion to help us flush out problems with |
| 4067 | // checking for dependent types and type-dependent expressions. |
| 4068 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4069 | // assert(!T->isDependentType() && |
Douglas Gregor | 690dc7f | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 4070 | // "Can't ask whether a dependent type is complete"); |
| 4071 | |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4072 | // If we have a complete type, we're done. |
Douglas Gregor | d07cc36 | 2012-01-02 17:18:37 +0000 | [diff] [blame] | 4073 | NamedDecl *Def = 0; |
| 4074 | if (!T->isIncompleteType(&Def)) { |
| 4075 | // If we know about the definition but it is not visible, complain. |
| 4076 | if (diag != 0 && Def && !LookupResult::isVisible(Def)) { |
| 4077 | // Suppress this error outside of a SFINAE context if we've already |
| 4078 | // emitted the error once for this type. There's no usefulness in |
| 4079 | // repeating the diagnostic. |
| 4080 | // FIXME: Add a Fix-It that imports the corresponding module or includes |
| 4081 | // the header. |
| 4082 | if (isSFINAEContext() || HiddenDefinitions.insert(Def)) { |
| 4083 | Diag(Loc, diag::err_module_private_definition) << T; |
| 4084 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 4085 | } |
| 4086 | } |
| 4087 | |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4088 | return false; |
Douglas Gregor | d07cc36 | 2012-01-02 17:18:37 +0000 | [diff] [blame] | 4089 | } |
Eli Friedman | 3c0eb16 | 2008-05-27 03:33:27 +0000 | [diff] [blame] | 4090 | |
Sean Callanan | bd79119 | 2011-12-16 00:20:31 +0000 | [diff] [blame] | 4091 | const TagType *Tag = T->getAs<TagType>(); |
| 4092 | const ObjCInterfaceType *IFace = 0; |
| 4093 | |
| 4094 | if (Tag) { |
| 4095 | // Avoid diagnosing invalid decls as incomplete. |
| 4096 | if (Tag->getDecl()->isInvalidDecl()) |
| 4097 | return true; |
| 4098 | |
| 4099 | // Give the external AST source a chance to complete the type. |
| 4100 | if (Tag->getDecl()->hasExternalLexicalStorage()) { |
| 4101 | Context.getExternalSource()->CompleteType(Tag->getDecl()); |
| 4102 | if (!Tag->isIncompleteType()) |
| 4103 | return false; |
| 4104 | } |
| 4105 | } |
| 4106 | else if ((IFace = T->getAs<ObjCInterfaceType>())) { |
| 4107 | // Avoid diagnosing invalid decls as incomplete. |
| 4108 | if (IFace->getDecl()->isInvalidDecl()) |
| 4109 | return true; |
| 4110 | |
| 4111 | // Give the external AST source a chance to complete the type. |
| 4112 | if (IFace->getDecl()->hasExternalLexicalStorage()) { |
| 4113 | Context.getExternalSource()->CompleteType(IFace->getDecl()); |
| 4114 | if (!IFace->isIncompleteType()) |
| 4115 | return false; |
| 4116 | } |
| 4117 | } |
| 4118 | |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 4119 | // 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] | 4120 | // class template specialization, or an array with known size of such, |
| 4121 | // try to instantiate it. |
| 4122 | QualType MaybeTemplate = T; |
Douglas Gregor | 89c49f0 | 2009-11-09 22:08:55 +0000 | [diff] [blame] | 4123 | if (const ConstantArrayType *Array = Context.getAsConstantArrayType(T)) |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 4124 | MaybeTemplate = Array->getElementType(); |
| 4125 | if (const RecordType *Record = MaybeTemplate->getAs<RecordType>()) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4126 | if (ClassTemplateSpecializationDecl *ClassTemplateSpec |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 4127 | = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) { |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 4128 | if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared) |
| 4129 | return InstantiateClassTemplateSpecialization(Loc, ClassTemplateSpec, |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 4130 | TSK_ImplicitInstantiation, |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4131 | /*Complain=*/diag != 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4132 | } else if (CXXRecordDecl *Rec |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 4133 | = dyn_cast<CXXRecordDecl>(Record->getDecl())) { |
| 4134 | if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4135 | MemberSpecializationInfo *MSInfo = Rec->getMemberSpecializationInfo(); |
| 4136 | assert(MSInfo && "Missing member specialization information?"); |
Douglas Gregor | 357bbd0 | 2009-08-28 20:50:45 +0000 | [diff] [blame] | 4137 | // This record was instantiated from a class within a template. |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 4138 | if (MSInfo->getTemplateSpecializationKind() |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 4139 | != TSK_ExplicitSpecialization) |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 4140 | return InstantiateClass(Loc, Rec, Pattern, |
| 4141 | getTemplateInstantiationArgs(Rec), |
| 4142 | TSK_ImplicitInstantiation, |
| 4143 | /*Complain=*/diag != 0); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 4144 | } |
| 4145 | } |
| 4146 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 4147 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4148 | if (diag == 0) |
| 4149 | return true; |
Douglas Gregor | b302996 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 4150 | |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4151 | // We have an incomplete type. Produce a diagnostic. |
Anders Carlsson | 91a0cc9 | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 4152 | Diag(Loc, PD) << T; |
Douglas Gregor | b302996 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 4153 | |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 4154 | // If we have a note, produce it. |
| 4155 | if (!Note.first.isInvalid()) |
| 4156 | Diag(Note.first, Note.second); |
| 4157 | |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4158 | // If the type was a forward declaration of a class/struct/union |
Rafael Espindola | 0162070 | 2010-03-21 22:56:43 +0000 | [diff] [blame] | 4159 | // type, produce a note. |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4160 | if (Tag && !Tag->getDecl()->isInvalidDecl()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4161 | Diag(Tag->getDecl()->getLocation(), |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4162 | Tag->isBeingDefined() ? diag::note_type_being_defined |
| 4163 | : diag::note_forward_declaration) |
Douglas Gregor | b302996 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 4164 | << QualType(Tag, 0); |
| 4165 | |
| 4166 | // If the Objective-C class was a forward declaration, produce a note. |
| 4167 | if (IFace && !IFace->getDecl()->isInvalidDecl()) |
| 4168 | Diag(IFace->getDecl()->getLocation(), diag::note_forward_class); |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4169 | |
| 4170 | return true; |
| 4171 | } |
Douglas Gregor | e625893 | 2009-03-19 00:39:20 +0000 | [diff] [blame] | 4172 | |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 4173 | bool Sema::RequireCompleteType(SourceLocation Loc, QualType T, |
| 4174 | const PartialDiagnostic &PD) { |
| 4175 | return RequireCompleteType(Loc, T, PD, |
| 4176 | std::make_pair(SourceLocation(), PDiag(0))); |
| 4177 | } |
| 4178 | |
| 4179 | bool Sema::RequireCompleteType(SourceLocation Loc, QualType T, |
| 4180 | unsigned DiagID) { |
| 4181 | return RequireCompleteType(Loc, T, PDiag(DiagID), |
| 4182 | std::make_pair(SourceLocation(), PDiag(0))); |
| 4183 | } |
| 4184 | |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 4185 | /// @brief Ensure that the type T is a literal type. |
| 4186 | /// |
| 4187 | /// This routine checks whether the type @p T is a literal type. If @p T is an |
| 4188 | /// incomplete type, an attempt is made to complete it. If @p T is a literal |
| 4189 | /// type, or @p AllowIncompleteType is true and @p T is an incomplete type, |
| 4190 | /// returns false. Otherwise, this routine issues the diagnostic @p PD (giving |
| 4191 | /// it the type @p T), along with notes explaining why the type is not a |
| 4192 | /// literal type, and returns true. |
| 4193 | /// |
| 4194 | /// @param Loc The location in the source that the non-literal type |
| 4195 | /// diagnostic should refer to. |
| 4196 | /// |
| 4197 | /// @param T The type that this routine is examining for literalness. |
| 4198 | /// |
| 4199 | /// @param PD The partial diagnostic that will be printed out if T is not a |
| 4200 | /// literal type. |
| 4201 | /// |
| 4202 | /// @param AllowIncompleteType If true, an incomplete type will be considered |
| 4203 | /// acceptable. |
| 4204 | /// |
| 4205 | /// @returns @c true if @p T is not a literal type and a diagnostic was emitted, |
| 4206 | /// @c false otherwise. |
| 4207 | bool Sema::RequireLiteralType(SourceLocation Loc, QualType T, |
| 4208 | const PartialDiagnostic &PD, |
| 4209 | bool AllowIncompleteType) { |
| 4210 | assert(!T->isDependentType() && "type should not be dependent"); |
| 4211 | |
| 4212 | bool Incomplete = RequireCompleteType(Loc, T, 0); |
Richard Smith | 8398cbf | 2012-02-01 04:40:02 +0000 | [diff] [blame] | 4213 | if (T->isLiteralType() || |
| 4214 | (AllowIncompleteType && Incomplete && !T->isVoidType())) |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 4215 | return false; |
| 4216 | |
| 4217 | if (PD.getDiagID() == 0) |
| 4218 | return true; |
| 4219 | |
| 4220 | Diag(Loc, PD) << T; |
| 4221 | |
| 4222 | if (T->isVariableArrayType()) |
| 4223 | return true; |
| 4224 | |
| 4225 | const RecordType *RT = T->getBaseElementTypeUnsafe()->getAs<RecordType>(); |
| 4226 | if (!RT) |
| 4227 | return true; |
| 4228 | |
| 4229 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 4230 | |
| 4231 | // If the class has virtual base classes, then it's not an aggregate, and |
| 4232 | // cannot have any constexpr constructors, so is non-literal. This is better |
| 4233 | // to diagnose than the resulting absence of constexpr constructors. |
| 4234 | if (RD->getNumVBases()) { |
| 4235 | Diag(RD->getLocation(), diag::note_non_literal_virtual_base) |
| 4236 | << RD->isStruct() << RD->getNumVBases(); |
| 4237 | for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(), |
| 4238 | E = RD->vbases_end(); I != E; ++I) |
| 4239 | Diag(I->getSourceRange().getBegin(), |
| 4240 | diag::note_constexpr_virtual_base_here) << I->getSourceRange(); |
| 4241 | } else if (!RD->isAggregate() && !RD->hasConstexprNonCopyMoveConstructor()) { |
| 4242 | Diag(RD->getLocation(), diag::note_non_literal_no_constexpr_ctors) << RD; |
| 4243 | |
| 4244 | switch (RD->getTemplateSpecializationKind()) { |
| 4245 | case TSK_Undeclared: |
| 4246 | case TSK_ExplicitSpecialization: |
| 4247 | break; |
| 4248 | |
| 4249 | case TSK_ImplicitInstantiation: |
| 4250 | case TSK_ExplicitInstantiationDeclaration: |
| 4251 | case TSK_ExplicitInstantiationDefinition: |
| 4252 | // If the base template had constexpr constructors which were |
| 4253 | // instantiated as non-constexpr constructors, explain why. |
| 4254 | for (CXXRecordDecl::ctor_iterator I = RD->ctor_begin(), |
| 4255 | E = RD->ctor_end(); I != E; ++I) { |
| 4256 | if ((*I)->isCopyConstructor() || (*I)->isMoveConstructor()) |
| 4257 | continue; |
| 4258 | |
| 4259 | FunctionDecl *Base = (*I)->getInstantiatedFromMemberFunction(); |
| 4260 | if (Base && Base->isConstexpr()) |
| 4261 | CheckConstexprFunctionDecl(*I, CCK_NoteNonConstexprInstantiation); |
| 4262 | } |
| 4263 | } |
| 4264 | } else if (RD->hasNonLiteralTypeFieldsOrBases()) { |
| 4265 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 4266 | E = RD->bases_end(); I != E; ++I) { |
| 4267 | if (!I->getType()->isLiteralType()) { |
| 4268 | Diag(I->getSourceRange().getBegin(), |
| 4269 | diag::note_non_literal_base_class) |
| 4270 | << RD << I->getType() << I->getSourceRange(); |
| 4271 | return true; |
| 4272 | } |
| 4273 | } |
| 4274 | for (CXXRecordDecl::field_iterator I = RD->field_begin(), |
| 4275 | E = RD->field_end(); I != E; ++I) { |
| 4276 | if (!(*I)->getType()->isLiteralType()) { |
| 4277 | Diag((*I)->getLocation(), diag::note_non_literal_field) |
| 4278 | << RD << (*I) << (*I)->getType(); |
| 4279 | return true; |
Richard Smith | 5fa6a04 | 2011-10-12 05:08:15 +0000 | [diff] [blame] | 4280 | } else if ((*I)->isMutable()) { |
| 4281 | Diag((*I)->getLocation(), diag::note_non_literal_mutable_field) << RD; |
| 4282 | return true; |
Richard Smith | 9f569cc | 2011-10-01 02:31:28 +0000 | [diff] [blame] | 4283 | } |
| 4284 | } |
| 4285 | } else if (!RD->hasTrivialDestructor()) { |
| 4286 | // All fields and bases are of literal types, so have trivial destructors. |
| 4287 | // If this class's destructor is non-trivial it must be user-declared. |
| 4288 | CXXDestructorDecl *Dtor = RD->getDestructor(); |
| 4289 | assert(Dtor && "class has literal fields and bases but no dtor?"); |
| 4290 | if (!Dtor) |
| 4291 | return true; |
| 4292 | |
| 4293 | Diag(Dtor->getLocation(), Dtor->isUserProvided() ? |
| 4294 | diag::note_non_literal_user_provided_dtor : |
| 4295 | diag::note_non_literal_nontrivial_dtor) << RD; |
| 4296 | } |
| 4297 | |
| 4298 | return true; |
| 4299 | } |
| 4300 | |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4301 | /// \brief Retrieve a version of the type 'T' that is elaborated by Keyword |
| 4302 | /// and qualified by the nested-name-specifier contained in SS. |
| 4303 | QualType Sema::getElaboratedType(ElaboratedTypeKeyword Keyword, |
| 4304 | const CXXScopeSpec &SS, QualType T) { |
| 4305 | if (T.isNull()) |
Douglas Gregor | e625893 | 2009-03-19 00:39:20 +0000 | [diff] [blame] | 4306 | return T; |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4307 | NestedNameSpecifier *NNS; |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 4308 | if (SS.isValid()) |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4309 | NNS = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
| 4310 | else { |
| 4311 | if (Keyword == ETK_None) |
| 4312 | return T; |
| 4313 | NNS = 0; |
| 4314 | } |
| 4315 | return Context.getElaboratedType(Keyword, NNS, T); |
Douglas Gregor | e625893 | 2009-03-19 00:39:20 +0000 | [diff] [blame] | 4316 | } |
Anders Carlsson | af017e6 | 2009-06-29 22:58:55 +0000 | [diff] [blame] | 4317 | |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 4318 | QualType Sema::BuildTypeofExprType(Expr *E, SourceLocation Loc) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 4319 | ExprResult ER = CheckPlaceholderExpr(E); |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 4320 | if (ER.isInvalid()) return QualType(); |
| 4321 | E = ER.take(); |
| 4322 | |
Fariborz Jahanian | 2b1d51b | 2010-10-05 23:24:00 +0000 | [diff] [blame] | 4323 | if (!E->isTypeDependent()) { |
| 4324 | QualType T = E->getType(); |
Fariborz Jahanian | aca7f7b | 2010-10-06 00:23:25 +0000 | [diff] [blame] | 4325 | if (const TagType *TT = T->getAs<TagType>()) |
| 4326 | DiagnoseUseOfDecl(TT->getDecl(), E->getExprLoc()); |
Fariborz Jahanian | 2b1d51b | 2010-10-05 23:24:00 +0000 | [diff] [blame] | 4327 | } |
Anders Carlsson | af017e6 | 2009-06-29 22:58:55 +0000 | [diff] [blame] | 4328 | return Context.getTypeOfExprType(E); |
| 4329 | } |
| 4330 | |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 4331 | QualType Sema::BuildDecltypeType(Expr *E, SourceLocation Loc) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 4332 | ExprResult ER = CheckPlaceholderExpr(E); |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 4333 | if (ER.isInvalid()) return QualType(); |
| 4334 | E = ER.take(); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4335 | |
Anders Carlsson | af017e6 | 2009-06-29 22:58:55 +0000 | [diff] [blame] | 4336 | return Context.getDecltypeType(E); |
| 4337 | } |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 4338 | |
| 4339 | QualType Sema::BuildUnaryTransformType(QualType BaseType, |
| 4340 | UnaryTransformType::UTTKind UKind, |
| 4341 | SourceLocation Loc) { |
| 4342 | switch (UKind) { |
| 4343 | case UnaryTransformType::EnumUnderlyingType: |
| 4344 | if (!BaseType->isDependentType() && !BaseType->isEnumeralType()) { |
| 4345 | Diag(Loc, diag::err_only_enums_have_underlying_types); |
| 4346 | return QualType(); |
| 4347 | } else { |
| 4348 | QualType Underlying = BaseType; |
| 4349 | if (!BaseType->isDependentType()) { |
| 4350 | EnumDecl *ED = BaseType->getAs<EnumType>()->getDecl(); |
| 4351 | assert(ED && "EnumType has no EnumDecl"); |
| 4352 | DiagnoseUseOfDecl(ED, Loc); |
| 4353 | Underlying = ED->getIntegerType(); |
| 4354 | } |
| 4355 | assert(!Underlying.isNull()); |
| 4356 | return Context.getUnaryTransformType(BaseType, Underlying, |
| 4357 | UnaryTransformType::EnumUnderlyingType); |
| 4358 | } |
| 4359 | } |
| 4360 | llvm_unreachable("unknown unary transform type"); |
| 4361 | } |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4362 | |
| 4363 | QualType Sema::BuildAtomicType(QualType T, SourceLocation Loc) { |
| 4364 | if (!T->isDependentType()) { |
| 4365 | int DisallowedKind = -1; |
| 4366 | if (T->isIncompleteType()) |
| 4367 | // FIXME: It isn't entirely clear whether incomplete atomic types |
| 4368 | // are allowed or not; for simplicity, ban them for the moment. |
| 4369 | DisallowedKind = 0; |
| 4370 | else if (T->isArrayType()) |
| 4371 | DisallowedKind = 1; |
| 4372 | else if (T->isFunctionType()) |
| 4373 | DisallowedKind = 2; |
| 4374 | else if (T->isReferenceType()) |
| 4375 | DisallowedKind = 3; |
| 4376 | else if (T->isAtomicType()) |
| 4377 | DisallowedKind = 4; |
| 4378 | else if (T.hasQualifiers()) |
| 4379 | DisallowedKind = 5; |
| 4380 | else if (!T.isTriviallyCopyableType(Context)) |
| 4381 | // Some other non-trivially-copyable type (probably a C++ class) |
| 4382 | DisallowedKind = 6; |
| 4383 | |
| 4384 | if (DisallowedKind != -1) { |
| 4385 | Diag(Loc, diag::err_atomic_specifier_bad_type) << DisallowedKind << T; |
| 4386 | return QualType(); |
| 4387 | } |
| 4388 | |
| 4389 | // FIXME: Do we need any handling for ARC here? |
| 4390 | } |
| 4391 | |
| 4392 | // Build the pointer type. |
| 4393 | return Context.getAtomicType(T); |
| 4394 | } |