blob: afce5e33ba6b51522369388e3073912cbbc7aa03 [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 Gregor72564e72009-02-26 23:50:07 +0000311 Result = Context.getTypeOfExprType(E);
Chris Lattner958858e2008-02-20 21:40:32 +0000312 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000313 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000314 case DeclSpec::TST_decltype: {
315 Expr *E = static_cast<Expr *>(DS.getTypeRep());
316 assert(E && "Didn't get an expression for decltype?");
317 // TypeQuals handled by caller.
Chris Lattner1564e392009-10-25 18:07:27 +0000318 Result = TheSema.BuildDecltypeType(E);
Anders Carlssonaf017e62009-06-29 22:58:55 +0000319 if (Result.isNull()) {
320 Result = Context.IntTy;
Chris Lattner5db2bb12009-10-25 18:21:37 +0000321 TheDeclarator.setInvalidType(true);
Anders Carlssonaf017e62009-06-29 22:58:55 +0000322 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000323 break;
324 }
Anders Carlssone89d1592009-06-26 18:41:36 +0000325 case DeclSpec::TST_auto: {
326 // TypeQuals handled by caller.
327 Result = Context.UndeducedAutoTy;
328 break;
329 }
Mike Stump1eb44332009-09-09 15:08:12 +0000330
Douglas Gregor809070a2009-02-18 17:45:20 +0000331 case DeclSpec::TST_error:
Chris Lattner5153ee62009-04-25 08:47:54 +0000332 Result = Context.IntTy;
Chris Lattner5db2bb12009-10-25 18:21:37 +0000333 TheDeclarator.setInvalidType(true);
Chris Lattner5153ee62009-04-25 08:47:54 +0000334 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000335 }
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Chris Lattner958858e2008-02-20 21:40:32 +0000337 // Handle complex types.
Douglas Gregorf244cd72009-02-14 21:06:05 +0000338 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
Chris Lattner1564e392009-10-25 18:07:27 +0000339 if (TheSema.getLangOptions().Freestanding)
340 TheSema.Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
Chris Lattnerfab5b452008-02-20 23:53:49 +0000341 Result = Context.getComplexType(Result);
Douglas Gregorf244cd72009-02-14 21:06:05 +0000342 }
Mike Stump1eb44332009-09-09 15:08:12 +0000343
Chris Lattner958858e2008-02-20 21:40:32 +0000344 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
345 "FIXME: imaginary types not supported yet!");
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Chris Lattner38d8b982008-02-20 22:04:11 +0000347 // See if there are any attributes on the declspec that apply to the type (as
348 // opposed to the decl).
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000349 if (const AttributeList *AL = DS.getAttributes())
Chris Lattner1564e392009-10-25 18:07:27 +0000350 TheSema.ProcessTypeAttributeList(Result, AL);
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Chris Lattner96b77fc2008-04-02 06:50:17 +0000352 // Apply const/volatile/restrict qualifiers to T.
353 if (unsigned TypeQuals = DS.getTypeQualifiers()) {
354
355 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
356 // or incomplete types shall not be restrict-qualified." C++ also allows
357 // restrict-qualified references.
John McCall0953e762009-09-24 19:53:00 +0000358 if (TypeQuals & DeclSpec::TQ_restrict) {
Daniel Dunbarbb710012009-02-26 19:13:44 +0000359 if (Result->isPointerType() || Result->isReferenceType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000360 QualType EltTy = Result->isPointerType() ?
Ted Kremenek6217b802009-07-29 21:53:49 +0000361 Result->getAs<PointerType>()->getPointeeType() :
362 Result->getAs<ReferenceType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000363
Douglas Gregorbad0e652009-03-24 20:32:41 +0000364 // If we have a pointer or reference, the pointee must have an object
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000365 // incomplete type.
366 if (!EltTy->isIncompleteOrObjectType()) {
Chris Lattner1564e392009-10-25 18:07:27 +0000367 TheSema.Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000368 diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattnerd1625842008-11-24 06:25:27 +0000369 << EltTy << DS.getSourceRange();
John McCall0953e762009-09-24 19:53:00 +0000370 TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000371 }
372 } else {
Chris Lattner1564e392009-10-25 18:07:27 +0000373 TheSema.Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000374 diag::err_typecheck_invalid_restrict_not_pointer)
Chris Lattnerd1625842008-11-24 06:25:27 +0000375 << Result << DS.getSourceRange();
John McCall0953e762009-09-24 19:53:00 +0000376 TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
Chris Lattner96b77fc2008-04-02 06:50:17 +0000377 }
Chris Lattner96b77fc2008-04-02 06:50:17 +0000378 }
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Chris Lattner96b77fc2008-04-02 06:50:17 +0000380 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
381 // of a function type includes any type qualifiers, the behavior is
382 // undefined."
383 if (Result->isFunctionType() && TypeQuals) {
384 // Get some location to point at, either the C or V location.
385 SourceLocation Loc;
John McCall0953e762009-09-24 19:53:00 +0000386 if (TypeQuals & DeclSpec::TQ_const)
Chris Lattner96b77fc2008-04-02 06:50:17 +0000387 Loc = DS.getConstSpecLoc();
John McCall0953e762009-09-24 19:53:00 +0000388 else if (TypeQuals & DeclSpec::TQ_volatile)
Chris Lattner96b77fc2008-04-02 06:50:17 +0000389 Loc = DS.getVolatileSpecLoc();
John McCall0953e762009-09-24 19:53:00 +0000390 else {
391 assert((TypeQuals & DeclSpec::TQ_restrict) &&
392 "Has CVR quals but not C, V, or R?");
393 Loc = DS.getRestrictSpecLoc();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000394 }
Chris Lattner1564e392009-10-25 18:07:27 +0000395 TheSema.Diag(Loc, diag::warn_typecheck_function_qualifiers)
Chris Lattnerd1625842008-11-24 06:25:27 +0000396 << Result << DS.getSourceRange();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000397 }
Mike Stump1eb44332009-09-09 15:08:12 +0000398
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000399 // C++ [dcl.ref]p1:
400 // Cv-qualified references are ill-formed except when the
401 // cv-qualifiers are introduced through the use of a typedef
402 // (7.1.3) or of a template type argument (14.3), in which
403 // case the cv-qualifiers are ignored.
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000404 // FIXME: Shouldn't we be checking SCS_typedef here?
405 if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000406 TypeQuals && Result->isReferenceType()) {
John McCall0953e762009-09-24 19:53:00 +0000407 TypeQuals &= ~DeclSpec::TQ_const;
408 TypeQuals &= ~DeclSpec::TQ_volatile;
Mike Stump1eb44332009-09-09 15:08:12 +0000409 }
410
John McCall0953e762009-09-24 19:53:00 +0000411 Qualifiers Quals = Qualifiers::fromCVRMask(TypeQuals);
412 Result = Context.getQualifiedType(Result, Quals);
Chris Lattner96b77fc2008-04-02 06:50:17 +0000413 }
John McCall0953e762009-09-24 19:53:00 +0000414
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000415 return Result;
416}
417
Douglas Gregorcd281c32009-02-28 00:25:32 +0000418static std::string getPrintableNameForEntity(DeclarationName Entity) {
419 if (Entity)
420 return Entity.getAsString();
Mike Stump1eb44332009-09-09 15:08:12 +0000421
Douglas Gregorcd281c32009-02-28 00:25:32 +0000422 return "type name";
423}
424
425/// \brief Build a pointer type.
426///
427/// \param T The type to which we'll be building a pointer.
428///
429/// \param Quals The cvr-qualifiers to be applied to the pointer type.
430///
431/// \param Loc The location of the entity whose type involves this
432/// pointer type or, if there is no such entity, the location of the
433/// type that will have pointer type.
434///
435/// \param Entity The name of the entity that involves the pointer
436/// type, if known.
437///
438/// \returns A suitable pointer type, if there are no
439/// errors. Otherwise, returns a NULL type.
Mike Stump1eb44332009-09-09 15:08:12 +0000440QualType Sema::BuildPointerType(QualType T, unsigned Quals,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000441 SourceLocation Loc, DeclarationName Entity) {
442 if (T->isReferenceType()) {
443 // C++ 8.3.2p4: There shall be no ... pointers to references ...
444 Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
John McCallac406052009-10-30 00:37:20 +0000445 << getPrintableNameForEntity(Entity) << T;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000446 return QualType();
447 }
448
John McCall0953e762009-09-24 19:53:00 +0000449 Qualifiers Qs = Qualifiers::fromCVRMask(Quals);
450
Douglas Gregorcd281c32009-02-28 00:25:32 +0000451 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
452 // object or incomplete types shall not be restrict-qualified."
John McCall0953e762009-09-24 19:53:00 +0000453 if (Qs.hasRestrict() && !T->isIncompleteOrObjectType()) {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000454 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
455 << T;
John McCall0953e762009-09-24 19:53:00 +0000456 Qs.removeRestrict();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000457 }
458
459 // Build the pointer type.
John McCall0953e762009-09-24 19:53:00 +0000460 return Context.getQualifiedType(Context.getPointerType(T), Qs);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000461}
462
463/// \brief Build a reference type.
464///
465/// \param T The type to which we'll be building a reference.
466///
John McCall0953e762009-09-24 19:53:00 +0000467/// \param CVR The cvr-qualifiers to be applied to the reference type.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000468///
469/// \param Loc The location of the entity whose type involves this
470/// reference type or, if there is no such entity, the location of the
471/// type that will have reference type.
472///
473/// \param Entity The name of the entity that involves the reference
474/// type, if known.
475///
476/// \returns A suitable reference type, if there are no
477/// errors. Otherwise, returns a NULL type.
John McCall54e14c42009-10-22 22:37:11 +0000478QualType Sema::BuildReferenceType(QualType T, bool SpelledAsLValue,
479 unsigned CVR, SourceLocation Loc,
480 DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000481 Qualifiers Quals = Qualifiers::fromCVRMask(CVR);
John McCall54e14c42009-10-22 22:37:11 +0000482
483 bool LValueRef = SpelledAsLValue || T->getAs<LValueReferenceType>();
484
485 // C++0x [dcl.typedef]p9: If a typedef TD names a type that is a
486 // reference to a type T, and attempt to create the type "lvalue
487 // reference to cv TD" creates the type "lvalue reference to T".
488 // We use the qualifiers (restrict or none) of the original reference,
489 // not the new ones. This is consistent with GCC.
490
491 // C++ [dcl.ref]p4: There shall be no references to references.
492 //
493 // According to C++ DR 106, references to references are only
494 // diagnosed when they are written directly (e.g., "int & &"),
495 // but not when they happen via a typedef:
496 //
497 // typedef int& intref;
498 // typedef intref& intref2;
499 //
500 // Parser::ParseDeclaratorInternal diagnoses the case where
501 // references are written directly; here, we handle the
502 // collapsing of references-to-references as described in C++
503 // DR 106 and amended by C++ DR 540.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000504
505 // C++ [dcl.ref]p1:
Eli Friedman33a31382009-08-05 19:21:58 +0000506 // A declarator that specifies the type "reference to cv void"
Douglas Gregorcd281c32009-02-28 00:25:32 +0000507 // is ill-formed.
508 if (T->isVoidType()) {
509 Diag(Loc, diag::err_reference_to_void);
510 return QualType();
511 }
512
513 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
514 // object or incomplete types shall not be restrict-qualified."
John McCall0953e762009-09-24 19:53:00 +0000515 if (Quals.hasRestrict() && !T->isIncompleteOrObjectType()) {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000516 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
517 << T;
John McCall0953e762009-09-24 19:53:00 +0000518 Quals.removeRestrict();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000519 }
520
521 // C++ [dcl.ref]p1:
522 // [...] Cv-qualified references are ill-formed except when the
523 // cv-qualifiers are introduced through the use of a typedef
524 // (7.1.3) or of a template type argument (14.3), in which case
525 // the cv-qualifiers are ignored.
526 //
527 // We diagnose extraneous cv-qualifiers for the non-typedef,
528 // non-template type argument case within the parser. Here, we just
529 // ignore any extraneous cv-qualifiers.
John McCall0953e762009-09-24 19:53:00 +0000530 Quals.removeConst();
531 Quals.removeVolatile();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000532
533 // Handle restrict on references.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000534 if (LValueRef)
John McCall54e14c42009-10-22 22:37:11 +0000535 return Context.getQualifiedType(
536 Context.getLValueReferenceType(T, SpelledAsLValue), Quals);
John McCall0953e762009-09-24 19:53:00 +0000537 return Context.getQualifiedType(Context.getRValueReferenceType(T), Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000538}
539
540/// \brief Build an array type.
541///
542/// \param T The type of each element in the array.
543///
544/// \param ASM C99 array size modifier (e.g., '*', 'static').
Mike Stump1eb44332009-09-09 15:08:12 +0000545///
546/// \param ArraySize Expression describing the size of the array.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000547///
548/// \param Quals The cvr-qualifiers to be applied to the array's
549/// element type.
550///
551/// \param Loc The location of the entity whose type involves this
552/// array type or, if there is no such entity, the location of the
553/// type that will have array type.
554///
555/// \param Entity The name of the entity that involves the array
556/// type, if known.
557///
558/// \returns A suitable array type, if there are no errors. Otherwise,
559/// returns a NULL type.
560QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
561 Expr *ArraySize, unsigned Quals,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000562 SourceRange Brackets, DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000563
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000564 SourceLocation Loc = Brackets.getBegin();
Mike Stump1eb44332009-09-09 15:08:12 +0000565 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000566 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
Sebastian Redl923d56d2009-11-05 15:52:31 +0000567 // Not in C++, though. There we only dislike void.
568 if (getLangOptions().CPlusPlus) {
569 if (T->isVoidType()) {
570 Diag(Loc, diag::err_illegal_decl_array_incomplete_type) << T;
571 return QualType();
572 }
573 } else {
574 if (RequireCompleteType(Loc, T,
575 diag::err_illegal_decl_array_incomplete_type))
576 return QualType();
577 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000578
579 if (T->isFunctionType()) {
580 Diag(Loc, diag::err_illegal_decl_array_of_functions)
John McCallac406052009-10-30 00:37:20 +0000581 << getPrintableNameForEntity(Entity) << T;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000582 return QualType();
583 }
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Douglas Gregorcd281c32009-02-28 00:25:32 +0000585 // C++ 8.3.2p4: There shall be no ... arrays of references ...
586 if (T->isReferenceType()) {
587 Diag(Loc, diag::err_illegal_decl_array_of_references)
John McCallac406052009-10-30 00:37:20 +0000588 << getPrintableNameForEntity(Entity) << T;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000589 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000590 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000591
Anders Carlssone7cf07d2009-06-26 19:33:28 +0000592 if (Context.getCanonicalType(T) == Context.UndeducedAutoTy) {
Mike Stump1eb44332009-09-09 15:08:12 +0000593 Diag(Loc, diag::err_illegal_decl_array_of_auto)
Anders Carlssone7cf07d2009-06-26 19:33:28 +0000594 << getPrintableNameForEntity(Entity);
595 return QualType();
596 }
Mike Stump1eb44332009-09-09 15:08:12 +0000597
Ted Kremenek6217b802009-07-29 21:53:49 +0000598 if (const RecordType *EltTy = T->getAs<RecordType>()) {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000599 // If the element type is a struct or union that contains a variadic
600 // array, accept it as a GNU extension: C99 6.7.2.1p2.
601 if (EltTy->getDecl()->hasFlexibleArrayMember())
602 Diag(Loc, diag::ext_flexible_array_in_array) << T;
603 } else if (T->isObjCInterfaceType()) {
Chris Lattnerc7c11b12009-04-27 01:55:56 +0000604 Diag(Loc, diag::err_objc_array_of_interfaces) << T;
605 return QualType();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000606 }
Mike Stump1eb44332009-09-09 15:08:12 +0000607
Douglas Gregorcd281c32009-02-28 00:25:32 +0000608 // C99 6.7.5.2p1: The size expression shall have integer type.
609 if (ArraySize && !ArraySize->isTypeDependent() &&
610 !ArraySize->getType()->isIntegerType()) {
611 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
612 << ArraySize->getType() << ArraySize->getSourceRange();
613 ArraySize->Destroy(Context);
614 return QualType();
615 }
616 llvm::APSInt ConstVal(32);
617 if (!ArraySize) {
Eli Friedmanf91f5c82009-04-26 21:57:51 +0000618 if (ASM == ArrayType::Star)
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000619 T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets);
Eli Friedmanf91f5c82009-04-26 21:57:51 +0000620 else
621 T = Context.getIncompleteArrayType(T, ASM, Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000622 } else if (ArraySize->isValueDependent()) {
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000623 T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000624 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
Sebastian Redl923d56d2009-11-05 15:52:31 +0000625 (!T->isDependentType() && !T->isIncompleteType() &&
626 !T->isConstantSizeType())) {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000627 // Per C99, a variable array is an array with either a non-constant
628 // size or an element type that has a non-constant-size
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000629 T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000630 } else {
631 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
632 // have a value greater than zero.
Sebastian Redl923d56d2009-11-05 15:52:31 +0000633 if (ConstVal.isSigned() && ConstVal.isNegative()) {
634 Diag(ArraySize->getLocStart(),
635 diag::err_typecheck_negative_array_size)
636 << ArraySize->getSourceRange();
637 return QualType();
638 }
639 if (ConstVal == 0) {
640 // GCC accepts zero sized static arrays.
641 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size)
642 << ArraySize->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000643 }
John McCall46a617a2009-10-16 00:14:28 +0000644 T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000645 }
646 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
647 if (!getLangOptions().C99) {
Mike Stump1eb44332009-09-09 15:08:12 +0000648 if (ArraySize && !ArraySize->isTypeDependent() &&
649 !ArraySize->isValueDependent() &&
Douglas Gregorcd281c32009-02-28 00:25:32 +0000650 !ArraySize->isIntegerConstantExpr(Context))
Douglas Gregor043cad22009-09-11 00:18:58 +0000651 Diag(Loc, getLangOptions().CPlusPlus? diag::err_vla_cxx : diag::ext_vla);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000652 else if (ASM != ArrayType::Normal || Quals != 0)
Douglas Gregor043cad22009-09-11 00:18:58 +0000653 Diag(Loc,
654 getLangOptions().CPlusPlus? diag::err_c99_array_usage_cxx
655 : diag::ext_c99_array_usage);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000656 }
657
658 return T;
659}
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000660
661/// \brief Build an ext-vector type.
662///
663/// Run the required checks for the extended vector type.
Mike Stump1eb44332009-09-09 15:08:12 +0000664QualType Sema::BuildExtVectorType(QualType T, ExprArg ArraySize,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000665 SourceLocation AttrLoc) {
666
667 Expr *Arg = (Expr *)ArraySize.get();
668
669 // unlike gcc's vector_size attribute, we do not allow vectors to be defined
670 // in conjunction with complex types (pointers, arrays, functions, etc.).
Mike Stump1eb44332009-09-09 15:08:12 +0000671 if (!T->isDependentType() &&
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000672 !T->isIntegerType() && !T->isRealFloatingType()) {
673 Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T;
674 return QualType();
675 }
676
677 if (!Arg->isTypeDependent() && !Arg->isValueDependent()) {
678 llvm::APSInt vecSize(32);
679 if (!Arg->isIntegerConstantExpr(vecSize, Context)) {
680 Diag(AttrLoc, diag::err_attribute_argument_not_int)
681 << "ext_vector_type" << Arg->getSourceRange();
682 return QualType();
683 }
Mike Stump1eb44332009-09-09 15:08:12 +0000684
685 // unlike gcc's vector_size attribute, the size is specified as the
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000686 // number of elements, not the number of bytes.
Mike Stump1eb44332009-09-09 15:08:12 +0000687 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
688
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000689 if (vectorSize == 0) {
690 Diag(AttrLoc, diag::err_attribute_zero_size)
691 << Arg->getSourceRange();
692 return QualType();
693 }
Mike Stump1eb44332009-09-09 15:08:12 +0000694
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000695 if (!T->isDependentType())
696 return Context.getExtVectorType(T, vectorSize);
Mike Stump1eb44332009-09-09 15:08:12 +0000697 }
698
699 return Context.getDependentSizedExtVectorType(T, ArraySize.takeAs<Expr>(),
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000700 AttrLoc);
701}
Mike Stump1eb44332009-09-09 15:08:12 +0000702
Douglas Gregor724651c2009-02-28 01:04:19 +0000703/// \brief Build a function type.
704///
705/// This routine checks the function type according to C++ rules and
706/// under the assumption that the result type and parameter types have
707/// just been instantiated from a template. It therefore duplicates
Douglas Gregor2943aed2009-03-03 04:44:36 +0000708/// some of the behavior of GetTypeForDeclarator, but in a much
Douglas Gregor724651c2009-02-28 01:04:19 +0000709/// simpler form that is only suitable for this narrow use case.
710///
711/// \param T The return type of the function.
712///
713/// \param ParamTypes The parameter types of the function. This array
714/// will be modified to account for adjustments to the types of the
715/// function parameters.
716///
717/// \param NumParamTypes The number of parameter types in ParamTypes.
718///
719/// \param Variadic Whether this is a variadic function type.
720///
721/// \param Quals The cvr-qualifiers to be applied to the function type.
722///
723/// \param Loc The location of the entity whose type involves this
724/// function type or, if there is no such entity, the location of the
725/// type that will have function type.
726///
727/// \param Entity The name of the entity that involves the function
728/// type, if known.
729///
730/// \returns A suitable function type, if there are no
731/// errors. Otherwise, returns a NULL type.
732QualType Sema::BuildFunctionType(QualType T,
Mike Stump1eb44332009-09-09 15:08:12 +0000733 QualType *ParamTypes,
Douglas Gregor724651c2009-02-28 01:04:19 +0000734 unsigned NumParamTypes,
735 bool Variadic, unsigned Quals,
736 SourceLocation Loc, DeclarationName Entity) {
737 if (T->isArrayType() || T->isFunctionType()) {
738 Diag(Loc, diag::err_func_returning_array_function) << T;
739 return QualType();
740 }
Mike Stump1eb44332009-09-09 15:08:12 +0000741
Douglas Gregor724651c2009-02-28 01:04:19 +0000742 bool Invalid = false;
743 for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) {
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000744 QualType ParamType = adjustParameterType(ParamTypes[Idx]);
745 if (ParamType->isVoidType()) {
Douglas Gregor724651c2009-02-28 01:04:19 +0000746 Diag(Loc, diag::err_param_with_void_type);
747 Invalid = true;
748 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000749
John McCall54e14c42009-10-22 22:37:11 +0000750 ParamTypes[Idx] = ParamType;
Douglas Gregor724651c2009-02-28 01:04:19 +0000751 }
752
753 if (Invalid)
754 return QualType();
755
Mike Stump1eb44332009-09-09 15:08:12 +0000756 return Context.getFunctionType(T, ParamTypes, NumParamTypes, Variadic,
Douglas Gregor724651c2009-02-28 01:04:19 +0000757 Quals);
758}
Mike Stump1eb44332009-09-09 15:08:12 +0000759
Douglas Gregor949bf692009-06-09 22:17:39 +0000760/// \brief Build a member pointer type \c T Class::*.
761///
762/// \param T the type to which the member pointer refers.
763/// \param Class the class type into which the member pointer points.
John McCall0953e762009-09-24 19:53:00 +0000764/// \param CVR Qualifiers applied to the member pointer type
Douglas Gregor949bf692009-06-09 22:17:39 +0000765/// \param Loc the location where this type begins
766/// \param Entity the name of the entity that will have this member pointer type
767///
768/// \returns a member pointer type, if successful, or a NULL type if there was
769/// an error.
Mike Stump1eb44332009-09-09 15:08:12 +0000770QualType Sema::BuildMemberPointerType(QualType T, QualType Class,
John McCall0953e762009-09-24 19:53:00 +0000771 unsigned CVR, SourceLocation Loc,
Douglas Gregor949bf692009-06-09 22:17:39 +0000772 DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000773 Qualifiers Quals = Qualifiers::fromCVRMask(CVR);
774
Douglas Gregor949bf692009-06-09 22:17:39 +0000775 // Verify that we're not building a pointer to pointer to function with
776 // exception specification.
777 if (CheckDistantExceptionSpec(T)) {
778 Diag(Loc, diag::err_distant_exception_spec);
779
780 // FIXME: If we're doing this as part of template instantiation,
781 // we should return immediately.
782
783 // Build the type anyway, but use the canonical type so that the
784 // exception specifiers are stripped off.
785 T = Context.getCanonicalType(T);
786 }
787
788 // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member
789 // with reference type, or "cv void."
790 if (T->isReferenceType()) {
Anders Carlsson8d4655d2009-06-30 00:06:57 +0000791 Diag(Loc, diag::err_illegal_decl_mempointer_to_reference)
John McCallac406052009-10-30 00:37:20 +0000792 << (Entity? Entity.getAsString() : "type name") << T;
Douglas Gregor949bf692009-06-09 22:17:39 +0000793 return QualType();
794 }
795
796 if (T->isVoidType()) {
797 Diag(Loc, diag::err_illegal_decl_mempointer_to_void)
798 << (Entity? Entity.getAsString() : "type name");
799 return QualType();
800 }
801
802 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
803 // object or incomplete types shall not be restrict-qualified."
John McCall0953e762009-09-24 19:53:00 +0000804 if (Quals.hasRestrict() && !T->isIncompleteOrObjectType()) {
Douglas Gregor949bf692009-06-09 22:17:39 +0000805 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
806 << T;
807
808 // FIXME: If we're doing this as part of template instantiation,
809 // we should return immediately.
John McCall0953e762009-09-24 19:53:00 +0000810 Quals.removeRestrict();
Douglas Gregor949bf692009-06-09 22:17:39 +0000811 }
812
813 if (!Class->isDependentType() && !Class->isRecordType()) {
814 Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class;
815 return QualType();
816 }
817
John McCall0953e762009-09-24 19:53:00 +0000818 return Context.getQualifiedType(
819 Context.getMemberPointerType(T, Class.getTypePtr()), Quals);
Douglas Gregor949bf692009-06-09 22:17:39 +0000820}
Mike Stump1eb44332009-09-09 15:08:12 +0000821
Anders Carlsson9a917e42009-06-12 22:56:54 +0000822/// \brief Build a block pointer type.
823///
824/// \param T The type to which we'll be building a block pointer.
825///
John McCall0953e762009-09-24 19:53:00 +0000826/// \param CVR The cvr-qualifiers to be applied to the block pointer type.
Anders Carlsson9a917e42009-06-12 22:56:54 +0000827///
828/// \param Loc The location of the entity whose type involves this
829/// block pointer type or, if there is no such entity, the location of the
830/// type that will have block pointer type.
831///
832/// \param Entity The name of the entity that involves the block pointer
833/// type, if known.
834///
835/// \returns A suitable block pointer type, if there are no
836/// errors. Otherwise, returns a NULL type.
John McCall0953e762009-09-24 19:53:00 +0000837QualType Sema::BuildBlockPointerType(QualType T, unsigned CVR,
Mike Stump1eb44332009-09-09 15:08:12 +0000838 SourceLocation Loc,
Anders Carlsson9a917e42009-06-12 22:56:54 +0000839 DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000840 if (!T->isFunctionType()) {
Anders Carlsson9a917e42009-06-12 22:56:54 +0000841 Diag(Loc, diag::err_nonfunction_block_type);
842 return QualType();
843 }
Mike Stump1eb44332009-09-09 15:08:12 +0000844
John McCall0953e762009-09-24 19:53:00 +0000845 Qualifiers Quals = Qualifiers::fromCVRMask(CVR);
846 return Context.getQualifiedType(Context.getBlockPointerType(T), Quals);
Anders Carlsson9a917e42009-06-12 22:56:54 +0000847}
848
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000849QualType Sema::GetTypeFromParser(TypeTy *Ty, DeclaratorInfo **DInfo) {
850 QualType QT = QualType::getFromOpaquePtr(Ty);
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000851 if (QT.isNull()) {
852 if (DInfo) *DInfo = 0;
853 return QualType();
854 }
855
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000856 DeclaratorInfo *DI = 0;
857 if (LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) {
858 QT = LIT->getType();
859 DI = LIT->getDeclaratorInfo();
860 }
Mike Stump1eb44332009-09-09 15:08:12 +0000861
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000862 if (DInfo) *DInfo = DI;
863 return QT;
864}
865
Mike Stump98eb8a72009-02-04 22:31:32 +0000866/// GetTypeForDeclarator - Convert the type for the specified
Sebastian Redl8ce35b02009-10-25 21:45:37 +0000867/// declarator to Type instances.
Douglas Gregor402abb52009-05-28 23:31:59 +0000868///
869/// If OwnedDecl is non-NULL, and this declarator's decl-specifier-seq
870/// owns the declaration of a type (e.g., the definition of a struct
871/// type), then *OwnedDecl will receive the owned declaration.
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000872QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S,
Sebastian Redl8ce35b02009-10-25 21:45:37 +0000873 DeclaratorInfo **DInfo,
Douglas Gregor402abb52009-05-28 23:31:59 +0000874 TagDecl **OwnedDecl) {
Douglas Gregor930d8b52009-01-30 22:09:00 +0000875 // Determine the type of the declarator. Not all forms of declarator
876 // have a type.
877 QualType T;
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000878
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000879 switch (D.getName().getKind()) {
880 case UnqualifiedId::IK_Identifier:
881 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunt0486d742009-11-28 04:44:28 +0000882 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000883 case UnqualifiedId::IK_TemplateId:
Sebastian Redl8ce35b02009-10-25 21:45:37 +0000884 T = ConvertDeclSpecToType(D, *this);
Chris Lattner5db2bb12009-10-25 18:21:37 +0000885
886 if (!D.isInvalidType() && OwnedDecl && D.getDeclSpec().isTypeSpecOwned())
887 *OwnedDecl = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep());
Douglas Gregor930d8b52009-01-30 22:09:00 +0000888 break;
889
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000890 case UnqualifiedId::IK_ConstructorName:
891 case UnqualifiedId::IK_DestructorName:
892 case UnqualifiedId::IK_ConversionFunctionId:
Douglas Gregor930d8b52009-01-30 22:09:00 +0000893 // Constructors and destructors don't have return types. Use
894 // "void" instead. Conversion operators will check their return
895 // types separately.
896 T = Context.VoidTy;
897 break;
898 }
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000899
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000900 if (T == Context.UndeducedAutoTy) {
901 int Error = -1;
Mike Stump1eb44332009-09-09 15:08:12 +0000902
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000903 switch (D.getContext()) {
904 case Declarator::KNRTypeListContext:
905 assert(0 && "K&R type lists aren't allowed in C++");
906 break;
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000907 case Declarator::PrototypeContext:
908 Error = 0; // Function prototype
909 break;
910 case Declarator::MemberContext:
911 switch (cast<TagDecl>(CurContext)->getTagKind()) {
912 case TagDecl::TK_enum: assert(0 && "unhandled tag kind"); break;
913 case TagDecl::TK_struct: Error = 1; /* Struct member */ break;
914 case TagDecl::TK_union: Error = 2; /* Union member */ break;
915 case TagDecl::TK_class: Error = 3; /* Class member */ break;
Mike Stump1eb44332009-09-09 15:08:12 +0000916 }
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000917 break;
918 case Declarator::CXXCatchContext:
919 Error = 4; // Exception declaration
920 break;
921 case Declarator::TemplateParamContext:
922 Error = 5; // Template parameter
923 break;
924 case Declarator::BlockLiteralContext:
925 Error = 6; // Block literal
926 break;
927 case Declarator::FileContext:
928 case Declarator::BlockContext:
929 case Declarator::ForContext:
930 case Declarator::ConditionContext:
931 case Declarator::TypeNameContext:
932 break;
933 }
934
935 if (Error != -1) {
936 Diag(D.getDeclSpec().getTypeSpecTypeLoc(), diag::err_auto_not_allowed)
937 << Error;
938 T = Context.IntTy;
939 D.setInvalidType(true);
940 }
941 }
Mike Stump1eb44332009-09-09 15:08:12 +0000942
Douglas Gregorcd281c32009-02-28 00:25:32 +0000943 // The name we're declaring, if any.
944 DeclarationName Name;
945 if (D.getIdentifier())
946 Name = D.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +0000947
Mike Stump98eb8a72009-02-04 22:31:32 +0000948 // Walk the DeclTypeInfo, building the recursive type as we go.
949 // DeclTypeInfos are ordered from the identifier out, which is
950 // opposite of what we want :).
Sebastian Redl8ce35b02009-10-25 21:45:37 +0000951 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
952 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000953 switch (DeclType.Kind) {
954 default: assert(0 && "Unknown decltype!");
Steve Naroff5618bd42008-08-27 16:04:49 +0000955 case DeclaratorChunk::BlockPointer:
Chris Lattner9af55002009-03-27 04:18:06 +0000956 // If blocks are disabled, emit an error.
957 if (!LangOpts.Blocks)
958 Diag(DeclType.Loc, diag::err_blocks_disable);
Mike Stump1eb44332009-09-09 15:08:12 +0000959
960 T = BuildBlockPointerType(T, DeclType.Cls.TypeQuals, D.getIdentifierLoc(),
Anders Carlsson9a917e42009-06-12 22:56:54 +0000961 Name);
Steve Naroff5618bd42008-08-27 16:04:49 +0000962 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000963 case DeclaratorChunk::Pointer:
Sebastian Redl6a7330c2009-05-29 15:01:05 +0000964 // Verify that we're not building a pointer to pointer to function with
965 // exception specification.
966 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
967 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
968 D.setInvalidType(true);
969 // Build the type anyway.
970 }
Steve Naroff14108da2009-07-10 23:34:53 +0000971 if (getLangOptions().ObjC1 && T->isObjCInterfaceType()) {
John McCall183700f2009-09-21 23:43:11 +0000972 const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>();
Steve Naroff14108da2009-07-10 23:34:53 +0000973 T = Context.getObjCObjectPointerType(T,
Steve Naroff67ef8ea2009-07-20 17:56:53 +0000974 (ObjCProtocolDecl **)OIT->qual_begin(),
975 OIT->getNumProtocols());
Steve Naroff14108da2009-07-10 23:34:53 +0000976 break;
977 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000978 T = BuildPointerType(T, DeclType.Ptr.TypeQuals, DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000979 break;
John McCall0953e762009-09-24 19:53:00 +0000980 case DeclaratorChunk::Reference: {
981 Qualifiers Quals;
982 if (DeclType.Ref.HasRestrict) Quals.addRestrict();
983
Sebastian Redl6a7330c2009-05-29 15:01:05 +0000984 // Verify that we're not building a reference to pointer to function with
985 // exception specification.
986 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
987 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
988 D.setInvalidType(true);
989 // Build the type anyway.
990 }
John McCall0953e762009-09-24 19:53:00 +0000991 T = BuildReferenceType(T, DeclType.Ref.LValueRef, Quals,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000992 DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000993 break;
John McCall0953e762009-09-24 19:53:00 +0000994 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000995 case DeclaratorChunk::Array: {
Sebastian Redl6a7330c2009-05-29 15:01:05 +0000996 // Verify that we're not building an array of pointers to function with
997 // exception specification.
998 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
999 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1000 D.setInvalidType(true);
1001 // Build the type anyway.
1002 }
Chris Lattnerfd89bc82008-04-02 01:05:10 +00001003 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner94f81fd2007-08-28 16:54:00 +00001004 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001005 ArrayType::ArraySizeModifier ASM;
1006 if (ATI.isStar)
1007 ASM = ArrayType::Star;
1008 else if (ATI.hasStatic)
1009 ASM = ArrayType::Static;
1010 else
1011 ASM = ArrayType::Normal;
Eli Friedmanf91f5c82009-04-26 21:57:51 +00001012 if (ASM == ArrayType::Star &&
1013 D.getContext() != Declarator::PrototypeContext) {
1014 // FIXME: This check isn't quite right: it allows star in prototypes
1015 // for function definitions, and disallows some edge cases detailed
1016 // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html
1017 Diag(DeclType.Loc, diag::err_array_star_outside_prototype);
1018 ASM = ArrayType::Normal;
1019 D.setInvalidType(true);
1020 }
John McCall0953e762009-09-24 19:53:00 +00001021 T = BuildArrayType(T, ASM, ArraySize,
1022 Qualifiers::fromCVRMask(ATI.TypeQuals),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001023 SourceRange(DeclType.Loc, DeclType.EndLoc), Name);
Reid Spencer5f016e22007-07-11 17:01:13 +00001024 break;
1025 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001026 case DeclaratorChunk::Function: {
Reid Spencer5f016e22007-07-11 17:01:13 +00001027 // If the function declarator has a prototype (i.e. it is not () and
1028 // does not have a K&R-style identifier list), then the arguments are part
1029 // of the type, otherwise the argument list is ().
1030 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Sebastian Redl3cc97262009-05-31 11:47:27 +00001031
Chris Lattnercd881292007-12-19 05:31:29 +00001032 // C99 6.7.5.3p1: The return type may not be a function or array type.
Chris Lattner68cfd492007-12-19 18:01:43 +00001033 if (T->isArrayType() || T->isFunctionType()) {
Chris Lattnerd1625842008-11-24 06:25:27 +00001034 Diag(DeclType.Loc, diag::err_func_returning_array_function) << T;
Chris Lattnercd881292007-12-19 05:31:29 +00001035 T = Context.IntTy;
1036 D.setInvalidType(true);
1037 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001038
Douglas Gregor402abb52009-05-28 23:31:59 +00001039 if (getLangOptions().CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) {
1040 // C++ [dcl.fct]p6:
1041 // Types shall not be defined in return or parameter types.
1042 TagDecl *Tag = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep());
1043 if (Tag->isDefinition())
1044 Diag(Tag->getLocation(), diag::err_type_defined_in_result_type)
1045 << Context.getTypeDeclType(Tag);
1046 }
1047
Sebastian Redl3cc97262009-05-31 11:47:27 +00001048 // Exception specs are not allowed in typedefs. Complain, but add it
1049 // anyway.
1050 if (FTI.hasExceptionSpec &&
1051 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
1052 Diag(FTI.getThrowLoc(), diag::err_exception_spec_in_typedef);
1053
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001054 if (FTI.NumArgs == 0) {
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001055 if (getLangOptions().CPlusPlus) {
1056 // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the
1057 // function takes no arguments.
Sebastian Redl465226e2009-05-27 22:11:52 +00001058 llvm::SmallVector<QualType, 4> Exceptions;
1059 Exceptions.reserve(FTI.NumExceptions);
Mike Stump1eb44332009-09-09 15:08:12 +00001060 for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001061 // FIXME: Preserve type source info.
1062 QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty);
Sebastian Redlef65f062009-05-29 18:02:33 +00001063 // Check that the type is valid for an exception spec, and drop it
1064 // if not.
1065 if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1066 Exceptions.push_back(ET);
1067 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001068 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, FTI.TypeQuals,
1069 FTI.hasExceptionSpec,
1070 FTI.hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00001071 Exceptions.size(), Exceptions.data());
Douglas Gregor965acbb2009-02-18 07:07:28 +00001072 } else if (FTI.isVariadic) {
1073 // We allow a zero-parameter variadic function in C if the
1074 // function is marked with the "overloadable"
1075 // attribute. Scan for this attribute now.
1076 bool Overloadable = false;
1077 for (const AttributeList *Attrs = D.getAttributes();
1078 Attrs; Attrs = Attrs->getNext()) {
1079 if (Attrs->getKind() == AttributeList::AT_overloadable) {
1080 Overloadable = true;
1081 break;
1082 }
1083 }
1084
1085 if (!Overloadable)
1086 Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
1087 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, 0);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001088 } else {
1089 // Simple void foo(), where the incoming T is the result type.
Douglas Gregor72564e72009-02-26 23:50:07 +00001090 T = Context.getFunctionNoProtoType(T);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001091 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001092 } else if (FTI.ArgInfo[0].Param == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001093 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
Mike Stump1eb44332009-09-09 15:08:12 +00001094 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
John McCall54e14c42009-10-22 22:37:11 +00001095 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001096 } else {
1097 // Otherwise, we have a function with an argument list that is
1098 // potentially variadic.
1099 llvm::SmallVector<QualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +00001100
Reid Spencer5f016e22007-07-11 17:01:13 +00001101 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001102 ParmVarDecl *Param =
1103 cast<ParmVarDecl>(FTI.ArgInfo[i].Param.getAs<Decl>());
Chris Lattner8123a952008-04-10 02:22:51 +00001104 QualType ArgTy = Param->getType();
Chris Lattner78c75fb2007-07-21 05:30:18 +00001105 assert(!ArgTy.isNull() && "Couldn't parse type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001106
1107 // Adjust the parameter type.
Douglas Gregorbeb58cb2009-03-23 23:17:00 +00001108 assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001109
Reid Spencer5f016e22007-07-11 17:01:13 +00001110 // Look for 'void'. void is allowed only as a single argument to a
1111 // function with no other parameters (C99 6.7.5.3p10). We record
Douglas Gregor72564e72009-02-26 23:50:07 +00001112 // int(void) as a FunctionProtoType with an empty argument list.
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001113 if (ArgTy->isVoidType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001114 // If this is something like 'float(int, void)', reject it. 'void'
1115 // is an incomplete type (C99 6.2.5p19) and function decls cannot
1116 // have arguments of incomplete type.
1117 if (FTI.NumArgs != 1 || FTI.isVariadic) {
1118 Diag(DeclType.Loc, diag::err_void_only_param);
Chris Lattner2ff54262007-07-21 05:18:12 +00001119 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00001120 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00001121 } else if (FTI.ArgInfo[i].Ident) {
1122 // Reject, but continue to parse 'int(void abc)'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001123 Diag(FTI.ArgInfo[i].IdentLoc,
Chris Lattner4565d4e2007-07-21 05:26:43 +00001124 diag::err_param_with_void_type);
Chris Lattner2ff54262007-07-21 05:18:12 +00001125 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00001126 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00001127 } else {
1128 // Reject, but continue to parse 'float(const void)'.
John McCall0953e762009-09-24 19:53:00 +00001129 if (ArgTy.hasQualifiers())
Chris Lattner2ff54262007-07-21 05:18:12 +00001130 Diag(DeclType.Loc, diag::err_void_param_qualified);
Mike Stump1eb44332009-09-09 15:08:12 +00001131
Chris Lattner2ff54262007-07-21 05:18:12 +00001132 // Do not add 'void' to the ArgTys list.
1133 break;
1134 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001135 } else if (!FTI.hasPrototype) {
1136 if (ArgTy->isPromotableIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00001137 ArgTy = Context.getPromotedIntegerType(ArgTy);
John McCall183700f2009-09-21 23:43:11 +00001138 } else if (const BuiltinType* BTy = ArgTy->getAs<BuiltinType>()) {
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001139 if (BTy->getKind() == BuiltinType::Float)
1140 ArgTy = Context.DoubleTy;
1141 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001142 }
Mike Stump1eb44332009-09-09 15:08:12 +00001143
John McCall54e14c42009-10-22 22:37:11 +00001144 ArgTys.push_back(ArgTy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001145 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001146
1147 llvm::SmallVector<QualType, 4> Exceptions;
1148 Exceptions.reserve(FTI.NumExceptions);
Mike Stump1eb44332009-09-09 15:08:12 +00001149 for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001150 // FIXME: Preserve type source info.
1151 QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty);
Sebastian Redlef65f062009-05-29 18:02:33 +00001152 // Check that the type is valid for an exception spec, and drop it if
1153 // not.
1154 if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1155 Exceptions.push_back(ET);
1156 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001157
Jay Foadbeaaccd2009-05-21 09:52:38 +00001158 T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(),
Sebastian Redl465226e2009-05-27 22:11:52 +00001159 FTI.isVariadic, FTI.TypeQuals,
1160 FTI.hasExceptionSpec,
1161 FTI.hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00001162 Exceptions.size(), Exceptions.data());
Reid Spencer5f016e22007-07-11 17:01:13 +00001163 }
1164 break;
1165 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001166 case DeclaratorChunk::MemberPointer:
Sebastian Redl4994d2d2009-07-04 11:39:00 +00001167 // Verify that we're not building a pointer to pointer to function with
1168 // exception specification.
1169 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1170 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1171 D.setInvalidType(true);
1172 // Build the type anyway.
1173 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001174 // The scope spec must refer to a class, or be dependent.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001175 QualType ClsType;
Douglas Gregor87c12c42009-11-04 16:49:01 +00001176 if (isDependentScopeSpecifier(DeclType.Mem.Scope())
1177 || dyn_cast_or_null<CXXRecordDecl>(
1178 computeDeclContext(DeclType.Mem.Scope()))) {
Mike Stump1eb44332009-09-09 15:08:12 +00001179 NestedNameSpecifier *NNS
Douglas Gregor949bf692009-06-09 22:17:39 +00001180 = (NestedNameSpecifier *)DeclType.Mem.Scope().getScopeRep();
Douglas Gregor87c12c42009-11-04 16:49:01 +00001181 NestedNameSpecifier *NNSPrefix = NNS->getPrefix();
1182 switch (NNS->getKind()) {
1183 case NestedNameSpecifier::Identifier:
1184 ClsType = Context.getTypenameType(NNSPrefix, NNS->getAsIdentifier());
1185 break;
1186
1187 case NestedNameSpecifier::Namespace:
1188 case NestedNameSpecifier::Global:
1189 llvm::llvm_unreachable("Nested-name-specifier must name a type");
1190 break;
1191
1192 case NestedNameSpecifier::TypeSpec:
1193 case NestedNameSpecifier::TypeSpecWithTemplate:
1194 ClsType = QualType(NNS->getAsType(), 0);
1195 if (NNSPrefix)
1196 ClsType = Context.getQualifiedNameType(NNSPrefix, ClsType);
1197 break;
1198 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001199 } else {
Douglas Gregor949bf692009-06-09 22:17:39 +00001200 Diag(DeclType.Mem.Scope().getBeginLoc(),
1201 diag::err_illegal_decl_mempointer_in_nonclass)
1202 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
1203 << DeclType.Mem.Scope().getRange();
Sebastian Redlf30208a2009-01-24 21:16:55 +00001204 D.setInvalidType(true);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001205 }
1206
Douglas Gregor949bf692009-06-09 22:17:39 +00001207 if (!ClsType.isNull())
1208 T = BuildMemberPointerType(T, ClsType, DeclType.Mem.TypeQuals,
1209 DeclType.Loc, D.getIdentifier());
1210 if (T.isNull()) {
1211 T = Context.IntTy;
Sebastian Redlf30208a2009-01-24 21:16:55 +00001212 D.setInvalidType(true);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001213 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001214 break;
1215 }
1216
Douglas Gregorcd281c32009-02-28 00:25:32 +00001217 if (T.isNull()) {
1218 D.setInvalidType(true);
1219 T = Context.IntTy;
1220 }
1221
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001222 // See if there are any attributes on this declarator chunk.
1223 if (const AttributeList *AL = DeclType.getAttrs())
1224 ProcessTypeAttributeList(T, AL);
Reid Spencer5f016e22007-07-11 17:01:13 +00001225 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001226
1227 if (getLangOptions().CPlusPlus && T->isFunctionType()) {
John McCall183700f2009-09-21 23:43:11 +00001228 const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>();
Chris Lattner778ed742009-10-25 17:36:50 +00001229 assert(FnTy && "Why oh why is there not a FunctionProtoType here?");
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001230
1231 // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type
1232 // for a nonstatic member function, the function type to which a pointer
1233 // to member refers, or the top-level function type of a function typedef
1234 // declaration.
1235 if (FnTy->getTypeQuals() != 0 &&
1236 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
Douglas Gregor584049d2008-12-15 23:53:10 +00001237 ((D.getContext() != Declarator::MemberContext &&
1238 (!D.getCXXScopeSpec().isSet() ||
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001239 !computeDeclContext(D.getCXXScopeSpec(), /*FIXME:*/true)
1240 ->isRecord())) ||
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001241 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001242 if (D.isFunctionDeclarator())
1243 Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
1244 else
1245 Diag(D.getIdentifierLoc(),
1246 diag::err_invalid_qualified_typedef_function_type_use);
1247
1248 // Strip the cv-quals from the type.
1249 T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001250 FnTy->getNumArgs(), FnTy->isVariadic(), 0);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001251 }
1252 }
Mike Stump1eb44332009-09-09 15:08:12 +00001253
Chris Lattner0bf29ad2008-06-29 00:19:33 +00001254 // If there were any type attributes applied to the decl itself (not the
1255 // type, apply the type attribute to the type!)
1256 if (const AttributeList *Attrs = D.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001257 ProcessTypeAttributeList(T, Attrs);
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001258
John McCall54e14c42009-10-22 22:37:11 +00001259 if (DInfo) {
1260 if (D.isInvalidType())
1261 *DInfo = 0;
1262 else
Sebastian Redl8ce35b02009-10-25 21:45:37 +00001263 *DInfo = GetDeclaratorInfoForDeclarator(D, T);
John McCall54e14c42009-10-22 22:37:11 +00001264 }
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001265
Reid Spencer5f016e22007-07-11 17:01:13 +00001266 return T;
1267}
1268
John McCall51bd8032009-10-18 01:05:36 +00001269namespace {
1270 class TypeSpecLocFiller : public TypeLocVisitor<TypeSpecLocFiller> {
1271 const DeclSpec &DS;
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001272
John McCall51bd8032009-10-18 01:05:36 +00001273 public:
1274 TypeSpecLocFiller(const DeclSpec &DS) : DS(DS) {}
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001275
John McCall51bd8032009-10-18 01:05:36 +00001276 void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
1277 Visit(TL.getUnqualifiedLoc());
1278 }
1279 void VisitTypedefTypeLoc(TypedefTypeLoc TL) {
1280 TL.setNameLoc(DS.getTypeSpecTypeLoc());
1281 }
1282 void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
1283 TL.setNameLoc(DS.getTypeSpecTypeLoc());
Argyrios Kyrtzidiseb667592009-09-29 19:45:22 +00001284
John McCall54e14c42009-10-22 22:37:11 +00001285 if (DS.getProtocolQualifiers()) {
1286 assert(TL.getNumProtocols() > 0);
1287 assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
1288 TL.setLAngleLoc(DS.getProtocolLAngleLoc());
1289 TL.setRAngleLoc(DS.getSourceRange().getEnd());
1290 for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i)
1291 TL.setProtocolLoc(i, DS.getProtocolLocs()[i]);
1292 } else {
1293 assert(TL.getNumProtocols() == 0);
1294 TL.setLAngleLoc(SourceLocation());
1295 TL.setRAngleLoc(SourceLocation());
1296 }
1297 }
1298 void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
1299 assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
1300
1301 TL.setStarLoc(SourceLocation());
1302
1303 if (DS.getProtocolQualifiers()) {
1304 assert(TL.getNumProtocols() > 0);
1305 assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
1306 TL.setHasProtocolsAsWritten(true);
1307 TL.setLAngleLoc(DS.getProtocolLAngleLoc());
1308 TL.setRAngleLoc(DS.getSourceRange().getEnd());
1309 for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i)
1310 TL.setProtocolLoc(i, DS.getProtocolLocs()[i]);
1311
1312 } else {
1313 assert(TL.getNumProtocols() == 0);
1314 TL.setHasProtocolsAsWritten(false);
1315 TL.setLAngleLoc(SourceLocation());
1316 TL.setRAngleLoc(SourceLocation());
1317 }
1318
1319 // This might not have been written with an inner type.
1320 if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) {
1321 TL.setHasBaseTypeAsWritten(false);
1322 TL.getBaseTypeLoc().initialize(SourceLocation());
1323 } else {
1324 TL.setHasBaseTypeAsWritten(true);
John McCall51bd8032009-10-18 01:05:36 +00001325 Visit(TL.getBaseTypeLoc());
John McCall54e14c42009-10-22 22:37:11 +00001326 }
John McCall51bd8032009-10-18 01:05:36 +00001327 }
John McCall833ca992009-10-29 08:12:44 +00001328 void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) {
1329 DeclaratorInfo *DInfo = 0;
1330 Sema::GetTypeFromParser(DS.getTypeRep(), &DInfo);
1331
1332 // If we got no declarator info from previous Sema routines,
1333 // just fill with the typespec loc.
1334 if (!DInfo) {
1335 TL.initialize(DS.getTypeSpecTypeLoc());
1336 return;
1337 }
1338
1339 TemplateSpecializationTypeLoc OldTL =
1340 cast<TemplateSpecializationTypeLoc>(DInfo->getTypeLoc());
1341 TL.copy(OldTL);
1342 }
John McCall51bd8032009-10-18 01:05:36 +00001343 void VisitTypeLoc(TypeLoc TL) {
1344 // FIXME: add other typespec types and change this to an assert.
1345 TL.initialize(DS.getTypeSpecTypeLoc());
1346 }
1347 };
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001348
John McCall51bd8032009-10-18 01:05:36 +00001349 class DeclaratorLocFiller : public TypeLocVisitor<DeclaratorLocFiller> {
1350 const DeclaratorChunk &Chunk;
1351
1352 public:
1353 DeclaratorLocFiller(const DeclaratorChunk &Chunk) : Chunk(Chunk) {}
1354
1355 void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
1356 llvm::llvm_unreachable("qualified type locs not expected here!");
1357 }
1358
1359 void VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
1360 assert(Chunk.Kind == DeclaratorChunk::BlockPointer);
1361 TL.setCaretLoc(Chunk.Loc);
1362 }
1363 void VisitPointerTypeLoc(PointerTypeLoc TL) {
1364 assert(Chunk.Kind == DeclaratorChunk::Pointer);
1365 TL.setStarLoc(Chunk.Loc);
1366 }
1367 void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
1368 assert(Chunk.Kind == DeclaratorChunk::Pointer);
1369 TL.setStarLoc(Chunk.Loc);
John McCall54e14c42009-10-22 22:37:11 +00001370 TL.setHasBaseTypeAsWritten(true);
1371 TL.setHasProtocolsAsWritten(false);
1372 TL.setLAngleLoc(SourceLocation());
1373 TL.setRAngleLoc(SourceLocation());
John McCall51bd8032009-10-18 01:05:36 +00001374 }
1375 void VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
1376 assert(Chunk.Kind == DeclaratorChunk::MemberPointer);
1377 TL.setStarLoc(Chunk.Loc);
1378 // FIXME: nested name specifier
1379 }
1380 void VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
1381 assert(Chunk.Kind == DeclaratorChunk::Reference);
John McCall54e14c42009-10-22 22:37:11 +00001382 // 'Amp' is misleading: this might have been originally
1383 /// spelled with AmpAmp.
John McCall51bd8032009-10-18 01:05:36 +00001384 TL.setAmpLoc(Chunk.Loc);
1385 }
1386 void VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
1387 assert(Chunk.Kind == DeclaratorChunk::Reference);
1388 assert(!Chunk.Ref.LValueRef);
1389 TL.setAmpAmpLoc(Chunk.Loc);
1390 }
1391 void VisitArrayTypeLoc(ArrayTypeLoc TL) {
1392 assert(Chunk.Kind == DeclaratorChunk::Array);
1393 TL.setLBracketLoc(Chunk.Loc);
1394 TL.setRBracketLoc(Chunk.EndLoc);
1395 TL.setSizeExpr(static_cast<Expr*>(Chunk.Arr.NumElts));
1396 }
1397 void VisitFunctionTypeLoc(FunctionTypeLoc TL) {
1398 assert(Chunk.Kind == DeclaratorChunk::Function);
1399 TL.setLParenLoc(Chunk.Loc);
1400 TL.setRParenLoc(Chunk.EndLoc);
1401
1402 const DeclaratorChunk::FunctionTypeInfo &FTI = Chunk.Fun;
John McCall54e14c42009-10-22 22:37:11 +00001403 for (unsigned i = 0, e = TL.getNumArgs(), tpi = 0; i != e; ++i) {
John McCall51bd8032009-10-18 01:05:36 +00001404 ParmVarDecl *Param = FTI.ArgInfo[i].Param.getAs<ParmVarDecl>();
John McCall54e14c42009-10-22 22:37:11 +00001405 TL.setArg(tpi++, Param);
John McCall51bd8032009-10-18 01:05:36 +00001406 }
1407 // FIXME: exception specs
1408 }
1409
1410 void VisitTypeLoc(TypeLoc TL) {
1411 llvm::llvm_unreachable("unsupported TypeLoc kind in declarator!");
1412 }
1413 };
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001414}
1415
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001416/// \brief Create and instantiate a DeclaratorInfo with type source information.
1417///
1418/// \param T QualType referring to the type as written in source code.
1419DeclaratorInfo *
Sebastian Redl8ce35b02009-10-25 21:45:37 +00001420Sema::GetDeclaratorInfoForDeclarator(Declarator &D, QualType T) {
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001421 DeclaratorInfo *DInfo = Context.CreateDeclaratorInfo(T);
John McCall51bd8032009-10-18 01:05:36 +00001422 UnqualTypeLoc CurrTL = DInfo->getTypeLoc().getUnqualifiedLoc();
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001423
Sebastian Redl8ce35b02009-10-25 21:45:37 +00001424 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
John McCall51bd8032009-10-18 01:05:36 +00001425 DeclaratorLocFiller(D.getTypeObject(i)).Visit(CurrTL);
1426 CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001427 }
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001428
John McCall51bd8032009-10-18 01:05:36 +00001429 TypeSpecLocFiller(D.getDeclSpec()).Visit(CurrTL);
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001430
1431 return DInfo;
1432}
1433
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001434/// \brief Create a LocInfoType to hold the given QualType and DeclaratorInfo.
1435QualType Sema::CreateLocInfoType(QualType T, DeclaratorInfo *DInfo) {
1436 // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser
1437 // and Sema during declaration parsing. Try deallocating/caching them when
1438 // it's appropriate, instead of allocating them and keeping them around.
1439 LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType), 8);
1440 new (LocT) LocInfoType(T, DInfo);
1441 assert(LocT->getTypeClass() != T->getTypeClass() &&
1442 "LocInfoType's TypeClass conflicts with an existing Type class");
1443 return QualType(LocT, 0);
1444}
1445
1446void LocInfoType::getAsStringInternal(std::string &Str,
1447 const PrintingPolicy &Policy) const {
Argyrios Kyrtzidis35d44e52009-08-19 01:46:06 +00001448 assert(false && "LocInfoType leaked into the type system; an opaque TypeTy*"
1449 " was used directly instead of getting the QualType through"
1450 " GetTypeFromParser");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001451}
1452
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001453/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
Fariborz Jahanian360300c2007-11-09 22:27:59 +00001454/// declarator
Chris Lattnerb28317a2009-03-28 19:18:32 +00001455QualType Sema::ObjCGetTypeForMethodDefinition(DeclPtrTy D) {
1456 ObjCMethodDecl *MDecl = cast<ObjCMethodDecl>(D.getAs<Decl>());
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001457 QualType T = MDecl->getResultType();
1458 llvm::SmallVector<QualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +00001459
Fariborz Jahanian35600022007-11-09 17:18:29 +00001460 // Add the first two invisible argument types for self and _cmd.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001461 if (MDecl->isInstanceMethod()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001462 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +00001463 selfTy = Context.getPointerType(selfTy);
1464 ArgTys.push_back(selfTy);
Chris Lattner89951a82009-02-20 18:43:26 +00001465 } else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001466 ArgTys.push_back(Context.getObjCIdType());
1467 ArgTys.push_back(Context.getObjCSelType());
Mike Stump1eb44332009-09-09 15:08:12 +00001468
Chris Lattner89951a82009-02-20 18:43:26 +00001469 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
1470 E = MDecl->param_end(); PI != E; ++PI) {
1471 QualType ArgTy = (*PI)->getType();
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001472 assert(!ArgTy.isNull() && "Couldn't parse type?");
Douglas Gregorbeb58cb2009-03-23 23:17:00 +00001473 ArgTy = adjustParameterType(ArgTy);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001474 ArgTys.push_back(ArgTy);
1475 }
1476 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001477 MDecl->isVariadic(), 0);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001478 return T;
1479}
1480
Sebastian Redl9e5e4aa2009-01-26 19:54:48 +00001481/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that
1482/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
1483/// they point to and return true. If T1 and T2 aren't pointer types
1484/// or pointer-to-member types, or if they are not similar at this
1485/// level, returns false and leaves T1 and T2 unchanged. Top-level
1486/// qualifiers on T1 and T2 are ignored. This function will typically
1487/// be called in a loop that successively "unwraps" pointer and
1488/// pointer-to-member types to compare them at each level.
Chris Lattnerecb81f22009-02-16 21:43:00 +00001489bool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001490 const PointerType *T1PtrType = T1->getAs<PointerType>(),
1491 *T2PtrType = T2->getAs<PointerType>();
Douglas Gregor57373262008-10-22 14:17:15 +00001492 if (T1PtrType && T2PtrType) {
1493 T1 = T1PtrType->getPointeeType();
1494 T2 = T2PtrType->getPointeeType();
1495 return true;
1496 }
1497
Ted Kremenek6217b802009-07-29 21:53:49 +00001498 const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(),
1499 *T2MPType = T2->getAs<MemberPointerType>();
Sebastian Redl21593ac2009-01-28 18:33:18 +00001500 if (T1MPType && T2MPType &&
1501 Context.getCanonicalType(T1MPType->getClass()) ==
1502 Context.getCanonicalType(T2MPType->getClass())) {
Sebastian Redl4433aaf2009-01-25 19:43:20 +00001503 T1 = T1MPType->getPointeeType();
1504 T2 = T2MPType->getPointeeType();
1505 return true;
1506 }
Douglas Gregor57373262008-10-22 14:17:15 +00001507 return false;
1508}
1509
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001510Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001511 // C99 6.7.6: Type names have no identifier. This is already validated by
1512 // the parser.
1513 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00001514
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001515 DeclaratorInfo *DInfo = 0;
Douglas Gregor402abb52009-05-28 23:31:59 +00001516 TagDecl *OwnedTag = 0;
Sebastian Redl8ce35b02009-10-25 21:45:37 +00001517 QualType T = GetTypeForDeclarator(D, S, &DInfo, &OwnedTag);
Chris Lattner5153ee62009-04-25 08:47:54 +00001518 if (D.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +00001519 return true;
Steve Naroff5912a352007-08-28 20:14:24 +00001520
Douglas Gregor402abb52009-05-28 23:31:59 +00001521 if (getLangOptions().CPlusPlus) {
1522 // Check that there are no default arguments (C++ only).
Douglas Gregor6d6eb572008-05-07 04:49:29 +00001523 CheckExtraCXXDefaultArguments(D);
1524
Douglas Gregor402abb52009-05-28 23:31:59 +00001525 // C++0x [dcl.type]p3:
1526 // A type-specifier-seq shall not define a class or enumeration
1527 // unless it appears in the type-id of an alias-declaration
1528 // (7.1.3).
1529 if (OwnedTag && OwnedTag->isDefinition())
1530 Diag(OwnedTag->getLocation(), diag::err_type_defined_in_type_specifier)
1531 << Context.getTypeDeclType(OwnedTag);
1532 }
1533
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001534 if (DInfo)
1535 T = CreateLocInfoType(T, DInfo);
1536
Reid Spencer5f016e22007-07-11 17:01:13 +00001537 return T.getAsOpaquePtr();
1538}
1539
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001540
1541
1542//===----------------------------------------------------------------------===//
1543// Type Attribute Processing
1544//===----------------------------------------------------------------------===//
1545
1546/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
1547/// specified type. The attribute contains 1 argument, the id of the address
1548/// space for the type.
Mike Stump1eb44332009-09-09 15:08:12 +00001549static void HandleAddressSpaceTypeAttribute(QualType &Type,
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001550 const AttributeList &Attr, Sema &S){
John McCall0953e762009-09-24 19:53:00 +00001551
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001552 // If this type is already address space qualified, reject it.
1553 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
1554 // for two or more different address spaces."
1555 if (Type.getAddressSpace()) {
1556 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
1557 return;
1558 }
Mike Stump1eb44332009-09-09 15:08:12 +00001559
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001560 // Check the attribute arguments.
1561 if (Attr.getNumArgs() != 1) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +00001562 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001563 return;
1564 }
1565 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
1566 llvm::APSInt addrSpace(32);
1567 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001568 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
1569 << ASArgExpr->getSourceRange();
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001570 return;
1571 }
1572
John McCallefadb772009-07-28 06:52:18 +00001573 // Bounds checking.
1574 if (addrSpace.isSigned()) {
1575 if (addrSpace.isNegative()) {
1576 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative)
1577 << ASArgExpr->getSourceRange();
1578 return;
1579 }
1580 addrSpace.setIsSigned(false);
1581 }
1582 llvm::APSInt max(addrSpace.getBitWidth());
John McCall0953e762009-09-24 19:53:00 +00001583 max = Qualifiers::MaxAddressSpace;
John McCallefadb772009-07-28 06:52:18 +00001584 if (addrSpace > max) {
1585 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high)
John McCall0953e762009-09-24 19:53:00 +00001586 << Qualifiers::MaxAddressSpace << ASArgExpr->getSourceRange();
John McCallefadb772009-07-28 06:52:18 +00001587 return;
1588 }
1589
Mike Stump1eb44332009-09-09 15:08:12 +00001590 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001591 Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001592}
1593
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001594/// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the
1595/// specified type. The attribute contains 1 argument, weak or strong.
Mike Stump1eb44332009-09-09 15:08:12 +00001596static void HandleObjCGCTypeAttribute(QualType &Type,
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001597 const AttributeList &Attr, Sema &S) {
John McCall0953e762009-09-24 19:53:00 +00001598 if (Type.getObjCGCAttr() != Qualifiers::GCNone) {
Fariborz Jahanian5934e752009-02-18 18:52:41 +00001599 S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001600 return;
1601 }
Mike Stump1eb44332009-09-09 15:08:12 +00001602
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001603 // Check the attribute arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001604 if (!Attr.getParameterName()) {
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001605 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1606 << "objc_gc" << 1;
1607 return;
1608 }
John McCall0953e762009-09-24 19:53:00 +00001609 Qualifiers::GC GCAttr;
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001610 if (Attr.getNumArgs() != 0) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001611 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1612 return;
1613 }
Mike Stump1eb44332009-09-09 15:08:12 +00001614 if (Attr.getParameterName()->isStr("weak"))
John McCall0953e762009-09-24 19:53:00 +00001615 GCAttr = Qualifiers::Weak;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001616 else if (Attr.getParameterName()->isStr("strong"))
John McCall0953e762009-09-24 19:53:00 +00001617 GCAttr = Qualifiers::Strong;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001618 else {
1619 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1620 << "objc_gc" << Attr.getParameterName();
1621 return;
1622 }
Mike Stump1eb44332009-09-09 15:08:12 +00001623
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001624 Type = S.Context.getObjCGCQualType(Type, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001625}
1626
Mike Stump24556362009-07-25 21:26:53 +00001627/// HandleNoReturnTypeAttribute - Process the noreturn attribute on the
1628/// specified type. The attribute contains 0 arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001629static void HandleNoReturnTypeAttribute(QualType &Type,
Mike Stump24556362009-07-25 21:26:53 +00001630 const AttributeList &Attr, Sema &S) {
1631 if (Attr.getNumArgs() != 0)
1632 return;
1633
1634 // We only apply this to a pointer to function or a pointer to block.
1635 if (!Type->isFunctionPointerType()
1636 && !Type->isBlockPointerType()
1637 && !Type->isFunctionType())
1638 return;
1639
1640 Type = S.Context.getNoReturnType(Type);
1641}
1642
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001643void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) {
Chris Lattner232e8822008-02-21 01:08:11 +00001644 // Scan through and apply attributes to this type where it makes sense. Some
1645 // attributes (such as __address_space__, __vector_size__, etc) apply to the
1646 // type, but others can be present in the type specifiers even though they
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00001647 // apply to the decl. Here we apply type attributes and ignore the rest.
1648 for (; AL; AL = AL->getNext()) {
Chris Lattner232e8822008-02-21 01:08:11 +00001649 // If this is an attribute we can handle, do so now, otherwise, add it to
1650 // the LeftOverAttrs list for rechaining.
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00001651 switch (AL->getKind()) {
Chris Lattner232e8822008-02-21 01:08:11 +00001652 default: break;
1653 case AttributeList::AT_address_space:
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001654 HandleAddressSpaceTypeAttribute(Result, *AL, *this);
1655 break;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001656 case AttributeList::AT_objc_gc:
1657 HandleObjCGCTypeAttribute(Result, *AL, *this);
1658 break;
Mike Stump24556362009-07-25 21:26:53 +00001659 case AttributeList::AT_noreturn:
1660 HandleNoReturnTypeAttribute(Result, *AL, *this);
1661 break;
Chris Lattner232e8822008-02-21 01:08:11 +00001662 }
Chris Lattner232e8822008-02-21 01:08:11 +00001663 }
Chris Lattner232e8822008-02-21 01:08:11 +00001664}
1665
Mike Stump1eb44332009-09-09 15:08:12 +00001666/// @brief Ensure that the type T is a complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001667///
1668/// This routine checks whether the type @p T is complete in any
1669/// context where a complete type is required. If @p T is a complete
Douglas Gregor86447ec2009-03-09 16:13:40 +00001670/// type, returns false. If @p T is a class template specialization,
1671/// this routine then attempts to perform class template
1672/// instantiation. If instantiation fails, or if @p T is incomplete
1673/// and cannot be completed, issues the diagnostic @p diag (giving it
1674/// the type @p T) and returns true.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001675///
1676/// @param Loc The location in the source that the incomplete type
1677/// diagnostic should refer to.
1678///
1679/// @param T The type that this routine is examining for completeness.
1680///
Mike Stump1eb44332009-09-09 15:08:12 +00001681/// @param PD The partial diagnostic that will be printed out if T is not a
Anders Carlssonb7906612009-08-26 23:45:07 +00001682/// complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001683///
1684/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
1685/// @c false otherwise.
Anders Carlsson91a0cc92009-08-26 22:33:56 +00001686bool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
Anders Carlsson8c8d9192009-10-09 23:51:55 +00001687 const PartialDiagnostic &PD,
1688 std::pair<SourceLocation,
1689 PartialDiagnostic> Note) {
Anders Carlsson91a0cc92009-08-26 22:33:56 +00001690 unsigned diag = PD.getDiagID();
Mike Stump1eb44332009-09-09 15:08:12 +00001691
Douglas Gregor573d9c32009-10-21 23:19:44 +00001692 // FIXME: Add this assertion to make sure we always get instantiation points.
1693 // assert(!Loc.isInvalid() && "Invalid location in RequireCompleteType");
Douglas Gregor690dc7f2009-05-21 23:48:18 +00001694 // FIXME: Add this assertion to help us flush out problems with
1695 // checking for dependent types and type-dependent expressions.
1696 //
Mike Stump1eb44332009-09-09 15:08:12 +00001697 // assert(!T->isDependentType() &&
Douglas Gregor690dc7f2009-05-21 23:48:18 +00001698 // "Can't ask whether a dependent type is complete");
1699
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001700 // If we have a complete type, we're done.
1701 if (!T->isIncompleteType())
1702 return false;
Eli Friedman3c0eb162008-05-27 03:33:27 +00001703
Douglas Gregord475b8d2009-03-25 21:17:03 +00001704 // If we have a class template specialization or a class member of a
Sebastian Redl923d56d2009-11-05 15:52:31 +00001705 // class template specialization, or an array with known size of such,
1706 // try to instantiate it.
1707 QualType MaybeTemplate = T;
Douglas Gregor89c49f02009-11-09 22:08:55 +00001708 if (const ConstantArrayType *Array = Context.getAsConstantArrayType(T))
Sebastian Redl923d56d2009-11-05 15:52:31 +00001709 MaybeTemplate = Array->getElementType();
1710 if (const RecordType *Record = MaybeTemplate->getAs<RecordType>()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001711 if (ClassTemplateSpecializationDecl *ClassTemplateSpec
Douglas Gregord475b8d2009-03-25 21:17:03 +00001712 = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001713 if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared)
1714 return InstantiateClassTemplateSpecialization(Loc, ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001715 TSK_ImplicitInstantiation,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001716 /*Complain=*/diag != 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001717 } else if (CXXRecordDecl *Rec
Douglas Gregord475b8d2009-03-25 21:17:03 +00001718 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
1719 if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001720 MemberSpecializationInfo *MSInfo = Rec->getMemberSpecializationInfo();
1721 assert(MSInfo && "Missing member specialization information?");
Douglas Gregor357bbd02009-08-28 20:50:45 +00001722 // This record was instantiated from a class within a template.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001723 if (MSInfo->getTemplateSpecializationKind()
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001724 != TSK_ExplicitSpecialization)
Douglas Gregorf6b11852009-10-08 15:14:33 +00001725 return InstantiateClass(Loc, Rec, Pattern,
1726 getTemplateInstantiationArgs(Rec),
1727 TSK_ImplicitInstantiation,
1728 /*Complain=*/diag != 0);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001729 }
1730 }
1731 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00001732
Douglas Gregor5842ba92009-08-24 15:23:48 +00001733 if (diag == 0)
1734 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001735
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001736 // We have an incomplete type. Produce a diagnostic.
Anders Carlsson91a0cc92009-08-26 22:33:56 +00001737 Diag(Loc, PD) << T;
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001738
Anders Carlsson8c8d9192009-10-09 23:51:55 +00001739 // If we have a note, produce it.
1740 if (!Note.first.isInvalid())
1741 Diag(Note.first, Note.second);
1742
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001743 // If the type was a forward declaration of a class/struct/union
Mike Stump1eb44332009-09-09 15:08:12 +00001744 // type, produce
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001745 const TagType *Tag = 0;
Ted Kremenek6217b802009-07-29 21:53:49 +00001746 if (const RecordType *Record = T->getAs<RecordType>())
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001747 Tag = Record;
John McCall183700f2009-09-21 23:43:11 +00001748 else if (const EnumType *Enum = T->getAs<EnumType>())
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001749 Tag = Enum;
1750
1751 if (Tag && !Tag->getDecl()->isInvalidDecl())
Mike Stump1eb44332009-09-09 15:08:12 +00001752 Diag(Tag->getDecl()->getLocation(),
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001753 Tag->isBeingDefined() ? diag::note_type_being_defined
1754 : diag::note_forward_declaration)
1755 << QualType(Tag, 0);
1756
1757 return true;
1758}
Douglas Gregore6258932009-03-19 00:39:20 +00001759
1760/// \brief Retrieve a version of the type 'T' that is qualified by the
1761/// nested-name-specifier contained in SS.
1762QualType Sema::getQualifiedNameType(const CXXScopeSpec &SS, QualType T) {
1763 if (!SS.isSet() || SS.isInvalid() || T.isNull())
1764 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00001765
Douglas Gregorab452ba2009-03-26 23:50:42 +00001766 NestedNameSpecifier *NNS
Douglas Gregor35073692009-03-26 23:56:24 +00001767 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Douglas Gregorab452ba2009-03-26 23:50:42 +00001768 return Context.getQualifiedNameType(NNS, T);
Douglas Gregore6258932009-03-19 00:39:20 +00001769}
Anders Carlssonaf017e62009-06-29 22:58:55 +00001770
1771QualType Sema::BuildTypeofExprType(Expr *E) {
1772 return Context.getTypeOfExprType(E);
1773}
1774
1775QualType Sema::BuildDecltypeType(Expr *E) {
1776 if (E->getType() == Context.OverloadTy) {
Mike Stump1eb44332009-09-09 15:08:12 +00001777 Diag(E->getLocStart(),
Anders Carlssonaf017e62009-06-29 22:58:55 +00001778 diag::err_cannot_determine_declared_type_of_overloaded_function);
1779 return QualType();
1780 }
1781 return Context.getDecltypeType(E);
1782}