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