blob: b3cdbb272539e1a526c509b8335f7197177d7d5c [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- SemaType.cpp - Semantic Analysis for Types -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements type-related semantic analysis.
11//
12//===----------------------------------------------------------------------===//
13
John McCall2d887082010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
John McCall7cd088e2010-08-24 07:21:54 +000015#include "clang/Sema/Template.h"
Peter Collingbourne207f4d82011-03-18 22:38:29 +000016#include "clang/Basic/OpenCL.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "clang/AST/ASTContext.h"
Douglas Gregora8f32e02009-10-06 17:59:45 +000018#include "clang/AST/CXXInheritance.h"
Steve Naroff980e5082007-10-01 19:00:59 +000019#include "clang/AST/DeclObjC.h"
Douglas Gregor2943aed2009-03-03 04:44:36 +000020#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +000021#include "clang/AST/TypeLoc.h"
John McCall51bd8032009-10-18 01:05:36 +000022#include "clang/AST/TypeLocVisitor.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000023#include "clang/AST/Expr.h"
Anders Carlsson91a0cc92009-08-26 22:33:56 +000024#include "clang/Basic/PartialDiagnostic.h"
Charles Davisd18f9f92010-08-16 04:01:50 +000025#include "clang/Basic/TargetInfo.h"
John McCall2792fa52011-03-08 04:17:03 +000026#include "clang/Lex/Preprocessor.h"
John McCall19510852010-08-20 18:27:03 +000027#include "clang/Sema/DeclSpec.h"
Sebastian Redl4994d2d2009-07-04 11:39:00 +000028#include "llvm/ADT/SmallPtrSet.h"
Douglas Gregor87c12c42009-11-04 16:49:01 +000029#include "llvm/Support/ErrorHandling.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000030using namespace clang;
31
Douglas Gregor2dc0e642009-03-23 23:06:20 +000032/// \brief Perform adjustment on the parameter type of a function.
33///
34/// This routine adjusts the given parameter type @p T to the actual
Mike Stump1eb44332009-09-09 15:08:12 +000035/// parameter type used by semantic analysis (C99 6.7.5.3p[7,8],
36/// C++ [dcl.fct]p3). The adjusted parameter type is returned.
Douglas Gregor2dc0e642009-03-23 23:06:20 +000037QualType Sema::adjustParameterType(QualType T) {
38 // C99 6.7.5.3p7:
Chris Lattner778ed742009-10-25 17:36:50 +000039 // A declaration of a parameter as "array of type" shall be
40 // adjusted to "qualified pointer to type", where the type
41 // qualifiers (if any) are those specified within the [ and ] of
42 // the array type derivation.
43 if (T->isArrayType())
Douglas Gregor2dc0e642009-03-23 23:06:20 +000044 return Context.getArrayDecayedType(T);
Chris Lattner778ed742009-10-25 17:36:50 +000045
46 // C99 6.7.5.3p8:
47 // A declaration of a parameter as "function returning type"
48 // shall be adjusted to "pointer to function returning type", as
49 // in 6.3.2.1.
50 if (T->isFunctionType())
Douglas Gregor2dc0e642009-03-23 23:06:20 +000051 return Context.getPointerType(T);
52
53 return T;
54}
55
Chris Lattner5db2bb12009-10-25 18:21:37 +000056
57
58/// isOmittedBlockReturnType - Return true if this declarator is missing a
59/// return type because this is a omitted return type on a block literal.
Sebastian Redl8ce35b02009-10-25 21:45:37 +000060static bool isOmittedBlockReturnType(const Declarator &D) {
Chris Lattner5db2bb12009-10-25 18:21:37 +000061 if (D.getContext() != Declarator::BlockLiteralContext ||
Sebastian Redl8ce35b02009-10-25 21:45:37 +000062 D.getDeclSpec().hasTypeSpecifier())
Chris Lattner5db2bb12009-10-25 18:21:37 +000063 return false;
64
65 if (D.getNumTypeObjects() == 0)
Chris Lattnera64ef0a2009-10-25 22:09:09 +000066 return true; // ^{ ... }
Chris Lattner5db2bb12009-10-25 18:21:37 +000067
68 if (D.getNumTypeObjects() == 1 &&
69 D.getTypeObject(0).Kind == DeclaratorChunk::Function)
Chris Lattnera64ef0a2009-10-25 22:09:09 +000070 return true; // ^(int X, float Y) { ... }
Chris Lattner5db2bb12009-10-25 18:21:37 +000071
72 return false;
73}
74
John McCall2792fa52011-03-08 04:17:03 +000075/// diagnoseBadTypeAttribute - Diagnoses a type attribute which
76/// doesn't apply to the given type.
77static void diagnoseBadTypeAttribute(Sema &S, const AttributeList &attr,
78 QualType type) {
79 bool useInstantiationLoc = false;
80
81 unsigned diagID = 0;
82 switch (attr.getKind()) {
83 case AttributeList::AT_objc_gc:
84 diagID = diag::warn_pointer_attribute_wrong_type;
85 useInstantiationLoc = true;
86 break;
87
88 default:
89 // Assume everything else was a function attribute.
90 diagID = diag::warn_function_attribute_wrong_type;
91 break;
92 }
93
94 SourceLocation loc = attr.getLoc();
95 llvm::StringRef name = attr.getName()->getName();
96
97 // The GC attributes are usually written with macros; special-case them.
98 if (useInstantiationLoc && loc.isMacroID() && attr.getParameterName()) {
John McCall834e3f62011-03-08 07:59:04 +000099 if (attr.getParameterName()->isStr("strong")) {
100 if (S.findMacroSpelling(loc, "__strong")) name = "__strong";
101 } else if (attr.getParameterName()->isStr("weak")) {
102 if (S.findMacroSpelling(loc, "__weak")) name = "__weak";
John McCall2792fa52011-03-08 04:17:03 +0000103 }
104 }
105
106 S.Diag(loc, diagID) << name << type;
107}
108
John McCall711c52b2011-01-05 12:14:39 +0000109// objc_gc applies to Objective-C pointers or, otherwise, to the
110// smallest available pointer type (i.e. 'void*' in 'void**').
111#define OBJC_POINTER_TYPE_ATTRS_CASELIST \
112 case AttributeList::AT_objc_gc
John McCall04a67a62010-02-05 21:31:56 +0000113
John McCall711c52b2011-01-05 12:14:39 +0000114// Function type attributes.
115#define FUNCTION_TYPE_ATTRS_CASELIST \
116 case AttributeList::AT_noreturn: \
117 case AttributeList::AT_cdecl: \
118 case AttributeList::AT_fastcall: \
119 case AttributeList::AT_stdcall: \
120 case AttributeList::AT_thiscall: \
121 case AttributeList::AT_pascal: \
122 case AttributeList::AT_regparm
John McCall04a67a62010-02-05 21:31:56 +0000123
John McCall711c52b2011-01-05 12:14:39 +0000124namespace {
125 /// An object which stores processing state for the entire
126 /// GetTypeForDeclarator process.
127 class TypeProcessingState {
128 Sema &sema;
129
130 /// The declarator being processed.
131 Declarator &declarator;
132
133 /// The index of the declarator chunk we're currently processing.
134 /// May be the total number of valid chunks, indicating the
135 /// DeclSpec.
136 unsigned chunkIndex;
137
138 /// Whether there are non-trivial modifications to the decl spec.
139 bool trivial;
140
141 /// The original set of attributes on the DeclSpec.
142 llvm::SmallVector<AttributeList*, 2> savedAttrs;
143
144 /// A list of attributes to diagnose the uselessness of when the
145 /// processing is complete.
146 llvm::SmallVector<AttributeList*, 2> ignoredTypeAttrs;
147
148 public:
149 TypeProcessingState(Sema &sema, Declarator &declarator)
150 : sema(sema), declarator(declarator),
151 chunkIndex(declarator.getNumTypeObjects()),
152 trivial(true) {}
153
154 Sema &getSema() const {
155 return sema;
Abramo Bagnarae215f722010-04-30 13:10:51 +0000156 }
John McCall711c52b2011-01-05 12:14:39 +0000157
158 Declarator &getDeclarator() const {
159 return declarator;
160 }
161
162 unsigned getCurrentChunkIndex() const {
163 return chunkIndex;
164 }
165
166 void setCurrentChunkIndex(unsigned idx) {
167 assert(idx <= declarator.getNumTypeObjects());
168 chunkIndex = idx;
169 }
170
171 AttributeList *&getCurrentAttrListRef() const {
172 assert(chunkIndex <= declarator.getNumTypeObjects());
173 if (chunkIndex == declarator.getNumTypeObjects())
174 return getMutableDeclSpec().getAttributes().getListRef();
175 return declarator.getTypeObject(chunkIndex).getAttrListRef();
176 }
177
178 /// Save the current set of attributes on the DeclSpec.
179 void saveDeclSpecAttrs() {
180 // Don't try to save them multiple times.
181 if (!savedAttrs.empty()) return;
182
183 DeclSpec &spec = getMutableDeclSpec();
184 for (AttributeList *attr = spec.getAttributes().getList(); attr;
185 attr = attr->getNext())
186 savedAttrs.push_back(attr);
187 trivial &= savedAttrs.empty();
188 }
189
190 /// Record that we had nowhere to put the given type attribute.
191 /// We will diagnose such attributes later.
192 void addIgnoredTypeAttr(AttributeList &attr) {
193 ignoredTypeAttrs.push_back(&attr);
194 }
195
196 /// Diagnose all the ignored type attributes, given that the
197 /// declarator worked out to the given type.
198 void diagnoseIgnoredTypeAttrs(QualType type) const {
199 for (llvm::SmallVectorImpl<AttributeList*>::const_iterator
200 i = ignoredTypeAttrs.begin(), e = ignoredTypeAttrs.end();
John McCall2792fa52011-03-08 04:17:03 +0000201 i != e; ++i)
202 diagnoseBadTypeAttribute(getSema(), **i, type);
John McCall711c52b2011-01-05 12:14:39 +0000203 }
204
205 ~TypeProcessingState() {
206 if (trivial) return;
207
208 restoreDeclSpecAttrs();
209 }
210
211 private:
212 DeclSpec &getMutableDeclSpec() const {
213 return const_cast<DeclSpec&>(declarator.getDeclSpec());
214 }
215
216 void restoreDeclSpecAttrs() {
217 assert(!savedAttrs.empty());
218 getMutableDeclSpec().getAttributes().set(savedAttrs[0]);
219 for (unsigned i = 0, e = savedAttrs.size() - 1; i != e; ++i)
220 savedAttrs[i]->setNext(savedAttrs[i+1]);
221 savedAttrs.back()->setNext(0);
222 }
223 };
224
225 /// Basically std::pair except that we really want to avoid an
226 /// implicit operator= for safety concerns. It's also a minor
227 /// link-time optimization for this to be a private type.
228 struct AttrAndList {
229 /// The attribute.
230 AttributeList &first;
231
232 /// The head of the list the attribute is currently in.
233 AttributeList *&second;
234
235 AttrAndList(AttributeList &attr, AttributeList *&head)
236 : first(attr), second(head) {}
237 };
John McCall04a67a62010-02-05 21:31:56 +0000238}
239
John McCall711c52b2011-01-05 12:14:39 +0000240namespace llvm {
241 template <> struct isPodLike<AttrAndList> {
242 static const bool value = true;
243 };
244}
245
246static void spliceAttrIntoList(AttributeList &attr, AttributeList *&head) {
247 attr.setNext(head);
248 head = &attr;
249}
250
251static void spliceAttrOutOfList(AttributeList &attr, AttributeList *&head) {
252 if (head == &attr) {
253 head = attr.getNext();
254 return;
John McCall04a67a62010-02-05 21:31:56 +0000255 }
John McCall711c52b2011-01-05 12:14:39 +0000256
257 AttributeList *cur = head;
258 while (true) {
259 assert(cur && cur->getNext() && "ran out of attrs?");
260 if (cur->getNext() == &attr) {
261 cur->setNext(attr.getNext());
262 return;
263 }
264 cur = cur->getNext();
265 }
266}
267
268static void moveAttrFromListToList(AttributeList &attr,
269 AttributeList *&fromList,
270 AttributeList *&toList) {
271 spliceAttrOutOfList(attr, fromList);
272 spliceAttrIntoList(attr, toList);
273}
274
275static void processTypeAttrs(TypeProcessingState &state,
276 QualType &type, bool isDeclSpec,
277 AttributeList *attrs);
278
279static bool handleFunctionTypeAttr(TypeProcessingState &state,
280 AttributeList &attr,
281 QualType &type);
282
283static bool handleObjCGCTypeAttr(TypeProcessingState &state,
284 AttributeList &attr, QualType &type);
285
286static bool handleObjCPointerTypeAttr(TypeProcessingState &state,
287 AttributeList &attr, QualType &type) {
288 // Right now, we have exactly one of these attributes: objc_gc.
289 assert(attr.getKind() == AttributeList::AT_objc_gc);
290 return handleObjCGCTypeAttr(state, attr, type);
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.
298static 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 McCall2792fa52011-03-08 04:17:03 +0000323
324 diagnoseBadTypeAttribute(state.getSema(), attr, type);
John McCall711c52b2011-01-05 12:14:39 +0000325}
326
327/// Distribute an objc_gc type attribute that was written on the
328/// declarator.
329static void
330distributeObjCPointerTypeAttrFromDeclarator(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 McCallae278a32011-01-12 00:34:59 +0000345 continue;
John McCall711c52b2011-01-05 12:14:39 +0000346
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) {
363 if (handleObjCPointerTypeAttr(state, attr, declSpecType))
364 return;
365 }
366
367 // Otherwise, if we found an appropriate chunk, splice the attribute
368 // into it.
369 if (innermost != -1U) {
370 moveAttrFromListToList(attr, declarator.getAttrListRef(),
371 declarator.getTypeObject(innermost).getAttrListRef());
372 return;
373 }
374
375 // Otherwise, diagnose when we're done building the type.
376 spliceAttrOutOfList(attr, declarator.getAttrListRef());
377 state.addIgnoredTypeAttr(attr);
378}
379
380/// A function type attribute was written somewhere in a declaration
381/// *other* than on the declarator itself or in the decl spec. Given
382/// that it didn't apply in whatever position it was written in, try
383/// to move it to a more appropriate position.
384static void distributeFunctionTypeAttr(TypeProcessingState &state,
385 AttributeList &attr,
386 QualType type) {
387 Declarator &declarator = state.getDeclarator();
388
389 // Try to push the attribute from the return type of a function to
390 // the function itself.
391 for (unsigned i = state.getCurrentChunkIndex(); i != 0; --i) {
392 DeclaratorChunk &chunk = declarator.getTypeObject(i-1);
393 switch (chunk.Kind) {
394 case DeclaratorChunk::Function:
395 moveAttrFromListToList(attr, state.getCurrentAttrListRef(),
396 chunk.getAttrListRef());
397 return;
398
399 case DeclaratorChunk::Paren:
400 case DeclaratorChunk::Pointer:
401 case DeclaratorChunk::BlockPointer:
402 case DeclaratorChunk::Array:
403 case DeclaratorChunk::Reference:
404 case DeclaratorChunk::MemberPointer:
405 continue;
406 }
407 }
408
John McCall2792fa52011-03-08 04:17:03 +0000409 diagnoseBadTypeAttribute(state.getSema(), attr, type);
John McCall711c52b2011-01-05 12:14:39 +0000410}
411
412/// Try to distribute a function type attribute to the innermost
413/// function chunk or type. Returns true if the attribute was
414/// distributed, false if no location was found.
415static bool
416distributeFunctionTypeAttrToInnermost(TypeProcessingState &state,
417 AttributeList &attr,
418 AttributeList *&attrList,
419 QualType &declSpecType) {
420 Declarator &declarator = state.getDeclarator();
421
422 // Put it on the innermost function chunk, if there is one.
423 for (unsigned i = 0, e = declarator.getNumTypeObjects(); i != e; ++i) {
424 DeclaratorChunk &chunk = declarator.getTypeObject(i);
425 if (chunk.Kind != DeclaratorChunk::Function) continue;
426
427 moveAttrFromListToList(attr, attrList, chunk.getAttrListRef());
428 return true;
429 }
430
431 return handleFunctionTypeAttr(state, attr, declSpecType);
432}
433
434/// A function type attribute was written in the decl spec. Try to
435/// apply it somewhere.
436static void
437distributeFunctionTypeAttrFromDeclSpec(TypeProcessingState &state,
438 AttributeList &attr,
439 QualType &declSpecType) {
440 state.saveDeclSpecAttrs();
441
442 // Try to distribute to the innermost.
443 if (distributeFunctionTypeAttrToInnermost(state, attr,
444 state.getCurrentAttrListRef(),
445 declSpecType))
446 return;
447
448 // If that failed, diagnose the bad attribute when the declarator is
449 // fully built.
450 state.addIgnoredTypeAttr(attr);
451}
452
453/// A function type attribute was written on the declarator. Try to
454/// apply it somewhere.
455static void
456distributeFunctionTypeAttrFromDeclarator(TypeProcessingState &state,
457 AttributeList &attr,
458 QualType &declSpecType) {
459 Declarator &declarator = state.getDeclarator();
460
461 // Try to distribute to the innermost.
462 if (distributeFunctionTypeAttrToInnermost(state, attr,
463 declarator.getAttrListRef(),
464 declSpecType))
465 return;
466
467 // If that failed, diagnose the bad attribute when the declarator is
468 // fully built.
469 spliceAttrOutOfList(attr, declarator.getAttrListRef());
470 state.addIgnoredTypeAttr(attr);
471}
472
473/// \brief Given that there are attributes written on the declarator
474/// itself, try to distribute any type attributes to the appropriate
475/// declarator chunk.
476///
477/// These are attributes like the following:
478/// int f ATTR;
479/// int (f ATTR)();
480/// but not necessarily this:
481/// int f() ATTR;
482static void distributeTypeAttrsFromDeclarator(TypeProcessingState &state,
483 QualType &declSpecType) {
484 // Collect all the type attributes from the declarator itself.
485 assert(state.getDeclarator().getAttributes() && "declarator has no attrs!");
486 AttributeList *attr = state.getDeclarator().getAttributes();
487 AttributeList *next;
488 do {
489 next = attr->getNext();
490
491 switch (attr->getKind()) {
492 OBJC_POINTER_TYPE_ATTRS_CASELIST:
493 distributeObjCPointerTypeAttrFromDeclarator(state, *attr, declSpecType);
494 break;
495
496 FUNCTION_TYPE_ATTRS_CASELIST:
497 distributeFunctionTypeAttrFromDeclarator(state, *attr, declSpecType);
498 break;
499
500 default:
501 break;
502 }
503 } while ((attr = next));
504}
505
506/// Add a synthetic '()' to a block-literal declarator if it is
507/// required, given the return type.
508static void maybeSynthesizeBlockSignature(TypeProcessingState &state,
509 QualType declSpecType) {
510 Declarator &declarator = state.getDeclarator();
511
512 // First, check whether the declarator would produce a function,
513 // i.e. whether the innermost semantic chunk is a function.
514 if (declarator.isFunctionDeclarator()) {
515 // If so, make that declarator a prototyped declarator.
516 declarator.getFunctionTypeInfo().hasPrototype = true;
517 return;
518 }
519
John McCallda263792011-02-08 01:59:10 +0000520 // If there are any type objects, the type as written won't name a
521 // function, regardless of the decl spec type. This is because a
522 // block signature declarator is always an abstract-declarator, and
523 // abstract-declarators can't just be parentheses chunks. Therefore
524 // we need to build a function chunk unless there are no type
525 // objects and the decl spec type is a function.
John McCall711c52b2011-01-05 12:14:39 +0000526 if (!declarator.getNumTypeObjects() && declSpecType->isFunctionType())
527 return;
528
John McCallda263792011-02-08 01:59:10 +0000529 // Note that there *are* cases with invalid declarators where
530 // declarators consist solely of parentheses. In general, these
531 // occur only in failed efforts to make function declarators, so
532 // faking up the function chunk is still the right thing to do.
John McCall711c52b2011-01-05 12:14:39 +0000533
534 // Otherwise, we need to fake up a function declarator.
535 SourceLocation loc = declarator.getSourceRange().getBegin();
536
537 // ...and *prepend* it to the declarator.
538 declarator.AddInnermostTypeInfo(DeclaratorChunk::getFunction(
539 ParsedAttributes(),
540 /*proto*/ true,
541 /*variadic*/ false, SourceLocation(),
542 /*args*/ 0, 0,
543 /*type quals*/ 0,
Douglas Gregor83f51722011-01-26 03:43:54 +0000544 /*ref-qualifier*/true, SourceLocation(),
Sebastian Redl6e5d3192011-03-05 22:42:13 +0000545 /*EH*/ EST_None, SourceLocation(), 0, 0, 0, 0,
John McCall711c52b2011-01-05 12:14:39 +0000546 /*parens*/ loc, loc,
547 declarator));
548
549 // For consistency, make sure the state still has us as processing
550 // the decl spec.
551 assert(state.getCurrentChunkIndex() == declarator.getNumTypeObjects() - 1);
552 state.setCurrentChunkIndex(declarator.getNumTypeObjects());
John McCall04a67a62010-02-05 21:31:56 +0000553}
554
Douglas Gregor930d8b52009-01-30 22:09:00 +0000555/// \brief Convert the specified declspec to the appropriate type
556/// object.
Chris Lattner5db2bb12009-10-25 18:21:37 +0000557/// \param D the declarator containing the declaration specifier.
Chris Lattner5153ee62009-04-25 08:47:54 +0000558/// \returns The type described by the declaration specifiers. This function
559/// never returns null.
John McCall711c52b2011-01-05 12:14:39 +0000560static QualType ConvertDeclSpecToType(Sema &S, TypeProcessingState &state) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000561 // FIXME: Should move the logic from DeclSpec::Finish to here for validity
562 // checking.
John McCall711c52b2011-01-05 12:14:39 +0000563
564 Declarator &declarator = state.getDeclarator();
565 const DeclSpec &DS = declarator.getDeclSpec();
566 SourceLocation DeclLoc = declarator.getIdentifierLoc();
Chris Lattner5db2bb12009-10-25 18:21:37 +0000567 if (DeclLoc.isInvalid())
568 DeclLoc = DS.getSourceRange().getBegin();
Chris Lattner1564e392009-10-25 18:07:27 +0000569
John McCall711c52b2011-01-05 12:14:39 +0000570 ASTContext &Context = S.Context;
Mike Stump1eb44332009-09-09 15:08:12 +0000571
Chris Lattner5db2bb12009-10-25 18:21:37 +0000572 QualType Result;
Reid Spencer5f016e22007-07-11 17:01:13 +0000573 switch (DS.getTypeSpecType()) {
Chris Lattner96b77fc2008-04-02 06:50:17 +0000574 case DeclSpec::TST_void:
575 Result = Context.VoidTy;
576 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000577 case DeclSpec::TST_char:
578 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000579 Result = Context.CharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000580 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000581 Result = Context.SignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000582 else {
583 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
584 "Unknown TSS value");
Chris Lattnerfab5b452008-02-20 23:53:49 +0000585 Result = Context.UnsignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000586 }
Chris Lattner958858e2008-02-20 21:40:32 +0000587 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000588 case DeclSpec::TST_wchar:
589 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
590 Result = Context.WCharTy;
591 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
John McCall711c52b2011-01-05 12:14:39 +0000592 S.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000593 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000594 Result = Context.getSignedWCharType();
595 } else {
596 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
597 "Unknown TSS value");
John McCall711c52b2011-01-05 12:14:39 +0000598 S.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000599 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000600 Result = Context.getUnsignedWCharType();
601 }
602 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000603 case DeclSpec::TST_char16:
604 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
605 "Unknown TSS value");
606 Result = Context.Char16Ty;
607 break;
608 case DeclSpec::TST_char32:
609 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
610 "Unknown TSS value");
611 Result = Context.Char32Ty;
612 break;
Chris Lattnerd658b562008-04-05 06:32:51 +0000613 case DeclSpec::TST_unspecified:
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000614 // "<proto1,proto2>" is an objc qualified ID with a missing id.
Chris Lattner097e9162008-10-20 02:01:50 +0000615 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
John McCallc12c5bb2010-05-15 11:32:37 +0000616 Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy,
617 (ObjCProtocolDecl**)PQ,
618 DS.getNumProtocolQualifiers());
619 Result = Context.getObjCObjectPointerType(Result);
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000620 break;
621 }
Chris Lattner5db2bb12009-10-25 18:21:37 +0000622
623 // If this is a missing declspec in a block literal return context, then it
624 // is inferred from the return statements inside the block.
John McCall711c52b2011-01-05 12:14:39 +0000625 if (isOmittedBlockReturnType(declarator)) {
Chris Lattner5db2bb12009-10-25 18:21:37 +0000626 Result = Context.DependentTy;
627 break;
628 }
Mike Stump1eb44332009-09-09 15:08:12 +0000629
Chris Lattnerd658b562008-04-05 06:32:51 +0000630 // Unspecified typespec defaults to int in C90. However, the C90 grammar
631 // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
632 // type-qualifier, or storage-class-specifier. If not, emit an extwarn.
633 // Note that the one exception to this is function definitions, which are
634 // allowed to be completely missing a declspec. This is handled in the
635 // parser already though by it pretending to have seen an 'int' in this
636 // case.
John McCall711c52b2011-01-05 12:14:39 +0000637 if (S.getLangOptions().ImplicitInt) {
Chris Lattner35d276f2009-02-27 18:53:28 +0000638 // In C89 mode, we only warn if there is a completely missing declspec
639 // when one is not allowed.
Chris Lattner3f84ad22009-04-22 05:27:59 +0000640 if (DS.isEmpty()) {
John McCall711c52b2011-01-05 12:14:39 +0000641 S.Diag(DeclLoc, diag::ext_missing_declspec)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000642 << DS.getSourceRange()
Douglas Gregor849b2432010-03-31 17:46:05 +0000643 << FixItHint::CreateInsertion(DS.getSourceRange().getBegin(), "int");
Chris Lattner3f84ad22009-04-22 05:27:59 +0000644 }
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000645 } else if (!DS.hasTypeSpecifier()) {
Chris Lattnerd658b562008-04-05 06:32:51 +0000646 // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says:
647 // "At least one type specifier shall be given in the declaration
648 // specifiers in each declaration, and in the specifier-qualifier list in
649 // each struct declaration and type name."
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000650 // FIXME: Does Microsoft really have the implicit int extension in C++?
John McCall711c52b2011-01-05 12:14:39 +0000651 if (S.getLangOptions().CPlusPlus &&
652 !S.getLangOptions().Microsoft) {
653 S.Diag(DeclLoc, diag::err_missing_type_specifier)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000654 << DS.getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000655
Chris Lattnerb78d8332009-06-26 04:45:06 +0000656 // When this occurs in C++ code, often something is very broken with the
657 // value being declared, poison it as invalid so we don't get chains of
658 // errors.
John McCall711c52b2011-01-05 12:14:39 +0000659 declarator.setInvalidType(true);
Chris Lattnerb78d8332009-06-26 04:45:06 +0000660 } else {
John McCall711c52b2011-01-05 12:14:39 +0000661 S.Diag(DeclLoc, diag::ext_missing_type_specifier)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000662 << DS.getSourceRange();
Chris Lattnerb78d8332009-06-26 04:45:06 +0000663 }
Chris Lattnerd658b562008-04-05 06:32:51 +0000664 }
Mike Stump1eb44332009-09-09 15:08:12 +0000665
666 // FALL THROUGH.
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000667 case DeclSpec::TST_int: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000668 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
669 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000670 case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
671 case DeclSpec::TSW_short: Result = Context.ShortTy; break;
672 case DeclSpec::TSW_long: Result = Context.LongTy; break;
Chris Lattner311157f2009-10-25 18:25:04 +0000673 case DeclSpec::TSW_longlong:
674 Result = Context.LongLongTy;
675
676 // long long is a C99 feature.
John McCall711c52b2011-01-05 12:14:39 +0000677 if (!S.getLangOptions().C99 &&
678 !S.getLangOptions().CPlusPlus0x)
679 S.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong);
Chris Lattner311157f2009-10-25 18:25:04 +0000680 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000681 }
682 } else {
683 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000684 case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
685 case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break;
686 case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break;
Chris Lattner311157f2009-10-25 18:25:04 +0000687 case DeclSpec::TSW_longlong:
688 Result = Context.UnsignedLongLongTy;
689
690 // long long is a C99 feature.
John McCall711c52b2011-01-05 12:14:39 +0000691 if (!S.getLangOptions().C99 &&
692 !S.getLangOptions().CPlusPlus0x)
693 S.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong);
Chris Lattner311157f2009-10-25 18:25:04 +0000694 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000695 }
696 }
Chris Lattner958858e2008-02-20 21:40:32 +0000697 break;
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000698 }
Chris Lattnerfab5b452008-02-20 23:53:49 +0000699 case DeclSpec::TST_float: Result = Context.FloatTy; break;
Chris Lattner958858e2008-02-20 21:40:32 +0000700 case DeclSpec::TST_double:
701 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000702 Result = Context.LongDoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000703 else
Chris Lattnerfab5b452008-02-20 23:53:49 +0000704 Result = Context.DoubleTy;
Peter Collingbourne39d3e7a2011-02-15 19:46:23 +0000705
706 if (S.getLangOptions().OpenCL && !S.getOpenCLOptions().cl_khr_fp64) {
707 S.Diag(DS.getTypeSpecTypeLoc(), diag::err_double_requires_fp64);
708 declarator.setInvalidType(true);
709 }
Chris Lattner958858e2008-02-20 21:40:32 +0000710 break;
Chris Lattnerfab5b452008-02-20 23:53:49 +0000711 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
Reid Spencer5f016e22007-07-11 17:01:13 +0000712 case DeclSpec::TST_decimal32: // _Decimal32
713 case DeclSpec::TST_decimal64: // _Decimal64
714 case DeclSpec::TST_decimal128: // _Decimal128
John McCall711c52b2011-01-05 12:14:39 +0000715 S.Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported);
Chris Lattner8f12f652009-05-13 05:02:08 +0000716 Result = Context.IntTy;
John McCall711c52b2011-01-05 12:14:39 +0000717 declarator.setInvalidType(true);
Chris Lattner8f12f652009-05-13 05:02:08 +0000718 break;
Chris Lattner99dc9142008-04-13 18:59:07 +0000719 case DeclSpec::TST_class:
Reid Spencer5f016e22007-07-11 17:01:13 +0000720 case DeclSpec::TST_enum:
721 case DeclSpec::TST_union:
722 case DeclSpec::TST_struct: {
John McCallb3d87482010-08-24 05:47:05 +0000723 TypeDecl *D = dyn_cast_or_null<TypeDecl>(DS.getRepAsDecl());
John McCall6e247262009-10-10 05:48:19 +0000724 if (!D) {
725 // This can happen in C++ with ambiguous lookups.
726 Result = Context.IntTy;
John McCall711c52b2011-01-05 12:14:39 +0000727 declarator.setInvalidType(true);
John McCall6e247262009-10-10 05:48:19 +0000728 break;
729 }
730
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000731 // If the type is deprecated or unavailable, diagnose it.
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000732 S.DiagnoseUseOfDecl(D, DS.getTypeSpecTypeNameLoc());
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000733
Reid Spencer5f016e22007-07-11 17:01:13 +0000734 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000735 DS.getTypeSpecSign() == 0 && "No qualifiers on tag names!");
736
Reid Spencer5f016e22007-07-11 17:01:13 +0000737 // TypeQuals handled by caller.
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000738 Result = Context.getTypeDeclType(D);
John McCall2191b202009-09-05 06:31:47 +0000739
Abramo Bagnara0daaf322011-03-16 20:16:18 +0000740 // In both C and C++, make an ElaboratedType.
741 ElaboratedTypeKeyword Keyword
742 = ElaboratedType::getKeywordForTypeSpec(DS.getTypeSpecType());
743 Result = S.getElaboratedType(Keyword, DS.getTypeSpecScope(), Result);
744
Chris Lattner5153ee62009-04-25 08:47:54 +0000745 if (D->isInvalidDecl())
John McCall711c52b2011-01-05 12:14:39 +0000746 declarator.setInvalidType(true);
Chris Lattner958858e2008-02-20 21:40:32 +0000747 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000748 }
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000749 case DeclSpec::TST_typename: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000750 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
751 DS.getTypeSpecSign() == 0 &&
752 "Can't handle qualifiers on typedef names yet!");
John McCall711c52b2011-01-05 12:14:39 +0000753 Result = S.GetTypeFromParser(DS.getRepAsType());
John McCall27940d22010-07-30 05:17:22 +0000754 if (Result.isNull())
John McCall711c52b2011-01-05 12:14:39 +0000755 declarator.setInvalidType(true);
John McCall27940d22010-07-30 05:17:22 +0000756 else if (DeclSpec::ProtocolQualifierListTy PQ
757 = DS.getProtocolQualifiers()) {
John McCallc12c5bb2010-05-15 11:32:37 +0000758 if (const ObjCObjectType *ObjT = Result->getAs<ObjCObjectType>()) {
759 // Silently drop any existing protocol qualifiers.
760 // TODO: determine whether that's the right thing to do.
761 if (ObjT->getNumProtocols())
762 Result = ObjT->getBaseType();
763
764 if (DS.getNumProtocolQualifiers())
765 Result = Context.getObjCObjectType(Result,
766 (ObjCProtocolDecl**) PQ,
767 DS.getNumProtocolQualifiers());
768 } else if (Result->isObjCIdType()) {
Chris Lattnerae4da612008-07-26 01:53:50 +0000769 // id<protocol-list>
John McCallc12c5bb2010-05-15 11:32:37 +0000770 Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy,
771 (ObjCProtocolDecl**) PQ,
772 DS.getNumProtocolQualifiers());
773 Result = Context.getObjCObjectPointerType(Result);
774 } else if (Result->isObjCClassType()) {
Steve Naroff4262a072009-02-23 18:53:24 +0000775 // Class<protocol-list>
John McCallc12c5bb2010-05-15 11:32:37 +0000776 Result = Context.getObjCObjectType(Context.ObjCBuiltinClassTy,
777 (ObjCProtocolDecl**) PQ,
778 DS.getNumProtocolQualifiers());
779 Result = Context.getObjCObjectPointerType(Result);
Chris Lattner3f84ad22009-04-22 05:27:59 +0000780 } else {
John McCall711c52b2011-01-05 12:14:39 +0000781 S.Diag(DeclLoc, diag::err_invalid_protocol_qualifiers)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000782 << DS.getSourceRange();
John McCall711c52b2011-01-05 12:14:39 +0000783 declarator.setInvalidType(true);
Chris Lattner3f84ad22009-04-22 05:27:59 +0000784 }
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000785 }
Mike Stump1eb44332009-09-09 15:08:12 +0000786
Reid Spencer5f016e22007-07-11 17:01:13 +0000787 // TypeQuals handled by caller.
Chris Lattner958858e2008-02-20 21:40:32 +0000788 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000789 }
Chris Lattner958858e2008-02-20 21:40:32 +0000790 case DeclSpec::TST_typeofType:
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000791 // FIXME: Preserve type source info.
John McCall711c52b2011-01-05 12:14:39 +0000792 Result = S.GetTypeFromParser(DS.getRepAsType());
Chris Lattner958858e2008-02-20 21:40:32 +0000793 assert(!Result.isNull() && "Didn't get a type for typeof?");
Fariborz Jahanian730e1752010-10-06 17:00:02 +0000794 if (!Result->isDependentType())
795 if (const TagType *TT = Result->getAs<TagType>())
John McCall711c52b2011-01-05 12:14:39 +0000796 S.DiagnoseUseOfDecl(TT->getDecl(), DS.getTypeSpecTypeLoc());
Steve Naroffd1861fd2007-07-31 12:34:36 +0000797 // TypeQuals handled by caller.
Chris Lattnerfab5b452008-02-20 23:53:49 +0000798 Result = Context.getTypeOfType(Result);
Chris Lattner958858e2008-02-20 21:40:32 +0000799 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000800 case DeclSpec::TST_typeofExpr: {
John McCallb3d87482010-08-24 05:47:05 +0000801 Expr *E = DS.getRepAsExpr();
Steve Naroffd1861fd2007-07-31 12:34:36 +0000802 assert(E && "Didn't get an expression for typeof?");
803 // TypeQuals handled by caller.
John McCall711c52b2011-01-05 12:14:39 +0000804 Result = S.BuildTypeofExprType(E, DS.getTypeSpecTypeLoc());
Douglas Gregor4b52e252009-12-21 23:17:24 +0000805 if (Result.isNull()) {
806 Result = Context.IntTy;
John McCall711c52b2011-01-05 12:14:39 +0000807 declarator.setInvalidType(true);
Douglas Gregor4b52e252009-12-21 23:17:24 +0000808 }
Chris Lattner958858e2008-02-20 21:40:32 +0000809 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000810 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000811 case DeclSpec::TST_decltype: {
John McCallb3d87482010-08-24 05:47:05 +0000812 Expr *E = DS.getRepAsExpr();
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000813 assert(E && "Didn't get an expression for decltype?");
814 // TypeQuals handled by caller.
John McCall711c52b2011-01-05 12:14:39 +0000815 Result = S.BuildDecltypeType(E, DS.getTypeSpecTypeLoc());
Anders Carlssonaf017e62009-06-29 22:58:55 +0000816 if (Result.isNull()) {
817 Result = Context.IntTy;
John McCall711c52b2011-01-05 12:14:39 +0000818 declarator.setInvalidType(true);
Anders Carlssonaf017e62009-06-29 22:58:55 +0000819 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000820 break;
821 }
Anders Carlssone89d1592009-06-26 18:41:36 +0000822 case DeclSpec::TST_auto: {
823 // TypeQuals handled by caller.
Richard Smith34b41d92011-02-20 03:19:35 +0000824 Result = Context.getAutoType(QualType());
Anders Carlssone89d1592009-06-26 18:41:36 +0000825 break;
826 }
Mike Stump1eb44332009-09-09 15:08:12 +0000827
Douglas Gregor809070a2009-02-18 17:45:20 +0000828 case DeclSpec::TST_error:
Chris Lattner5153ee62009-04-25 08:47:54 +0000829 Result = Context.IntTy;
John McCall711c52b2011-01-05 12:14:39 +0000830 declarator.setInvalidType(true);
Chris Lattner5153ee62009-04-25 08:47:54 +0000831 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000832 }
Mike Stump1eb44332009-09-09 15:08:12 +0000833
Chris Lattner958858e2008-02-20 21:40:32 +0000834 // Handle complex types.
Douglas Gregorf244cd72009-02-14 21:06:05 +0000835 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
John McCall711c52b2011-01-05 12:14:39 +0000836 if (S.getLangOptions().Freestanding)
837 S.Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
Chris Lattnerfab5b452008-02-20 23:53:49 +0000838 Result = Context.getComplexType(Result);
John Thompson82287d12010-02-05 00:12:22 +0000839 } else if (DS.isTypeAltiVecVector()) {
840 unsigned typeSize = static_cast<unsigned>(Context.getTypeSize(Result));
841 assert(typeSize > 0 && "type size for vector must be greater than 0 bits");
Bob Wilsone86d78c2010-11-10 21:56:12 +0000842 VectorType::VectorKind VecKind = VectorType::AltiVecVector;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000843 if (DS.isTypeAltiVecPixel())
Bob Wilsone86d78c2010-11-10 21:56:12 +0000844 VecKind = VectorType::AltiVecPixel;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000845 else if (DS.isTypeAltiVecBool())
Bob Wilsone86d78c2010-11-10 21:56:12 +0000846 VecKind = VectorType::AltiVecBool;
847 Result = Context.getVectorType(Result, 128/typeSize, VecKind);
Douglas Gregorf244cd72009-02-14 21:06:05 +0000848 }
Mike Stump1eb44332009-09-09 15:08:12 +0000849
Argyrios Kyrtzidis47423bd2010-09-23 09:40:31 +0000850 // FIXME: Imaginary.
851 if (DS.getTypeSpecComplex() == DeclSpec::TSC_imaginary)
John McCall711c52b2011-01-05 12:14:39 +0000852 S.Diag(DS.getTypeSpecComplexLoc(), diag::err_imaginary_not_supported);
Mike Stump1eb44332009-09-09 15:08:12 +0000853
John McCall711c52b2011-01-05 12:14:39 +0000854 // Before we process any type attributes, synthesize a block literal
855 // function declarator if necessary.
856 if (declarator.getContext() == Declarator::BlockLiteralContext)
857 maybeSynthesizeBlockSignature(state, Result);
858
859 // Apply any type attributes from the decl spec. This may cause the
860 // list of type attributes to be temporarily saved while the type
861 // attributes are pushed around.
862 if (AttributeList *attrs = DS.getAttributes().getList())
863 processTypeAttrs(state, Result, true, attrs);
Mike Stump1eb44332009-09-09 15:08:12 +0000864
Chris Lattner96b77fc2008-04-02 06:50:17 +0000865 // Apply const/volatile/restrict qualifiers to T.
866 if (unsigned TypeQuals = DS.getTypeQualifiers()) {
867
868 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
869 // or incomplete types shall not be restrict-qualified." C++ also allows
870 // restrict-qualified references.
John McCall0953e762009-09-24 19:53:00 +0000871 if (TypeQuals & DeclSpec::TQ_restrict) {
Fariborz Jahanian2b5ff1a2009-12-07 18:08:58 +0000872 if (Result->isAnyPointerType() || Result->isReferenceType()) {
873 QualType EltTy;
874 if (Result->isObjCObjectPointerType())
875 EltTy = Result;
876 else
877 EltTy = Result->isPointerType() ?
878 Result->getAs<PointerType>()->getPointeeType() :
879 Result->getAs<ReferenceType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000880
Douglas Gregorbad0e652009-03-24 20:32:41 +0000881 // If we have a pointer or reference, the pointee must have an object
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000882 // incomplete type.
883 if (!EltTy->isIncompleteOrObjectType()) {
John McCall711c52b2011-01-05 12:14:39 +0000884 S.Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000885 diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattnerd1625842008-11-24 06:25:27 +0000886 << EltTy << DS.getSourceRange();
John McCall0953e762009-09-24 19:53:00 +0000887 TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000888 }
889 } else {
John McCall711c52b2011-01-05 12:14:39 +0000890 S.Diag(DS.getRestrictSpecLoc(),
891 diag::err_typecheck_invalid_restrict_not_pointer)
Chris Lattnerd1625842008-11-24 06:25:27 +0000892 << Result << DS.getSourceRange();
John McCall0953e762009-09-24 19:53:00 +0000893 TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
Chris Lattner96b77fc2008-04-02 06:50:17 +0000894 }
Chris Lattner96b77fc2008-04-02 06:50:17 +0000895 }
Mike Stump1eb44332009-09-09 15:08:12 +0000896
Chris Lattner96b77fc2008-04-02 06:50:17 +0000897 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
898 // of a function type includes any type qualifiers, the behavior is
899 // undefined."
900 if (Result->isFunctionType() && TypeQuals) {
901 // Get some location to point at, either the C or V location.
902 SourceLocation Loc;
John McCall0953e762009-09-24 19:53:00 +0000903 if (TypeQuals & DeclSpec::TQ_const)
Chris Lattner96b77fc2008-04-02 06:50:17 +0000904 Loc = DS.getConstSpecLoc();
John McCall0953e762009-09-24 19:53:00 +0000905 else if (TypeQuals & DeclSpec::TQ_volatile)
Chris Lattner96b77fc2008-04-02 06:50:17 +0000906 Loc = DS.getVolatileSpecLoc();
John McCall0953e762009-09-24 19:53:00 +0000907 else {
908 assert((TypeQuals & DeclSpec::TQ_restrict) &&
909 "Has CVR quals but not C, V, or R?");
910 Loc = DS.getRestrictSpecLoc();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000911 }
John McCall711c52b2011-01-05 12:14:39 +0000912 S.Diag(Loc, diag::warn_typecheck_function_qualifiers)
Chris Lattnerd1625842008-11-24 06:25:27 +0000913 << Result << DS.getSourceRange();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000914 }
Mike Stump1eb44332009-09-09 15:08:12 +0000915
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000916 // C++ [dcl.ref]p1:
917 // Cv-qualified references are ill-formed except when the
918 // cv-qualifiers are introduced through the use of a typedef
919 // (7.1.3) or of a template type argument (14.3), in which
920 // case the cv-qualifiers are ignored.
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000921 // FIXME: Shouldn't we be checking SCS_typedef here?
922 if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000923 TypeQuals && Result->isReferenceType()) {
John McCall0953e762009-09-24 19:53:00 +0000924 TypeQuals &= ~DeclSpec::TQ_const;
925 TypeQuals &= ~DeclSpec::TQ_volatile;
Mike Stump1eb44332009-09-09 15:08:12 +0000926 }
927
John McCall0953e762009-09-24 19:53:00 +0000928 Qualifiers Quals = Qualifiers::fromCVRMask(TypeQuals);
929 Result = Context.getQualifiedType(Result, Quals);
Chris Lattner96b77fc2008-04-02 06:50:17 +0000930 }
John McCall0953e762009-09-24 19:53:00 +0000931
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000932 return Result;
933}
934
Douglas Gregorcd281c32009-02-28 00:25:32 +0000935static std::string getPrintableNameForEntity(DeclarationName Entity) {
936 if (Entity)
937 return Entity.getAsString();
Mike Stump1eb44332009-09-09 15:08:12 +0000938
Douglas Gregorcd281c32009-02-28 00:25:32 +0000939 return "type name";
940}
941
John McCall28654742010-06-05 06:41:15 +0000942QualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc,
943 Qualifiers Qs) {
944 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
945 // object or incomplete types shall not be restrict-qualified."
946 if (Qs.hasRestrict()) {
947 unsigned DiagID = 0;
948 QualType ProblemTy;
949
950 const Type *Ty = T->getCanonicalTypeInternal().getTypePtr();
951 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(Ty)) {
952 if (!RTy->getPointeeType()->isIncompleteOrObjectType()) {
953 DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
954 ProblemTy = T->getAs<ReferenceType>()->getPointeeType();
955 }
956 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
957 if (!PTy->getPointeeType()->isIncompleteOrObjectType()) {
958 DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
959 ProblemTy = T->getAs<PointerType>()->getPointeeType();
960 }
961 } else if (const MemberPointerType *PTy = dyn_cast<MemberPointerType>(Ty)) {
962 if (!PTy->getPointeeType()->isIncompleteOrObjectType()) {
963 DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
964 ProblemTy = T->getAs<PointerType>()->getPointeeType();
965 }
966 } else if (!Ty->isDependentType()) {
967 // FIXME: this deserves a proper diagnostic
968 DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
969 ProblemTy = T;
970 }
971
972 if (DiagID) {
973 Diag(Loc, DiagID) << ProblemTy;
974 Qs.removeRestrict();
975 }
976 }
977
978 return Context.getQualifiedType(T, Qs);
979}
980
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000981/// \brief Build a paren type including \p T.
982QualType Sema::BuildParenType(QualType T) {
983 return Context.getParenType(T);
984}
985
Douglas Gregorcd281c32009-02-28 00:25:32 +0000986/// \brief Build a pointer type.
987///
988/// \param T The type to which we'll be building a pointer.
989///
Douglas Gregorcd281c32009-02-28 00:25:32 +0000990/// \param Loc The location of the entity whose type involves this
991/// pointer type or, if there is no such entity, the location of the
992/// type that will have pointer type.
993///
994/// \param Entity The name of the entity that involves the pointer
995/// type, if known.
996///
997/// \returns A suitable pointer type, if there are no
998/// errors. Otherwise, returns a NULL type.
John McCall28654742010-06-05 06:41:15 +0000999QualType Sema::BuildPointerType(QualType T,
Douglas Gregorcd281c32009-02-28 00:25:32 +00001000 SourceLocation Loc, DeclarationName Entity) {
1001 if (T->isReferenceType()) {
1002 // C++ 8.3.2p4: There shall be no ... pointers to references ...
1003 Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
John McCallac406052009-10-30 00:37:20 +00001004 << getPrintableNameForEntity(Entity) << T;
Douglas Gregorcd281c32009-02-28 00:25:32 +00001005 return QualType();
1006 }
1007
John McCallc12c5bb2010-05-15 11:32:37 +00001008 assert(!T->isObjCObjectType() && "Should build ObjCObjectPointerType");
Douglas Gregor92e986e2010-04-22 16:44:27 +00001009
Douglas Gregorcd281c32009-02-28 00:25:32 +00001010 // Build the pointer type.
John McCall28654742010-06-05 06:41:15 +00001011 return Context.getPointerType(T);
Douglas Gregorcd281c32009-02-28 00:25:32 +00001012}
1013
1014/// \brief Build a reference type.
1015///
1016/// \param T The type to which we'll be building a reference.
1017///
Douglas Gregorcd281c32009-02-28 00:25:32 +00001018/// \param Loc The location of the entity whose type involves this
1019/// reference type or, if there is no such entity, the location of the
1020/// type that will have reference type.
1021///
1022/// \param Entity The name of the entity that involves the reference
1023/// type, if known.
1024///
1025/// \returns A suitable reference type, if there are no
1026/// errors. Otherwise, returns a NULL type.
John McCall54e14c42009-10-22 22:37:11 +00001027QualType Sema::BuildReferenceType(QualType T, bool SpelledAsLValue,
John McCall28654742010-06-05 06:41:15 +00001028 SourceLocation Loc,
John McCall54e14c42009-10-22 22:37:11 +00001029 DeclarationName Entity) {
Douglas Gregor69d83162011-01-20 16:08:06 +00001030 // C++0x [dcl.ref]p6:
1031 // If a typedef (7.1.3), a type template-parameter (14.3.1), or a
1032 // decltype-specifier (7.1.6.2) denotes a type TR that is a reference to a
1033 // type T, an attempt to create the type "lvalue reference to cv TR" creates
1034 // the type "lvalue reference to T", while an attempt to create the type
1035 // "rvalue reference to cv TR" creates the type TR.
John McCall54e14c42009-10-22 22:37:11 +00001036 bool LValueRef = SpelledAsLValue || T->getAs<LValueReferenceType>();
1037
John McCall54e14c42009-10-22 22:37:11 +00001038 // C++ [dcl.ref]p4: There shall be no references to references.
1039 //
1040 // According to C++ DR 106, references to references are only
1041 // diagnosed when they are written directly (e.g., "int & &"),
1042 // but not when they happen via a typedef:
1043 //
1044 // typedef int& intref;
1045 // typedef intref& intref2;
1046 //
1047 // Parser::ParseDeclaratorInternal diagnoses the case where
1048 // references are written directly; here, we handle the
Douglas Gregor69d83162011-01-20 16:08:06 +00001049 // collapsing of references-to-references as described in C++0x.
1050 // DR 106 and 540 introduce reference-collapsing into C++98/03.
Douglas Gregorcd281c32009-02-28 00:25:32 +00001051
1052 // C++ [dcl.ref]p1:
Eli Friedman33a31382009-08-05 19:21:58 +00001053 // A declarator that specifies the type "reference to cv void"
Douglas Gregorcd281c32009-02-28 00:25:32 +00001054 // is ill-formed.
1055 if (T->isVoidType()) {
1056 Diag(Loc, diag::err_reference_to_void);
1057 return QualType();
1058 }
1059
Douglas Gregorcd281c32009-02-28 00:25:32 +00001060 // Handle restrict on references.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001061 if (LValueRef)
John McCall28654742010-06-05 06:41:15 +00001062 return Context.getLValueReferenceType(T, SpelledAsLValue);
1063 return Context.getRValueReferenceType(T);
Douglas Gregorcd281c32009-02-28 00:25:32 +00001064}
1065
1066/// \brief Build an array type.
1067///
1068/// \param T The type of each element in the array.
1069///
1070/// \param ASM C99 array size modifier (e.g., '*', 'static').
Mike Stump1eb44332009-09-09 15:08:12 +00001071///
1072/// \param ArraySize Expression describing the size of the array.
Douglas Gregorcd281c32009-02-28 00:25:32 +00001073///
Douglas Gregorcd281c32009-02-28 00:25:32 +00001074/// \param Loc The location of the entity whose type involves this
1075/// array type or, if there is no such entity, the location of the
1076/// type that will have array type.
1077///
1078/// \param Entity The name of the entity that involves the array
1079/// type, if known.
1080///
1081/// \returns A suitable array type, if there are no errors. Otherwise,
1082/// returns a NULL type.
1083QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
1084 Expr *ArraySize, unsigned Quals,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001085 SourceRange Brackets, DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +00001086
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001087 SourceLocation Loc = Brackets.getBegin();
Sebastian Redl923d56d2009-11-05 15:52:31 +00001088 if (getLangOptions().CPlusPlus) {
Douglas Gregor138bb232010-04-27 19:38:14 +00001089 // C++ [dcl.array]p1:
1090 // T is called the array element type; this type shall not be a reference
1091 // type, the (possibly cv-qualified) type void, a function type or an
1092 // abstract class type.
1093 //
1094 // Note: function types are handled in the common path with C.
1095 if (T->isReferenceType()) {
1096 Diag(Loc, diag::err_illegal_decl_array_of_references)
1097 << getPrintableNameForEntity(Entity) << T;
1098 return QualType();
1099 }
1100
Sebastian Redl923d56d2009-11-05 15:52:31 +00001101 if (T->isVoidType()) {
1102 Diag(Loc, diag::err_illegal_decl_array_incomplete_type) << T;
1103 return QualType();
1104 }
Douglas Gregor138bb232010-04-27 19:38:14 +00001105
1106 if (RequireNonAbstractType(Brackets.getBegin(), T,
1107 diag::err_array_of_abstract_type))
1108 return QualType();
1109
Sebastian Redl923d56d2009-11-05 15:52:31 +00001110 } else {
Douglas Gregor138bb232010-04-27 19:38:14 +00001111 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
1112 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
Sebastian Redl923d56d2009-11-05 15:52:31 +00001113 if (RequireCompleteType(Loc, T,
1114 diag::err_illegal_decl_array_incomplete_type))
1115 return QualType();
1116 }
Douglas Gregorcd281c32009-02-28 00:25:32 +00001117
1118 if (T->isFunctionType()) {
1119 Diag(Loc, diag::err_illegal_decl_array_of_functions)
John McCallac406052009-10-30 00:37:20 +00001120 << getPrintableNameForEntity(Entity) << T;
Douglas Gregorcd281c32009-02-28 00:25:32 +00001121 return QualType();
1122 }
Mike Stump1eb44332009-09-09 15:08:12 +00001123
Richard Smith34b41d92011-02-20 03:19:35 +00001124 if (T->getContainedAutoType()) {
1125 Diag(Loc, diag::err_illegal_decl_array_of_auto)
1126 << getPrintableNameForEntity(Entity) << T;
Anders Carlssone7cf07d2009-06-26 19:33:28 +00001127 return QualType();
1128 }
Mike Stump1eb44332009-09-09 15:08:12 +00001129
Ted Kremenek6217b802009-07-29 21:53:49 +00001130 if (const RecordType *EltTy = T->getAs<RecordType>()) {
Douglas Gregorcd281c32009-02-28 00:25:32 +00001131 // If the element type is a struct or union that contains a variadic
1132 // array, accept it as a GNU extension: C99 6.7.2.1p2.
1133 if (EltTy->getDecl()->hasFlexibleArrayMember())
1134 Diag(Loc, diag::ext_flexible_array_in_array) << T;
John McCallc12c5bb2010-05-15 11:32:37 +00001135 } else if (T->isObjCObjectType()) {
Chris Lattnerc7c11b12009-04-27 01:55:56 +00001136 Diag(Loc, diag::err_objc_array_of_interfaces) << T;
1137 return QualType();
Douglas Gregorcd281c32009-02-28 00:25:32 +00001138 }
Mike Stump1eb44332009-09-09 15:08:12 +00001139
John McCall5e3c67b2010-12-15 04:42:30 +00001140 // Do lvalue-to-rvalue conversions on the array size expression.
1141 if (ArraySize && !ArraySize->isRValue())
1142 DefaultLvalueConversion(ArraySize);
1143
Douglas Gregorcd281c32009-02-28 00:25:32 +00001144 // C99 6.7.5.2p1: The size expression shall have integer type.
John McCall5e3c67b2010-12-15 04:42:30 +00001145 // TODO: in theory, if we were insane, we could allow contextual
1146 // conversions to integer type here.
Douglas Gregorcd281c32009-02-28 00:25:32 +00001147 if (ArraySize && !ArraySize->isTypeDependent() &&
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001148 !ArraySize->getType()->isIntegralOrUnscopedEnumerationType()) {
Douglas Gregorcd281c32009-02-28 00:25:32 +00001149 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
1150 << ArraySize->getType() << ArraySize->getSourceRange();
Douglas Gregorcd281c32009-02-28 00:25:32 +00001151 return QualType();
1152 }
Douglas Gregor2767ce22010-08-18 00:39:00 +00001153 llvm::APSInt ConstVal(Context.getTypeSize(Context.getSizeType()));
Douglas Gregorcd281c32009-02-28 00:25:32 +00001154 if (!ArraySize) {
Eli Friedmanf91f5c82009-04-26 21:57:51 +00001155 if (ASM == ArrayType::Star)
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001156 T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets);
Eli Friedmanf91f5c82009-04-26 21:57:51 +00001157 else
1158 T = Context.getIncompleteArrayType(T, ASM, Quals);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001159 } else if (ArraySize->isTypeDependent() || ArraySize->isValueDependent()) {
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001160 T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets);
Douglas Gregorcd281c32009-02-28 00:25:32 +00001161 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
Sebastian Redl923d56d2009-11-05 15:52:31 +00001162 (!T->isDependentType() && !T->isIncompleteType() &&
1163 !T->isConstantSizeType())) {
Douglas Gregorcd281c32009-02-28 00:25:32 +00001164 // Per C99, a variable array is an array with either a non-constant
1165 // size or an element type that has a non-constant-size
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001166 T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets);
Douglas Gregorcd281c32009-02-28 00:25:32 +00001167 } else {
1168 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
1169 // have a value greater than zero.
Sebastian Redl923d56d2009-11-05 15:52:31 +00001170 if (ConstVal.isSigned() && ConstVal.isNegative()) {
Chandler Carruthb2b5cc02011-01-04 04:44:35 +00001171 if (Entity)
1172 Diag(ArraySize->getLocStart(), diag::err_decl_negative_array_size)
1173 << getPrintableNameForEntity(Entity) << ArraySize->getSourceRange();
1174 else
1175 Diag(ArraySize->getLocStart(), diag::err_typecheck_negative_array_size)
1176 << ArraySize->getSourceRange();
Sebastian Redl923d56d2009-11-05 15:52:31 +00001177 return QualType();
1178 }
1179 if (ConstVal == 0) {
Douglas Gregor02024a92010-03-28 02:42:43 +00001180 // GCC accepts zero sized static arrays. We allow them when
1181 // we're not in a SFINAE context.
1182 Diag(ArraySize->getLocStart(),
1183 isSFINAEContext()? diag::err_typecheck_zero_array_size
1184 : diag::ext_typecheck_zero_array_size)
Sebastian Redl923d56d2009-11-05 15:52:31 +00001185 << ArraySize->getSourceRange();
Douglas Gregor2767ce22010-08-18 00:39:00 +00001186 } else if (!T->isDependentType() && !T->isVariablyModifiedType() &&
1187 !T->isIncompleteType()) {
1188 // Is the array too large?
1189 unsigned ActiveSizeBits
1190 = ConstantArrayType::getNumAddressingBits(Context, T, ConstVal);
1191 if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context))
1192 Diag(ArraySize->getLocStart(), diag::err_array_too_large)
1193 << ConstVal.toString(10)
1194 << ArraySize->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001195 }
Douglas Gregor2767ce22010-08-18 00:39:00 +00001196
John McCall46a617a2009-10-16 00:14:28 +00001197 T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +00001198 }
David Chisnallaf407762010-01-11 23:08:08 +00001199 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
1200 if (!getLangOptions().C99) {
Douglas Gregor0fddb972010-05-22 16:17:30 +00001201 if (T->isVariableArrayType()) {
1202 // Prohibit the use of non-POD types in VLAs.
Douglas Gregor204ce172010-05-24 20:42:30 +00001203 if (!T->isDependentType() &&
1204 !Context.getBaseElementType(T)->isPODType()) {
Douglas Gregor0fddb972010-05-22 16:17:30 +00001205 Diag(Loc, diag::err_vla_non_pod)
1206 << Context.getBaseElementType(T);
1207 return QualType();
1208 }
Douglas Gregora481ec42010-05-23 19:57:01 +00001209 // Prohibit the use of VLAs during template argument deduction.
1210 else if (isSFINAEContext()) {
1211 Diag(Loc, diag::err_vla_in_sfinae);
1212 return QualType();
1213 }
Douglas Gregor0fddb972010-05-22 16:17:30 +00001214 // Just extwarn about VLAs.
1215 else
1216 Diag(Loc, diag::ext_vla);
1217 } else if (ASM != ArrayType::Normal || Quals != 0)
Douglas Gregor043cad22009-09-11 00:18:58 +00001218 Diag(Loc,
1219 getLangOptions().CPlusPlus? diag::err_c99_array_usage_cxx
1220 : diag::ext_c99_array_usage);
Douglas Gregorcd281c32009-02-28 00:25:32 +00001221 }
1222
1223 return T;
1224}
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001225
1226/// \brief Build an ext-vector type.
1227///
1228/// Run the required checks for the extended vector type.
John McCall9ae2f072010-08-23 23:25:46 +00001229QualType Sema::BuildExtVectorType(QualType T, Expr *ArraySize,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001230 SourceLocation AttrLoc) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001231 // unlike gcc's vector_size attribute, we do not allow vectors to be defined
1232 // in conjunction with complex types (pointers, arrays, functions, etc.).
Mike Stump1eb44332009-09-09 15:08:12 +00001233 if (!T->isDependentType() &&
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001234 !T->isIntegerType() && !T->isRealFloatingType()) {
1235 Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T;
1236 return QualType();
1237 }
1238
John McCall9ae2f072010-08-23 23:25:46 +00001239 if (!ArraySize->isTypeDependent() && !ArraySize->isValueDependent()) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001240 llvm::APSInt vecSize(32);
John McCall9ae2f072010-08-23 23:25:46 +00001241 if (!ArraySize->isIntegerConstantExpr(vecSize, Context)) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001242 Diag(AttrLoc, diag::err_attribute_argument_not_int)
John McCall9ae2f072010-08-23 23:25:46 +00001243 << "ext_vector_type" << ArraySize->getSourceRange();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001244 return QualType();
1245 }
Mike Stump1eb44332009-09-09 15:08:12 +00001246
1247 // unlike gcc's vector_size attribute, the size is specified as the
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001248 // number of elements, not the number of bytes.
Mike Stump1eb44332009-09-09 15:08:12 +00001249 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
1250
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001251 if (vectorSize == 0) {
1252 Diag(AttrLoc, diag::err_attribute_zero_size)
John McCall9ae2f072010-08-23 23:25:46 +00001253 << ArraySize->getSourceRange();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001254 return QualType();
1255 }
Mike Stump1eb44332009-09-09 15:08:12 +00001256
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001257 if (!T->isDependentType())
1258 return Context.getExtVectorType(T, vectorSize);
Mike Stump1eb44332009-09-09 15:08:12 +00001259 }
1260
John McCall9ae2f072010-08-23 23:25:46 +00001261 return Context.getDependentSizedExtVectorType(T, ArraySize, AttrLoc);
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001262}
Mike Stump1eb44332009-09-09 15:08:12 +00001263
Douglas Gregor724651c2009-02-28 01:04:19 +00001264/// \brief Build a function type.
1265///
1266/// This routine checks the function type according to C++ rules and
1267/// under the assumption that the result type and parameter types have
1268/// just been instantiated from a template. It therefore duplicates
Douglas Gregor2943aed2009-03-03 04:44:36 +00001269/// some of the behavior of GetTypeForDeclarator, but in a much
Douglas Gregor724651c2009-02-28 01:04:19 +00001270/// simpler form that is only suitable for this narrow use case.
1271///
1272/// \param T The return type of the function.
1273///
1274/// \param ParamTypes The parameter types of the function. This array
1275/// will be modified to account for adjustments to the types of the
1276/// function parameters.
1277///
1278/// \param NumParamTypes The number of parameter types in ParamTypes.
1279///
1280/// \param Variadic Whether this is a variadic function type.
1281///
1282/// \param Quals The cvr-qualifiers to be applied to the function type.
1283///
1284/// \param Loc The location of the entity whose type involves this
1285/// function type or, if there is no such entity, the location of the
1286/// type that will have function type.
1287///
1288/// \param Entity The name of the entity that involves the function
1289/// type, if known.
1290///
1291/// \returns A suitable function type, if there are no
1292/// errors. Otherwise, returns a NULL type.
1293QualType Sema::BuildFunctionType(QualType T,
Mike Stump1eb44332009-09-09 15:08:12 +00001294 QualType *ParamTypes,
Douglas Gregor724651c2009-02-28 01:04:19 +00001295 unsigned NumParamTypes,
1296 bool Variadic, unsigned Quals,
Douglas Gregorc938c162011-01-26 05:01:58 +00001297 RefQualifierKind RefQualifier,
Eli Friedmanfa869542010-08-05 02:54:05 +00001298 SourceLocation Loc, DeclarationName Entity,
John McCalle23cf432010-12-14 08:05:40 +00001299 FunctionType::ExtInfo Info) {
Douglas Gregor724651c2009-02-28 01:04:19 +00001300 if (T->isArrayType() || T->isFunctionType()) {
Douglas Gregor58408bc2010-01-11 18:46:21 +00001301 Diag(Loc, diag::err_func_returning_array_function)
1302 << T->isFunctionType() << T;
Douglas Gregor724651c2009-02-28 01:04:19 +00001303 return QualType();
1304 }
Douglas Gregor5291c3c2010-07-13 08:18:22 +00001305
Douglas Gregor724651c2009-02-28 01:04:19 +00001306 bool Invalid = false;
1307 for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) {
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001308 QualType ParamType = adjustParameterType(ParamTypes[Idx]);
1309 if (ParamType->isVoidType()) {
Douglas Gregor724651c2009-02-28 01:04:19 +00001310 Diag(Loc, diag::err_param_with_void_type);
1311 Invalid = true;
1312 }
Douglas Gregorcd281c32009-02-28 00:25:32 +00001313
John McCall54e14c42009-10-22 22:37:11 +00001314 ParamTypes[Idx] = ParamType;
Douglas Gregor724651c2009-02-28 01:04:19 +00001315 }
1316
1317 if (Invalid)
1318 return QualType();
1319
John McCalle23cf432010-12-14 08:05:40 +00001320 FunctionProtoType::ExtProtoInfo EPI;
1321 EPI.Variadic = Variadic;
1322 EPI.TypeQuals = Quals;
Douglas Gregorc938c162011-01-26 05:01:58 +00001323 EPI.RefQualifier = RefQualifier;
John McCalle23cf432010-12-14 08:05:40 +00001324 EPI.ExtInfo = Info;
1325
1326 return Context.getFunctionType(T, ParamTypes, NumParamTypes, EPI);
Douglas Gregor724651c2009-02-28 01:04:19 +00001327}
Mike Stump1eb44332009-09-09 15:08:12 +00001328
Douglas Gregor949bf692009-06-09 22:17:39 +00001329/// \brief Build a member pointer type \c T Class::*.
1330///
1331/// \param T the type to which the member pointer refers.
1332/// \param Class the class type into which the member pointer points.
John McCall0953e762009-09-24 19:53:00 +00001333/// \param CVR Qualifiers applied to the member pointer type
Douglas Gregor949bf692009-06-09 22:17:39 +00001334/// \param Loc the location where this type begins
1335/// \param Entity the name of the entity that will have this member pointer type
1336///
1337/// \returns a member pointer type, if successful, or a NULL type if there was
1338/// an error.
Mike Stump1eb44332009-09-09 15:08:12 +00001339QualType Sema::BuildMemberPointerType(QualType T, QualType Class,
John McCall28654742010-06-05 06:41:15 +00001340 SourceLocation Loc,
Douglas Gregor949bf692009-06-09 22:17:39 +00001341 DeclarationName Entity) {
1342 // Verify that we're not building a pointer to pointer to function with
1343 // exception specification.
1344 if (CheckDistantExceptionSpec(T)) {
1345 Diag(Loc, diag::err_distant_exception_spec);
1346
1347 // FIXME: If we're doing this as part of template instantiation,
1348 // we should return immediately.
1349
1350 // Build the type anyway, but use the canonical type so that the
1351 // exception specifiers are stripped off.
1352 T = Context.getCanonicalType(T);
1353 }
1354
Sebastian Redl73780122010-06-09 21:19:43 +00001355 // C++ 8.3.3p3: A pointer to member shall not point to ... a member
Douglas Gregor949bf692009-06-09 22:17:39 +00001356 // with reference type, or "cv void."
1357 if (T->isReferenceType()) {
Anders Carlsson8d4655d2009-06-30 00:06:57 +00001358 Diag(Loc, diag::err_illegal_decl_mempointer_to_reference)
John McCallac406052009-10-30 00:37:20 +00001359 << (Entity? Entity.getAsString() : "type name") << T;
Douglas Gregor949bf692009-06-09 22:17:39 +00001360 return QualType();
1361 }
1362
1363 if (T->isVoidType()) {
1364 Diag(Loc, diag::err_illegal_decl_mempointer_to_void)
1365 << (Entity? Entity.getAsString() : "type name");
1366 return QualType();
1367 }
1368
Douglas Gregor949bf692009-06-09 22:17:39 +00001369 if (!Class->isDependentType() && !Class->isRecordType()) {
1370 Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class;
1371 return QualType();
1372 }
1373
Charles Davisd18f9f92010-08-16 04:01:50 +00001374 // In the Microsoft ABI, the class is allowed to be an incomplete
1375 // type. In such cases, the compiler makes a worst-case assumption.
1376 // We make no such assumption right now, so emit an error if the
1377 // class isn't a complete type.
Charles Davis20cf7172010-08-19 02:18:14 +00001378 if (Context.Target.getCXXABI() == CXXABI_Microsoft &&
Charles Davisd18f9f92010-08-16 04:01:50 +00001379 RequireCompleteType(Loc, Class, diag::err_incomplete_type))
1380 return QualType();
1381
John McCall28654742010-06-05 06:41:15 +00001382 return Context.getMemberPointerType(T, Class.getTypePtr());
Douglas Gregor949bf692009-06-09 22:17:39 +00001383}
Mike Stump1eb44332009-09-09 15:08:12 +00001384
Anders Carlsson9a917e42009-06-12 22:56:54 +00001385/// \brief Build a block pointer type.
1386///
1387/// \param T The type to which we'll be building a block pointer.
1388///
John McCall0953e762009-09-24 19:53:00 +00001389/// \param CVR The cvr-qualifiers to be applied to the block pointer type.
Anders Carlsson9a917e42009-06-12 22:56:54 +00001390///
1391/// \param Loc The location of the entity whose type involves this
1392/// block pointer type or, if there is no such entity, the location of the
1393/// type that will have block pointer type.
1394///
1395/// \param Entity The name of the entity that involves the block pointer
1396/// type, if known.
1397///
1398/// \returns A suitable block pointer type, if there are no
1399/// errors. Otherwise, returns a NULL type.
John McCall28654742010-06-05 06:41:15 +00001400QualType Sema::BuildBlockPointerType(QualType T,
Mike Stump1eb44332009-09-09 15:08:12 +00001401 SourceLocation Loc,
Anders Carlsson9a917e42009-06-12 22:56:54 +00001402 DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +00001403 if (!T->isFunctionType()) {
Anders Carlsson9a917e42009-06-12 22:56:54 +00001404 Diag(Loc, diag::err_nonfunction_block_type);
1405 return QualType();
1406 }
Mike Stump1eb44332009-09-09 15:08:12 +00001407
John McCall28654742010-06-05 06:41:15 +00001408 return Context.getBlockPointerType(T);
Anders Carlsson9a917e42009-06-12 22:56:54 +00001409}
1410
John McCallb3d87482010-08-24 05:47:05 +00001411QualType Sema::GetTypeFromParser(ParsedType Ty, TypeSourceInfo **TInfo) {
1412 QualType QT = Ty.get();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001413 if (QT.isNull()) {
John McCalla93c9342009-12-07 02:54:59 +00001414 if (TInfo) *TInfo = 0;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001415 return QualType();
1416 }
1417
John McCalla93c9342009-12-07 02:54:59 +00001418 TypeSourceInfo *DI = 0;
John McCallf4c73712011-01-19 06:33:43 +00001419 if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001420 QT = LIT->getType();
John McCalla93c9342009-12-07 02:54:59 +00001421 DI = LIT->getTypeSourceInfo();
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001422 }
Mike Stump1eb44332009-09-09 15:08:12 +00001423
John McCalla93c9342009-12-07 02:54:59 +00001424 if (TInfo) *TInfo = DI;
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001425 return QT;
1426}
1427
Chandler Carruthd067c072011-02-23 18:51:59 +00001428static void DiagnoseIgnoredQualifiers(unsigned Quals,
1429 SourceLocation ConstQualLoc,
1430 SourceLocation VolatileQualLoc,
1431 SourceLocation RestrictQualLoc,
1432 Sema& S) {
1433 std::string QualStr;
1434 unsigned NumQuals = 0;
1435 SourceLocation Loc;
1436
1437 FixItHint ConstFixIt;
1438 FixItHint VolatileFixIt;
1439 FixItHint RestrictFixIt;
1440
1441 // FIXME: The locations here are set kind of arbitrarily. It'd be nicer to
1442 // find a range and grow it to encompass all the qualifiers, regardless of
1443 // the order in which they textually appear.
1444 if (Quals & Qualifiers::Const) {
1445 ConstFixIt = FixItHint::CreateRemoval(ConstQualLoc);
1446 Loc = ConstQualLoc;
1447 ++NumQuals;
1448 QualStr = "const";
1449 }
1450 if (Quals & Qualifiers::Volatile) {
1451 VolatileFixIt = FixItHint::CreateRemoval(VolatileQualLoc);
1452 if (NumQuals == 0) {
1453 Loc = VolatileQualLoc;
1454 QualStr = "volatile";
1455 } else {
1456 QualStr += " volatile";
1457 }
1458 ++NumQuals;
1459 }
1460 if (Quals & Qualifiers::Restrict) {
1461 RestrictFixIt = FixItHint::CreateRemoval(RestrictQualLoc);
1462 if (NumQuals == 0) {
1463 Loc = RestrictQualLoc;
1464 QualStr = "restrict";
1465 } else {
1466 QualStr += " restrict";
1467 }
1468 ++NumQuals;
1469 }
1470
1471 assert(NumQuals > 0 && "No known qualifiers?");
1472
1473 S.Diag(Loc, diag::warn_qual_return_type)
1474 << QualStr << NumQuals
1475 << ConstFixIt << VolatileFixIt << RestrictFixIt;
1476}
1477
Mike Stump98eb8a72009-02-04 22:31:32 +00001478/// GetTypeForDeclarator - Convert the type for the specified
Sebastian Redl8ce35b02009-10-25 21:45:37 +00001479/// declarator to Type instances.
Douglas Gregor402abb52009-05-28 23:31:59 +00001480///
1481/// If OwnedDecl is non-NULL, and this declarator's decl-specifier-seq
1482/// owns the declaration of a type (e.g., the definition of a struct
1483/// type), then *OwnedDecl will receive the owned declaration.
John McCallbf1a0282010-06-04 23:28:52 +00001484///
1485/// The result of this call will never be null, but the associated
1486/// type may be a null type if there's an unrecoverable error.
1487TypeSourceInfo *Sema::GetTypeForDeclarator(Declarator &D, Scope *S,
Richard Smith34b41d92011-02-20 03:19:35 +00001488 TagDecl **OwnedDecl,
1489 bool AutoAllowedInTypeName) {
Douglas Gregor930d8b52009-01-30 22:09:00 +00001490 // Determine the type of the declarator. Not all forms of declarator
1491 // have a type.
1492 QualType T;
Douglas Gregor05baacb2010-04-12 23:19:01 +00001493 TypeSourceInfo *ReturnTypeInfo = 0;
John McCall711c52b2011-01-05 12:14:39 +00001494
1495 TypeProcessingState state(*this, D);
John McCall04a67a62010-02-05 21:31:56 +00001496
Sebastian Redl8999fe12011-03-14 18:08:30 +00001497 // In C++0x, deallocation functions (normal and array operator delete)
1498 // are implicitly noexcept.
1499 bool ImplicitlyNoexcept = false;
1500
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001501 switch (D.getName().getKind()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001502 case UnqualifiedId::IK_OperatorFunctionId:
Sebastian Redl8999fe12011-03-14 18:08:30 +00001503 if (getLangOptions().CPlusPlus0x) {
1504 OverloadedOperatorKind OO = D.getName().OperatorFunctionId.Operator;
1505 if (OO == OO_Delete || OO == OO_Array_Delete)
1506 ImplicitlyNoexcept = true;
1507 }
1508 // Intentional fall-through.
1509 case UnqualifiedId::IK_Identifier:
Sean Hunt0486d742009-11-28 04:44:28 +00001510 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001511 case UnqualifiedId::IK_TemplateId:
John McCall711c52b2011-01-05 12:14:39 +00001512 T = ConvertDeclSpecToType(*this, state);
Chris Lattner5db2bb12009-10-25 18:21:37 +00001513
Douglas Gregor591bd3c2010-02-08 22:07:33 +00001514 if (!D.isInvalidType() && D.getDeclSpec().isTypeSpecOwned()) {
John McCallb3d87482010-08-24 05:47:05 +00001515 TagDecl* Owned = cast<TagDecl>(D.getDeclSpec().getRepAsDecl());
Abramo Bagnara15987972011-03-08 22:33:38 +00001516 // Owned declaration is embedded in declarator.
1517 Owned->setEmbeddedInDeclarator(true);
Douglas Gregor591bd3c2010-02-08 22:07:33 +00001518 if (OwnedDecl) *OwnedDecl = Owned;
1519 }
Douglas Gregor930d8b52009-01-30 22:09:00 +00001520 break;
1521
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001522 case UnqualifiedId::IK_ConstructorName:
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001523 case UnqualifiedId::IK_ConstructorTemplateId:
Douglas Gregor3f9a0562009-11-03 01:35:08 +00001524 case UnqualifiedId::IK_DestructorName:
Douglas Gregor930d8b52009-01-30 22:09:00 +00001525 // Constructors and destructors don't have return types. Use
Douglas Gregor48026d22010-01-11 18:40:55 +00001526 // "void" instead.
Douglas Gregor930d8b52009-01-30 22:09:00 +00001527 T = Context.VoidTy;
1528 break;
Douglas Gregor48026d22010-01-11 18:40:55 +00001529
1530 case UnqualifiedId::IK_ConversionFunctionId:
1531 // The result type of a conversion function is the type that it
1532 // converts to.
Douglas Gregor05baacb2010-04-12 23:19:01 +00001533 T = GetTypeFromParser(D.getName().ConversionFunctionId,
John McCallbf1a0282010-06-04 23:28:52 +00001534 &ReturnTypeInfo);
Douglas Gregor48026d22010-01-11 18:40:55 +00001535 break;
Douglas Gregor930d8b52009-01-30 22:09:00 +00001536 }
Douglas Gregordab60ad2010-10-01 18:44:50 +00001537
John McCall711c52b2011-01-05 12:14:39 +00001538 if (D.getAttributes())
1539 distributeTypeAttrsFromDeclarator(state, T);
1540
Richard Smithe7397c62011-02-22 00:36:53 +00001541 // C++0x [dcl.spec.auto]p5: reject 'auto' if it is not in an allowed context.
Richard Smith8110f042011-02-22 01:22:29 +00001542 // In C++0x, a function declarator using 'auto' must have a trailing return
1543 // type (this is checked later) and we can skip this. In other languages
1544 // using auto, we need to check regardless.
Richard Smith34b41d92011-02-20 03:19:35 +00001545 if (D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto &&
Richard Smith8110f042011-02-22 01:22:29 +00001546 (!getLangOptions().CPlusPlus0x || !D.isFunctionDeclarator())) {
Anders Carlssonbaf45d32009-06-26 22:18:59 +00001547 int Error = -1;
Mike Stump1eb44332009-09-09 15:08:12 +00001548
Anders Carlssonbaf45d32009-06-26 22:18:59 +00001549 switch (D.getContext()) {
1550 case Declarator::KNRTypeListContext:
1551 assert(0 && "K&R type lists aren't allowed in C++");
1552 break;
John McCallc05a94b2011-03-23 23:43:04 +00001553 case Declarator::ObjCPrototypeContext:
Anders Carlssonbaf45d32009-06-26 22:18:59 +00001554 case Declarator::PrototypeContext:
1555 Error = 0; // Function prototype
1556 break;
1557 case Declarator::MemberContext:
1558 switch (cast<TagDecl>(CurContext)->getTagKind()) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001559 case TTK_Enum: assert(0 && "unhandled tag kind"); break;
1560 case TTK_Struct: Error = 1; /* Struct member */ break;
1561 case TTK_Union: Error = 2; /* Union member */ break;
1562 case TTK_Class: Error = 3; /* Class member */ break;
Mike Stump1eb44332009-09-09 15:08:12 +00001563 }
Anders Carlssonbaf45d32009-06-26 22:18:59 +00001564 break;
1565 case Declarator::CXXCatchContext:
1566 Error = 4; // Exception declaration
1567 break;
1568 case Declarator::TemplateParamContext:
1569 Error = 5; // Template parameter
1570 break;
1571 case Declarator::BlockLiteralContext:
Richard Smith34b41d92011-02-20 03:19:35 +00001572 Error = 6; // Block literal
1573 break;
1574 case Declarator::TemplateTypeArgContext:
1575 Error = 7; // Template type argument
1576 break;
1577 case Declarator::TypeNameContext:
1578 if (!AutoAllowedInTypeName)
Richard Smith8110f042011-02-22 01:22:29 +00001579 Error = 10; // Generic
Anders Carlssonbaf45d32009-06-26 22:18:59 +00001580 break;
1581 case Declarator::FileContext:
1582 case Declarator::BlockContext:
1583 case Declarator::ForContext:
1584 case Declarator::ConditionContext:
Anders Carlssonbaf45d32009-06-26 22:18:59 +00001585 break;
1586 }
1587
Richard Smithddc83f92011-02-21 23:18:00 +00001588 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
1589 Error = 8;
1590
Richard Smith8110f042011-02-22 01:22:29 +00001591 // In Objective-C it is an error to use 'auto' on a function declarator.
1592 if (D.isFunctionDeclarator())
1593 Error = 9;
1594
Richard Smithe7397c62011-02-22 00:36:53 +00001595 // C++0x [dcl.spec.auto]p2: 'auto' is always fine if the declarator
1596 // contains a trailing return type. That is only legal at the outermost
1597 // level. Check all declarator chunks (outermost first) anyway, to give
1598 // better diagnostics.
Richard Smith8110f042011-02-22 01:22:29 +00001599 if (getLangOptions().CPlusPlus0x && Error != -1) {
Richard Smithe7397c62011-02-22 00:36:53 +00001600 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
1601 unsigned chunkIndex = e - i - 1;
1602 state.setCurrentChunkIndex(chunkIndex);
1603 DeclaratorChunk &DeclType = D.getTypeObject(chunkIndex);
1604 if (DeclType.Kind == DeclaratorChunk::Function) {
1605 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
1606 if (FTI.TrailingReturnType) {
1607 Error = -1;
1608 break;
1609 }
1610 }
1611 }
1612 }
1613
Anders Carlssonbaf45d32009-06-26 22:18:59 +00001614 if (Error != -1) {
1615 Diag(D.getDeclSpec().getTypeSpecTypeLoc(), diag::err_auto_not_allowed)
1616 << Error;
1617 T = Context.IntTy;
1618 D.setInvalidType(true);
1619 }
1620 }
Richard Smithe7397c62011-02-22 00:36:53 +00001621
Richard Smith34b41d92011-02-20 03:19:35 +00001622 if (T.isNull())
1623 return Context.getNullTypeSourceInfo();
1624
Douglas Gregorcd281c32009-02-28 00:25:32 +00001625 // The name we're declaring, if any.
1626 DeclarationName Name;
1627 if (D.getIdentifier())
1628 Name = D.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00001629
Mike Stump98eb8a72009-02-04 22:31:32 +00001630 // Walk the DeclTypeInfo, building the recursive type as we go.
1631 // DeclTypeInfos are ordered from the identifier out, which is
1632 // opposite of what we want :).
Sebastian Redl8ce35b02009-10-25 21:45:37 +00001633 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
John McCall711c52b2011-01-05 12:14:39 +00001634 unsigned chunkIndex = e - i - 1;
1635 state.setCurrentChunkIndex(chunkIndex);
1636 DeclaratorChunk &DeclType = D.getTypeObject(chunkIndex);
Reid Spencer5f016e22007-07-11 17:01:13 +00001637 switch (DeclType.Kind) {
1638 default: assert(0 && "Unknown decltype!");
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001639 case DeclaratorChunk::Paren:
1640 T = BuildParenType(T);
1641 break;
Steve Naroff5618bd42008-08-27 16:04:49 +00001642 case DeclaratorChunk::BlockPointer:
Chris Lattner9af55002009-03-27 04:18:06 +00001643 // If blocks are disabled, emit an error.
1644 if (!LangOpts.Blocks)
1645 Diag(DeclType.Loc, diag::err_blocks_disable);
Mike Stump1eb44332009-09-09 15:08:12 +00001646
John McCall28654742010-06-05 06:41:15 +00001647 T = BuildBlockPointerType(T, D.getIdentifierLoc(), Name);
1648 if (DeclType.Cls.TypeQuals)
1649 T = BuildQualifiedType(T, DeclType.Loc, DeclType.Cls.TypeQuals);
Steve Naroff5618bd42008-08-27 16:04:49 +00001650 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001651 case DeclaratorChunk::Pointer:
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001652 // Verify that we're not building a pointer to pointer to function with
1653 // exception specification.
1654 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1655 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1656 D.setInvalidType(true);
1657 // Build the type anyway.
1658 }
John McCallc12c5bb2010-05-15 11:32:37 +00001659 if (getLangOptions().ObjC1 && T->getAs<ObjCObjectType>()) {
1660 T = Context.getObjCObjectPointerType(T);
John McCall28654742010-06-05 06:41:15 +00001661 if (DeclType.Ptr.TypeQuals)
1662 T = BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals);
Steve Naroff14108da2009-07-10 23:34:53 +00001663 break;
1664 }
John McCall28654742010-06-05 06:41:15 +00001665 T = BuildPointerType(T, DeclType.Loc, Name);
1666 if (DeclType.Ptr.TypeQuals)
1667 T = BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals);
John McCall711c52b2011-01-05 12:14:39 +00001668
Reid Spencer5f016e22007-07-11 17:01:13 +00001669 break;
John McCall0953e762009-09-24 19:53:00 +00001670 case DeclaratorChunk::Reference: {
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001671 // Verify that we're not building a reference to pointer to function with
1672 // exception specification.
1673 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1674 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1675 D.setInvalidType(true);
1676 // Build the type anyway.
1677 }
John McCall28654742010-06-05 06:41:15 +00001678 T = BuildReferenceType(T, DeclType.Ref.LValueRef, DeclType.Loc, Name);
1679
1680 Qualifiers Quals;
1681 if (DeclType.Ref.HasRestrict)
1682 T = BuildQualifiedType(T, DeclType.Loc, Qualifiers::Restrict);
Reid Spencer5f016e22007-07-11 17:01:13 +00001683 break;
John McCall0953e762009-09-24 19:53:00 +00001684 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001685 case DeclaratorChunk::Array: {
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001686 // Verify that we're not building an array of pointers to function with
1687 // exception specification.
1688 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1689 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1690 D.setInvalidType(true);
1691 // Build the type anyway.
1692 }
Chris Lattnerfd89bc82008-04-02 01:05:10 +00001693 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner94f81fd2007-08-28 16:54:00 +00001694 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001695 ArrayType::ArraySizeModifier ASM;
1696 if (ATI.isStar)
1697 ASM = ArrayType::Star;
1698 else if (ATI.hasStatic)
1699 ASM = ArrayType::Static;
1700 else
1701 ASM = ArrayType::Normal;
John McCallc05a94b2011-03-23 23:43:04 +00001702 if (ASM == ArrayType::Star && !D.isPrototypeContext()) {
Eli Friedmanf91f5c82009-04-26 21:57:51 +00001703 // FIXME: This check isn't quite right: it allows star in prototypes
1704 // for function definitions, and disallows some edge cases detailed
1705 // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html
1706 Diag(DeclType.Loc, diag::err_array_star_outside_prototype);
1707 ASM = ArrayType::Normal;
1708 D.setInvalidType(true);
1709 }
John McCall0953e762009-09-24 19:53:00 +00001710 T = BuildArrayType(T, ASM, ArraySize,
1711 Qualifiers::fromCVRMask(ATI.TypeQuals),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001712 SourceRange(DeclType.Loc, DeclType.EndLoc), Name);
Reid Spencer5f016e22007-07-11 17:01:13 +00001713 break;
1714 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001715 case DeclaratorChunk::Function: {
Reid Spencer5f016e22007-07-11 17:01:13 +00001716 // If the function declarator has a prototype (i.e. it is not () and
1717 // does not have a K&R-style identifier list), then the arguments are part
1718 // of the type, otherwise the argument list is ().
1719 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Sebastian Redl3cc97262009-05-31 11:47:27 +00001720
Richard Smithe7397c62011-02-22 00:36:53 +00001721 // Check for auto functions and trailing return type and adjust the
1722 // return type accordingly.
1723 if (!D.isInvalidType()) {
1724 // trailing-return-type is only required if we're declaring a function,
1725 // and not, for instance, a pointer to a function.
1726 if (D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto &&
1727 !FTI.TrailingReturnType && chunkIndex == 0) {
1728 Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
1729 diag::err_auto_missing_trailing_return);
1730 T = Context.IntTy;
1731 D.setInvalidType(true);
1732 } else if (FTI.TrailingReturnType) {
1733 // T must be exactly 'auto' at this point. See CWG issue 681.
1734 if (isa<ParenType>(T)) {
1735 Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
1736 diag::err_trailing_return_in_parens)
1737 << T << D.getDeclSpec().getSourceRange();
1738 D.setInvalidType(true);
1739 } else if (T.hasQualifiers() || !isa<AutoType>(T)) {
1740 Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
1741 diag::err_trailing_return_without_auto)
1742 << T << D.getDeclSpec().getSourceRange();
1743 D.setInvalidType(true);
1744 }
1745
1746 T = GetTypeFromParser(
1747 ParsedType::getFromOpaquePtr(FTI.TrailingReturnType),
1748 &ReturnTypeInfo);
1749 }
1750 }
1751
Chris Lattnercd881292007-12-19 05:31:29 +00001752 // C99 6.7.5.3p1: The return type may not be a function or array type.
Douglas Gregor58408bc2010-01-11 18:46:21 +00001753 // For conversion functions, we'll diagnose this particular error later.
Douglas Gregor48026d22010-01-11 18:40:55 +00001754 if ((T->isArrayType() || T->isFunctionType()) &&
1755 (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId)) {
Argyrios Kyrtzidis98650442011-01-25 23:16:33 +00001756 unsigned diagID = diag::err_func_returning_array_function;
Argyrios Kyrtzidisa4356ad2011-01-26 01:26:44 +00001757 // Last processing chunk in block context means this function chunk
1758 // represents the block.
1759 if (chunkIndex == 0 &&
1760 D.getContext() == Declarator::BlockLiteralContext)
Argyrios Kyrtzidis98650442011-01-25 23:16:33 +00001761 diagID = diag::err_block_returning_array_function;
1762 Diag(DeclType.Loc, diagID) << T->isFunctionType() << T;
Chris Lattnercd881292007-12-19 05:31:29 +00001763 T = Context.IntTy;
1764 D.setInvalidType(true);
1765 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001766
Douglas Gregor5291c3c2010-07-13 08:18:22 +00001767 // cv-qualifiers on return types are pointless except when the type is a
1768 // class type in C++.
Douglas Gregorfff95132011-03-01 17:04:42 +00001769 if (isa<PointerType>(T) && T.getLocalCVRQualifiers() &&
Rafael Espindola1e153942011-03-11 04:56:58 +00001770 (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId) &&
Chandler Carruthd067c072011-02-23 18:51:59 +00001771 (!getLangOptions().CPlusPlus || !T->isDependentType())) {
1772 assert(chunkIndex + 1 < e && "No DeclaratorChunk for the return type?");
1773 DeclaratorChunk ReturnTypeChunk = D.getTypeObject(chunkIndex + 1);
1774 assert(ReturnTypeChunk.Kind == DeclaratorChunk::Pointer);
1775
1776 DeclaratorChunk::PointerTypeInfo &PTI = ReturnTypeChunk.Ptr;
1777
1778 DiagnoseIgnoredQualifiers(PTI.TypeQuals,
1779 SourceLocation::getFromRawEncoding(PTI.ConstQualLoc),
1780 SourceLocation::getFromRawEncoding(PTI.VolatileQualLoc),
1781 SourceLocation::getFromRawEncoding(PTI.RestrictQualLoc),
1782 *this);
1783
1784 } else if (T.getCVRQualifiers() && D.getDeclSpec().getTypeQualifiers() &&
Douglas Gregor5291c3c2010-07-13 08:18:22 +00001785 (!getLangOptions().CPlusPlus ||
1786 (!T->isDependentType() && !T->isRecordType()))) {
Chandler Carruthd067c072011-02-23 18:51:59 +00001787
1788 DiagnoseIgnoredQualifiers(D.getDeclSpec().getTypeQualifiers(),
1789 D.getDeclSpec().getConstSpecLoc(),
1790 D.getDeclSpec().getVolatileSpecLoc(),
1791 D.getDeclSpec().getRestrictSpecLoc(),
1792 *this);
Douglas Gregor5291c3c2010-07-13 08:18:22 +00001793 }
Chandler Carruthd067c072011-02-23 18:51:59 +00001794
Douglas Gregor402abb52009-05-28 23:31:59 +00001795 if (getLangOptions().CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) {
1796 // C++ [dcl.fct]p6:
1797 // Types shall not be defined in return or parameter types.
John McCallb3d87482010-08-24 05:47:05 +00001798 TagDecl *Tag = cast<TagDecl>(D.getDeclSpec().getRepAsDecl());
Douglas Gregor402abb52009-05-28 23:31:59 +00001799 if (Tag->isDefinition())
1800 Diag(Tag->getLocation(), diag::err_type_defined_in_result_type)
1801 << Context.getTypeDeclType(Tag);
1802 }
1803
Sebastian Redl3cc97262009-05-31 11:47:27 +00001804 // Exception specs are not allowed in typedefs. Complain, but add it
1805 // anyway.
Sebastian Redl6e5d3192011-03-05 22:42:13 +00001806 if (FTI.getExceptionSpecType() != EST_None &&
Sebastian Redl3cc97262009-05-31 11:47:27 +00001807 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
Sebastian Redl6e5d3192011-03-05 22:42:13 +00001808 Diag(FTI.getExceptionSpecLoc(), diag::err_exception_spec_in_typedef);
Sebastian Redl3cc97262009-05-31 11:47:27 +00001809
John McCall28654742010-06-05 06:41:15 +00001810 if (!FTI.NumArgs && !FTI.isVariadic && !getLangOptions().CPlusPlus) {
1811 // Simple void foo(), where the incoming T is the result type.
1812 T = Context.getFunctionNoProtoType(T);
1813 } else {
1814 // We allow a zero-parameter variadic function in C if the
1815 // function is marked with the "overloadable" attribute. Scan
1816 // for this attribute now.
1817 if (!FTI.NumArgs && FTI.isVariadic && !getLangOptions().CPlusPlus) {
Douglas Gregor965acbb2009-02-18 07:07:28 +00001818 bool Overloadable = false;
1819 for (const AttributeList *Attrs = D.getAttributes();
1820 Attrs; Attrs = Attrs->getNext()) {
1821 if (Attrs->getKind() == AttributeList::AT_overloadable) {
1822 Overloadable = true;
1823 break;
1824 }
1825 }
1826
1827 if (!Overloadable)
1828 Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001829 }
John McCall28654742010-06-05 06:41:15 +00001830
1831 if (FTI.NumArgs && FTI.ArgInfo[0].Param == 0) {
Chris Lattner788b0fd2010-06-23 06:00:24 +00001832 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function
1833 // definition.
John McCall28654742010-06-05 06:41:15 +00001834 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
1835 D.setInvalidType(true);
1836 break;
1837 }
1838
John McCalle23cf432010-12-14 08:05:40 +00001839 FunctionProtoType::ExtProtoInfo EPI;
1840 EPI.Variadic = FTI.isVariadic;
1841 EPI.TypeQuals = FTI.TypeQuals;
Douglas Gregorc938c162011-01-26 05:01:58 +00001842 EPI.RefQualifier = !FTI.hasRefQualifier()? RQ_None
1843 : FTI.RefQualifierIsLValueRef? RQ_LValue
1844 : RQ_RValue;
1845
Reid Spencer5f016e22007-07-11 17:01:13 +00001846 // Otherwise, we have a function with an argument list that is
1847 // potentially variadic.
1848 llvm::SmallVector<QualType, 16> ArgTys;
John McCall28654742010-06-05 06:41:15 +00001849 ArgTys.reserve(FTI.NumArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001850
Reid Spencer5f016e22007-07-11 17:01:13 +00001851 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00001852 ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param);
Chris Lattner8123a952008-04-10 02:22:51 +00001853 QualType ArgTy = Param->getType();
Chris Lattner78c75fb2007-07-21 05:30:18 +00001854 assert(!ArgTy.isNull() && "Couldn't parse type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001855
1856 // Adjust the parameter type.
Douglas Gregorbeb58cb2009-03-23 23:17:00 +00001857 assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001858
Reid Spencer5f016e22007-07-11 17:01:13 +00001859 // Look for 'void'. void is allowed only as a single argument to a
1860 // function with no other parameters (C99 6.7.5.3p10). We record
Douglas Gregor72564e72009-02-26 23:50:07 +00001861 // int(void) as a FunctionProtoType with an empty argument list.
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001862 if (ArgTy->isVoidType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001863 // If this is something like 'float(int, void)', reject it. 'void'
1864 // is an incomplete type (C99 6.2.5p19) and function decls cannot
1865 // have arguments of incomplete type.
1866 if (FTI.NumArgs != 1 || FTI.isVariadic) {
1867 Diag(DeclType.Loc, diag::err_void_only_param);
Chris Lattner2ff54262007-07-21 05:18:12 +00001868 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00001869 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00001870 } else if (FTI.ArgInfo[i].Ident) {
1871 // Reject, but continue to parse 'int(void abc)'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001872 Diag(FTI.ArgInfo[i].IdentLoc,
Chris Lattner4565d4e2007-07-21 05:26:43 +00001873 diag::err_param_with_void_type);
Chris Lattner2ff54262007-07-21 05:18:12 +00001874 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00001875 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00001876 } else {
1877 // Reject, but continue to parse 'float(const void)'.
John McCall0953e762009-09-24 19:53:00 +00001878 if (ArgTy.hasQualifiers())
Chris Lattner2ff54262007-07-21 05:18:12 +00001879 Diag(DeclType.Loc, diag::err_void_param_qualified);
Mike Stump1eb44332009-09-09 15:08:12 +00001880
Chris Lattner2ff54262007-07-21 05:18:12 +00001881 // Do not add 'void' to the ArgTys list.
1882 break;
1883 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001884 } else if (!FTI.hasPrototype) {
1885 if (ArgTy->isPromotableIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00001886 ArgTy = Context.getPromotedIntegerType(ArgTy);
John McCalleecf5fa2011-03-09 04:22:44 +00001887 Param->setKNRPromoted(true);
John McCall183700f2009-09-21 23:43:11 +00001888 } else if (const BuiltinType* BTy = ArgTy->getAs<BuiltinType>()) {
John McCalleecf5fa2011-03-09 04:22:44 +00001889 if (BTy->getKind() == BuiltinType::Float) {
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001890 ArgTy = Context.DoubleTy;
John McCalleecf5fa2011-03-09 04:22:44 +00001891 Param->setKNRPromoted(true);
1892 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001893 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001894 }
Fariborz Jahanian56a965c2010-09-08 21:36:35 +00001895
John McCall54e14c42009-10-22 22:37:11 +00001896 ArgTys.push_back(ArgTy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001897 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001898
1899 llvm::SmallVector<QualType, 4> Exceptions;
Sebastian Redl8b5b4092011-03-06 10:52:04 +00001900 EPI.ExceptionSpecType = FTI.getExceptionSpecType();
1901 if (FTI.getExceptionSpecType() == EST_Dynamic) {
John McCalle23cf432010-12-14 08:05:40 +00001902 Exceptions.reserve(FTI.NumExceptions);
1903 for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
1904 // FIXME: Preserve type source info.
1905 QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty);
1906 // Check that the type is valid for an exception spec, and
1907 // drop it if not.
1908 if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1909 Exceptions.push_back(ET);
1910 }
John McCall373920b2010-12-14 16:45:57 +00001911 EPI.NumExceptions = Exceptions.size();
John McCalle23cf432010-12-14 08:05:40 +00001912 EPI.Exceptions = Exceptions.data();
Sebastian Redl8b5b4092011-03-06 10:52:04 +00001913 } else if (FTI.getExceptionSpecType() == EST_ComputedNoexcept) {
Sebastian Redl60618fa2011-03-12 11:50:43 +00001914 // If an error occurred, there's no expression here.
1915 if (Expr *NoexceptExpr = FTI.NoexceptExpr) {
1916 assert((NoexceptExpr->isTypeDependent() ||
1917 NoexceptExpr->getType()->getCanonicalTypeUnqualified() ==
1918 Context.BoolTy) &&
1919 "Parser should have made sure that the expression is boolean");
1920 SourceLocation ErrLoc;
1921 llvm::APSInt Dummy;
1922 if (!NoexceptExpr->isValueDependent() &&
1923 !NoexceptExpr->isIntegerConstantExpr(Dummy, Context, &ErrLoc,
1924 /*evaluated*/false))
1925 Diag(ErrLoc, diag::err_noexcept_needs_constant_expression)
1926 << NoexceptExpr->getSourceRange();
1927 else
1928 EPI.NoexceptExpr = NoexceptExpr;
1929 }
Sebastian Redl8999fe12011-03-14 18:08:30 +00001930 } else if (FTI.getExceptionSpecType() == EST_None &&
1931 ImplicitlyNoexcept && chunkIndex == 0) {
1932 // Only the outermost chunk is marked noexcept, of course.
1933 EPI.ExceptionSpecType = EST_BasicNoexcept;
Sebastian Redlef65f062009-05-29 18:02:33 +00001934 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001935
John McCalle23cf432010-12-14 08:05:40 +00001936 T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(), EPI);
Reid Spencer5f016e22007-07-11 17:01:13 +00001937 }
John McCall04a67a62010-02-05 21:31:56 +00001938
Reid Spencer5f016e22007-07-11 17:01:13 +00001939 break;
1940 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001941 case DeclaratorChunk::MemberPointer:
1942 // The scope spec must refer to a class, or be dependent.
Abramo Bagnara7bd06762010-08-13 12:56:25 +00001943 CXXScopeSpec &SS = DeclType.Mem.Scope();
Sebastian Redlf30208a2009-01-24 21:16:55 +00001944 QualType ClsType;
Abramo Bagnara7bd06762010-08-13 12:56:25 +00001945 if (SS.isInvalid()) {
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00001946 // Avoid emitting extra errors if we already errored on the scope.
1947 D.setInvalidType(true);
Abramo Bagnara7bd06762010-08-13 12:56:25 +00001948 } else if (isDependentScopeSpecifier(SS) ||
1949 dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS))) {
Mike Stump1eb44332009-09-09 15:08:12 +00001950 NestedNameSpecifier *NNS
Abramo Bagnara7bd06762010-08-13 12:56:25 +00001951 = static_cast<NestedNameSpecifier*>(SS.getScopeRep());
Douglas Gregor87c12c42009-11-04 16:49:01 +00001952 NestedNameSpecifier *NNSPrefix = NNS->getPrefix();
1953 switch (NNS->getKind()) {
1954 case NestedNameSpecifier::Identifier:
Abramo Bagnara7bd06762010-08-13 12:56:25 +00001955 ClsType = Context.getDependentNameType(ETK_None, NNSPrefix,
Douglas Gregor4a2023f2010-03-31 20:19:30 +00001956 NNS->getAsIdentifier());
Douglas Gregor87c12c42009-11-04 16:49:01 +00001957 break;
1958
1959 case NestedNameSpecifier::Namespace:
Douglas Gregor14aba762011-02-24 02:36:08 +00001960 case NestedNameSpecifier::NamespaceAlias:
Douglas Gregor87c12c42009-11-04 16:49:01 +00001961 case NestedNameSpecifier::Global:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001962 llvm_unreachable("Nested-name-specifier must name a type");
Douglas Gregor87c12c42009-11-04 16:49:01 +00001963 break;
Abramo Bagnara7bd06762010-08-13 12:56:25 +00001964
Douglas Gregor87c12c42009-11-04 16:49:01 +00001965 case NestedNameSpecifier::TypeSpec:
1966 case NestedNameSpecifier::TypeSpecWithTemplate:
1967 ClsType = QualType(NNS->getAsType(), 0);
Abramo Bagnara91ce2c42011-03-10 10:18:27 +00001968 // Note: if the NNS has a prefix and ClsType is a nondependent
1969 // TemplateSpecializationType, then the NNS prefix is NOT included
1970 // in ClsType; hence we wrap ClsType into an ElaboratedType.
1971 // NOTE: in particular, no wrap occurs if ClsType already is an
1972 // Elaborated, DependentName, or DependentTemplateSpecialization.
1973 if (NNSPrefix && isa<TemplateSpecializationType>(NNS->getAsType()))
Abramo Bagnara7bd06762010-08-13 12:56:25 +00001974 ClsType = Context.getElaboratedType(ETK_None, NNSPrefix, ClsType);
Douglas Gregor87c12c42009-11-04 16:49:01 +00001975 break;
1976 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001977 } else {
Douglas Gregor949bf692009-06-09 22:17:39 +00001978 Diag(DeclType.Mem.Scope().getBeginLoc(),
1979 diag::err_illegal_decl_mempointer_in_nonclass)
1980 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
1981 << DeclType.Mem.Scope().getRange();
Sebastian Redlf30208a2009-01-24 21:16:55 +00001982 D.setInvalidType(true);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001983 }
1984
Douglas Gregor949bf692009-06-09 22:17:39 +00001985 if (!ClsType.isNull())
John McCall28654742010-06-05 06:41:15 +00001986 T = BuildMemberPointerType(T, ClsType, DeclType.Loc, D.getIdentifier());
Douglas Gregor949bf692009-06-09 22:17:39 +00001987 if (T.isNull()) {
1988 T = Context.IntTy;
Sebastian Redlf30208a2009-01-24 21:16:55 +00001989 D.setInvalidType(true);
John McCall28654742010-06-05 06:41:15 +00001990 } else if (DeclType.Mem.TypeQuals) {
1991 T = BuildQualifiedType(T, DeclType.Loc, DeclType.Mem.TypeQuals);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001992 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001993 break;
1994 }
1995
Douglas Gregorcd281c32009-02-28 00:25:32 +00001996 if (T.isNull()) {
1997 D.setInvalidType(true);
1998 T = Context.IntTy;
1999 }
2000
Chris Lattnerc9b346d2008-06-29 00:50:08 +00002001 // See if there are any attributes on this declarator chunk.
John McCall711c52b2011-01-05 12:14:39 +00002002 if (AttributeList *attrs = const_cast<AttributeList*>(DeclType.getAttrs()))
2003 processTypeAttrs(state, T, false, attrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002004 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002005
2006 if (getLangOptions().CPlusPlus && T->isFunctionType()) {
John McCall183700f2009-09-21 23:43:11 +00002007 const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>();
Chris Lattner778ed742009-10-25 17:36:50 +00002008 assert(FnTy && "Why oh why is there not a FunctionProtoType here?");
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002009
Douglas Gregor708f3b82010-10-14 22:03:51 +00002010 // C++ 8.3.5p4:
2011 // A cv-qualifier-seq shall only be part of the function type
2012 // for a nonstatic member function, the function type to which a pointer
2013 // to member refers, or the top-level function type of a function typedef
2014 // declaration.
Douglas Gregor683a81f2011-01-31 16:09:46 +00002015 //
2016 // Core issue 547 also allows cv-qualifiers on function types that are
2017 // top-level template type arguments.
John McCall613ef3d2010-10-19 01:54:45 +00002018 bool FreeFunction;
2019 if (!D.getCXXScopeSpec().isSet()) {
2020 FreeFunction = (D.getContext() != Declarator::MemberContext ||
2021 D.getDeclSpec().isFriendSpecified());
2022 } else {
2023 DeclContext *DC = computeDeclContext(D.getCXXScopeSpec());
2024 FreeFunction = (DC && !DC->isRecord());
2025 }
2026
Douglas Gregorc938c162011-01-26 05:01:58 +00002027 // C++0x [dcl.fct]p6:
2028 // A ref-qualifier shall only be part of the function type for a
2029 // non-static member function, the function type to which a pointer to
2030 // member refers, or the top-level function type of a function typedef
2031 // declaration.
2032 if ((FnTy->getTypeQuals() != 0 || FnTy->getRefQualifier()) &&
Douglas Gregor683a81f2011-01-31 16:09:46 +00002033 !(D.getContext() == Declarator::TemplateTypeArgContext &&
2034 !D.isFunctionDeclarator()) &&
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002035 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
Sebastian Redlc61bb202010-07-09 21:26:08 +00002036 (FreeFunction ||
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002037 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
Douglas Gregor683a81f2011-01-31 16:09:46 +00002038 if (D.getContext() == Declarator::TemplateTypeArgContext) {
2039 // Accept qualified function types as template type arguments as a GNU
2040 // extension. This is also the subject of C++ core issue 547.
2041 std::string Quals;
2042 if (FnTy->getTypeQuals() != 0)
2043 Quals = Qualifiers::fromCVRMask(FnTy->getTypeQuals()).getAsString();
2044
2045 switch (FnTy->getRefQualifier()) {
2046 case RQ_None:
2047 break;
2048
2049 case RQ_LValue:
2050 if (!Quals.empty())
2051 Quals += ' ';
2052 Quals += '&';
2053 break;
Douglas Gregorc938c162011-01-26 05:01:58 +00002054
Douglas Gregor683a81f2011-01-31 16:09:46 +00002055 case RQ_RValue:
2056 if (!Quals.empty())
2057 Quals += ' ';
2058 Quals += "&&";
2059 break;
Douglas Gregorc938c162011-01-26 05:01:58 +00002060 }
Douglas Gregor683a81f2011-01-31 16:09:46 +00002061
2062 Diag(D.getIdentifierLoc(),
2063 diag::ext_qualified_function_type_template_arg)
2064 << Quals;
2065 } else {
2066 if (FnTy->getTypeQuals() != 0) {
2067 if (D.isFunctionDeclarator())
2068 Diag(D.getIdentifierLoc(),
2069 diag::err_invalid_qualified_function_type);
2070 else
2071 Diag(D.getIdentifierLoc(),
2072 diag::err_invalid_qualified_typedef_function_type_use)
2073 << FreeFunction;
2074 }
2075
2076 if (FnTy->getRefQualifier()) {
2077 if (D.isFunctionDeclarator()) {
2078 SourceLocation Loc = D.getIdentifierLoc();
2079 for (unsigned I = 0, N = D.getNumTypeObjects(); I != N; ++I) {
2080 const DeclaratorChunk &Chunk = D.getTypeObject(N-I-1);
2081 if (Chunk.Kind == DeclaratorChunk::Function &&
2082 Chunk.Fun.hasRefQualifier()) {
2083 Loc = Chunk.Fun.getRefQualifierLoc();
2084 break;
2085 }
2086 }
2087
2088 Diag(Loc, diag::err_invalid_ref_qualifier_function_type)
2089 << (FnTy->getRefQualifier() == RQ_LValue)
2090 << FixItHint::CreateRemoval(Loc);
2091 } else {
2092 Diag(D.getIdentifierLoc(),
2093 diag::err_invalid_ref_qualifier_typedef_function_type_use)
2094 << FreeFunction
2095 << (FnTy->getRefQualifier() == RQ_LValue);
2096 }
2097 }
2098
2099 // Strip the cv-qualifiers and ref-qualifiers from the type.
2100 FunctionProtoType::ExtProtoInfo EPI = FnTy->getExtProtoInfo();
2101 EPI.TypeQuals = 0;
2102 EPI.RefQualifier = RQ_None;
2103
2104 T = Context.getFunctionType(FnTy->getResultType(),
2105 FnTy->arg_type_begin(),
2106 FnTy->getNumArgs(), EPI);
Douglas Gregorc938c162011-01-26 05:01:58 +00002107 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002108 }
2109 }
Mike Stump1eb44332009-09-09 15:08:12 +00002110
John McCall711c52b2011-01-05 12:14:39 +00002111 // Apply any undistributed attributes from the declarator.
2112 if (!T.isNull())
2113 if (AttributeList *attrs = D.getAttributes())
2114 processTypeAttrs(state, T, false, attrs);
2115
2116 // Diagnose any ignored type attributes.
2117 if (!T.isNull()) state.diagnoseIgnoredTypeAttrs(T);
2118
Peter Collingbourne148f1f72011-03-20 08:06:45 +00002119 // C++0x [dcl.constexpr]p9:
2120 // A constexpr specifier used in an object declaration declares the object
2121 // as const.
2122 if (D.getDeclSpec().isConstexprSpecified() && T->isObjectType()) {
Sebastian Redl73780122010-06-09 21:19:43 +00002123 T.addConst();
2124 }
2125
Douglas Gregora8bc8c92010-12-23 22:44:42 +00002126 // If there was an ellipsis in the declarator, the declaration declares a
2127 // parameter pack whose type may be a pack expansion type.
2128 if (D.hasEllipsis() && !T.isNull()) {
2129 // C++0x [dcl.fct]p13:
2130 // A declarator-id or abstract-declarator containing an ellipsis shall
2131 // only be used in a parameter-declaration. Such a parameter-declaration
2132 // is a parameter pack (14.5.3). [...]
2133 switch (D.getContext()) {
2134 case Declarator::PrototypeContext:
2135 // C++0x [dcl.fct]p13:
2136 // [...] When it is part of a parameter-declaration-clause, the
2137 // parameter pack is a function parameter pack (14.5.3). The type T
2138 // of the declarator-id of the function parameter pack shall contain
2139 // a template parameter pack; each template parameter pack in T is
2140 // expanded by the function parameter pack.
2141 //
2142 // We represent function parameter packs as function parameters whose
2143 // type is a pack expansion.
2144 if (!T->containsUnexpandedParameterPack()) {
2145 Diag(D.getEllipsisLoc(),
2146 diag::err_function_parameter_pack_without_parameter_packs)
2147 << T << D.getSourceRange();
2148 D.setEllipsisLoc(SourceLocation());
2149 } else {
Douglas Gregorcded4f62011-01-14 17:04:44 +00002150 T = Context.getPackExpansionType(T, llvm::Optional<unsigned>());
Douglas Gregora8bc8c92010-12-23 22:44:42 +00002151 }
2152 break;
2153
2154 case Declarator::TemplateParamContext:
2155 // C++0x [temp.param]p15:
2156 // If a template-parameter is a [...] is a parameter-declaration that
2157 // declares a parameter pack (8.3.5), then the template-parameter is a
2158 // template parameter pack (14.5.3).
2159 //
2160 // Note: core issue 778 clarifies that, if there are any unexpanded
2161 // parameter packs in the type of the non-type template parameter, then
2162 // it expands those parameter packs.
2163 if (T->containsUnexpandedParameterPack())
Douglas Gregorcded4f62011-01-14 17:04:44 +00002164 T = Context.getPackExpansionType(T, llvm::Optional<unsigned>());
Douglas Gregor10738d32010-12-23 23:51:58 +00002165 else if (!getLangOptions().CPlusPlus0x)
Douglas Gregor5ce5f522011-01-19 21:59:15 +00002166 Diag(D.getEllipsisLoc(), diag::ext_variadic_templates);
Douglas Gregora8bc8c92010-12-23 22:44:42 +00002167 break;
2168
2169 case Declarator::FileContext:
2170 case Declarator::KNRTypeListContext:
John McCallc05a94b2011-03-23 23:43:04 +00002171 case Declarator::ObjCPrototypeContext: // FIXME: special diagnostic here?
Douglas Gregora8bc8c92010-12-23 22:44:42 +00002172 case Declarator::TypeNameContext:
2173 case Declarator::MemberContext:
2174 case Declarator::BlockContext:
2175 case Declarator::ForContext:
2176 case Declarator::ConditionContext:
2177 case Declarator::CXXCatchContext:
2178 case Declarator::BlockLiteralContext:
Douglas Gregor683a81f2011-01-31 16:09:46 +00002179 case Declarator::TemplateTypeArgContext:
Douglas Gregora8bc8c92010-12-23 22:44:42 +00002180 // FIXME: We may want to allow parameter packs in block-literal contexts
2181 // in the future.
2182 Diag(D.getEllipsisLoc(), diag::err_ellipsis_in_declarator_not_parameter);
2183 D.setEllipsisLoc(SourceLocation());
2184 break;
2185 }
2186 }
Richard Smithe7397c62011-02-22 00:36:53 +00002187
John McCallbf1a0282010-06-04 23:28:52 +00002188 if (T.isNull())
2189 return Context.getNullTypeSourceInfo();
2190 else if (D.isInvalidType())
2191 return Context.getTrivialTypeSourceInfo(T);
2192 return GetTypeSourceInfoForDeclarator(D, T, ReturnTypeInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +00002193}
2194
John McCall14aa2172011-03-04 04:00:19 +00002195/// Map an AttributedType::Kind to an AttributeList::Kind.
2196static AttributeList::Kind getAttrListKind(AttributedType::Kind kind) {
2197 switch (kind) {
2198 case AttributedType::attr_address_space:
2199 return AttributeList::AT_address_space;
2200 case AttributedType::attr_regparm:
2201 return AttributeList::AT_regparm;
2202 case AttributedType::attr_vector_size:
2203 return AttributeList::AT_vector_size;
2204 case AttributedType::attr_neon_vector_type:
2205 return AttributeList::AT_neon_vector_type;
2206 case AttributedType::attr_neon_polyvector_type:
2207 return AttributeList::AT_neon_polyvector_type;
2208 case AttributedType::attr_objc_gc:
2209 return AttributeList::AT_objc_gc;
2210 case AttributedType::attr_noreturn:
2211 return AttributeList::AT_noreturn;
2212 case AttributedType::attr_cdecl:
2213 return AttributeList::AT_cdecl;
2214 case AttributedType::attr_fastcall:
2215 return AttributeList::AT_fastcall;
2216 case AttributedType::attr_stdcall:
2217 return AttributeList::AT_stdcall;
2218 case AttributedType::attr_thiscall:
2219 return AttributeList::AT_thiscall;
2220 case AttributedType::attr_pascal:
2221 return AttributeList::AT_pascal;
2222 }
2223 llvm_unreachable("unexpected attribute kind!");
2224 return AttributeList::Kind();
2225}
2226
2227static void fillAttributedTypeLoc(AttributedTypeLoc TL,
2228 const AttributeList *attrs) {
2229 AttributedType::Kind kind = TL.getAttrKind();
2230
2231 assert(attrs && "no type attributes in the expected location!");
2232 AttributeList::Kind parsedKind = getAttrListKind(kind);
2233 while (attrs->getKind() != parsedKind) {
2234 attrs = attrs->getNext();
2235 assert(attrs && "no matching attribute in expected location!");
2236 }
2237
2238 TL.setAttrNameLoc(attrs->getLoc());
2239 if (TL.hasAttrExprOperand())
2240 TL.setAttrExprOperand(attrs->getArg(0));
2241 else if (TL.hasAttrEnumOperand())
2242 TL.setAttrEnumOperandLoc(attrs->getParameterLoc());
2243
2244 // FIXME: preserve this information to here.
2245 if (TL.hasAttrOperand())
2246 TL.setAttrOperandParensRange(SourceRange());
2247}
2248
John McCall51bd8032009-10-18 01:05:36 +00002249namespace {
2250 class TypeSpecLocFiller : public TypeLocVisitor<TypeSpecLocFiller> {
Douglas Gregorc21c7e92011-01-25 19:13:18 +00002251 ASTContext &Context;
John McCall51bd8032009-10-18 01:05:36 +00002252 const DeclSpec &DS;
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00002253
John McCall51bd8032009-10-18 01:05:36 +00002254 public:
Douglas Gregorc21c7e92011-01-25 19:13:18 +00002255 TypeSpecLocFiller(ASTContext &Context, const DeclSpec &DS)
2256 : Context(Context), DS(DS) {}
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00002257
John McCall14aa2172011-03-04 04:00:19 +00002258 void VisitAttributedTypeLoc(AttributedTypeLoc TL) {
2259 fillAttributedTypeLoc(TL, DS.getAttributes().getList());
2260 Visit(TL.getModifiedLoc());
2261 }
John McCall51bd8032009-10-18 01:05:36 +00002262 void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
2263 Visit(TL.getUnqualifiedLoc());
2264 }
2265 void VisitTypedefTypeLoc(TypedefTypeLoc TL) {
2266 TL.setNameLoc(DS.getTypeSpecTypeLoc());
2267 }
2268 void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
2269 TL.setNameLoc(DS.getTypeSpecTypeLoc());
John McCallc12c5bb2010-05-15 11:32:37 +00002270 }
2271 void VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
2272 // Handle the base type, which might not have been written explicitly.
2273 if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) {
2274 TL.setHasBaseTypeAsWritten(false);
Douglas Gregorc21c7e92011-01-25 19:13:18 +00002275 TL.getBaseLoc().initialize(Context, SourceLocation());
John McCallc12c5bb2010-05-15 11:32:37 +00002276 } else {
2277 TL.setHasBaseTypeAsWritten(true);
2278 Visit(TL.getBaseLoc());
2279 }
Argyrios Kyrtzidiseb667592009-09-29 19:45:22 +00002280
John McCallc12c5bb2010-05-15 11:32:37 +00002281 // Protocol qualifiers.
John McCall54e14c42009-10-22 22:37:11 +00002282 if (DS.getProtocolQualifiers()) {
2283 assert(TL.getNumProtocols() > 0);
2284 assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
2285 TL.setLAngleLoc(DS.getProtocolLAngleLoc());
2286 TL.setRAngleLoc(DS.getSourceRange().getEnd());
2287 for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i)
2288 TL.setProtocolLoc(i, DS.getProtocolLocs()[i]);
2289 } else {
2290 assert(TL.getNumProtocols() == 0);
2291 TL.setLAngleLoc(SourceLocation());
2292 TL.setRAngleLoc(SourceLocation());
2293 }
2294 }
2295 void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
John McCall54e14c42009-10-22 22:37:11 +00002296 TL.setStarLoc(SourceLocation());
John McCallc12c5bb2010-05-15 11:32:37 +00002297 Visit(TL.getPointeeLoc());
John McCall51bd8032009-10-18 01:05:36 +00002298 }
John McCall833ca992009-10-29 08:12:44 +00002299 void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) {
John McCalla93c9342009-12-07 02:54:59 +00002300 TypeSourceInfo *TInfo = 0;
John McCallb3d87482010-08-24 05:47:05 +00002301 Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
John McCall833ca992009-10-29 08:12:44 +00002302
2303 // If we got no declarator info from previous Sema routines,
2304 // just fill with the typespec loc.
John McCalla93c9342009-12-07 02:54:59 +00002305 if (!TInfo) {
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002306 TL.initialize(Context, DS.getTypeSpecTypeNameLoc());
John McCall833ca992009-10-29 08:12:44 +00002307 return;
2308 }
2309
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002310 TypeLoc OldTL = TInfo->getTypeLoc();
2311 if (TInfo->getType()->getAs<ElaboratedType>()) {
2312 ElaboratedTypeLoc ElabTL = cast<ElaboratedTypeLoc>(OldTL);
2313 TemplateSpecializationTypeLoc NamedTL =
2314 cast<TemplateSpecializationTypeLoc>(ElabTL.getNamedTypeLoc());
2315 TL.copy(NamedTL);
2316 }
2317 else
2318 TL.copy(cast<TemplateSpecializationTypeLoc>(OldTL));
John McCall833ca992009-10-29 08:12:44 +00002319 }
John McCallcfb708c2010-01-13 20:03:27 +00002320 void VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
2321 assert(DS.getTypeSpecType() == DeclSpec::TST_typeofExpr);
2322 TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
2323 TL.setParensRange(DS.getTypeofParensRange());
2324 }
2325 void VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
2326 assert(DS.getTypeSpecType() == DeclSpec::TST_typeofType);
2327 TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
2328 TL.setParensRange(DS.getTypeofParensRange());
John McCallb3d87482010-08-24 05:47:05 +00002329 assert(DS.getRepAsType());
John McCallcfb708c2010-01-13 20:03:27 +00002330 TypeSourceInfo *TInfo = 0;
John McCallb3d87482010-08-24 05:47:05 +00002331 Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
John McCallcfb708c2010-01-13 20:03:27 +00002332 TL.setUnderlyingTInfo(TInfo);
2333 }
Douglas Gregorddf889a2010-01-18 18:04:31 +00002334 void VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
2335 // By default, use the source location of the type specifier.
2336 TL.setBuiltinLoc(DS.getTypeSpecTypeLoc());
2337 if (TL.needsExtraLocalData()) {
2338 // Set info for the written builtin specifiers.
2339 TL.getWrittenBuiltinSpecs() = DS.getWrittenBuiltinSpecs();
2340 // Try to have a meaningful source location.
2341 if (TL.getWrittenSignSpec() != TSS_unspecified)
2342 // Sign spec loc overrides the others (e.g., 'unsigned long').
2343 TL.setBuiltinLoc(DS.getTypeSpecSignLoc());
2344 else if (TL.getWrittenWidthSpec() != TSW_unspecified)
2345 // Width spec loc overrides type spec loc (e.g., 'short int').
2346 TL.setBuiltinLoc(DS.getTypeSpecWidthLoc());
2347 }
2348 }
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002349 void VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
2350 ElaboratedTypeKeyword Keyword
2351 = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
Nico Weber253e80b2010-11-22 10:30:56 +00002352 if (DS.getTypeSpecType() == TST_typename) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002353 TypeSourceInfo *TInfo = 0;
John McCallb3d87482010-08-24 05:47:05 +00002354 Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002355 if (TInfo) {
2356 TL.copy(cast<ElaboratedTypeLoc>(TInfo->getTypeLoc()));
2357 return;
2358 }
2359 }
2360 TL.setKeywordLoc(Keyword != ETK_None
2361 ? DS.getTypeSpecTypeLoc()
2362 : SourceLocation());
2363 const CXXScopeSpec& SS = DS.getTypeSpecScope();
Douglas Gregor9e876872011-03-01 18:12:44 +00002364 TL.setQualifierLoc(SS.getWithLocInContext(Context));
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002365 Visit(TL.getNextTypeLoc().getUnqualifiedLoc());
2366 }
2367 void VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
2368 ElaboratedTypeKeyword Keyword
2369 = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
Nico Weber253e80b2010-11-22 10:30:56 +00002370 if (DS.getTypeSpecType() == TST_typename) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002371 TypeSourceInfo *TInfo = 0;
John McCallb3d87482010-08-24 05:47:05 +00002372 Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002373 if (TInfo) {
2374 TL.copy(cast<DependentNameTypeLoc>(TInfo->getTypeLoc()));
2375 return;
2376 }
2377 }
2378 TL.setKeywordLoc(Keyword != ETK_None
2379 ? DS.getTypeSpecTypeLoc()
2380 : SourceLocation());
2381 const CXXScopeSpec& SS = DS.getTypeSpecScope();
Douglas Gregor2494dd02011-03-01 01:34:45 +00002382 TL.setQualifierLoc(SS.getWithLocInContext(Context));
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002383 TL.setNameLoc(DS.getTypeSpecTypeNameLoc());
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002384 }
John McCall33500952010-06-11 00:33:02 +00002385 void VisitDependentTemplateSpecializationTypeLoc(
2386 DependentTemplateSpecializationTypeLoc TL) {
2387 ElaboratedTypeKeyword Keyword
2388 = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
2389 if (Keyword == ETK_Typename) {
2390 TypeSourceInfo *TInfo = 0;
John McCallb3d87482010-08-24 05:47:05 +00002391 Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
John McCall33500952010-06-11 00:33:02 +00002392 if (TInfo) {
2393 TL.copy(cast<DependentTemplateSpecializationTypeLoc>(
2394 TInfo->getTypeLoc()));
2395 return;
2396 }
2397 }
Douglas Gregorc21c7e92011-01-25 19:13:18 +00002398 TL.initializeLocal(Context, SourceLocation());
John McCall33500952010-06-11 00:33:02 +00002399 TL.setKeywordLoc(Keyword != ETK_None
2400 ? DS.getTypeSpecTypeLoc()
2401 : SourceLocation());
2402 const CXXScopeSpec& SS = DS.getTypeSpecScope();
Douglas Gregor94fdffa2011-03-01 20:11:18 +00002403 TL.setQualifierLoc(SS.getWithLocInContext(Context));
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002404 TL.setNameLoc(DS.getTypeSpecTypeNameLoc());
2405 }
2406 void VisitTagTypeLoc(TagTypeLoc TL) {
2407 TL.setNameLoc(DS.getTypeSpecTypeNameLoc());
John McCall33500952010-06-11 00:33:02 +00002408 }
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002409
John McCall51bd8032009-10-18 01:05:36 +00002410 void VisitTypeLoc(TypeLoc TL) {
2411 // FIXME: add other typespec types and change this to an assert.
Douglas Gregorc21c7e92011-01-25 19:13:18 +00002412 TL.initialize(Context, DS.getTypeSpecTypeLoc());
John McCall51bd8032009-10-18 01:05:36 +00002413 }
2414 };
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00002415
John McCall51bd8032009-10-18 01:05:36 +00002416 class DeclaratorLocFiller : public TypeLocVisitor<DeclaratorLocFiller> {
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00002417 ASTContext &Context;
John McCall51bd8032009-10-18 01:05:36 +00002418 const DeclaratorChunk &Chunk;
2419
2420 public:
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00002421 DeclaratorLocFiller(ASTContext &Context, const DeclaratorChunk &Chunk)
2422 : Context(Context), Chunk(Chunk) {}
John McCall51bd8032009-10-18 01:05:36 +00002423
2424 void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002425 llvm_unreachable("qualified type locs not expected here!");
John McCall51bd8032009-10-18 01:05:36 +00002426 }
2427
2428 void VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
2429 assert(Chunk.Kind == DeclaratorChunk::BlockPointer);
2430 TL.setCaretLoc(Chunk.Loc);
2431 }
2432 void VisitPointerTypeLoc(PointerTypeLoc TL) {
2433 assert(Chunk.Kind == DeclaratorChunk::Pointer);
2434 TL.setStarLoc(Chunk.Loc);
2435 }
2436 void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
2437 assert(Chunk.Kind == DeclaratorChunk::Pointer);
2438 TL.setStarLoc(Chunk.Loc);
2439 }
2440 void VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
2441 assert(Chunk.Kind == DeclaratorChunk::MemberPointer);
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00002442 const CXXScopeSpec& SS = Chunk.Mem.Scope();
2443 NestedNameSpecifierLoc NNSLoc = SS.getWithLocInContext(Context);
2444
2445 const Type* ClsTy = TL.getClass();
2446 QualType ClsQT = QualType(ClsTy, 0);
2447 TypeSourceInfo *ClsTInfo = Context.CreateTypeSourceInfo(ClsQT, 0);
2448 // Now copy source location info into the type loc component.
2449 TypeLoc ClsTL = ClsTInfo->getTypeLoc();
2450 switch (NNSLoc.getNestedNameSpecifier()->getKind()) {
2451 case NestedNameSpecifier::Identifier:
2452 assert(isa<DependentNameType>(ClsTy) && "Unexpected TypeLoc");
2453 {
Abramo Bagnarafd9c42e2011-03-06 22:48:24 +00002454 DependentNameTypeLoc DNTLoc = cast<DependentNameTypeLoc>(ClsTL);
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00002455 DNTLoc.setKeywordLoc(SourceLocation());
2456 DNTLoc.setQualifierLoc(NNSLoc.getPrefix());
2457 DNTLoc.setNameLoc(NNSLoc.getLocalBeginLoc());
2458 }
2459 break;
2460
2461 case NestedNameSpecifier::TypeSpec:
2462 case NestedNameSpecifier::TypeSpecWithTemplate:
2463 if (isa<ElaboratedType>(ClsTy)) {
2464 ElaboratedTypeLoc ETLoc = *cast<ElaboratedTypeLoc>(&ClsTL);
2465 ETLoc.setKeywordLoc(SourceLocation());
2466 ETLoc.setQualifierLoc(NNSLoc.getPrefix());
2467 TypeLoc NamedTL = ETLoc.getNamedTypeLoc();
2468 NamedTL.initializeFullCopy(NNSLoc.getTypeLoc());
2469 } else {
2470 ClsTL.initializeFullCopy(NNSLoc.getTypeLoc());
2471 }
2472 break;
2473
2474 case NestedNameSpecifier::Namespace:
2475 case NestedNameSpecifier::NamespaceAlias:
2476 case NestedNameSpecifier::Global:
2477 llvm_unreachable("Nested-name-specifier must name a type");
2478 break;
2479 }
2480
2481 // Finally fill in MemberPointerLocInfo fields.
John McCall51bd8032009-10-18 01:05:36 +00002482 TL.setStarLoc(Chunk.Loc);
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00002483 TL.setClassTInfo(ClsTInfo);
John McCall51bd8032009-10-18 01:05:36 +00002484 }
2485 void VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
2486 assert(Chunk.Kind == DeclaratorChunk::Reference);
John McCall54e14c42009-10-22 22:37:11 +00002487 // 'Amp' is misleading: this might have been originally
2488 /// spelled with AmpAmp.
John McCall51bd8032009-10-18 01:05:36 +00002489 TL.setAmpLoc(Chunk.Loc);
2490 }
2491 void VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
2492 assert(Chunk.Kind == DeclaratorChunk::Reference);
2493 assert(!Chunk.Ref.LValueRef);
2494 TL.setAmpAmpLoc(Chunk.Loc);
2495 }
2496 void VisitArrayTypeLoc(ArrayTypeLoc TL) {
2497 assert(Chunk.Kind == DeclaratorChunk::Array);
2498 TL.setLBracketLoc(Chunk.Loc);
2499 TL.setRBracketLoc(Chunk.EndLoc);
2500 TL.setSizeExpr(static_cast<Expr*>(Chunk.Arr.NumElts));
2501 }
2502 void VisitFunctionTypeLoc(FunctionTypeLoc TL) {
2503 assert(Chunk.Kind == DeclaratorChunk::Function);
Abramo Bagnara796aa442011-03-12 11:17:06 +00002504 TL.setLocalRangeBegin(Chunk.Loc);
2505 TL.setLocalRangeEnd(Chunk.EndLoc);
Douglas Gregordab60ad2010-10-01 18:44:50 +00002506 TL.setTrailingReturn(!!Chunk.Fun.TrailingReturnType);
John McCall51bd8032009-10-18 01:05:36 +00002507
2508 const DeclaratorChunk::FunctionTypeInfo &FTI = Chunk.Fun;
John McCall54e14c42009-10-22 22:37:11 +00002509 for (unsigned i = 0, e = TL.getNumArgs(), tpi = 0; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002510 ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param);
John McCall54e14c42009-10-22 22:37:11 +00002511 TL.setArg(tpi++, Param);
John McCall51bd8032009-10-18 01:05:36 +00002512 }
2513 // FIXME: exception specs
2514 }
Abramo Bagnara075f8f12010-12-10 16:29:40 +00002515 void VisitParenTypeLoc(ParenTypeLoc TL) {
2516 assert(Chunk.Kind == DeclaratorChunk::Paren);
2517 TL.setLParenLoc(Chunk.Loc);
2518 TL.setRParenLoc(Chunk.EndLoc);
2519 }
John McCall51bd8032009-10-18 01:05:36 +00002520
2521 void VisitTypeLoc(TypeLoc TL) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002522 llvm_unreachable("unsupported TypeLoc kind in declarator!");
John McCall51bd8032009-10-18 01:05:36 +00002523 }
2524 };
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00002525}
2526
John McCalla93c9342009-12-07 02:54:59 +00002527/// \brief Create and instantiate a TypeSourceInfo with type source information.
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00002528///
2529/// \param T QualType referring to the type as written in source code.
Douglas Gregor05baacb2010-04-12 23:19:01 +00002530///
2531/// \param ReturnTypeInfo For declarators whose return type does not show
2532/// up in the normal place in the declaration specifiers (such as a C++
2533/// conversion function), this pointer will refer to a type source information
2534/// for that return type.
John McCalla93c9342009-12-07 02:54:59 +00002535TypeSourceInfo *
Douglas Gregor05baacb2010-04-12 23:19:01 +00002536Sema::GetTypeSourceInfoForDeclarator(Declarator &D, QualType T,
2537 TypeSourceInfo *ReturnTypeInfo) {
John McCalla93c9342009-12-07 02:54:59 +00002538 TypeSourceInfo *TInfo = Context.CreateTypeSourceInfo(T);
2539 UnqualTypeLoc CurrTL = TInfo->getTypeLoc().getUnqualifiedLoc();
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00002540
Douglas Gregora8bc8c92010-12-23 22:44:42 +00002541 // Handle parameter packs whose type is a pack expansion.
2542 if (isa<PackExpansionType>(T)) {
2543 cast<PackExpansionTypeLoc>(CurrTL).setEllipsisLoc(D.getEllipsisLoc());
2544 CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
2545 }
2546
Sebastian Redl8ce35b02009-10-25 21:45:37 +00002547 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
John McCall14aa2172011-03-04 04:00:19 +00002548 while (isa<AttributedTypeLoc>(CurrTL)) {
2549 AttributedTypeLoc TL = cast<AttributedTypeLoc>(CurrTL);
2550 fillAttributedTypeLoc(TL, D.getTypeObject(i).getAttrs());
2551 CurrTL = TL.getNextTypeLoc().getUnqualifiedLoc();
2552 }
2553
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +00002554 DeclaratorLocFiller(Context, D.getTypeObject(i)).Visit(CurrTL);
John McCall51bd8032009-10-18 01:05:36 +00002555 CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00002556 }
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00002557
John McCallb3d87482010-08-24 05:47:05 +00002558 // If we have different source information for the return type, use
2559 // that. This really only applies to C++ conversion functions.
2560 if (ReturnTypeInfo) {
Douglas Gregor05baacb2010-04-12 23:19:01 +00002561 TypeLoc TL = ReturnTypeInfo->getTypeLoc();
2562 assert(TL.getFullDataSize() == CurrTL.getFullDataSize());
2563 memcpy(CurrTL.getOpaqueData(), TL.getOpaqueData(), TL.getFullDataSize());
John McCallb3d87482010-08-24 05:47:05 +00002564 } else {
Douglas Gregorc21c7e92011-01-25 19:13:18 +00002565 TypeSpecLocFiller(Context, D.getDeclSpec()).Visit(CurrTL);
Douglas Gregor05baacb2010-04-12 23:19:01 +00002566 }
2567
John McCalla93c9342009-12-07 02:54:59 +00002568 return TInfo;
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00002569}
2570
John McCalla93c9342009-12-07 02:54:59 +00002571/// \brief Create a LocInfoType to hold the given QualType and TypeSourceInfo.
John McCallb3d87482010-08-24 05:47:05 +00002572ParsedType Sema::CreateParsedType(QualType T, TypeSourceInfo *TInfo) {
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00002573 // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser
2574 // and Sema during declaration parsing. Try deallocating/caching them when
2575 // it's appropriate, instead of allocating them and keeping them around.
Douglas Gregoreb0eb492010-12-10 08:12:03 +00002576 LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType),
2577 TypeAlignment);
John McCalla93c9342009-12-07 02:54:59 +00002578 new (LocT) LocInfoType(T, TInfo);
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00002579 assert(LocT->getTypeClass() != T->getTypeClass() &&
2580 "LocInfoType's TypeClass conflicts with an existing Type class");
John McCallb3d87482010-08-24 05:47:05 +00002581 return ParsedType::make(QualType(LocT, 0));
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00002582}
2583
2584void LocInfoType::getAsStringInternal(std::string &Str,
2585 const PrintingPolicy &Policy) const {
Argyrios Kyrtzidis35d44e52009-08-19 01:46:06 +00002586 assert(false && "LocInfoType leaked into the type system; an opaque TypeTy*"
2587 " was used directly instead of getting the QualType through"
2588 " GetTypeFromParser");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00002589}
2590
John McCallf312b1e2010-08-26 23:41:50 +00002591TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002592 // C99 6.7.6: Type names have no identifier. This is already validated by
2593 // the parser.
2594 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00002595
Douglas Gregor402abb52009-05-28 23:31:59 +00002596 TagDecl *OwnedTag = 0;
John McCallbf1a0282010-06-04 23:28:52 +00002597 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S, &OwnedTag);
2598 QualType T = TInfo->getType();
Chris Lattner5153ee62009-04-25 08:47:54 +00002599 if (D.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +00002600 return true;
Steve Naroff5912a352007-08-28 20:14:24 +00002601
Douglas Gregor402abb52009-05-28 23:31:59 +00002602 if (getLangOptions().CPlusPlus) {
2603 // Check that there are no default arguments (C++ only).
Douglas Gregor6d6eb572008-05-07 04:49:29 +00002604 CheckExtraCXXDefaultArguments(D);
2605
Douglas Gregor402abb52009-05-28 23:31:59 +00002606 // C++0x [dcl.type]p3:
2607 // A type-specifier-seq shall not define a class or enumeration
2608 // unless it appears in the type-id of an alias-declaration
2609 // (7.1.3).
2610 if (OwnedTag && OwnedTag->isDefinition())
2611 Diag(OwnedTag->getLocation(), diag::err_type_defined_in_type_specifier)
2612 << Context.getTypeDeclType(OwnedTag);
2613 }
2614
John McCallb3d87482010-08-24 05:47:05 +00002615 return CreateParsedType(T, TInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +00002616}
2617
Chris Lattnerc9b346d2008-06-29 00:50:08 +00002618
2619
2620//===----------------------------------------------------------------------===//
2621// Type Attribute Processing
2622//===----------------------------------------------------------------------===//
2623
2624/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
2625/// specified type. The attribute contains 1 argument, the id of the address
2626/// space for the type.
Mike Stump1eb44332009-09-09 15:08:12 +00002627static void HandleAddressSpaceTypeAttribute(QualType &Type,
Chris Lattnerc9b346d2008-06-29 00:50:08 +00002628 const AttributeList &Attr, Sema &S){
John McCall0953e762009-09-24 19:53:00 +00002629
Chris Lattnerc9b346d2008-06-29 00:50:08 +00002630 // If this type is already address space qualified, reject it.
2631 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
2632 // for two or more different address spaces."
2633 if (Type.getAddressSpace()) {
2634 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
Abramo Bagnarae215f722010-04-30 13:10:51 +00002635 Attr.setInvalid();
Chris Lattnerc9b346d2008-06-29 00:50:08 +00002636 return;
2637 }
Mike Stump1eb44332009-09-09 15:08:12 +00002638
Chris Lattnerc9b346d2008-06-29 00:50:08 +00002639 // Check the attribute arguments.
2640 if (Attr.getNumArgs() != 1) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +00002641 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002642 Attr.setInvalid();
Chris Lattnerc9b346d2008-06-29 00:50:08 +00002643 return;
2644 }
2645 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
2646 llvm::APSInt addrSpace(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002647 if (ASArgExpr->isTypeDependent() || ASArgExpr->isValueDependent() ||
2648 !ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00002649 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
2650 << ASArgExpr->getSourceRange();
Abramo Bagnarae215f722010-04-30 13:10:51 +00002651 Attr.setInvalid();
Chris Lattnerc9b346d2008-06-29 00:50:08 +00002652 return;
2653 }
2654
John McCallefadb772009-07-28 06:52:18 +00002655 // Bounds checking.
2656 if (addrSpace.isSigned()) {
2657 if (addrSpace.isNegative()) {
2658 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative)
2659 << ASArgExpr->getSourceRange();
Abramo Bagnarae215f722010-04-30 13:10:51 +00002660 Attr.setInvalid();
John McCallefadb772009-07-28 06:52:18 +00002661 return;
2662 }
2663 addrSpace.setIsSigned(false);
2664 }
2665 llvm::APSInt max(addrSpace.getBitWidth());
John McCall0953e762009-09-24 19:53:00 +00002666 max = Qualifiers::MaxAddressSpace;
John McCallefadb772009-07-28 06:52:18 +00002667 if (addrSpace > max) {
2668 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high)
John McCall0953e762009-09-24 19:53:00 +00002669 << Qualifiers::MaxAddressSpace << ASArgExpr->getSourceRange();
Abramo Bagnarae215f722010-04-30 13:10:51 +00002670 Attr.setInvalid();
John McCallefadb772009-07-28 06:52:18 +00002671 return;
2672 }
2673
Mike Stump1eb44332009-09-09 15:08:12 +00002674 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00002675 Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
Chris Lattnerc9b346d2008-06-29 00:50:08 +00002676}
2677
John McCall711c52b2011-01-05 12:14:39 +00002678/// handleObjCGCTypeAttr - Process the __attribute__((objc_gc)) type
2679/// attribute on the specified type. Returns true to indicate that
2680/// the attribute was handled, false to indicate that the type does
2681/// not permit the attribute.
2682static bool handleObjCGCTypeAttr(TypeProcessingState &state,
2683 AttributeList &attr,
2684 QualType &type) {
2685 Sema &S = state.getSema();
2686
2687 // Delay if this isn't some kind of pointer.
2688 if (!type->isPointerType() &&
2689 !type->isObjCObjectPointerType() &&
2690 !type->isBlockPointerType())
2691 return false;
2692
2693 if (type.getObjCGCAttr() != Qualifiers::GCNone) {
2694 S.Diag(attr.getLoc(), diag::err_attribute_multiple_objc_gc);
2695 attr.setInvalid();
2696 return true;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00002697 }
Mike Stump1eb44332009-09-09 15:08:12 +00002698
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00002699 // Check the attribute arguments.
John McCall711c52b2011-01-05 12:14:39 +00002700 if (!attr.getParameterName()) {
2701 S.Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
Fariborz Jahanianba372b82009-02-18 17:52:36 +00002702 << "objc_gc" << 1;
John McCall711c52b2011-01-05 12:14:39 +00002703 attr.setInvalid();
2704 return true;
Fariborz Jahanianba372b82009-02-18 17:52:36 +00002705 }
John McCall0953e762009-09-24 19:53:00 +00002706 Qualifiers::GC GCAttr;
John McCall711c52b2011-01-05 12:14:39 +00002707 if (attr.getNumArgs() != 0) {
2708 S.Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2709 attr.setInvalid();
2710 return true;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00002711 }
John McCall711c52b2011-01-05 12:14:39 +00002712 if (attr.getParameterName()->isStr("weak"))
John McCall0953e762009-09-24 19:53:00 +00002713 GCAttr = Qualifiers::Weak;
John McCall711c52b2011-01-05 12:14:39 +00002714 else if (attr.getParameterName()->isStr("strong"))
John McCall0953e762009-09-24 19:53:00 +00002715 GCAttr = Qualifiers::Strong;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00002716 else {
John McCall711c52b2011-01-05 12:14:39 +00002717 S.Diag(attr.getLoc(), diag::warn_attribute_type_not_supported)
2718 << "objc_gc" << attr.getParameterName();
2719 attr.setInvalid();
2720 return true;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00002721 }
Mike Stump1eb44332009-09-09 15:08:12 +00002722
John McCall14aa2172011-03-04 04:00:19 +00002723 QualType origType = type;
2724 type = S.Context.getObjCGCQualType(origType, GCAttr);
2725
2726 // Make an attributed type to preserve the source information.
2727 if (attr.getLoc().isValid())
2728 type = S.Context.getAttributedType(AttributedType::attr_objc_gc,
2729 origType, type);
2730
John McCall711c52b2011-01-05 12:14:39 +00002731 return true;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00002732}
2733
John McCalle6a365d2010-12-19 02:44:49 +00002734namespace {
2735 /// A helper class to unwrap a type down to a function for the
2736 /// purposes of applying attributes there.
2737 ///
2738 /// Use:
2739 /// FunctionTypeUnwrapper unwrapped(SemaRef, T);
2740 /// if (unwrapped.isFunctionType()) {
2741 /// const FunctionType *fn = unwrapped.get();
2742 /// // change fn somehow
2743 /// T = unwrapped.wrap(fn);
2744 /// }
2745 struct FunctionTypeUnwrapper {
2746 enum WrapKind {
2747 Desugar,
2748 Parens,
2749 Pointer,
2750 BlockPointer,
2751 Reference,
2752 MemberPointer
2753 };
2754
2755 QualType Original;
2756 const FunctionType *Fn;
2757 llvm::SmallVector<unsigned char /*WrapKind*/, 8> Stack;
2758
2759 FunctionTypeUnwrapper(Sema &S, QualType T) : Original(T) {
2760 while (true) {
2761 const Type *Ty = T.getTypePtr();
2762 if (isa<FunctionType>(Ty)) {
2763 Fn = cast<FunctionType>(Ty);
2764 return;
2765 } else if (isa<ParenType>(Ty)) {
2766 T = cast<ParenType>(Ty)->getInnerType();
2767 Stack.push_back(Parens);
2768 } else if (isa<PointerType>(Ty)) {
2769 T = cast<PointerType>(Ty)->getPointeeType();
2770 Stack.push_back(Pointer);
2771 } else if (isa<BlockPointerType>(Ty)) {
2772 T = cast<BlockPointerType>(Ty)->getPointeeType();
2773 Stack.push_back(BlockPointer);
2774 } else if (isa<MemberPointerType>(Ty)) {
2775 T = cast<MemberPointerType>(Ty)->getPointeeType();
2776 Stack.push_back(MemberPointer);
2777 } else if (isa<ReferenceType>(Ty)) {
2778 T = cast<ReferenceType>(Ty)->getPointeeType();
2779 Stack.push_back(Reference);
2780 } else {
2781 const Type *DTy = Ty->getUnqualifiedDesugaredType();
2782 if (Ty == DTy) {
2783 Fn = 0;
2784 return;
2785 }
2786
2787 T = QualType(DTy, 0);
2788 Stack.push_back(Desugar);
2789 }
2790 }
2791 }
2792
2793 bool isFunctionType() const { return (Fn != 0); }
2794 const FunctionType *get() const { return Fn; }
2795
2796 QualType wrap(Sema &S, const FunctionType *New) {
2797 // If T wasn't modified from the unwrapped type, do nothing.
2798 if (New == get()) return Original;
2799
2800 Fn = New;
2801 return wrap(S.Context, Original, 0);
2802 }
2803
2804 private:
2805 QualType wrap(ASTContext &C, QualType Old, unsigned I) {
2806 if (I == Stack.size())
2807 return C.getQualifiedType(Fn, Old.getQualifiers());
2808
2809 // Build up the inner type, applying the qualifiers from the old
2810 // type to the new type.
2811 SplitQualType SplitOld = Old.split();
2812
2813 // As a special case, tail-recurse if there are no qualifiers.
2814 if (SplitOld.second.empty())
2815 return wrap(C, SplitOld.first, I);
2816 return C.getQualifiedType(wrap(C, SplitOld.first, I), SplitOld.second);
2817 }
2818
2819 QualType wrap(ASTContext &C, const Type *Old, unsigned I) {
2820 if (I == Stack.size()) return QualType(Fn, 0);
2821
2822 switch (static_cast<WrapKind>(Stack[I++])) {
2823 case Desugar:
2824 // This is the point at which we potentially lose source
2825 // information.
2826 return wrap(C, Old->getUnqualifiedDesugaredType(), I);
2827
2828 case Parens: {
2829 QualType New = wrap(C, cast<ParenType>(Old)->getInnerType(), I);
2830 return C.getParenType(New);
2831 }
2832
2833 case Pointer: {
2834 QualType New = wrap(C, cast<PointerType>(Old)->getPointeeType(), I);
2835 return C.getPointerType(New);
2836 }
2837
2838 case BlockPointer: {
2839 QualType New = wrap(C, cast<BlockPointerType>(Old)->getPointeeType(),I);
2840 return C.getBlockPointerType(New);
2841 }
2842
2843 case MemberPointer: {
2844 const MemberPointerType *OldMPT = cast<MemberPointerType>(Old);
2845 QualType New = wrap(C, OldMPT->getPointeeType(), I);
2846 return C.getMemberPointerType(New, OldMPT->getClass());
2847 }
2848
2849 case Reference: {
2850 const ReferenceType *OldRef = cast<ReferenceType>(Old);
2851 QualType New = wrap(C, OldRef->getPointeeType(), I);
2852 if (isa<LValueReferenceType>(OldRef))
2853 return C.getLValueReferenceType(New, OldRef->isSpelledAsLValue());
2854 else
2855 return C.getRValueReferenceType(New);
2856 }
2857 }
2858
2859 llvm_unreachable("unknown wrapping kind");
2860 return QualType();
2861 }
2862 };
2863}
2864
John McCall711c52b2011-01-05 12:14:39 +00002865/// Process an individual function attribute. Returns true to
2866/// indicate that the attribute was handled, false if it wasn't.
2867static bool handleFunctionTypeAttr(TypeProcessingState &state,
2868 AttributeList &attr,
2869 QualType &type) {
2870 Sema &S = state.getSema();
John McCalle6a365d2010-12-19 02:44:49 +00002871
John McCall711c52b2011-01-05 12:14:39 +00002872 FunctionTypeUnwrapper unwrapped(S, type);
Mike Stump24556362009-07-25 21:26:53 +00002873
John McCall711c52b2011-01-05 12:14:39 +00002874 if (attr.getKind() == AttributeList::AT_noreturn) {
2875 if (S.CheckNoReturnAttr(attr))
John McCall04a67a62010-02-05 21:31:56 +00002876 return true;
John McCalle6a365d2010-12-19 02:44:49 +00002877
John McCall711c52b2011-01-05 12:14:39 +00002878 // Delay if this is not a function type.
2879 if (!unwrapped.isFunctionType())
2880 return false;
2881
John McCall04a67a62010-02-05 21:31:56 +00002882 // Otherwise we can process right away.
John McCall711c52b2011-01-05 12:14:39 +00002883 FunctionType::ExtInfo EI = unwrapped.get()->getExtInfo().withNoReturn(true);
2884 type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
2885 return true;
John McCall04a67a62010-02-05 21:31:56 +00002886 }
Mike Stump24556362009-07-25 21:26:53 +00002887
John McCall711c52b2011-01-05 12:14:39 +00002888 if (attr.getKind() == AttributeList::AT_regparm) {
2889 unsigned value;
2890 if (S.CheckRegparmAttr(attr, value))
Rafael Espindola425ef722010-03-30 22:15:11 +00002891 return true;
2892
John McCall711c52b2011-01-05 12:14:39 +00002893 // Delay if this is not a function type.
2894 if (!unwrapped.isFunctionType())
Rafael Espindola425ef722010-03-30 22:15:11 +00002895 return false;
2896
Argyrios Kyrtzidisce955662011-01-25 23:16:40 +00002897 // Diagnose regparm with fastcall.
2898 const FunctionType *fn = unwrapped.get();
2899 CallingConv CC = fn->getCallConv();
2900 if (CC == CC_X86FastCall) {
2901 S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
2902 << FunctionType::getNameForCallConv(CC)
2903 << "regparm";
2904 attr.setInvalid();
2905 return true;
2906 }
2907
John McCalle6a365d2010-12-19 02:44:49 +00002908 FunctionType::ExtInfo EI =
John McCall711c52b2011-01-05 12:14:39 +00002909 unwrapped.get()->getExtInfo().withRegParm(value);
2910 type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
2911 return true;
Rafael Espindola425ef722010-03-30 22:15:11 +00002912 }
2913
John McCall04a67a62010-02-05 21:31:56 +00002914 // Otherwise, a calling convention.
John McCall711c52b2011-01-05 12:14:39 +00002915 CallingConv CC;
2916 if (S.CheckCallingConvAttr(attr, CC))
2917 return true;
John McCallf82b4e82010-02-04 05:44:44 +00002918
John McCall04a67a62010-02-05 21:31:56 +00002919 // Delay if the type didn't work out to a function.
John McCall711c52b2011-01-05 12:14:39 +00002920 if (!unwrapped.isFunctionType()) return false;
John McCall04a67a62010-02-05 21:31:56 +00002921
John McCall711c52b2011-01-05 12:14:39 +00002922 const FunctionType *fn = unwrapped.get();
2923 CallingConv CCOld = fn->getCallConv();
Charles Davis064f7db2010-02-23 06:13:55 +00002924 if (S.Context.getCanonicalCallConv(CC) ==
Abramo Bagnarae215f722010-04-30 13:10:51 +00002925 S.Context.getCanonicalCallConv(CCOld)) {
Argyrios Kyrtzidisce955662011-01-25 23:16:40 +00002926 FunctionType::ExtInfo EI= unwrapped.get()->getExtInfo().withCallingConv(CC);
2927 type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
John McCall711c52b2011-01-05 12:14:39 +00002928 return true;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002929 }
John McCall04a67a62010-02-05 21:31:56 +00002930
2931 if (CCOld != CC_Default) {
2932 // Should we diagnose reapplications of the same convention?
John McCall711c52b2011-01-05 12:14:39 +00002933 S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
John McCall04a67a62010-02-05 21:31:56 +00002934 << FunctionType::getNameForCallConv(CC)
2935 << FunctionType::getNameForCallConv(CCOld);
John McCall711c52b2011-01-05 12:14:39 +00002936 attr.setInvalid();
2937 return true;
John McCall04a67a62010-02-05 21:31:56 +00002938 }
2939
2940 // Diagnose the use of X86 fastcall on varargs or unprototyped functions.
2941 if (CC == CC_X86FastCall) {
John McCall711c52b2011-01-05 12:14:39 +00002942 if (isa<FunctionNoProtoType>(fn)) {
2943 S.Diag(attr.getLoc(), diag::err_cconv_knr)
John McCall04a67a62010-02-05 21:31:56 +00002944 << FunctionType::getNameForCallConv(CC);
John McCall711c52b2011-01-05 12:14:39 +00002945 attr.setInvalid();
2946 return true;
John McCall04a67a62010-02-05 21:31:56 +00002947 }
2948
John McCall711c52b2011-01-05 12:14:39 +00002949 const FunctionProtoType *FnP = cast<FunctionProtoType>(fn);
John McCall04a67a62010-02-05 21:31:56 +00002950 if (FnP->isVariadic()) {
John McCall711c52b2011-01-05 12:14:39 +00002951 S.Diag(attr.getLoc(), diag::err_cconv_varargs)
John McCall04a67a62010-02-05 21:31:56 +00002952 << FunctionType::getNameForCallConv(CC);
John McCall711c52b2011-01-05 12:14:39 +00002953 attr.setInvalid();
2954 return true;
John McCall04a67a62010-02-05 21:31:56 +00002955 }
Argyrios Kyrtzidisce955662011-01-25 23:16:40 +00002956
2957 // Also diagnose fastcall with regparm.
2958 if (fn->getRegParmType()) {
2959 S.Diag(attr.getLoc(), diag::err_attributes_are_not_compatible)
2960 << "regparm"
2961 << FunctionType::getNameForCallConv(CC);
2962 attr.setInvalid();
2963 return true;
2964 }
John McCall04a67a62010-02-05 21:31:56 +00002965 }
2966
John McCall711c52b2011-01-05 12:14:39 +00002967 FunctionType::ExtInfo EI = unwrapped.get()->getExtInfo().withCallingConv(CC);
2968 type = unwrapped.wrap(S, S.Context.adjustFunctionType(unwrapped.get(), EI));
2969 return true;
John McCallf82b4e82010-02-04 05:44:44 +00002970}
2971
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002972/// Handle OpenCL image access qualifiers: read_only, write_only, read_write
2973static void HandleOpenCLImageAccessAttribute(QualType& CurType,
2974 const AttributeList &Attr,
2975 Sema &S) {
2976 // Check the attribute arguments.
2977 if (Attr.getNumArgs() != 1) {
2978 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2979 Attr.setInvalid();
2980 return;
2981 }
2982 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
2983 llvm::APSInt arg(32);
2984 if (sizeExpr->isTypeDependent() || sizeExpr->isValueDependent() ||
2985 !sizeExpr->isIntegerConstantExpr(arg, S.Context)) {
2986 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2987 << "opencl_image_access" << sizeExpr->getSourceRange();
2988 Attr.setInvalid();
2989 return;
2990 }
2991 unsigned iarg = static_cast<unsigned>(arg.getZExtValue());
2992 switch (iarg) {
2993 case CLIA_read_only:
2994 case CLIA_write_only:
2995 case CLIA_read_write:
2996 // Implemented in a separate patch
2997 break;
2998 default:
2999 // Implemented in a separate patch
3000 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
3001 << sizeExpr->getSourceRange();
3002 Attr.setInvalid();
3003 break;
3004 }
3005}
3006
John Thompson6e132aa2009-12-04 21:51:28 +00003007/// HandleVectorSizeAttribute - this attribute is only applicable to integral
3008/// and float scalars, although arrays, pointers, and function return values are
3009/// allowed in conjunction with this construct. Aggregates with this attribute
3010/// are invalid, even if they are of the same size as a corresponding scalar.
3011/// The raw attribute should contain precisely 1 argument, the vector size for
3012/// the variable, measured in bytes. If curType and rawAttr are well formed,
3013/// this routine will return a new vector type.
Chris Lattner788b0fd2010-06-23 06:00:24 +00003014static void HandleVectorSizeAttr(QualType& CurType, const AttributeList &Attr,
3015 Sema &S) {
Bob Wilson56affbc2010-11-16 00:32:16 +00003016 // Check the attribute arguments.
John Thompson6e132aa2009-12-04 21:51:28 +00003017 if (Attr.getNumArgs() != 1) {
3018 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003019 Attr.setInvalid();
John Thompson6e132aa2009-12-04 21:51:28 +00003020 return;
3021 }
3022 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
3023 llvm::APSInt vecSize(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003024 if (sizeExpr->isTypeDependent() || sizeExpr->isValueDependent() ||
3025 !sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
John Thompson6e132aa2009-12-04 21:51:28 +00003026 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
3027 << "vector_size" << sizeExpr->getSourceRange();
Abramo Bagnarae215f722010-04-30 13:10:51 +00003028 Attr.setInvalid();
John Thompson6e132aa2009-12-04 21:51:28 +00003029 return;
3030 }
3031 // the base type must be integer or float, and can't already be a vector.
Douglas Gregorf6094622010-07-23 15:58:24 +00003032 if (!CurType->isIntegerType() && !CurType->isRealFloatingType()) {
John Thompson6e132aa2009-12-04 21:51:28 +00003033 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003034 Attr.setInvalid();
John Thompson6e132aa2009-12-04 21:51:28 +00003035 return;
3036 }
3037 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
3038 // vecSize is specified in bytes - convert to bits.
3039 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
3040
3041 // the vector size needs to be an integral multiple of the type size.
3042 if (vectorSize % typeSize) {
3043 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
3044 << sizeExpr->getSourceRange();
Abramo Bagnarae215f722010-04-30 13:10:51 +00003045 Attr.setInvalid();
John Thompson6e132aa2009-12-04 21:51:28 +00003046 return;
3047 }
3048 if (vectorSize == 0) {
3049 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
3050 << sizeExpr->getSourceRange();
Abramo Bagnarae215f722010-04-30 13:10:51 +00003051 Attr.setInvalid();
John Thompson6e132aa2009-12-04 21:51:28 +00003052 return;
3053 }
3054
3055 // Success! Instantiate the vector type, the number of elements is > 0, and
3056 // not required to be a power of 2, unlike GCC.
Chris Lattner788b0fd2010-06-23 06:00:24 +00003057 CurType = S.Context.getVectorType(CurType, vectorSize/typeSize,
Bob Wilsone86d78c2010-11-10 21:56:12 +00003058 VectorType::GenericVector);
John Thompson6e132aa2009-12-04 21:51:28 +00003059}
3060
Bob Wilson4211bb62010-11-16 00:32:24 +00003061/// HandleNeonVectorTypeAttr - The "neon_vector_type" and
3062/// "neon_polyvector_type" attributes are used to create vector types that
3063/// are mangled according to ARM's ABI. Otherwise, these types are identical
3064/// to those created with the "vector_size" attribute. Unlike "vector_size"
3065/// the argument to these Neon attributes is the number of vector elements,
3066/// not the vector size in bytes. The vector width and element type must
3067/// match one of the standard Neon vector types.
3068static void HandleNeonVectorTypeAttr(QualType& CurType,
3069 const AttributeList &Attr, Sema &S,
3070 VectorType::VectorKind VecKind,
3071 const char *AttrName) {
3072 // Check the attribute arguments.
3073 if (Attr.getNumArgs() != 1) {
3074 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3075 Attr.setInvalid();
3076 return;
3077 }
3078 // The number of elements must be an ICE.
3079 Expr *numEltsExpr = static_cast<Expr *>(Attr.getArg(0));
3080 llvm::APSInt numEltsInt(32);
3081 if (numEltsExpr->isTypeDependent() || numEltsExpr->isValueDependent() ||
3082 !numEltsExpr->isIntegerConstantExpr(numEltsInt, S.Context)) {
3083 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
3084 << AttrName << numEltsExpr->getSourceRange();
3085 Attr.setInvalid();
3086 return;
3087 }
3088 // Only certain element types are supported for Neon vectors.
3089 const BuiltinType* BTy = CurType->getAs<BuiltinType>();
3090 if (!BTy ||
3091 (VecKind == VectorType::NeonPolyVector &&
3092 BTy->getKind() != BuiltinType::SChar &&
3093 BTy->getKind() != BuiltinType::Short) ||
3094 (BTy->getKind() != BuiltinType::SChar &&
3095 BTy->getKind() != BuiltinType::UChar &&
3096 BTy->getKind() != BuiltinType::Short &&
3097 BTy->getKind() != BuiltinType::UShort &&
3098 BTy->getKind() != BuiltinType::Int &&
3099 BTy->getKind() != BuiltinType::UInt &&
3100 BTy->getKind() != BuiltinType::LongLong &&
3101 BTy->getKind() != BuiltinType::ULongLong &&
3102 BTy->getKind() != BuiltinType::Float)) {
3103 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) <<CurType;
3104 Attr.setInvalid();
3105 return;
3106 }
3107 // The total size of the vector must be 64 or 128 bits.
3108 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
3109 unsigned numElts = static_cast<unsigned>(numEltsInt.getZExtValue());
3110 unsigned vecSize = typeSize * numElts;
3111 if (vecSize != 64 && vecSize != 128) {
3112 S.Diag(Attr.getLoc(), diag::err_attribute_bad_neon_vector_size) << CurType;
3113 Attr.setInvalid();
3114 return;
3115 }
3116
3117 CurType = S.Context.getVectorType(CurType, numElts, VecKind);
3118}
3119
John McCall711c52b2011-01-05 12:14:39 +00003120static void processTypeAttrs(TypeProcessingState &state, QualType &type,
3121 bool isDeclSpec, AttributeList *attrs) {
Chris Lattner232e8822008-02-21 01:08:11 +00003122 // Scan through and apply attributes to this type where it makes sense. Some
3123 // attributes (such as __address_space__, __vector_size__, etc) apply to the
3124 // type, but others can be present in the type specifiers even though they
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00003125 // apply to the decl. Here we apply type attributes and ignore the rest.
John McCall711c52b2011-01-05 12:14:39 +00003126
3127 AttributeList *next;
3128 do {
3129 AttributeList &attr = *attrs;
3130 next = attr.getNext();
3131
Abramo Bagnarae215f722010-04-30 13:10:51 +00003132 // Skip attributes that were marked to be invalid.
John McCall711c52b2011-01-05 12:14:39 +00003133 if (attr.isInvalid())
Abramo Bagnarae215f722010-04-30 13:10:51 +00003134 continue;
3135
Abramo Bagnarab1f1b262010-04-30 09:13:03 +00003136 // If this is an attribute we can handle, do so now,
3137 // otherwise, add it to the FnAttrs list for rechaining.
John McCall711c52b2011-01-05 12:14:39 +00003138 switch (attr.getKind()) {
Chris Lattner232e8822008-02-21 01:08:11 +00003139 default: break;
John McCall04a67a62010-02-05 21:31:56 +00003140
Chris Lattner232e8822008-02-21 01:08:11 +00003141 case AttributeList::AT_address_space:
John McCall711c52b2011-01-05 12:14:39 +00003142 HandleAddressSpaceTypeAttribute(type, attr, state.getSema());
Chris Lattnerc9b346d2008-06-29 00:50:08 +00003143 break;
John McCall711c52b2011-01-05 12:14:39 +00003144 OBJC_POINTER_TYPE_ATTRS_CASELIST:
3145 if (!handleObjCPointerTypeAttr(state, attr, type))
3146 distributeObjCPointerTypeAttr(state, attr, type);
Mike Stump24556362009-07-25 21:26:53 +00003147 break;
John Thompson6e132aa2009-12-04 21:51:28 +00003148 case AttributeList::AT_vector_size:
John McCall711c52b2011-01-05 12:14:39 +00003149 HandleVectorSizeAttr(type, attr, state.getSema());
John McCall04a67a62010-02-05 21:31:56 +00003150 break;
Bob Wilson4211bb62010-11-16 00:32:24 +00003151 case AttributeList::AT_neon_vector_type:
John McCall711c52b2011-01-05 12:14:39 +00003152 HandleNeonVectorTypeAttr(type, attr, state.getSema(),
3153 VectorType::NeonVector, "neon_vector_type");
Bob Wilson4211bb62010-11-16 00:32:24 +00003154 break;
3155 case AttributeList::AT_neon_polyvector_type:
John McCall711c52b2011-01-05 12:14:39 +00003156 HandleNeonVectorTypeAttr(type, attr, state.getSema(),
3157 VectorType::NeonPolyVector,
Bob Wilson4211bb62010-11-16 00:32:24 +00003158 "neon_polyvector_type");
3159 break;
John McCall04a67a62010-02-05 21:31:56 +00003160
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003161 case AttributeList::AT_opencl_image_access:
3162 HandleOpenCLImageAccessAttribute(type, attr, state.getSema());
3163 break;
3164
John McCall711c52b2011-01-05 12:14:39 +00003165 FUNCTION_TYPE_ATTRS_CASELIST:
3166 // Never process function type attributes as part of the
3167 // declaration-specifiers.
3168 if (isDeclSpec)
3169 distributeFunctionTypeAttrFromDeclSpec(state, attr, type);
3170
3171 // Otherwise, handle the possible delays.
3172 else if (!handleFunctionTypeAttr(state, attr, type))
3173 distributeFunctionTypeAttr(state, attr, type);
John Thompson6e132aa2009-12-04 21:51:28 +00003174 break;
Chris Lattner232e8822008-02-21 01:08:11 +00003175 }
John McCall711c52b2011-01-05 12:14:39 +00003176 } while ((attrs = next));
Chris Lattner232e8822008-02-21 01:08:11 +00003177}
3178
Mike Stump1eb44332009-09-09 15:08:12 +00003179/// @brief Ensure that the type T is a complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00003180///
3181/// This routine checks whether the type @p T is complete in any
3182/// context where a complete type is required. If @p T is a complete
Douglas Gregor86447ec2009-03-09 16:13:40 +00003183/// type, returns false. If @p T is a class template specialization,
3184/// this routine then attempts to perform class template
3185/// instantiation. If instantiation fails, or if @p T is incomplete
3186/// and cannot be completed, issues the diagnostic @p diag (giving it
3187/// the type @p T) and returns true.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00003188///
3189/// @param Loc The location in the source that the incomplete type
3190/// diagnostic should refer to.
3191///
3192/// @param T The type that this routine is examining for completeness.
3193///
Mike Stump1eb44332009-09-09 15:08:12 +00003194/// @param PD The partial diagnostic that will be printed out if T is not a
Anders Carlssonb7906612009-08-26 23:45:07 +00003195/// complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00003196///
3197/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
3198/// @c false otherwise.
Anders Carlsson91a0cc92009-08-26 22:33:56 +00003199bool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
Anders Carlsson8c8d9192009-10-09 23:51:55 +00003200 const PartialDiagnostic &PD,
3201 std::pair<SourceLocation,
3202 PartialDiagnostic> Note) {
Anders Carlsson91a0cc92009-08-26 22:33:56 +00003203 unsigned diag = PD.getDiagID();
Mike Stump1eb44332009-09-09 15:08:12 +00003204
Douglas Gregor573d9c32009-10-21 23:19:44 +00003205 // FIXME: Add this assertion to make sure we always get instantiation points.
3206 // assert(!Loc.isInvalid() && "Invalid location in RequireCompleteType");
Douglas Gregor690dc7f2009-05-21 23:48:18 +00003207 // FIXME: Add this assertion to help us flush out problems with
3208 // checking for dependent types and type-dependent expressions.
3209 //
Mike Stump1eb44332009-09-09 15:08:12 +00003210 // assert(!T->isDependentType() &&
Douglas Gregor690dc7f2009-05-21 23:48:18 +00003211 // "Can't ask whether a dependent type is complete");
3212
Douglas Gregor4ec339f2009-01-19 19:26:10 +00003213 // If we have a complete type, we're done.
3214 if (!T->isIncompleteType())
3215 return false;
Eli Friedman3c0eb162008-05-27 03:33:27 +00003216
Douglas Gregord475b8d2009-03-25 21:17:03 +00003217 // If we have a class template specialization or a class member of a
Sebastian Redl923d56d2009-11-05 15:52:31 +00003218 // class template specialization, or an array with known size of such,
3219 // try to instantiate it.
3220 QualType MaybeTemplate = T;
Douglas Gregor89c49f02009-11-09 22:08:55 +00003221 if (const ConstantArrayType *Array = Context.getAsConstantArrayType(T))
Sebastian Redl923d56d2009-11-05 15:52:31 +00003222 MaybeTemplate = Array->getElementType();
3223 if (const RecordType *Record = MaybeTemplate->getAs<RecordType>()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00003224 if (ClassTemplateSpecializationDecl *ClassTemplateSpec
Douglas Gregord475b8d2009-03-25 21:17:03 +00003225 = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
Douglas Gregor972e6ce2009-10-27 06:26:26 +00003226 if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared)
3227 return InstantiateClassTemplateSpecialization(Loc, ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00003228 TSK_ImplicitInstantiation,
Douglas Gregor5842ba92009-08-24 15:23:48 +00003229 /*Complain=*/diag != 0);
Mike Stump1eb44332009-09-09 15:08:12 +00003230 } else if (CXXRecordDecl *Rec
Douglas Gregord475b8d2009-03-25 21:17:03 +00003231 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
3232 if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00003233 MemberSpecializationInfo *MSInfo = Rec->getMemberSpecializationInfo();
3234 assert(MSInfo && "Missing member specialization information?");
Douglas Gregor357bbd02009-08-28 20:50:45 +00003235 // This record was instantiated from a class within a template.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00003236 if (MSInfo->getTemplateSpecializationKind()
Douglas Gregor972e6ce2009-10-27 06:26:26 +00003237 != TSK_ExplicitSpecialization)
Douglas Gregorf6b11852009-10-08 15:14:33 +00003238 return InstantiateClass(Loc, Rec, Pattern,
3239 getTemplateInstantiationArgs(Rec),
3240 TSK_ImplicitInstantiation,
3241 /*Complain=*/diag != 0);
Douglas Gregord475b8d2009-03-25 21:17:03 +00003242 }
3243 }
3244 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00003245
Douglas Gregor5842ba92009-08-24 15:23:48 +00003246 if (diag == 0)
3247 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003248
John McCall916c8702010-11-16 01:44:35 +00003249 const TagType *Tag = T->getAs<TagType>();
Rafael Espindola01620702010-03-21 22:56:43 +00003250
3251 // Avoid diagnosing invalid decls as incomplete.
3252 if (Tag && Tag->getDecl()->isInvalidDecl())
3253 return true;
3254
John McCall916c8702010-11-16 01:44:35 +00003255 // Give the external AST source a chance to complete the type.
3256 if (Tag && Tag->getDecl()->hasExternalLexicalStorage()) {
3257 Context.getExternalSource()->CompleteType(Tag->getDecl());
3258 if (!Tag->isIncompleteType())
3259 return false;
3260 }
3261
Douglas Gregor4ec339f2009-01-19 19:26:10 +00003262 // We have an incomplete type. Produce a diagnostic.
Anders Carlsson91a0cc92009-08-26 22:33:56 +00003263 Diag(Loc, PD) << T;
Douglas Gregor4ec339f2009-01-19 19:26:10 +00003264
Anders Carlsson8c8d9192009-10-09 23:51:55 +00003265 // If we have a note, produce it.
3266 if (!Note.first.isInvalid())
3267 Diag(Note.first, Note.second);
3268
Douglas Gregor4ec339f2009-01-19 19:26:10 +00003269 // If the type was a forward declaration of a class/struct/union
Rafael Espindola01620702010-03-21 22:56:43 +00003270 // type, produce a note.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00003271 if (Tag && !Tag->getDecl()->isInvalidDecl())
Mike Stump1eb44332009-09-09 15:08:12 +00003272 Diag(Tag->getDecl()->getLocation(),
Douglas Gregor4ec339f2009-01-19 19:26:10 +00003273 Tag->isBeingDefined() ? diag::note_type_being_defined
3274 : diag::note_forward_declaration)
3275 << QualType(Tag, 0);
3276
3277 return true;
3278}
Douglas Gregore6258932009-03-19 00:39:20 +00003279
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00003280bool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
3281 const PartialDiagnostic &PD) {
3282 return RequireCompleteType(Loc, T, PD,
3283 std::make_pair(SourceLocation(), PDiag(0)));
3284}
3285
3286bool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
3287 unsigned DiagID) {
3288 return RequireCompleteType(Loc, T, PDiag(DiagID),
3289 std::make_pair(SourceLocation(), PDiag(0)));
3290}
3291
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003292/// \brief Retrieve a version of the type 'T' that is elaborated by Keyword
3293/// and qualified by the nested-name-specifier contained in SS.
3294QualType Sema::getElaboratedType(ElaboratedTypeKeyword Keyword,
3295 const CXXScopeSpec &SS, QualType T) {
3296 if (T.isNull())
Douglas Gregore6258932009-03-19 00:39:20 +00003297 return T;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003298 NestedNameSpecifier *NNS;
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00003299 if (SS.isValid())
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003300 NNS = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
3301 else {
3302 if (Keyword == ETK_None)
3303 return T;
3304 NNS = 0;
3305 }
3306 return Context.getElaboratedType(Keyword, NNS, T);
Douglas Gregore6258932009-03-19 00:39:20 +00003307}
Anders Carlssonaf017e62009-06-29 22:58:55 +00003308
John McCall2a984ca2010-10-12 00:20:44 +00003309QualType Sema::BuildTypeofExprType(Expr *E, SourceLocation Loc) {
3310 ExprResult ER = CheckPlaceholderExpr(E, Loc);
3311 if (ER.isInvalid()) return QualType();
3312 E = ER.take();
3313
Fariborz Jahanian2b1d51b2010-10-05 23:24:00 +00003314 if (!E->isTypeDependent()) {
3315 QualType T = E->getType();
Fariborz Jahanianaca7f7b2010-10-06 00:23:25 +00003316 if (const TagType *TT = T->getAs<TagType>())
3317 DiagnoseUseOfDecl(TT->getDecl(), E->getExprLoc());
Fariborz Jahanian2b1d51b2010-10-05 23:24:00 +00003318 }
Anders Carlssonaf017e62009-06-29 22:58:55 +00003319 return Context.getTypeOfExprType(E);
3320}
3321
John McCall2a984ca2010-10-12 00:20:44 +00003322QualType Sema::BuildDecltypeType(Expr *E, SourceLocation Loc) {
3323 ExprResult ER = CheckPlaceholderExpr(E, Loc);
3324 if (ER.isInvalid()) return QualType();
3325 E = ER.take();
Douglas Gregor4b52e252009-12-21 23:17:24 +00003326
Anders Carlssonaf017e62009-06-29 22:58:55 +00003327 return Context.getDecltypeType(E);
3328}