blob: 8833792494eeeb529670872358ab0582334232a8 [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
14#include "Sema.h"
15#include "clang/AST/ASTContext.h"
Douglas Gregora8f32e02009-10-06 17:59:45 +000016#include "clang/AST/CXXInheritance.h"
Steve Naroff980e5082007-10-01 19:00:59 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregor2943aed2009-03-03 04:44:36 +000018#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +000019#include "clang/AST/TypeLoc.h"
John McCall51bd8032009-10-18 01:05:36 +000020#include "clang/AST/TypeLocVisitor.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000021#include "clang/AST/Expr.h"
Anders Carlsson91a0cc92009-08-26 22:33:56 +000022#include "clang/Basic/PartialDiagnostic.h"
Daniel Dunbare4858a62008-08-11 03:45:03 +000023#include "clang/Parse/DeclSpec.h"
Sebastian Redl4994d2d2009-07-04 11:39:00 +000024#include "llvm/ADT/SmallPtrSet.h"
Douglas Gregor87c12c42009-11-04 16:49:01 +000025#include "llvm/Support/ErrorHandling.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000026using namespace clang;
27
Douglas Gregor2dc0e642009-03-23 23:06:20 +000028/// \brief Perform adjustment on the parameter type of a function.
29///
30/// This routine adjusts the given parameter type @p T to the actual
Mike Stump1eb44332009-09-09 15:08:12 +000031/// parameter type used by semantic analysis (C99 6.7.5.3p[7,8],
32/// C++ [dcl.fct]p3). The adjusted parameter type is returned.
Douglas Gregor2dc0e642009-03-23 23:06:20 +000033QualType Sema::adjustParameterType(QualType T) {
34 // C99 6.7.5.3p7:
Chris Lattner778ed742009-10-25 17:36:50 +000035 // A declaration of a parameter as "array of type" shall be
36 // adjusted to "qualified pointer to type", where the type
37 // qualifiers (if any) are those specified within the [ and ] of
38 // the array type derivation.
39 if (T->isArrayType())
Douglas Gregor2dc0e642009-03-23 23:06:20 +000040 return Context.getArrayDecayedType(T);
Chris Lattner778ed742009-10-25 17:36:50 +000041
42 // C99 6.7.5.3p8:
43 // A declaration of a parameter as "function returning type"
44 // shall be adjusted to "pointer to function returning type", as
45 // in 6.3.2.1.
46 if (T->isFunctionType())
Douglas Gregor2dc0e642009-03-23 23:06:20 +000047 return Context.getPointerType(T);
48
49 return T;
50}
51
Chris Lattner5db2bb12009-10-25 18:21:37 +000052
53
54/// isOmittedBlockReturnType - Return true if this declarator is missing a
55/// return type because this is a omitted return type on a block literal.
Sebastian Redl8ce35b02009-10-25 21:45:37 +000056static bool isOmittedBlockReturnType(const Declarator &D) {
Chris Lattner5db2bb12009-10-25 18:21:37 +000057 if (D.getContext() != Declarator::BlockLiteralContext ||
Sebastian Redl8ce35b02009-10-25 21:45:37 +000058 D.getDeclSpec().hasTypeSpecifier())
Chris Lattner5db2bb12009-10-25 18:21:37 +000059 return false;
60
61 if (D.getNumTypeObjects() == 0)
Chris Lattnera64ef0a2009-10-25 22:09:09 +000062 return true; // ^{ ... }
Chris Lattner5db2bb12009-10-25 18:21:37 +000063
64 if (D.getNumTypeObjects() == 1 &&
65 D.getTypeObject(0).Kind == DeclaratorChunk::Function)
Chris Lattnera64ef0a2009-10-25 22:09:09 +000066 return true; // ^(int X, float Y) { ... }
Chris Lattner5db2bb12009-10-25 18:21:37 +000067
68 return false;
69}
70
Douglas Gregor930d8b52009-01-30 22:09:00 +000071/// \brief Convert the specified declspec to the appropriate type
72/// object.
Chris Lattner5db2bb12009-10-25 18:21:37 +000073/// \param D the declarator containing the declaration specifier.
Chris Lattner5153ee62009-04-25 08:47:54 +000074/// \returns The type described by the declaration specifiers. This function
75/// never returns null.
Sebastian Redl8ce35b02009-10-25 21:45:37 +000076static QualType ConvertDeclSpecToType(Declarator &TheDeclarator, Sema &TheSema){
Reid Spencer5f016e22007-07-11 17:01:13 +000077 // FIXME: Should move the logic from DeclSpec::Finish to here for validity
78 // checking.
Chris Lattner5db2bb12009-10-25 18:21:37 +000079 const DeclSpec &DS = TheDeclarator.getDeclSpec();
80 SourceLocation DeclLoc = TheDeclarator.getIdentifierLoc();
81 if (DeclLoc.isInvalid())
82 DeclLoc = DS.getSourceRange().getBegin();
Chris Lattner1564e392009-10-25 18:07:27 +000083
84 ASTContext &Context = TheSema.Context;
Mike Stump1eb44332009-09-09 15:08:12 +000085
Chris Lattner5db2bb12009-10-25 18:21:37 +000086 QualType Result;
Reid Spencer5f016e22007-07-11 17:01:13 +000087 switch (DS.getTypeSpecType()) {
Chris Lattner96b77fc2008-04-02 06:50:17 +000088 case DeclSpec::TST_void:
89 Result = Context.VoidTy;
90 break;
Reid Spencer5f016e22007-07-11 17:01:13 +000091 case DeclSpec::TST_char:
92 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
Chris Lattnerfab5b452008-02-20 23:53:49 +000093 Result = Context.CharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000094 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
Chris Lattnerfab5b452008-02-20 23:53:49 +000095 Result = Context.SignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000096 else {
97 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
98 "Unknown TSS value");
Chris Lattnerfab5b452008-02-20 23:53:49 +000099 Result = Context.UnsignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000100 }
Chris Lattner958858e2008-02-20 21:40:32 +0000101 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000102 case DeclSpec::TST_wchar:
103 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
104 Result = Context.WCharTy;
105 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
Chris Lattner1564e392009-10-25 18:07:27 +0000106 TheSema.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000107 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000108 Result = Context.getSignedWCharType();
109 } else {
110 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
111 "Unknown TSS value");
Chris Lattner1564e392009-10-25 18:07:27 +0000112 TheSema.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000113 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000114 Result = Context.getUnsignedWCharType();
115 }
116 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000117 case DeclSpec::TST_char16:
118 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
119 "Unknown TSS value");
120 Result = Context.Char16Ty;
121 break;
122 case DeclSpec::TST_char32:
123 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
124 "Unknown TSS value");
125 Result = Context.Char32Ty;
126 break;
Chris Lattnerd658b562008-04-05 06:32:51 +0000127 case DeclSpec::TST_unspecified:
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000128 // "<proto1,proto2>" is an objc qualified ID with a missing id.
Chris Lattner097e9162008-10-20 02:01:50 +0000129 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000130 Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy,
Steve Naroff14108da2009-07-10 23:34:53 +0000131 (ObjCProtocolDecl**)PQ,
Steve Naroff683087f2009-06-29 16:22:52 +0000132 DS.getNumProtocolQualifiers());
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000133 break;
134 }
Chris Lattner5db2bb12009-10-25 18:21:37 +0000135
136 // If this is a missing declspec in a block literal return context, then it
137 // is inferred from the return statements inside the block.
Sebastian Redl8ce35b02009-10-25 21:45:37 +0000138 if (isOmittedBlockReturnType(TheDeclarator)) {
Chris Lattner5db2bb12009-10-25 18:21:37 +0000139 Result = Context.DependentTy;
140 break;
141 }
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Chris Lattnerd658b562008-04-05 06:32:51 +0000143 // Unspecified typespec defaults to int in C90. However, the C90 grammar
144 // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
145 // type-qualifier, or storage-class-specifier. If not, emit an extwarn.
146 // Note that the one exception to this is function definitions, which are
147 // allowed to be completely missing a declspec. This is handled in the
148 // parser already though by it pretending to have seen an 'int' in this
149 // case.
Chris Lattner1564e392009-10-25 18:07:27 +0000150 if (TheSema.getLangOptions().ImplicitInt) {
Chris Lattner35d276f2009-02-27 18:53:28 +0000151 // In C89 mode, we only warn if there is a completely missing declspec
152 // when one is not allowed.
Chris Lattner3f84ad22009-04-22 05:27:59 +0000153 if (DS.isEmpty()) {
Chris Lattner1564e392009-10-25 18:07:27 +0000154 TheSema.Diag(DeclLoc, diag::ext_missing_declspec)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000155 << DS.getSourceRange()
Chris Lattner173144a2009-02-27 22:31:56 +0000156 << CodeModificationHint::CreateInsertion(DS.getSourceRange().getBegin(),
157 "int");
Chris Lattner3f84ad22009-04-22 05:27:59 +0000158 }
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000159 } else if (!DS.hasTypeSpecifier()) {
Chris Lattnerd658b562008-04-05 06:32:51 +0000160 // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says:
161 // "At least one type specifier shall be given in the declaration
162 // specifiers in each declaration, and in the specifier-qualifier list in
163 // each struct declaration and type name."
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000164 // FIXME: Does Microsoft really have the implicit int extension in C++?
Chris Lattner1564e392009-10-25 18:07:27 +0000165 if (TheSema.getLangOptions().CPlusPlus &&
166 !TheSema.getLangOptions().Microsoft) {
167 TheSema.Diag(DeclLoc, diag::err_missing_type_specifier)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000168 << DS.getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000169
Chris Lattnerb78d8332009-06-26 04:45:06 +0000170 // When this occurs in C++ code, often something is very broken with the
171 // value being declared, poison it as invalid so we don't get chains of
172 // errors.
Chris Lattner5db2bb12009-10-25 18:21:37 +0000173 TheDeclarator.setInvalidType(true);
Chris Lattnerb78d8332009-06-26 04:45:06 +0000174 } else {
Chris Lattner1564e392009-10-25 18:07:27 +0000175 TheSema.Diag(DeclLoc, diag::ext_missing_type_specifier)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000176 << DS.getSourceRange();
Chris Lattnerb78d8332009-06-26 04:45:06 +0000177 }
Chris Lattnerd658b562008-04-05 06:32:51 +0000178 }
Mike Stump1eb44332009-09-09 15:08:12 +0000179
180 // FALL THROUGH.
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000181 case DeclSpec::TST_int: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000182 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
183 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000184 case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
185 case DeclSpec::TSW_short: Result = Context.ShortTy; break;
186 case DeclSpec::TSW_long: Result = Context.LongTy; break;
Chris Lattner311157f2009-10-25 18:25:04 +0000187 case DeclSpec::TSW_longlong:
188 Result = Context.LongLongTy;
189
190 // long long is a C99 feature.
191 if (!TheSema.getLangOptions().C99 &&
192 !TheSema.getLangOptions().CPlusPlus0x)
193 TheSema.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong);
194 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000195 }
196 } else {
197 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000198 case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
199 case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break;
200 case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break;
Chris Lattner311157f2009-10-25 18:25:04 +0000201 case DeclSpec::TSW_longlong:
202 Result = Context.UnsignedLongLongTy;
203
204 // long long is a C99 feature.
205 if (!TheSema.getLangOptions().C99 &&
206 !TheSema.getLangOptions().CPlusPlus0x)
207 TheSema.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong);
208 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000209 }
210 }
Chris Lattner958858e2008-02-20 21:40:32 +0000211 break;
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000212 }
Chris Lattnerfab5b452008-02-20 23:53:49 +0000213 case DeclSpec::TST_float: Result = Context.FloatTy; break;
Chris Lattner958858e2008-02-20 21:40:32 +0000214 case DeclSpec::TST_double:
215 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000216 Result = Context.LongDoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000217 else
Chris Lattnerfab5b452008-02-20 23:53:49 +0000218 Result = Context.DoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000219 break;
Chris Lattnerfab5b452008-02-20 23:53:49 +0000220 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
Reid Spencer5f016e22007-07-11 17:01:13 +0000221 case DeclSpec::TST_decimal32: // _Decimal32
222 case DeclSpec::TST_decimal64: // _Decimal64
223 case DeclSpec::TST_decimal128: // _Decimal128
Chris Lattner1564e392009-10-25 18:07:27 +0000224 TheSema.Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported);
Chris Lattner8f12f652009-05-13 05:02:08 +0000225 Result = Context.IntTy;
Chris Lattner5db2bb12009-10-25 18:21:37 +0000226 TheDeclarator.setInvalidType(true);
Chris Lattner8f12f652009-05-13 05:02:08 +0000227 break;
Chris Lattner99dc9142008-04-13 18:59:07 +0000228 case DeclSpec::TST_class:
Reid Spencer5f016e22007-07-11 17:01:13 +0000229 case DeclSpec::TST_enum:
230 case DeclSpec::TST_union:
231 case DeclSpec::TST_struct: {
Douglas Gregorc7621a62009-11-05 20:54:04 +0000232 TypeDecl *D
233 = dyn_cast_or_null<TypeDecl>(static_cast<Decl *>(DS.getTypeRep()));
John McCall6e247262009-10-10 05:48:19 +0000234 if (!D) {
235 // This can happen in C++ with ambiguous lookups.
236 Result = Context.IntTy;
Chris Lattner5db2bb12009-10-25 18:21:37 +0000237 TheDeclarator.setInvalidType(true);
John McCall6e247262009-10-10 05:48:19 +0000238 break;
239 }
240
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000241 // If the type is deprecated or unavailable, diagnose it.
John McCall54abf7d2009-11-04 02:18:39 +0000242 TheSema.DiagnoseUseOfDecl(D, DS.getTypeSpecTypeLoc());
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000243
Reid Spencer5f016e22007-07-11 17:01:13 +0000244 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000245 DS.getTypeSpecSign() == 0 && "No qualifiers on tag names!");
246
Reid Spencer5f016e22007-07-11 17:01:13 +0000247 // TypeQuals handled by caller.
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000248 Result = Context.getTypeDeclType(D);
John McCall2191b202009-09-05 06:31:47 +0000249
250 // In C++, make an ElaboratedType.
Chris Lattner1564e392009-10-25 18:07:27 +0000251 if (TheSema.getLangOptions().CPlusPlus) {
John McCall2191b202009-09-05 06:31:47 +0000252 TagDecl::TagKind Tag
253 = TagDecl::getTagKindForTypeSpec(DS.getTypeSpecType());
254 Result = Context.getElaboratedType(Result, Tag);
255 }
Mike Stump1eb44332009-09-09 15:08:12 +0000256
Chris Lattner5153ee62009-04-25 08:47:54 +0000257 if (D->isInvalidDecl())
Chris Lattner5db2bb12009-10-25 18:21:37 +0000258 TheDeclarator.setInvalidType(true);
Chris Lattner958858e2008-02-20 21:40:32 +0000259 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000260 }
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000261 case DeclSpec::TST_typename: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000262 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
263 DS.getTypeSpecSign() == 0 &&
264 "Can't handle qualifiers on typedef names yet!");
Chris Lattner1564e392009-10-25 18:07:27 +0000265 Result = TheSema.GetTypeFromParser(DS.getTypeRep());
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000266
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000267 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000268 if (const ObjCInterfaceType *
269 Interface = Result->getAs<ObjCInterfaceType>()) {
Steve Naroff67ef8ea2009-07-20 17:56:53 +0000270 // It would be nice if protocol qualifiers were only stored with the
271 // ObjCObjectPointerType. Unfortunately, this isn't possible due
272 // to the following typedef idiom (which is uncommon, but allowed):
Mike Stump1eb44332009-09-09 15:08:12 +0000273 //
Steve Naroff67ef8ea2009-07-20 17:56:53 +0000274 // typedef Foo<P> T;
275 // static void func() {
276 // Foo<P> *yy;
277 // T *zz;
278 // }
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000279 Result = Context.getObjCInterfaceType(Interface->getDecl(),
280 (ObjCProtocolDecl**)PQ,
281 DS.getNumProtocolQualifiers());
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000282 } else if (Result->isObjCIdType())
Chris Lattnerae4da612008-07-26 01:53:50 +0000283 // id<protocol-list>
Mike Stump1eb44332009-09-09 15:08:12 +0000284 Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy,
Steve Naroff14108da2009-07-10 23:34:53 +0000285 (ObjCProtocolDecl**)PQ, DS.getNumProtocolQualifiers());
286 else if (Result->isObjCClassType()) {
Steve Naroff4262a072009-02-23 18:53:24 +0000287 // Class<protocol-list>
Mike Stump1eb44332009-09-09 15:08:12 +0000288 Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinClassTy,
Steve Naroff470301b2009-07-22 16:07:01 +0000289 (ObjCProtocolDecl**)PQ, DS.getNumProtocolQualifiers());
Chris Lattner3f84ad22009-04-22 05:27:59 +0000290 } else {
Chris Lattner1564e392009-10-25 18:07:27 +0000291 TheSema.Diag(DeclLoc, diag::err_invalid_protocol_qualifiers)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000292 << DS.getSourceRange();
Chris Lattner5db2bb12009-10-25 18:21:37 +0000293 TheDeclarator.setInvalidType(true);
Chris Lattner3f84ad22009-04-22 05:27:59 +0000294 }
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000295 }
Mike Stump1eb44332009-09-09 15:08:12 +0000296
Reid Spencer5f016e22007-07-11 17:01:13 +0000297 // TypeQuals handled by caller.
Chris Lattner958858e2008-02-20 21:40:32 +0000298 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000299 }
Chris Lattner958858e2008-02-20 21:40:32 +0000300 case DeclSpec::TST_typeofType:
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000301 // FIXME: Preserve type source info.
Chris Lattner1564e392009-10-25 18:07:27 +0000302 Result = TheSema.GetTypeFromParser(DS.getTypeRep());
Chris Lattner958858e2008-02-20 21:40:32 +0000303 assert(!Result.isNull() && "Didn't get a type for typeof?");
Steve Naroffd1861fd2007-07-31 12:34:36 +0000304 // TypeQuals handled by caller.
Chris Lattnerfab5b452008-02-20 23:53:49 +0000305 Result = Context.getTypeOfType(Result);
Chris Lattner958858e2008-02-20 21:40:32 +0000306 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000307 case DeclSpec::TST_typeofExpr: {
308 Expr *E = static_cast<Expr *>(DS.getTypeRep());
309 assert(E && "Didn't get an expression for typeof?");
310 // TypeQuals handled by caller.
Douglas Gregor4b52e252009-12-21 23:17:24 +0000311 Result = TheSema.BuildTypeofExprType(E);
312 if (Result.isNull()) {
313 Result = Context.IntTy;
314 TheDeclarator.setInvalidType(true);
315 }
Chris Lattner958858e2008-02-20 21:40:32 +0000316 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000317 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000318 case DeclSpec::TST_decltype: {
319 Expr *E = static_cast<Expr *>(DS.getTypeRep());
320 assert(E && "Didn't get an expression for decltype?");
321 // TypeQuals handled by caller.
Chris Lattner1564e392009-10-25 18:07:27 +0000322 Result = TheSema.BuildDecltypeType(E);
Anders Carlssonaf017e62009-06-29 22:58:55 +0000323 if (Result.isNull()) {
324 Result = Context.IntTy;
Chris Lattner5db2bb12009-10-25 18:21:37 +0000325 TheDeclarator.setInvalidType(true);
Anders Carlssonaf017e62009-06-29 22:58:55 +0000326 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000327 break;
328 }
Anders Carlssone89d1592009-06-26 18:41:36 +0000329 case DeclSpec::TST_auto: {
330 // TypeQuals handled by caller.
331 Result = Context.UndeducedAutoTy;
332 break;
333 }
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Douglas Gregor809070a2009-02-18 17:45:20 +0000335 case DeclSpec::TST_error:
Chris Lattner5153ee62009-04-25 08:47:54 +0000336 Result = Context.IntTy;
Chris Lattner5db2bb12009-10-25 18:21:37 +0000337 TheDeclarator.setInvalidType(true);
Chris Lattner5153ee62009-04-25 08:47:54 +0000338 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000339 }
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Chris Lattner958858e2008-02-20 21:40:32 +0000341 // Handle complex types.
Douglas Gregorf244cd72009-02-14 21:06:05 +0000342 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
Chris Lattner1564e392009-10-25 18:07:27 +0000343 if (TheSema.getLangOptions().Freestanding)
344 TheSema.Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
Chris Lattnerfab5b452008-02-20 23:53:49 +0000345 Result = Context.getComplexType(Result);
Douglas Gregorf244cd72009-02-14 21:06:05 +0000346 }
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Chris Lattner958858e2008-02-20 21:40:32 +0000348 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
349 "FIXME: imaginary types not supported yet!");
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Chris Lattner38d8b982008-02-20 22:04:11 +0000351 // See if there are any attributes on the declspec that apply to the type (as
352 // opposed to the decl).
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000353 if (const AttributeList *AL = DS.getAttributes())
Chris Lattner1564e392009-10-25 18:07:27 +0000354 TheSema.ProcessTypeAttributeList(Result, AL);
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Chris Lattner96b77fc2008-04-02 06:50:17 +0000356 // Apply const/volatile/restrict qualifiers to T.
357 if (unsigned TypeQuals = DS.getTypeQualifiers()) {
358
359 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
360 // or incomplete types shall not be restrict-qualified." C++ also allows
361 // restrict-qualified references.
John McCall0953e762009-09-24 19:53:00 +0000362 if (TypeQuals & DeclSpec::TQ_restrict) {
Fariborz Jahanian2b5ff1a2009-12-07 18:08:58 +0000363 if (Result->isAnyPointerType() || Result->isReferenceType()) {
364 QualType EltTy;
365 if (Result->isObjCObjectPointerType())
366 EltTy = Result;
367 else
368 EltTy = Result->isPointerType() ?
369 Result->getAs<PointerType>()->getPointeeType() :
370 Result->getAs<ReferenceType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Douglas Gregorbad0e652009-03-24 20:32:41 +0000372 // If we have a pointer or reference, the pointee must have an object
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000373 // incomplete type.
374 if (!EltTy->isIncompleteOrObjectType()) {
Chris Lattner1564e392009-10-25 18:07:27 +0000375 TheSema.Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000376 diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattnerd1625842008-11-24 06:25:27 +0000377 << EltTy << DS.getSourceRange();
John McCall0953e762009-09-24 19:53:00 +0000378 TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000379 }
380 } else {
Chris Lattner1564e392009-10-25 18:07:27 +0000381 TheSema.Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000382 diag::err_typecheck_invalid_restrict_not_pointer)
Chris Lattnerd1625842008-11-24 06:25:27 +0000383 << Result << DS.getSourceRange();
John McCall0953e762009-09-24 19:53:00 +0000384 TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
Chris Lattner96b77fc2008-04-02 06:50:17 +0000385 }
Chris Lattner96b77fc2008-04-02 06:50:17 +0000386 }
Mike Stump1eb44332009-09-09 15:08:12 +0000387
Chris Lattner96b77fc2008-04-02 06:50:17 +0000388 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
389 // of a function type includes any type qualifiers, the behavior is
390 // undefined."
391 if (Result->isFunctionType() && TypeQuals) {
392 // Get some location to point at, either the C or V location.
393 SourceLocation Loc;
John McCall0953e762009-09-24 19:53:00 +0000394 if (TypeQuals & DeclSpec::TQ_const)
Chris Lattner96b77fc2008-04-02 06:50:17 +0000395 Loc = DS.getConstSpecLoc();
John McCall0953e762009-09-24 19:53:00 +0000396 else if (TypeQuals & DeclSpec::TQ_volatile)
Chris Lattner96b77fc2008-04-02 06:50:17 +0000397 Loc = DS.getVolatileSpecLoc();
John McCall0953e762009-09-24 19:53:00 +0000398 else {
399 assert((TypeQuals & DeclSpec::TQ_restrict) &&
400 "Has CVR quals but not C, V, or R?");
401 Loc = DS.getRestrictSpecLoc();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000402 }
Chris Lattner1564e392009-10-25 18:07:27 +0000403 TheSema.Diag(Loc, diag::warn_typecheck_function_qualifiers)
Chris Lattnerd1625842008-11-24 06:25:27 +0000404 << Result << DS.getSourceRange();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000405 }
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000407 // C++ [dcl.ref]p1:
408 // Cv-qualified references are ill-formed except when the
409 // cv-qualifiers are introduced through the use of a typedef
410 // (7.1.3) or of a template type argument (14.3), in which
411 // case the cv-qualifiers are ignored.
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000412 // FIXME: Shouldn't we be checking SCS_typedef here?
413 if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000414 TypeQuals && Result->isReferenceType()) {
John McCall0953e762009-09-24 19:53:00 +0000415 TypeQuals &= ~DeclSpec::TQ_const;
416 TypeQuals &= ~DeclSpec::TQ_volatile;
Mike Stump1eb44332009-09-09 15:08:12 +0000417 }
418
John McCall0953e762009-09-24 19:53:00 +0000419 Qualifiers Quals = Qualifiers::fromCVRMask(TypeQuals);
420 Result = Context.getQualifiedType(Result, Quals);
Chris Lattner96b77fc2008-04-02 06:50:17 +0000421 }
John McCall0953e762009-09-24 19:53:00 +0000422
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000423 return Result;
424}
425
Douglas Gregorcd281c32009-02-28 00:25:32 +0000426static std::string getPrintableNameForEntity(DeclarationName Entity) {
427 if (Entity)
428 return Entity.getAsString();
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Douglas Gregorcd281c32009-02-28 00:25:32 +0000430 return "type name";
431}
432
433/// \brief Build a pointer type.
434///
435/// \param T The type to which we'll be building a pointer.
436///
437/// \param Quals The cvr-qualifiers to be applied to the pointer type.
438///
439/// \param Loc The location of the entity whose type involves this
440/// pointer type or, if there is no such entity, the location of the
441/// type that will have pointer type.
442///
443/// \param Entity The name of the entity that involves the pointer
444/// type, if known.
445///
446/// \returns A suitable pointer type, if there are no
447/// errors. Otherwise, returns a NULL type.
Mike Stump1eb44332009-09-09 15:08:12 +0000448QualType Sema::BuildPointerType(QualType T, unsigned Quals,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000449 SourceLocation Loc, DeclarationName Entity) {
450 if (T->isReferenceType()) {
451 // C++ 8.3.2p4: There shall be no ... pointers to references ...
452 Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
John McCallac406052009-10-30 00:37:20 +0000453 << getPrintableNameForEntity(Entity) << T;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000454 return QualType();
455 }
456
John McCall0953e762009-09-24 19:53:00 +0000457 Qualifiers Qs = Qualifiers::fromCVRMask(Quals);
458
Douglas Gregorcd281c32009-02-28 00:25:32 +0000459 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
460 // object or incomplete types shall not be restrict-qualified."
John McCall0953e762009-09-24 19:53:00 +0000461 if (Qs.hasRestrict() && !T->isIncompleteOrObjectType()) {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000462 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
463 << T;
John McCall0953e762009-09-24 19:53:00 +0000464 Qs.removeRestrict();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000465 }
466
467 // Build the pointer type.
John McCall0953e762009-09-24 19:53:00 +0000468 return Context.getQualifiedType(Context.getPointerType(T), Qs);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000469}
470
471/// \brief Build a reference type.
472///
473/// \param T The type to which we'll be building a reference.
474///
John McCall0953e762009-09-24 19:53:00 +0000475/// \param CVR The cvr-qualifiers to be applied to the reference type.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000476///
477/// \param Loc The location of the entity whose type involves this
478/// reference type or, if there is no such entity, the location of the
479/// type that will have reference type.
480///
481/// \param Entity The name of the entity that involves the reference
482/// type, if known.
483///
484/// \returns A suitable reference type, if there are no
485/// errors. Otherwise, returns a NULL type.
John McCall54e14c42009-10-22 22:37:11 +0000486QualType Sema::BuildReferenceType(QualType T, bool SpelledAsLValue,
487 unsigned CVR, SourceLocation Loc,
488 DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000489 Qualifiers Quals = Qualifiers::fromCVRMask(CVR);
John McCall54e14c42009-10-22 22:37:11 +0000490
491 bool LValueRef = SpelledAsLValue || T->getAs<LValueReferenceType>();
492
493 // C++0x [dcl.typedef]p9: If a typedef TD names a type that is a
494 // reference to a type T, and attempt to create the type "lvalue
495 // reference to cv TD" creates the type "lvalue reference to T".
496 // We use the qualifiers (restrict or none) of the original reference,
497 // not the new ones. This is consistent with GCC.
498
499 // C++ [dcl.ref]p4: There shall be no references to references.
500 //
501 // According to C++ DR 106, references to references are only
502 // diagnosed when they are written directly (e.g., "int & &"),
503 // but not when they happen via a typedef:
504 //
505 // typedef int& intref;
506 // typedef intref& intref2;
507 //
508 // Parser::ParseDeclaratorInternal diagnoses the case where
509 // references are written directly; here, we handle the
510 // collapsing of references-to-references as described in C++
511 // DR 106 and amended by C++ DR 540.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000512
513 // C++ [dcl.ref]p1:
Eli Friedman33a31382009-08-05 19:21:58 +0000514 // A declarator that specifies the type "reference to cv void"
Douglas Gregorcd281c32009-02-28 00:25:32 +0000515 // is ill-formed.
516 if (T->isVoidType()) {
517 Diag(Loc, diag::err_reference_to_void);
518 return QualType();
519 }
520
521 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
522 // object or incomplete types shall not be restrict-qualified."
John McCall0953e762009-09-24 19:53:00 +0000523 if (Quals.hasRestrict() && !T->isIncompleteOrObjectType()) {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000524 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
525 << T;
John McCall0953e762009-09-24 19:53:00 +0000526 Quals.removeRestrict();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000527 }
528
529 // C++ [dcl.ref]p1:
530 // [...] Cv-qualified references are ill-formed except when the
531 // cv-qualifiers are introduced through the use of a typedef
532 // (7.1.3) or of a template type argument (14.3), in which case
533 // the cv-qualifiers are ignored.
534 //
535 // We diagnose extraneous cv-qualifiers for the non-typedef,
536 // non-template type argument case within the parser. Here, we just
537 // ignore any extraneous cv-qualifiers.
John McCall0953e762009-09-24 19:53:00 +0000538 Quals.removeConst();
539 Quals.removeVolatile();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000540
541 // Handle restrict on references.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000542 if (LValueRef)
John McCall54e14c42009-10-22 22:37:11 +0000543 return Context.getQualifiedType(
544 Context.getLValueReferenceType(T, SpelledAsLValue), Quals);
John McCall0953e762009-09-24 19:53:00 +0000545 return Context.getQualifiedType(Context.getRValueReferenceType(T), Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000546}
547
548/// \brief Build an array type.
549///
550/// \param T The type of each element in the array.
551///
552/// \param ASM C99 array size modifier (e.g., '*', 'static').
Mike Stump1eb44332009-09-09 15:08:12 +0000553///
554/// \param ArraySize Expression describing the size of the array.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000555///
556/// \param Quals The cvr-qualifiers to be applied to the array's
557/// element type.
558///
559/// \param Loc The location of the entity whose type involves this
560/// array type or, if there is no such entity, the location of the
561/// type that will have array type.
562///
563/// \param Entity The name of the entity that involves the array
564/// type, if known.
565///
566/// \returns A suitable array type, if there are no errors. Otherwise,
567/// returns a NULL type.
568QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
569 Expr *ArraySize, unsigned Quals,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000570 SourceRange Brackets, DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000571
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000572 SourceLocation Loc = Brackets.getBegin();
Mike Stump1eb44332009-09-09 15:08:12 +0000573 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000574 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
Sebastian Redl923d56d2009-11-05 15:52:31 +0000575 // Not in C++, though. There we only dislike void.
576 if (getLangOptions().CPlusPlus) {
577 if (T->isVoidType()) {
578 Diag(Loc, diag::err_illegal_decl_array_incomplete_type) << T;
579 return QualType();
580 }
581 } else {
582 if (RequireCompleteType(Loc, T,
583 diag::err_illegal_decl_array_incomplete_type))
584 return QualType();
585 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000586
587 if (T->isFunctionType()) {
588 Diag(Loc, diag::err_illegal_decl_array_of_functions)
John McCallac406052009-10-30 00:37:20 +0000589 << getPrintableNameForEntity(Entity) << T;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000590 return QualType();
591 }
Mike Stump1eb44332009-09-09 15:08:12 +0000592
Douglas Gregorcd281c32009-02-28 00:25:32 +0000593 // C++ 8.3.2p4: There shall be no ... arrays of references ...
594 if (T->isReferenceType()) {
595 Diag(Loc, diag::err_illegal_decl_array_of_references)
John McCallac406052009-10-30 00:37:20 +0000596 << getPrintableNameForEntity(Entity) << T;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000597 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000598 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000599
Anders Carlssone7cf07d2009-06-26 19:33:28 +0000600 if (Context.getCanonicalType(T) == Context.UndeducedAutoTy) {
Mike Stump1eb44332009-09-09 15:08:12 +0000601 Diag(Loc, diag::err_illegal_decl_array_of_auto)
Anders Carlssone7cf07d2009-06-26 19:33:28 +0000602 << getPrintableNameForEntity(Entity);
603 return QualType();
604 }
Mike Stump1eb44332009-09-09 15:08:12 +0000605
Ted Kremenek6217b802009-07-29 21:53:49 +0000606 if (const RecordType *EltTy = T->getAs<RecordType>()) {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000607 // If the element type is a struct or union that contains a variadic
608 // array, accept it as a GNU extension: C99 6.7.2.1p2.
609 if (EltTy->getDecl()->hasFlexibleArrayMember())
610 Diag(Loc, diag::ext_flexible_array_in_array) << T;
611 } else if (T->isObjCInterfaceType()) {
Chris Lattnerc7c11b12009-04-27 01:55:56 +0000612 Diag(Loc, diag::err_objc_array_of_interfaces) << T;
613 return QualType();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000614 }
Mike Stump1eb44332009-09-09 15:08:12 +0000615
Douglas Gregorcd281c32009-02-28 00:25:32 +0000616 // C99 6.7.5.2p1: The size expression shall have integer type.
617 if (ArraySize && !ArraySize->isTypeDependent() &&
618 !ArraySize->getType()->isIntegerType()) {
619 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
620 << ArraySize->getType() << ArraySize->getSourceRange();
621 ArraySize->Destroy(Context);
622 return QualType();
623 }
624 llvm::APSInt ConstVal(32);
625 if (!ArraySize) {
Eli Friedmanf91f5c82009-04-26 21:57:51 +0000626 if (ASM == ArrayType::Star)
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000627 T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets);
Eli Friedmanf91f5c82009-04-26 21:57:51 +0000628 else
629 T = Context.getIncompleteArrayType(T, ASM, Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000630 } else if (ArraySize->isValueDependent()) {
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000631 T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000632 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
Sebastian Redl923d56d2009-11-05 15:52:31 +0000633 (!T->isDependentType() && !T->isIncompleteType() &&
634 !T->isConstantSizeType())) {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000635 // Per C99, a variable array is an array with either a non-constant
636 // size or an element type that has a non-constant-size
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000637 T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000638 } else {
639 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
640 // have a value greater than zero.
Sebastian Redl923d56d2009-11-05 15:52:31 +0000641 if (ConstVal.isSigned() && ConstVal.isNegative()) {
642 Diag(ArraySize->getLocStart(),
643 diag::err_typecheck_negative_array_size)
644 << ArraySize->getSourceRange();
645 return QualType();
646 }
647 if (ConstVal == 0) {
648 // GCC accepts zero sized static arrays.
649 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size)
650 << ArraySize->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000651 }
John McCall46a617a2009-10-16 00:14:28 +0000652 T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000653 }
David Chisnallaf407762010-01-11 23:08:08 +0000654 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
655 if (!getLangOptions().C99) {
Mike Stump1eb44332009-09-09 15:08:12 +0000656 if (ArraySize && !ArraySize->isTypeDependent() &&
657 !ArraySize->isValueDependent() &&
Douglas Gregorcd281c32009-02-28 00:25:32 +0000658 !ArraySize->isIntegerConstantExpr(Context))
Douglas Gregor043cad22009-09-11 00:18:58 +0000659 Diag(Loc, getLangOptions().CPlusPlus? diag::err_vla_cxx : diag::ext_vla);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000660 else if (ASM != ArrayType::Normal || Quals != 0)
Douglas Gregor043cad22009-09-11 00:18:58 +0000661 Diag(Loc,
662 getLangOptions().CPlusPlus? diag::err_c99_array_usage_cxx
663 : diag::ext_c99_array_usage);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000664 }
665
666 return T;
667}
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000668
669/// \brief Build an ext-vector type.
670///
671/// Run the required checks for the extended vector type.
Mike Stump1eb44332009-09-09 15:08:12 +0000672QualType Sema::BuildExtVectorType(QualType T, ExprArg ArraySize,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000673 SourceLocation AttrLoc) {
674
675 Expr *Arg = (Expr *)ArraySize.get();
676
677 // unlike gcc's vector_size attribute, we do not allow vectors to be defined
678 // in conjunction with complex types (pointers, arrays, functions, etc.).
Mike Stump1eb44332009-09-09 15:08:12 +0000679 if (!T->isDependentType() &&
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000680 !T->isIntegerType() && !T->isRealFloatingType()) {
681 Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T;
682 return QualType();
683 }
684
685 if (!Arg->isTypeDependent() && !Arg->isValueDependent()) {
686 llvm::APSInt vecSize(32);
687 if (!Arg->isIntegerConstantExpr(vecSize, Context)) {
688 Diag(AttrLoc, diag::err_attribute_argument_not_int)
689 << "ext_vector_type" << Arg->getSourceRange();
690 return QualType();
691 }
Mike Stump1eb44332009-09-09 15:08:12 +0000692
693 // unlike gcc's vector_size attribute, the size is specified as the
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000694 // number of elements, not the number of bytes.
Mike Stump1eb44332009-09-09 15:08:12 +0000695 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
696
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000697 if (vectorSize == 0) {
698 Diag(AttrLoc, diag::err_attribute_zero_size)
699 << Arg->getSourceRange();
700 return QualType();
701 }
Mike Stump1eb44332009-09-09 15:08:12 +0000702
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000703 if (!T->isDependentType())
704 return Context.getExtVectorType(T, vectorSize);
Mike Stump1eb44332009-09-09 15:08:12 +0000705 }
706
707 return Context.getDependentSizedExtVectorType(T, ArraySize.takeAs<Expr>(),
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000708 AttrLoc);
709}
Mike Stump1eb44332009-09-09 15:08:12 +0000710
Douglas Gregor724651c2009-02-28 01:04:19 +0000711/// \brief Build a function type.
712///
713/// This routine checks the function type according to C++ rules and
714/// under the assumption that the result type and parameter types have
715/// just been instantiated from a template. It therefore duplicates
Douglas Gregor2943aed2009-03-03 04:44:36 +0000716/// some of the behavior of GetTypeForDeclarator, but in a much
Douglas Gregor724651c2009-02-28 01:04:19 +0000717/// simpler form that is only suitable for this narrow use case.
718///
719/// \param T The return type of the function.
720///
721/// \param ParamTypes The parameter types of the function. This array
722/// will be modified to account for adjustments to the types of the
723/// function parameters.
724///
725/// \param NumParamTypes The number of parameter types in ParamTypes.
726///
727/// \param Variadic Whether this is a variadic function type.
728///
729/// \param Quals The cvr-qualifiers to be applied to the function type.
730///
731/// \param Loc The location of the entity whose type involves this
732/// function type or, if there is no such entity, the location of the
733/// type that will have function type.
734///
735/// \param Entity The name of the entity that involves the function
736/// type, if known.
737///
738/// \returns A suitable function type, if there are no
739/// errors. Otherwise, returns a NULL type.
740QualType Sema::BuildFunctionType(QualType T,
Mike Stump1eb44332009-09-09 15:08:12 +0000741 QualType *ParamTypes,
Douglas Gregor724651c2009-02-28 01:04:19 +0000742 unsigned NumParamTypes,
743 bool Variadic, unsigned Quals,
744 SourceLocation Loc, DeclarationName Entity) {
745 if (T->isArrayType() || T->isFunctionType()) {
Douglas Gregor58408bc2010-01-11 18:46:21 +0000746 Diag(Loc, diag::err_func_returning_array_function)
747 << T->isFunctionType() << T;
Douglas Gregor724651c2009-02-28 01:04:19 +0000748 return QualType();
749 }
Mike Stump1eb44332009-09-09 15:08:12 +0000750
Douglas Gregor724651c2009-02-28 01:04:19 +0000751 bool Invalid = false;
752 for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) {
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000753 QualType ParamType = adjustParameterType(ParamTypes[Idx]);
754 if (ParamType->isVoidType()) {
Douglas Gregor724651c2009-02-28 01:04:19 +0000755 Diag(Loc, diag::err_param_with_void_type);
756 Invalid = true;
757 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000758
John McCall54e14c42009-10-22 22:37:11 +0000759 ParamTypes[Idx] = ParamType;
Douglas Gregor724651c2009-02-28 01:04:19 +0000760 }
761
762 if (Invalid)
763 return QualType();
764
Mike Stump1eb44332009-09-09 15:08:12 +0000765 return Context.getFunctionType(T, ParamTypes, NumParamTypes, Variadic,
Douglas Gregor724651c2009-02-28 01:04:19 +0000766 Quals);
767}
Mike Stump1eb44332009-09-09 15:08:12 +0000768
Douglas Gregor949bf692009-06-09 22:17:39 +0000769/// \brief Build a member pointer type \c T Class::*.
770///
771/// \param T the type to which the member pointer refers.
772/// \param Class the class type into which the member pointer points.
John McCall0953e762009-09-24 19:53:00 +0000773/// \param CVR Qualifiers applied to the member pointer type
Douglas Gregor949bf692009-06-09 22:17:39 +0000774/// \param Loc the location where this type begins
775/// \param Entity the name of the entity that will have this member pointer type
776///
777/// \returns a member pointer type, if successful, or a NULL type if there was
778/// an error.
Mike Stump1eb44332009-09-09 15:08:12 +0000779QualType Sema::BuildMemberPointerType(QualType T, QualType Class,
John McCall0953e762009-09-24 19:53:00 +0000780 unsigned CVR, SourceLocation Loc,
Douglas Gregor949bf692009-06-09 22:17:39 +0000781 DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000782 Qualifiers Quals = Qualifiers::fromCVRMask(CVR);
783
Douglas Gregor949bf692009-06-09 22:17:39 +0000784 // Verify that we're not building a pointer to pointer to function with
785 // exception specification.
786 if (CheckDistantExceptionSpec(T)) {
787 Diag(Loc, diag::err_distant_exception_spec);
788
789 // FIXME: If we're doing this as part of template instantiation,
790 // we should return immediately.
791
792 // Build the type anyway, but use the canonical type so that the
793 // exception specifiers are stripped off.
794 T = Context.getCanonicalType(T);
795 }
796
797 // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member
798 // with reference type, or "cv void."
799 if (T->isReferenceType()) {
Anders Carlsson8d4655d2009-06-30 00:06:57 +0000800 Diag(Loc, diag::err_illegal_decl_mempointer_to_reference)
John McCallac406052009-10-30 00:37:20 +0000801 << (Entity? Entity.getAsString() : "type name") << T;
Douglas Gregor949bf692009-06-09 22:17:39 +0000802 return QualType();
803 }
804
805 if (T->isVoidType()) {
806 Diag(Loc, diag::err_illegal_decl_mempointer_to_void)
807 << (Entity? Entity.getAsString() : "type name");
808 return QualType();
809 }
810
811 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
812 // object or incomplete types shall not be restrict-qualified."
John McCall0953e762009-09-24 19:53:00 +0000813 if (Quals.hasRestrict() && !T->isIncompleteOrObjectType()) {
Douglas Gregor949bf692009-06-09 22:17:39 +0000814 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
815 << T;
816
817 // FIXME: If we're doing this as part of template instantiation,
818 // we should return immediately.
John McCall0953e762009-09-24 19:53:00 +0000819 Quals.removeRestrict();
Douglas Gregor949bf692009-06-09 22:17:39 +0000820 }
821
822 if (!Class->isDependentType() && !Class->isRecordType()) {
823 Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class;
824 return QualType();
825 }
826
John McCall0953e762009-09-24 19:53:00 +0000827 return Context.getQualifiedType(
828 Context.getMemberPointerType(T, Class.getTypePtr()), Quals);
Douglas Gregor949bf692009-06-09 22:17:39 +0000829}
Mike Stump1eb44332009-09-09 15:08:12 +0000830
Anders Carlsson9a917e42009-06-12 22:56:54 +0000831/// \brief Build a block pointer type.
832///
833/// \param T The type to which we'll be building a block pointer.
834///
John McCall0953e762009-09-24 19:53:00 +0000835/// \param CVR The cvr-qualifiers to be applied to the block pointer type.
Anders Carlsson9a917e42009-06-12 22:56:54 +0000836///
837/// \param Loc The location of the entity whose type involves this
838/// block pointer type or, if there is no such entity, the location of the
839/// type that will have block pointer type.
840///
841/// \param Entity The name of the entity that involves the block pointer
842/// type, if known.
843///
844/// \returns A suitable block pointer type, if there are no
845/// errors. Otherwise, returns a NULL type.
John McCall0953e762009-09-24 19:53:00 +0000846QualType Sema::BuildBlockPointerType(QualType T, unsigned CVR,
Mike Stump1eb44332009-09-09 15:08:12 +0000847 SourceLocation Loc,
Anders Carlsson9a917e42009-06-12 22:56:54 +0000848 DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000849 if (!T->isFunctionType()) {
Anders Carlsson9a917e42009-06-12 22:56:54 +0000850 Diag(Loc, diag::err_nonfunction_block_type);
851 return QualType();
852 }
Mike Stump1eb44332009-09-09 15:08:12 +0000853
John McCall0953e762009-09-24 19:53:00 +0000854 Qualifiers Quals = Qualifiers::fromCVRMask(CVR);
855 return Context.getQualifiedType(Context.getBlockPointerType(T), Quals);
Anders Carlsson9a917e42009-06-12 22:56:54 +0000856}
857
John McCalla93c9342009-12-07 02:54:59 +0000858QualType Sema::GetTypeFromParser(TypeTy *Ty, TypeSourceInfo **TInfo) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000859 QualType QT = QualType::getFromOpaquePtr(Ty);
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000860 if (QT.isNull()) {
John McCalla93c9342009-12-07 02:54:59 +0000861 if (TInfo) *TInfo = 0;
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000862 return QualType();
863 }
864
John McCalla93c9342009-12-07 02:54:59 +0000865 TypeSourceInfo *DI = 0;
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000866 if (LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) {
867 QT = LIT->getType();
John McCalla93c9342009-12-07 02:54:59 +0000868 DI = LIT->getTypeSourceInfo();
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000869 }
Mike Stump1eb44332009-09-09 15:08:12 +0000870
John McCalla93c9342009-12-07 02:54:59 +0000871 if (TInfo) *TInfo = DI;
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000872 return QT;
873}
874
Mike Stump98eb8a72009-02-04 22:31:32 +0000875/// GetTypeForDeclarator - Convert the type for the specified
Sebastian Redl8ce35b02009-10-25 21:45:37 +0000876/// declarator to Type instances.
Douglas Gregor402abb52009-05-28 23:31:59 +0000877///
878/// If OwnedDecl is non-NULL, and this declarator's decl-specifier-seq
879/// owns the declaration of a type (e.g., the definition of a struct
880/// type), then *OwnedDecl will receive the owned declaration.
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000881QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S,
John McCalla93c9342009-12-07 02:54:59 +0000882 TypeSourceInfo **TInfo,
Douglas Gregor402abb52009-05-28 23:31:59 +0000883 TagDecl **OwnedDecl) {
Douglas Gregor930d8b52009-01-30 22:09:00 +0000884 // Determine the type of the declarator. Not all forms of declarator
885 // have a type.
886 QualType T;
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000887
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000888 switch (D.getName().getKind()) {
889 case UnqualifiedId::IK_Identifier:
890 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunt0486d742009-11-28 04:44:28 +0000891 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000892 case UnqualifiedId::IK_TemplateId:
Sebastian Redl8ce35b02009-10-25 21:45:37 +0000893 T = ConvertDeclSpecToType(D, *this);
Chris Lattner5db2bb12009-10-25 18:21:37 +0000894
895 if (!D.isInvalidType() && OwnedDecl && D.getDeclSpec().isTypeSpecOwned())
896 *OwnedDecl = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep());
Douglas Gregor930d8b52009-01-30 22:09:00 +0000897 break;
898
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000899 case UnqualifiedId::IK_ConstructorName:
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000900 case UnqualifiedId::IK_ConstructorTemplateId:
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000901 case UnqualifiedId::IK_DestructorName:
Douglas Gregor930d8b52009-01-30 22:09:00 +0000902 // Constructors and destructors don't have return types. Use
Douglas Gregor48026d22010-01-11 18:40:55 +0000903 // "void" instead.
Douglas Gregor930d8b52009-01-30 22:09:00 +0000904 T = Context.VoidTy;
905 break;
Douglas Gregor48026d22010-01-11 18:40:55 +0000906
907 case UnqualifiedId::IK_ConversionFunctionId:
908 // The result type of a conversion function is the type that it
909 // converts to.
910 T = GetTypeFromParser(D.getName().ConversionFunctionId);
911 break;
Douglas Gregor930d8b52009-01-30 22:09:00 +0000912 }
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000913
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000914 if (T.isNull())
915 return T;
916
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000917 if (T == Context.UndeducedAutoTy) {
918 int Error = -1;
Mike Stump1eb44332009-09-09 15:08:12 +0000919
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000920 switch (D.getContext()) {
921 case Declarator::KNRTypeListContext:
922 assert(0 && "K&R type lists aren't allowed in C++");
923 break;
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000924 case Declarator::PrototypeContext:
925 Error = 0; // Function prototype
926 break;
927 case Declarator::MemberContext:
928 switch (cast<TagDecl>(CurContext)->getTagKind()) {
929 case TagDecl::TK_enum: assert(0 && "unhandled tag kind"); break;
930 case TagDecl::TK_struct: Error = 1; /* Struct member */ break;
931 case TagDecl::TK_union: Error = 2; /* Union member */ break;
932 case TagDecl::TK_class: Error = 3; /* Class member */ break;
Mike Stump1eb44332009-09-09 15:08:12 +0000933 }
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000934 break;
935 case Declarator::CXXCatchContext:
936 Error = 4; // Exception declaration
937 break;
938 case Declarator::TemplateParamContext:
939 Error = 5; // Template parameter
940 break;
941 case Declarator::BlockLiteralContext:
942 Error = 6; // Block literal
943 break;
944 case Declarator::FileContext:
945 case Declarator::BlockContext:
946 case Declarator::ForContext:
947 case Declarator::ConditionContext:
948 case Declarator::TypeNameContext:
949 break;
950 }
951
952 if (Error != -1) {
953 Diag(D.getDeclSpec().getTypeSpecTypeLoc(), diag::err_auto_not_allowed)
954 << Error;
955 T = Context.IntTy;
956 D.setInvalidType(true);
957 }
958 }
Mike Stump1eb44332009-09-09 15:08:12 +0000959
Douglas Gregorcd281c32009-02-28 00:25:32 +0000960 // The name we're declaring, if any.
961 DeclarationName Name;
962 if (D.getIdentifier())
963 Name = D.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +0000964
Mike Stump98eb8a72009-02-04 22:31:32 +0000965 // Walk the DeclTypeInfo, building the recursive type as we go.
966 // DeclTypeInfos are ordered from the identifier out, which is
967 // opposite of what we want :).
Sebastian Redl8ce35b02009-10-25 21:45:37 +0000968 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
969 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000970 switch (DeclType.Kind) {
971 default: assert(0 && "Unknown decltype!");
Steve Naroff5618bd42008-08-27 16:04:49 +0000972 case DeclaratorChunk::BlockPointer:
Chris Lattner9af55002009-03-27 04:18:06 +0000973 // If blocks are disabled, emit an error.
974 if (!LangOpts.Blocks)
975 Diag(DeclType.Loc, diag::err_blocks_disable);
Mike Stump1eb44332009-09-09 15:08:12 +0000976
977 T = BuildBlockPointerType(T, DeclType.Cls.TypeQuals, D.getIdentifierLoc(),
Anders Carlsson9a917e42009-06-12 22:56:54 +0000978 Name);
Steve Naroff5618bd42008-08-27 16:04:49 +0000979 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000980 case DeclaratorChunk::Pointer:
Sebastian Redl6a7330c2009-05-29 15:01:05 +0000981 // Verify that we're not building a pointer to pointer to function with
982 // exception specification.
983 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
984 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
985 D.setInvalidType(true);
986 // Build the type anyway.
987 }
Steve Naroff14108da2009-07-10 23:34:53 +0000988 if (getLangOptions().ObjC1 && T->isObjCInterfaceType()) {
John McCall183700f2009-09-21 23:43:11 +0000989 const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>();
Steve Naroff14108da2009-07-10 23:34:53 +0000990 T = Context.getObjCObjectPointerType(T,
Steve Naroff67ef8ea2009-07-20 17:56:53 +0000991 (ObjCProtocolDecl **)OIT->qual_begin(),
992 OIT->getNumProtocols());
Steve Naroff14108da2009-07-10 23:34:53 +0000993 break;
994 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000995 T = BuildPointerType(T, DeclType.Ptr.TypeQuals, DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000996 break;
John McCall0953e762009-09-24 19:53:00 +0000997 case DeclaratorChunk::Reference: {
998 Qualifiers Quals;
999 if (DeclType.Ref.HasRestrict) Quals.addRestrict();
1000
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001001 // Verify that we're not building a reference to pointer to function with
1002 // exception specification.
1003 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1004 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1005 D.setInvalidType(true);
1006 // Build the type anyway.
1007 }
John McCall0953e762009-09-24 19:53:00 +00001008 T = BuildReferenceType(T, DeclType.Ref.LValueRef, Quals,
Douglas Gregorcd281c32009-02-28 00:25:32 +00001009 DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +00001010 break;
John McCall0953e762009-09-24 19:53:00 +00001011 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001012 case DeclaratorChunk::Array: {
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001013 // Verify that we're not building an array of pointers to function with
1014 // exception specification.
1015 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1016 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1017 D.setInvalidType(true);
1018 // Build the type anyway.
1019 }
Chris Lattnerfd89bc82008-04-02 01:05:10 +00001020 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner94f81fd2007-08-28 16:54:00 +00001021 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001022 ArrayType::ArraySizeModifier ASM;
1023 if (ATI.isStar)
1024 ASM = ArrayType::Star;
1025 else if (ATI.hasStatic)
1026 ASM = ArrayType::Static;
1027 else
1028 ASM = ArrayType::Normal;
Eli Friedmanf91f5c82009-04-26 21:57:51 +00001029 if (ASM == ArrayType::Star &&
1030 D.getContext() != Declarator::PrototypeContext) {
1031 // FIXME: This check isn't quite right: it allows star in prototypes
1032 // for function definitions, and disallows some edge cases detailed
1033 // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html
1034 Diag(DeclType.Loc, diag::err_array_star_outside_prototype);
1035 ASM = ArrayType::Normal;
1036 D.setInvalidType(true);
1037 }
John McCall0953e762009-09-24 19:53:00 +00001038 T = BuildArrayType(T, ASM, ArraySize,
1039 Qualifiers::fromCVRMask(ATI.TypeQuals),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001040 SourceRange(DeclType.Loc, DeclType.EndLoc), Name);
Reid Spencer5f016e22007-07-11 17:01:13 +00001041 break;
1042 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001043 case DeclaratorChunk::Function: {
Reid Spencer5f016e22007-07-11 17:01:13 +00001044 // If the function declarator has a prototype (i.e. it is not () and
1045 // does not have a K&R-style identifier list), then the arguments are part
1046 // of the type, otherwise the argument list is ().
1047 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Sebastian Redl3cc97262009-05-31 11:47:27 +00001048
Chris Lattnercd881292007-12-19 05:31:29 +00001049 // C99 6.7.5.3p1: The return type may not be a function or array type.
Douglas Gregor58408bc2010-01-11 18:46:21 +00001050 // For conversion functions, we'll diagnose this particular error later.
Douglas Gregor48026d22010-01-11 18:40:55 +00001051 if ((T->isArrayType() || T->isFunctionType()) &&
1052 (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId)) {
Douglas Gregor58408bc2010-01-11 18:46:21 +00001053 Diag(DeclType.Loc, diag::err_func_returning_array_function)
1054 << T->isFunctionType() << T;
Chris Lattnercd881292007-12-19 05:31:29 +00001055 T = Context.IntTy;
1056 D.setInvalidType(true);
1057 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001058
Douglas Gregor402abb52009-05-28 23:31:59 +00001059 if (getLangOptions().CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) {
1060 // C++ [dcl.fct]p6:
1061 // Types shall not be defined in return or parameter types.
1062 TagDecl *Tag = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep());
1063 if (Tag->isDefinition())
1064 Diag(Tag->getLocation(), diag::err_type_defined_in_result_type)
1065 << Context.getTypeDeclType(Tag);
1066 }
1067
Sebastian Redl3cc97262009-05-31 11:47:27 +00001068 // Exception specs are not allowed in typedefs. Complain, but add it
1069 // anyway.
1070 if (FTI.hasExceptionSpec &&
1071 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
1072 Diag(FTI.getThrowLoc(), diag::err_exception_spec_in_typedef);
1073
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001074 if (FTI.NumArgs == 0) {
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001075 if (getLangOptions().CPlusPlus) {
1076 // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the
1077 // function takes no arguments.
Sebastian Redl465226e2009-05-27 22:11:52 +00001078 llvm::SmallVector<QualType, 4> Exceptions;
1079 Exceptions.reserve(FTI.NumExceptions);
Mike Stump1eb44332009-09-09 15:08:12 +00001080 for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001081 // FIXME: Preserve type source info.
1082 QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty);
Sebastian Redlef65f062009-05-29 18:02:33 +00001083 // Check that the type is valid for an exception spec, and drop it
1084 // if not.
1085 if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1086 Exceptions.push_back(ET);
1087 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001088 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, FTI.TypeQuals,
1089 FTI.hasExceptionSpec,
1090 FTI.hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00001091 Exceptions.size(), Exceptions.data());
Douglas Gregor965acbb2009-02-18 07:07:28 +00001092 } else if (FTI.isVariadic) {
1093 // We allow a zero-parameter variadic function in C if the
1094 // function is marked with the "overloadable"
1095 // attribute. Scan for this attribute now.
1096 bool Overloadable = false;
1097 for (const AttributeList *Attrs = D.getAttributes();
1098 Attrs; Attrs = Attrs->getNext()) {
1099 if (Attrs->getKind() == AttributeList::AT_overloadable) {
1100 Overloadable = true;
1101 break;
1102 }
1103 }
1104
1105 if (!Overloadable)
1106 Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
1107 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, 0);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001108 } else {
1109 // Simple void foo(), where the incoming T is the result type.
Douglas Gregor72564e72009-02-26 23:50:07 +00001110 T = Context.getFunctionNoProtoType(T);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001111 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001112 } else if (FTI.ArgInfo[0].Param == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001113 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
Mike Stump1eb44332009-09-09 15:08:12 +00001114 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
John McCall54e14c42009-10-22 22:37:11 +00001115 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001116 } else {
1117 // Otherwise, we have a function with an argument list that is
1118 // potentially variadic.
1119 llvm::SmallVector<QualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +00001120
Reid Spencer5f016e22007-07-11 17:01:13 +00001121 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001122 ParmVarDecl *Param =
1123 cast<ParmVarDecl>(FTI.ArgInfo[i].Param.getAs<Decl>());
Chris Lattner8123a952008-04-10 02:22:51 +00001124 QualType ArgTy = Param->getType();
Chris Lattner78c75fb2007-07-21 05:30:18 +00001125 assert(!ArgTy.isNull() && "Couldn't parse type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001126
1127 // Adjust the parameter type.
Douglas Gregorbeb58cb2009-03-23 23:17:00 +00001128 assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001129
Reid Spencer5f016e22007-07-11 17:01:13 +00001130 // Look for 'void'. void is allowed only as a single argument to a
1131 // function with no other parameters (C99 6.7.5.3p10). We record
Douglas Gregor72564e72009-02-26 23:50:07 +00001132 // int(void) as a FunctionProtoType with an empty argument list.
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001133 if (ArgTy->isVoidType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001134 // If this is something like 'float(int, void)', reject it. 'void'
1135 // is an incomplete type (C99 6.2.5p19) and function decls cannot
1136 // have arguments of incomplete type.
1137 if (FTI.NumArgs != 1 || FTI.isVariadic) {
1138 Diag(DeclType.Loc, diag::err_void_only_param);
Chris Lattner2ff54262007-07-21 05:18:12 +00001139 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00001140 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00001141 } else if (FTI.ArgInfo[i].Ident) {
1142 // Reject, but continue to parse 'int(void abc)'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001143 Diag(FTI.ArgInfo[i].IdentLoc,
Chris Lattner4565d4e2007-07-21 05:26:43 +00001144 diag::err_param_with_void_type);
Chris Lattner2ff54262007-07-21 05:18:12 +00001145 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00001146 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00001147 } else {
1148 // Reject, but continue to parse 'float(const void)'.
John McCall0953e762009-09-24 19:53:00 +00001149 if (ArgTy.hasQualifiers())
Chris Lattner2ff54262007-07-21 05:18:12 +00001150 Diag(DeclType.Loc, diag::err_void_param_qualified);
Mike Stump1eb44332009-09-09 15:08:12 +00001151
Chris Lattner2ff54262007-07-21 05:18:12 +00001152 // Do not add 'void' to the ArgTys list.
1153 break;
1154 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001155 } else if (!FTI.hasPrototype) {
1156 if (ArgTy->isPromotableIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00001157 ArgTy = Context.getPromotedIntegerType(ArgTy);
John McCall183700f2009-09-21 23:43:11 +00001158 } else if (const BuiltinType* BTy = ArgTy->getAs<BuiltinType>()) {
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001159 if (BTy->getKind() == BuiltinType::Float)
1160 ArgTy = Context.DoubleTy;
1161 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001162 }
Mike Stump1eb44332009-09-09 15:08:12 +00001163
John McCall54e14c42009-10-22 22:37:11 +00001164 ArgTys.push_back(ArgTy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001165 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001166
1167 llvm::SmallVector<QualType, 4> Exceptions;
1168 Exceptions.reserve(FTI.NumExceptions);
Mike Stump1eb44332009-09-09 15:08:12 +00001169 for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001170 // FIXME: Preserve type source info.
1171 QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty);
Sebastian Redlef65f062009-05-29 18:02:33 +00001172 // Check that the type is valid for an exception spec, and drop it if
1173 // not.
1174 if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1175 Exceptions.push_back(ET);
1176 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001177
Jay Foadbeaaccd2009-05-21 09:52:38 +00001178 T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(),
Sebastian Redl465226e2009-05-27 22:11:52 +00001179 FTI.isVariadic, FTI.TypeQuals,
1180 FTI.hasExceptionSpec,
1181 FTI.hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00001182 Exceptions.size(), Exceptions.data());
Reid Spencer5f016e22007-07-11 17:01:13 +00001183 }
1184 break;
1185 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001186 case DeclaratorChunk::MemberPointer:
Sebastian Redl4994d2d2009-07-04 11:39:00 +00001187 // Verify that we're not building a pointer to pointer to function with
1188 // exception specification.
1189 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1190 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1191 D.setInvalidType(true);
1192 // Build the type anyway.
1193 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001194 // The scope spec must refer to a class, or be dependent.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001195 QualType ClsType;
Douglas Gregor87c12c42009-11-04 16:49:01 +00001196 if (isDependentScopeSpecifier(DeclType.Mem.Scope())
1197 || dyn_cast_or_null<CXXRecordDecl>(
1198 computeDeclContext(DeclType.Mem.Scope()))) {
Mike Stump1eb44332009-09-09 15:08:12 +00001199 NestedNameSpecifier *NNS
Douglas Gregor949bf692009-06-09 22:17:39 +00001200 = (NestedNameSpecifier *)DeclType.Mem.Scope().getScopeRep();
Douglas Gregor87c12c42009-11-04 16:49:01 +00001201 NestedNameSpecifier *NNSPrefix = NNS->getPrefix();
1202 switch (NNS->getKind()) {
1203 case NestedNameSpecifier::Identifier:
1204 ClsType = Context.getTypenameType(NNSPrefix, NNS->getAsIdentifier());
1205 break;
1206
1207 case NestedNameSpecifier::Namespace:
1208 case NestedNameSpecifier::Global:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001209 llvm_unreachable("Nested-name-specifier must name a type");
Douglas Gregor87c12c42009-11-04 16:49:01 +00001210 break;
1211
1212 case NestedNameSpecifier::TypeSpec:
1213 case NestedNameSpecifier::TypeSpecWithTemplate:
1214 ClsType = QualType(NNS->getAsType(), 0);
1215 if (NNSPrefix)
1216 ClsType = Context.getQualifiedNameType(NNSPrefix, ClsType);
1217 break;
1218 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001219 } else {
Douglas Gregor949bf692009-06-09 22:17:39 +00001220 Diag(DeclType.Mem.Scope().getBeginLoc(),
1221 diag::err_illegal_decl_mempointer_in_nonclass)
1222 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
1223 << DeclType.Mem.Scope().getRange();
Sebastian Redlf30208a2009-01-24 21:16:55 +00001224 D.setInvalidType(true);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001225 }
1226
Douglas Gregor949bf692009-06-09 22:17:39 +00001227 if (!ClsType.isNull())
1228 T = BuildMemberPointerType(T, ClsType, DeclType.Mem.TypeQuals,
1229 DeclType.Loc, D.getIdentifier());
1230 if (T.isNull()) {
1231 T = Context.IntTy;
Sebastian Redlf30208a2009-01-24 21:16:55 +00001232 D.setInvalidType(true);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001233 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001234 break;
1235 }
1236
Douglas Gregorcd281c32009-02-28 00:25:32 +00001237 if (T.isNull()) {
1238 D.setInvalidType(true);
1239 T = Context.IntTy;
1240 }
1241
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001242 // See if there are any attributes on this declarator chunk.
1243 if (const AttributeList *AL = DeclType.getAttrs())
John McCallf82b4e82010-02-04 05:44:44 +00001244 ProcessTypeAttributeList(T, AL, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001245 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001246
1247 if (getLangOptions().CPlusPlus && T->isFunctionType()) {
John McCall183700f2009-09-21 23:43:11 +00001248 const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>();
Chris Lattner778ed742009-10-25 17:36:50 +00001249 assert(FnTy && "Why oh why is there not a FunctionProtoType here?");
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001250
1251 // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type
1252 // for a nonstatic member function, the function type to which a pointer
1253 // to member refers, or the top-level function type of a function typedef
1254 // declaration.
1255 if (FnTy->getTypeQuals() != 0 &&
1256 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
Douglas Gregor584049d2008-12-15 23:53:10 +00001257 ((D.getContext() != Declarator::MemberContext &&
1258 (!D.getCXXScopeSpec().isSet() ||
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001259 !computeDeclContext(D.getCXXScopeSpec(), /*FIXME:*/true)
1260 ->isRecord())) ||
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001261 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001262 if (D.isFunctionDeclarator())
1263 Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
1264 else
1265 Diag(D.getIdentifierLoc(),
1266 diag::err_invalid_qualified_typedef_function_type_use);
1267
1268 // Strip the cv-quals from the type.
1269 T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001270 FnTy->getNumArgs(), FnTy->isVariadic(), 0);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001271 }
1272 }
Mike Stump1eb44332009-09-09 15:08:12 +00001273
Chris Lattner0bf29ad2008-06-29 00:19:33 +00001274 // If there were any type attributes applied to the decl itself (not the
1275 // type, apply the type attribute to the type!)
1276 if (const AttributeList *Attrs = D.getAttributes())
John McCallf82b4e82010-02-04 05:44:44 +00001277 ProcessTypeAttributeList(T, Attrs, true);
1278 // Also look in the decl spec.
1279 if (const AttributeList *Attrs = D.getDeclSpec().getAttributes())
1280 ProcessTypeAttributeList(T, Attrs, true, true);
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001281
John McCalla93c9342009-12-07 02:54:59 +00001282 if (TInfo) {
John McCall54e14c42009-10-22 22:37:11 +00001283 if (D.isInvalidType())
John McCalla93c9342009-12-07 02:54:59 +00001284 *TInfo = 0;
John McCall54e14c42009-10-22 22:37:11 +00001285 else
John McCalla93c9342009-12-07 02:54:59 +00001286 *TInfo = GetTypeSourceInfoForDeclarator(D, T);
John McCall54e14c42009-10-22 22:37:11 +00001287 }
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001288
Reid Spencer5f016e22007-07-11 17:01:13 +00001289 return T;
1290}
1291
John McCall51bd8032009-10-18 01:05:36 +00001292namespace {
1293 class TypeSpecLocFiller : public TypeLocVisitor<TypeSpecLocFiller> {
1294 const DeclSpec &DS;
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001295
John McCall51bd8032009-10-18 01:05:36 +00001296 public:
1297 TypeSpecLocFiller(const DeclSpec &DS) : DS(DS) {}
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001298
John McCall51bd8032009-10-18 01:05:36 +00001299 void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
1300 Visit(TL.getUnqualifiedLoc());
1301 }
1302 void VisitTypedefTypeLoc(TypedefTypeLoc TL) {
1303 TL.setNameLoc(DS.getTypeSpecTypeLoc());
1304 }
1305 void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
1306 TL.setNameLoc(DS.getTypeSpecTypeLoc());
Argyrios Kyrtzidiseb667592009-09-29 19:45:22 +00001307
John McCall54e14c42009-10-22 22:37:11 +00001308 if (DS.getProtocolQualifiers()) {
1309 assert(TL.getNumProtocols() > 0);
1310 assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
1311 TL.setLAngleLoc(DS.getProtocolLAngleLoc());
1312 TL.setRAngleLoc(DS.getSourceRange().getEnd());
1313 for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i)
1314 TL.setProtocolLoc(i, DS.getProtocolLocs()[i]);
1315 } else {
1316 assert(TL.getNumProtocols() == 0);
1317 TL.setLAngleLoc(SourceLocation());
1318 TL.setRAngleLoc(SourceLocation());
1319 }
1320 }
1321 void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
1322 assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
1323
1324 TL.setStarLoc(SourceLocation());
1325
1326 if (DS.getProtocolQualifiers()) {
1327 assert(TL.getNumProtocols() > 0);
1328 assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
1329 TL.setHasProtocolsAsWritten(true);
1330 TL.setLAngleLoc(DS.getProtocolLAngleLoc());
1331 TL.setRAngleLoc(DS.getSourceRange().getEnd());
1332 for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i)
1333 TL.setProtocolLoc(i, DS.getProtocolLocs()[i]);
1334
1335 } else {
1336 assert(TL.getNumProtocols() == 0);
1337 TL.setHasProtocolsAsWritten(false);
1338 TL.setLAngleLoc(SourceLocation());
1339 TL.setRAngleLoc(SourceLocation());
1340 }
1341
1342 // This might not have been written with an inner type.
1343 if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) {
1344 TL.setHasBaseTypeAsWritten(false);
1345 TL.getBaseTypeLoc().initialize(SourceLocation());
1346 } else {
1347 TL.setHasBaseTypeAsWritten(true);
John McCall51bd8032009-10-18 01:05:36 +00001348 Visit(TL.getBaseTypeLoc());
John McCall54e14c42009-10-22 22:37:11 +00001349 }
John McCall51bd8032009-10-18 01:05:36 +00001350 }
John McCall833ca992009-10-29 08:12:44 +00001351 void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) {
John McCalla93c9342009-12-07 02:54:59 +00001352 TypeSourceInfo *TInfo = 0;
1353 Sema::GetTypeFromParser(DS.getTypeRep(), &TInfo);
John McCall833ca992009-10-29 08:12:44 +00001354
1355 // If we got no declarator info from previous Sema routines,
1356 // just fill with the typespec loc.
John McCalla93c9342009-12-07 02:54:59 +00001357 if (!TInfo) {
John McCall833ca992009-10-29 08:12:44 +00001358 TL.initialize(DS.getTypeSpecTypeLoc());
1359 return;
1360 }
1361
1362 TemplateSpecializationTypeLoc OldTL =
John McCalla93c9342009-12-07 02:54:59 +00001363 cast<TemplateSpecializationTypeLoc>(TInfo->getTypeLoc());
John McCall833ca992009-10-29 08:12:44 +00001364 TL.copy(OldTL);
1365 }
John McCallcfb708c2010-01-13 20:03:27 +00001366 void VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
1367 assert(DS.getTypeSpecType() == DeclSpec::TST_typeofExpr);
1368 TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
1369 TL.setParensRange(DS.getTypeofParensRange());
1370 }
1371 void VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
1372 assert(DS.getTypeSpecType() == DeclSpec::TST_typeofType);
1373 TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
1374 TL.setParensRange(DS.getTypeofParensRange());
1375 assert(DS.getTypeRep());
1376 TypeSourceInfo *TInfo = 0;
1377 Sema::GetTypeFromParser(DS.getTypeRep(), &TInfo);
1378 TL.setUnderlyingTInfo(TInfo);
1379 }
Douglas Gregorddf889a2010-01-18 18:04:31 +00001380 void VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
1381 // By default, use the source location of the type specifier.
1382 TL.setBuiltinLoc(DS.getTypeSpecTypeLoc());
1383 if (TL.needsExtraLocalData()) {
1384 // Set info for the written builtin specifiers.
1385 TL.getWrittenBuiltinSpecs() = DS.getWrittenBuiltinSpecs();
1386 // Try to have a meaningful source location.
1387 if (TL.getWrittenSignSpec() != TSS_unspecified)
1388 // Sign spec loc overrides the others (e.g., 'unsigned long').
1389 TL.setBuiltinLoc(DS.getTypeSpecSignLoc());
1390 else if (TL.getWrittenWidthSpec() != TSW_unspecified)
1391 // Width spec loc overrides type spec loc (e.g., 'short int').
1392 TL.setBuiltinLoc(DS.getTypeSpecWidthLoc());
1393 }
1394 }
John McCall51bd8032009-10-18 01:05:36 +00001395 void VisitTypeLoc(TypeLoc TL) {
1396 // FIXME: add other typespec types and change this to an assert.
1397 TL.initialize(DS.getTypeSpecTypeLoc());
1398 }
1399 };
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001400
John McCall51bd8032009-10-18 01:05:36 +00001401 class DeclaratorLocFiller : public TypeLocVisitor<DeclaratorLocFiller> {
1402 const DeclaratorChunk &Chunk;
1403
1404 public:
1405 DeclaratorLocFiller(const DeclaratorChunk &Chunk) : Chunk(Chunk) {}
1406
1407 void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001408 llvm_unreachable("qualified type locs not expected here!");
John McCall51bd8032009-10-18 01:05:36 +00001409 }
1410
1411 void VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
1412 assert(Chunk.Kind == DeclaratorChunk::BlockPointer);
1413 TL.setCaretLoc(Chunk.Loc);
1414 }
1415 void VisitPointerTypeLoc(PointerTypeLoc TL) {
1416 assert(Chunk.Kind == DeclaratorChunk::Pointer);
1417 TL.setStarLoc(Chunk.Loc);
1418 }
1419 void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
1420 assert(Chunk.Kind == DeclaratorChunk::Pointer);
1421 TL.setStarLoc(Chunk.Loc);
John McCall54e14c42009-10-22 22:37:11 +00001422 TL.setHasBaseTypeAsWritten(true);
1423 TL.setHasProtocolsAsWritten(false);
1424 TL.setLAngleLoc(SourceLocation());
1425 TL.setRAngleLoc(SourceLocation());
John McCall51bd8032009-10-18 01:05:36 +00001426 }
1427 void VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
1428 assert(Chunk.Kind == DeclaratorChunk::MemberPointer);
1429 TL.setStarLoc(Chunk.Loc);
1430 // FIXME: nested name specifier
1431 }
1432 void VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
1433 assert(Chunk.Kind == DeclaratorChunk::Reference);
John McCall54e14c42009-10-22 22:37:11 +00001434 // 'Amp' is misleading: this might have been originally
1435 /// spelled with AmpAmp.
John McCall51bd8032009-10-18 01:05:36 +00001436 TL.setAmpLoc(Chunk.Loc);
1437 }
1438 void VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
1439 assert(Chunk.Kind == DeclaratorChunk::Reference);
1440 assert(!Chunk.Ref.LValueRef);
1441 TL.setAmpAmpLoc(Chunk.Loc);
1442 }
1443 void VisitArrayTypeLoc(ArrayTypeLoc TL) {
1444 assert(Chunk.Kind == DeclaratorChunk::Array);
1445 TL.setLBracketLoc(Chunk.Loc);
1446 TL.setRBracketLoc(Chunk.EndLoc);
1447 TL.setSizeExpr(static_cast<Expr*>(Chunk.Arr.NumElts));
1448 }
1449 void VisitFunctionTypeLoc(FunctionTypeLoc TL) {
1450 assert(Chunk.Kind == DeclaratorChunk::Function);
1451 TL.setLParenLoc(Chunk.Loc);
1452 TL.setRParenLoc(Chunk.EndLoc);
1453
1454 const DeclaratorChunk::FunctionTypeInfo &FTI = Chunk.Fun;
John McCall54e14c42009-10-22 22:37:11 +00001455 for (unsigned i = 0, e = TL.getNumArgs(), tpi = 0; i != e; ++i) {
John McCall51bd8032009-10-18 01:05:36 +00001456 ParmVarDecl *Param = FTI.ArgInfo[i].Param.getAs<ParmVarDecl>();
John McCall54e14c42009-10-22 22:37:11 +00001457 TL.setArg(tpi++, Param);
John McCall51bd8032009-10-18 01:05:36 +00001458 }
1459 // FIXME: exception specs
1460 }
1461
1462 void VisitTypeLoc(TypeLoc TL) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001463 llvm_unreachable("unsupported TypeLoc kind in declarator!");
John McCall51bd8032009-10-18 01:05:36 +00001464 }
1465 };
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001466}
1467
John McCalla93c9342009-12-07 02:54:59 +00001468/// \brief Create and instantiate a TypeSourceInfo with type source information.
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001469///
1470/// \param T QualType referring to the type as written in source code.
John McCalla93c9342009-12-07 02:54:59 +00001471TypeSourceInfo *
1472Sema::GetTypeSourceInfoForDeclarator(Declarator &D, QualType T) {
1473 TypeSourceInfo *TInfo = Context.CreateTypeSourceInfo(T);
1474 UnqualTypeLoc CurrTL = TInfo->getTypeLoc().getUnqualifiedLoc();
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001475
Sebastian Redl8ce35b02009-10-25 21:45:37 +00001476 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
John McCall51bd8032009-10-18 01:05:36 +00001477 DeclaratorLocFiller(D.getTypeObject(i)).Visit(CurrTL);
1478 CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001479 }
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001480
John McCall51bd8032009-10-18 01:05:36 +00001481 TypeSpecLocFiller(D.getDeclSpec()).Visit(CurrTL);
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001482
John McCalla93c9342009-12-07 02:54:59 +00001483 return TInfo;
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001484}
1485
John McCalla93c9342009-12-07 02:54:59 +00001486/// \brief Create a LocInfoType to hold the given QualType and TypeSourceInfo.
1487QualType Sema::CreateLocInfoType(QualType T, TypeSourceInfo *TInfo) {
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001488 // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser
1489 // and Sema during declaration parsing. Try deallocating/caching them when
1490 // it's appropriate, instead of allocating them and keeping them around.
1491 LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType), 8);
John McCalla93c9342009-12-07 02:54:59 +00001492 new (LocT) LocInfoType(T, TInfo);
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001493 assert(LocT->getTypeClass() != T->getTypeClass() &&
1494 "LocInfoType's TypeClass conflicts with an existing Type class");
1495 return QualType(LocT, 0);
1496}
1497
1498void LocInfoType::getAsStringInternal(std::string &Str,
1499 const PrintingPolicy &Policy) const {
Argyrios Kyrtzidis35d44e52009-08-19 01:46:06 +00001500 assert(false && "LocInfoType leaked into the type system; an opaque TypeTy*"
1501 " was used directly instead of getting the QualType through"
1502 " GetTypeFromParser");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001503}
1504
Sebastian Redl9e5e4aa2009-01-26 19:54:48 +00001505/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that
1506/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
1507/// they point to and return true. If T1 and T2 aren't pointer types
1508/// or pointer-to-member types, or if they are not similar at this
1509/// level, returns false and leaves T1 and T2 unchanged. Top-level
1510/// qualifiers on T1 and T2 are ignored. This function will typically
1511/// be called in a loop that successively "unwraps" pointer and
1512/// pointer-to-member types to compare them at each level.
Chris Lattnerecb81f22009-02-16 21:43:00 +00001513bool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001514 const PointerType *T1PtrType = T1->getAs<PointerType>(),
1515 *T2PtrType = T2->getAs<PointerType>();
Douglas Gregor57373262008-10-22 14:17:15 +00001516 if (T1PtrType && T2PtrType) {
1517 T1 = T1PtrType->getPointeeType();
1518 T2 = T2PtrType->getPointeeType();
1519 return true;
1520 }
1521
Ted Kremenek6217b802009-07-29 21:53:49 +00001522 const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(),
1523 *T2MPType = T2->getAs<MemberPointerType>();
Sebastian Redl21593ac2009-01-28 18:33:18 +00001524 if (T1MPType && T2MPType &&
1525 Context.getCanonicalType(T1MPType->getClass()) ==
1526 Context.getCanonicalType(T2MPType->getClass())) {
Sebastian Redl4433aaf2009-01-25 19:43:20 +00001527 T1 = T1MPType->getPointeeType();
1528 T2 = T2MPType->getPointeeType();
1529 return true;
1530 }
Douglas Gregor57373262008-10-22 14:17:15 +00001531 return false;
1532}
1533
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001534Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001535 // C99 6.7.6: Type names have no identifier. This is already validated by
1536 // the parser.
1537 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00001538
John McCalla93c9342009-12-07 02:54:59 +00001539 TypeSourceInfo *TInfo = 0;
Douglas Gregor402abb52009-05-28 23:31:59 +00001540 TagDecl *OwnedTag = 0;
John McCalla93c9342009-12-07 02:54:59 +00001541 QualType T = GetTypeForDeclarator(D, S, &TInfo, &OwnedTag);
Chris Lattner5153ee62009-04-25 08:47:54 +00001542 if (D.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +00001543 return true;
Steve Naroff5912a352007-08-28 20:14:24 +00001544
Douglas Gregor402abb52009-05-28 23:31:59 +00001545 if (getLangOptions().CPlusPlus) {
1546 // Check that there are no default arguments (C++ only).
Douglas Gregor6d6eb572008-05-07 04:49:29 +00001547 CheckExtraCXXDefaultArguments(D);
1548
Douglas Gregor402abb52009-05-28 23:31:59 +00001549 // C++0x [dcl.type]p3:
1550 // A type-specifier-seq shall not define a class or enumeration
1551 // unless it appears in the type-id of an alias-declaration
1552 // (7.1.3).
1553 if (OwnedTag && OwnedTag->isDefinition())
1554 Diag(OwnedTag->getLocation(), diag::err_type_defined_in_type_specifier)
1555 << Context.getTypeDeclType(OwnedTag);
1556 }
1557
John McCalla93c9342009-12-07 02:54:59 +00001558 if (TInfo)
1559 T = CreateLocInfoType(T, TInfo);
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001560
Reid Spencer5f016e22007-07-11 17:01:13 +00001561 return T.getAsOpaquePtr();
1562}
1563
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001564
1565
1566//===----------------------------------------------------------------------===//
1567// Type Attribute Processing
1568//===----------------------------------------------------------------------===//
1569
1570/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
1571/// specified type. The attribute contains 1 argument, the id of the address
1572/// space for the type.
Mike Stump1eb44332009-09-09 15:08:12 +00001573static void HandleAddressSpaceTypeAttribute(QualType &Type,
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001574 const AttributeList &Attr, Sema &S){
John McCall0953e762009-09-24 19:53:00 +00001575
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001576 // If this type is already address space qualified, reject it.
1577 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
1578 // for two or more different address spaces."
1579 if (Type.getAddressSpace()) {
1580 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
1581 return;
1582 }
Mike Stump1eb44332009-09-09 15:08:12 +00001583
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001584 // Check the attribute arguments.
1585 if (Attr.getNumArgs() != 1) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +00001586 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001587 return;
1588 }
1589 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
1590 llvm::APSInt addrSpace(32);
1591 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001592 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
1593 << ASArgExpr->getSourceRange();
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001594 return;
1595 }
1596
John McCallefadb772009-07-28 06:52:18 +00001597 // Bounds checking.
1598 if (addrSpace.isSigned()) {
1599 if (addrSpace.isNegative()) {
1600 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative)
1601 << ASArgExpr->getSourceRange();
1602 return;
1603 }
1604 addrSpace.setIsSigned(false);
1605 }
1606 llvm::APSInt max(addrSpace.getBitWidth());
John McCall0953e762009-09-24 19:53:00 +00001607 max = Qualifiers::MaxAddressSpace;
John McCallefadb772009-07-28 06:52:18 +00001608 if (addrSpace > max) {
1609 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high)
John McCall0953e762009-09-24 19:53:00 +00001610 << Qualifiers::MaxAddressSpace << ASArgExpr->getSourceRange();
John McCallefadb772009-07-28 06:52:18 +00001611 return;
1612 }
1613
Mike Stump1eb44332009-09-09 15:08:12 +00001614 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001615 Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001616}
1617
John McCallf82b4e82010-02-04 05:44:44 +00001618/// HandleCDeclTypeAttribute - Process the cdecl attribute on the
1619/// specified type. The attribute contains 0 arguments.
1620static void HandleCDeclTypeAttribute(QualType &Type,
1621 const AttributeList &Attr, Sema &S) {
1622 if (Attr.getNumArgs() != 0)
1623 return;
1624
1625 // We only apply this to a pointer to function.
1626 if (!Type->isFunctionPointerType()
1627 && !Type->isFunctionType())
1628 return;
1629
1630 Type = S.Context.getCallConvType(Type, CC_C);
1631}
1632
1633/// HandleFastCallTypeAttribute - Process the fastcall attribute on the
1634/// specified type. The attribute contains 0 arguments.
1635static void HandleFastCallTypeAttribute(QualType &Type,
1636 const AttributeList &Attr, Sema &S) {
1637 if (Attr.getNumArgs() != 0)
1638 return;
1639
1640 // We only apply this to a pointer to function.
1641 if (!Type->isFunctionPointerType()
1642 && !Type->isFunctionType())
1643 return;
1644
1645 Type = S.Context.getCallConvType(Type, CC_X86FastCall);
1646}
1647
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001648/// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the
1649/// specified type. The attribute contains 1 argument, weak or strong.
Mike Stump1eb44332009-09-09 15:08:12 +00001650static void HandleObjCGCTypeAttribute(QualType &Type,
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001651 const AttributeList &Attr, Sema &S) {
John McCall0953e762009-09-24 19:53:00 +00001652 if (Type.getObjCGCAttr() != Qualifiers::GCNone) {
Fariborz Jahanian5934e752009-02-18 18:52:41 +00001653 S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001654 return;
1655 }
Mike Stump1eb44332009-09-09 15:08:12 +00001656
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001657 // Check the attribute arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001658 if (!Attr.getParameterName()) {
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001659 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1660 << "objc_gc" << 1;
1661 return;
1662 }
John McCall0953e762009-09-24 19:53:00 +00001663 Qualifiers::GC GCAttr;
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001664 if (Attr.getNumArgs() != 0) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001665 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1666 return;
1667 }
Mike Stump1eb44332009-09-09 15:08:12 +00001668 if (Attr.getParameterName()->isStr("weak"))
John McCall0953e762009-09-24 19:53:00 +00001669 GCAttr = Qualifiers::Weak;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001670 else if (Attr.getParameterName()->isStr("strong"))
John McCall0953e762009-09-24 19:53:00 +00001671 GCAttr = Qualifiers::Strong;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001672 else {
1673 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1674 << "objc_gc" << Attr.getParameterName();
1675 return;
1676 }
Mike Stump1eb44332009-09-09 15:08:12 +00001677
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001678 Type = S.Context.getObjCGCQualType(Type, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001679}
1680
Mike Stump24556362009-07-25 21:26:53 +00001681/// HandleNoReturnTypeAttribute - Process the noreturn attribute on the
1682/// specified type. The attribute contains 0 arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001683static void HandleNoReturnTypeAttribute(QualType &Type,
Mike Stump24556362009-07-25 21:26:53 +00001684 const AttributeList &Attr, Sema &S) {
1685 if (Attr.getNumArgs() != 0)
1686 return;
1687
1688 // We only apply this to a pointer to function or a pointer to block.
1689 if (!Type->isFunctionPointerType()
1690 && !Type->isBlockPointerType()
1691 && !Type->isFunctionType())
1692 return;
1693
1694 Type = S.Context.getNoReturnType(Type);
1695}
1696
John McCallf82b4e82010-02-04 05:44:44 +00001697/// HandleStdCallTypeAttribute - Process the stdcall attribute on the
1698/// specified type. The attribute contains 0 arguments.
1699static void HandleStdCallTypeAttribute(QualType &Type,
1700 const AttributeList &Attr, Sema &S) {
1701 if (Attr.getNumArgs() != 0)
1702 return;
1703
1704 // We only apply this to a pointer to function.
1705 if (!Type->isFunctionPointerType()
1706 && !Type->isFunctionType())
1707 return;
1708
1709 Type = S.Context.getCallConvType(Type, CC_X86StdCall);
1710}
1711
John Thompson6e132aa2009-12-04 21:51:28 +00001712/// HandleVectorSizeAttribute - this attribute is only applicable to integral
1713/// and float scalars, although arrays, pointers, and function return values are
1714/// allowed in conjunction with this construct. Aggregates with this attribute
1715/// are invalid, even if they are of the same size as a corresponding scalar.
1716/// The raw attribute should contain precisely 1 argument, the vector size for
1717/// the variable, measured in bytes. If curType and rawAttr are well formed,
1718/// this routine will return a new vector type.
1719static void HandleVectorSizeAttr(QualType& CurType, const AttributeList &Attr, Sema &S) {
1720 // Check the attribute arugments.
1721 if (Attr.getNumArgs() != 1) {
1722 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1723 return;
1724 }
1725 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
1726 llvm::APSInt vecSize(32);
1727 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
1728 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1729 << "vector_size" << sizeExpr->getSourceRange();
1730 return;
1731 }
1732 // the base type must be integer or float, and can't already be a vector.
1733 if (CurType->isVectorType() ||
1734 (!CurType->isIntegerType() && !CurType->isRealFloatingType())) {
1735 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
1736 return;
1737 }
1738 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
1739 // vecSize is specified in bytes - convert to bits.
1740 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
1741
1742 // the vector size needs to be an integral multiple of the type size.
1743 if (vectorSize % typeSize) {
1744 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
1745 << sizeExpr->getSourceRange();
1746 return;
1747 }
1748 if (vectorSize == 0) {
1749 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
1750 << sizeExpr->getSourceRange();
1751 return;
1752 }
1753
1754 // Success! Instantiate the vector type, the number of elements is > 0, and
1755 // not required to be a power of 2, unlike GCC.
1756 CurType = S.Context.getVectorType(CurType, vectorSize/typeSize);
1757}
1758
John McCallf82b4e82010-02-04 05:44:44 +00001759void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL,
1760 bool HandleCallConvAttributes,
1761 bool HandleOnlyCallConv) {
1762 if(HandleOnlyCallConv)
1763 assert(HandleCallConvAttributes && "Can't not handle call-conv attributes"
1764 " while only handling them!");
Chris Lattner232e8822008-02-21 01:08:11 +00001765 // Scan through and apply attributes to this type where it makes sense. Some
1766 // attributes (such as __address_space__, __vector_size__, etc) apply to the
1767 // type, but others can be present in the type specifiers even though they
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00001768 // apply to the decl. Here we apply type attributes and ignore the rest.
1769 for (; AL; AL = AL->getNext()) {
Chris Lattner232e8822008-02-21 01:08:11 +00001770 // If this is an attribute we can handle, do so now, otherwise, add it to
1771 // the LeftOverAttrs list for rechaining.
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00001772 switch (AL->getKind()) {
Chris Lattner232e8822008-02-21 01:08:11 +00001773 default: break;
1774 case AttributeList::AT_address_space:
John McCallf82b4e82010-02-04 05:44:44 +00001775 if (!HandleOnlyCallConv)
1776 HandleAddressSpaceTypeAttribute(Result, *AL, *this);
1777 break;
1778 case AttributeList::AT_cdecl:
1779 if (HandleCallConvAttributes)
1780 HandleCDeclTypeAttribute(Result, *AL, *this);
1781 break;
1782 case AttributeList::AT_fastcall:
1783 if (HandleCallConvAttributes)
1784 HandleFastCallTypeAttribute(Result, *AL, *this);
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001785 break;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001786 case AttributeList::AT_objc_gc:
John McCallf82b4e82010-02-04 05:44:44 +00001787 if (!HandleOnlyCallConv)
1788 HandleObjCGCTypeAttribute(Result, *AL, *this);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001789 break;
Mike Stump24556362009-07-25 21:26:53 +00001790 case AttributeList::AT_noreturn:
John McCallf82b4e82010-02-04 05:44:44 +00001791 if (!HandleOnlyCallConv)
1792 HandleNoReturnTypeAttribute(Result, *AL, *this);
1793 break;
1794 case AttributeList::AT_stdcall:
1795 if (HandleCallConvAttributes)
1796 HandleStdCallTypeAttribute(Result, *AL, *this);
Mike Stump24556362009-07-25 21:26:53 +00001797 break;
John Thompson6e132aa2009-12-04 21:51:28 +00001798 case AttributeList::AT_vector_size:
John McCallf82b4e82010-02-04 05:44:44 +00001799 if (!HandleOnlyCallConv)
1800 HandleVectorSizeAttr(Result, *AL, *this);
John Thompson6e132aa2009-12-04 21:51:28 +00001801 break;
Chris Lattner232e8822008-02-21 01:08:11 +00001802 }
Chris Lattner232e8822008-02-21 01:08:11 +00001803 }
Chris Lattner232e8822008-02-21 01:08:11 +00001804}
1805
Mike Stump1eb44332009-09-09 15:08:12 +00001806/// @brief Ensure that the type T is a complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001807///
1808/// This routine checks whether the type @p T is complete in any
1809/// context where a complete type is required. If @p T is a complete
Douglas Gregor86447ec2009-03-09 16:13:40 +00001810/// type, returns false. If @p T is a class template specialization,
1811/// this routine then attempts to perform class template
1812/// instantiation. If instantiation fails, or if @p T is incomplete
1813/// and cannot be completed, issues the diagnostic @p diag (giving it
1814/// the type @p T) and returns true.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001815///
1816/// @param Loc The location in the source that the incomplete type
1817/// diagnostic should refer to.
1818///
1819/// @param T The type that this routine is examining for completeness.
1820///
Mike Stump1eb44332009-09-09 15:08:12 +00001821/// @param PD The partial diagnostic that will be printed out if T is not a
Anders Carlssonb7906612009-08-26 23:45:07 +00001822/// complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001823///
1824/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
1825/// @c false otherwise.
Anders Carlsson91a0cc92009-08-26 22:33:56 +00001826bool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
Anders Carlsson8c8d9192009-10-09 23:51:55 +00001827 const PartialDiagnostic &PD,
1828 std::pair<SourceLocation,
1829 PartialDiagnostic> Note) {
Anders Carlsson91a0cc92009-08-26 22:33:56 +00001830 unsigned diag = PD.getDiagID();
Mike Stump1eb44332009-09-09 15:08:12 +00001831
Douglas Gregor573d9c32009-10-21 23:19:44 +00001832 // FIXME: Add this assertion to make sure we always get instantiation points.
1833 // assert(!Loc.isInvalid() && "Invalid location in RequireCompleteType");
Douglas Gregor690dc7f2009-05-21 23:48:18 +00001834 // FIXME: Add this assertion to help us flush out problems with
1835 // checking for dependent types and type-dependent expressions.
1836 //
Mike Stump1eb44332009-09-09 15:08:12 +00001837 // assert(!T->isDependentType() &&
Douglas Gregor690dc7f2009-05-21 23:48:18 +00001838 // "Can't ask whether a dependent type is complete");
1839
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001840 // If we have a complete type, we're done.
1841 if (!T->isIncompleteType())
1842 return false;
Eli Friedman3c0eb162008-05-27 03:33:27 +00001843
Douglas Gregord475b8d2009-03-25 21:17:03 +00001844 // If we have a class template specialization or a class member of a
Sebastian Redl923d56d2009-11-05 15:52:31 +00001845 // class template specialization, or an array with known size of such,
1846 // try to instantiate it.
1847 QualType MaybeTemplate = T;
Douglas Gregor89c49f02009-11-09 22:08:55 +00001848 if (const ConstantArrayType *Array = Context.getAsConstantArrayType(T))
Sebastian Redl923d56d2009-11-05 15:52:31 +00001849 MaybeTemplate = Array->getElementType();
1850 if (const RecordType *Record = MaybeTemplate->getAs<RecordType>()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001851 if (ClassTemplateSpecializationDecl *ClassTemplateSpec
Douglas Gregord475b8d2009-03-25 21:17:03 +00001852 = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001853 if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared)
1854 return InstantiateClassTemplateSpecialization(Loc, ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001855 TSK_ImplicitInstantiation,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001856 /*Complain=*/diag != 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001857 } else if (CXXRecordDecl *Rec
Douglas Gregord475b8d2009-03-25 21:17:03 +00001858 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
1859 if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001860 MemberSpecializationInfo *MSInfo = Rec->getMemberSpecializationInfo();
1861 assert(MSInfo && "Missing member specialization information?");
Douglas Gregor357bbd02009-08-28 20:50:45 +00001862 // This record was instantiated from a class within a template.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001863 if (MSInfo->getTemplateSpecializationKind()
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001864 != TSK_ExplicitSpecialization)
Douglas Gregorf6b11852009-10-08 15:14:33 +00001865 return InstantiateClass(Loc, Rec, Pattern,
1866 getTemplateInstantiationArgs(Rec),
1867 TSK_ImplicitInstantiation,
1868 /*Complain=*/diag != 0);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001869 }
1870 }
1871 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00001872
Douglas Gregor5842ba92009-08-24 15:23:48 +00001873 if (diag == 0)
1874 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001875
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001876 // We have an incomplete type. Produce a diagnostic.
Anders Carlsson91a0cc92009-08-26 22:33:56 +00001877 Diag(Loc, PD) << T;
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001878
Anders Carlsson8c8d9192009-10-09 23:51:55 +00001879 // If we have a note, produce it.
1880 if (!Note.first.isInvalid())
1881 Diag(Note.first, Note.second);
1882
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001883 // If the type was a forward declaration of a class/struct/union
Mike Stump1eb44332009-09-09 15:08:12 +00001884 // type, produce
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001885 const TagType *Tag = 0;
Ted Kremenek6217b802009-07-29 21:53:49 +00001886 if (const RecordType *Record = T->getAs<RecordType>())
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001887 Tag = Record;
John McCall183700f2009-09-21 23:43:11 +00001888 else if (const EnumType *Enum = T->getAs<EnumType>())
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001889 Tag = Enum;
1890
1891 if (Tag && !Tag->getDecl()->isInvalidDecl())
Mike Stump1eb44332009-09-09 15:08:12 +00001892 Diag(Tag->getDecl()->getLocation(),
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001893 Tag->isBeingDefined() ? diag::note_type_being_defined
1894 : diag::note_forward_declaration)
1895 << QualType(Tag, 0);
1896
1897 return true;
1898}
Douglas Gregore6258932009-03-19 00:39:20 +00001899
1900/// \brief Retrieve a version of the type 'T' that is qualified by the
1901/// nested-name-specifier contained in SS.
1902QualType Sema::getQualifiedNameType(const CXXScopeSpec &SS, QualType T) {
1903 if (!SS.isSet() || SS.isInvalid() || T.isNull())
1904 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00001905
Douglas Gregorab452ba2009-03-26 23:50:42 +00001906 NestedNameSpecifier *NNS
Douglas Gregor35073692009-03-26 23:56:24 +00001907 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Douglas Gregorab452ba2009-03-26 23:50:42 +00001908 return Context.getQualifiedNameType(NNS, T);
Douglas Gregore6258932009-03-19 00:39:20 +00001909}
Anders Carlssonaf017e62009-06-29 22:58:55 +00001910
1911QualType Sema::BuildTypeofExprType(Expr *E) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00001912 if (E->getType() == Context.OverloadTy) {
1913 // C++ [temp.arg.explicit]p3 allows us to resolve a template-id to a
1914 // function template specialization wherever deduction cannot occur.
1915 if (FunctionDecl *Specialization
1916 = ResolveSingleFunctionTemplateSpecialization(E)) {
1917 E = FixOverloadedFunctionReference(E, Specialization);
1918 if (!E)
1919 return QualType();
1920 } else {
1921 Diag(E->getLocStart(),
1922 diag::err_cannot_determine_declared_type_of_overloaded_function)
1923 << false << E->getSourceRange();
1924 return QualType();
1925 }
1926 }
1927
Anders Carlssonaf017e62009-06-29 22:58:55 +00001928 return Context.getTypeOfExprType(E);
1929}
1930
1931QualType Sema::BuildDecltypeType(Expr *E) {
1932 if (E->getType() == Context.OverloadTy) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00001933 // C++ [temp.arg.explicit]p3 allows us to resolve a template-id to a
1934 // function template specialization wherever deduction cannot occur.
1935 if (FunctionDecl *Specialization
1936 = ResolveSingleFunctionTemplateSpecialization(E)) {
1937 E = FixOverloadedFunctionReference(E, Specialization);
1938 if (!E)
1939 return QualType();
1940 } else {
1941 Diag(E->getLocStart(),
1942 diag::err_cannot_determine_declared_type_of_overloaded_function)
1943 << true << E->getSourceRange();
1944 return QualType();
1945 }
Anders Carlssonaf017e62009-06-29 22:58:55 +00001946 }
Douglas Gregor4b52e252009-12-21 23:17:24 +00001947
Anders Carlssonaf017e62009-06-29 22:58:55 +00001948 return Context.getDecltypeType(E);
1949}