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