blob: 23c159fbc5ad1ee9a03b3fcc512896f33314557a [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"
Reid Spencer5f016e22007-07-11 17:01:13 +000016#include "clang/AST/ASTContext.h"
Douglas Gregora8f32e02009-10-06 17:59:45 +000017#include "clang/AST/CXXInheritance.h"
Steve Naroff980e5082007-10-01 19:00:59 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor2943aed2009-03-03 04:44:36 +000019#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +000020#include "clang/AST/TypeLoc.h"
John McCall51bd8032009-10-18 01:05:36 +000021#include "clang/AST/TypeLocVisitor.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000022#include "clang/AST/Expr.h"
Anders Carlsson91a0cc92009-08-26 22:33:56 +000023#include "clang/Basic/PartialDiagnostic.h"
Charles Davisd18f9f92010-08-16 04:01:50 +000024#include "clang/Basic/TargetInfo.h"
John McCall19510852010-08-20 18:27:03 +000025#include "clang/Sema/DeclSpec.h"
Sebastian Redl4994d2d2009-07-04 11:39:00 +000026#include "llvm/ADT/SmallPtrSet.h"
Douglas Gregor87c12c42009-11-04 16:49:01 +000027#include "llvm/Support/ErrorHandling.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000028using namespace clang;
29
Douglas Gregor2dc0e642009-03-23 23:06:20 +000030/// \brief Perform adjustment on the parameter type of a function.
31///
32/// This routine adjusts the given parameter type @p T to the actual
Mike Stump1eb44332009-09-09 15:08:12 +000033/// parameter type used by semantic analysis (C99 6.7.5.3p[7,8],
34/// C++ [dcl.fct]p3). The adjusted parameter type is returned.
Douglas Gregor2dc0e642009-03-23 23:06:20 +000035QualType Sema::adjustParameterType(QualType T) {
36 // C99 6.7.5.3p7:
Chris Lattner778ed742009-10-25 17:36:50 +000037 // A declaration of a parameter as "array of type" shall be
38 // adjusted to "qualified pointer to type", where the type
39 // qualifiers (if any) are those specified within the [ and ] of
40 // the array type derivation.
41 if (T->isArrayType())
Douglas Gregor2dc0e642009-03-23 23:06:20 +000042 return Context.getArrayDecayedType(T);
Chris Lattner778ed742009-10-25 17:36:50 +000043
44 // C99 6.7.5.3p8:
45 // A declaration of a parameter as "function returning type"
46 // shall be adjusted to "pointer to function returning type", as
47 // in 6.3.2.1.
48 if (T->isFunctionType())
Douglas Gregor2dc0e642009-03-23 23:06:20 +000049 return Context.getPointerType(T);
50
51 return T;
52}
53
Chris Lattner5db2bb12009-10-25 18:21:37 +000054
55
56/// isOmittedBlockReturnType - Return true if this declarator is missing a
57/// return type because this is a omitted return type on a block literal.
Sebastian Redl8ce35b02009-10-25 21:45:37 +000058static bool isOmittedBlockReturnType(const Declarator &D) {
Chris Lattner5db2bb12009-10-25 18:21:37 +000059 if (D.getContext() != Declarator::BlockLiteralContext ||
Sebastian Redl8ce35b02009-10-25 21:45:37 +000060 D.getDeclSpec().hasTypeSpecifier())
Chris Lattner5db2bb12009-10-25 18:21:37 +000061 return false;
62
63 if (D.getNumTypeObjects() == 0)
Chris Lattnera64ef0a2009-10-25 22:09:09 +000064 return true; // ^{ ... }
Chris Lattner5db2bb12009-10-25 18:21:37 +000065
66 if (D.getNumTypeObjects() == 1 &&
67 D.getTypeObject(0).Kind == DeclaratorChunk::Function)
Chris Lattnera64ef0a2009-10-25 22:09:09 +000068 return true; // ^(int X, float Y) { ... }
Chris Lattner5db2bb12009-10-25 18:21:37 +000069
70 return false;
71}
72
John McCall04a67a62010-02-05 21:31:56 +000073typedef std::pair<const AttributeList*,QualType> DelayedAttribute;
74typedef llvm::SmallVectorImpl<DelayedAttribute> DelayedAttributeSet;
75
76static void ProcessTypeAttributeList(Sema &S, QualType &Type,
Charles Davis328ce342010-02-24 02:27:18 +000077 bool IsDeclSpec,
John McCall04a67a62010-02-05 21:31:56 +000078 const AttributeList *Attrs,
79 DelayedAttributeSet &DelayedFnAttrs);
80static bool ProcessFnAttr(Sema &S, QualType &Type, const AttributeList &Attr);
81
82static void ProcessDelayedFnAttrs(Sema &S, QualType &Type,
83 DelayedAttributeSet &Attrs) {
84 for (DelayedAttributeSet::iterator I = Attrs.begin(),
85 E = Attrs.end(); I != E; ++I)
Abramo Bagnarae215f722010-04-30 13:10:51 +000086 if (ProcessFnAttr(S, Type, *I->first)) {
John McCall04a67a62010-02-05 21:31:56 +000087 S.Diag(I->first->getLoc(), diag::warn_function_attribute_wrong_type)
88 << I->first->getName() << I->second;
Abramo Bagnarae215f722010-04-30 13:10:51 +000089 // Avoid any further processing of this attribute.
90 I->first->setInvalid();
91 }
John McCall04a67a62010-02-05 21:31:56 +000092 Attrs.clear();
93}
94
95static void DiagnoseDelayedFnAttrs(Sema &S, DelayedAttributeSet &Attrs) {
96 for (DelayedAttributeSet::iterator I = Attrs.begin(),
97 E = Attrs.end(); I != E; ++I) {
98 S.Diag(I->first->getLoc(), diag::warn_function_attribute_wrong_type)
99 << I->first->getName() << I->second;
Abramo Bagnarae215f722010-04-30 13:10:51 +0000100 // Avoid any further processing of this attribute.
101 I->first->setInvalid();
John McCall04a67a62010-02-05 21:31:56 +0000102 }
103 Attrs.clear();
104}
105
Douglas Gregor930d8b52009-01-30 22:09:00 +0000106/// \brief Convert the specified declspec to the appropriate type
107/// object.
Chris Lattner5db2bb12009-10-25 18:21:37 +0000108/// \param D the declarator containing the declaration specifier.
Chris Lattner5153ee62009-04-25 08:47:54 +0000109/// \returns The type described by the declaration specifiers. This function
110/// never returns null.
John McCall04a67a62010-02-05 21:31:56 +0000111static QualType ConvertDeclSpecToType(Sema &TheSema,
112 Declarator &TheDeclarator,
113 DelayedAttributeSet &Delayed) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 // FIXME: Should move the logic from DeclSpec::Finish to here for validity
115 // checking.
Chris Lattner5db2bb12009-10-25 18:21:37 +0000116 const DeclSpec &DS = TheDeclarator.getDeclSpec();
117 SourceLocation DeclLoc = TheDeclarator.getIdentifierLoc();
118 if (DeclLoc.isInvalid())
119 DeclLoc = DS.getSourceRange().getBegin();
Chris Lattner1564e392009-10-25 18:07:27 +0000120
121 ASTContext &Context = TheSema.Context;
Mike Stump1eb44332009-09-09 15:08:12 +0000122
Chris Lattner5db2bb12009-10-25 18:21:37 +0000123 QualType Result;
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 switch (DS.getTypeSpecType()) {
Chris Lattner96b77fc2008-04-02 06:50:17 +0000125 case DeclSpec::TST_void:
126 Result = Context.VoidTy;
127 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000128 case DeclSpec::TST_char:
129 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000130 Result = Context.CharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000131 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000132 Result = Context.SignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000133 else {
134 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
135 "Unknown TSS value");
Chris Lattnerfab5b452008-02-20 23:53:49 +0000136 Result = Context.UnsignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000137 }
Chris Lattner958858e2008-02-20 21:40:32 +0000138 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000139 case DeclSpec::TST_wchar:
140 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
141 Result = Context.WCharTy;
142 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
Chris Lattner1564e392009-10-25 18:07:27 +0000143 TheSema.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000144 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000145 Result = Context.getSignedWCharType();
146 } else {
147 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
148 "Unknown TSS value");
Chris Lattner1564e392009-10-25 18:07:27 +0000149 TheSema.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000150 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000151 Result = Context.getUnsignedWCharType();
152 }
153 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000154 case DeclSpec::TST_char16:
155 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
156 "Unknown TSS value");
157 Result = Context.Char16Ty;
158 break;
159 case DeclSpec::TST_char32:
160 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
161 "Unknown TSS value");
162 Result = Context.Char32Ty;
163 break;
Chris Lattnerd658b562008-04-05 06:32:51 +0000164 case DeclSpec::TST_unspecified:
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000165 // "<proto1,proto2>" is an objc qualified ID with a missing id.
Chris Lattner097e9162008-10-20 02:01:50 +0000166 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
John McCallc12c5bb2010-05-15 11:32:37 +0000167 Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy,
168 (ObjCProtocolDecl**)PQ,
169 DS.getNumProtocolQualifiers());
170 Result = Context.getObjCObjectPointerType(Result);
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000171 break;
172 }
Chris Lattner5db2bb12009-10-25 18:21:37 +0000173
174 // If this is a missing declspec in a block literal return context, then it
175 // is inferred from the return statements inside the block.
Sebastian Redl8ce35b02009-10-25 21:45:37 +0000176 if (isOmittedBlockReturnType(TheDeclarator)) {
Chris Lattner5db2bb12009-10-25 18:21:37 +0000177 Result = Context.DependentTy;
178 break;
179 }
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Chris Lattnerd658b562008-04-05 06:32:51 +0000181 // Unspecified typespec defaults to int in C90. However, the C90 grammar
182 // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
183 // type-qualifier, or storage-class-specifier. If not, emit an extwarn.
184 // Note that the one exception to this is function definitions, which are
185 // allowed to be completely missing a declspec. This is handled in the
186 // parser already though by it pretending to have seen an 'int' in this
187 // case.
Chris Lattner1564e392009-10-25 18:07:27 +0000188 if (TheSema.getLangOptions().ImplicitInt) {
Chris Lattner35d276f2009-02-27 18:53:28 +0000189 // In C89 mode, we only warn if there is a completely missing declspec
190 // when one is not allowed.
Chris Lattner3f84ad22009-04-22 05:27:59 +0000191 if (DS.isEmpty()) {
Chris Lattner1564e392009-10-25 18:07:27 +0000192 TheSema.Diag(DeclLoc, diag::ext_missing_declspec)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000193 << DS.getSourceRange()
Douglas Gregor849b2432010-03-31 17:46:05 +0000194 << FixItHint::CreateInsertion(DS.getSourceRange().getBegin(), "int");
Chris Lattner3f84ad22009-04-22 05:27:59 +0000195 }
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000196 } else if (!DS.hasTypeSpecifier()) {
Chris Lattnerd658b562008-04-05 06:32:51 +0000197 // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says:
198 // "At least one type specifier shall be given in the declaration
199 // specifiers in each declaration, and in the specifier-qualifier list in
200 // each struct declaration and type name."
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000201 // FIXME: Does Microsoft really have the implicit int extension in C++?
Chris Lattner1564e392009-10-25 18:07:27 +0000202 if (TheSema.getLangOptions().CPlusPlus &&
203 !TheSema.getLangOptions().Microsoft) {
204 TheSema.Diag(DeclLoc, diag::err_missing_type_specifier)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000205 << DS.getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000206
Chris Lattnerb78d8332009-06-26 04:45:06 +0000207 // When this occurs in C++ code, often something is very broken with the
208 // value being declared, poison it as invalid so we don't get chains of
209 // errors.
Chris Lattner5db2bb12009-10-25 18:21:37 +0000210 TheDeclarator.setInvalidType(true);
Chris Lattnerb78d8332009-06-26 04:45:06 +0000211 } else {
Chris Lattner1564e392009-10-25 18:07:27 +0000212 TheSema.Diag(DeclLoc, diag::ext_missing_type_specifier)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000213 << DS.getSourceRange();
Chris Lattnerb78d8332009-06-26 04:45:06 +0000214 }
Chris Lattnerd658b562008-04-05 06:32:51 +0000215 }
Mike Stump1eb44332009-09-09 15:08:12 +0000216
217 // FALL THROUGH.
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000218 case DeclSpec::TST_int: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000219 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
220 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000221 case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
222 case DeclSpec::TSW_short: Result = Context.ShortTy; break;
223 case DeclSpec::TSW_long: Result = Context.LongTy; break;
Chris Lattner311157f2009-10-25 18:25:04 +0000224 case DeclSpec::TSW_longlong:
225 Result = Context.LongLongTy;
226
227 // long long is a C99 feature.
228 if (!TheSema.getLangOptions().C99 &&
229 !TheSema.getLangOptions().CPlusPlus0x)
230 TheSema.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong);
231 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000232 }
233 } else {
234 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000235 case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
236 case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break;
237 case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break;
Chris Lattner311157f2009-10-25 18:25:04 +0000238 case DeclSpec::TSW_longlong:
239 Result = Context.UnsignedLongLongTy;
240
241 // long long is a C99 feature.
242 if (!TheSema.getLangOptions().C99 &&
243 !TheSema.getLangOptions().CPlusPlus0x)
244 TheSema.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong);
245 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000246 }
247 }
Chris Lattner958858e2008-02-20 21:40:32 +0000248 break;
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000249 }
Chris Lattnerfab5b452008-02-20 23:53:49 +0000250 case DeclSpec::TST_float: Result = Context.FloatTy; break;
Chris Lattner958858e2008-02-20 21:40:32 +0000251 case DeclSpec::TST_double:
252 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000253 Result = Context.LongDoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000254 else
Chris Lattnerfab5b452008-02-20 23:53:49 +0000255 Result = Context.DoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000256 break;
Chris Lattnerfab5b452008-02-20 23:53:49 +0000257 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
Reid Spencer5f016e22007-07-11 17:01:13 +0000258 case DeclSpec::TST_decimal32: // _Decimal32
259 case DeclSpec::TST_decimal64: // _Decimal64
260 case DeclSpec::TST_decimal128: // _Decimal128
Chris Lattner1564e392009-10-25 18:07:27 +0000261 TheSema.Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported);
Chris Lattner8f12f652009-05-13 05:02:08 +0000262 Result = Context.IntTy;
Chris Lattner5db2bb12009-10-25 18:21:37 +0000263 TheDeclarator.setInvalidType(true);
Chris Lattner8f12f652009-05-13 05:02:08 +0000264 break;
Chris Lattner99dc9142008-04-13 18:59:07 +0000265 case DeclSpec::TST_class:
Reid Spencer5f016e22007-07-11 17:01:13 +0000266 case DeclSpec::TST_enum:
267 case DeclSpec::TST_union:
268 case DeclSpec::TST_struct: {
John McCallb3d87482010-08-24 05:47:05 +0000269 TypeDecl *D = dyn_cast_or_null<TypeDecl>(DS.getRepAsDecl());
John McCall6e247262009-10-10 05:48:19 +0000270 if (!D) {
271 // This can happen in C++ with ambiguous lookups.
272 Result = Context.IntTy;
Chris Lattner5db2bb12009-10-25 18:21:37 +0000273 TheDeclarator.setInvalidType(true);
John McCall6e247262009-10-10 05:48:19 +0000274 break;
275 }
276
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000277 // If the type is deprecated or unavailable, diagnose it.
John McCall54abf7d2009-11-04 02:18:39 +0000278 TheSema.DiagnoseUseOfDecl(D, DS.getTypeSpecTypeLoc());
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000279
Reid Spencer5f016e22007-07-11 17:01:13 +0000280 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000281 DS.getTypeSpecSign() == 0 && "No qualifiers on tag names!");
282
Reid Spencer5f016e22007-07-11 17:01:13 +0000283 // TypeQuals handled by caller.
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000284 Result = Context.getTypeDeclType(D);
John McCall2191b202009-09-05 06:31:47 +0000285
286 // In C++, make an ElaboratedType.
Chris Lattner1564e392009-10-25 18:07:27 +0000287 if (TheSema.getLangOptions().CPlusPlus) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000288 ElaboratedTypeKeyword Keyword
289 = ElaboratedType::getKeywordForTypeSpec(DS.getTypeSpecType());
290 Result = TheSema.getElaboratedType(Keyword, DS.getTypeSpecScope(),
291 Result);
John McCall2191b202009-09-05 06:31:47 +0000292 }
Chris Lattner5153ee62009-04-25 08:47:54 +0000293 if (D->isInvalidDecl())
Chris Lattner5db2bb12009-10-25 18:21:37 +0000294 TheDeclarator.setInvalidType(true);
Chris Lattner958858e2008-02-20 21:40:32 +0000295 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000296 }
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000297 case DeclSpec::TST_typename: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000298 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
299 DS.getTypeSpecSign() == 0 &&
300 "Can't handle qualifiers on typedef names yet!");
John McCallb3d87482010-08-24 05:47:05 +0000301 Result = TheSema.GetTypeFromParser(DS.getRepAsType());
John McCall27940d22010-07-30 05:17:22 +0000302 if (Result.isNull())
303 TheDeclarator.setInvalidType(true);
304 else if (DeclSpec::ProtocolQualifierListTy PQ
305 = DS.getProtocolQualifiers()) {
John McCallc12c5bb2010-05-15 11:32:37 +0000306 if (const ObjCObjectType *ObjT = Result->getAs<ObjCObjectType>()) {
307 // Silently drop any existing protocol qualifiers.
308 // TODO: determine whether that's the right thing to do.
309 if (ObjT->getNumProtocols())
310 Result = ObjT->getBaseType();
311
312 if (DS.getNumProtocolQualifiers())
313 Result = Context.getObjCObjectType(Result,
314 (ObjCProtocolDecl**) PQ,
315 DS.getNumProtocolQualifiers());
316 } else if (Result->isObjCIdType()) {
Chris Lattnerae4da612008-07-26 01:53:50 +0000317 // id<protocol-list>
John McCallc12c5bb2010-05-15 11:32:37 +0000318 Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy,
319 (ObjCProtocolDecl**) PQ,
320 DS.getNumProtocolQualifiers());
321 Result = Context.getObjCObjectPointerType(Result);
322 } else if (Result->isObjCClassType()) {
Steve Naroff4262a072009-02-23 18:53:24 +0000323 // Class<protocol-list>
John McCallc12c5bb2010-05-15 11:32:37 +0000324 Result = Context.getObjCObjectType(Context.ObjCBuiltinClassTy,
325 (ObjCProtocolDecl**) PQ,
326 DS.getNumProtocolQualifiers());
327 Result = Context.getObjCObjectPointerType(Result);
Chris Lattner3f84ad22009-04-22 05:27:59 +0000328 } else {
Chris Lattner1564e392009-10-25 18:07:27 +0000329 TheSema.Diag(DeclLoc, diag::err_invalid_protocol_qualifiers)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000330 << DS.getSourceRange();
Chris Lattner5db2bb12009-10-25 18:21:37 +0000331 TheDeclarator.setInvalidType(true);
Chris Lattner3f84ad22009-04-22 05:27:59 +0000332 }
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000333 }
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Reid Spencer5f016e22007-07-11 17:01:13 +0000335 // TypeQuals handled by caller.
Chris Lattner958858e2008-02-20 21:40:32 +0000336 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000337 }
Chris Lattner958858e2008-02-20 21:40:32 +0000338 case DeclSpec::TST_typeofType:
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000339 // FIXME: Preserve type source info.
John McCallb3d87482010-08-24 05:47:05 +0000340 Result = TheSema.GetTypeFromParser(DS.getRepAsType());
Chris Lattner958858e2008-02-20 21:40:32 +0000341 assert(!Result.isNull() && "Didn't get a type for typeof?");
Fariborz Jahanian730e1752010-10-06 17:00:02 +0000342 if (!Result->isDependentType())
343 if (const TagType *TT = Result->getAs<TagType>())
344 TheSema.DiagnoseUseOfDecl(TT->getDecl(),
345 DS.getTypeSpecTypeLoc());
Steve Naroffd1861fd2007-07-31 12:34:36 +0000346 // TypeQuals handled by caller.
Chris Lattnerfab5b452008-02-20 23:53:49 +0000347 Result = Context.getTypeOfType(Result);
Chris Lattner958858e2008-02-20 21:40:32 +0000348 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000349 case DeclSpec::TST_typeofExpr: {
John McCallb3d87482010-08-24 05:47:05 +0000350 Expr *E = DS.getRepAsExpr();
Steve Naroffd1861fd2007-07-31 12:34:36 +0000351 assert(E && "Didn't get an expression for typeof?");
352 // TypeQuals handled by caller.
John McCall2a984ca2010-10-12 00:20:44 +0000353 Result = TheSema.BuildTypeofExprType(E, DS.getTypeSpecTypeLoc());
Douglas Gregor4b52e252009-12-21 23:17:24 +0000354 if (Result.isNull()) {
355 Result = Context.IntTy;
356 TheDeclarator.setInvalidType(true);
357 }
Chris Lattner958858e2008-02-20 21:40:32 +0000358 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000359 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000360 case DeclSpec::TST_decltype: {
John McCallb3d87482010-08-24 05:47:05 +0000361 Expr *E = DS.getRepAsExpr();
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000362 assert(E && "Didn't get an expression for decltype?");
363 // TypeQuals handled by caller.
John McCall2a984ca2010-10-12 00:20:44 +0000364 Result = TheSema.BuildDecltypeType(E, DS.getTypeSpecTypeLoc());
Anders Carlssonaf017e62009-06-29 22:58:55 +0000365 if (Result.isNull()) {
366 Result = Context.IntTy;
Chris Lattner5db2bb12009-10-25 18:21:37 +0000367 TheDeclarator.setInvalidType(true);
Anders Carlssonaf017e62009-06-29 22:58:55 +0000368 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000369 break;
370 }
Anders Carlssone89d1592009-06-26 18:41:36 +0000371 case DeclSpec::TST_auto: {
372 // TypeQuals handled by caller.
373 Result = Context.UndeducedAutoTy;
374 break;
375 }
Mike Stump1eb44332009-09-09 15:08:12 +0000376
Douglas Gregor809070a2009-02-18 17:45:20 +0000377 case DeclSpec::TST_error:
Chris Lattner5153ee62009-04-25 08:47:54 +0000378 Result = Context.IntTy;
Chris Lattner5db2bb12009-10-25 18:21:37 +0000379 TheDeclarator.setInvalidType(true);
Chris Lattner5153ee62009-04-25 08:47:54 +0000380 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000381 }
Mike Stump1eb44332009-09-09 15:08:12 +0000382
Chris Lattner958858e2008-02-20 21:40:32 +0000383 // Handle complex types.
Douglas Gregorf244cd72009-02-14 21:06:05 +0000384 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
Chris Lattner1564e392009-10-25 18:07:27 +0000385 if (TheSema.getLangOptions().Freestanding)
386 TheSema.Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
Chris Lattnerfab5b452008-02-20 23:53:49 +0000387 Result = Context.getComplexType(Result);
John Thompson82287d12010-02-05 00:12:22 +0000388 } else if (DS.isTypeAltiVecVector()) {
389 unsigned typeSize = static_cast<unsigned>(Context.getTypeSize(Result));
390 assert(typeSize > 0 && "type size for vector must be greater than 0 bits");
Bob Wilsone86d78c2010-11-10 21:56:12 +0000391 VectorType::VectorKind VecKind = VectorType::AltiVecVector;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000392 if (DS.isTypeAltiVecPixel())
Bob Wilsone86d78c2010-11-10 21:56:12 +0000393 VecKind = VectorType::AltiVecPixel;
Chris Lattner788b0fd2010-06-23 06:00:24 +0000394 else if (DS.isTypeAltiVecBool())
Bob Wilsone86d78c2010-11-10 21:56:12 +0000395 VecKind = VectorType::AltiVecBool;
396 Result = Context.getVectorType(Result, 128/typeSize, VecKind);
Douglas Gregorf244cd72009-02-14 21:06:05 +0000397 }
Mike Stump1eb44332009-09-09 15:08:12 +0000398
Argyrios Kyrtzidis47423bd2010-09-23 09:40:31 +0000399 // FIXME: Imaginary.
400 if (DS.getTypeSpecComplex() == DeclSpec::TSC_imaginary)
401 TheSema.Diag(DS.getTypeSpecComplexLoc(), diag::err_imaginary_not_supported);
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Chris Lattner38d8b982008-02-20 22:04:11 +0000403 // See if there are any attributes on the declspec that apply to the type (as
404 // opposed to the decl).
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000405 if (const AttributeList *AL = DS.getAttributes())
Charles Davis328ce342010-02-24 02:27:18 +0000406 ProcessTypeAttributeList(TheSema, Result, true, AL, Delayed);
Mike Stump1eb44332009-09-09 15:08:12 +0000407
Chris Lattner96b77fc2008-04-02 06:50:17 +0000408 // Apply const/volatile/restrict qualifiers to T.
409 if (unsigned TypeQuals = DS.getTypeQualifiers()) {
410
411 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
412 // or incomplete types shall not be restrict-qualified." C++ also allows
413 // restrict-qualified references.
John McCall0953e762009-09-24 19:53:00 +0000414 if (TypeQuals & DeclSpec::TQ_restrict) {
Fariborz Jahanian2b5ff1a2009-12-07 18:08:58 +0000415 if (Result->isAnyPointerType() || Result->isReferenceType()) {
416 QualType EltTy;
417 if (Result->isObjCObjectPointerType())
418 EltTy = Result;
419 else
420 EltTy = Result->isPointerType() ?
421 Result->getAs<PointerType>()->getPointeeType() :
422 Result->getAs<ReferenceType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Douglas Gregorbad0e652009-03-24 20:32:41 +0000424 // If we have a pointer or reference, the pointee must have an object
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000425 // incomplete type.
426 if (!EltTy->isIncompleteOrObjectType()) {
Chris Lattner1564e392009-10-25 18:07:27 +0000427 TheSema.Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000428 diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattnerd1625842008-11-24 06:25:27 +0000429 << EltTy << DS.getSourceRange();
John McCall0953e762009-09-24 19:53:00 +0000430 TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000431 }
432 } else {
Chris Lattner1564e392009-10-25 18:07:27 +0000433 TheSema.Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000434 diag::err_typecheck_invalid_restrict_not_pointer)
Chris Lattnerd1625842008-11-24 06:25:27 +0000435 << Result << DS.getSourceRange();
John McCall0953e762009-09-24 19:53:00 +0000436 TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
Chris Lattner96b77fc2008-04-02 06:50:17 +0000437 }
Chris Lattner96b77fc2008-04-02 06:50:17 +0000438 }
Mike Stump1eb44332009-09-09 15:08:12 +0000439
Chris Lattner96b77fc2008-04-02 06:50:17 +0000440 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
441 // of a function type includes any type qualifiers, the behavior is
442 // undefined."
443 if (Result->isFunctionType() && TypeQuals) {
444 // Get some location to point at, either the C or V location.
445 SourceLocation Loc;
John McCall0953e762009-09-24 19:53:00 +0000446 if (TypeQuals & DeclSpec::TQ_const)
Chris Lattner96b77fc2008-04-02 06:50:17 +0000447 Loc = DS.getConstSpecLoc();
John McCall0953e762009-09-24 19:53:00 +0000448 else if (TypeQuals & DeclSpec::TQ_volatile)
Chris Lattner96b77fc2008-04-02 06:50:17 +0000449 Loc = DS.getVolatileSpecLoc();
John McCall0953e762009-09-24 19:53:00 +0000450 else {
451 assert((TypeQuals & DeclSpec::TQ_restrict) &&
452 "Has CVR quals but not C, V, or R?");
453 Loc = DS.getRestrictSpecLoc();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000454 }
Chris Lattner1564e392009-10-25 18:07:27 +0000455 TheSema.Diag(Loc, diag::warn_typecheck_function_qualifiers)
Chris Lattnerd1625842008-11-24 06:25:27 +0000456 << Result << DS.getSourceRange();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000457 }
Mike Stump1eb44332009-09-09 15:08:12 +0000458
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000459 // C++ [dcl.ref]p1:
460 // Cv-qualified references are ill-formed except when the
461 // cv-qualifiers are introduced through the use of a typedef
462 // (7.1.3) or of a template type argument (14.3), in which
463 // case the cv-qualifiers are ignored.
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000464 // FIXME: Shouldn't we be checking SCS_typedef here?
465 if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000466 TypeQuals && Result->isReferenceType()) {
John McCall0953e762009-09-24 19:53:00 +0000467 TypeQuals &= ~DeclSpec::TQ_const;
468 TypeQuals &= ~DeclSpec::TQ_volatile;
Mike Stump1eb44332009-09-09 15:08:12 +0000469 }
470
John McCall0953e762009-09-24 19:53:00 +0000471 Qualifiers Quals = Qualifiers::fromCVRMask(TypeQuals);
472 Result = Context.getQualifiedType(Result, Quals);
Chris Lattner96b77fc2008-04-02 06:50:17 +0000473 }
John McCall0953e762009-09-24 19:53:00 +0000474
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000475 return Result;
476}
477
Douglas Gregorcd281c32009-02-28 00:25:32 +0000478static std::string getPrintableNameForEntity(DeclarationName Entity) {
479 if (Entity)
480 return Entity.getAsString();
Mike Stump1eb44332009-09-09 15:08:12 +0000481
Douglas Gregorcd281c32009-02-28 00:25:32 +0000482 return "type name";
483}
484
John McCall28654742010-06-05 06:41:15 +0000485QualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc,
486 Qualifiers Qs) {
487 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
488 // object or incomplete types shall not be restrict-qualified."
489 if (Qs.hasRestrict()) {
490 unsigned DiagID = 0;
491 QualType ProblemTy;
492
493 const Type *Ty = T->getCanonicalTypeInternal().getTypePtr();
494 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(Ty)) {
495 if (!RTy->getPointeeType()->isIncompleteOrObjectType()) {
496 DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
497 ProblemTy = T->getAs<ReferenceType>()->getPointeeType();
498 }
499 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
500 if (!PTy->getPointeeType()->isIncompleteOrObjectType()) {
501 DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
502 ProblemTy = T->getAs<PointerType>()->getPointeeType();
503 }
504 } else if (const MemberPointerType *PTy = dyn_cast<MemberPointerType>(Ty)) {
505 if (!PTy->getPointeeType()->isIncompleteOrObjectType()) {
506 DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
507 ProblemTy = T->getAs<PointerType>()->getPointeeType();
508 }
509 } else if (!Ty->isDependentType()) {
510 // FIXME: this deserves a proper diagnostic
511 DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
512 ProblemTy = T;
513 }
514
515 if (DiagID) {
516 Diag(Loc, DiagID) << ProblemTy;
517 Qs.removeRestrict();
518 }
519 }
520
521 return Context.getQualifiedType(T, Qs);
522}
523
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000524/// \brief Build a paren type including \p T.
525QualType Sema::BuildParenType(QualType T) {
526 return Context.getParenType(T);
527}
528
Douglas Gregorcd281c32009-02-28 00:25:32 +0000529/// \brief Build a pointer type.
530///
531/// \param T The type to which we'll be building a pointer.
532///
Douglas Gregorcd281c32009-02-28 00:25:32 +0000533/// \param Loc The location of the entity whose type involves this
534/// pointer type or, if there is no such entity, the location of the
535/// type that will have pointer type.
536///
537/// \param Entity The name of the entity that involves the pointer
538/// type, if known.
539///
540/// \returns A suitable pointer type, if there are no
541/// errors. Otherwise, returns a NULL type.
John McCall28654742010-06-05 06:41:15 +0000542QualType Sema::BuildPointerType(QualType T,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000543 SourceLocation Loc, DeclarationName Entity) {
544 if (T->isReferenceType()) {
545 // C++ 8.3.2p4: There shall be no ... pointers to references ...
546 Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
John McCallac406052009-10-30 00:37:20 +0000547 << getPrintableNameForEntity(Entity) << T;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000548 return QualType();
549 }
550
John McCallc12c5bb2010-05-15 11:32:37 +0000551 assert(!T->isObjCObjectType() && "Should build ObjCObjectPointerType");
Douglas Gregor92e986e2010-04-22 16:44:27 +0000552
Douglas Gregorcd281c32009-02-28 00:25:32 +0000553 // Build the pointer type.
John McCall28654742010-06-05 06:41:15 +0000554 return Context.getPointerType(T);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000555}
556
557/// \brief Build a reference type.
558///
559/// \param T The type to which we'll be building a reference.
560///
Douglas Gregorcd281c32009-02-28 00:25:32 +0000561/// \param Loc The location of the entity whose type involves this
562/// reference type or, if there is no such entity, the location of the
563/// type that will have reference type.
564///
565/// \param Entity The name of the entity that involves the reference
566/// type, if known.
567///
568/// \returns A suitable reference type, if there are no
569/// errors. Otherwise, returns a NULL type.
John McCall54e14c42009-10-22 22:37:11 +0000570QualType Sema::BuildReferenceType(QualType T, bool SpelledAsLValue,
John McCall28654742010-06-05 06:41:15 +0000571 SourceLocation Loc,
John McCall54e14c42009-10-22 22:37:11 +0000572 DeclarationName Entity) {
John McCall54e14c42009-10-22 22:37:11 +0000573 bool LValueRef = SpelledAsLValue || T->getAs<LValueReferenceType>();
574
575 // C++0x [dcl.typedef]p9: If a typedef TD names a type that is a
576 // reference to a type T, and attempt to create the type "lvalue
577 // reference to cv TD" creates the type "lvalue reference to T".
578 // We use the qualifiers (restrict or none) of the original reference,
579 // not the new ones. This is consistent with GCC.
580
581 // C++ [dcl.ref]p4: There shall be no references to references.
582 //
583 // According to C++ DR 106, references to references are only
584 // diagnosed when they are written directly (e.g., "int & &"),
585 // but not when they happen via a typedef:
586 //
587 // typedef int& intref;
588 // typedef intref& intref2;
589 //
590 // Parser::ParseDeclaratorInternal diagnoses the case where
591 // references are written directly; here, we handle the
592 // collapsing of references-to-references as described in C++
593 // DR 106 and amended by C++ DR 540.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000594
595 // C++ [dcl.ref]p1:
Eli Friedman33a31382009-08-05 19:21:58 +0000596 // A declarator that specifies the type "reference to cv void"
Douglas Gregorcd281c32009-02-28 00:25:32 +0000597 // is ill-formed.
598 if (T->isVoidType()) {
599 Diag(Loc, diag::err_reference_to_void);
600 return QualType();
601 }
602
Douglas Gregorcd281c32009-02-28 00:25:32 +0000603 // Handle restrict on references.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000604 if (LValueRef)
John McCall28654742010-06-05 06:41:15 +0000605 return Context.getLValueReferenceType(T, SpelledAsLValue);
606 return Context.getRValueReferenceType(T);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000607}
608
609/// \brief Build an array type.
610///
611/// \param T The type of each element in the array.
612///
613/// \param ASM C99 array size modifier (e.g., '*', 'static').
Mike Stump1eb44332009-09-09 15:08:12 +0000614///
615/// \param ArraySize Expression describing the size of the array.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000616///
Douglas Gregorcd281c32009-02-28 00:25:32 +0000617/// \param Loc The location of the entity whose type involves this
618/// array type or, if there is no such entity, the location of the
619/// type that will have array type.
620///
621/// \param Entity The name of the entity that involves the array
622/// type, if known.
623///
624/// \returns A suitable array type, if there are no errors. Otherwise,
625/// returns a NULL type.
626QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
627 Expr *ArraySize, unsigned Quals,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000628 SourceRange Brackets, DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000629
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000630 SourceLocation Loc = Brackets.getBegin();
Sebastian Redl923d56d2009-11-05 15:52:31 +0000631 if (getLangOptions().CPlusPlus) {
Douglas Gregor138bb232010-04-27 19:38:14 +0000632 // C++ [dcl.array]p1:
633 // T is called the array element type; this type shall not be a reference
634 // type, the (possibly cv-qualified) type void, a function type or an
635 // abstract class type.
636 //
637 // Note: function types are handled in the common path with C.
638 if (T->isReferenceType()) {
639 Diag(Loc, diag::err_illegal_decl_array_of_references)
640 << getPrintableNameForEntity(Entity) << T;
641 return QualType();
642 }
643
Sebastian Redl923d56d2009-11-05 15:52:31 +0000644 if (T->isVoidType()) {
645 Diag(Loc, diag::err_illegal_decl_array_incomplete_type) << T;
646 return QualType();
647 }
Douglas Gregor138bb232010-04-27 19:38:14 +0000648
649 if (RequireNonAbstractType(Brackets.getBegin(), T,
650 diag::err_array_of_abstract_type))
651 return QualType();
652
Sebastian Redl923d56d2009-11-05 15:52:31 +0000653 } else {
Douglas Gregor138bb232010-04-27 19:38:14 +0000654 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
655 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
Sebastian Redl923d56d2009-11-05 15:52:31 +0000656 if (RequireCompleteType(Loc, T,
657 diag::err_illegal_decl_array_incomplete_type))
658 return QualType();
659 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000660
661 if (T->isFunctionType()) {
662 Diag(Loc, diag::err_illegal_decl_array_of_functions)
John McCallac406052009-10-30 00:37:20 +0000663 << getPrintableNameForEntity(Entity) << T;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000664 return QualType();
665 }
Mike Stump1eb44332009-09-09 15:08:12 +0000666
Anders Carlssone7cf07d2009-06-26 19:33:28 +0000667 if (Context.getCanonicalType(T) == Context.UndeducedAutoTy) {
Mike Stump1eb44332009-09-09 15:08:12 +0000668 Diag(Loc, diag::err_illegal_decl_array_of_auto)
Anders Carlssone7cf07d2009-06-26 19:33:28 +0000669 << getPrintableNameForEntity(Entity);
670 return QualType();
671 }
Mike Stump1eb44332009-09-09 15:08:12 +0000672
Ted Kremenek6217b802009-07-29 21:53:49 +0000673 if (const RecordType *EltTy = T->getAs<RecordType>()) {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000674 // If the element type is a struct or union that contains a variadic
675 // array, accept it as a GNU extension: C99 6.7.2.1p2.
676 if (EltTy->getDecl()->hasFlexibleArrayMember())
677 Diag(Loc, diag::ext_flexible_array_in_array) << T;
John McCallc12c5bb2010-05-15 11:32:37 +0000678 } else if (T->isObjCObjectType()) {
Chris Lattnerc7c11b12009-04-27 01:55:56 +0000679 Diag(Loc, diag::err_objc_array_of_interfaces) << T;
680 return QualType();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000681 }
Mike Stump1eb44332009-09-09 15:08:12 +0000682
Douglas Gregorcd281c32009-02-28 00:25:32 +0000683 // C99 6.7.5.2p1: The size expression shall have integer type.
684 if (ArraySize && !ArraySize->isTypeDependent() &&
Douglas Gregor1274ccd2010-10-08 23:50:27 +0000685 !ArraySize->getType()->isIntegralOrUnscopedEnumerationType()) {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000686 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
687 << ArraySize->getType() << ArraySize->getSourceRange();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000688 return QualType();
689 }
Douglas Gregor2767ce22010-08-18 00:39:00 +0000690 llvm::APSInt ConstVal(Context.getTypeSize(Context.getSizeType()));
Douglas Gregorcd281c32009-02-28 00:25:32 +0000691 if (!ArraySize) {
Eli Friedmanf91f5c82009-04-26 21:57:51 +0000692 if (ASM == ArrayType::Star)
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000693 T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets);
Eli Friedmanf91f5c82009-04-26 21:57:51 +0000694 else
695 T = Context.getIncompleteArrayType(T, ASM, Quals);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000696 } else if (ArraySize->isTypeDependent() || ArraySize->isValueDependent()) {
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000697 T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000698 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
Sebastian Redl923d56d2009-11-05 15:52:31 +0000699 (!T->isDependentType() && !T->isIncompleteType() &&
700 !T->isConstantSizeType())) {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000701 // Per C99, a variable array is an array with either a non-constant
702 // size or an element type that has a non-constant-size
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000703 T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000704 } else {
705 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
706 // have a value greater than zero.
Sebastian Redl923d56d2009-11-05 15:52:31 +0000707 if (ConstVal.isSigned() && ConstVal.isNegative()) {
708 Diag(ArraySize->getLocStart(),
709 diag::err_typecheck_negative_array_size)
710 << ArraySize->getSourceRange();
711 return QualType();
712 }
713 if (ConstVal == 0) {
Douglas Gregor02024a92010-03-28 02:42:43 +0000714 // GCC accepts zero sized static arrays. We allow them when
715 // we're not in a SFINAE context.
716 Diag(ArraySize->getLocStart(),
717 isSFINAEContext()? diag::err_typecheck_zero_array_size
718 : diag::ext_typecheck_zero_array_size)
Sebastian Redl923d56d2009-11-05 15:52:31 +0000719 << ArraySize->getSourceRange();
Douglas Gregor2767ce22010-08-18 00:39:00 +0000720 } else if (!T->isDependentType() && !T->isVariablyModifiedType() &&
721 !T->isIncompleteType()) {
722 // Is the array too large?
723 unsigned ActiveSizeBits
724 = ConstantArrayType::getNumAddressingBits(Context, T, ConstVal);
725 if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context))
726 Diag(ArraySize->getLocStart(), diag::err_array_too_large)
727 << ConstVal.toString(10)
728 << ArraySize->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000729 }
Douglas Gregor2767ce22010-08-18 00:39:00 +0000730
John McCall46a617a2009-10-16 00:14:28 +0000731 T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000732 }
David Chisnallaf407762010-01-11 23:08:08 +0000733 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
734 if (!getLangOptions().C99) {
Douglas Gregor0fddb972010-05-22 16:17:30 +0000735 if (T->isVariableArrayType()) {
736 // Prohibit the use of non-POD types in VLAs.
Douglas Gregor204ce172010-05-24 20:42:30 +0000737 if (!T->isDependentType() &&
738 !Context.getBaseElementType(T)->isPODType()) {
Douglas Gregor0fddb972010-05-22 16:17:30 +0000739 Diag(Loc, diag::err_vla_non_pod)
740 << Context.getBaseElementType(T);
741 return QualType();
742 }
Douglas Gregora481ec42010-05-23 19:57:01 +0000743 // Prohibit the use of VLAs during template argument deduction.
744 else if (isSFINAEContext()) {
745 Diag(Loc, diag::err_vla_in_sfinae);
746 return QualType();
747 }
Douglas Gregor0fddb972010-05-22 16:17:30 +0000748 // Just extwarn about VLAs.
749 else
750 Diag(Loc, diag::ext_vla);
751 } else if (ASM != ArrayType::Normal || Quals != 0)
Douglas Gregor043cad22009-09-11 00:18:58 +0000752 Diag(Loc,
753 getLangOptions().CPlusPlus? diag::err_c99_array_usage_cxx
754 : diag::ext_c99_array_usage);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000755 }
756
757 return T;
758}
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000759
760/// \brief Build an ext-vector type.
761///
762/// Run the required checks for the extended vector type.
John McCall9ae2f072010-08-23 23:25:46 +0000763QualType Sema::BuildExtVectorType(QualType T, Expr *ArraySize,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000764 SourceLocation AttrLoc) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000765 // unlike gcc's vector_size attribute, we do not allow vectors to be defined
766 // in conjunction with complex types (pointers, arrays, functions, etc.).
Mike Stump1eb44332009-09-09 15:08:12 +0000767 if (!T->isDependentType() &&
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000768 !T->isIntegerType() && !T->isRealFloatingType()) {
769 Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T;
770 return QualType();
771 }
772
John McCall9ae2f072010-08-23 23:25:46 +0000773 if (!ArraySize->isTypeDependent() && !ArraySize->isValueDependent()) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000774 llvm::APSInt vecSize(32);
John McCall9ae2f072010-08-23 23:25:46 +0000775 if (!ArraySize->isIntegerConstantExpr(vecSize, Context)) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000776 Diag(AttrLoc, diag::err_attribute_argument_not_int)
John McCall9ae2f072010-08-23 23:25:46 +0000777 << "ext_vector_type" << ArraySize->getSourceRange();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000778 return QualType();
779 }
Mike Stump1eb44332009-09-09 15:08:12 +0000780
781 // unlike gcc's vector_size attribute, the size is specified as the
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000782 // number of elements, not the number of bytes.
Mike Stump1eb44332009-09-09 15:08:12 +0000783 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
784
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000785 if (vectorSize == 0) {
786 Diag(AttrLoc, diag::err_attribute_zero_size)
John McCall9ae2f072010-08-23 23:25:46 +0000787 << ArraySize->getSourceRange();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000788 return QualType();
789 }
Mike Stump1eb44332009-09-09 15:08:12 +0000790
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000791 if (!T->isDependentType())
792 return Context.getExtVectorType(T, vectorSize);
Mike Stump1eb44332009-09-09 15:08:12 +0000793 }
794
John McCall9ae2f072010-08-23 23:25:46 +0000795 return Context.getDependentSizedExtVectorType(T, ArraySize, AttrLoc);
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000796}
Mike Stump1eb44332009-09-09 15:08:12 +0000797
Douglas Gregor724651c2009-02-28 01:04:19 +0000798/// \brief Build a function type.
799///
800/// This routine checks the function type according to C++ rules and
801/// under the assumption that the result type and parameter types have
802/// just been instantiated from a template. It therefore duplicates
Douglas Gregor2943aed2009-03-03 04:44:36 +0000803/// some of the behavior of GetTypeForDeclarator, but in a much
Douglas Gregor724651c2009-02-28 01:04:19 +0000804/// simpler form that is only suitable for this narrow use case.
805///
806/// \param T The return type of the function.
807///
808/// \param ParamTypes The parameter types of the function. This array
809/// will be modified to account for adjustments to the types of the
810/// function parameters.
811///
812/// \param NumParamTypes The number of parameter types in ParamTypes.
813///
814/// \param Variadic Whether this is a variadic function type.
815///
816/// \param Quals The cvr-qualifiers to be applied to the function type.
817///
818/// \param Loc The location of the entity whose type involves this
819/// function type or, if there is no such entity, the location of the
820/// type that will have function type.
821///
822/// \param Entity The name of the entity that involves the function
823/// type, if known.
824///
825/// \returns A suitable function type, if there are no
826/// errors. Otherwise, returns a NULL type.
827QualType Sema::BuildFunctionType(QualType T,
Mike Stump1eb44332009-09-09 15:08:12 +0000828 QualType *ParamTypes,
Douglas Gregor724651c2009-02-28 01:04:19 +0000829 unsigned NumParamTypes,
830 bool Variadic, unsigned Quals,
Eli Friedmanfa869542010-08-05 02:54:05 +0000831 SourceLocation Loc, DeclarationName Entity,
832 const FunctionType::ExtInfo &Info) {
Douglas Gregor724651c2009-02-28 01:04:19 +0000833 if (T->isArrayType() || T->isFunctionType()) {
Douglas Gregor58408bc2010-01-11 18:46:21 +0000834 Diag(Loc, diag::err_func_returning_array_function)
835 << T->isFunctionType() << T;
Douglas Gregor724651c2009-02-28 01:04:19 +0000836 return QualType();
837 }
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000838
Douglas Gregor724651c2009-02-28 01:04:19 +0000839 bool Invalid = false;
840 for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) {
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000841 QualType ParamType = adjustParameterType(ParamTypes[Idx]);
842 if (ParamType->isVoidType()) {
Douglas Gregor724651c2009-02-28 01:04:19 +0000843 Diag(Loc, diag::err_param_with_void_type);
844 Invalid = true;
845 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000846
John McCall54e14c42009-10-22 22:37:11 +0000847 ParamTypes[Idx] = ParamType;
Douglas Gregor724651c2009-02-28 01:04:19 +0000848 }
849
850 if (Invalid)
851 return QualType();
852
Mike Stump1eb44332009-09-09 15:08:12 +0000853 return Context.getFunctionType(T, ParamTypes, NumParamTypes, Variadic,
Eli Friedmanfa869542010-08-05 02:54:05 +0000854 Quals, false, false, 0, 0, Info);
Douglas Gregor724651c2009-02-28 01:04:19 +0000855}
Mike Stump1eb44332009-09-09 15:08:12 +0000856
Douglas Gregor949bf692009-06-09 22:17:39 +0000857/// \brief Build a member pointer type \c T Class::*.
858///
859/// \param T the type to which the member pointer refers.
860/// \param Class the class type into which the member pointer points.
John McCall0953e762009-09-24 19:53:00 +0000861/// \param CVR Qualifiers applied to the member pointer type
Douglas Gregor949bf692009-06-09 22:17:39 +0000862/// \param Loc the location where this type begins
863/// \param Entity the name of the entity that will have this member pointer type
864///
865/// \returns a member pointer type, if successful, or a NULL type if there was
866/// an error.
Mike Stump1eb44332009-09-09 15:08:12 +0000867QualType Sema::BuildMemberPointerType(QualType T, QualType Class,
John McCall28654742010-06-05 06:41:15 +0000868 SourceLocation Loc,
Douglas Gregor949bf692009-06-09 22:17:39 +0000869 DeclarationName Entity) {
870 // Verify that we're not building a pointer to pointer to function with
871 // exception specification.
872 if (CheckDistantExceptionSpec(T)) {
873 Diag(Loc, diag::err_distant_exception_spec);
874
875 // FIXME: If we're doing this as part of template instantiation,
876 // we should return immediately.
877
878 // Build the type anyway, but use the canonical type so that the
879 // exception specifiers are stripped off.
880 T = Context.getCanonicalType(T);
881 }
882
Sebastian Redl73780122010-06-09 21:19:43 +0000883 // C++ 8.3.3p3: A pointer to member shall not point to ... a member
Douglas Gregor949bf692009-06-09 22:17:39 +0000884 // with reference type, or "cv void."
885 if (T->isReferenceType()) {
Anders Carlsson8d4655d2009-06-30 00:06:57 +0000886 Diag(Loc, diag::err_illegal_decl_mempointer_to_reference)
John McCallac406052009-10-30 00:37:20 +0000887 << (Entity? Entity.getAsString() : "type name") << T;
Douglas Gregor949bf692009-06-09 22:17:39 +0000888 return QualType();
889 }
890
891 if (T->isVoidType()) {
892 Diag(Loc, diag::err_illegal_decl_mempointer_to_void)
893 << (Entity? Entity.getAsString() : "type name");
894 return QualType();
895 }
896
Douglas Gregor949bf692009-06-09 22:17:39 +0000897 if (!Class->isDependentType() && !Class->isRecordType()) {
898 Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class;
899 return QualType();
900 }
901
Charles Davisd18f9f92010-08-16 04:01:50 +0000902 // In the Microsoft ABI, the class is allowed to be an incomplete
903 // type. In such cases, the compiler makes a worst-case assumption.
904 // We make no such assumption right now, so emit an error if the
905 // class isn't a complete type.
Charles Davis20cf7172010-08-19 02:18:14 +0000906 if (Context.Target.getCXXABI() == CXXABI_Microsoft &&
Charles Davisd18f9f92010-08-16 04:01:50 +0000907 RequireCompleteType(Loc, Class, diag::err_incomplete_type))
908 return QualType();
909
John McCall28654742010-06-05 06:41:15 +0000910 return Context.getMemberPointerType(T, Class.getTypePtr());
Douglas Gregor949bf692009-06-09 22:17:39 +0000911}
Mike Stump1eb44332009-09-09 15:08:12 +0000912
Anders Carlsson9a917e42009-06-12 22:56:54 +0000913/// \brief Build a block pointer type.
914///
915/// \param T The type to which we'll be building a block pointer.
916///
John McCall0953e762009-09-24 19:53:00 +0000917/// \param CVR The cvr-qualifiers to be applied to the block pointer type.
Anders Carlsson9a917e42009-06-12 22:56:54 +0000918///
919/// \param Loc The location of the entity whose type involves this
920/// block pointer type or, if there is no such entity, the location of the
921/// type that will have block pointer type.
922///
923/// \param Entity The name of the entity that involves the block pointer
924/// type, if known.
925///
926/// \returns A suitable block pointer type, if there are no
927/// errors. Otherwise, returns a NULL type.
John McCall28654742010-06-05 06:41:15 +0000928QualType Sema::BuildBlockPointerType(QualType T,
Mike Stump1eb44332009-09-09 15:08:12 +0000929 SourceLocation Loc,
Anders Carlsson9a917e42009-06-12 22:56:54 +0000930 DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000931 if (!T->isFunctionType()) {
Anders Carlsson9a917e42009-06-12 22:56:54 +0000932 Diag(Loc, diag::err_nonfunction_block_type);
933 return QualType();
934 }
Mike Stump1eb44332009-09-09 15:08:12 +0000935
John McCall28654742010-06-05 06:41:15 +0000936 return Context.getBlockPointerType(T);
Anders Carlsson9a917e42009-06-12 22:56:54 +0000937}
938
John McCallb3d87482010-08-24 05:47:05 +0000939QualType Sema::GetTypeFromParser(ParsedType Ty, TypeSourceInfo **TInfo) {
940 QualType QT = Ty.get();
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000941 if (QT.isNull()) {
John McCalla93c9342009-12-07 02:54:59 +0000942 if (TInfo) *TInfo = 0;
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000943 return QualType();
944 }
945
John McCalla93c9342009-12-07 02:54:59 +0000946 TypeSourceInfo *DI = 0;
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000947 if (LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) {
948 QT = LIT->getType();
John McCalla93c9342009-12-07 02:54:59 +0000949 DI = LIT->getTypeSourceInfo();
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000950 }
Mike Stump1eb44332009-09-09 15:08:12 +0000951
John McCalla93c9342009-12-07 02:54:59 +0000952 if (TInfo) *TInfo = DI;
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000953 return QT;
954}
955
Mike Stump98eb8a72009-02-04 22:31:32 +0000956/// GetTypeForDeclarator - Convert the type for the specified
Sebastian Redl8ce35b02009-10-25 21:45:37 +0000957/// declarator to Type instances.
Douglas Gregor402abb52009-05-28 23:31:59 +0000958///
959/// If OwnedDecl is non-NULL, and this declarator's decl-specifier-seq
960/// owns the declaration of a type (e.g., the definition of a struct
961/// type), then *OwnedDecl will receive the owned declaration.
John McCallbf1a0282010-06-04 23:28:52 +0000962///
963/// The result of this call will never be null, but the associated
964/// type may be a null type if there's an unrecoverable error.
965TypeSourceInfo *Sema::GetTypeForDeclarator(Declarator &D, Scope *S,
966 TagDecl **OwnedDecl) {
Douglas Gregor930d8b52009-01-30 22:09:00 +0000967 // Determine the type of the declarator. Not all forms of declarator
968 // have a type.
969 QualType T;
Douglas Gregor05baacb2010-04-12 23:19:01 +0000970 TypeSourceInfo *ReturnTypeInfo = 0;
971
John McCall04a67a62010-02-05 21:31:56 +0000972 llvm::SmallVector<DelayedAttribute,4> FnAttrsFromDeclSpec;
973
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000974 switch (D.getName().getKind()) {
975 case UnqualifiedId::IK_Identifier:
976 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunt0486d742009-11-28 04:44:28 +0000977 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000978 case UnqualifiedId::IK_TemplateId:
John McCall04a67a62010-02-05 21:31:56 +0000979 T = ConvertDeclSpecToType(*this, D, FnAttrsFromDeclSpec);
Chris Lattner5db2bb12009-10-25 18:21:37 +0000980
Douglas Gregor591bd3c2010-02-08 22:07:33 +0000981 if (!D.isInvalidType() && D.getDeclSpec().isTypeSpecOwned()) {
John McCallb3d87482010-08-24 05:47:05 +0000982 TagDecl* Owned = cast<TagDecl>(D.getDeclSpec().getRepAsDecl());
Douglas Gregorb37b6482010-02-12 17:40:34 +0000983 // Owned is embedded if it was defined here, or if it is the
984 // very first (i.e., canonical) declaration of this tag type.
985 Owned->setEmbeddedInDeclarator(Owned->isDefinition() ||
986 Owned->isCanonicalDecl());
Douglas Gregor591bd3c2010-02-08 22:07:33 +0000987 if (OwnedDecl) *OwnedDecl = Owned;
988 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000989 break;
990
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000991 case UnqualifiedId::IK_ConstructorName:
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000992 case UnqualifiedId::IK_ConstructorTemplateId:
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000993 case UnqualifiedId::IK_DestructorName:
Douglas Gregor930d8b52009-01-30 22:09:00 +0000994 // Constructors and destructors don't have return types. Use
Douglas Gregor48026d22010-01-11 18:40:55 +0000995 // "void" instead.
Douglas Gregor930d8b52009-01-30 22:09:00 +0000996 T = Context.VoidTy;
997 break;
Douglas Gregor48026d22010-01-11 18:40:55 +0000998
999 case UnqualifiedId::IK_ConversionFunctionId:
1000 // The result type of a conversion function is the type that it
1001 // converts to.
Douglas Gregor05baacb2010-04-12 23:19:01 +00001002 T = GetTypeFromParser(D.getName().ConversionFunctionId,
John McCallbf1a0282010-06-04 23:28:52 +00001003 &ReturnTypeInfo);
Douglas Gregor48026d22010-01-11 18:40:55 +00001004 break;
Douglas Gregor930d8b52009-01-30 22:09:00 +00001005 }
Douglas Gregordab60ad2010-10-01 18:44:50 +00001006
1007 // Check for auto functions and trailing return type and adjust the
1008 // return type accordingly.
1009 if (getLangOptions().CPlusPlus0x && D.isFunctionDeclarator()) {
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001010 const DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
Douglas Gregordab60ad2010-10-01 18:44:50 +00001011 if (T == Context.UndeducedAutoTy) {
1012 if (FTI.TrailingReturnType) {
1013 T = GetTypeFromParser(ParsedType::getFromOpaquePtr(FTI.TrailingReturnType),
1014 &ReturnTypeInfo);
1015 }
1016 else {
1017 Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
1018 diag::err_auto_missing_trailing_return);
1019 T = Context.IntTy;
1020 D.setInvalidType(true);
1021 }
1022 }
1023 else if (FTI.TrailingReturnType) {
1024 Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
1025 diag::err_trailing_return_without_auto);
1026 D.setInvalidType(true);
1027 }
1028 }
1029
Douglas Gregor1f5f3a42009-12-03 17:10:37 +00001030 if (T.isNull())
John McCallbf1a0282010-06-04 23:28:52 +00001031 return Context.getNullTypeSourceInfo();
Douglas Gregor1f5f3a42009-12-03 17:10:37 +00001032
Anders Carlssonbaf45d32009-06-26 22:18:59 +00001033 if (T == Context.UndeducedAutoTy) {
1034 int Error = -1;
Mike Stump1eb44332009-09-09 15:08:12 +00001035
Anders Carlssonbaf45d32009-06-26 22:18:59 +00001036 switch (D.getContext()) {
1037 case Declarator::KNRTypeListContext:
1038 assert(0 && "K&R type lists aren't allowed in C++");
1039 break;
Anders Carlssonbaf45d32009-06-26 22:18:59 +00001040 case Declarator::PrototypeContext:
1041 Error = 0; // Function prototype
1042 break;
1043 case Declarator::MemberContext:
1044 switch (cast<TagDecl>(CurContext)->getTagKind()) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001045 case TTK_Enum: assert(0 && "unhandled tag kind"); break;
1046 case TTK_Struct: Error = 1; /* Struct member */ break;
1047 case TTK_Union: Error = 2; /* Union member */ break;
1048 case TTK_Class: Error = 3; /* Class member */ break;
Mike Stump1eb44332009-09-09 15:08:12 +00001049 }
Anders Carlssonbaf45d32009-06-26 22:18:59 +00001050 break;
1051 case Declarator::CXXCatchContext:
1052 Error = 4; // Exception declaration
1053 break;
1054 case Declarator::TemplateParamContext:
1055 Error = 5; // Template parameter
1056 break;
1057 case Declarator::BlockLiteralContext:
1058 Error = 6; // Block literal
1059 break;
1060 case Declarator::FileContext:
1061 case Declarator::BlockContext:
1062 case Declarator::ForContext:
1063 case Declarator::ConditionContext:
1064 case Declarator::TypeNameContext:
1065 break;
1066 }
1067
1068 if (Error != -1) {
1069 Diag(D.getDeclSpec().getTypeSpecTypeLoc(), diag::err_auto_not_allowed)
1070 << Error;
1071 T = Context.IntTy;
1072 D.setInvalidType(true);
1073 }
1074 }
Mike Stump1eb44332009-09-09 15:08:12 +00001075
Douglas Gregorcd281c32009-02-28 00:25:32 +00001076 // The name we're declaring, if any.
1077 DeclarationName Name;
1078 if (D.getIdentifier())
1079 Name = D.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00001080
John McCall04a67a62010-02-05 21:31:56 +00001081 llvm::SmallVector<DelayedAttribute,4> FnAttrsFromPreviousChunk;
1082
Mike Stump98eb8a72009-02-04 22:31:32 +00001083 // Walk the DeclTypeInfo, building the recursive type as we go.
1084 // DeclTypeInfos are ordered from the identifier out, which is
1085 // opposite of what we want :).
Sebastian Redl8ce35b02009-10-25 21:45:37 +00001086 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
1087 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1);
Reid Spencer5f016e22007-07-11 17:01:13 +00001088 switch (DeclType.Kind) {
1089 default: assert(0 && "Unknown decltype!");
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001090 case DeclaratorChunk::Paren:
1091 T = BuildParenType(T);
1092 break;
Steve Naroff5618bd42008-08-27 16:04:49 +00001093 case DeclaratorChunk::BlockPointer:
Chris Lattner9af55002009-03-27 04:18:06 +00001094 // If blocks are disabled, emit an error.
1095 if (!LangOpts.Blocks)
1096 Diag(DeclType.Loc, diag::err_blocks_disable);
Mike Stump1eb44332009-09-09 15:08:12 +00001097
John McCall28654742010-06-05 06:41:15 +00001098 T = BuildBlockPointerType(T, D.getIdentifierLoc(), Name);
1099 if (DeclType.Cls.TypeQuals)
1100 T = BuildQualifiedType(T, DeclType.Loc, DeclType.Cls.TypeQuals);
Steve Naroff5618bd42008-08-27 16:04:49 +00001101 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001102 case DeclaratorChunk::Pointer:
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001103 // Verify that we're not building a pointer to pointer to function with
1104 // exception specification.
1105 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1106 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1107 D.setInvalidType(true);
1108 // Build the type anyway.
1109 }
John McCallc12c5bb2010-05-15 11:32:37 +00001110 if (getLangOptions().ObjC1 && T->getAs<ObjCObjectType>()) {
1111 T = Context.getObjCObjectPointerType(T);
John McCall28654742010-06-05 06:41:15 +00001112 if (DeclType.Ptr.TypeQuals)
1113 T = BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals);
Steve Naroff14108da2009-07-10 23:34:53 +00001114 break;
1115 }
John McCall28654742010-06-05 06:41:15 +00001116 T = BuildPointerType(T, DeclType.Loc, Name);
1117 if (DeclType.Ptr.TypeQuals)
1118 T = BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001119 break;
John McCall0953e762009-09-24 19:53:00 +00001120 case DeclaratorChunk::Reference: {
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001121 // Verify that we're not building a reference to pointer to function with
1122 // exception specification.
1123 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1124 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1125 D.setInvalidType(true);
1126 // Build the type anyway.
1127 }
John McCall28654742010-06-05 06:41:15 +00001128 T = BuildReferenceType(T, DeclType.Ref.LValueRef, DeclType.Loc, Name);
1129
1130 Qualifiers Quals;
1131 if (DeclType.Ref.HasRestrict)
1132 T = BuildQualifiedType(T, DeclType.Loc, Qualifiers::Restrict);
Reid Spencer5f016e22007-07-11 17:01:13 +00001133 break;
John McCall0953e762009-09-24 19:53:00 +00001134 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001135 case DeclaratorChunk::Array: {
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001136 // Verify that we're not building an array of pointers to function with
1137 // exception specification.
1138 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1139 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1140 D.setInvalidType(true);
1141 // Build the type anyway.
1142 }
Chris Lattnerfd89bc82008-04-02 01:05:10 +00001143 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner94f81fd2007-08-28 16:54:00 +00001144 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001145 ArrayType::ArraySizeModifier ASM;
1146 if (ATI.isStar)
1147 ASM = ArrayType::Star;
1148 else if (ATI.hasStatic)
1149 ASM = ArrayType::Static;
1150 else
1151 ASM = ArrayType::Normal;
Eli Friedmanf91f5c82009-04-26 21:57:51 +00001152 if (ASM == ArrayType::Star &&
1153 D.getContext() != Declarator::PrototypeContext) {
1154 // FIXME: This check isn't quite right: it allows star in prototypes
1155 // for function definitions, and disallows some edge cases detailed
1156 // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html
1157 Diag(DeclType.Loc, diag::err_array_star_outside_prototype);
1158 ASM = ArrayType::Normal;
1159 D.setInvalidType(true);
1160 }
John McCall0953e762009-09-24 19:53:00 +00001161 T = BuildArrayType(T, ASM, ArraySize,
1162 Qualifiers::fromCVRMask(ATI.TypeQuals),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001163 SourceRange(DeclType.Loc, DeclType.EndLoc), Name);
Reid Spencer5f016e22007-07-11 17:01:13 +00001164 break;
1165 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001166 case DeclaratorChunk::Function: {
Reid Spencer5f016e22007-07-11 17:01:13 +00001167 // If the function declarator has a prototype (i.e. it is not () and
1168 // does not have a K&R-style identifier list), then the arguments are part
1169 // of the type, otherwise the argument list is ().
1170 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Sebastian Redl3cc97262009-05-31 11:47:27 +00001171
Chris Lattnercd881292007-12-19 05:31:29 +00001172 // C99 6.7.5.3p1: The return type may not be a function or array type.
Douglas Gregor58408bc2010-01-11 18:46:21 +00001173 // For conversion functions, we'll diagnose this particular error later.
Douglas Gregor48026d22010-01-11 18:40:55 +00001174 if ((T->isArrayType() || T->isFunctionType()) &&
1175 (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId)) {
Douglas Gregor58408bc2010-01-11 18:46:21 +00001176 Diag(DeclType.Loc, diag::err_func_returning_array_function)
1177 << T->isFunctionType() << T;
Chris Lattnercd881292007-12-19 05:31:29 +00001178 T = Context.IntTy;
1179 D.setInvalidType(true);
1180 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001181
Douglas Gregor5291c3c2010-07-13 08:18:22 +00001182 // cv-qualifiers on return types are pointless except when the type is a
1183 // class type in C++.
1184 if (T.getCVRQualifiers() && D.getDeclSpec().getTypeQualifiers() &&
1185 (!getLangOptions().CPlusPlus ||
1186 (!T->isDependentType() && !T->isRecordType()))) {
1187 unsigned Quals = D.getDeclSpec().getTypeQualifiers();
Douglas Gregorde80ec12010-07-13 08:50:30 +00001188 std::string QualStr;
1189 unsigned NumQuals = 0;
Douglas Gregor5291c3c2010-07-13 08:18:22 +00001190 SourceLocation Loc;
Douglas Gregorde80ec12010-07-13 08:50:30 +00001191 if (Quals & Qualifiers::Const) {
Douglas Gregor5291c3c2010-07-13 08:18:22 +00001192 Loc = D.getDeclSpec().getConstSpecLoc();
Douglas Gregorde80ec12010-07-13 08:50:30 +00001193 ++NumQuals;
1194 QualStr = "const";
Douglas Gregor5291c3c2010-07-13 08:18:22 +00001195 }
Douglas Gregorde80ec12010-07-13 08:50:30 +00001196 if (Quals & Qualifiers::Volatile) {
1197 if (NumQuals == 0) {
1198 Loc = D.getDeclSpec().getVolatileSpecLoc();
1199 QualStr = "volatile";
1200 } else
1201 QualStr += " volatile";
1202 ++NumQuals;
1203 }
1204 if (Quals & Qualifiers::Restrict) {
1205 if (NumQuals == 0) {
1206 Loc = D.getDeclSpec().getRestrictSpecLoc();
1207 QualStr = "restrict";
1208 } else
1209 QualStr += " restrict";
1210 ++NumQuals;
1211 }
1212 assert(NumQuals > 0 && "No known qualifiers?");
1213
Douglas Gregor5291c3c2010-07-13 08:18:22 +00001214 SemaDiagnosticBuilder DB = Diag(Loc, diag::warn_qual_return_type);
Douglas Gregorde80ec12010-07-13 08:50:30 +00001215 DB << QualStr << NumQuals;
Douglas Gregor5291c3c2010-07-13 08:18:22 +00001216 if (Quals & Qualifiers::Const)
1217 DB << FixItHint::CreateRemoval(D.getDeclSpec().getConstSpecLoc());
1218 if (Quals & Qualifiers::Volatile)
1219 DB << FixItHint::CreateRemoval(D.getDeclSpec().getVolatileSpecLoc());
1220 if (Quals & Qualifiers::Restrict)
1221 DB << FixItHint::CreateRemoval(D.getDeclSpec().getRestrictSpecLoc());
1222 }
1223
Douglas Gregor402abb52009-05-28 23:31:59 +00001224 if (getLangOptions().CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) {
1225 // C++ [dcl.fct]p6:
1226 // Types shall not be defined in return or parameter types.
John McCallb3d87482010-08-24 05:47:05 +00001227 TagDecl *Tag = cast<TagDecl>(D.getDeclSpec().getRepAsDecl());
Douglas Gregor402abb52009-05-28 23:31:59 +00001228 if (Tag->isDefinition())
1229 Diag(Tag->getLocation(), diag::err_type_defined_in_result_type)
1230 << Context.getTypeDeclType(Tag);
1231 }
1232
Sebastian Redl3cc97262009-05-31 11:47:27 +00001233 // Exception specs are not allowed in typedefs. Complain, but add it
1234 // anyway.
1235 if (FTI.hasExceptionSpec &&
1236 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
1237 Diag(FTI.getThrowLoc(), diag::err_exception_spec_in_typedef);
1238
John McCall28654742010-06-05 06:41:15 +00001239 if (!FTI.NumArgs && !FTI.isVariadic && !getLangOptions().CPlusPlus) {
1240 // Simple void foo(), where the incoming T is the result type.
1241 T = Context.getFunctionNoProtoType(T);
1242 } else {
1243 // We allow a zero-parameter variadic function in C if the
1244 // function is marked with the "overloadable" attribute. Scan
1245 // for this attribute now.
1246 if (!FTI.NumArgs && FTI.isVariadic && !getLangOptions().CPlusPlus) {
Douglas Gregor965acbb2009-02-18 07:07:28 +00001247 bool Overloadable = false;
1248 for (const AttributeList *Attrs = D.getAttributes();
1249 Attrs; Attrs = Attrs->getNext()) {
1250 if (Attrs->getKind() == AttributeList::AT_overloadable) {
1251 Overloadable = true;
1252 break;
1253 }
1254 }
1255
1256 if (!Overloadable)
1257 Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001258 }
John McCall28654742010-06-05 06:41:15 +00001259
1260 if (FTI.NumArgs && FTI.ArgInfo[0].Param == 0) {
Chris Lattner788b0fd2010-06-23 06:00:24 +00001261 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function
1262 // definition.
John McCall28654742010-06-05 06:41:15 +00001263 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
1264 D.setInvalidType(true);
1265 break;
1266 }
1267
Reid Spencer5f016e22007-07-11 17:01:13 +00001268 // Otherwise, we have a function with an argument list that is
1269 // potentially variadic.
1270 llvm::SmallVector<QualType, 16> ArgTys;
John McCall28654742010-06-05 06:41:15 +00001271 ArgTys.reserve(FTI.NumArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001272
Reid Spencer5f016e22007-07-11 17:01:13 +00001273 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00001274 ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param);
Chris Lattner8123a952008-04-10 02:22:51 +00001275 QualType ArgTy = Param->getType();
Chris Lattner78c75fb2007-07-21 05:30:18 +00001276 assert(!ArgTy.isNull() && "Couldn't parse type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001277
1278 // Adjust the parameter type.
Douglas Gregorbeb58cb2009-03-23 23:17:00 +00001279 assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001280
Reid Spencer5f016e22007-07-11 17:01:13 +00001281 // Look for 'void'. void is allowed only as a single argument to a
1282 // function with no other parameters (C99 6.7.5.3p10). We record
Douglas Gregor72564e72009-02-26 23:50:07 +00001283 // int(void) as a FunctionProtoType with an empty argument list.
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001284 if (ArgTy->isVoidType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001285 // If this is something like 'float(int, void)', reject it. 'void'
1286 // is an incomplete type (C99 6.2.5p19) and function decls cannot
1287 // have arguments of incomplete type.
1288 if (FTI.NumArgs != 1 || FTI.isVariadic) {
1289 Diag(DeclType.Loc, diag::err_void_only_param);
Chris Lattner2ff54262007-07-21 05:18:12 +00001290 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00001291 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00001292 } else if (FTI.ArgInfo[i].Ident) {
1293 // Reject, but continue to parse 'int(void abc)'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001294 Diag(FTI.ArgInfo[i].IdentLoc,
Chris Lattner4565d4e2007-07-21 05:26:43 +00001295 diag::err_param_with_void_type);
Chris Lattner2ff54262007-07-21 05:18:12 +00001296 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00001297 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00001298 } else {
1299 // Reject, but continue to parse 'float(const void)'.
John McCall0953e762009-09-24 19:53:00 +00001300 if (ArgTy.hasQualifiers())
Chris Lattner2ff54262007-07-21 05:18:12 +00001301 Diag(DeclType.Loc, diag::err_void_param_qualified);
Mike Stump1eb44332009-09-09 15:08:12 +00001302
Chris Lattner2ff54262007-07-21 05:18:12 +00001303 // Do not add 'void' to the ArgTys list.
1304 break;
1305 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001306 } else if (!FTI.hasPrototype) {
1307 if (ArgTy->isPromotableIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00001308 ArgTy = Context.getPromotedIntegerType(ArgTy);
John McCall183700f2009-09-21 23:43:11 +00001309 } else if (const BuiltinType* BTy = ArgTy->getAs<BuiltinType>()) {
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001310 if (BTy->getKind() == BuiltinType::Float)
1311 ArgTy = Context.DoubleTy;
1312 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001313 }
Fariborz Jahanian56a965c2010-09-08 21:36:35 +00001314
John McCall54e14c42009-10-22 22:37:11 +00001315 ArgTys.push_back(ArgTy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001316 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001317
1318 llvm::SmallVector<QualType, 4> Exceptions;
1319 Exceptions.reserve(FTI.NumExceptions);
Mike Stump1eb44332009-09-09 15:08:12 +00001320 for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001321 // FIXME: Preserve type source info.
1322 QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty);
Sebastian Redlef65f062009-05-29 18:02:33 +00001323 // Check that the type is valid for an exception spec, and drop it if
1324 // not.
1325 if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1326 Exceptions.push_back(ET);
1327 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001328
Jay Foadbeaaccd2009-05-21 09:52:38 +00001329 T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(),
Sebastian Redl465226e2009-05-27 22:11:52 +00001330 FTI.isVariadic, FTI.TypeQuals,
1331 FTI.hasExceptionSpec,
1332 FTI.hasAnyExceptionSpec,
Douglas Gregorce056bc2010-02-21 22:15:06 +00001333 Exceptions.size(), Exceptions.data(),
Rafael Espindola264ba482010-03-30 20:24:48 +00001334 FunctionType::ExtInfo());
Reid Spencer5f016e22007-07-11 17:01:13 +00001335 }
John McCall04a67a62010-02-05 21:31:56 +00001336
1337 // For GCC compatibility, we allow attributes that apply only to
1338 // function types to be placed on a function's return type
1339 // instead (as long as that type doesn't happen to be function
1340 // or function-pointer itself).
1341 ProcessDelayedFnAttrs(*this, T, FnAttrsFromPreviousChunk);
1342
Reid Spencer5f016e22007-07-11 17:01:13 +00001343 break;
1344 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001345 case DeclaratorChunk::MemberPointer:
1346 // The scope spec must refer to a class, or be dependent.
Abramo Bagnara7bd06762010-08-13 12:56:25 +00001347 CXXScopeSpec &SS = DeclType.Mem.Scope();
Sebastian Redlf30208a2009-01-24 21:16:55 +00001348 QualType ClsType;
Abramo Bagnara7bd06762010-08-13 12:56:25 +00001349 if (SS.isInvalid()) {
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00001350 // Avoid emitting extra errors if we already errored on the scope.
1351 D.setInvalidType(true);
Abramo Bagnara7bd06762010-08-13 12:56:25 +00001352 } else if (isDependentScopeSpecifier(SS) ||
1353 dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS))) {
Mike Stump1eb44332009-09-09 15:08:12 +00001354 NestedNameSpecifier *NNS
Abramo Bagnara7bd06762010-08-13 12:56:25 +00001355 = static_cast<NestedNameSpecifier*>(SS.getScopeRep());
Douglas Gregor87c12c42009-11-04 16:49:01 +00001356 NestedNameSpecifier *NNSPrefix = NNS->getPrefix();
1357 switch (NNS->getKind()) {
1358 case NestedNameSpecifier::Identifier:
Abramo Bagnara7bd06762010-08-13 12:56:25 +00001359 ClsType = Context.getDependentNameType(ETK_None, NNSPrefix,
Douglas Gregor4a2023f2010-03-31 20:19:30 +00001360 NNS->getAsIdentifier());
Douglas Gregor87c12c42009-11-04 16:49:01 +00001361 break;
1362
1363 case NestedNameSpecifier::Namespace:
1364 case NestedNameSpecifier::Global:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001365 llvm_unreachable("Nested-name-specifier must name a type");
Douglas Gregor87c12c42009-11-04 16:49:01 +00001366 break;
Abramo Bagnara7bd06762010-08-13 12:56:25 +00001367
Douglas Gregor87c12c42009-11-04 16:49:01 +00001368 case NestedNameSpecifier::TypeSpec:
1369 case NestedNameSpecifier::TypeSpecWithTemplate:
1370 ClsType = QualType(NNS->getAsType(), 0);
Abramo Bagnara7bd06762010-08-13 12:56:25 +00001371 // Note: if NNS is dependent, then its prefix (if any) is already
1372 // included in ClsType; this does not hold if the NNS is
1373 // nondependent: in this case (if there is indeed a prefix)
1374 // ClsType needs to be wrapped into an elaborated type.
1375 if (NNSPrefix && !NNS->isDependent())
1376 ClsType = Context.getElaboratedType(ETK_None, NNSPrefix, ClsType);
Douglas Gregor87c12c42009-11-04 16:49:01 +00001377 break;
1378 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001379 } else {
Douglas Gregor949bf692009-06-09 22:17:39 +00001380 Diag(DeclType.Mem.Scope().getBeginLoc(),
1381 diag::err_illegal_decl_mempointer_in_nonclass)
1382 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
1383 << DeclType.Mem.Scope().getRange();
Sebastian Redlf30208a2009-01-24 21:16:55 +00001384 D.setInvalidType(true);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001385 }
1386
Douglas Gregor949bf692009-06-09 22:17:39 +00001387 if (!ClsType.isNull())
John McCall28654742010-06-05 06:41:15 +00001388 T = BuildMemberPointerType(T, ClsType, DeclType.Loc, D.getIdentifier());
Douglas Gregor949bf692009-06-09 22:17:39 +00001389 if (T.isNull()) {
1390 T = Context.IntTy;
Sebastian Redlf30208a2009-01-24 21:16:55 +00001391 D.setInvalidType(true);
John McCall28654742010-06-05 06:41:15 +00001392 } else if (DeclType.Mem.TypeQuals) {
1393 T = BuildQualifiedType(T, DeclType.Loc, DeclType.Mem.TypeQuals);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001394 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001395 break;
1396 }
1397
Douglas Gregorcd281c32009-02-28 00:25:32 +00001398 if (T.isNull()) {
1399 D.setInvalidType(true);
1400 T = Context.IntTy;
1401 }
1402
John McCall04a67a62010-02-05 21:31:56 +00001403 DiagnoseDelayedFnAttrs(*this, FnAttrsFromPreviousChunk);
1404
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001405 // See if there are any attributes on this declarator chunk.
1406 if (const AttributeList *AL = DeclType.getAttrs())
Charles Davis328ce342010-02-24 02:27:18 +00001407 ProcessTypeAttributeList(*this, T, false, AL, FnAttrsFromPreviousChunk);
Reid Spencer5f016e22007-07-11 17:01:13 +00001408 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001409
1410 if (getLangOptions().CPlusPlus && T->isFunctionType()) {
John McCall183700f2009-09-21 23:43:11 +00001411 const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>();
Chris Lattner778ed742009-10-25 17:36:50 +00001412 assert(FnTy && "Why oh why is there not a FunctionProtoType here?");
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001413
Douglas Gregor708f3b82010-10-14 22:03:51 +00001414 // C++ 8.3.5p4:
1415 // A cv-qualifier-seq shall only be part of the function type
1416 // for a nonstatic member function, the function type to which a pointer
1417 // to member refers, or the top-level function type of a function typedef
1418 // declaration.
John McCall613ef3d2010-10-19 01:54:45 +00001419 bool FreeFunction;
1420 if (!D.getCXXScopeSpec().isSet()) {
1421 FreeFunction = (D.getContext() != Declarator::MemberContext ||
1422 D.getDeclSpec().isFriendSpecified());
1423 } else {
1424 DeclContext *DC = computeDeclContext(D.getCXXScopeSpec());
1425 FreeFunction = (DC && !DC->isRecord());
1426 }
1427
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001428 if (FnTy->getTypeQuals() != 0 &&
1429 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
Sebastian Redlc61bb202010-07-09 21:26:08 +00001430 (FreeFunction ||
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001431 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001432 if (D.isFunctionDeclarator())
1433 Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
1434 else
1435 Diag(D.getIdentifierLoc(),
Sebastian Redlc61bb202010-07-09 21:26:08 +00001436 diag::err_invalid_qualified_typedef_function_type_use)
1437 << FreeFunction;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001438
1439 // Strip the cv-quals from the type.
1440 T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
Douglas Gregorce056bc2010-02-21 22:15:06 +00001441 FnTy->getNumArgs(), FnTy->isVariadic(), 0,
Rafael Espindola264ba482010-03-30 20:24:48 +00001442 false, false, 0, 0, FunctionType::ExtInfo());
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001443 }
1444 }
Mike Stump1eb44332009-09-09 15:08:12 +00001445
Sebastian Redl73780122010-06-09 21:19:43 +00001446 // If there's a constexpr specifier, treat it as a top-level const.
1447 if (D.getDeclSpec().isConstexprSpecified()) {
1448 T.addConst();
1449 }
1450
John McCall04a67a62010-02-05 21:31:56 +00001451 // Process any function attributes we might have delayed from the
1452 // declaration-specifiers.
1453 ProcessDelayedFnAttrs(*this, T, FnAttrsFromDeclSpec);
1454
1455 // If there were any type attributes applied to the decl itself, not
1456 // the type, apply them to the result type. But don't do this for
1457 // block-literal expressions, which are parsed wierdly.
1458 if (D.getContext() != Declarator::BlockLiteralContext)
1459 if (const AttributeList *Attrs = D.getAttributes())
Charles Davis328ce342010-02-24 02:27:18 +00001460 ProcessTypeAttributeList(*this, T, false, Attrs,
1461 FnAttrsFromPreviousChunk);
John McCall04a67a62010-02-05 21:31:56 +00001462
1463 DiagnoseDelayedFnAttrs(*this, FnAttrsFromPreviousChunk);
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001464
John McCallbf1a0282010-06-04 23:28:52 +00001465 if (T.isNull())
1466 return Context.getNullTypeSourceInfo();
1467 else if (D.isInvalidType())
1468 return Context.getTrivialTypeSourceInfo(T);
1469 return GetTypeSourceInfoForDeclarator(D, T, ReturnTypeInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +00001470}
1471
John McCall51bd8032009-10-18 01:05:36 +00001472namespace {
1473 class TypeSpecLocFiller : public TypeLocVisitor<TypeSpecLocFiller> {
1474 const DeclSpec &DS;
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001475
John McCall51bd8032009-10-18 01:05:36 +00001476 public:
1477 TypeSpecLocFiller(const DeclSpec &DS) : DS(DS) {}
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001478
John McCall51bd8032009-10-18 01:05:36 +00001479 void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
1480 Visit(TL.getUnqualifiedLoc());
1481 }
1482 void VisitTypedefTypeLoc(TypedefTypeLoc TL) {
1483 TL.setNameLoc(DS.getTypeSpecTypeLoc());
1484 }
1485 void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
1486 TL.setNameLoc(DS.getTypeSpecTypeLoc());
John McCallc12c5bb2010-05-15 11:32:37 +00001487 }
1488 void VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
1489 // Handle the base type, which might not have been written explicitly.
1490 if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) {
1491 TL.setHasBaseTypeAsWritten(false);
1492 TL.getBaseLoc().initialize(SourceLocation());
1493 } else {
1494 TL.setHasBaseTypeAsWritten(true);
1495 Visit(TL.getBaseLoc());
1496 }
Argyrios Kyrtzidiseb667592009-09-29 19:45:22 +00001497
John McCallc12c5bb2010-05-15 11:32:37 +00001498 // Protocol qualifiers.
John McCall54e14c42009-10-22 22:37:11 +00001499 if (DS.getProtocolQualifiers()) {
1500 assert(TL.getNumProtocols() > 0);
1501 assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
1502 TL.setLAngleLoc(DS.getProtocolLAngleLoc());
1503 TL.setRAngleLoc(DS.getSourceRange().getEnd());
1504 for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i)
1505 TL.setProtocolLoc(i, DS.getProtocolLocs()[i]);
1506 } else {
1507 assert(TL.getNumProtocols() == 0);
1508 TL.setLAngleLoc(SourceLocation());
1509 TL.setRAngleLoc(SourceLocation());
1510 }
1511 }
1512 void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
John McCall54e14c42009-10-22 22:37:11 +00001513 TL.setStarLoc(SourceLocation());
John McCallc12c5bb2010-05-15 11:32:37 +00001514 Visit(TL.getPointeeLoc());
John McCall51bd8032009-10-18 01:05:36 +00001515 }
John McCall833ca992009-10-29 08:12:44 +00001516 void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) {
John McCalla93c9342009-12-07 02:54:59 +00001517 TypeSourceInfo *TInfo = 0;
John McCallb3d87482010-08-24 05:47:05 +00001518 Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
John McCall833ca992009-10-29 08:12:44 +00001519
1520 // If we got no declarator info from previous Sema routines,
1521 // just fill with the typespec loc.
John McCalla93c9342009-12-07 02:54:59 +00001522 if (!TInfo) {
John McCall833ca992009-10-29 08:12:44 +00001523 TL.initialize(DS.getTypeSpecTypeLoc());
1524 return;
1525 }
1526
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00001527 TypeLoc OldTL = TInfo->getTypeLoc();
1528 if (TInfo->getType()->getAs<ElaboratedType>()) {
1529 ElaboratedTypeLoc ElabTL = cast<ElaboratedTypeLoc>(OldTL);
1530 TemplateSpecializationTypeLoc NamedTL =
1531 cast<TemplateSpecializationTypeLoc>(ElabTL.getNamedTypeLoc());
1532 TL.copy(NamedTL);
1533 }
1534 else
1535 TL.copy(cast<TemplateSpecializationTypeLoc>(OldTL));
John McCall833ca992009-10-29 08:12:44 +00001536 }
John McCallcfb708c2010-01-13 20:03:27 +00001537 void VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
1538 assert(DS.getTypeSpecType() == DeclSpec::TST_typeofExpr);
1539 TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
1540 TL.setParensRange(DS.getTypeofParensRange());
1541 }
1542 void VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
1543 assert(DS.getTypeSpecType() == DeclSpec::TST_typeofType);
1544 TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
1545 TL.setParensRange(DS.getTypeofParensRange());
John McCallb3d87482010-08-24 05:47:05 +00001546 assert(DS.getRepAsType());
John McCallcfb708c2010-01-13 20:03:27 +00001547 TypeSourceInfo *TInfo = 0;
John McCallb3d87482010-08-24 05:47:05 +00001548 Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
John McCallcfb708c2010-01-13 20:03:27 +00001549 TL.setUnderlyingTInfo(TInfo);
1550 }
Douglas Gregorddf889a2010-01-18 18:04:31 +00001551 void VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
1552 // By default, use the source location of the type specifier.
1553 TL.setBuiltinLoc(DS.getTypeSpecTypeLoc());
1554 if (TL.needsExtraLocalData()) {
1555 // Set info for the written builtin specifiers.
1556 TL.getWrittenBuiltinSpecs() = DS.getWrittenBuiltinSpecs();
1557 // Try to have a meaningful source location.
1558 if (TL.getWrittenSignSpec() != TSS_unspecified)
1559 // Sign spec loc overrides the others (e.g., 'unsigned long').
1560 TL.setBuiltinLoc(DS.getTypeSpecSignLoc());
1561 else if (TL.getWrittenWidthSpec() != TSW_unspecified)
1562 // Width spec loc overrides type spec loc (e.g., 'short int').
1563 TL.setBuiltinLoc(DS.getTypeSpecWidthLoc());
1564 }
1565 }
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00001566 void VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
1567 ElaboratedTypeKeyword Keyword
1568 = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
Nico Weber253e80b2010-11-22 10:30:56 +00001569 if (DS.getTypeSpecType() == TST_typename) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00001570 TypeSourceInfo *TInfo = 0;
John McCallb3d87482010-08-24 05:47:05 +00001571 Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00001572 if (TInfo) {
1573 TL.copy(cast<ElaboratedTypeLoc>(TInfo->getTypeLoc()));
1574 return;
1575 }
1576 }
1577 TL.setKeywordLoc(Keyword != ETK_None
1578 ? DS.getTypeSpecTypeLoc()
1579 : SourceLocation());
1580 const CXXScopeSpec& SS = DS.getTypeSpecScope();
1581 TL.setQualifierRange(SS.isEmpty() ? SourceRange(): SS.getRange());
1582 Visit(TL.getNextTypeLoc().getUnqualifiedLoc());
1583 }
1584 void VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
1585 ElaboratedTypeKeyword Keyword
1586 = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
Nico Weber253e80b2010-11-22 10:30:56 +00001587 if (DS.getTypeSpecType() == TST_typename) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00001588 TypeSourceInfo *TInfo = 0;
John McCallb3d87482010-08-24 05:47:05 +00001589 Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00001590 if (TInfo) {
1591 TL.copy(cast<DependentNameTypeLoc>(TInfo->getTypeLoc()));
1592 return;
1593 }
1594 }
1595 TL.setKeywordLoc(Keyword != ETK_None
1596 ? DS.getTypeSpecTypeLoc()
1597 : SourceLocation());
1598 const CXXScopeSpec& SS = DS.getTypeSpecScope();
1599 TL.setQualifierRange(SS.isEmpty() ? SourceRange() : SS.getRange());
1600 // FIXME: load appropriate source location.
1601 TL.setNameLoc(DS.getTypeSpecTypeLoc());
1602 }
John McCall33500952010-06-11 00:33:02 +00001603 void VisitDependentTemplateSpecializationTypeLoc(
1604 DependentTemplateSpecializationTypeLoc TL) {
1605 ElaboratedTypeKeyword Keyword
1606 = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
1607 if (Keyword == ETK_Typename) {
1608 TypeSourceInfo *TInfo = 0;
John McCallb3d87482010-08-24 05:47:05 +00001609 Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
John McCall33500952010-06-11 00:33:02 +00001610 if (TInfo) {
1611 TL.copy(cast<DependentTemplateSpecializationTypeLoc>(
1612 TInfo->getTypeLoc()));
1613 return;
1614 }
1615 }
1616 TL.initializeLocal(SourceLocation());
1617 TL.setKeywordLoc(Keyword != ETK_None
1618 ? DS.getTypeSpecTypeLoc()
1619 : SourceLocation());
1620 const CXXScopeSpec& SS = DS.getTypeSpecScope();
1621 TL.setQualifierRange(SS.isEmpty() ? SourceRange() : SS.getRange());
1622 // FIXME: load appropriate source location.
1623 TL.setNameLoc(DS.getTypeSpecTypeLoc());
1624 }
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00001625
John McCall51bd8032009-10-18 01:05:36 +00001626 void VisitTypeLoc(TypeLoc TL) {
1627 // FIXME: add other typespec types and change this to an assert.
1628 TL.initialize(DS.getTypeSpecTypeLoc());
1629 }
1630 };
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001631
John McCall51bd8032009-10-18 01:05:36 +00001632 class DeclaratorLocFiller : public TypeLocVisitor<DeclaratorLocFiller> {
1633 const DeclaratorChunk &Chunk;
1634
1635 public:
1636 DeclaratorLocFiller(const DeclaratorChunk &Chunk) : Chunk(Chunk) {}
1637
1638 void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001639 llvm_unreachable("qualified type locs not expected here!");
John McCall51bd8032009-10-18 01:05:36 +00001640 }
1641
1642 void VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
1643 assert(Chunk.Kind == DeclaratorChunk::BlockPointer);
1644 TL.setCaretLoc(Chunk.Loc);
1645 }
1646 void VisitPointerTypeLoc(PointerTypeLoc TL) {
1647 assert(Chunk.Kind == DeclaratorChunk::Pointer);
1648 TL.setStarLoc(Chunk.Loc);
1649 }
1650 void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
1651 assert(Chunk.Kind == DeclaratorChunk::Pointer);
1652 TL.setStarLoc(Chunk.Loc);
1653 }
1654 void VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
1655 assert(Chunk.Kind == DeclaratorChunk::MemberPointer);
1656 TL.setStarLoc(Chunk.Loc);
1657 // FIXME: nested name specifier
1658 }
1659 void VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
1660 assert(Chunk.Kind == DeclaratorChunk::Reference);
John McCall54e14c42009-10-22 22:37:11 +00001661 // 'Amp' is misleading: this might have been originally
1662 /// spelled with AmpAmp.
John McCall51bd8032009-10-18 01:05:36 +00001663 TL.setAmpLoc(Chunk.Loc);
1664 }
1665 void VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
1666 assert(Chunk.Kind == DeclaratorChunk::Reference);
1667 assert(!Chunk.Ref.LValueRef);
1668 TL.setAmpAmpLoc(Chunk.Loc);
1669 }
1670 void VisitArrayTypeLoc(ArrayTypeLoc TL) {
1671 assert(Chunk.Kind == DeclaratorChunk::Array);
1672 TL.setLBracketLoc(Chunk.Loc);
1673 TL.setRBracketLoc(Chunk.EndLoc);
1674 TL.setSizeExpr(static_cast<Expr*>(Chunk.Arr.NumElts));
1675 }
1676 void VisitFunctionTypeLoc(FunctionTypeLoc TL) {
1677 assert(Chunk.Kind == DeclaratorChunk::Function);
1678 TL.setLParenLoc(Chunk.Loc);
1679 TL.setRParenLoc(Chunk.EndLoc);
Douglas Gregordab60ad2010-10-01 18:44:50 +00001680 TL.setTrailingReturn(!!Chunk.Fun.TrailingReturnType);
John McCall51bd8032009-10-18 01:05:36 +00001681
1682 const DeclaratorChunk::FunctionTypeInfo &FTI = Chunk.Fun;
John McCall54e14c42009-10-22 22:37:11 +00001683 for (unsigned i = 0, e = TL.getNumArgs(), tpi = 0; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00001684 ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param);
John McCall54e14c42009-10-22 22:37:11 +00001685 TL.setArg(tpi++, Param);
John McCall51bd8032009-10-18 01:05:36 +00001686 }
1687 // FIXME: exception specs
1688 }
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001689 void VisitParenTypeLoc(ParenTypeLoc TL) {
1690 assert(Chunk.Kind == DeclaratorChunk::Paren);
1691 TL.setLParenLoc(Chunk.Loc);
1692 TL.setRParenLoc(Chunk.EndLoc);
1693 }
John McCall51bd8032009-10-18 01:05:36 +00001694
1695 void VisitTypeLoc(TypeLoc TL) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001696 llvm_unreachable("unsupported TypeLoc kind in declarator!");
John McCall51bd8032009-10-18 01:05:36 +00001697 }
1698 };
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001699}
1700
John McCalla93c9342009-12-07 02:54:59 +00001701/// \brief Create and instantiate a TypeSourceInfo with type source information.
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001702///
1703/// \param T QualType referring to the type as written in source code.
Douglas Gregor05baacb2010-04-12 23:19:01 +00001704///
1705/// \param ReturnTypeInfo For declarators whose return type does not show
1706/// up in the normal place in the declaration specifiers (such as a C++
1707/// conversion function), this pointer will refer to a type source information
1708/// for that return type.
John McCalla93c9342009-12-07 02:54:59 +00001709TypeSourceInfo *
Douglas Gregor05baacb2010-04-12 23:19:01 +00001710Sema::GetTypeSourceInfoForDeclarator(Declarator &D, QualType T,
1711 TypeSourceInfo *ReturnTypeInfo) {
John McCalla93c9342009-12-07 02:54:59 +00001712 TypeSourceInfo *TInfo = Context.CreateTypeSourceInfo(T);
1713 UnqualTypeLoc CurrTL = TInfo->getTypeLoc().getUnqualifiedLoc();
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001714
Sebastian Redl8ce35b02009-10-25 21:45:37 +00001715 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
John McCall51bd8032009-10-18 01:05:36 +00001716 DeclaratorLocFiller(D.getTypeObject(i)).Visit(CurrTL);
1717 CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001718 }
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001719
John McCallb3d87482010-08-24 05:47:05 +00001720 // If we have different source information for the return type, use
1721 // that. This really only applies to C++ conversion functions.
1722 if (ReturnTypeInfo) {
Douglas Gregor05baacb2010-04-12 23:19:01 +00001723 TypeLoc TL = ReturnTypeInfo->getTypeLoc();
1724 assert(TL.getFullDataSize() == CurrTL.getFullDataSize());
1725 memcpy(CurrTL.getOpaqueData(), TL.getOpaqueData(), TL.getFullDataSize());
John McCallb3d87482010-08-24 05:47:05 +00001726 } else {
1727 TypeSpecLocFiller(D.getDeclSpec()).Visit(CurrTL);
Douglas Gregor05baacb2010-04-12 23:19:01 +00001728 }
1729
John McCalla93c9342009-12-07 02:54:59 +00001730 return TInfo;
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001731}
1732
John McCalla93c9342009-12-07 02:54:59 +00001733/// \brief Create a LocInfoType to hold the given QualType and TypeSourceInfo.
John McCallb3d87482010-08-24 05:47:05 +00001734ParsedType Sema::CreateParsedType(QualType T, TypeSourceInfo *TInfo) {
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001735 // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser
1736 // and Sema during declaration parsing. Try deallocating/caching them when
1737 // it's appropriate, instead of allocating them and keeping them around.
Douglas Gregoreb0eb492010-12-10 08:12:03 +00001738 LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType),
1739 TypeAlignment);
John McCalla93c9342009-12-07 02:54:59 +00001740 new (LocT) LocInfoType(T, TInfo);
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001741 assert(LocT->getTypeClass() != T->getTypeClass() &&
1742 "LocInfoType's TypeClass conflicts with an existing Type class");
John McCallb3d87482010-08-24 05:47:05 +00001743 return ParsedType::make(QualType(LocT, 0));
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001744}
1745
1746void LocInfoType::getAsStringInternal(std::string &Str,
1747 const PrintingPolicy &Policy) const {
Argyrios Kyrtzidis35d44e52009-08-19 01:46:06 +00001748 assert(false && "LocInfoType leaked into the type system; an opaque TypeTy*"
1749 " was used directly instead of getting the QualType through"
1750 " GetTypeFromParser");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001751}
1752
John McCallf312b1e2010-08-26 23:41:50 +00001753TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001754 // C99 6.7.6: Type names have no identifier. This is already validated by
1755 // the parser.
1756 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00001757
Douglas Gregor402abb52009-05-28 23:31:59 +00001758 TagDecl *OwnedTag = 0;
John McCallbf1a0282010-06-04 23:28:52 +00001759 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S, &OwnedTag);
1760 QualType T = TInfo->getType();
Chris Lattner5153ee62009-04-25 08:47:54 +00001761 if (D.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +00001762 return true;
Steve Naroff5912a352007-08-28 20:14:24 +00001763
Douglas Gregor402abb52009-05-28 23:31:59 +00001764 if (getLangOptions().CPlusPlus) {
1765 // Check that there are no default arguments (C++ only).
Douglas Gregor6d6eb572008-05-07 04:49:29 +00001766 CheckExtraCXXDefaultArguments(D);
1767
Douglas Gregor402abb52009-05-28 23:31:59 +00001768 // C++0x [dcl.type]p3:
1769 // A type-specifier-seq shall not define a class or enumeration
1770 // unless it appears in the type-id of an alias-declaration
1771 // (7.1.3).
1772 if (OwnedTag && OwnedTag->isDefinition())
1773 Diag(OwnedTag->getLocation(), diag::err_type_defined_in_type_specifier)
1774 << Context.getTypeDeclType(OwnedTag);
1775 }
1776
John McCallb3d87482010-08-24 05:47:05 +00001777 return CreateParsedType(T, TInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +00001778}
1779
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001780
1781
1782//===----------------------------------------------------------------------===//
1783// Type Attribute Processing
1784//===----------------------------------------------------------------------===//
1785
1786/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
1787/// specified type. The attribute contains 1 argument, the id of the address
1788/// space for the type.
Mike Stump1eb44332009-09-09 15:08:12 +00001789static void HandleAddressSpaceTypeAttribute(QualType &Type,
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001790 const AttributeList &Attr, Sema &S){
John McCall0953e762009-09-24 19:53:00 +00001791
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001792 // If this type is already address space qualified, reject it.
1793 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
1794 // for two or more different address spaces."
1795 if (Type.getAddressSpace()) {
1796 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
Abramo Bagnarae215f722010-04-30 13:10:51 +00001797 Attr.setInvalid();
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001798 return;
1799 }
Mike Stump1eb44332009-09-09 15:08:12 +00001800
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001801 // Check the attribute arguments.
1802 if (Attr.getNumArgs() != 1) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +00001803 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Abramo Bagnarae215f722010-04-30 13:10:51 +00001804 Attr.setInvalid();
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001805 return;
1806 }
1807 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
1808 llvm::APSInt addrSpace(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001809 if (ASArgExpr->isTypeDependent() || ASArgExpr->isValueDependent() ||
1810 !ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001811 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
1812 << ASArgExpr->getSourceRange();
Abramo Bagnarae215f722010-04-30 13:10:51 +00001813 Attr.setInvalid();
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001814 return;
1815 }
1816
John McCallefadb772009-07-28 06:52:18 +00001817 // Bounds checking.
1818 if (addrSpace.isSigned()) {
1819 if (addrSpace.isNegative()) {
1820 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative)
1821 << ASArgExpr->getSourceRange();
Abramo Bagnarae215f722010-04-30 13:10:51 +00001822 Attr.setInvalid();
John McCallefadb772009-07-28 06:52:18 +00001823 return;
1824 }
1825 addrSpace.setIsSigned(false);
1826 }
1827 llvm::APSInt max(addrSpace.getBitWidth());
John McCall0953e762009-09-24 19:53:00 +00001828 max = Qualifiers::MaxAddressSpace;
John McCallefadb772009-07-28 06:52:18 +00001829 if (addrSpace > max) {
1830 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high)
John McCall0953e762009-09-24 19:53:00 +00001831 << Qualifiers::MaxAddressSpace << ASArgExpr->getSourceRange();
Abramo Bagnarae215f722010-04-30 13:10:51 +00001832 Attr.setInvalid();
John McCallefadb772009-07-28 06:52:18 +00001833 return;
1834 }
1835
Mike Stump1eb44332009-09-09 15:08:12 +00001836 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001837 Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001838}
1839
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001840/// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the
1841/// specified type. The attribute contains 1 argument, weak or strong.
Mike Stump1eb44332009-09-09 15:08:12 +00001842static void HandleObjCGCTypeAttribute(QualType &Type,
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001843 const AttributeList &Attr, Sema &S) {
John McCall0953e762009-09-24 19:53:00 +00001844 if (Type.getObjCGCAttr() != Qualifiers::GCNone) {
Fariborz Jahanian5934e752009-02-18 18:52:41 +00001845 S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc);
Abramo Bagnarae215f722010-04-30 13:10:51 +00001846 Attr.setInvalid();
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001847 return;
1848 }
Mike Stump1eb44332009-09-09 15:08:12 +00001849
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001850 // Check the attribute arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001851 if (!Attr.getParameterName()) {
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001852 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1853 << "objc_gc" << 1;
Abramo Bagnarae215f722010-04-30 13:10:51 +00001854 Attr.setInvalid();
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001855 return;
1856 }
John McCall0953e762009-09-24 19:53:00 +00001857 Qualifiers::GC GCAttr;
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001858 if (Attr.getNumArgs() != 0) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001859 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Abramo Bagnarae215f722010-04-30 13:10:51 +00001860 Attr.setInvalid();
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001861 return;
1862 }
Mike Stump1eb44332009-09-09 15:08:12 +00001863 if (Attr.getParameterName()->isStr("weak"))
John McCall0953e762009-09-24 19:53:00 +00001864 GCAttr = Qualifiers::Weak;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001865 else if (Attr.getParameterName()->isStr("strong"))
John McCall0953e762009-09-24 19:53:00 +00001866 GCAttr = Qualifiers::Strong;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001867 else {
1868 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1869 << "objc_gc" << Attr.getParameterName();
Abramo Bagnarae215f722010-04-30 13:10:51 +00001870 Attr.setInvalid();
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001871 return;
1872 }
Mike Stump1eb44332009-09-09 15:08:12 +00001873
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001874 Type = S.Context.getObjCGCQualType(Type, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001875}
1876
John McCall04a67a62010-02-05 21:31:56 +00001877/// Process an individual function attribute. Returns true if the
1878/// attribute does not make sense to apply to this type.
1879bool ProcessFnAttr(Sema &S, QualType &Type, const AttributeList &Attr) {
1880 if (Attr.getKind() == AttributeList::AT_noreturn) {
1881 // Complain immediately if the arg count is wrong.
1882 if (Attr.getNumArgs() != 0) {
1883 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Abramo Bagnarae215f722010-04-30 13:10:51 +00001884 Attr.setInvalid();
John McCall04a67a62010-02-05 21:31:56 +00001885 return false;
1886 }
Mike Stump24556362009-07-25 21:26:53 +00001887
John McCall04a67a62010-02-05 21:31:56 +00001888 // Delay if this is not a function or pointer to block.
1889 if (!Type->isFunctionPointerType()
1890 && !Type->isBlockPointerType()
Douglas Gregorafac01d2010-09-01 16:29:03 +00001891 && !Type->isFunctionType()
1892 && !Type->isMemberFunctionPointerType())
John McCall04a67a62010-02-05 21:31:56 +00001893 return true;
Ted Kremenek58f281f2010-08-19 00:52:13 +00001894
John McCall04a67a62010-02-05 21:31:56 +00001895 // Otherwise we can process right away.
1896 Type = S.Context.getNoReturnType(Type);
1897 return false;
1898 }
Mike Stump24556362009-07-25 21:26:53 +00001899
Rafael Espindola425ef722010-03-30 22:15:11 +00001900 if (Attr.getKind() == AttributeList::AT_regparm) {
1901 // The warning is emitted elsewhere
1902 if (Attr.getNumArgs() != 1) {
1903 return false;
1904 }
1905
1906 // Delay if this is not a function or pointer to block.
1907 if (!Type->isFunctionPointerType()
1908 && !Type->isBlockPointerType()
Douglas Gregorafac01d2010-09-01 16:29:03 +00001909 && !Type->isFunctionType()
1910 && !Type->isMemberFunctionPointerType())
Rafael Espindola425ef722010-03-30 22:15:11 +00001911 return true;
1912
1913 // Otherwise we can process right away.
1914 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1915 llvm::APSInt NumParams(32);
1916
1917 // The warning is emitted elsewhere
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001918 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
1919 !NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context))
Rafael Espindola425ef722010-03-30 22:15:11 +00001920 return false;
1921
John McCall1e030eb2010-10-14 01:57:10 +00001922 if (S.Context.Target.getRegParmMax() == 0) {
1923 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
1924 << NumParamsExpr->getSourceRange();
1925 Attr.setInvalid();
John McCall008df5d2010-10-14 02:06:32 +00001926 return false;
John McCall1e030eb2010-10-14 01:57:10 +00001927 }
1928
1929 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
1930 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
1931 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
1932 Attr.setInvalid();
John McCall008df5d2010-10-14 02:06:32 +00001933 return false;
John McCall1e030eb2010-10-14 01:57:10 +00001934 }
1935
Rafael Espindola425ef722010-03-30 22:15:11 +00001936 Type = S.Context.getRegParmType(Type, NumParams.getZExtValue());
1937 return false;
1938 }
1939
John McCall04a67a62010-02-05 21:31:56 +00001940 // Otherwise, a calling convention.
1941 if (Attr.getNumArgs() != 0) {
1942 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Abramo Bagnarae215f722010-04-30 13:10:51 +00001943 Attr.setInvalid();
John McCall04a67a62010-02-05 21:31:56 +00001944 return false;
1945 }
John McCallf82b4e82010-02-04 05:44:44 +00001946
John McCall04a67a62010-02-05 21:31:56 +00001947 QualType T = Type;
1948 if (const PointerType *PT = Type->getAs<PointerType>())
1949 T = PT->getPointeeType();
Douglas Gregorafac01d2010-09-01 16:29:03 +00001950 else if (const BlockPointerType *BPT = Type->getAs<BlockPointerType>())
1951 T = BPT->getPointeeType();
1952 else if (const MemberPointerType *MPT = Type->getAs<MemberPointerType>())
1953 T = MPT->getPointeeType();
1954 else if (const ReferenceType *RT = Type->getAs<ReferenceType>())
1955 T = RT->getPointeeType();
John McCall04a67a62010-02-05 21:31:56 +00001956 const FunctionType *Fn = T->getAs<FunctionType>();
John McCallf82b4e82010-02-04 05:44:44 +00001957
John McCall04a67a62010-02-05 21:31:56 +00001958 // Delay if the type didn't work out to a function.
1959 if (!Fn) return true;
1960
1961 // TODO: diagnose uses of these conventions on the wrong target.
1962 CallingConv CC;
1963 switch (Attr.getKind()) {
1964 case AttributeList::AT_cdecl: CC = CC_C; break;
1965 case AttributeList::AT_fastcall: CC = CC_X86FastCall; break;
1966 case AttributeList::AT_stdcall: CC = CC_X86StdCall; break;
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001967 case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break;
Dawn Perchik52fc3142010-09-03 01:29:35 +00001968 case AttributeList::AT_pascal: CC = CC_X86Pascal; break;
John McCall04a67a62010-02-05 21:31:56 +00001969 default: llvm_unreachable("unexpected attribute kind"); return false;
1970 }
1971
1972 CallingConv CCOld = Fn->getCallConv();
Charles Davis064f7db2010-02-23 06:13:55 +00001973 if (S.Context.getCanonicalCallConv(CC) ==
Abramo Bagnarae215f722010-04-30 13:10:51 +00001974 S.Context.getCanonicalCallConv(CCOld)) {
1975 Attr.setInvalid();
1976 return false;
1977 }
John McCall04a67a62010-02-05 21:31:56 +00001978
1979 if (CCOld != CC_Default) {
1980 // Should we diagnose reapplications of the same convention?
1981 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1982 << FunctionType::getNameForCallConv(CC)
1983 << FunctionType::getNameForCallConv(CCOld);
Abramo Bagnarae215f722010-04-30 13:10:51 +00001984 Attr.setInvalid();
John McCall04a67a62010-02-05 21:31:56 +00001985 return false;
1986 }
1987
1988 // Diagnose the use of X86 fastcall on varargs or unprototyped functions.
1989 if (CC == CC_X86FastCall) {
1990 if (isa<FunctionNoProtoType>(Fn)) {
1991 S.Diag(Attr.getLoc(), diag::err_cconv_knr)
1992 << FunctionType::getNameForCallConv(CC);
Abramo Bagnarae215f722010-04-30 13:10:51 +00001993 Attr.setInvalid();
John McCall04a67a62010-02-05 21:31:56 +00001994 return false;
1995 }
1996
1997 const FunctionProtoType *FnP = cast<FunctionProtoType>(Fn);
1998 if (FnP->isVariadic()) {
1999 S.Diag(Attr.getLoc(), diag::err_cconv_varargs)
2000 << FunctionType::getNameForCallConv(CC);
Abramo Bagnarae215f722010-04-30 13:10:51 +00002001 Attr.setInvalid();
John McCall04a67a62010-02-05 21:31:56 +00002002 return false;
2003 }
2004 }
2005
2006 Type = S.Context.getCallConvType(Type, CC);
2007 return false;
John McCallf82b4e82010-02-04 05:44:44 +00002008}
2009
John Thompson6e132aa2009-12-04 21:51:28 +00002010/// HandleVectorSizeAttribute - this attribute is only applicable to integral
2011/// and float scalars, although arrays, pointers, and function return values are
2012/// allowed in conjunction with this construct. Aggregates with this attribute
2013/// are invalid, even if they are of the same size as a corresponding scalar.
2014/// The raw attribute should contain precisely 1 argument, the vector size for
2015/// the variable, measured in bytes. If curType and rawAttr are well formed,
2016/// this routine will return a new vector type.
Chris Lattner788b0fd2010-06-23 06:00:24 +00002017static void HandleVectorSizeAttr(QualType& CurType, const AttributeList &Attr,
2018 Sema &S) {
Bob Wilson56affbc2010-11-16 00:32:16 +00002019 // Check the attribute arguments.
John Thompson6e132aa2009-12-04 21:51:28 +00002020 if (Attr.getNumArgs() != 1) {
2021 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002022 Attr.setInvalid();
John Thompson6e132aa2009-12-04 21:51:28 +00002023 return;
2024 }
2025 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
2026 llvm::APSInt vecSize(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002027 if (sizeExpr->isTypeDependent() || sizeExpr->isValueDependent() ||
2028 !sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
John Thompson6e132aa2009-12-04 21:51:28 +00002029 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2030 << "vector_size" << sizeExpr->getSourceRange();
Abramo Bagnarae215f722010-04-30 13:10:51 +00002031 Attr.setInvalid();
John Thompson6e132aa2009-12-04 21:51:28 +00002032 return;
2033 }
2034 // the base type must be integer or float, and can't already be a vector.
Douglas Gregorf6094622010-07-23 15:58:24 +00002035 if (!CurType->isIntegerType() && !CurType->isRealFloatingType()) {
John Thompson6e132aa2009-12-04 21:51:28 +00002036 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002037 Attr.setInvalid();
John Thompson6e132aa2009-12-04 21:51:28 +00002038 return;
2039 }
2040 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
2041 // vecSize is specified in bytes - convert to bits.
2042 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
2043
2044 // the vector size needs to be an integral multiple of the type size.
2045 if (vectorSize % typeSize) {
2046 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
2047 << sizeExpr->getSourceRange();
Abramo Bagnarae215f722010-04-30 13:10:51 +00002048 Attr.setInvalid();
John Thompson6e132aa2009-12-04 21:51:28 +00002049 return;
2050 }
2051 if (vectorSize == 0) {
2052 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
2053 << sizeExpr->getSourceRange();
Abramo Bagnarae215f722010-04-30 13:10:51 +00002054 Attr.setInvalid();
John Thompson6e132aa2009-12-04 21:51:28 +00002055 return;
2056 }
2057
2058 // Success! Instantiate the vector type, the number of elements is > 0, and
2059 // not required to be a power of 2, unlike GCC.
Chris Lattner788b0fd2010-06-23 06:00:24 +00002060 CurType = S.Context.getVectorType(CurType, vectorSize/typeSize,
Bob Wilsone86d78c2010-11-10 21:56:12 +00002061 VectorType::GenericVector);
John Thompson6e132aa2009-12-04 21:51:28 +00002062}
2063
Bob Wilson4211bb62010-11-16 00:32:24 +00002064/// HandleNeonVectorTypeAttr - The "neon_vector_type" and
2065/// "neon_polyvector_type" attributes are used to create vector types that
2066/// are mangled according to ARM's ABI. Otherwise, these types are identical
2067/// to those created with the "vector_size" attribute. Unlike "vector_size"
2068/// the argument to these Neon attributes is the number of vector elements,
2069/// not the vector size in bytes. The vector width and element type must
2070/// match one of the standard Neon vector types.
2071static void HandleNeonVectorTypeAttr(QualType& CurType,
2072 const AttributeList &Attr, Sema &S,
2073 VectorType::VectorKind VecKind,
2074 const char *AttrName) {
2075 // Check the attribute arguments.
2076 if (Attr.getNumArgs() != 1) {
2077 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2078 Attr.setInvalid();
2079 return;
2080 }
2081 // The number of elements must be an ICE.
2082 Expr *numEltsExpr = static_cast<Expr *>(Attr.getArg(0));
2083 llvm::APSInt numEltsInt(32);
2084 if (numEltsExpr->isTypeDependent() || numEltsExpr->isValueDependent() ||
2085 !numEltsExpr->isIntegerConstantExpr(numEltsInt, S.Context)) {
2086 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2087 << AttrName << numEltsExpr->getSourceRange();
2088 Attr.setInvalid();
2089 return;
2090 }
2091 // Only certain element types are supported for Neon vectors.
2092 const BuiltinType* BTy = CurType->getAs<BuiltinType>();
2093 if (!BTy ||
2094 (VecKind == VectorType::NeonPolyVector &&
2095 BTy->getKind() != BuiltinType::SChar &&
2096 BTy->getKind() != BuiltinType::Short) ||
2097 (BTy->getKind() != BuiltinType::SChar &&
2098 BTy->getKind() != BuiltinType::UChar &&
2099 BTy->getKind() != BuiltinType::Short &&
2100 BTy->getKind() != BuiltinType::UShort &&
2101 BTy->getKind() != BuiltinType::Int &&
2102 BTy->getKind() != BuiltinType::UInt &&
2103 BTy->getKind() != BuiltinType::LongLong &&
2104 BTy->getKind() != BuiltinType::ULongLong &&
2105 BTy->getKind() != BuiltinType::Float)) {
2106 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) <<CurType;
2107 Attr.setInvalid();
2108 return;
2109 }
2110 // The total size of the vector must be 64 or 128 bits.
2111 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
2112 unsigned numElts = static_cast<unsigned>(numEltsInt.getZExtValue());
2113 unsigned vecSize = typeSize * numElts;
2114 if (vecSize != 64 && vecSize != 128) {
2115 S.Diag(Attr.getLoc(), diag::err_attribute_bad_neon_vector_size) << CurType;
2116 Attr.setInvalid();
2117 return;
2118 }
2119
2120 CurType = S.Context.getVectorType(CurType, numElts, VecKind);
2121}
2122
John McCall04a67a62010-02-05 21:31:56 +00002123void ProcessTypeAttributeList(Sema &S, QualType &Result,
Charles Davis328ce342010-02-24 02:27:18 +00002124 bool IsDeclSpec, const AttributeList *AL,
John McCall04a67a62010-02-05 21:31:56 +00002125 DelayedAttributeSet &FnAttrs) {
Chris Lattner232e8822008-02-21 01:08:11 +00002126 // Scan through and apply attributes to this type where it makes sense. Some
2127 // attributes (such as __address_space__, __vector_size__, etc) apply to the
2128 // type, but others can be present in the type specifiers even though they
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00002129 // apply to the decl. Here we apply type attributes and ignore the rest.
2130 for (; AL; AL = AL->getNext()) {
Abramo Bagnarae215f722010-04-30 13:10:51 +00002131 // Skip attributes that were marked to be invalid.
2132 if (AL->isInvalid())
2133 continue;
2134
Abramo Bagnarab1f1b262010-04-30 09:13:03 +00002135 // If this is an attribute we can handle, do so now,
2136 // otherwise, add it to the FnAttrs list for rechaining.
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00002137 switch (AL->getKind()) {
Chris Lattner232e8822008-02-21 01:08:11 +00002138 default: break;
John McCall04a67a62010-02-05 21:31:56 +00002139
Chris Lattner232e8822008-02-21 01:08:11 +00002140 case AttributeList::AT_address_space:
John McCall04a67a62010-02-05 21:31:56 +00002141 HandleAddressSpaceTypeAttribute(Result, *AL, S);
Chris Lattnerc9b346d2008-06-29 00:50:08 +00002142 break;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00002143 case AttributeList::AT_objc_gc:
John McCall04a67a62010-02-05 21:31:56 +00002144 HandleObjCGCTypeAttribute(Result, *AL, S);
Mike Stump24556362009-07-25 21:26:53 +00002145 break;
John Thompson6e132aa2009-12-04 21:51:28 +00002146 case AttributeList::AT_vector_size:
John McCall04a67a62010-02-05 21:31:56 +00002147 HandleVectorSizeAttr(Result, *AL, S);
2148 break;
Bob Wilson4211bb62010-11-16 00:32:24 +00002149 case AttributeList::AT_neon_vector_type:
2150 HandleNeonVectorTypeAttr(Result, *AL, S, VectorType::NeonVector,
2151 "neon_vector_type");
2152 break;
2153 case AttributeList::AT_neon_polyvector_type:
2154 HandleNeonVectorTypeAttr(Result, *AL, S, VectorType::NeonPolyVector,
2155 "neon_polyvector_type");
2156 break;
John McCall04a67a62010-02-05 21:31:56 +00002157
2158 case AttributeList::AT_noreturn:
2159 case AttributeList::AT_cdecl:
2160 case AttributeList::AT_fastcall:
2161 case AttributeList::AT_stdcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002162 case AttributeList::AT_thiscall:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002163 case AttributeList::AT_pascal:
Rafael Espindola425ef722010-03-30 22:15:11 +00002164 case AttributeList::AT_regparm:
Charles Davis328ce342010-02-24 02:27:18 +00002165 // Don't process these on the DeclSpec.
2166 if (IsDeclSpec ||
2167 ProcessFnAttr(S, Result, *AL))
John McCall04a67a62010-02-05 21:31:56 +00002168 FnAttrs.push_back(DelayedAttribute(AL, Result));
John Thompson6e132aa2009-12-04 21:51:28 +00002169 break;
Chris Lattner232e8822008-02-21 01:08:11 +00002170 }
Chris Lattner232e8822008-02-21 01:08:11 +00002171 }
Chris Lattner232e8822008-02-21 01:08:11 +00002172}
2173
Mike Stump1eb44332009-09-09 15:08:12 +00002174/// @brief Ensure that the type T is a complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00002175///
2176/// This routine checks whether the type @p T is complete in any
2177/// context where a complete type is required. If @p T is a complete
Douglas Gregor86447ec2009-03-09 16:13:40 +00002178/// type, returns false. If @p T is a class template specialization,
2179/// this routine then attempts to perform class template
2180/// instantiation. If instantiation fails, or if @p T is incomplete
2181/// and cannot be completed, issues the diagnostic @p diag (giving it
2182/// the type @p T) and returns true.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00002183///
2184/// @param Loc The location in the source that the incomplete type
2185/// diagnostic should refer to.
2186///
2187/// @param T The type that this routine is examining for completeness.
2188///
Mike Stump1eb44332009-09-09 15:08:12 +00002189/// @param PD The partial diagnostic that will be printed out if T is not a
Anders Carlssonb7906612009-08-26 23:45:07 +00002190/// complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00002191///
2192/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
2193/// @c false otherwise.
Anders Carlsson91a0cc92009-08-26 22:33:56 +00002194bool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
Anders Carlsson8c8d9192009-10-09 23:51:55 +00002195 const PartialDiagnostic &PD,
2196 std::pair<SourceLocation,
2197 PartialDiagnostic> Note) {
Anders Carlsson91a0cc92009-08-26 22:33:56 +00002198 unsigned diag = PD.getDiagID();
Mike Stump1eb44332009-09-09 15:08:12 +00002199
Douglas Gregor573d9c32009-10-21 23:19:44 +00002200 // FIXME: Add this assertion to make sure we always get instantiation points.
2201 // assert(!Loc.isInvalid() && "Invalid location in RequireCompleteType");
Douglas Gregor690dc7f2009-05-21 23:48:18 +00002202 // FIXME: Add this assertion to help us flush out problems with
2203 // checking for dependent types and type-dependent expressions.
2204 //
Mike Stump1eb44332009-09-09 15:08:12 +00002205 // assert(!T->isDependentType() &&
Douglas Gregor690dc7f2009-05-21 23:48:18 +00002206 // "Can't ask whether a dependent type is complete");
2207
Douglas Gregor4ec339f2009-01-19 19:26:10 +00002208 // If we have a complete type, we're done.
2209 if (!T->isIncompleteType())
2210 return false;
Eli Friedman3c0eb162008-05-27 03:33:27 +00002211
Douglas Gregord475b8d2009-03-25 21:17:03 +00002212 // If we have a class template specialization or a class member of a
Sebastian Redl923d56d2009-11-05 15:52:31 +00002213 // class template specialization, or an array with known size of such,
2214 // try to instantiate it.
2215 QualType MaybeTemplate = T;
Douglas Gregor89c49f02009-11-09 22:08:55 +00002216 if (const ConstantArrayType *Array = Context.getAsConstantArrayType(T))
Sebastian Redl923d56d2009-11-05 15:52:31 +00002217 MaybeTemplate = Array->getElementType();
2218 if (const RecordType *Record = MaybeTemplate->getAs<RecordType>()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00002219 if (ClassTemplateSpecializationDecl *ClassTemplateSpec
Douglas Gregord475b8d2009-03-25 21:17:03 +00002220 = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
Douglas Gregor972e6ce2009-10-27 06:26:26 +00002221 if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared)
2222 return InstantiateClassTemplateSpecialization(Loc, ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002223 TSK_ImplicitInstantiation,
Douglas Gregor5842ba92009-08-24 15:23:48 +00002224 /*Complain=*/diag != 0);
Mike Stump1eb44332009-09-09 15:08:12 +00002225 } else if (CXXRecordDecl *Rec
Douglas Gregord475b8d2009-03-25 21:17:03 +00002226 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
2227 if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00002228 MemberSpecializationInfo *MSInfo = Rec->getMemberSpecializationInfo();
2229 assert(MSInfo && "Missing member specialization information?");
Douglas Gregor357bbd02009-08-28 20:50:45 +00002230 // This record was instantiated from a class within a template.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00002231 if (MSInfo->getTemplateSpecializationKind()
Douglas Gregor972e6ce2009-10-27 06:26:26 +00002232 != TSK_ExplicitSpecialization)
Douglas Gregorf6b11852009-10-08 15:14:33 +00002233 return InstantiateClass(Loc, Rec, Pattern,
2234 getTemplateInstantiationArgs(Rec),
2235 TSK_ImplicitInstantiation,
2236 /*Complain=*/diag != 0);
Douglas Gregord475b8d2009-03-25 21:17:03 +00002237 }
2238 }
2239 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00002240
Douglas Gregor5842ba92009-08-24 15:23:48 +00002241 if (diag == 0)
2242 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002243
John McCall916c8702010-11-16 01:44:35 +00002244 const TagType *Tag = T->getAs<TagType>();
Rafael Espindola01620702010-03-21 22:56:43 +00002245
2246 // Avoid diagnosing invalid decls as incomplete.
2247 if (Tag && Tag->getDecl()->isInvalidDecl())
2248 return true;
2249
John McCall916c8702010-11-16 01:44:35 +00002250 // Give the external AST source a chance to complete the type.
2251 if (Tag && Tag->getDecl()->hasExternalLexicalStorage()) {
2252 Context.getExternalSource()->CompleteType(Tag->getDecl());
2253 if (!Tag->isIncompleteType())
2254 return false;
2255 }
2256
Douglas Gregor4ec339f2009-01-19 19:26:10 +00002257 // We have an incomplete type. Produce a diagnostic.
Anders Carlsson91a0cc92009-08-26 22:33:56 +00002258 Diag(Loc, PD) << T;
Douglas Gregor4ec339f2009-01-19 19:26:10 +00002259
Anders Carlsson8c8d9192009-10-09 23:51:55 +00002260 // If we have a note, produce it.
2261 if (!Note.first.isInvalid())
2262 Diag(Note.first, Note.second);
2263
Douglas Gregor4ec339f2009-01-19 19:26:10 +00002264 // If the type was a forward declaration of a class/struct/union
Rafael Espindola01620702010-03-21 22:56:43 +00002265 // type, produce a note.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00002266 if (Tag && !Tag->getDecl()->isInvalidDecl())
Mike Stump1eb44332009-09-09 15:08:12 +00002267 Diag(Tag->getDecl()->getLocation(),
Douglas Gregor4ec339f2009-01-19 19:26:10 +00002268 Tag->isBeingDefined() ? diag::note_type_being_defined
2269 : diag::note_forward_declaration)
2270 << QualType(Tag, 0);
2271
2272 return true;
2273}
Douglas Gregore6258932009-03-19 00:39:20 +00002274
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00002275bool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
2276 const PartialDiagnostic &PD) {
2277 return RequireCompleteType(Loc, T, PD,
2278 std::make_pair(SourceLocation(), PDiag(0)));
2279}
2280
2281bool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
2282 unsigned DiagID) {
2283 return RequireCompleteType(Loc, T, PDiag(DiagID),
2284 std::make_pair(SourceLocation(), PDiag(0)));
2285}
2286
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002287/// \brief Retrieve a version of the type 'T' that is elaborated by Keyword
2288/// and qualified by the nested-name-specifier contained in SS.
2289QualType Sema::getElaboratedType(ElaboratedTypeKeyword Keyword,
2290 const CXXScopeSpec &SS, QualType T) {
2291 if (T.isNull())
Douglas Gregore6258932009-03-19 00:39:20 +00002292 return T;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002293 NestedNameSpecifier *NNS;
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002294 if (SS.isValid())
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002295 NNS = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
2296 else {
2297 if (Keyword == ETK_None)
2298 return T;
2299 NNS = 0;
2300 }
2301 return Context.getElaboratedType(Keyword, NNS, T);
Douglas Gregore6258932009-03-19 00:39:20 +00002302}
Anders Carlssonaf017e62009-06-29 22:58:55 +00002303
John McCall2a984ca2010-10-12 00:20:44 +00002304QualType Sema::BuildTypeofExprType(Expr *E, SourceLocation Loc) {
2305 ExprResult ER = CheckPlaceholderExpr(E, Loc);
2306 if (ER.isInvalid()) return QualType();
2307 E = ER.take();
2308
Fariborz Jahanian2b1d51b2010-10-05 23:24:00 +00002309 if (!E->isTypeDependent()) {
2310 QualType T = E->getType();
Fariborz Jahanianaca7f7b2010-10-06 00:23:25 +00002311 if (const TagType *TT = T->getAs<TagType>())
2312 DiagnoseUseOfDecl(TT->getDecl(), E->getExprLoc());
Fariborz Jahanian2b1d51b2010-10-05 23:24:00 +00002313 }
Anders Carlssonaf017e62009-06-29 22:58:55 +00002314 return Context.getTypeOfExprType(E);
2315}
2316
John McCall2a984ca2010-10-12 00:20:44 +00002317QualType Sema::BuildDecltypeType(Expr *E, SourceLocation Loc) {
2318 ExprResult ER = CheckPlaceholderExpr(E, Loc);
2319 if (ER.isInvalid()) return QualType();
2320 E = ER.take();
Douglas Gregor4b52e252009-12-21 23:17:24 +00002321
Anders Carlssonaf017e62009-06-29 22:58:55 +00002322 return Context.getDecltypeType(E);
2323}