blob: c135f43209abcb0ff51597a341b179d44bbec624 [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"
Reid Spencer5f016e22007-07-11 17:01:13 +000025using namespace clang;
26
Douglas Gregor2dc0e642009-03-23 23:06:20 +000027/// \brief Perform adjustment on the parameter type of a function.
28///
29/// This routine adjusts the given parameter type @p T to the actual
Mike Stump1eb44332009-09-09 15:08:12 +000030/// parameter type used by semantic analysis (C99 6.7.5.3p[7,8],
31/// C++ [dcl.fct]p3). The adjusted parameter type is returned.
Douglas Gregor2dc0e642009-03-23 23:06:20 +000032QualType Sema::adjustParameterType(QualType T) {
33 // C99 6.7.5.3p7:
Chris Lattner778ed742009-10-25 17:36:50 +000034 // A declaration of a parameter as "array of type" shall be
35 // adjusted to "qualified pointer to type", where the type
36 // qualifiers (if any) are those specified within the [ and ] of
37 // the array type derivation.
38 if (T->isArrayType())
Douglas Gregor2dc0e642009-03-23 23:06:20 +000039 return Context.getArrayDecayedType(T);
Chris Lattner778ed742009-10-25 17:36:50 +000040
41 // C99 6.7.5.3p8:
42 // A declaration of a parameter as "function returning type"
43 // shall be adjusted to "pointer to function returning type", as
44 // in 6.3.2.1.
45 if (T->isFunctionType())
Douglas Gregor2dc0e642009-03-23 23:06:20 +000046 return Context.getPointerType(T);
47
48 return T;
49}
50
Chris Lattner5db2bb12009-10-25 18:21:37 +000051
52
53/// isOmittedBlockReturnType - Return true if this declarator is missing a
54/// return type because this is a omitted return type on a block literal.
Sebastian Redl8ce35b02009-10-25 21:45:37 +000055static bool isOmittedBlockReturnType(const Declarator &D) {
Chris Lattner5db2bb12009-10-25 18:21:37 +000056 if (D.getContext() != Declarator::BlockLiteralContext ||
Sebastian Redl8ce35b02009-10-25 21:45:37 +000057 D.getDeclSpec().hasTypeSpecifier())
Chris Lattner5db2bb12009-10-25 18:21:37 +000058 return false;
59
60 if (D.getNumTypeObjects() == 0)
Chris Lattnera64ef0a2009-10-25 22:09:09 +000061 return true; // ^{ ... }
Chris Lattner5db2bb12009-10-25 18:21:37 +000062
63 if (D.getNumTypeObjects() == 1 &&
64 D.getTypeObject(0).Kind == DeclaratorChunk::Function)
Chris Lattnera64ef0a2009-10-25 22:09:09 +000065 return true; // ^(int X, float Y) { ... }
Chris Lattner5db2bb12009-10-25 18:21:37 +000066
67 return false;
68}
69
Chris Lattner52338262009-10-25 22:31:57 +000070/// isDeclaratorDeprecated - Return true if the declarator is deprecated.
71/// We do not want to warn about use of deprecated types (e.g. typedefs) when
72/// defining a declaration that is itself deprecated.
73static bool isDeclaratorDeprecated(const Declarator &D) {
74 for (const AttributeList *AL = D.getAttributes(); AL; AL = AL->getNext())
75 if (AL->getKind() == AttributeList::AT_deprecated)
76 return true;
77 return false;
78}
79
Douglas Gregor930d8b52009-01-30 22:09:00 +000080/// \brief Convert the specified declspec to the appropriate type
81/// object.
Chris Lattner5db2bb12009-10-25 18:21:37 +000082/// \param D the declarator containing the declaration specifier.
Chris Lattner5153ee62009-04-25 08:47:54 +000083/// \returns The type described by the declaration specifiers. This function
84/// never returns null.
Sebastian Redl8ce35b02009-10-25 21:45:37 +000085static QualType ConvertDeclSpecToType(Declarator &TheDeclarator, Sema &TheSema){
Reid Spencer5f016e22007-07-11 17:01:13 +000086 // FIXME: Should move the logic from DeclSpec::Finish to here for validity
87 // checking.
Chris Lattner5db2bb12009-10-25 18:21:37 +000088 const DeclSpec &DS = TheDeclarator.getDeclSpec();
89 SourceLocation DeclLoc = TheDeclarator.getIdentifierLoc();
90 if (DeclLoc.isInvalid())
91 DeclLoc = DS.getSourceRange().getBegin();
Chris Lattner1564e392009-10-25 18:07:27 +000092
93 ASTContext &Context = TheSema.Context;
Mike Stump1eb44332009-09-09 15:08:12 +000094
Chris Lattner5db2bb12009-10-25 18:21:37 +000095 QualType Result;
Reid Spencer5f016e22007-07-11 17:01:13 +000096 switch (DS.getTypeSpecType()) {
Chris Lattner96b77fc2008-04-02 06:50:17 +000097 case DeclSpec::TST_void:
98 Result = Context.VoidTy;
99 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000100 case DeclSpec::TST_char:
101 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000102 Result = Context.CharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000103 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000104 Result = Context.SignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000105 else {
106 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
107 "Unknown TSS value");
Chris Lattnerfab5b452008-02-20 23:53:49 +0000108 Result = Context.UnsignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000109 }
Chris Lattner958858e2008-02-20 21:40:32 +0000110 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000111 case DeclSpec::TST_wchar:
112 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
113 Result = Context.WCharTy;
114 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
Chris Lattner1564e392009-10-25 18:07:27 +0000115 TheSema.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000116 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000117 Result = Context.getSignedWCharType();
118 } else {
119 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
120 "Unknown TSS value");
Chris Lattner1564e392009-10-25 18:07:27 +0000121 TheSema.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000122 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000123 Result = Context.getUnsignedWCharType();
124 }
125 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000126 case DeclSpec::TST_char16:
127 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
128 "Unknown TSS value");
129 Result = Context.Char16Ty;
130 break;
131 case DeclSpec::TST_char32:
132 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
133 "Unknown TSS value");
134 Result = Context.Char32Ty;
135 break;
Chris Lattnerd658b562008-04-05 06:32:51 +0000136 case DeclSpec::TST_unspecified:
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000137 // "<proto1,proto2>" is an objc qualified ID with a missing id.
Chris Lattner097e9162008-10-20 02:01:50 +0000138 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000139 Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy,
Steve Naroff14108da2009-07-10 23:34:53 +0000140 (ObjCProtocolDecl**)PQ,
Steve Naroff683087f2009-06-29 16:22:52 +0000141 DS.getNumProtocolQualifiers());
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000142 break;
143 }
Chris Lattner5db2bb12009-10-25 18:21:37 +0000144
145 // If this is a missing declspec in a block literal return context, then it
146 // is inferred from the return statements inside the block.
Sebastian Redl8ce35b02009-10-25 21:45:37 +0000147 if (isOmittedBlockReturnType(TheDeclarator)) {
Chris Lattner5db2bb12009-10-25 18:21:37 +0000148 Result = Context.DependentTy;
149 break;
150 }
Mike Stump1eb44332009-09-09 15:08:12 +0000151
Chris Lattnerd658b562008-04-05 06:32:51 +0000152 // Unspecified typespec defaults to int in C90. However, the C90 grammar
153 // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
154 // type-qualifier, or storage-class-specifier. If not, emit an extwarn.
155 // Note that the one exception to this is function definitions, which are
156 // allowed to be completely missing a declspec. This is handled in the
157 // parser already though by it pretending to have seen an 'int' in this
158 // case.
Chris Lattner1564e392009-10-25 18:07:27 +0000159 if (TheSema.getLangOptions().ImplicitInt) {
Chris Lattner35d276f2009-02-27 18:53:28 +0000160 // In C89 mode, we only warn if there is a completely missing declspec
161 // when one is not allowed.
Chris Lattner3f84ad22009-04-22 05:27:59 +0000162 if (DS.isEmpty()) {
Chris Lattner1564e392009-10-25 18:07:27 +0000163 TheSema.Diag(DeclLoc, diag::ext_missing_declspec)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000164 << DS.getSourceRange()
Chris Lattner173144a2009-02-27 22:31:56 +0000165 << CodeModificationHint::CreateInsertion(DS.getSourceRange().getBegin(),
166 "int");
Chris Lattner3f84ad22009-04-22 05:27:59 +0000167 }
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000168 } else if (!DS.hasTypeSpecifier()) {
Chris Lattnerd658b562008-04-05 06:32:51 +0000169 // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says:
170 // "At least one type specifier shall be given in the declaration
171 // specifiers in each declaration, and in the specifier-qualifier list in
172 // each struct declaration and type name."
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000173 // FIXME: Does Microsoft really have the implicit int extension in C++?
Chris Lattner1564e392009-10-25 18:07:27 +0000174 if (TheSema.getLangOptions().CPlusPlus &&
175 !TheSema.getLangOptions().Microsoft) {
176 TheSema.Diag(DeclLoc, diag::err_missing_type_specifier)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000177 << DS.getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Chris Lattnerb78d8332009-06-26 04:45:06 +0000179 // When this occurs in C++ code, often something is very broken with the
180 // value being declared, poison it as invalid so we don't get chains of
181 // errors.
Chris Lattner5db2bb12009-10-25 18:21:37 +0000182 TheDeclarator.setInvalidType(true);
Chris Lattnerb78d8332009-06-26 04:45:06 +0000183 } else {
Chris Lattner1564e392009-10-25 18:07:27 +0000184 TheSema.Diag(DeclLoc, diag::ext_missing_type_specifier)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000185 << DS.getSourceRange();
Chris Lattnerb78d8332009-06-26 04:45:06 +0000186 }
Chris Lattnerd658b562008-04-05 06:32:51 +0000187 }
Mike Stump1eb44332009-09-09 15:08:12 +0000188
189 // FALL THROUGH.
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000190 case DeclSpec::TST_int: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
192 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000193 case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
194 case DeclSpec::TSW_short: Result = Context.ShortTy; break;
195 case DeclSpec::TSW_long: Result = Context.LongTy; break;
Chris Lattner311157f2009-10-25 18:25:04 +0000196 case DeclSpec::TSW_longlong:
197 Result = Context.LongLongTy;
198
199 // long long is a C99 feature.
200 if (!TheSema.getLangOptions().C99 &&
201 !TheSema.getLangOptions().CPlusPlus0x)
202 TheSema.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong);
203 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000204 }
205 } else {
206 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000207 case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
208 case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break;
209 case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break;
Chris Lattner311157f2009-10-25 18:25:04 +0000210 case DeclSpec::TSW_longlong:
211 Result = Context.UnsignedLongLongTy;
212
213 // long long is a C99 feature.
214 if (!TheSema.getLangOptions().C99 &&
215 !TheSema.getLangOptions().CPlusPlus0x)
216 TheSema.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong);
217 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000218 }
219 }
Chris Lattner958858e2008-02-20 21:40:32 +0000220 break;
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000221 }
Chris Lattnerfab5b452008-02-20 23:53:49 +0000222 case DeclSpec::TST_float: Result = Context.FloatTy; break;
Chris Lattner958858e2008-02-20 21:40:32 +0000223 case DeclSpec::TST_double:
224 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000225 Result = Context.LongDoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000226 else
Chris Lattnerfab5b452008-02-20 23:53:49 +0000227 Result = Context.DoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000228 break;
Chris Lattnerfab5b452008-02-20 23:53:49 +0000229 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
Reid Spencer5f016e22007-07-11 17:01:13 +0000230 case DeclSpec::TST_decimal32: // _Decimal32
231 case DeclSpec::TST_decimal64: // _Decimal64
232 case DeclSpec::TST_decimal128: // _Decimal128
Chris Lattner1564e392009-10-25 18:07:27 +0000233 TheSema.Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported);
Chris Lattner8f12f652009-05-13 05:02:08 +0000234 Result = Context.IntTy;
Chris Lattner5db2bb12009-10-25 18:21:37 +0000235 TheDeclarator.setInvalidType(true);
Chris Lattner8f12f652009-05-13 05:02:08 +0000236 break;
Chris Lattner99dc9142008-04-13 18:59:07 +0000237 case DeclSpec::TST_class:
Reid Spencer5f016e22007-07-11 17:01:13 +0000238 case DeclSpec::TST_enum:
239 case DeclSpec::TST_union:
240 case DeclSpec::TST_struct: {
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000241 TypeDecl *D = cast_or_null<TypeDecl>(static_cast<Decl *>(DS.getTypeRep()));
John McCall6e247262009-10-10 05:48:19 +0000242 if (!D) {
243 // This can happen in C++ with ambiguous lookups.
244 Result = Context.IntTy;
Chris Lattner5db2bb12009-10-25 18:21:37 +0000245 TheDeclarator.setInvalidType(true);
John McCall6e247262009-10-10 05:48:19 +0000246 break;
247 }
248
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000249 // If the type is deprecated or unavailable, diagnose it.
Chris Lattner52338262009-10-25 22:31:57 +0000250 TheSema.DiagnoseUseOfDecl(D, DS.getTypeSpecTypeLoc(),
251 isDeclaratorDeprecated(TheDeclarator));
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000252
Reid Spencer5f016e22007-07-11 17:01:13 +0000253 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000254 DS.getTypeSpecSign() == 0 && "No qualifiers on tag names!");
255
Reid Spencer5f016e22007-07-11 17:01:13 +0000256 // TypeQuals handled by caller.
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000257 Result = Context.getTypeDeclType(D);
John McCall2191b202009-09-05 06:31:47 +0000258
259 // In C++, make an ElaboratedType.
Chris Lattner1564e392009-10-25 18:07:27 +0000260 if (TheSema.getLangOptions().CPlusPlus) {
John McCall2191b202009-09-05 06:31:47 +0000261 TagDecl::TagKind Tag
262 = TagDecl::getTagKindForTypeSpec(DS.getTypeSpecType());
263 Result = Context.getElaboratedType(Result, Tag);
264 }
Mike Stump1eb44332009-09-09 15:08:12 +0000265
Chris Lattner5153ee62009-04-25 08:47:54 +0000266 if (D->isInvalidDecl())
Chris Lattner5db2bb12009-10-25 18:21:37 +0000267 TheDeclarator.setInvalidType(true);
Chris Lattner958858e2008-02-20 21:40:32 +0000268 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000269 }
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000270 case DeclSpec::TST_typename: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000271 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
272 DS.getTypeSpecSign() == 0 &&
273 "Can't handle qualifiers on typedef names yet!");
Chris Lattner1564e392009-10-25 18:07:27 +0000274 Result = TheSema.GetTypeFromParser(DS.getTypeRep());
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000275
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000276 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000277 if (const ObjCInterfaceType *
278 Interface = Result->getAs<ObjCInterfaceType>()) {
Steve Naroff67ef8ea2009-07-20 17:56:53 +0000279 // It would be nice if protocol qualifiers were only stored with the
280 // ObjCObjectPointerType. Unfortunately, this isn't possible due
281 // to the following typedef idiom (which is uncommon, but allowed):
Mike Stump1eb44332009-09-09 15:08:12 +0000282 //
Steve Naroff67ef8ea2009-07-20 17:56:53 +0000283 // typedef Foo<P> T;
284 // static void func() {
285 // Foo<P> *yy;
286 // T *zz;
287 // }
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000288 Result = Context.getObjCInterfaceType(Interface->getDecl(),
289 (ObjCProtocolDecl**)PQ,
290 DS.getNumProtocolQualifiers());
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000291 } else if (Result->isObjCIdType())
Chris Lattnerae4da612008-07-26 01:53:50 +0000292 // id<protocol-list>
Mike Stump1eb44332009-09-09 15:08:12 +0000293 Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy,
Steve Naroff14108da2009-07-10 23:34:53 +0000294 (ObjCProtocolDecl**)PQ, DS.getNumProtocolQualifiers());
295 else if (Result->isObjCClassType()) {
Steve Naroff4262a072009-02-23 18:53:24 +0000296 // Class<protocol-list>
Mike Stump1eb44332009-09-09 15:08:12 +0000297 Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinClassTy,
Steve Naroff470301b2009-07-22 16:07:01 +0000298 (ObjCProtocolDecl**)PQ, DS.getNumProtocolQualifiers());
Chris Lattner3f84ad22009-04-22 05:27:59 +0000299 } else {
Chris Lattner1564e392009-10-25 18:07:27 +0000300 TheSema.Diag(DeclLoc, diag::err_invalid_protocol_qualifiers)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000301 << DS.getSourceRange();
Chris Lattner5db2bb12009-10-25 18:21:37 +0000302 TheDeclarator.setInvalidType(true);
Chris Lattner3f84ad22009-04-22 05:27:59 +0000303 }
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000304 }
Mike Stump1eb44332009-09-09 15:08:12 +0000305
Chris Lattnereaaebc72009-04-25 08:06:05 +0000306 // If this is a reference to an invalid typedef, propagate the invalidity.
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000307 if (TypedefType *TDT = dyn_cast<TypedefType>(Result)) {
Chris Lattnereaaebc72009-04-25 08:06:05 +0000308 if (TDT->getDecl()->isInvalidDecl())
Chris Lattner5db2bb12009-10-25 18:21:37 +0000309 TheDeclarator.setInvalidType(true);
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000310
311 // If the type is deprecated or unavailable, diagnose it.
Chris Lattner52338262009-10-25 22:31:57 +0000312 TheSema.DiagnoseUseOfDecl(TDT->getDecl(), DS.getTypeSpecTypeLoc(),
313 isDeclaratorDeprecated(TheDeclarator));
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000314 } else if (ObjCInterfaceType *OIT = dyn_cast<ObjCInterfaceType>(Result)) {
315 // If the type is deprecated or unavailable, diagnose it.
Chris Lattner52338262009-10-25 22:31:57 +0000316 TheSema.DiagnoseUseOfDecl(OIT->getDecl(), DS.getTypeSpecTypeLoc(),
317 isDeclaratorDeprecated(TheDeclarator));
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000318 } else if (ObjCObjectPointerType *DPT =
319 dyn_cast<ObjCObjectPointerType>(Result)) {
320 // If the type is deprecated or unavailable, diagnose it.
321 if (ObjCInterfaceDecl *D = DPT->getInterfaceDecl())
Chris Lattner52338262009-10-25 22:31:57 +0000322 TheSema.DiagnoseUseOfDecl(D, DS.getTypeSpecTypeLoc(),
323 isDeclaratorDeprecated(TheDeclarator));
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000324 }
325
Mike Stump1eb44332009-09-09 15:08:12 +0000326
Reid Spencer5f016e22007-07-11 17:01:13 +0000327 // TypeQuals handled by caller.
Chris Lattner958858e2008-02-20 21:40:32 +0000328 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000329 }
Chris Lattner958858e2008-02-20 21:40:32 +0000330 case DeclSpec::TST_typeofType:
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000331 // FIXME: Preserve type source info.
Chris Lattner1564e392009-10-25 18:07:27 +0000332 Result = TheSema.GetTypeFromParser(DS.getTypeRep());
Chris Lattner958858e2008-02-20 21:40:32 +0000333 assert(!Result.isNull() && "Didn't get a type for typeof?");
Steve Naroffd1861fd2007-07-31 12:34:36 +0000334 // TypeQuals handled by caller.
Chris Lattnerfab5b452008-02-20 23:53:49 +0000335 Result = Context.getTypeOfType(Result);
Chris Lattner958858e2008-02-20 21:40:32 +0000336 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000337 case DeclSpec::TST_typeofExpr: {
338 Expr *E = static_cast<Expr *>(DS.getTypeRep());
339 assert(E && "Didn't get an expression for typeof?");
340 // TypeQuals handled by caller.
Douglas Gregor72564e72009-02-26 23:50:07 +0000341 Result = Context.getTypeOfExprType(E);
Chris Lattner958858e2008-02-20 21:40:32 +0000342 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000343 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000344 case DeclSpec::TST_decltype: {
345 Expr *E = static_cast<Expr *>(DS.getTypeRep());
346 assert(E && "Didn't get an expression for decltype?");
347 // TypeQuals handled by caller.
Chris Lattner1564e392009-10-25 18:07:27 +0000348 Result = TheSema.BuildDecltypeType(E);
Anders Carlssonaf017e62009-06-29 22:58:55 +0000349 if (Result.isNull()) {
350 Result = Context.IntTy;
Chris Lattner5db2bb12009-10-25 18:21:37 +0000351 TheDeclarator.setInvalidType(true);
Anders Carlssonaf017e62009-06-29 22:58:55 +0000352 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000353 break;
354 }
Anders Carlssone89d1592009-06-26 18:41:36 +0000355 case DeclSpec::TST_auto: {
356 // TypeQuals handled by caller.
357 Result = Context.UndeducedAutoTy;
358 break;
359 }
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Douglas Gregor809070a2009-02-18 17:45:20 +0000361 case DeclSpec::TST_error:
Chris Lattner5153ee62009-04-25 08:47:54 +0000362 Result = Context.IntTy;
Chris Lattner5db2bb12009-10-25 18:21:37 +0000363 TheDeclarator.setInvalidType(true);
Chris Lattner5153ee62009-04-25 08:47:54 +0000364 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000365 }
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Chris Lattner958858e2008-02-20 21:40:32 +0000367 // Handle complex types.
Douglas Gregorf244cd72009-02-14 21:06:05 +0000368 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
Chris Lattner1564e392009-10-25 18:07:27 +0000369 if (TheSema.getLangOptions().Freestanding)
370 TheSema.Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
Chris Lattnerfab5b452008-02-20 23:53:49 +0000371 Result = Context.getComplexType(Result);
Douglas Gregorf244cd72009-02-14 21:06:05 +0000372 }
Mike Stump1eb44332009-09-09 15:08:12 +0000373
Chris Lattner958858e2008-02-20 21:40:32 +0000374 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
375 "FIXME: imaginary types not supported yet!");
Mike Stump1eb44332009-09-09 15:08:12 +0000376
Chris Lattner38d8b982008-02-20 22:04:11 +0000377 // See if there are any attributes on the declspec that apply to the type (as
378 // opposed to the decl).
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000379 if (const AttributeList *AL = DS.getAttributes())
Chris Lattner1564e392009-10-25 18:07:27 +0000380 TheSema.ProcessTypeAttributeList(Result, AL);
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Chris Lattner96b77fc2008-04-02 06:50:17 +0000382 // Apply const/volatile/restrict qualifiers to T.
383 if (unsigned TypeQuals = DS.getTypeQualifiers()) {
384
385 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
386 // or incomplete types shall not be restrict-qualified." C++ also allows
387 // restrict-qualified references.
John McCall0953e762009-09-24 19:53:00 +0000388 if (TypeQuals & DeclSpec::TQ_restrict) {
Daniel Dunbarbb710012009-02-26 19:13:44 +0000389 if (Result->isPointerType() || Result->isReferenceType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000390 QualType EltTy = Result->isPointerType() ?
Ted Kremenek6217b802009-07-29 21:53:49 +0000391 Result->getAs<PointerType>()->getPointeeType() :
392 Result->getAs<ReferenceType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Douglas Gregorbad0e652009-03-24 20:32:41 +0000394 // If we have a pointer or reference, the pointee must have an object
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000395 // incomplete type.
396 if (!EltTy->isIncompleteOrObjectType()) {
Chris Lattner1564e392009-10-25 18:07:27 +0000397 TheSema.Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000398 diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattnerd1625842008-11-24 06:25:27 +0000399 << EltTy << DS.getSourceRange();
John McCall0953e762009-09-24 19:53:00 +0000400 TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000401 }
402 } else {
Chris Lattner1564e392009-10-25 18:07:27 +0000403 TheSema.Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000404 diag::err_typecheck_invalid_restrict_not_pointer)
Chris Lattnerd1625842008-11-24 06:25:27 +0000405 << Result << DS.getSourceRange();
John McCall0953e762009-09-24 19:53:00 +0000406 TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
Chris Lattner96b77fc2008-04-02 06:50:17 +0000407 }
Chris Lattner96b77fc2008-04-02 06:50:17 +0000408 }
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Chris Lattner96b77fc2008-04-02 06:50:17 +0000410 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
411 // of a function type includes any type qualifiers, the behavior is
412 // undefined."
413 if (Result->isFunctionType() && TypeQuals) {
414 // Get some location to point at, either the C or V location.
415 SourceLocation Loc;
John McCall0953e762009-09-24 19:53:00 +0000416 if (TypeQuals & DeclSpec::TQ_const)
Chris Lattner96b77fc2008-04-02 06:50:17 +0000417 Loc = DS.getConstSpecLoc();
John McCall0953e762009-09-24 19:53:00 +0000418 else if (TypeQuals & DeclSpec::TQ_volatile)
Chris Lattner96b77fc2008-04-02 06:50:17 +0000419 Loc = DS.getVolatileSpecLoc();
John McCall0953e762009-09-24 19:53:00 +0000420 else {
421 assert((TypeQuals & DeclSpec::TQ_restrict) &&
422 "Has CVR quals but not C, V, or R?");
423 Loc = DS.getRestrictSpecLoc();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000424 }
Chris Lattner1564e392009-10-25 18:07:27 +0000425 TheSema.Diag(Loc, diag::warn_typecheck_function_qualifiers)
Chris Lattnerd1625842008-11-24 06:25:27 +0000426 << Result << DS.getSourceRange();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000427 }
Mike Stump1eb44332009-09-09 15:08:12 +0000428
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000429 // C++ [dcl.ref]p1:
430 // Cv-qualified references are ill-formed except when the
431 // cv-qualifiers are introduced through the use of a typedef
432 // (7.1.3) or of a template type argument (14.3), in which
433 // case the cv-qualifiers are ignored.
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000434 // FIXME: Shouldn't we be checking SCS_typedef here?
435 if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000436 TypeQuals && Result->isReferenceType()) {
John McCall0953e762009-09-24 19:53:00 +0000437 TypeQuals &= ~DeclSpec::TQ_const;
438 TypeQuals &= ~DeclSpec::TQ_volatile;
Mike Stump1eb44332009-09-09 15:08:12 +0000439 }
440
John McCall0953e762009-09-24 19:53:00 +0000441 Qualifiers Quals = Qualifiers::fromCVRMask(TypeQuals);
442 Result = Context.getQualifiedType(Result, Quals);
Chris Lattner96b77fc2008-04-02 06:50:17 +0000443 }
John McCall0953e762009-09-24 19:53:00 +0000444
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000445 return Result;
446}
447
Douglas Gregorcd281c32009-02-28 00:25:32 +0000448static std::string getPrintableNameForEntity(DeclarationName Entity) {
449 if (Entity)
450 return Entity.getAsString();
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Douglas Gregorcd281c32009-02-28 00:25:32 +0000452 return "type name";
453}
454
455/// \brief Build a pointer type.
456///
457/// \param T The type to which we'll be building a pointer.
458///
459/// \param Quals The cvr-qualifiers to be applied to the pointer type.
460///
461/// \param Loc The location of the entity whose type involves this
462/// pointer type or, if there is no such entity, the location of the
463/// type that will have pointer type.
464///
465/// \param Entity The name of the entity that involves the pointer
466/// type, if known.
467///
468/// \returns A suitable pointer type, if there are no
469/// errors. Otherwise, returns a NULL type.
Mike Stump1eb44332009-09-09 15:08:12 +0000470QualType Sema::BuildPointerType(QualType T, unsigned Quals,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000471 SourceLocation Loc, DeclarationName Entity) {
472 if (T->isReferenceType()) {
473 // C++ 8.3.2p4: There shall be no ... pointers to references ...
474 Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
475 << getPrintableNameForEntity(Entity);
476 return QualType();
477 }
478
John McCall0953e762009-09-24 19:53:00 +0000479 Qualifiers Qs = Qualifiers::fromCVRMask(Quals);
480
Douglas Gregorcd281c32009-02-28 00:25:32 +0000481 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
482 // object or incomplete types shall not be restrict-qualified."
John McCall0953e762009-09-24 19:53:00 +0000483 if (Qs.hasRestrict() && !T->isIncompleteOrObjectType()) {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000484 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
485 << T;
John McCall0953e762009-09-24 19:53:00 +0000486 Qs.removeRestrict();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000487 }
488
489 // Build the pointer type.
John McCall0953e762009-09-24 19:53:00 +0000490 return Context.getQualifiedType(Context.getPointerType(T), Qs);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000491}
492
493/// \brief Build a reference type.
494///
495/// \param T The type to which we'll be building a reference.
496///
John McCall0953e762009-09-24 19:53:00 +0000497/// \param CVR The cvr-qualifiers to be applied to the reference type.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000498///
499/// \param Loc The location of the entity whose type involves this
500/// reference type or, if there is no such entity, the location of the
501/// type that will have reference type.
502///
503/// \param Entity The name of the entity that involves the reference
504/// type, if known.
505///
506/// \returns A suitable reference type, if there are no
507/// errors. Otherwise, returns a NULL type.
John McCall54e14c42009-10-22 22:37:11 +0000508QualType Sema::BuildReferenceType(QualType T, bool SpelledAsLValue,
509 unsigned CVR, SourceLocation Loc,
510 DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000511 Qualifiers Quals = Qualifiers::fromCVRMask(CVR);
John McCall54e14c42009-10-22 22:37:11 +0000512
513 bool LValueRef = SpelledAsLValue || T->getAs<LValueReferenceType>();
514
515 // C++0x [dcl.typedef]p9: If a typedef TD names a type that is a
516 // reference to a type T, and attempt to create the type "lvalue
517 // reference to cv TD" creates the type "lvalue reference to T".
518 // We use the qualifiers (restrict or none) of the original reference,
519 // not the new ones. This is consistent with GCC.
520
521 // C++ [dcl.ref]p4: There shall be no references to references.
522 //
523 // According to C++ DR 106, references to references are only
524 // diagnosed when they are written directly (e.g., "int & &"),
525 // but not when they happen via a typedef:
526 //
527 // typedef int& intref;
528 // typedef intref& intref2;
529 //
530 // Parser::ParseDeclaratorInternal diagnoses the case where
531 // references are written directly; here, we handle the
532 // collapsing of references-to-references as described in C++
533 // DR 106 and amended by C++ DR 540.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000534
535 // C++ [dcl.ref]p1:
Eli Friedman33a31382009-08-05 19:21:58 +0000536 // A declarator that specifies the type "reference to cv void"
Douglas Gregorcd281c32009-02-28 00:25:32 +0000537 // is ill-formed.
538 if (T->isVoidType()) {
539 Diag(Loc, diag::err_reference_to_void);
540 return QualType();
541 }
542
543 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
544 // object or incomplete types shall not be restrict-qualified."
John McCall0953e762009-09-24 19:53:00 +0000545 if (Quals.hasRestrict() && !T->isIncompleteOrObjectType()) {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000546 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
547 << T;
John McCall0953e762009-09-24 19:53:00 +0000548 Quals.removeRestrict();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000549 }
550
551 // C++ [dcl.ref]p1:
552 // [...] Cv-qualified references are ill-formed except when the
553 // cv-qualifiers are introduced through the use of a typedef
554 // (7.1.3) or of a template type argument (14.3), in which case
555 // the cv-qualifiers are ignored.
556 //
557 // We diagnose extraneous cv-qualifiers for the non-typedef,
558 // non-template type argument case within the parser. Here, we just
559 // ignore any extraneous cv-qualifiers.
John McCall0953e762009-09-24 19:53:00 +0000560 Quals.removeConst();
561 Quals.removeVolatile();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000562
563 // Handle restrict on references.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000564 if (LValueRef)
John McCall54e14c42009-10-22 22:37:11 +0000565 return Context.getQualifiedType(
566 Context.getLValueReferenceType(T, SpelledAsLValue), Quals);
John McCall0953e762009-09-24 19:53:00 +0000567 return Context.getQualifiedType(Context.getRValueReferenceType(T), Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000568}
569
570/// \brief Build an array type.
571///
572/// \param T The type of each element in the array.
573///
574/// \param ASM C99 array size modifier (e.g., '*', 'static').
Mike Stump1eb44332009-09-09 15:08:12 +0000575///
576/// \param ArraySize Expression describing the size of the array.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000577///
578/// \param Quals The cvr-qualifiers to be applied to the array's
579/// element type.
580///
581/// \param Loc The location of the entity whose type involves this
582/// array type or, if there is no such entity, the location of the
583/// type that will have array type.
584///
585/// \param Entity The name of the entity that involves the array
586/// type, if known.
587///
588/// \returns A suitable array type, if there are no errors. Otherwise,
589/// returns a NULL type.
590QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
591 Expr *ArraySize, unsigned Quals,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000592 SourceRange Brackets, DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000593
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000594 SourceLocation Loc = Brackets.getBegin();
Mike Stump1eb44332009-09-09 15:08:12 +0000595 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000596 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
Mike Stump1eb44332009-09-09 15:08:12 +0000597 if (RequireCompleteType(Loc, T,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000598 diag::err_illegal_decl_array_incomplete_type))
599 return QualType();
600
601 if (T->isFunctionType()) {
602 Diag(Loc, diag::err_illegal_decl_array_of_functions)
603 << getPrintableNameForEntity(Entity);
604 return QualType();
605 }
Mike Stump1eb44332009-09-09 15:08:12 +0000606
Douglas Gregorcd281c32009-02-28 00:25:32 +0000607 // C++ 8.3.2p4: There shall be no ... arrays of references ...
608 if (T->isReferenceType()) {
609 Diag(Loc, diag::err_illegal_decl_array_of_references)
610 << getPrintableNameForEntity(Entity);
611 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000612 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000613
Anders Carlssone7cf07d2009-06-26 19:33:28 +0000614 if (Context.getCanonicalType(T) == Context.UndeducedAutoTy) {
Mike Stump1eb44332009-09-09 15:08:12 +0000615 Diag(Loc, diag::err_illegal_decl_array_of_auto)
Anders Carlssone7cf07d2009-06-26 19:33:28 +0000616 << getPrintableNameForEntity(Entity);
617 return QualType();
618 }
Mike Stump1eb44332009-09-09 15:08:12 +0000619
Ted Kremenek6217b802009-07-29 21:53:49 +0000620 if (const RecordType *EltTy = T->getAs<RecordType>()) {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000621 // If the element type is a struct or union that contains a variadic
622 // array, accept it as a GNU extension: C99 6.7.2.1p2.
623 if (EltTy->getDecl()->hasFlexibleArrayMember())
624 Diag(Loc, diag::ext_flexible_array_in_array) << T;
625 } else if (T->isObjCInterfaceType()) {
Chris Lattnerc7c11b12009-04-27 01:55:56 +0000626 Diag(Loc, diag::err_objc_array_of_interfaces) << T;
627 return QualType();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000628 }
Mike Stump1eb44332009-09-09 15:08:12 +0000629
Douglas Gregorcd281c32009-02-28 00:25:32 +0000630 // C99 6.7.5.2p1: The size expression shall have integer type.
631 if (ArraySize && !ArraySize->isTypeDependent() &&
632 !ArraySize->getType()->isIntegerType()) {
633 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
634 << ArraySize->getType() << ArraySize->getSourceRange();
635 ArraySize->Destroy(Context);
636 return QualType();
637 }
638 llvm::APSInt ConstVal(32);
639 if (!ArraySize) {
Eli Friedmanf91f5c82009-04-26 21:57:51 +0000640 if (ASM == ArrayType::Star)
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000641 T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets);
Eli Friedmanf91f5c82009-04-26 21:57:51 +0000642 else
643 T = Context.getIncompleteArrayType(T, ASM, Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000644 } else if (ArraySize->isValueDependent()) {
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000645 T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000646 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
647 (!T->isDependentType() && !T->isConstantSizeType())) {
648 // Per C99, a variable array is an array with either a non-constant
649 // size or an element type that has a non-constant-size
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000650 T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000651 } else {
652 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
653 // have a value greater than zero.
654 if (ConstVal.isSigned()) {
655 if (ConstVal.isNegative()) {
656 Diag(ArraySize->getLocStart(),
657 diag::err_typecheck_negative_array_size)
658 << ArraySize->getSourceRange();
659 return QualType();
660 } else if (ConstVal == 0) {
661 // GCC accepts zero sized static arrays.
662 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size)
663 << ArraySize->getSourceRange();
664 }
Mike Stump1eb44332009-09-09 15:08:12 +0000665 }
John McCall46a617a2009-10-16 00:14:28 +0000666 T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000667 }
668 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
669 if (!getLangOptions().C99) {
Mike Stump1eb44332009-09-09 15:08:12 +0000670 if (ArraySize && !ArraySize->isTypeDependent() &&
671 !ArraySize->isValueDependent() &&
Douglas Gregorcd281c32009-02-28 00:25:32 +0000672 !ArraySize->isIntegerConstantExpr(Context))
Douglas Gregor043cad22009-09-11 00:18:58 +0000673 Diag(Loc, getLangOptions().CPlusPlus? diag::err_vla_cxx : diag::ext_vla);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000674 else if (ASM != ArrayType::Normal || Quals != 0)
Douglas Gregor043cad22009-09-11 00:18:58 +0000675 Diag(Loc,
676 getLangOptions().CPlusPlus? diag::err_c99_array_usage_cxx
677 : diag::ext_c99_array_usage);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000678 }
679
680 return T;
681}
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000682
683/// \brief Build an ext-vector type.
684///
685/// Run the required checks for the extended vector type.
Mike Stump1eb44332009-09-09 15:08:12 +0000686QualType Sema::BuildExtVectorType(QualType T, ExprArg ArraySize,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000687 SourceLocation AttrLoc) {
688
689 Expr *Arg = (Expr *)ArraySize.get();
690
691 // unlike gcc's vector_size attribute, we do not allow vectors to be defined
692 // in conjunction with complex types (pointers, arrays, functions, etc.).
Mike Stump1eb44332009-09-09 15:08:12 +0000693 if (!T->isDependentType() &&
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000694 !T->isIntegerType() && !T->isRealFloatingType()) {
695 Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T;
696 return QualType();
697 }
698
699 if (!Arg->isTypeDependent() && !Arg->isValueDependent()) {
700 llvm::APSInt vecSize(32);
701 if (!Arg->isIntegerConstantExpr(vecSize, Context)) {
702 Diag(AttrLoc, diag::err_attribute_argument_not_int)
703 << "ext_vector_type" << Arg->getSourceRange();
704 return QualType();
705 }
Mike Stump1eb44332009-09-09 15:08:12 +0000706
707 // unlike gcc's vector_size attribute, the size is specified as the
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000708 // number of elements, not the number of bytes.
Mike Stump1eb44332009-09-09 15:08:12 +0000709 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
710
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000711 if (vectorSize == 0) {
712 Diag(AttrLoc, diag::err_attribute_zero_size)
713 << Arg->getSourceRange();
714 return QualType();
715 }
Mike Stump1eb44332009-09-09 15:08:12 +0000716
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000717 if (!T->isDependentType())
718 return Context.getExtVectorType(T, vectorSize);
Mike Stump1eb44332009-09-09 15:08:12 +0000719 }
720
721 return Context.getDependentSizedExtVectorType(T, ArraySize.takeAs<Expr>(),
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000722 AttrLoc);
723}
Mike Stump1eb44332009-09-09 15:08:12 +0000724
Douglas Gregor724651c2009-02-28 01:04:19 +0000725/// \brief Build a function type.
726///
727/// This routine checks the function type according to C++ rules and
728/// under the assumption that the result type and parameter types have
729/// just been instantiated from a template. It therefore duplicates
Douglas Gregor2943aed2009-03-03 04:44:36 +0000730/// some of the behavior of GetTypeForDeclarator, but in a much
Douglas Gregor724651c2009-02-28 01:04:19 +0000731/// simpler form that is only suitable for this narrow use case.
732///
733/// \param T The return type of the function.
734///
735/// \param ParamTypes The parameter types of the function. This array
736/// will be modified to account for adjustments to the types of the
737/// function parameters.
738///
739/// \param NumParamTypes The number of parameter types in ParamTypes.
740///
741/// \param Variadic Whether this is a variadic function type.
742///
743/// \param Quals The cvr-qualifiers to be applied to the function type.
744///
745/// \param Loc The location of the entity whose type involves this
746/// function type or, if there is no such entity, the location of the
747/// type that will have function type.
748///
749/// \param Entity The name of the entity that involves the function
750/// type, if known.
751///
752/// \returns A suitable function type, if there are no
753/// errors. Otherwise, returns a NULL type.
754QualType Sema::BuildFunctionType(QualType T,
Mike Stump1eb44332009-09-09 15:08:12 +0000755 QualType *ParamTypes,
Douglas Gregor724651c2009-02-28 01:04:19 +0000756 unsigned NumParamTypes,
757 bool Variadic, unsigned Quals,
758 SourceLocation Loc, DeclarationName Entity) {
759 if (T->isArrayType() || T->isFunctionType()) {
760 Diag(Loc, diag::err_func_returning_array_function) << T;
761 return QualType();
762 }
Mike Stump1eb44332009-09-09 15:08:12 +0000763
Douglas Gregor724651c2009-02-28 01:04:19 +0000764 bool Invalid = false;
765 for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) {
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000766 QualType ParamType = adjustParameterType(ParamTypes[Idx]);
767 if (ParamType->isVoidType()) {
Douglas Gregor724651c2009-02-28 01:04:19 +0000768 Diag(Loc, diag::err_param_with_void_type);
769 Invalid = true;
770 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000771
John McCall54e14c42009-10-22 22:37:11 +0000772 ParamTypes[Idx] = ParamType;
Douglas Gregor724651c2009-02-28 01:04:19 +0000773 }
774
775 if (Invalid)
776 return QualType();
777
Mike Stump1eb44332009-09-09 15:08:12 +0000778 return Context.getFunctionType(T, ParamTypes, NumParamTypes, Variadic,
Douglas Gregor724651c2009-02-28 01:04:19 +0000779 Quals);
780}
Mike Stump1eb44332009-09-09 15:08:12 +0000781
Douglas Gregor949bf692009-06-09 22:17:39 +0000782/// \brief Build a member pointer type \c T Class::*.
783///
784/// \param T the type to which the member pointer refers.
785/// \param Class the class type into which the member pointer points.
John McCall0953e762009-09-24 19:53:00 +0000786/// \param CVR Qualifiers applied to the member pointer type
Douglas Gregor949bf692009-06-09 22:17:39 +0000787/// \param Loc the location where this type begins
788/// \param Entity the name of the entity that will have this member pointer type
789///
790/// \returns a member pointer type, if successful, or a NULL type if there was
791/// an error.
Mike Stump1eb44332009-09-09 15:08:12 +0000792QualType Sema::BuildMemberPointerType(QualType T, QualType Class,
John McCall0953e762009-09-24 19:53:00 +0000793 unsigned CVR, SourceLocation Loc,
Douglas Gregor949bf692009-06-09 22:17:39 +0000794 DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000795 Qualifiers Quals = Qualifiers::fromCVRMask(CVR);
796
Douglas Gregor949bf692009-06-09 22:17:39 +0000797 // Verify that we're not building a pointer to pointer to function with
798 // exception specification.
799 if (CheckDistantExceptionSpec(T)) {
800 Diag(Loc, diag::err_distant_exception_spec);
801
802 // FIXME: If we're doing this as part of template instantiation,
803 // we should return immediately.
804
805 // Build the type anyway, but use the canonical type so that the
806 // exception specifiers are stripped off.
807 T = Context.getCanonicalType(T);
808 }
809
810 // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member
811 // with reference type, or "cv void."
812 if (T->isReferenceType()) {
Anders Carlsson8d4655d2009-06-30 00:06:57 +0000813 Diag(Loc, diag::err_illegal_decl_mempointer_to_reference)
Douglas Gregor949bf692009-06-09 22:17:39 +0000814 << (Entity? Entity.getAsString() : "type name");
815 return QualType();
816 }
817
818 if (T->isVoidType()) {
819 Diag(Loc, diag::err_illegal_decl_mempointer_to_void)
820 << (Entity? Entity.getAsString() : "type name");
821 return QualType();
822 }
823
824 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
825 // object or incomplete types shall not be restrict-qualified."
John McCall0953e762009-09-24 19:53:00 +0000826 if (Quals.hasRestrict() && !T->isIncompleteOrObjectType()) {
Douglas Gregor949bf692009-06-09 22:17:39 +0000827 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
828 << T;
829
830 // FIXME: If we're doing this as part of template instantiation,
831 // we should return immediately.
John McCall0953e762009-09-24 19:53:00 +0000832 Quals.removeRestrict();
Douglas Gregor949bf692009-06-09 22:17:39 +0000833 }
834
835 if (!Class->isDependentType() && !Class->isRecordType()) {
836 Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class;
837 return QualType();
838 }
839
John McCall0953e762009-09-24 19:53:00 +0000840 return Context.getQualifiedType(
841 Context.getMemberPointerType(T, Class.getTypePtr()), Quals);
Douglas Gregor949bf692009-06-09 22:17:39 +0000842}
Mike Stump1eb44332009-09-09 15:08:12 +0000843
Anders Carlsson9a917e42009-06-12 22:56:54 +0000844/// \brief Build a block pointer type.
845///
846/// \param T The type to which we'll be building a block pointer.
847///
John McCall0953e762009-09-24 19:53:00 +0000848/// \param CVR The cvr-qualifiers to be applied to the block pointer type.
Anders Carlsson9a917e42009-06-12 22:56:54 +0000849///
850/// \param Loc The location of the entity whose type involves this
851/// block pointer type or, if there is no such entity, the location of the
852/// type that will have block pointer type.
853///
854/// \param Entity The name of the entity that involves the block pointer
855/// type, if known.
856///
857/// \returns A suitable block pointer type, if there are no
858/// errors. Otherwise, returns a NULL type.
John McCall0953e762009-09-24 19:53:00 +0000859QualType Sema::BuildBlockPointerType(QualType T, unsigned CVR,
Mike Stump1eb44332009-09-09 15:08:12 +0000860 SourceLocation Loc,
Anders Carlsson9a917e42009-06-12 22:56:54 +0000861 DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000862 if (!T->isFunctionType()) {
Anders Carlsson9a917e42009-06-12 22:56:54 +0000863 Diag(Loc, diag::err_nonfunction_block_type);
864 return QualType();
865 }
Mike Stump1eb44332009-09-09 15:08:12 +0000866
John McCall0953e762009-09-24 19:53:00 +0000867 Qualifiers Quals = Qualifiers::fromCVRMask(CVR);
868 return Context.getQualifiedType(Context.getBlockPointerType(T), Quals);
Anders Carlsson9a917e42009-06-12 22:56:54 +0000869}
870
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000871QualType Sema::GetTypeFromParser(TypeTy *Ty, DeclaratorInfo **DInfo) {
872 QualType QT = QualType::getFromOpaquePtr(Ty);
873 DeclaratorInfo *DI = 0;
874 if (LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) {
875 QT = LIT->getType();
876 DI = LIT->getDeclaratorInfo();
877 }
Mike Stump1eb44332009-09-09 15:08:12 +0000878
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000879 if (DInfo) *DInfo = DI;
880 return QT;
881}
882
Mike Stump98eb8a72009-02-04 22:31:32 +0000883/// GetTypeForDeclarator - Convert the type for the specified
Sebastian Redl8ce35b02009-10-25 21:45:37 +0000884/// declarator to Type instances.
Douglas Gregor402abb52009-05-28 23:31:59 +0000885///
886/// If OwnedDecl is non-NULL, and this declarator's decl-specifier-seq
887/// owns the declaration of a type (e.g., the definition of a struct
888/// type), then *OwnedDecl will receive the owned declaration.
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000889QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S,
Sebastian Redl8ce35b02009-10-25 21:45:37 +0000890 DeclaratorInfo **DInfo,
Douglas Gregor402abb52009-05-28 23:31:59 +0000891 TagDecl **OwnedDecl) {
Douglas Gregor930d8b52009-01-30 22:09:00 +0000892 // Determine the type of the declarator. Not all forms of declarator
893 // have a type.
894 QualType T;
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000895
Douglas Gregor930d8b52009-01-30 22:09:00 +0000896 switch (D.getKind()) {
897 case Declarator::DK_Abstract:
898 case Declarator::DK_Normal:
Douglas Gregordb422df2009-09-25 21:45:23 +0000899 case Declarator::DK_Operator:
Chris Lattner5db2bb12009-10-25 18:21:37 +0000900 case Declarator::DK_TemplateId:
Sebastian Redl8ce35b02009-10-25 21:45:37 +0000901 T = ConvertDeclSpecToType(D, *this);
Chris Lattner5db2bb12009-10-25 18:21:37 +0000902
903 if (!D.isInvalidType() && OwnedDecl && D.getDeclSpec().isTypeSpecOwned())
904 *OwnedDecl = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep());
Douglas Gregor930d8b52009-01-30 22:09:00 +0000905 break;
906
907 case Declarator::DK_Constructor:
908 case Declarator::DK_Destructor:
909 case Declarator::DK_Conversion:
910 // Constructors and destructors don't have return types. Use
911 // "void" instead. Conversion operators will check their return
912 // types separately.
913 T = Context.VoidTy;
914 break;
915 }
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000916
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000917 if (T == Context.UndeducedAutoTy) {
918 int Error = -1;
Mike Stump1eb44332009-09-09 15:08:12 +0000919
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000920 switch (D.getContext()) {
921 case Declarator::KNRTypeListContext:
922 assert(0 && "K&R type lists aren't allowed in C++");
923 break;
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000924 case Declarator::PrototypeContext:
925 Error = 0; // Function prototype
926 break;
927 case Declarator::MemberContext:
928 switch (cast<TagDecl>(CurContext)->getTagKind()) {
929 case TagDecl::TK_enum: assert(0 && "unhandled tag kind"); break;
930 case TagDecl::TK_struct: Error = 1; /* Struct member */ break;
931 case TagDecl::TK_union: Error = 2; /* Union member */ break;
932 case TagDecl::TK_class: Error = 3; /* Class member */ break;
Mike Stump1eb44332009-09-09 15:08:12 +0000933 }
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000934 break;
935 case Declarator::CXXCatchContext:
936 Error = 4; // Exception declaration
937 break;
938 case Declarator::TemplateParamContext:
939 Error = 5; // Template parameter
940 break;
941 case Declarator::BlockLiteralContext:
942 Error = 6; // Block literal
943 break;
944 case Declarator::FileContext:
945 case Declarator::BlockContext:
946 case Declarator::ForContext:
947 case Declarator::ConditionContext:
948 case Declarator::TypeNameContext:
949 break;
950 }
951
952 if (Error != -1) {
953 Diag(D.getDeclSpec().getTypeSpecTypeLoc(), diag::err_auto_not_allowed)
954 << Error;
955 T = Context.IntTy;
956 D.setInvalidType(true);
957 }
958 }
Mike Stump1eb44332009-09-09 15:08:12 +0000959
Douglas Gregorcd281c32009-02-28 00:25:32 +0000960 // The name we're declaring, if any.
961 DeclarationName Name;
962 if (D.getIdentifier())
963 Name = D.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +0000964
Mike Stump98eb8a72009-02-04 22:31:32 +0000965 // Walk the DeclTypeInfo, building the recursive type as we go.
966 // DeclTypeInfos are ordered from the identifier out, which is
967 // opposite of what we want :).
Sebastian Redl8ce35b02009-10-25 21:45:37 +0000968 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
969 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000970 switch (DeclType.Kind) {
971 default: assert(0 && "Unknown decltype!");
Steve Naroff5618bd42008-08-27 16:04:49 +0000972 case DeclaratorChunk::BlockPointer:
Chris Lattner9af55002009-03-27 04:18:06 +0000973 // If blocks are disabled, emit an error.
974 if (!LangOpts.Blocks)
975 Diag(DeclType.Loc, diag::err_blocks_disable);
Mike Stump1eb44332009-09-09 15:08:12 +0000976
977 T = BuildBlockPointerType(T, DeclType.Cls.TypeQuals, D.getIdentifierLoc(),
Anders Carlsson9a917e42009-06-12 22:56:54 +0000978 Name);
Steve Naroff5618bd42008-08-27 16:04:49 +0000979 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000980 case DeclaratorChunk::Pointer:
Sebastian Redl6a7330c2009-05-29 15:01:05 +0000981 // Verify that we're not building a pointer to pointer to function with
982 // exception specification.
983 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
984 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
985 D.setInvalidType(true);
986 // Build the type anyway.
987 }
Steve Naroff14108da2009-07-10 23:34:53 +0000988 if (getLangOptions().ObjC1 && T->isObjCInterfaceType()) {
John McCall183700f2009-09-21 23:43:11 +0000989 const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>();
Steve Naroff14108da2009-07-10 23:34:53 +0000990 T = Context.getObjCObjectPointerType(T,
Steve Naroff67ef8ea2009-07-20 17:56:53 +0000991 (ObjCProtocolDecl **)OIT->qual_begin(),
992 OIT->getNumProtocols());
Steve Naroff14108da2009-07-10 23:34:53 +0000993 break;
994 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000995 T = BuildPointerType(T, DeclType.Ptr.TypeQuals, DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000996 break;
John McCall0953e762009-09-24 19:53:00 +0000997 case DeclaratorChunk::Reference: {
998 Qualifiers Quals;
999 if (DeclType.Ref.HasRestrict) Quals.addRestrict();
1000
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001001 // Verify that we're not building a reference to pointer to function with
1002 // exception specification.
1003 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1004 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1005 D.setInvalidType(true);
1006 // Build the type anyway.
1007 }
John McCall0953e762009-09-24 19:53:00 +00001008 T = BuildReferenceType(T, DeclType.Ref.LValueRef, Quals,
Douglas Gregorcd281c32009-02-28 00:25:32 +00001009 DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +00001010 break;
John McCall0953e762009-09-24 19:53:00 +00001011 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001012 case DeclaratorChunk::Array: {
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001013 // Verify that we're not building an array of pointers to function with
1014 // exception specification.
1015 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1016 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1017 D.setInvalidType(true);
1018 // Build the type anyway.
1019 }
Chris Lattnerfd89bc82008-04-02 01:05:10 +00001020 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner94f81fd2007-08-28 16:54:00 +00001021 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001022 ArrayType::ArraySizeModifier ASM;
1023 if (ATI.isStar)
1024 ASM = ArrayType::Star;
1025 else if (ATI.hasStatic)
1026 ASM = ArrayType::Static;
1027 else
1028 ASM = ArrayType::Normal;
Eli Friedmanf91f5c82009-04-26 21:57:51 +00001029 if (ASM == ArrayType::Star &&
1030 D.getContext() != Declarator::PrototypeContext) {
1031 // FIXME: This check isn't quite right: it allows star in prototypes
1032 // for function definitions, and disallows some edge cases detailed
1033 // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html
1034 Diag(DeclType.Loc, diag::err_array_star_outside_prototype);
1035 ASM = ArrayType::Normal;
1036 D.setInvalidType(true);
1037 }
John McCall0953e762009-09-24 19:53:00 +00001038 T = BuildArrayType(T, ASM, ArraySize,
1039 Qualifiers::fromCVRMask(ATI.TypeQuals),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001040 SourceRange(DeclType.Loc, DeclType.EndLoc), Name);
Reid Spencer5f016e22007-07-11 17:01:13 +00001041 break;
1042 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001043 case DeclaratorChunk::Function: {
Reid Spencer5f016e22007-07-11 17:01:13 +00001044 // If the function declarator has a prototype (i.e. it is not () and
1045 // does not have a K&R-style identifier list), then the arguments are part
1046 // of the type, otherwise the argument list is ().
1047 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Sebastian Redl3cc97262009-05-31 11:47:27 +00001048
Chris Lattnercd881292007-12-19 05:31:29 +00001049 // C99 6.7.5.3p1: The return type may not be a function or array type.
Chris Lattner68cfd492007-12-19 18:01:43 +00001050 if (T->isArrayType() || T->isFunctionType()) {
Chris Lattnerd1625842008-11-24 06:25:27 +00001051 Diag(DeclType.Loc, diag::err_func_returning_array_function) << T;
Chris Lattnercd881292007-12-19 05:31:29 +00001052 T = Context.IntTy;
1053 D.setInvalidType(true);
1054 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001055
Douglas Gregor402abb52009-05-28 23:31:59 +00001056 if (getLangOptions().CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) {
1057 // C++ [dcl.fct]p6:
1058 // Types shall not be defined in return or parameter types.
1059 TagDecl *Tag = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep());
1060 if (Tag->isDefinition())
1061 Diag(Tag->getLocation(), diag::err_type_defined_in_result_type)
1062 << Context.getTypeDeclType(Tag);
1063 }
1064
Sebastian Redl3cc97262009-05-31 11:47:27 +00001065 // Exception specs are not allowed in typedefs. Complain, but add it
1066 // anyway.
1067 if (FTI.hasExceptionSpec &&
1068 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
1069 Diag(FTI.getThrowLoc(), diag::err_exception_spec_in_typedef);
1070
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001071 if (FTI.NumArgs == 0) {
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001072 if (getLangOptions().CPlusPlus) {
1073 // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the
1074 // function takes no arguments.
Sebastian Redl465226e2009-05-27 22:11:52 +00001075 llvm::SmallVector<QualType, 4> Exceptions;
1076 Exceptions.reserve(FTI.NumExceptions);
Mike Stump1eb44332009-09-09 15:08:12 +00001077 for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001078 // FIXME: Preserve type source info.
1079 QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty);
Sebastian Redlef65f062009-05-29 18:02:33 +00001080 // Check that the type is valid for an exception spec, and drop it
1081 // if not.
1082 if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1083 Exceptions.push_back(ET);
1084 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001085 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, FTI.TypeQuals,
1086 FTI.hasExceptionSpec,
1087 FTI.hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00001088 Exceptions.size(), Exceptions.data());
Douglas Gregor965acbb2009-02-18 07:07:28 +00001089 } else if (FTI.isVariadic) {
1090 // We allow a zero-parameter variadic function in C if the
1091 // function is marked with the "overloadable"
1092 // attribute. Scan for this attribute now.
1093 bool Overloadable = false;
1094 for (const AttributeList *Attrs = D.getAttributes();
1095 Attrs; Attrs = Attrs->getNext()) {
1096 if (Attrs->getKind() == AttributeList::AT_overloadable) {
1097 Overloadable = true;
1098 break;
1099 }
1100 }
1101
1102 if (!Overloadable)
1103 Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
1104 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, 0);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001105 } else {
1106 // Simple void foo(), where the incoming T is the result type.
Douglas Gregor72564e72009-02-26 23:50:07 +00001107 T = Context.getFunctionNoProtoType(T);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001108 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001109 } else if (FTI.ArgInfo[0].Param == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001110 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
Mike Stump1eb44332009-09-09 15:08:12 +00001111 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
John McCall54e14c42009-10-22 22:37:11 +00001112 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001113 } else {
1114 // Otherwise, we have a function with an argument list that is
1115 // potentially variadic.
1116 llvm::SmallVector<QualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +00001117
Reid Spencer5f016e22007-07-11 17:01:13 +00001118 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001119 ParmVarDecl *Param =
1120 cast<ParmVarDecl>(FTI.ArgInfo[i].Param.getAs<Decl>());
Chris Lattner8123a952008-04-10 02:22:51 +00001121 QualType ArgTy = Param->getType();
Chris Lattner78c75fb2007-07-21 05:30:18 +00001122 assert(!ArgTy.isNull() && "Couldn't parse type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001123
1124 // Adjust the parameter type.
Douglas Gregorbeb58cb2009-03-23 23:17:00 +00001125 assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001126
Reid Spencer5f016e22007-07-11 17:01:13 +00001127 // Look for 'void'. void is allowed only as a single argument to a
1128 // function with no other parameters (C99 6.7.5.3p10). We record
Douglas Gregor72564e72009-02-26 23:50:07 +00001129 // int(void) as a FunctionProtoType with an empty argument list.
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001130 if (ArgTy->isVoidType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001131 // If this is something like 'float(int, void)', reject it. 'void'
1132 // is an incomplete type (C99 6.2.5p19) and function decls cannot
1133 // have arguments of incomplete type.
1134 if (FTI.NumArgs != 1 || FTI.isVariadic) {
1135 Diag(DeclType.Loc, diag::err_void_only_param);
Chris Lattner2ff54262007-07-21 05:18:12 +00001136 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00001137 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00001138 } else if (FTI.ArgInfo[i].Ident) {
1139 // Reject, but continue to parse 'int(void abc)'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001140 Diag(FTI.ArgInfo[i].IdentLoc,
Chris Lattner4565d4e2007-07-21 05:26:43 +00001141 diag::err_param_with_void_type);
Chris Lattner2ff54262007-07-21 05:18:12 +00001142 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00001143 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00001144 } else {
1145 // Reject, but continue to parse 'float(const void)'.
John McCall0953e762009-09-24 19:53:00 +00001146 if (ArgTy.hasQualifiers())
Chris Lattner2ff54262007-07-21 05:18:12 +00001147 Diag(DeclType.Loc, diag::err_void_param_qualified);
Mike Stump1eb44332009-09-09 15:08:12 +00001148
Chris Lattner2ff54262007-07-21 05:18:12 +00001149 // Do not add 'void' to the ArgTys list.
1150 break;
1151 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001152 } else if (!FTI.hasPrototype) {
1153 if (ArgTy->isPromotableIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00001154 ArgTy = Context.getPromotedIntegerType(ArgTy);
John McCall183700f2009-09-21 23:43:11 +00001155 } else if (const BuiltinType* BTy = ArgTy->getAs<BuiltinType>()) {
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001156 if (BTy->getKind() == BuiltinType::Float)
1157 ArgTy = Context.DoubleTy;
1158 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001159 }
Mike Stump1eb44332009-09-09 15:08:12 +00001160
John McCall54e14c42009-10-22 22:37:11 +00001161 ArgTys.push_back(ArgTy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001162 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001163
1164 llvm::SmallVector<QualType, 4> Exceptions;
1165 Exceptions.reserve(FTI.NumExceptions);
Mike Stump1eb44332009-09-09 15:08:12 +00001166 for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001167 // FIXME: Preserve type source info.
1168 QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty);
Sebastian Redlef65f062009-05-29 18:02:33 +00001169 // Check that the type is valid for an exception spec, and drop it if
1170 // not.
1171 if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1172 Exceptions.push_back(ET);
1173 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001174
Jay Foadbeaaccd2009-05-21 09:52:38 +00001175 T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(),
Sebastian Redl465226e2009-05-27 22:11:52 +00001176 FTI.isVariadic, FTI.TypeQuals,
1177 FTI.hasExceptionSpec,
1178 FTI.hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00001179 Exceptions.size(), Exceptions.data());
Reid Spencer5f016e22007-07-11 17:01:13 +00001180 }
1181 break;
1182 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001183 case DeclaratorChunk::MemberPointer:
Sebastian Redl4994d2d2009-07-04 11:39:00 +00001184 // Verify that we're not building a pointer to pointer to function with
1185 // exception specification.
1186 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1187 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1188 D.setInvalidType(true);
1189 // Build the type anyway.
1190 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001191 // The scope spec must refer to a class, or be dependent.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001192 QualType ClsType;
Douglas Gregor949bf692009-06-09 22:17:39 +00001193 if (isDependentScopeSpecifier(DeclType.Mem.Scope())) {
Mike Stump1eb44332009-09-09 15:08:12 +00001194 NestedNameSpecifier *NNS
Douglas Gregor949bf692009-06-09 22:17:39 +00001195 = (NestedNameSpecifier *)DeclType.Mem.Scope().getScopeRep();
1196 assert(NNS->getAsType() && "Nested-name-specifier must name a type");
1197 ClsType = QualType(NNS->getAsType(), 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001198 } else if (CXXRecordDecl *RD
Douglas Gregor949bf692009-06-09 22:17:39 +00001199 = dyn_cast_or_null<CXXRecordDecl>(
1200 computeDeclContext(DeclType.Mem.Scope()))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001201 ClsType = Context.getTagDeclType(RD);
1202 } else {
Douglas Gregor949bf692009-06-09 22:17:39 +00001203 Diag(DeclType.Mem.Scope().getBeginLoc(),
1204 diag::err_illegal_decl_mempointer_in_nonclass)
1205 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
1206 << DeclType.Mem.Scope().getRange();
Sebastian Redlf30208a2009-01-24 21:16:55 +00001207 D.setInvalidType(true);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001208 }
1209
Douglas Gregor949bf692009-06-09 22:17:39 +00001210 if (!ClsType.isNull())
1211 T = BuildMemberPointerType(T, ClsType, DeclType.Mem.TypeQuals,
1212 DeclType.Loc, D.getIdentifier());
1213 if (T.isNull()) {
1214 T = Context.IntTy;
Sebastian Redlf30208a2009-01-24 21:16:55 +00001215 D.setInvalidType(true);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001216 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001217 break;
1218 }
1219
Douglas Gregorcd281c32009-02-28 00:25:32 +00001220 if (T.isNull()) {
1221 D.setInvalidType(true);
1222 T = Context.IntTy;
1223 }
1224
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001225 // See if there are any attributes on this declarator chunk.
1226 if (const AttributeList *AL = DeclType.getAttrs())
1227 ProcessTypeAttributeList(T, AL);
Reid Spencer5f016e22007-07-11 17:01:13 +00001228 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001229
1230 if (getLangOptions().CPlusPlus && T->isFunctionType()) {
John McCall183700f2009-09-21 23:43:11 +00001231 const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>();
Chris Lattner778ed742009-10-25 17:36:50 +00001232 assert(FnTy && "Why oh why is there not a FunctionProtoType here?");
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001233
1234 // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type
1235 // for a nonstatic member function, the function type to which a pointer
1236 // to member refers, or the top-level function type of a function typedef
1237 // declaration.
1238 if (FnTy->getTypeQuals() != 0 &&
1239 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
Douglas Gregor584049d2008-12-15 23:53:10 +00001240 ((D.getContext() != Declarator::MemberContext &&
1241 (!D.getCXXScopeSpec().isSet() ||
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001242 !computeDeclContext(D.getCXXScopeSpec(), /*FIXME:*/true)
1243 ->isRecord())) ||
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001244 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001245 if (D.isFunctionDeclarator())
1246 Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
1247 else
1248 Diag(D.getIdentifierLoc(),
1249 diag::err_invalid_qualified_typedef_function_type_use);
1250
1251 // Strip the cv-quals from the type.
1252 T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001253 FnTy->getNumArgs(), FnTy->isVariadic(), 0);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001254 }
1255 }
Mike Stump1eb44332009-09-09 15:08:12 +00001256
Chris Lattner0bf29ad2008-06-29 00:19:33 +00001257 // If there were any type attributes applied to the decl itself (not the
1258 // type, apply the type attribute to the type!)
1259 if (const AttributeList *Attrs = D.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001260 ProcessTypeAttributeList(T, Attrs);
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001261
John McCall54e14c42009-10-22 22:37:11 +00001262 if (DInfo) {
1263 if (D.isInvalidType())
1264 *DInfo = 0;
1265 else
Sebastian Redl8ce35b02009-10-25 21:45:37 +00001266 *DInfo = GetDeclaratorInfoForDeclarator(D, T);
John McCall54e14c42009-10-22 22:37:11 +00001267 }
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001268
Reid Spencer5f016e22007-07-11 17:01:13 +00001269 return T;
1270}
1271
John McCall51bd8032009-10-18 01:05:36 +00001272namespace {
1273 class TypeSpecLocFiller : public TypeLocVisitor<TypeSpecLocFiller> {
1274 const DeclSpec &DS;
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001275
John McCall51bd8032009-10-18 01:05:36 +00001276 public:
1277 TypeSpecLocFiller(const DeclSpec &DS) : DS(DS) {}
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001278
John McCall51bd8032009-10-18 01:05:36 +00001279 void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
1280 Visit(TL.getUnqualifiedLoc());
1281 }
1282 void VisitTypedefTypeLoc(TypedefTypeLoc TL) {
1283 TL.setNameLoc(DS.getTypeSpecTypeLoc());
1284 }
1285 void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
1286 TL.setNameLoc(DS.getTypeSpecTypeLoc());
Argyrios Kyrtzidiseb667592009-09-29 19:45:22 +00001287
John McCall54e14c42009-10-22 22:37:11 +00001288 if (DS.getProtocolQualifiers()) {
1289 assert(TL.getNumProtocols() > 0);
1290 assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
1291 TL.setLAngleLoc(DS.getProtocolLAngleLoc());
1292 TL.setRAngleLoc(DS.getSourceRange().getEnd());
1293 for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i)
1294 TL.setProtocolLoc(i, DS.getProtocolLocs()[i]);
1295 } else {
1296 assert(TL.getNumProtocols() == 0);
1297 TL.setLAngleLoc(SourceLocation());
1298 TL.setRAngleLoc(SourceLocation());
1299 }
1300 }
1301 void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
1302 assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
1303
1304 TL.setStarLoc(SourceLocation());
1305
1306 if (DS.getProtocolQualifiers()) {
1307 assert(TL.getNumProtocols() > 0);
1308 assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
1309 TL.setHasProtocolsAsWritten(true);
1310 TL.setLAngleLoc(DS.getProtocolLAngleLoc());
1311 TL.setRAngleLoc(DS.getSourceRange().getEnd());
1312 for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i)
1313 TL.setProtocolLoc(i, DS.getProtocolLocs()[i]);
1314
1315 } else {
1316 assert(TL.getNumProtocols() == 0);
1317 TL.setHasProtocolsAsWritten(false);
1318 TL.setLAngleLoc(SourceLocation());
1319 TL.setRAngleLoc(SourceLocation());
1320 }
1321
1322 // This might not have been written with an inner type.
1323 if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) {
1324 TL.setHasBaseTypeAsWritten(false);
1325 TL.getBaseTypeLoc().initialize(SourceLocation());
1326 } else {
1327 TL.setHasBaseTypeAsWritten(true);
John McCall51bd8032009-10-18 01:05:36 +00001328 Visit(TL.getBaseTypeLoc());
John McCall54e14c42009-10-22 22:37:11 +00001329 }
John McCall51bd8032009-10-18 01:05:36 +00001330 }
John McCall833ca992009-10-29 08:12:44 +00001331 void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) {
1332 DeclaratorInfo *DInfo = 0;
1333 Sema::GetTypeFromParser(DS.getTypeRep(), &DInfo);
1334
1335 // If we got no declarator info from previous Sema routines,
1336 // just fill with the typespec loc.
1337 if (!DInfo) {
1338 TL.initialize(DS.getTypeSpecTypeLoc());
1339 return;
1340 }
1341
1342 TemplateSpecializationTypeLoc OldTL =
1343 cast<TemplateSpecializationTypeLoc>(DInfo->getTypeLoc());
1344 TL.copy(OldTL);
1345 }
John McCall51bd8032009-10-18 01:05:36 +00001346 void VisitTypeLoc(TypeLoc TL) {
1347 // FIXME: add other typespec types and change this to an assert.
1348 TL.initialize(DS.getTypeSpecTypeLoc());
1349 }
1350 };
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001351
John McCall51bd8032009-10-18 01:05:36 +00001352 class DeclaratorLocFiller : public TypeLocVisitor<DeclaratorLocFiller> {
1353 const DeclaratorChunk &Chunk;
1354
1355 public:
1356 DeclaratorLocFiller(const DeclaratorChunk &Chunk) : Chunk(Chunk) {}
1357
1358 void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
1359 llvm::llvm_unreachable("qualified type locs not expected here!");
1360 }
1361
1362 void VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
1363 assert(Chunk.Kind == DeclaratorChunk::BlockPointer);
1364 TL.setCaretLoc(Chunk.Loc);
1365 }
1366 void VisitPointerTypeLoc(PointerTypeLoc TL) {
1367 assert(Chunk.Kind == DeclaratorChunk::Pointer);
1368 TL.setStarLoc(Chunk.Loc);
1369 }
1370 void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
1371 assert(Chunk.Kind == DeclaratorChunk::Pointer);
1372 TL.setStarLoc(Chunk.Loc);
John McCall54e14c42009-10-22 22:37:11 +00001373 TL.setHasBaseTypeAsWritten(true);
1374 TL.setHasProtocolsAsWritten(false);
1375 TL.setLAngleLoc(SourceLocation());
1376 TL.setRAngleLoc(SourceLocation());
John McCall51bd8032009-10-18 01:05:36 +00001377 }
1378 void VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
1379 assert(Chunk.Kind == DeclaratorChunk::MemberPointer);
1380 TL.setStarLoc(Chunk.Loc);
1381 // FIXME: nested name specifier
1382 }
1383 void VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
1384 assert(Chunk.Kind == DeclaratorChunk::Reference);
John McCall54e14c42009-10-22 22:37:11 +00001385 // 'Amp' is misleading: this might have been originally
1386 /// spelled with AmpAmp.
John McCall51bd8032009-10-18 01:05:36 +00001387 TL.setAmpLoc(Chunk.Loc);
1388 }
1389 void VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
1390 assert(Chunk.Kind == DeclaratorChunk::Reference);
1391 assert(!Chunk.Ref.LValueRef);
1392 TL.setAmpAmpLoc(Chunk.Loc);
1393 }
1394 void VisitArrayTypeLoc(ArrayTypeLoc TL) {
1395 assert(Chunk.Kind == DeclaratorChunk::Array);
1396 TL.setLBracketLoc(Chunk.Loc);
1397 TL.setRBracketLoc(Chunk.EndLoc);
1398 TL.setSizeExpr(static_cast<Expr*>(Chunk.Arr.NumElts));
1399 }
1400 void VisitFunctionTypeLoc(FunctionTypeLoc TL) {
1401 assert(Chunk.Kind == DeclaratorChunk::Function);
1402 TL.setLParenLoc(Chunk.Loc);
1403 TL.setRParenLoc(Chunk.EndLoc);
1404
1405 const DeclaratorChunk::FunctionTypeInfo &FTI = Chunk.Fun;
John McCall54e14c42009-10-22 22:37:11 +00001406 for (unsigned i = 0, e = TL.getNumArgs(), tpi = 0; i != e; ++i) {
John McCall51bd8032009-10-18 01:05:36 +00001407 ParmVarDecl *Param = FTI.ArgInfo[i].Param.getAs<ParmVarDecl>();
John McCall54e14c42009-10-22 22:37:11 +00001408 TL.setArg(tpi++, Param);
John McCall51bd8032009-10-18 01:05:36 +00001409 }
1410 // FIXME: exception specs
1411 }
1412
1413 void VisitTypeLoc(TypeLoc TL) {
1414 llvm::llvm_unreachable("unsupported TypeLoc kind in declarator!");
1415 }
1416 };
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001417}
1418
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001419/// \brief Create and instantiate a DeclaratorInfo with type source information.
1420///
1421/// \param T QualType referring to the type as written in source code.
1422DeclaratorInfo *
Sebastian Redl8ce35b02009-10-25 21:45:37 +00001423Sema::GetDeclaratorInfoForDeclarator(Declarator &D, QualType T) {
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001424 DeclaratorInfo *DInfo = Context.CreateDeclaratorInfo(T);
John McCall51bd8032009-10-18 01:05:36 +00001425 UnqualTypeLoc CurrTL = DInfo->getTypeLoc().getUnqualifiedLoc();
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001426
Sebastian Redl8ce35b02009-10-25 21:45:37 +00001427 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
John McCall51bd8032009-10-18 01:05:36 +00001428 DeclaratorLocFiller(D.getTypeObject(i)).Visit(CurrTL);
1429 CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001430 }
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001431
John McCall51bd8032009-10-18 01:05:36 +00001432 TypeSpecLocFiller(D.getDeclSpec()).Visit(CurrTL);
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001433
1434 return DInfo;
1435}
1436
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001437/// \brief Create a LocInfoType to hold the given QualType and DeclaratorInfo.
1438QualType Sema::CreateLocInfoType(QualType T, DeclaratorInfo *DInfo) {
1439 // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser
1440 // and Sema during declaration parsing. Try deallocating/caching them when
1441 // it's appropriate, instead of allocating them and keeping them around.
1442 LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType), 8);
1443 new (LocT) LocInfoType(T, DInfo);
1444 assert(LocT->getTypeClass() != T->getTypeClass() &&
1445 "LocInfoType's TypeClass conflicts with an existing Type class");
1446 return QualType(LocT, 0);
1447}
1448
1449void LocInfoType::getAsStringInternal(std::string &Str,
1450 const PrintingPolicy &Policy) const {
Argyrios Kyrtzidis35d44e52009-08-19 01:46:06 +00001451 assert(false && "LocInfoType leaked into the type system; an opaque TypeTy*"
1452 " was used directly instead of getting the QualType through"
1453 " GetTypeFromParser");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001454}
1455
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001456/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
Fariborz Jahanian360300c2007-11-09 22:27:59 +00001457/// declarator
Chris Lattnerb28317a2009-03-28 19:18:32 +00001458QualType Sema::ObjCGetTypeForMethodDefinition(DeclPtrTy D) {
1459 ObjCMethodDecl *MDecl = cast<ObjCMethodDecl>(D.getAs<Decl>());
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001460 QualType T = MDecl->getResultType();
1461 llvm::SmallVector<QualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +00001462
Fariborz Jahanian35600022007-11-09 17:18:29 +00001463 // Add the first two invisible argument types for self and _cmd.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001464 if (MDecl->isInstanceMethod()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001465 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +00001466 selfTy = Context.getPointerType(selfTy);
1467 ArgTys.push_back(selfTy);
Chris Lattner89951a82009-02-20 18:43:26 +00001468 } else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001469 ArgTys.push_back(Context.getObjCIdType());
1470 ArgTys.push_back(Context.getObjCSelType());
Mike Stump1eb44332009-09-09 15:08:12 +00001471
Chris Lattner89951a82009-02-20 18:43:26 +00001472 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
1473 E = MDecl->param_end(); PI != E; ++PI) {
1474 QualType ArgTy = (*PI)->getType();
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001475 assert(!ArgTy.isNull() && "Couldn't parse type?");
Douglas Gregorbeb58cb2009-03-23 23:17:00 +00001476 ArgTy = adjustParameterType(ArgTy);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001477 ArgTys.push_back(ArgTy);
1478 }
1479 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001480 MDecl->isVariadic(), 0);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001481 return T;
1482}
1483
Sebastian Redl9e5e4aa2009-01-26 19:54:48 +00001484/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that
1485/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
1486/// they point to and return true. If T1 and T2 aren't pointer types
1487/// or pointer-to-member types, or if they are not similar at this
1488/// level, returns false and leaves T1 and T2 unchanged. Top-level
1489/// qualifiers on T1 and T2 are ignored. This function will typically
1490/// be called in a loop that successively "unwraps" pointer and
1491/// pointer-to-member types to compare them at each level.
Chris Lattnerecb81f22009-02-16 21:43:00 +00001492bool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001493 const PointerType *T1PtrType = T1->getAs<PointerType>(),
1494 *T2PtrType = T2->getAs<PointerType>();
Douglas Gregor57373262008-10-22 14:17:15 +00001495 if (T1PtrType && T2PtrType) {
1496 T1 = T1PtrType->getPointeeType();
1497 T2 = T2PtrType->getPointeeType();
1498 return true;
1499 }
1500
Ted Kremenek6217b802009-07-29 21:53:49 +00001501 const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(),
1502 *T2MPType = T2->getAs<MemberPointerType>();
Sebastian Redl21593ac2009-01-28 18:33:18 +00001503 if (T1MPType && T2MPType &&
1504 Context.getCanonicalType(T1MPType->getClass()) ==
1505 Context.getCanonicalType(T2MPType->getClass())) {
Sebastian Redl4433aaf2009-01-25 19:43:20 +00001506 T1 = T1MPType->getPointeeType();
1507 T2 = T2MPType->getPointeeType();
1508 return true;
1509 }
Douglas Gregor57373262008-10-22 14:17:15 +00001510 return false;
1511}
1512
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001513Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001514 // C99 6.7.6: Type names have no identifier. This is already validated by
1515 // the parser.
1516 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00001517
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001518 DeclaratorInfo *DInfo = 0;
Douglas Gregor402abb52009-05-28 23:31:59 +00001519 TagDecl *OwnedTag = 0;
Sebastian Redl8ce35b02009-10-25 21:45:37 +00001520 QualType T = GetTypeForDeclarator(D, S, &DInfo, &OwnedTag);
Chris Lattner5153ee62009-04-25 08:47:54 +00001521 if (D.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +00001522 return true;
Steve Naroff5912a352007-08-28 20:14:24 +00001523
Douglas Gregor402abb52009-05-28 23:31:59 +00001524 if (getLangOptions().CPlusPlus) {
1525 // Check that there are no default arguments (C++ only).
Douglas Gregor6d6eb572008-05-07 04:49:29 +00001526 CheckExtraCXXDefaultArguments(D);
1527
Douglas Gregor402abb52009-05-28 23:31:59 +00001528 // C++0x [dcl.type]p3:
1529 // A type-specifier-seq shall not define a class or enumeration
1530 // unless it appears in the type-id of an alias-declaration
1531 // (7.1.3).
1532 if (OwnedTag && OwnedTag->isDefinition())
1533 Diag(OwnedTag->getLocation(), diag::err_type_defined_in_type_specifier)
1534 << Context.getTypeDeclType(OwnedTag);
1535 }
1536
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001537 if (DInfo)
1538 T = CreateLocInfoType(T, DInfo);
1539
Reid Spencer5f016e22007-07-11 17:01:13 +00001540 return T.getAsOpaquePtr();
1541}
1542
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001543
1544
1545//===----------------------------------------------------------------------===//
1546// Type Attribute Processing
1547//===----------------------------------------------------------------------===//
1548
1549/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
1550/// specified type. The attribute contains 1 argument, the id of the address
1551/// space for the type.
Mike Stump1eb44332009-09-09 15:08:12 +00001552static void HandleAddressSpaceTypeAttribute(QualType &Type,
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001553 const AttributeList &Attr, Sema &S){
John McCall0953e762009-09-24 19:53:00 +00001554
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001555 // If this type is already address space qualified, reject it.
1556 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
1557 // for two or more different address spaces."
1558 if (Type.getAddressSpace()) {
1559 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
1560 return;
1561 }
Mike Stump1eb44332009-09-09 15:08:12 +00001562
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001563 // Check the attribute arguments.
1564 if (Attr.getNumArgs() != 1) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +00001565 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001566 return;
1567 }
1568 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
1569 llvm::APSInt addrSpace(32);
1570 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001571 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
1572 << ASArgExpr->getSourceRange();
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001573 return;
1574 }
1575
John McCallefadb772009-07-28 06:52:18 +00001576 // Bounds checking.
1577 if (addrSpace.isSigned()) {
1578 if (addrSpace.isNegative()) {
1579 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative)
1580 << ASArgExpr->getSourceRange();
1581 return;
1582 }
1583 addrSpace.setIsSigned(false);
1584 }
1585 llvm::APSInt max(addrSpace.getBitWidth());
John McCall0953e762009-09-24 19:53:00 +00001586 max = Qualifiers::MaxAddressSpace;
John McCallefadb772009-07-28 06:52:18 +00001587 if (addrSpace > max) {
1588 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high)
John McCall0953e762009-09-24 19:53:00 +00001589 << Qualifiers::MaxAddressSpace << ASArgExpr->getSourceRange();
John McCallefadb772009-07-28 06:52:18 +00001590 return;
1591 }
1592
Mike Stump1eb44332009-09-09 15:08:12 +00001593 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001594 Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001595}
1596
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001597/// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the
1598/// specified type. The attribute contains 1 argument, weak or strong.
Mike Stump1eb44332009-09-09 15:08:12 +00001599static void HandleObjCGCTypeAttribute(QualType &Type,
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001600 const AttributeList &Attr, Sema &S) {
John McCall0953e762009-09-24 19:53:00 +00001601 if (Type.getObjCGCAttr() != Qualifiers::GCNone) {
Fariborz Jahanian5934e752009-02-18 18:52:41 +00001602 S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001603 return;
1604 }
Mike Stump1eb44332009-09-09 15:08:12 +00001605
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001606 // Check the attribute arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001607 if (!Attr.getParameterName()) {
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001608 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1609 << "objc_gc" << 1;
1610 return;
1611 }
John McCall0953e762009-09-24 19:53:00 +00001612 Qualifiers::GC GCAttr;
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001613 if (Attr.getNumArgs() != 0) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001614 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1615 return;
1616 }
Mike Stump1eb44332009-09-09 15:08:12 +00001617 if (Attr.getParameterName()->isStr("weak"))
John McCall0953e762009-09-24 19:53:00 +00001618 GCAttr = Qualifiers::Weak;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001619 else if (Attr.getParameterName()->isStr("strong"))
John McCall0953e762009-09-24 19:53:00 +00001620 GCAttr = Qualifiers::Strong;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001621 else {
1622 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1623 << "objc_gc" << Attr.getParameterName();
1624 return;
1625 }
Mike Stump1eb44332009-09-09 15:08:12 +00001626
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001627 Type = S.Context.getObjCGCQualType(Type, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001628}
1629
Mike Stump24556362009-07-25 21:26:53 +00001630/// HandleNoReturnTypeAttribute - Process the noreturn attribute on the
1631/// specified type. The attribute contains 0 arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001632static void HandleNoReturnTypeAttribute(QualType &Type,
Mike Stump24556362009-07-25 21:26:53 +00001633 const AttributeList &Attr, Sema &S) {
1634 if (Attr.getNumArgs() != 0)
1635 return;
1636
1637 // We only apply this to a pointer to function or a pointer to block.
1638 if (!Type->isFunctionPointerType()
1639 && !Type->isBlockPointerType()
1640 && !Type->isFunctionType())
1641 return;
1642
1643 Type = S.Context.getNoReturnType(Type);
1644}
1645
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001646void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) {
Chris Lattner232e8822008-02-21 01:08:11 +00001647 // Scan through and apply attributes to this type where it makes sense. Some
1648 // attributes (such as __address_space__, __vector_size__, etc) apply to the
1649 // type, but others can be present in the type specifiers even though they
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00001650 // apply to the decl. Here we apply type attributes and ignore the rest.
1651 for (; AL; AL = AL->getNext()) {
Chris Lattner232e8822008-02-21 01:08:11 +00001652 // If this is an attribute we can handle, do so now, otherwise, add it to
1653 // the LeftOverAttrs list for rechaining.
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00001654 switch (AL->getKind()) {
Chris Lattner232e8822008-02-21 01:08:11 +00001655 default: break;
1656 case AttributeList::AT_address_space:
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001657 HandleAddressSpaceTypeAttribute(Result, *AL, *this);
1658 break;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001659 case AttributeList::AT_objc_gc:
1660 HandleObjCGCTypeAttribute(Result, *AL, *this);
1661 break;
Mike Stump24556362009-07-25 21:26:53 +00001662 case AttributeList::AT_noreturn:
1663 HandleNoReturnTypeAttribute(Result, *AL, *this);
1664 break;
Chris Lattner232e8822008-02-21 01:08:11 +00001665 }
Chris Lattner232e8822008-02-21 01:08:11 +00001666 }
Chris Lattner232e8822008-02-21 01:08:11 +00001667}
1668
Mike Stump1eb44332009-09-09 15:08:12 +00001669/// @brief Ensure that the type T is a complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001670///
1671/// This routine checks whether the type @p T is complete in any
1672/// context where a complete type is required. If @p T is a complete
Douglas Gregor86447ec2009-03-09 16:13:40 +00001673/// type, returns false. If @p T is a class template specialization,
1674/// this routine then attempts to perform class template
1675/// instantiation. If instantiation fails, or if @p T is incomplete
1676/// and cannot be completed, issues the diagnostic @p diag (giving it
1677/// the type @p T) and returns true.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001678///
1679/// @param Loc The location in the source that the incomplete type
1680/// diagnostic should refer to.
1681///
1682/// @param T The type that this routine is examining for completeness.
1683///
Mike Stump1eb44332009-09-09 15:08:12 +00001684/// @param PD The partial diagnostic that will be printed out if T is not a
Anders Carlssonb7906612009-08-26 23:45:07 +00001685/// complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001686///
1687/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
1688/// @c false otherwise.
Anders Carlsson91a0cc92009-08-26 22:33:56 +00001689bool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
Anders Carlsson8c8d9192009-10-09 23:51:55 +00001690 const PartialDiagnostic &PD,
1691 std::pair<SourceLocation,
1692 PartialDiagnostic> Note) {
Anders Carlsson91a0cc92009-08-26 22:33:56 +00001693 unsigned diag = PD.getDiagID();
Mike Stump1eb44332009-09-09 15:08:12 +00001694
Douglas Gregor573d9c32009-10-21 23:19:44 +00001695 // FIXME: Add this assertion to make sure we always get instantiation points.
1696 // assert(!Loc.isInvalid() && "Invalid location in RequireCompleteType");
Douglas Gregor690dc7f2009-05-21 23:48:18 +00001697 // FIXME: Add this assertion to help us flush out problems with
1698 // checking for dependent types and type-dependent expressions.
1699 //
Mike Stump1eb44332009-09-09 15:08:12 +00001700 // assert(!T->isDependentType() &&
Douglas Gregor690dc7f2009-05-21 23:48:18 +00001701 // "Can't ask whether a dependent type is complete");
1702
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001703 // If we have a complete type, we're done.
1704 if (!T->isIncompleteType())
1705 return false;
Eli Friedman3c0eb162008-05-27 03:33:27 +00001706
Douglas Gregord475b8d2009-03-25 21:17:03 +00001707 // If we have a class template specialization or a class member of a
1708 // class template specialization, try to instantiate it.
Ted Kremenek6217b802009-07-29 21:53:49 +00001709 if (const RecordType *Record = T->getAs<RecordType>()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001710 if (ClassTemplateSpecializationDecl *ClassTemplateSpec
Douglas Gregord475b8d2009-03-25 21:17:03 +00001711 = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001712 if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared)
1713 return InstantiateClassTemplateSpecialization(Loc, ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001714 TSK_ImplicitInstantiation,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001715 /*Complain=*/diag != 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001716 } else if (CXXRecordDecl *Rec
Douglas Gregord475b8d2009-03-25 21:17:03 +00001717 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
1718 if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001719 MemberSpecializationInfo *MSInfo = Rec->getMemberSpecializationInfo();
1720 assert(MSInfo && "Missing member specialization information?");
Douglas Gregor357bbd02009-08-28 20:50:45 +00001721 // This record was instantiated from a class within a template.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001722 if (MSInfo->getTemplateSpecializationKind()
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001723 != TSK_ExplicitSpecialization)
Douglas Gregorf6b11852009-10-08 15:14:33 +00001724 return InstantiateClass(Loc, Rec, Pattern,
1725 getTemplateInstantiationArgs(Rec),
1726 TSK_ImplicitInstantiation,
1727 /*Complain=*/diag != 0);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001728 }
1729 }
1730 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00001731
Douglas Gregor5842ba92009-08-24 15:23:48 +00001732 if (diag == 0)
1733 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001734
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001735 // We have an incomplete type. Produce a diagnostic.
Anders Carlsson91a0cc92009-08-26 22:33:56 +00001736 Diag(Loc, PD) << T;
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001737
Anders Carlsson8c8d9192009-10-09 23:51:55 +00001738 // If we have a note, produce it.
1739 if (!Note.first.isInvalid())
1740 Diag(Note.first, Note.second);
1741
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001742 // If the type was a forward declaration of a class/struct/union
Mike Stump1eb44332009-09-09 15:08:12 +00001743 // type, produce
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001744 const TagType *Tag = 0;
Ted Kremenek6217b802009-07-29 21:53:49 +00001745 if (const RecordType *Record = T->getAs<RecordType>())
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001746 Tag = Record;
John McCall183700f2009-09-21 23:43:11 +00001747 else if (const EnumType *Enum = T->getAs<EnumType>())
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001748 Tag = Enum;
1749
1750 if (Tag && !Tag->getDecl()->isInvalidDecl())
Mike Stump1eb44332009-09-09 15:08:12 +00001751 Diag(Tag->getDecl()->getLocation(),
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001752 Tag->isBeingDefined() ? diag::note_type_being_defined
1753 : diag::note_forward_declaration)
1754 << QualType(Tag, 0);
1755
1756 return true;
1757}
Douglas Gregore6258932009-03-19 00:39:20 +00001758
1759/// \brief Retrieve a version of the type 'T' that is qualified by the
1760/// nested-name-specifier contained in SS.
1761QualType Sema::getQualifiedNameType(const CXXScopeSpec &SS, QualType T) {
1762 if (!SS.isSet() || SS.isInvalid() || T.isNull())
1763 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00001764
Douglas Gregorab452ba2009-03-26 23:50:42 +00001765 NestedNameSpecifier *NNS
Douglas Gregor35073692009-03-26 23:56:24 +00001766 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Douglas Gregorab452ba2009-03-26 23:50:42 +00001767 return Context.getQualifiedNameType(NNS, T);
Douglas Gregore6258932009-03-19 00:39:20 +00001768}
Anders Carlssonaf017e62009-06-29 22:58:55 +00001769
1770QualType Sema::BuildTypeofExprType(Expr *E) {
1771 return Context.getTypeOfExprType(E);
1772}
1773
1774QualType Sema::BuildDecltypeType(Expr *E) {
1775 if (E->getType() == Context.OverloadTy) {
Mike Stump1eb44332009-09-09 15:08:12 +00001776 Diag(E->getLocStart(),
Anders Carlssonaf017e62009-06-29 22:58:55 +00001777 diag::err_cannot_determine_declared_type_of_overloaded_function);
1778 return QualType();
1779 }
1780 return Context.getDecltypeType(E);
1781}