blob: 67677120858781302598d83f921ec9929c74854a [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"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/Expr.h"
Anders Carlsson91a0cc92009-08-26 22:33:56 +000021#include "clang/Basic/PartialDiagnostic.h"
Daniel Dunbare4858a62008-08-11 03:45:03 +000022#include "clang/Parse/DeclSpec.h"
Sebastian Redl4994d2d2009-07-04 11:39:00 +000023#include "llvm/ADT/SmallPtrSet.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000024using namespace clang;
25
Douglas Gregor2dc0e642009-03-23 23:06:20 +000026/// \brief Perform adjustment on the parameter type of a function.
27///
28/// This routine adjusts the given parameter type @p T to the actual
Mike Stump1eb44332009-09-09 15:08:12 +000029/// parameter type used by semantic analysis (C99 6.7.5.3p[7,8],
30/// C++ [dcl.fct]p3). The adjusted parameter type is returned.
Douglas Gregor2dc0e642009-03-23 23:06:20 +000031QualType Sema::adjustParameterType(QualType T) {
32 // C99 6.7.5.3p7:
33 if (T->isArrayType()) {
34 // C99 6.7.5.3p7:
35 // A declaration of a parameter as "array of type" shall be
36 // adjusted to "qualified pointer to type", where the type
37 // qualifiers (if any) are those specified within the [ and ] of
38 // the array type derivation.
39 return Context.getArrayDecayedType(T);
40 } else if (T->isFunctionType())
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 return Context.getPointerType(T);
46
47 return T;
48}
49
Douglas Gregor930d8b52009-01-30 22:09:00 +000050/// \brief Convert the specified declspec to the appropriate type
51/// object.
52/// \param DS the declaration specifiers
Chris Lattner3f84ad22009-04-22 05:27:59 +000053/// \param DeclLoc The location of the declarator identifier or invalid if none.
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +000054/// \param SourceTy QualType representing the type as written in source form.
Chris Lattner5153ee62009-04-25 08:47:54 +000055/// \returns The type described by the declaration specifiers. This function
56/// never returns null.
Chris Lattner3f84ad22009-04-22 05:27:59 +000057QualType Sema::ConvertDeclSpecToType(const DeclSpec &DS,
Chris Lattnereaaebc72009-04-25 08:06:05 +000058 SourceLocation DeclLoc,
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +000059 bool &isInvalid, QualType &SourceTy) {
Reid Spencer5f016e22007-07-11 17:01:13 +000060 // FIXME: Should move the logic from DeclSpec::Finish to here for validity
61 // checking.
Chris Lattner958858e2008-02-20 21:40:32 +000062 QualType Result;
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +000063 SourceTy = Result;
Mike Stump1eb44332009-09-09 15:08:12 +000064
Reid Spencer5f016e22007-07-11 17:01:13 +000065 switch (DS.getTypeSpecType()) {
Chris Lattner96b77fc2008-04-02 06:50:17 +000066 case DeclSpec::TST_void:
67 Result = Context.VoidTy;
68 break;
Reid Spencer5f016e22007-07-11 17:01:13 +000069 case DeclSpec::TST_char:
70 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
Chris Lattnerfab5b452008-02-20 23:53:49 +000071 Result = Context.CharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000072 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
Chris Lattnerfab5b452008-02-20 23:53:49 +000073 Result = Context.SignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000074 else {
75 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
76 "Unknown TSS value");
Chris Lattnerfab5b452008-02-20 23:53:49 +000077 Result = Context.UnsignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000078 }
Chris Lattner958858e2008-02-20 21:40:32 +000079 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +000080 case DeclSpec::TST_wchar:
81 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
82 Result = Context.WCharTy;
83 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +000084 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
85 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +000086 Result = Context.getSignedWCharType();
87 } else {
88 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
89 "Unknown TSS value");
Chris Lattnerf3a41af2008-11-20 06:38:18 +000090 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
91 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +000092 Result = Context.getUnsignedWCharType();
93 }
94 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +000095 case DeclSpec::TST_char16:
96 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
97 "Unknown TSS value");
98 Result = Context.Char16Ty;
99 break;
100 case DeclSpec::TST_char32:
101 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
102 "Unknown TSS value");
103 Result = Context.Char32Ty;
104 break;
Chris Lattnerd658b562008-04-05 06:32:51 +0000105 case DeclSpec::TST_unspecified:
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000106 // "<proto1,proto2>" is an objc qualified ID with a missing id.
Chris Lattner097e9162008-10-20 02:01:50 +0000107 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000108 SourceTy = Context.getObjCProtocolListType(QualType(),
109 (ObjCProtocolDecl**)PQ,
110 DS.getNumProtocolQualifiers());
Mike Stump1eb44332009-09-09 15:08:12 +0000111 Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy,
Steve Naroff14108da2009-07-10 23:34:53 +0000112 (ObjCProtocolDecl**)PQ,
Steve Naroff683087f2009-06-29 16:22:52 +0000113 DS.getNumProtocolQualifiers());
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000114 break;
115 }
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Chris Lattnerd658b562008-04-05 06:32:51 +0000117 // Unspecified typespec defaults to int in C90. However, the C90 grammar
118 // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
119 // type-qualifier, or storage-class-specifier. If not, emit an extwarn.
120 // Note that the one exception to this is function definitions, which are
121 // allowed to be completely missing a declspec. This is handled in the
122 // parser already though by it pretending to have seen an 'int' in this
123 // case.
124 if (getLangOptions().ImplicitInt) {
Chris Lattner35d276f2009-02-27 18:53:28 +0000125 // In C89 mode, we only warn if there is a completely missing declspec
126 // when one is not allowed.
Chris Lattner3f84ad22009-04-22 05:27:59 +0000127 if (DS.isEmpty()) {
128 if (DeclLoc.isInvalid())
129 DeclLoc = DS.getSourceRange().getBegin();
Eli Friedmanfcff5772009-06-03 12:22:01 +0000130 Diag(DeclLoc, diag::ext_missing_declspec)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000131 << DS.getSourceRange()
Chris Lattner173144a2009-02-27 22:31:56 +0000132 << CodeModificationHint::CreateInsertion(DS.getSourceRange().getBegin(),
133 "int");
Chris Lattner3f84ad22009-04-22 05:27:59 +0000134 }
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000135 } else if (!DS.hasTypeSpecifier()) {
Chris Lattnerd658b562008-04-05 06:32:51 +0000136 // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says:
137 // "At least one type specifier shall be given in the declaration
138 // specifiers in each declaration, and in the specifier-qualifier list in
139 // each struct declaration and type name."
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000140 // FIXME: Does Microsoft really have the implicit int extension in C++?
Chris Lattner3f84ad22009-04-22 05:27:59 +0000141 if (DeclLoc.isInvalid())
142 DeclLoc = DS.getSourceRange().getBegin();
143
Chris Lattnerb78d8332009-06-26 04:45:06 +0000144 if (getLangOptions().CPlusPlus && !getLangOptions().Microsoft) {
Chris Lattner3f84ad22009-04-22 05:27:59 +0000145 Diag(DeclLoc, diag::err_missing_type_specifier)
146 << DS.getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Chris Lattnerb78d8332009-06-26 04:45:06 +0000148 // When this occurs in C++ code, often something is very broken with the
149 // value being declared, poison it as invalid so we don't get chains of
150 // errors.
151 isInvalid = true;
152 } else {
Eli Friedmanfcff5772009-06-03 12:22:01 +0000153 Diag(DeclLoc, diag::ext_missing_type_specifier)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000154 << DS.getSourceRange();
Chris Lattnerb78d8332009-06-26 04:45:06 +0000155 }
Chris Lattnerd658b562008-04-05 06:32:51 +0000156 }
Mike Stump1eb44332009-09-09 15:08:12 +0000157
158 // FALL THROUGH.
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000159 case DeclSpec::TST_int: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000160 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
161 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000162 case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
163 case DeclSpec::TSW_short: Result = Context.ShortTy; break;
164 case DeclSpec::TSW_long: Result = Context.LongTy; break;
165 case DeclSpec::TSW_longlong: Result = Context.LongLongTy; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 }
167 } else {
168 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000169 case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
170 case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break;
171 case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break;
172 case DeclSpec::TSW_longlong: Result =Context.UnsignedLongLongTy; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000173 }
174 }
Chris Lattner958858e2008-02-20 21:40:32 +0000175 break;
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000176 }
Chris Lattnerfab5b452008-02-20 23:53:49 +0000177 case DeclSpec::TST_float: Result = Context.FloatTy; break;
Chris Lattner958858e2008-02-20 21:40:32 +0000178 case DeclSpec::TST_double:
179 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000180 Result = Context.LongDoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000181 else
Chris Lattnerfab5b452008-02-20 23:53:49 +0000182 Result = Context.DoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000183 break;
Chris Lattnerfab5b452008-02-20 23:53:49 +0000184 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
Reid Spencer5f016e22007-07-11 17:01:13 +0000185 case DeclSpec::TST_decimal32: // _Decimal32
186 case DeclSpec::TST_decimal64: // _Decimal64
187 case DeclSpec::TST_decimal128: // _Decimal128
Chris Lattner8f12f652009-05-13 05:02:08 +0000188 Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported);
189 Result = Context.IntTy;
190 isInvalid = true;
191 break;
Chris Lattner99dc9142008-04-13 18:59:07 +0000192 case DeclSpec::TST_class:
Reid Spencer5f016e22007-07-11 17:01:13 +0000193 case DeclSpec::TST_enum:
194 case DeclSpec::TST_union:
195 case DeclSpec::TST_struct: {
196 Decl *D = static_cast<Decl *>(DS.getTypeRep());
Chris Lattner99dc9142008-04-13 18:59:07 +0000197 assert(D && "Didn't get a decl for a class/enum/union/struct?");
Reid Spencer5f016e22007-07-11 17:01:13 +0000198 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
199 DS.getTypeSpecSign() == 0 &&
200 "Can't handle qualifiers on typedef names yet!");
201 // TypeQuals handled by caller.
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000202 Result = Context.getTypeDeclType(cast<TypeDecl>(D));
John McCall2191b202009-09-05 06:31:47 +0000203
204 // In C++, make an ElaboratedType.
205 if (getLangOptions().CPlusPlus) {
206 TagDecl::TagKind Tag
207 = TagDecl::getTagKindForTypeSpec(DS.getTypeSpecType());
208 Result = Context.getElaboratedType(Result, Tag);
209 }
Mike Stump1eb44332009-09-09 15:08:12 +0000210
Chris Lattner5153ee62009-04-25 08:47:54 +0000211 if (D->isInvalidDecl())
212 isInvalid = true;
Chris Lattner958858e2008-02-20 21:40:32 +0000213 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000214 }
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000215 case DeclSpec::TST_typename: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000216 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
217 DS.getTypeSpecSign() == 0 &&
218 "Can't handle qualifiers on typedef names yet!");
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000219 Result = GetTypeFromParser(DS.getTypeRep());
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000220
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000221 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000222 SourceTy = Context.getObjCProtocolListType(Result,
223 (ObjCProtocolDecl**)PQ,
224 DS.getNumProtocolQualifiers());
225 if (const ObjCInterfaceType *
226 Interface = Result->getAs<ObjCInterfaceType>()) {
Steve Naroff67ef8ea2009-07-20 17:56:53 +0000227 // It would be nice if protocol qualifiers were only stored with the
228 // ObjCObjectPointerType. Unfortunately, this isn't possible due
229 // to the following typedef idiom (which is uncommon, but allowed):
Mike Stump1eb44332009-09-09 15:08:12 +0000230 //
Steve Naroff67ef8ea2009-07-20 17:56:53 +0000231 // typedef Foo<P> T;
232 // static void func() {
233 // Foo<P> *yy;
234 // T *zz;
235 // }
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000236 Result = Context.getObjCInterfaceType(Interface->getDecl(),
237 (ObjCProtocolDecl**)PQ,
238 DS.getNumProtocolQualifiers());
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000239 } else if (Result->isObjCIdType())
Chris Lattnerae4da612008-07-26 01:53:50 +0000240 // id<protocol-list>
Mike Stump1eb44332009-09-09 15:08:12 +0000241 Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy,
Steve Naroff14108da2009-07-10 23:34:53 +0000242 (ObjCProtocolDecl**)PQ, DS.getNumProtocolQualifiers());
243 else if (Result->isObjCClassType()) {
Chris Lattner3f84ad22009-04-22 05:27:59 +0000244 if (DeclLoc.isInvalid())
245 DeclLoc = DS.getSourceRange().getBegin();
Steve Naroff4262a072009-02-23 18:53:24 +0000246 // Class<protocol-list>
Mike Stump1eb44332009-09-09 15:08:12 +0000247 Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinClassTy,
Steve Naroff470301b2009-07-22 16:07:01 +0000248 (ObjCProtocolDecl**)PQ, DS.getNumProtocolQualifiers());
Chris Lattner3f84ad22009-04-22 05:27:59 +0000249 } else {
250 if (DeclLoc.isInvalid())
251 DeclLoc = DS.getSourceRange().getBegin();
252 Diag(DeclLoc, diag::err_invalid_protocol_qualifiers)
253 << DS.getSourceRange();
Chris Lattnereaaebc72009-04-25 08:06:05 +0000254 isInvalid = true;
Chris Lattner3f84ad22009-04-22 05:27:59 +0000255 }
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000256 }
Mike Stump1eb44332009-09-09 15:08:12 +0000257
Chris Lattnereaaebc72009-04-25 08:06:05 +0000258 // If this is a reference to an invalid typedef, propagate the invalidity.
259 if (TypedefType *TDT = dyn_cast<TypedefType>(Result))
260 if (TDT->getDecl()->isInvalidDecl())
261 isInvalid = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000262
Reid Spencer5f016e22007-07-11 17:01:13 +0000263 // TypeQuals handled by caller.
Chris Lattner958858e2008-02-20 21:40:32 +0000264 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000265 }
Chris Lattner958858e2008-02-20 21:40:32 +0000266 case DeclSpec::TST_typeofType:
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000267 // FIXME: Preserve type source info.
268 Result = GetTypeFromParser(DS.getTypeRep());
Chris Lattner958858e2008-02-20 21:40:32 +0000269 assert(!Result.isNull() && "Didn't get a type for typeof?");
Steve Naroffd1861fd2007-07-31 12:34:36 +0000270 // TypeQuals handled by caller.
Chris Lattnerfab5b452008-02-20 23:53:49 +0000271 Result = Context.getTypeOfType(Result);
Chris Lattner958858e2008-02-20 21:40:32 +0000272 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000273 case DeclSpec::TST_typeofExpr: {
274 Expr *E = static_cast<Expr *>(DS.getTypeRep());
275 assert(E && "Didn't get an expression for typeof?");
276 // TypeQuals handled by caller.
Douglas Gregor72564e72009-02-26 23:50:07 +0000277 Result = Context.getTypeOfExprType(E);
Chris Lattner958858e2008-02-20 21:40:32 +0000278 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000279 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000280 case DeclSpec::TST_decltype: {
281 Expr *E = static_cast<Expr *>(DS.getTypeRep());
282 assert(E && "Didn't get an expression for decltype?");
283 // TypeQuals handled by caller.
Anders Carlssonaf017e62009-06-29 22:58:55 +0000284 Result = BuildDecltypeType(E);
285 if (Result.isNull()) {
286 Result = Context.IntTy;
287 isInvalid = true;
288 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000289 break;
290 }
Anders Carlssone89d1592009-06-26 18:41:36 +0000291 case DeclSpec::TST_auto: {
292 // TypeQuals handled by caller.
293 Result = Context.UndeducedAutoTy;
294 break;
295 }
Mike Stump1eb44332009-09-09 15:08:12 +0000296
Douglas Gregor809070a2009-02-18 17:45:20 +0000297 case DeclSpec::TST_error:
Chris Lattner5153ee62009-04-25 08:47:54 +0000298 Result = Context.IntTy;
299 isInvalid = true;
300 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000301 }
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Chris Lattner958858e2008-02-20 21:40:32 +0000303 // Handle complex types.
Douglas Gregorf244cd72009-02-14 21:06:05 +0000304 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
305 if (getLangOptions().Freestanding)
306 Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
Chris Lattnerfab5b452008-02-20 23:53:49 +0000307 Result = Context.getComplexType(Result);
Douglas Gregorf244cd72009-02-14 21:06:05 +0000308 }
Mike Stump1eb44332009-09-09 15:08:12 +0000309
Chris Lattner958858e2008-02-20 21:40:32 +0000310 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
311 "FIXME: imaginary types not supported yet!");
Mike Stump1eb44332009-09-09 15:08:12 +0000312
Chris Lattner38d8b982008-02-20 22:04:11 +0000313 // See if there are any attributes on the declspec that apply to the type (as
314 // opposed to the decl).
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000315 if (const AttributeList *AL = DS.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000316 ProcessTypeAttributeList(Result, AL);
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Chris Lattner96b77fc2008-04-02 06:50:17 +0000318 // Apply const/volatile/restrict qualifiers to T.
319 if (unsigned TypeQuals = DS.getTypeQualifiers()) {
320
321 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
322 // or incomplete types shall not be restrict-qualified." C++ also allows
323 // restrict-qualified references.
John McCall0953e762009-09-24 19:53:00 +0000324 if (TypeQuals & DeclSpec::TQ_restrict) {
Daniel Dunbarbb710012009-02-26 19:13:44 +0000325 if (Result->isPointerType() || Result->isReferenceType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000326 QualType EltTy = Result->isPointerType() ?
Ted Kremenek6217b802009-07-29 21:53:49 +0000327 Result->getAs<PointerType>()->getPointeeType() :
328 Result->getAs<ReferenceType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000329
Douglas Gregorbad0e652009-03-24 20:32:41 +0000330 // If we have a pointer or reference, the pointee must have an object
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000331 // incomplete type.
332 if (!EltTy->isIncompleteOrObjectType()) {
333 Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000334 diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattnerd1625842008-11-24 06:25:27 +0000335 << EltTy << DS.getSourceRange();
John McCall0953e762009-09-24 19:53:00 +0000336 TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000337 }
338 } else {
Chris Lattner96b77fc2008-04-02 06:50:17 +0000339 Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000340 diag::err_typecheck_invalid_restrict_not_pointer)
Chris Lattnerd1625842008-11-24 06:25:27 +0000341 << Result << DS.getSourceRange();
John McCall0953e762009-09-24 19:53:00 +0000342 TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
Chris Lattner96b77fc2008-04-02 06:50:17 +0000343 }
Chris Lattner96b77fc2008-04-02 06:50:17 +0000344 }
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Chris Lattner96b77fc2008-04-02 06:50:17 +0000346 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
347 // of a function type includes any type qualifiers, the behavior is
348 // undefined."
349 if (Result->isFunctionType() && TypeQuals) {
350 // Get some location to point at, either the C or V location.
351 SourceLocation Loc;
John McCall0953e762009-09-24 19:53:00 +0000352 if (TypeQuals & DeclSpec::TQ_const)
Chris Lattner96b77fc2008-04-02 06:50:17 +0000353 Loc = DS.getConstSpecLoc();
John McCall0953e762009-09-24 19:53:00 +0000354 else if (TypeQuals & DeclSpec::TQ_volatile)
Chris Lattner96b77fc2008-04-02 06:50:17 +0000355 Loc = DS.getVolatileSpecLoc();
John McCall0953e762009-09-24 19:53:00 +0000356 else {
357 assert((TypeQuals & DeclSpec::TQ_restrict) &&
358 "Has CVR quals but not C, V, or R?");
359 Loc = DS.getRestrictSpecLoc();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000360 }
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000361 Diag(Loc, diag::warn_typecheck_function_qualifiers)
Chris Lattnerd1625842008-11-24 06:25:27 +0000362 << Result << DS.getSourceRange();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000363 }
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000365 // C++ [dcl.ref]p1:
366 // Cv-qualified references are ill-formed except when the
367 // cv-qualifiers are introduced through the use of a typedef
368 // (7.1.3) or of a template type argument (14.3), in which
369 // case the cv-qualifiers are ignored.
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000370 // FIXME: Shouldn't we be checking SCS_typedef here?
371 if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000372 TypeQuals && Result->isReferenceType()) {
John McCall0953e762009-09-24 19:53:00 +0000373 TypeQuals &= ~DeclSpec::TQ_const;
374 TypeQuals &= ~DeclSpec::TQ_volatile;
Mike Stump1eb44332009-09-09 15:08:12 +0000375 }
376
John McCall0953e762009-09-24 19:53:00 +0000377 Qualifiers Quals = Qualifiers::fromCVRMask(TypeQuals);
378 Result = Context.getQualifiedType(Result, Quals);
Chris Lattner96b77fc2008-04-02 06:50:17 +0000379 }
John McCall0953e762009-09-24 19:53:00 +0000380
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000381 if (SourceTy.isNull())
382 SourceTy = Result;
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000383 return Result;
384}
385
Douglas Gregorcd281c32009-02-28 00:25:32 +0000386static std::string getPrintableNameForEntity(DeclarationName Entity) {
387 if (Entity)
388 return Entity.getAsString();
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Douglas Gregorcd281c32009-02-28 00:25:32 +0000390 return "type name";
391}
392
393/// \brief Build a pointer type.
394///
395/// \param T The type to which we'll be building a pointer.
396///
397/// \param Quals The cvr-qualifiers to be applied to the pointer type.
398///
399/// \param Loc The location of the entity whose type involves this
400/// pointer type or, if there is no such entity, the location of the
401/// type that will have pointer type.
402///
403/// \param Entity The name of the entity that involves the pointer
404/// type, if known.
405///
406/// \returns A suitable pointer type, if there are no
407/// errors. Otherwise, returns a NULL type.
Mike Stump1eb44332009-09-09 15:08:12 +0000408QualType Sema::BuildPointerType(QualType T, unsigned Quals,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000409 SourceLocation Loc, DeclarationName Entity) {
410 if (T->isReferenceType()) {
411 // C++ 8.3.2p4: There shall be no ... pointers to references ...
412 Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
413 << getPrintableNameForEntity(Entity);
414 return QualType();
415 }
416
John McCall0953e762009-09-24 19:53:00 +0000417 Qualifiers Qs = Qualifiers::fromCVRMask(Quals);
418
Douglas Gregorcd281c32009-02-28 00:25:32 +0000419 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
420 // object or incomplete types shall not be restrict-qualified."
John McCall0953e762009-09-24 19:53:00 +0000421 if (Qs.hasRestrict() && !T->isIncompleteOrObjectType()) {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000422 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
423 << T;
John McCall0953e762009-09-24 19:53:00 +0000424 Qs.removeRestrict();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000425 }
426
427 // Build the pointer type.
John McCall0953e762009-09-24 19:53:00 +0000428 return Context.getQualifiedType(Context.getPointerType(T), Qs);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000429}
430
431/// \brief Build a reference type.
432///
433/// \param T The type to which we'll be building a reference.
434///
John McCall0953e762009-09-24 19:53:00 +0000435/// \param CVR The cvr-qualifiers to be applied to the reference type.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000436///
437/// \param Loc The location of the entity whose type involves this
438/// reference type or, if there is no such entity, the location of the
439/// type that will have reference type.
440///
441/// \param Entity The name of the entity that involves the reference
442/// type, if known.
443///
444/// \returns A suitable reference type, if there are no
445/// errors. Otherwise, returns a NULL type.
John McCall0953e762009-09-24 19:53:00 +0000446QualType Sema::BuildReferenceType(QualType T, bool LValueRef, unsigned CVR,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000447 SourceLocation Loc, DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000448 Qualifiers Quals = Qualifiers::fromCVRMask(CVR);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000449 if (LValueRef) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000450 if (const RValueReferenceType *R = T->getAs<RValueReferenceType>()) {
Sebastian Redldfe292d2009-03-22 21:28:55 +0000451 // C++0x [dcl.typedef]p9: If a typedef TD names a type that is a
452 // reference to a type T, and attempt to create the type "lvalue
453 // reference to cv TD" creates the type "lvalue reference to T".
454 // We use the qualifiers (restrict or none) of the original reference,
455 // not the new ones. This is consistent with GCC.
John McCall0953e762009-09-24 19:53:00 +0000456 QualType LVRT = Context.getLValueReferenceType(R->getPointeeType());
457 return Context.getQualifiedType(LVRT, T.getQualifiers());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000458 }
459 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000460 if (T->isReferenceType()) {
461 // C++ [dcl.ref]p4: There shall be no references to references.
Mike Stump1eb44332009-09-09 15:08:12 +0000462 //
Douglas Gregorcd281c32009-02-28 00:25:32 +0000463 // According to C++ DR 106, references to references are only
464 // diagnosed when they are written directly (e.g., "int & &"),
465 // but not when they happen via a typedef:
466 //
467 // typedef int& intref;
468 // typedef intref& intref2;
469 //
John McCall0953e762009-09-24 19:53:00 +0000470 // Parser::ParseDeclaratorInternal diagnoses the case where
Douglas Gregorcd281c32009-02-28 00:25:32 +0000471 // references are written directly; here, we handle the
472 // collapsing of references-to-references as described in C++
473 // DR 106 and amended by C++ DR 540.
474 return T;
475 }
476
477 // C++ [dcl.ref]p1:
Eli Friedman33a31382009-08-05 19:21:58 +0000478 // A declarator that specifies the type "reference to cv void"
Douglas Gregorcd281c32009-02-28 00:25:32 +0000479 // is ill-formed.
480 if (T->isVoidType()) {
481 Diag(Loc, diag::err_reference_to_void);
482 return QualType();
483 }
484
485 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
486 // object or incomplete types shall not be restrict-qualified."
John McCall0953e762009-09-24 19:53:00 +0000487 if (Quals.hasRestrict() && !T->isIncompleteOrObjectType()) {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000488 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
489 << T;
John McCall0953e762009-09-24 19:53:00 +0000490 Quals.removeRestrict();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000491 }
492
493 // C++ [dcl.ref]p1:
494 // [...] Cv-qualified references are ill-formed except when the
495 // cv-qualifiers are introduced through the use of a typedef
496 // (7.1.3) or of a template type argument (14.3), in which case
497 // the cv-qualifiers are ignored.
498 //
499 // We diagnose extraneous cv-qualifiers for the non-typedef,
500 // non-template type argument case within the parser. Here, we just
501 // ignore any extraneous cv-qualifiers.
John McCall0953e762009-09-24 19:53:00 +0000502 Quals.removeConst();
503 Quals.removeVolatile();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000504
505 // Handle restrict on references.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000506 if (LValueRef)
John McCall0953e762009-09-24 19:53:00 +0000507 return Context.getQualifiedType(Context.getLValueReferenceType(T), Quals);
508 return Context.getQualifiedType(Context.getRValueReferenceType(T), Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000509}
510
511/// \brief Build an array type.
512///
513/// \param T The type of each element in the array.
514///
515/// \param ASM C99 array size modifier (e.g., '*', 'static').
Mike Stump1eb44332009-09-09 15:08:12 +0000516///
517/// \param ArraySize Expression describing the size of the array.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000518///
519/// \param Quals The cvr-qualifiers to be applied to the array's
520/// element type.
521///
522/// \param Loc The location of the entity whose type involves this
523/// array type or, if there is no such entity, the location of the
524/// type that will have array type.
525///
526/// \param Entity The name of the entity that involves the array
527/// type, if known.
528///
529/// \returns A suitable array type, if there are no errors. Otherwise,
530/// returns a NULL type.
531QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
532 Expr *ArraySize, unsigned Quals,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000533 SourceRange Brackets, DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000534
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000535 SourceLocation Loc = Brackets.getBegin();
Mike Stump1eb44332009-09-09 15:08:12 +0000536 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000537 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
Mike Stump1eb44332009-09-09 15:08:12 +0000538 if (RequireCompleteType(Loc, T,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000539 diag::err_illegal_decl_array_incomplete_type))
540 return QualType();
541
542 if (T->isFunctionType()) {
543 Diag(Loc, diag::err_illegal_decl_array_of_functions)
544 << getPrintableNameForEntity(Entity);
545 return QualType();
546 }
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Douglas Gregorcd281c32009-02-28 00:25:32 +0000548 // C++ 8.3.2p4: There shall be no ... arrays of references ...
549 if (T->isReferenceType()) {
550 Diag(Loc, diag::err_illegal_decl_array_of_references)
551 << getPrintableNameForEntity(Entity);
552 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000553 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000554
Anders Carlssone7cf07d2009-06-26 19:33:28 +0000555 if (Context.getCanonicalType(T) == Context.UndeducedAutoTy) {
Mike Stump1eb44332009-09-09 15:08:12 +0000556 Diag(Loc, diag::err_illegal_decl_array_of_auto)
Anders Carlssone7cf07d2009-06-26 19:33:28 +0000557 << getPrintableNameForEntity(Entity);
558 return QualType();
559 }
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Ted Kremenek6217b802009-07-29 21:53:49 +0000561 if (const RecordType *EltTy = T->getAs<RecordType>()) {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000562 // If the element type is a struct or union that contains a variadic
563 // array, accept it as a GNU extension: C99 6.7.2.1p2.
564 if (EltTy->getDecl()->hasFlexibleArrayMember())
565 Diag(Loc, diag::ext_flexible_array_in_array) << T;
566 } else if (T->isObjCInterfaceType()) {
Chris Lattnerc7c11b12009-04-27 01:55:56 +0000567 Diag(Loc, diag::err_objc_array_of_interfaces) << T;
568 return QualType();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000569 }
Mike Stump1eb44332009-09-09 15:08:12 +0000570
Douglas Gregorcd281c32009-02-28 00:25:32 +0000571 // C99 6.7.5.2p1: The size expression shall have integer type.
572 if (ArraySize && !ArraySize->isTypeDependent() &&
573 !ArraySize->getType()->isIntegerType()) {
574 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
575 << ArraySize->getType() << ArraySize->getSourceRange();
576 ArraySize->Destroy(Context);
577 return QualType();
578 }
579 llvm::APSInt ConstVal(32);
580 if (!ArraySize) {
Eli Friedmanf91f5c82009-04-26 21:57:51 +0000581 if (ASM == ArrayType::Star)
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000582 T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets);
Eli Friedmanf91f5c82009-04-26 21:57:51 +0000583 else
584 T = Context.getIncompleteArrayType(T, ASM, Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000585 } else if (ArraySize->isValueDependent()) {
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000586 T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000587 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
588 (!T->isDependentType() && !T->isConstantSizeType())) {
589 // Per C99, a variable array is an array with either a non-constant
590 // size or an element type that has a non-constant-size
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000591 T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000592 } else {
593 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
594 // have a value greater than zero.
595 if (ConstVal.isSigned()) {
596 if (ConstVal.isNegative()) {
597 Diag(ArraySize->getLocStart(),
598 diag::err_typecheck_negative_array_size)
599 << ArraySize->getSourceRange();
600 return QualType();
601 } else if (ConstVal == 0) {
602 // GCC accepts zero sized static arrays.
603 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size)
604 << ArraySize->getSourceRange();
605 }
Mike Stump1eb44332009-09-09 15:08:12 +0000606 }
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000607 T = Context.getConstantArrayWithExprType(T, ConstVal, ArraySize,
608 ASM, Quals, Brackets);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000609 }
610 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
611 if (!getLangOptions().C99) {
Mike Stump1eb44332009-09-09 15:08:12 +0000612 if (ArraySize && !ArraySize->isTypeDependent() &&
613 !ArraySize->isValueDependent() &&
Douglas Gregorcd281c32009-02-28 00:25:32 +0000614 !ArraySize->isIntegerConstantExpr(Context))
Douglas Gregor043cad22009-09-11 00:18:58 +0000615 Diag(Loc, getLangOptions().CPlusPlus? diag::err_vla_cxx : diag::ext_vla);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000616 else if (ASM != ArrayType::Normal || Quals != 0)
Douglas Gregor043cad22009-09-11 00:18:58 +0000617 Diag(Loc,
618 getLangOptions().CPlusPlus? diag::err_c99_array_usage_cxx
619 : diag::ext_c99_array_usage);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000620 }
621
622 return T;
623}
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000624
625/// \brief Build an ext-vector type.
626///
627/// Run the required checks for the extended vector type.
Mike Stump1eb44332009-09-09 15:08:12 +0000628QualType Sema::BuildExtVectorType(QualType T, ExprArg ArraySize,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000629 SourceLocation AttrLoc) {
630
631 Expr *Arg = (Expr *)ArraySize.get();
632
633 // unlike gcc's vector_size attribute, we do not allow vectors to be defined
634 // in conjunction with complex types (pointers, arrays, functions, etc.).
Mike Stump1eb44332009-09-09 15:08:12 +0000635 if (!T->isDependentType() &&
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000636 !T->isIntegerType() && !T->isRealFloatingType()) {
637 Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T;
638 return QualType();
639 }
640
641 if (!Arg->isTypeDependent() && !Arg->isValueDependent()) {
642 llvm::APSInt vecSize(32);
643 if (!Arg->isIntegerConstantExpr(vecSize, Context)) {
644 Diag(AttrLoc, diag::err_attribute_argument_not_int)
645 << "ext_vector_type" << Arg->getSourceRange();
646 return QualType();
647 }
Mike Stump1eb44332009-09-09 15:08:12 +0000648
649 // unlike gcc's vector_size attribute, the size is specified as the
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000650 // number of elements, not the number of bytes.
Mike Stump1eb44332009-09-09 15:08:12 +0000651 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
652
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000653 if (vectorSize == 0) {
654 Diag(AttrLoc, diag::err_attribute_zero_size)
655 << Arg->getSourceRange();
656 return QualType();
657 }
Mike Stump1eb44332009-09-09 15:08:12 +0000658
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000659 if (!T->isDependentType())
660 return Context.getExtVectorType(T, vectorSize);
Mike Stump1eb44332009-09-09 15:08:12 +0000661 }
662
663 return Context.getDependentSizedExtVectorType(T, ArraySize.takeAs<Expr>(),
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000664 AttrLoc);
665}
Mike Stump1eb44332009-09-09 15:08:12 +0000666
Douglas Gregor724651c2009-02-28 01:04:19 +0000667/// \brief Build a function type.
668///
669/// This routine checks the function type according to C++ rules and
670/// under the assumption that the result type and parameter types have
671/// just been instantiated from a template. It therefore duplicates
Douglas Gregor2943aed2009-03-03 04:44:36 +0000672/// some of the behavior of GetTypeForDeclarator, but in a much
Douglas Gregor724651c2009-02-28 01:04:19 +0000673/// simpler form that is only suitable for this narrow use case.
674///
675/// \param T The return type of the function.
676///
677/// \param ParamTypes The parameter types of the function. This array
678/// will be modified to account for adjustments to the types of the
679/// function parameters.
680///
681/// \param NumParamTypes The number of parameter types in ParamTypes.
682///
683/// \param Variadic Whether this is a variadic function type.
684///
685/// \param Quals The cvr-qualifiers to be applied to the function type.
686///
687/// \param Loc The location of the entity whose type involves this
688/// function type or, if there is no such entity, the location of the
689/// type that will have function type.
690///
691/// \param Entity The name of the entity that involves the function
692/// type, if known.
693///
694/// \returns A suitable function type, if there are no
695/// errors. Otherwise, returns a NULL type.
696QualType Sema::BuildFunctionType(QualType T,
Mike Stump1eb44332009-09-09 15:08:12 +0000697 QualType *ParamTypes,
Douglas Gregor724651c2009-02-28 01:04:19 +0000698 unsigned NumParamTypes,
699 bool Variadic, unsigned Quals,
700 SourceLocation Loc, DeclarationName Entity) {
701 if (T->isArrayType() || T->isFunctionType()) {
702 Diag(Loc, diag::err_func_returning_array_function) << T;
703 return QualType();
704 }
Mike Stump1eb44332009-09-09 15:08:12 +0000705
Douglas Gregor724651c2009-02-28 01:04:19 +0000706 bool Invalid = false;
707 for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) {
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000708 QualType ParamType = adjustParameterType(ParamTypes[Idx]);
709 if (ParamType->isVoidType()) {
Douglas Gregor724651c2009-02-28 01:04:19 +0000710 Diag(Loc, diag::err_param_with_void_type);
711 Invalid = true;
712 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000713
Anders Carlsson83913e32009-09-16 23:47:08 +0000714 ParamTypes[Idx] = adjustFunctionParamType(ParamType);
Douglas Gregor724651c2009-02-28 01:04:19 +0000715 }
716
717 if (Invalid)
718 return QualType();
719
Mike Stump1eb44332009-09-09 15:08:12 +0000720 return Context.getFunctionType(T, ParamTypes, NumParamTypes, Variadic,
Douglas Gregor724651c2009-02-28 01:04:19 +0000721 Quals);
722}
Mike Stump1eb44332009-09-09 15:08:12 +0000723
Douglas Gregor949bf692009-06-09 22:17:39 +0000724/// \brief Build a member pointer type \c T Class::*.
725///
726/// \param T the type to which the member pointer refers.
727/// \param Class the class type into which the member pointer points.
John McCall0953e762009-09-24 19:53:00 +0000728/// \param CVR Qualifiers applied to the member pointer type
Douglas Gregor949bf692009-06-09 22:17:39 +0000729/// \param Loc the location where this type begins
730/// \param Entity the name of the entity that will have this member pointer type
731///
732/// \returns a member pointer type, if successful, or a NULL type if there was
733/// an error.
Mike Stump1eb44332009-09-09 15:08:12 +0000734QualType Sema::BuildMemberPointerType(QualType T, QualType Class,
John McCall0953e762009-09-24 19:53:00 +0000735 unsigned CVR, SourceLocation Loc,
Douglas Gregor949bf692009-06-09 22:17:39 +0000736 DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000737 Qualifiers Quals = Qualifiers::fromCVRMask(CVR);
738
Douglas Gregor949bf692009-06-09 22:17:39 +0000739 // Verify that we're not building a pointer to pointer to function with
740 // exception specification.
741 if (CheckDistantExceptionSpec(T)) {
742 Diag(Loc, diag::err_distant_exception_spec);
743
744 // FIXME: If we're doing this as part of template instantiation,
745 // we should return immediately.
746
747 // Build the type anyway, but use the canonical type so that the
748 // exception specifiers are stripped off.
749 T = Context.getCanonicalType(T);
750 }
751
752 // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member
753 // with reference type, or "cv void."
754 if (T->isReferenceType()) {
Anders Carlsson8d4655d2009-06-30 00:06:57 +0000755 Diag(Loc, diag::err_illegal_decl_mempointer_to_reference)
Douglas Gregor949bf692009-06-09 22:17:39 +0000756 << (Entity? Entity.getAsString() : "type name");
757 return QualType();
758 }
759
760 if (T->isVoidType()) {
761 Diag(Loc, diag::err_illegal_decl_mempointer_to_void)
762 << (Entity? Entity.getAsString() : "type name");
763 return QualType();
764 }
765
766 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
767 // object or incomplete types shall not be restrict-qualified."
John McCall0953e762009-09-24 19:53:00 +0000768 if (Quals.hasRestrict() && !T->isIncompleteOrObjectType()) {
Douglas Gregor949bf692009-06-09 22:17:39 +0000769 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
770 << T;
771
772 // FIXME: If we're doing this as part of template instantiation,
773 // we should return immediately.
John McCall0953e762009-09-24 19:53:00 +0000774 Quals.removeRestrict();
Douglas Gregor949bf692009-06-09 22:17:39 +0000775 }
776
777 if (!Class->isDependentType() && !Class->isRecordType()) {
778 Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class;
779 return QualType();
780 }
781
John McCall0953e762009-09-24 19:53:00 +0000782 return Context.getQualifiedType(
783 Context.getMemberPointerType(T, Class.getTypePtr()), Quals);
Douglas Gregor949bf692009-06-09 22:17:39 +0000784}
Mike Stump1eb44332009-09-09 15:08:12 +0000785
Anders Carlsson9a917e42009-06-12 22:56:54 +0000786/// \brief Build a block pointer type.
787///
788/// \param T The type to which we'll be building a block pointer.
789///
John McCall0953e762009-09-24 19:53:00 +0000790/// \param CVR The cvr-qualifiers to be applied to the block pointer type.
Anders Carlsson9a917e42009-06-12 22:56:54 +0000791///
792/// \param Loc The location of the entity whose type involves this
793/// block pointer type or, if there is no such entity, the location of the
794/// type that will have block pointer type.
795///
796/// \param Entity The name of the entity that involves the block pointer
797/// type, if known.
798///
799/// \returns A suitable block pointer type, if there are no
800/// errors. Otherwise, returns a NULL type.
John McCall0953e762009-09-24 19:53:00 +0000801QualType Sema::BuildBlockPointerType(QualType T, unsigned CVR,
Mike Stump1eb44332009-09-09 15:08:12 +0000802 SourceLocation Loc,
Anders Carlsson9a917e42009-06-12 22:56:54 +0000803 DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000804 if (!T->isFunctionType()) {
Anders Carlsson9a917e42009-06-12 22:56:54 +0000805 Diag(Loc, diag::err_nonfunction_block_type);
806 return QualType();
807 }
Mike Stump1eb44332009-09-09 15:08:12 +0000808
John McCall0953e762009-09-24 19:53:00 +0000809 Qualifiers Quals = Qualifiers::fromCVRMask(CVR);
810 return Context.getQualifiedType(Context.getBlockPointerType(T), Quals);
Anders Carlsson9a917e42009-06-12 22:56:54 +0000811}
812
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000813QualType Sema::GetTypeFromParser(TypeTy *Ty, DeclaratorInfo **DInfo) {
814 QualType QT = QualType::getFromOpaquePtr(Ty);
815 DeclaratorInfo *DI = 0;
816 if (LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) {
817 QT = LIT->getType();
818 DI = LIT->getDeclaratorInfo();
819 }
Mike Stump1eb44332009-09-09 15:08:12 +0000820
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000821 if (DInfo) *DInfo = DI;
822 return QT;
823}
824
Mike Stump98eb8a72009-02-04 22:31:32 +0000825/// GetTypeForDeclarator - Convert the type for the specified
826/// declarator to Type instances. Skip the outermost Skip type
827/// objects.
Douglas Gregor402abb52009-05-28 23:31:59 +0000828///
829/// If OwnedDecl is non-NULL, and this declarator's decl-specifier-seq
830/// owns the declaration of a type (e.g., the definition of a struct
831/// type), then *OwnedDecl will receive the owned declaration.
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000832QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S,
833 DeclaratorInfo **DInfo, unsigned Skip,
Douglas Gregor402abb52009-05-28 23:31:59 +0000834 TagDecl **OwnedDecl) {
Mike Stump98eb8a72009-02-04 22:31:32 +0000835 bool OmittedReturnType = false;
836
837 if (D.getContext() == Declarator::BlockLiteralContext
838 && Skip == 0
839 && !D.getDeclSpec().hasTypeSpecifier()
840 && (D.getNumTypeObjects() == 0
841 || (D.getNumTypeObjects() == 1
842 && D.getTypeObject(0).Kind == DeclaratorChunk::Function)))
843 OmittedReturnType = true;
844
Chris Lattnerb23deda2007-08-28 16:40:32 +0000845 // long long is a C99 feature.
Chris Lattnerd1eb3322007-08-28 16:41:29 +0000846 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Chris Lattnerb23deda2007-08-28 16:40:32 +0000847 D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
848 Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
Douglas Gregor930d8b52009-01-30 22:09:00 +0000849
850 // Determine the type of the declarator. Not all forms of declarator
851 // have a type.
852 QualType T;
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000853 // The QualType referring to the type as written in source code. We can't use
854 // T because it can change due to semantic analysis.
855 QualType SourceTy;
856
Douglas Gregor930d8b52009-01-30 22:09:00 +0000857 switch (D.getKind()) {
858 case Declarator::DK_Abstract:
859 case Declarator::DK_Normal:
Douglas Gregordb422df2009-09-25 21:45:23 +0000860 case Declarator::DK_Operator:
861 case Declarator::DK_TemplateId: {
Chris Lattner3f84ad22009-04-22 05:27:59 +0000862 const DeclSpec &DS = D.getDeclSpec();
863 if (OmittedReturnType) {
Mike Stump98eb8a72009-02-04 22:31:32 +0000864 // We default to a dependent type initially. Can be modified by
865 // the first return statement.
866 T = Context.DependentTy;
Chris Lattner3f84ad22009-04-22 05:27:59 +0000867 } else {
Chris Lattnereaaebc72009-04-25 08:06:05 +0000868 bool isInvalid = false;
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000869 T = ConvertDeclSpecToType(DS, D.getIdentifierLoc(), isInvalid, SourceTy);
Chris Lattnereaaebc72009-04-25 08:06:05 +0000870 if (isInvalid)
871 D.setInvalidType(true);
Douglas Gregor402abb52009-05-28 23:31:59 +0000872 else if (OwnedDecl && DS.isTypeSpecOwned())
873 *OwnedDecl = cast<TagDecl>((Decl *)DS.getTypeRep());
Douglas Gregor809070a2009-02-18 17:45:20 +0000874 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000875 break;
Mike Stump98eb8a72009-02-04 22:31:32 +0000876 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000877
878 case Declarator::DK_Constructor:
879 case Declarator::DK_Destructor:
880 case Declarator::DK_Conversion:
881 // Constructors and destructors don't have return types. Use
882 // "void" instead. Conversion operators will check their return
883 // types separately.
884 T = Context.VoidTy;
885 break;
886 }
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000887
888 if (SourceTy.isNull())
889 SourceTy = T;
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000890
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000891 if (T == Context.UndeducedAutoTy) {
892 int Error = -1;
Mike Stump1eb44332009-09-09 15:08:12 +0000893
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000894 switch (D.getContext()) {
895 case Declarator::KNRTypeListContext:
896 assert(0 && "K&R type lists aren't allowed in C++");
897 break;
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000898 case Declarator::PrototypeContext:
899 Error = 0; // Function prototype
900 break;
901 case Declarator::MemberContext:
902 switch (cast<TagDecl>(CurContext)->getTagKind()) {
903 case TagDecl::TK_enum: assert(0 && "unhandled tag kind"); break;
904 case TagDecl::TK_struct: Error = 1; /* Struct member */ break;
905 case TagDecl::TK_union: Error = 2; /* Union member */ break;
906 case TagDecl::TK_class: Error = 3; /* Class member */ break;
Mike Stump1eb44332009-09-09 15:08:12 +0000907 }
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000908 break;
909 case Declarator::CXXCatchContext:
910 Error = 4; // Exception declaration
911 break;
912 case Declarator::TemplateParamContext:
913 Error = 5; // Template parameter
914 break;
915 case Declarator::BlockLiteralContext:
916 Error = 6; // Block literal
917 break;
918 case Declarator::FileContext:
919 case Declarator::BlockContext:
920 case Declarator::ForContext:
921 case Declarator::ConditionContext:
922 case Declarator::TypeNameContext:
923 break;
924 }
925
926 if (Error != -1) {
927 Diag(D.getDeclSpec().getTypeSpecTypeLoc(), diag::err_auto_not_allowed)
928 << Error;
929 T = Context.IntTy;
930 D.setInvalidType(true);
931 }
932 }
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Douglas Gregorcd281c32009-02-28 00:25:32 +0000934 // The name we're declaring, if any.
935 DeclarationName Name;
936 if (D.getIdentifier())
937 Name = D.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +0000938
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +0000939 bool ShouldBuildInfo = DInfo != 0;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000940
Mike Stump98eb8a72009-02-04 22:31:32 +0000941 // Walk the DeclTypeInfo, building the recursive type as we go.
942 // DeclTypeInfos are ordered from the identifier out, which is
943 // opposite of what we want :).
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000944 for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) {
945 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1+Skip);
Reid Spencer5f016e22007-07-11 17:01:13 +0000946 switch (DeclType.Kind) {
947 default: assert(0 && "Unknown decltype!");
Steve Naroff5618bd42008-08-27 16:04:49 +0000948 case DeclaratorChunk::BlockPointer:
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +0000949 if (ShouldBuildInfo) {
950 if (SourceTy->isFunctionType())
John McCall0953e762009-09-24 19:53:00 +0000951 SourceTy
952 = Context.getQualifiedType(Context.getBlockPointerType(SourceTy),
953 Qualifiers::fromCVRMask(DeclType.Cls.TypeQuals));
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +0000954 else
955 // If not function type Context::getBlockPointerType asserts,
956 // so just give up.
957 ShouldBuildInfo = false;
958 }
959
Chris Lattner9af55002009-03-27 04:18:06 +0000960 // If blocks are disabled, emit an error.
961 if (!LangOpts.Blocks)
962 Diag(DeclType.Loc, diag::err_blocks_disable);
Mike Stump1eb44332009-09-09 15:08:12 +0000963
964 T = BuildBlockPointerType(T, DeclType.Cls.TypeQuals, D.getIdentifierLoc(),
Anders Carlsson9a917e42009-06-12 22:56:54 +0000965 Name);
Steve Naroff5618bd42008-08-27 16:04:49 +0000966 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000967 case DeclaratorChunk::Pointer:
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +0000968 //FIXME: Use ObjCObjectPointer for info when appropriate.
969 if (ShouldBuildInfo)
John McCall0953e762009-09-24 19:53:00 +0000970 SourceTy = Context.getQualifiedType(Context.getPointerType(SourceTy),
971 Qualifiers::fromCVRMask(DeclType.Ptr.TypeQuals));
Sebastian Redl6a7330c2009-05-29 15:01:05 +0000972 // Verify that we're not building a pointer to pointer to function with
973 // exception specification.
974 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
975 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
976 D.setInvalidType(true);
977 // Build the type anyway.
978 }
Steve Naroff14108da2009-07-10 23:34:53 +0000979 if (getLangOptions().ObjC1 && T->isObjCInterfaceType()) {
John McCall183700f2009-09-21 23:43:11 +0000980 const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>();
Steve Naroff14108da2009-07-10 23:34:53 +0000981 T = Context.getObjCObjectPointerType(T,
Steve Naroff67ef8ea2009-07-20 17:56:53 +0000982 (ObjCProtocolDecl **)OIT->qual_begin(),
983 OIT->getNumProtocols());
Steve Naroff14108da2009-07-10 23:34:53 +0000984 break;
985 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000986 T = BuildPointerType(T, DeclType.Ptr.TypeQuals, DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000987 break;
John McCall0953e762009-09-24 19:53:00 +0000988 case DeclaratorChunk::Reference: {
989 Qualifiers Quals;
990 if (DeclType.Ref.HasRestrict) Quals.addRestrict();
991
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +0000992 if (ShouldBuildInfo) {
993 if (DeclType.Ref.LValueRef)
994 SourceTy = Context.getLValueReferenceType(SourceTy);
995 else
996 SourceTy = Context.getRValueReferenceType(SourceTy);
John McCall0953e762009-09-24 19:53:00 +0000997 SourceTy = Context.getQualifiedType(SourceTy, Quals);
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +0000998 }
999
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001000 // Verify that we're not building a reference to pointer to function with
1001 // exception specification.
1002 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1003 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1004 D.setInvalidType(true);
1005 // Build the type anyway.
1006 }
John McCall0953e762009-09-24 19:53:00 +00001007 T = BuildReferenceType(T, DeclType.Ref.LValueRef, Quals,
Douglas Gregorcd281c32009-02-28 00:25:32 +00001008 DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +00001009 break;
John McCall0953e762009-09-24 19:53:00 +00001010 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001011 case DeclaratorChunk::Array: {
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001012 if (ShouldBuildInfo)
1013 // We just need to get an array type, the exact type doesn't matter.
1014 SourceTy = Context.getIncompleteArrayType(SourceTy, ArrayType::Normal,
John McCall0953e762009-09-24 19:53:00 +00001015 DeclType.Arr.TypeQuals);
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001016
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001017 // Verify that we're not building an array of pointers to function with
1018 // exception specification.
1019 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1020 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1021 D.setInvalidType(true);
1022 // Build the type anyway.
1023 }
Chris Lattnerfd89bc82008-04-02 01:05:10 +00001024 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner94f81fd2007-08-28 16:54:00 +00001025 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001026 ArrayType::ArraySizeModifier ASM;
1027 if (ATI.isStar)
1028 ASM = ArrayType::Star;
1029 else if (ATI.hasStatic)
1030 ASM = ArrayType::Static;
1031 else
1032 ASM = ArrayType::Normal;
Eli Friedmanf91f5c82009-04-26 21:57:51 +00001033 if (ASM == ArrayType::Star &&
1034 D.getContext() != Declarator::PrototypeContext) {
1035 // FIXME: This check isn't quite right: it allows star in prototypes
1036 // for function definitions, and disallows some edge cases detailed
1037 // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html
1038 Diag(DeclType.Loc, diag::err_array_star_outside_prototype);
1039 ASM = ArrayType::Normal;
1040 D.setInvalidType(true);
1041 }
John McCall0953e762009-09-24 19:53:00 +00001042 T = BuildArrayType(T, ASM, ArraySize,
1043 Qualifiers::fromCVRMask(ATI.TypeQuals),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001044 SourceRange(DeclType.Loc, DeclType.EndLoc), Name);
Reid Spencer5f016e22007-07-11 17:01:13 +00001045 break;
1046 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001047 case DeclaratorChunk::Function: {
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001048 if (ShouldBuildInfo) {
1049 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
1050 llvm::SmallVector<QualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +00001051
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001052 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
1053 ParmVarDecl *Param = FTI.ArgInfo[i].Param.getAs<ParmVarDecl>();
Anders Carlsson83913e32009-09-16 23:47:08 +00001054 if (Param) {
1055 QualType ArgTy = adjustFunctionParamType(Param->getType());
1056
1057 ArgTys.push_back(ArgTy);
1058 }
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001059 }
1060 SourceTy = Context.getFunctionType(SourceTy, ArgTys.data(),
1061 ArgTys.size(),
John McCall0953e762009-09-24 19:53:00 +00001062 FTI.isVariadic,
1063 FTI.TypeQuals);
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001064 }
1065
Reid Spencer5f016e22007-07-11 17:01:13 +00001066 // If the function declarator has a prototype (i.e. it is not () and
1067 // does not have a K&R-style identifier list), then the arguments are part
1068 // of the type, otherwise the argument list is ().
1069 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Sebastian Redl3cc97262009-05-31 11:47:27 +00001070
Chris Lattnercd881292007-12-19 05:31:29 +00001071 // C99 6.7.5.3p1: The return type may not be a function or array type.
Chris Lattner68cfd492007-12-19 18:01:43 +00001072 if (T->isArrayType() || T->isFunctionType()) {
Chris Lattnerd1625842008-11-24 06:25:27 +00001073 Diag(DeclType.Loc, diag::err_func_returning_array_function) << T;
Chris Lattnercd881292007-12-19 05:31:29 +00001074 T = Context.IntTy;
1075 D.setInvalidType(true);
1076 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001077
Douglas Gregor402abb52009-05-28 23:31:59 +00001078 if (getLangOptions().CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) {
1079 // C++ [dcl.fct]p6:
1080 // Types shall not be defined in return or parameter types.
1081 TagDecl *Tag = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep());
1082 if (Tag->isDefinition())
1083 Diag(Tag->getLocation(), diag::err_type_defined_in_result_type)
1084 << Context.getTypeDeclType(Tag);
1085 }
1086
Sebastian Redl3cc97262009-05-31 11:47:27 +00001087 // Exception specs are not allowed in typedefs. Complain, but add it
1088 // anyway.
1089 if (FTI.hasExceptionSpec &&
1090 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
1091 Diag(FTI.getThrowLoc(), diag::err_exception_spec_in_typedef);
1092
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001093 if (FTI.NumArgs == 0) {
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001094 if (getLangOptions().CPlusPlus) {
1095 // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the
1096 // function takes no arguments.
Sebastian Redl465226e2009-05-27 22:11:52 +00001097 llvm::SmallVector<QualType, 4> Exceptions;
1098 Exceptions.reserve(FTI.NumExceptions);
Mike Stump1eb44332009-09-09 15:08:12 +00001099 for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001100 // FIXME: Preserve type source info.
1101 QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty);
Sebastian Redlef65f062009-05-29 18:02:33 +00001102 // Check that the type is valid for an exception spec, and drop it
1103 // if not.
1104 if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1105 Exceptions.push_back(ET);
1106 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001107 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, FTI.TypeQuals,
1108 FTI.hasExceptionSpec,
1109 FTI.hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00001110 Exceptions.size(), Exceptions.data());
Douglas Gregor965acbb2009-02-18 07:07:28 +00001111 } else if (FTI.isVariadic) {
1112 // We allow a zero-parameter variadic function in C if the
1113 // function is marked with the "overloadable"
1114 // attribute. Scan for this attribute now.
1115 bool Overloadable = false;
1116 for (const AttributeList *Attrs = D.getAttributes();
1117 Attrs; Attrs = Attrs->getNext()) {
1118 if (Attrs->getKind() == AttributeList::AT_overloadable) {
1119 Overloadable = true;
1120 break;
1121 }
1122 }
1123
1124 if (!Overloadable)
1125 Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
1126 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, 0);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001127 } else {
1128 // Simple void foo(), where the incoming T is the result type.
Douglas Gregor72564e72009-02-26 23:50:07 +00001129 T = Context.getFunctionNoProtoType(T);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001130 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001131 } else if (FTI.ArgInfo[0].Param == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001132 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
Mike Stump1eb44332009-09-09 15:08:12 +00001133 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
Reid Spencer5f016e22007-07-11 17:01:13 +00001134 } else {
1135 // Otherwise, we have a function with an argument list that is
1136 // potentially variadic.
1137 llvm::SmallVector<QualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +00001138
Reid Spencer5f016e22007-07-11 17:01:13 +00001139 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001140 ParmVarDecl *Param =
1141 cast<ParmVarDecl>(FTI.ArgInfo[i].Param.getAs<Decl>());
Chris Lattner8123a952008-04-10 02:22:51 +00001142 QualType ArgTy = Param->getType();
Chris Lattner78c75fb2007-07-21 05:30:18 +00001143 assert(!ArgTy.isNull() && "Couldn't parse type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001144
1145 // Adjust the parameter type.
Douglas Gregorbeb58cb2009-03-23 23:17:00 +00001146 assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001147
Reid Spencer5f016e22007-07-11 17:01:13 +00001148 // Look for 'void'. void is allowed only as a single argument to a
1149 // function with no other parameters (C99 6.7.5.3p10). We record
Douglas Gregor72564e72009-02-26 23:50:07 +00001150 // int(void) as a FunctionProtoType with an empty argument list.
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001151 if (ArgTy->isVoidType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001152 // If this is something like 'float(int, void)', reject it. 'void'
1153 // is an incomplete type (C99 6.2.5p19) and function decls cannot
1154 // have arguments of incomplete type.
1155 if (FTI.NumArgs != 1 || FTI.isVariadic) {
1156 Diag(DeclType.Loc, diag::err_void_only_param);
Chris Lattner2ff54262007-07-21 05:18:12 +00001157 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00001158 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00001159 } else if (FTI.ArgInfo[i].Ident) {
1160 // Reject, but continue to parse 'int(void abc)'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001161 Diag(FTI.ArgInfo[i].IdentLoc,
Chris Lattner4565d4e2007-07-21 05:26:43 +00001162 diag::err_param_with_void_type);
Chris Lattner2ff54262007-07-21 05:18:12 +00001163 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00001164 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00001165 } else {
1166 // Reject, but continue to parse 'float(const void)'.
John McCall0953e762009-09-24 19:53:00 +00001167 if (ArgTy.hasQualifiers())
Chris Lattner2ff54262007-07-21 05:18:12 +00001168 Diag(DeclType.Loc, diag::err_void_param_qualified);
Mike Stump1eb44332009-09-09 15:08:12 +00001169
Chris Lattner2ff54262007-07-21 05:18:12 +00001170 // Do not add 'void' to the ArgTys list.
1171 break;
1172 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001173 } else if (!FTI.hasPrototype) {
1174 if (ArgTy->isPromotableIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00001175 ArgTy = Context.getPromotedIntegerType(ArgTy);
John McCall183700f2009-09-21 23:43:11 +00001176 } else if (const BuiltinType* BTy = ArgTy->getAs<BuiltinType>()) {
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001177 if (BTy->getKind() == BuiltinType::Float)
1178 ArgTy = Context.DoubleTy;
1179 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001180 }
Mike Stump1eb44332009-09-09 15:08:12 +00001181
Anders Carlsson83913e32009-09-16 23:47:08 +00001182 ArgTys.push_back(adjustFunctionParamType(ArgTy));
Reid Spencer5f016e22007-07-11 17:01:13 +00001183 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001184
1185 llvm::SmallVector<QualType, 4> Exceptions;
1186 Exceptions.reserve(FTI.NumExceptions);
Mike Stump1eb44332009-09-09 15:08:12 +00001187 for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001188 // FIXME: Preserve type source info.
1189 QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty);
Sebastian Redlef65f062009-05-29 18:02:33 +00001190 // Check that the type is valid for an exception spec, and drop it if
1191 // not.
1192 if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1193 Exceptions.push_back(ET);
1194 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001195
Jay Foadbeaaccd2009-05-21 09:52:38 +00001196 T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(),
Sebastian Redl465226e2009-05-27 22:11:52 +00001197 FTI.isVariadic, FTI.TypeQuals,
1198 FTI.hasExceptionSpec,
1199 FTI.hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00001200 Exceptions.size(), Exceptions.data());
Reid Spencer5f016e22007-07-11 17:01:13 +00001201 }
1202 break;
1203 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001204 case DeclaratorChunk::MemberPointer:
Sebastian Redl4994d2d2009-07-04 11:39:00 +00001205 // Verify that we're not building a pointer to pointer to function with
1206 // exception specification.
1207 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1208 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1209 D.setInvalidType(true);
1210 // Build the type anyway.
1211 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001212 // The scope spec must refer to a class, or be dependent.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001213 QualType ClsType;
Douglas Gregor949bf692009-06-09 22:17:39 +00001214 if (isDependentScopeSpecifier(DeclType.Mem.Scope())) {
Mike Stump1eb44332009-09-09 15:08:12 +00001215 NestedNameSpecifier *NNS
Douglas Gregor949bf692009-06-09 22:17:39 +00001216 = (NestedNameSpecifier *)DeclType.Mem.Scope().getScopeRep();
1217 assert(NNS->getAsType() && "Nested-name-specifier must name a type");
1218 ClsType = QualType(NNS->getAsType(), 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001219 } else if (CXXRecordDecl *RD
Douglas Gregor949bf692009-06-09 22:17:39 +00001220 = dyn_cast_or_null<CXXRecordDecl>(
1221 computeDeclContext(DeclType.Mem.Scope()))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001222 ClsType = Context.getTagDeclType(RD);
1223 } else {
Douglas Gregor949bf692009-06-09 22:17:39 +00001224 Diag(DeclType.Mem.Scope().getBeginLoc(),
1225 diag::err_illegal_decl_mempointer_in_nonclass)
1226 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
1227 << DeclType.Mem.Scope().getRange();
Sebastian Redlf30208a2009-01-24 21:16:55 +00001228 D.setInvalidType(true);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001229 }
1230
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001231 if (ShouldBuildInfo) {
1232 QualType cls = !ClsType.isNull() ? ClsType : Context.IntTy;
John McCall0953e762009-09-24 19:53:00 +00001233 SourceTy = Context.getQualifiedType(
1234 Context.getMemberPointerType(SourceTy, cls.getTypePtr()),
1235 Qualifiers::fromCVRMask(DeclType.Mem.TypeQuals));
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001236 }
1237
Douglas Gregor949bf692009-06-09 22:17:39 +00001238 if (!ClsType.isNull())
1239 T = BuildMemberPointerType(T, ClsType, DeclType.Mem.TypeQuals,
1240 DeclType.Loc, D.getIdentifier());
1241 if (T.isNull()) {
1242 T = Context.IntTy;
Sebastian Redlf30208a2009-01-24 21:16:55 +00001243 D.setInvalidType(true);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001244 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001245 break;
1246 }
1247
Douglas Gregorcd281c32009-02-28 00:25:32 +00001248 if (T.isNull()) {
1249 D.setInvalidType(true);
1250 T = Context.IntTy;
1251 }
1252
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001253 // See if there are any attributes on this declarator chunk.
1254 if (const AttributeList *AL = DeclType.getAttrs())
1255 ProcessTypeAttributeList(T, AL);
Reid Spencer5f016e22007-07-11 17:01:13 +00001256 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001257
1258 if (getLangOptions().CPlusPlus && T->isFunctionType()) {
John McCall183700f2009-09-21 23:43:11 +00001259 const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00001260 assert(FnTy && "Why oh why is there not a FunctionProtoType here ?");
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001261
1262 // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type
1263 // for a nonstatic member function, the function type to which a pointer
1264 // to member refers, or the top-level function type of a function typedef
1265 // declaration.
1266 if (FnTy->getTypeQuals() != 0 &&
1267 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
Douglas Gregor584049d2008-12-15 23:53:10 +00001268 ((D.getContext() != Declarator::MemberContext &&
1269 (!D.getCXXScopeSpec().isSet() ||
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001270 !computeDeclContext(D.getCXXScopeSpec(), /*FIXME:*/true)
1271 ->isRecord())) ||
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001272 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001273 if (D.isFunctionDeclarator())
1274 Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
1275 else
1276 Diag(D.getIdentifierLoc(),
1277 diag::err_invalid_qualified_typedef_function_type_use);
1278
1279 // Strip the cv-quals from the type.
1280 T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001281 FnTy->getNumArgs(), FnTy->isVariadic(), 0);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001282 }
1283 }
Mike Stump1eb44332009-09-09 15:08:12 +00001284
Chris Lattner0bf29ad2008-06-29 00:19:33 +00001285 // If there were any type attributes applied to the decl itself (not the
1286 // type, apply the type attribute to the type!)
1287 if (const AttributeList *Attrs = D.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001288 ProcessTypeAttributeList(T, Attrs);
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001289
1290 if (ShouldBuildInfo)
1291 *DInfo = GetDeclaratorInfoForDeclarator(D, SourceTy, Skip);
1292
Reid Spencer5f016e22007-07-11 17:01:13 +00001293 return T;
1294}
1295
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001296static void FillTypeSpecLoc(TypeLoc TSL, const DeclSpec &DS) {
1297 if (TSL.isNull()) return;
1298
1299 if (TypedefLoc *TL = dyn_cast<TypedefLoc>(&TSL)) {
1300 TL->setNameLoc(DS.getTypeSpecTypeLoc());
1301
Argyrios Kyrtzidiseb667592009-09-29 19:45:22 +00001302 } else if (ObjCInterfaceLoc *TL = dyn_cast<ObjCInterfaceLoc>(&TSL)) {
1303 TL->setNameLoc(DS.getTypeSpecTypeLoc());
1304
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001305 } else if (ObjCProtocolListLoc *PLL = dyn_cast<ObjCProtocolListLoc>(&TSL)) {
1306 assert(PLL->getNumProtocols() == DS.getNumProtocolQualifiers());
1307 PLL->setLAngleLoc(DS.getProtocolLAngleLoc());
1308 PLL->setRAngleLoc(DS.getSourceRange().getEnd());
1309 for (unsigned i = 0; i != DS.getNumProtocolQualifiers(); ++i)
1310 PLL->setProtocolLoc(i, DS.getProtocolLocs()[i]);
1311 FillTypeSpecLoc(PLL->getBaseTypeLoc(), DS);
1312
1313 } else {
1314 //FIXME: Other typespecs.
1315 DefaultTypeSpecLoc &DTL = cast<DefaultTypeSpecLoc>(TSL);
1316 DTL.setStartLoc(DS.getSourceRange().getBegin());
1317 }
1318}
1319
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001320/// \brief Create and instantiate a DeclaratorInfo with type source information.
1321///
1322/// \param T QualType referring to the type as written in source code.
1323DeclaratorInfo *
1324Sema::GetDeclaratorInfoForDeclarator(Declarator &D, QualType T, unsigned Skip) {
1325 DeclaratorInfo *DInfo = Context.CreateDeclaratorInfo(T);
1326 TypeLoc CurrTL = DInfo->getTypeLoc();
1327
1328 for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) {
1329 assert(!CurrTL.isNull());
1330
1331 DeclaratorChunk &DeclType = D.getTypeObject(i);
1332 switch (DeclType.Kind) {
1333 default: assert(0 && "Unknown decltype!");
1334 case DeclaratorChunk::BlockPointer: {
1335 BlockPointerLoc &BPL = cast<BlockPointerLoc>(CurrTL);
1336 BPL.setCaretLoc(DeclType.Loc);
1337 break;
1338 }
1339 case DeclaratorChunk::Pointer: {
1340 //FIXME: ObjCObject pointers.
1341 PointerLoc &PL = cast<PointerLoc>(CurrTL);
1342 PL.setStarLoc(DeclType.Loc);
1343 break;
1344 }
1345 case DeclaratorChunk::Reference: {
1346 ReferenceLoc &RL = cast<ReferenceLoc>(CurrTL);
1347 RL.setAmpLoc(DeclType.Loc);
1348 break;
1349 }
1350 case DeclaratorChunk::Array: {
1351 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
1352 ArrayLoc &AL = cast<ArrayLoc>(CurrTL);
1353 AL.setLBracketLoc(DeclType.Loc);
1354 AL.setRBracketLoc(DeclType.EndLoc);
1355 AL.setSizeExpr(static_cast<Expr*>(ATI.NumElts));
1356 //FIXME: Star location for [*].
1357 break;
1358 }
1359 case DeclaratorChunk::Function: {
1360 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
1361 FunctionLoc &FL = cast<FunctionLoc>(CurrTL);
1362 FL.setLParenLoc(DeclType.Loc);
1363 FL.setRParenLoc(DeclType.EndLoc);
1364 for (unsigned i = 0, e = FTI.NumArgs, tpi = 0; i != e; ++i) {
1365 ParmVarDecl *Param = FTI.ArgInfo[i].Param.getAs<ParmVarDecl>();
1366 if (Param) {
1367 assert(tpi < FL.getNumArgs());
1368 FL.setArg(tpi++, Param);
1369 }
1370 }
1371 break;
1372 //FIXME: Exception specs.
1373 }
1374 case DeclaratorChunk::MemberPointer: {
1375 MemberPointerLoc &MPL = cast<MemberPointerLoc>(CurrTL);
1376 MPL.setStarLoc(DeclType.Loc);
1377 //FIXME: Class location.
1378 break;
1379 }
Mike Stump1eb44332009-09-09 15:08:12 +00001380
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001381 }
1382
1383 CurrTL = CurrTL.getNextTypeLoc();
1384 }
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001385
1386 FillTypeSpecLoc(CurrTL, D.getDeclSpec());
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001387
1388 return DInfo;
1389}
1390
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001391/// \brief Create a LocInfoType to hold the given QualType and DeclaratorInfo.
1392QualType Sema::CreateLocInfoType(QualType T, DeclaratorInfo *DInfo) {
1393 // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser
1394 // and Sema during declaration parsing. Try deallocating/caching them when
1395 // it's appropriate, instead of allocating them and keeping them around.
1396 LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType), 8);
1397 new (LocT) LocInfoType(T, DInfo);
1398 assert(LocT->getTypeClass() != T->getTypeClass() &&
1399 "LocInfoType's TypeClass conflicts with an existing Type class");
1400 return QualType(LocT, 0);
1401}
1402
1403void LocInfoType::getAsStringInternal(std::string &Str,
1404 const PrintingPolicy &Policy) const {
Argyrios Kyrtzidis35d44e52009-08-19 01:46:06 +00001405 assert(false && "LocInfoType leaked into the type system; an opaque TypeTy*"
1406 " was used directly instead of getting the QualType through"
1407 " GetTypeFromParser");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001408}
1409
Sebastian Redlef65f062009-05-29 18:02:33 +00001410/// CheckSpecifiedExceptionType - Check if the given type is valid in an
1411/// exception specification. Incomplete types, or pointers to incomplete types
1412/// other than void are not allowed.
1413bool Sema::CheckSpecifiedExceptionType(QualType T, const SourceRange &Range) {
1414 // FIXME: This may not correctly work with the fix for core issue 437,
1415 // where a class's own type is considered complete within its body.
1416
1417 // C++ 15.4p2: A type denoted in an exception-specification shall not denote
1418 // an incomplete type.
1419 if (T->isIncompleteType())
1420 return Diag(Range.getBegin(), diag::err_incomplete_in_exception_spec)
1421 << Range << T << /*direct*/0;
1422
1423 // C++ 15.4p2: A type denoted in an exception-specification shall not denote
1424 // an incomplete type a pointer or reference to an incomplete type, other
1425 // than (cv) void*.
Sebastian Redlef65f062009-05-29 18:02:33 +00001426 int kind;
Ted Kremenek6217b802009-07-29 21:53:49 +00001427 if (const PointerType* IT = T->getAs<PointerType>()) {
Sebastian Redlef65f062009-05-29 18:02:33 +00001428 T = IT->getPointeeType();
1429 kind = 1;
Ted Kremenek6217b802009-07-29 21:53:49 +00001430 } else if (const ReferenceType* IT = T->getAs<ReferenceType>()) {
Sebastian Redlef65f062009-05-29 18:02:33 +00001431 T = IT->getPointeeType();
Sebastian Redl3cc97262009-05-31 11:47:27 +00001432 kind = 2;
Sebastian Redlef65f062009-05-29 18:02:33 +00001433 } else
1434 return false;
1435
1436 if (T->isIncompleteType() && !T->isVoidType())
1437 return Diag(Range.getBegin(), diag::err_incomplete_in_exception_spec)
1438 << Range << T << /*indirect*/kind;
1439
1440 return false;
1441}
1442
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001443/// CheckDistantExceptionSpec - Check if the given type is a pointer or pointer
1444/// to member to a function with an exception specification. This means that
1445/// it is invalid to add another level of indirection.
1446bool Sema::CheckDistantExceptionSpec(QualType T) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001447 if (const PointerType *PT = T->getAs<PointerType>())
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001448 T = PT->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001449 else if (const MemberPointerType *PT = T->getAs<MemberPointerType>())
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001450 T = PT->getPointeeType();
1451 else
1452 return false;
1453
John McCall183700f2009-09-21 23:43:11 +00001454 const FunctionProtoType *FnT = T->getAs<FunctionProtoType>();
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001455 if (!FnT)
1456 return false;
1457
1458 return FnT->hasExceptionSpec();
1459}
1460
Sebastian Redl4994d2d2009-07-04 11:39:00 +00001461/// CheckEquivalentExceptionSpec - Check if the two types have equivalent
1462/// exception specifications. Exception specifications are equivalent if
1463/// they allow exactly the same set of exception types. It does not matter how
1464/// that is achieved. See C++ [except.spec]p2.
1465bool Sema::CheckEquivalentExceptionSpec(
1466 const FunctionProtoType *Old, SourceLocation OldLoc,
1467 const FunctionProtoType *New, SourceLocation NewLoc) {
1468 bool OldAny = !Old->hasExceptionSpec() || Old->hasAnyExceptionSpec();
1469 bool NewAny = !New->hasExceptionSpec() || New->hasAnyExceptionSpec();
1470 if (OldAny && NewAny)
1471 return false;
1472 if (OldAny || NewAny) {
1473 Diag(NewLoc, diag::err_mismatched_exception_spec);
1474 Diag(OldLoc, diag::note_previous_declaration);
1475 return true;
1476 }
1477
1478 bool Success = true;
1479 // Both have a definite exception spec. Collect the first set, then compare
1480 // to the second.
1481 llvm::SmallPtrSet<const Type*, 8> Types;
1482 for (FunctionProtoType::exception_iterator I = Old->exception_begin(),
1483 E = Old->exception_end(); I != E; ++I)
1484 Types.insert(Context.getCanonicalType(*I).getTypePtr());
1485
1486 for (FunctionProtoType::exception_iterator I = New->exception_begin(),
1487 E = New->exception_end(); I != E && Success; ++I)
1488 Success = Types.erase(Context.getCanonicalType(*I).getTypePtr());
1489
1490 Success = Success && Types.empty();
1491
1492 if (Success) {
1493 return false;
1494 }
1495 Diag(NewLoc, diag::err_mismatched_exception_spec);
1496 Diag(OldLoc, diag::note_previous_declaration);
1497 return true;
1498}
1499
Sebastian Redl23c7d062009-07-07 20:29:57 +00001500/// CheckExceptionSpecSubset - Check whether the second function type's
1501/// exception specification is a subset (or equivalent) of the first function
1502/// type. This is used by override and pointer assignment checks.
1503bool Sema::CheckExceptionSpecSubset(unsigned DiagID, unsigned NoteID,
1504 const FunctionProtoType *Superset, SourceLocation SuperLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001505 const FunctionProtoType *Subset, SourceLocation SubLoc) {
Sebastian Redl23c7d062009-07-07 20:29:57 +00001506 // FIXME: As usual, we could be more specific in our error messages, but
1507 // that better waits until we've got types with source locations.
1508
1509 // If superset contains everything, we're done.
1510 if (!Superset->hasExceptionSpec() || Superset->hasAnyExceptionSpec())
1511 return false;
1512
1513 // It does not. If the subset contains everything, we've failed.
1514 if (!Subset->hasExceptionSpec() || Subset->hasAnyExceptionSpec()) {
1515 Diag(SubLoc, DiagID);
1516 Diag(SuperLoc, NoteID);
1517 return true;
1518 }
1519
1520 // Neither contains everything. Do a proper comparison.
1521 for (FunctionProtoType::exception_iterator SubI = Subset->exception_begin(),
1522 SubE = Subset->exception_end(); SubI != SubE; ++SubI) {
1523 // Take one type from the subset.
1524 QualType CanonicalSubT = Context.getCanonicalType(*SubI);
1525 bool SubIsPointer = false;
Ted Kremenek6217b802009-07-29 21:53:49 +00001526 if (const ReferenceType *RefTy = CanonicalSubT->getAs<ReferenceType>())
Sebastian Redl23c7d062009-07-07 20:29:57 +00001527 CanonicalSubT = RefTy->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001528 if (const PointerType *PtrTy = CanonicalSubT->getAs<PointerType>()) {
Sebastian Redl23c7d062009-07-07 20:29:57 +00001529 CanonicalSubT = PtrTy->getPointeeType();
1530 SubIsPointer = true;
1531 }
1532 bool SubIsClass = CanonicalSubT->isRecordType();
John McCall0953e762009-09-24 19:53:00 +00001533 CanonicalSubT = CanonicalSubT.getUnqualifiedType();
Sebastian Redl23c7d062009-07-07 20:29:57 +00001534
Douglas Gregora8f32e02009-10-06 17:59:45 +00001535 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
1536 /*DetectVirtual=*/false);
Sebastian Redl23c7d062009-07-07 20:29:57 +00001537
1538 bool Contained = false;
1539 // Make sure it's in the superset.
1540 for (FunctionProtoType::exception_iterator SuperI =
1541 Superset->exception_begin(), SuperE = Superset->exception_end();
1542 SuperI != SuperE; ++SuperI) {
1543 QualType CanonicalSuperT = Context.getCanonicalType(*SuperI);
1544 // SubT must be SuperT or derived from it, or pointer or reference to
1545 // such types.
Ted Kremenek6217b802009-07-29 21:53:49 +00001546 if (const ReferenceType *RefTy = CanonicalSuperT->getAs<ReferenceType>())
Sebastian Redl23c7d062009-07-07 20:29:57 +00001547 CanonicalSuperT = RefTy->getPointeeType();
1548 if (SubIsPointer) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001549 if (const PointerType *PtrTy = CanonicalSuperT->getAs<PointerType>())
Sebastian Redl23c7d062009-07-07 20:29:57 +00001550 CanonicalSuperT = PtrTy->getPointeeType();
1551 else {
1552 continue;
1553 }
1554 }
John McCall0953e762009-09-24 19:53:00 +00001555 CanonicalSuperT = CanonicalSuperT.getUnqualifiedType();
Sebastian Redl23c7d062009-07-07 20:29:57 +00001556 // If the types are the same, move on to the next type in the subset.
1557 if (CanonicalSubT == CanonicalSuperT) {
1558 Contained = true;
1559 break;
1560 }
1561
1562 // Otherwise we need to check the inheritance.
1563 if (!SubIsClass || !CanonicalSuperT->isRecordType())
1564 continue;
1565
1566 Paths.clear();
1567 if (!IsDerivedFrom(CanonicalSubT, CanonicalSuperT, Paths))
1568 continue;
1569
1570 if (Paths.isAmbiguous(CanonicalSuperT))
1571 continue;
1572
Sebastian Redl726212f2009-07-18 14:32:15 +00001573 if (FindInaccessibleBase(CanonicalSubT, CanonicalSuperT, Paths, true))
1574 continue;
Sebastian Redl23c7d062009-07-07 20:29:57 +00001575
1576 Contained = true;
1577 break;
1578 }
1579 if (!Contained) {
1580 Diag(SubLoc, DiagID);
1581 Diag(SuperLoc, NoteID);
1582 return true;
1583 }
1584 }
1585 // We've run the gauntlet.
1586 return false;
1587}
1588
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001589/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
Fariborz Jahanian360300c2007-11-09 22:27:59 +00001590/// declarator
Chris Lattnerb28317a2009-03-28 19:18:32 +00001591QualType Sema::ObjCGetTypeForMethodDefinition(DeclPtrTy D) {
1592 ObjCMethodDecl *MDecl = cast<ObjCMethodDecl>(D.getAs<Decl>());
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001593 QualType T = MDecl->getResultType();
1594 llvm::SmallVector<QualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +00001595
Fariborz Jahanian35600022007-11-09 17:18:29 +00001596 // Add the first two invisible argument types for self and _cmd.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001597 if (MDecl->isInstanceMethod()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001598 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +00001599 selfTy = Context.getPointerType(selfTy);
1600 ArgTys.push_back(selfTy);
Chris Lattner89951a82009-02-20 18:43:26 +00001601 } else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001602 ArgTys.push_back(Context.getObjCIdType());
1603 ArgTys.push_back(Context.getObjCSelType());
Mike Stump1eb44332009-09-09 15:08:12 +00001604
Chris Lattner89951a82009-02-20 18:43:26 +00001605 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
1606 E = MDecl->param_end(); PI != E; ++PI) {
1607 QualType ArgTy = (*PI)->getType();
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001608 assert(!ArgTy.isNull() && "Couldn't parse type?");
Douglas Gregorbeb58cb2009-03-23 23:17:00 +00001609 ArgTy = adjustParameterType(ArgTy);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001610 ArgTys.push_back(ArgTy);
1611 }
1612 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001613 MDecl->isVariadic(), 0);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001614 return T;
1615}
1616
Sebastian Redl9e5e4aa2009-01-26 19:54:48 +00001617/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that
1618/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
1619/// they point to and return true. If T1 and T2 aren't pointer types
1620/// or pointer-to-member types, or if they are not similar at this
1621/// level, returns false and leaves T1 and T2 unchanged. Top-level
1622/// qualifiers on T1 and T2 are ignored. This function will typically
1623/// be called in a loop that successively "unwraps" pointer and
1624/// pointer-to-member types to compare them at each level.
Chris Lattnerecb81f22009-02-16 21:43:00 +00001625bool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001626 const PointerType *T1PtrType = T1->getAs<PointerType>(),
1627 *T2PtrType = T2->getAs<PointerType>();
Douglas Gregor57373262008-10-22 14:17:15 +00001628 if (T1PtrType && T2PtrType) {
1629 T1 = T1PtrType->getPointeeType();
1630 T2 = T2PtrType->getPointeeType();
1631 return true;
1632 }
1633
Ted Kremenek6217b802009-07-29 21:53:49 +00001634 const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(),
1635 *T2MPType = T2->getAs<MemberPointerType>();
Sebastian Redl21593ac2009-01-28 18:33:18 +00001636 if (T1MPType && T2MPType &&
1637 Context.getCanonicalType(T1MPType->getClass()) ==
1638 Context.getCanonicalType(T2MPType->getClass())) {
Sebastian Redl4433aaf2009-01-25 19:43:20 +00001639 T1 = T1MPType->getPointeeType();
1640 T2 = T2MPType->getPointeeType();
1641 return true;
1642 }
Douglas Gregor57373262008-10-22 14:17:15 +00001643 return false;
1644}
1645
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001646Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001647 // C99 6.7.6: Type names have no identifier. This is already validated by
1648 // the parser.
1649 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00001650
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001651 DeclaratorInfo *DInfo = 0;
Douglas Gregor402abb52009-05-28 23:31:59 +00001652 TagDecl *OwnedTag = 0;
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001653 QualType T = GetTypeForDeclarator(D, S, &DInfo, /*Skip=*/0, &OwnedTag);
Chris Lattner5153ee62009-04-25 08:47:54 +00001654 if (D.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +00001655 return true;
Steve Naroff5912a352007-08-28 20:14:24 +00001656
Douglas Gregor402abb52009-05-28 23:31:59 +00001657 if (getLangOptions().CPlusPlus) {
1658 // Check that there are no default arguments (C++ only).
Douglas Gregor6d6eb572008-05-07 04:49:29 +00001659 CheckExtraCXXDefaultArguments(D);
1660
Douglas Gregor402abb52009-05-28 23:31:59 +00001661 // C++0x [dcl.type]p3:
1662 // A type-specifier-seq shall not define a class or enumeration
1663 // unless it appears in the type-id of an alias-declaration
1664 // (7.1.3).
1665 if (OwnedTag && OwnedTag->isDefinition())
1666 Diag(OwnedTag->getLocation(), diag::err_type_defined_in_type_specifier)
1667 << Context.getTypeDeclType(OwnedTag);
1668 }
1669
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001670 if (DInfo)
1671 T = CreateLocInfoType(T, DInfo);
1672
Reid Spencer5f016e22007-07-11 17:01:13 +00001673 return T.getAsOpaquePtr();
1674}
1675
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001676
1677
1678//===----------------------------------------------------------------------===//
1679// Type Attribute Processing
1680//===----------------------------------------------------------------------===//
1681
1682/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
1683/// specified type. The attribute contains 1 argument, the id of the address
1684/// space for the type.
Mike Stump1eb44332009-09-09 15:08:12 +00001685static void HandleAddressSpaceTypeAttribute(QualType &Type,
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001686 const AttributeList &Attr, Sema &S){
John McCall0953e762009-09-24 19:53:00 +00001687
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001688 // If this type is already address space qualified, reject it.
1689 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
1690 // for two or more different address spaces."
1691 if (Type.getAddressSpace()) {
1692 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
1693 return;
1694 }
Mike Stump1eb44332009-09-09 15:08:12 +00001695
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001696 // Check the attribute arguments.
1697 if (Attr.getNumArgs() != 1) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +00001698 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001699 return;
1700 }
1701 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
1702 llvm::APSInt addrSpace(32);
1703 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001704 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
1705 << ASArgExpr->getSourceRange();
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001706 return;
1707 }
1708
John McCallefadb772009-07-28 06:52:18 +00001709 // Bounds checking.
1710 if (addrSpace.isSigned()) {
1711 if (addrSpace.isNegative()) {
1712 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative)
1713 << ASArgExpr->getSourceRange();
1714 return;
1715 }
1716 addrSpace.setIsSigned(false);
1717 }
1718 llvm::APSInt max(addrSpace.getBitWidth());
John McCall0953e762009-09-24 19:53:00 +00001719 max = Qualifiers::MaxAddressSpace;
John McCallefadb772009-07-28 06:52:18 +00001720 if (addrSpace > max) {
1721 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high)
John McCall0953e762009-09-24 19:53:00 +00001722 << Qualifiers::MaxAddressSpace << ASArgExpr->getSourceRange();
John McCallefadb772009-07-28 06:52:18 +00001723 return;
1724 }
1725
Mike Stump1eb44332009-09-09 15:08:12 +00001726 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001727 Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001728}
1729
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001730/// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the
1731/// specified type. The attribute contains 1 argument, weak or strong.
Mike Stump1eb44332009-09-09 15:08:12 +00001732static void HandleObjCGCTypeAttribute(QualType &Type,
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001733 const AttributeList &Attr, Sema &S) {
John McCall0953e762009-09-24 19:53:00 +00001734 if (Type.getObjCGCAttr() != Qualifiers::GCNone) {
Fariborz Jahanian5934e752009-02-18 18:52:41 +00001735 S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001736 return;
1737 }
Mike Stump1eb44332009-09-09 15:08:12 +00001738
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001739 // Check the attribute arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001740 if (!Attr.getParameterName()) {
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001741 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1742 << "objc_gc" << 1;
1743 return;
1744 }
John McCall0953e762009-09-24 19:53:00 +00001745 Qualifiers::GC GCAttr;
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001746 if (Attr.getNumArgs() != 0) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001747 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1748 return;
1749 }
Mike Stump1eb44332009-09-09 15:08:12 +00001750 if (Attr.getParameterName()->isStr("weak"))
John McCall0953e762009-09-24 19:53:00 +00001751 GCAttr = Qualifiers::Weak;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001752 else if (Attr.getParameterName()->isStr("strong"))
John McCall0953e762009-09-24 19:53:00 +00001753 GCAttr = Qualifiers::Strong;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001754 else {
1755 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1756 << "objc_gc" << Attr.getParameterName();
1757 return;
1758 }
Mike Stump1eb44332009-09-09 15:08:12 +00001759
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001760 Type = S.Context.getObjCGCQualType(Type, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001761}
1762
Mike Stump24556362009-07-25 21:26:53 +00001763/// HandleNoReturnTypeAttribute - Process the noreturn attribute on the
1764/// specified type. The attribute contains 0 arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001765static void HandleNoReturnTypeAttribute(QualType &Type,
Mike Stump24556362009-07-25 21:26:53 +00001766 const AttributeList &Attr, Sema &S) {
1767 if (Attr.getNumArgs() != 0)
1768 return;
1769
1770 // We only apply this to a pointer to function or a pointer to block.
1771 if (!Type->isFunctionPointerType()
1772 && !Type->isBlockPointerType()
1773 && !Type->isFunctionType())
1774 return;
1775
1776 Type = S.Context.getNoReturnType(Type);
1777}
1778
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001779void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) {
Chris Lattner232e8822008-02-21 01:08:11 +00001780 // Scan through and apply attributes to this type where it makes sense. Some
1781 // attributes (such as __address_space__, __vector_size__, etc) apply to the
1782 // type, but others can be present in the type specifiers even though they
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00001783 // apply to the decl. Here we apply type attributes and ignore the rest.
1784 for (; AL; AL = AL->getNext()) {
Chris Lattner232e8822008-02-21 01:08:11 +00001785 // If this is an attribute we can handle, do so now, otherwise, add it to
1786 // the LeftOverAttrs list for rechaining.
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00001787 switch (AL->getKind()) {
Chris Lattner232e8822008-02-21 01:08:11 +00001788 default: break;
1789 case AttributeList::AT_address_space:
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001790 HandleAddressSpaceTypeAttribute(Result, *AL, *this);
1791 break;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001792 case AttributeList::AT_objc_gc:
1793 HandleObjCGCTypeAttribute(Result, *AL, *this);
1794 break;
Mike Stump24556362009-07-25 21:26:53 +00001795 case AttributeList::AT_noreturn:
1796 HandleNoReturnTypeAttribute(Result, *AL, *this);
1797 break;
Chris Lattner232e8822008-02-21 01:08:11 +00001798 }
Chris Lattner232e8822008-02-21 01:08:11 +00001799 }
Chris Lattner232e8822008-02-21 01:08:11 +00001800}
1801
Mike Stump1eb44332009-09-09 15:08:12 +00001802/// @brief Ensure that the type T is a complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001803///
1804/// This routine checks whether the type @p T is complete in any
1805/// context where a complete type is required. If @p T is a complete
Douglas Gregor86447ec2009-03-09 16:13:40 +00001806/// type, returns false. If @p T is a class template specialization,
1807/// this routine then attempts to perform class template
1808/// instantiation. If instantiation fails, or if @p T is incomplete
1809/// and cannot be completed, issues the diagnostic @p diag (giving it
1810/// the type @p T) and returns true.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001811///
1812/// @param Loc The location in the source that the incomplete type
1813/// diagnostic should refer to.
1814///
1815/// @param T The type that this routine is examining for completeness.
1816///
Mike Stump1eb44332009-09-09 15:08:12 +00001817/// @param PD The partial diagnostic that will be printed out if T is not a
Anders Carlssonb7906612009-08-26 23:45:07 +00001818/// complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001819///
1820/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
1821/// @c false otherwise.
Anders Carlsson91a0cc92009-08-26 22:33:56 +00001822bool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
Anders Carlsson8c8d9192009-10-09 23:51:55 +00001823 const PartialDiagnostic &PD,
1824 std::pair<SourceLocation,
1825 PartialDiagnostic> Note) {
Anders Carlsson91a0cc92009-08-26 22:33:56 +00001826 unsigned diag = PD.getDiagID();
Mike Stump1eb44332009-09-09 15:08:12 +00001827
Douglas Gregor690dc7f2009-05-21 23:48:18 +00001828 // FIXME: Add this assertion to help us flush out problems with
1829 // checking for dependent types and type-dependent expressions.
1830 //
Mike Stump1eb44332009-09-09 15:08:12 +00001831 // assert(!T->isDependentType() &&
Douglas Gregor690dc7f2009-05-21 23:48:18 +00001832 // "Can't ask whether a dependent type is complete");
1833
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001834 // If we have a complete type, we're done.
1835 if (!T->isIncompleteType())
1836 return false;
Eli Friedman3c0eb162008-05-27 03:33:27 +00001837
Douglas Gregord475b8d2009-03-25 21:17:03 +00001838 // If we have a class template specialization or a class member of a
1839 // class template specialization, try to instantiate it.
Ted Kremenek6217b802009-07-29 21:53:49 +00001840 if (const RecordType *Record = T->getAs<RecordType>()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001841 if (ClassTemplateSpecializationDecl *ClassTemplateSpec
Douglas Gregord475b8d2009-03-25 21:17:03 +00001842 = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001843 if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001844 if (Loc.isValid())
John McCall9cc78072009-09-11 07:25:08 +00001845 ClassTemplateSpec->setPointOfInstantiation(Loc);
Douglas Gregor2943aed2009-03-03 04:44:36 +00001846 return InstantiateClassTemplateSpecialization(ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001847 TSK_ImplicitInstantiation,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001848 /*Complain=*/diag != 0);
Douglas Gregor2943aed2009-03-03 04:44:36 +00001849 }
Mike Stump1eb44332009-09-09 15:08:12 +00001850 } else if (CXXRecordDecl *Rec
Douglas Gregord475b8d2009-03-25 21:17:03 +00001851 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
1852 if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) {
Douglas Gregor357bbd02009-08-28 20:50:45 +00001853 // This record was instantiated from a class within a template.
Douglas Gregorf6b11852009-10-08 15:14:33 +00001854 if (Rec->getTemplateSpecializationKind() != TSK_ExplicitSpecialization)
1855 return InstantiateClass(Loc, Rec, Pattern,
1856 getTemplateInstantiationArgs(Rec),
1857 TSK_ImplicitInstantiation,
1858 /*Complain=*/diag != 0);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001859 }
1860 }
1861 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00001862
Douglas Gregor5842ba92009-08-24 15:23:48 +00001863 if (diag == 0)
1864 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001865
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001866 // We have an incomplete type. Produce a diagnostic.
Anders Carlsson91a0cc92009-08-26 22:33:56 +00001867 Diag(Loc, PD) << T;
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001868
Anders Carlsson8c8d9192009-10-09 23:51:55 +00001869 // If we have a note, produce it.
1870 if (!Note.first.isInvalid())
1871 Diag(Note.first, Note.second);
1872
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001873 // If the type was a forward declaration of a class/struct/union
Mike Stump1eb44332009-09-09 15:08:12 +00001874 // type, produce
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001875 const TagType *Tag = 0;
Ted Kremenek6217b802009-07-29 21:53:49 +00001876 if (const RecordType *Record = T->getAs<RecordType>())
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001877 Tag = Record;
John McCall183700f2009-09-21 23:43:11 +00001878 else if (const EnumType *Enum = T->getAs<EnumType>())
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001879 Tag = Enum;
1880
1881 if (Tag && !Tag->getDecl()->isInvalidDecl())
Mike Stump1eb44332009-09-09 15:08:12 +00001882 Diag(Tag->getDecl()->getLocation(),
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001883 Tag->isBeingDefined() ? diag::note_type_being_defined
1884 : diag::note_forward_declaration)
1885 << QualType(Tag, 0);
1886
1887 return true;
1888}
Douglas Gregore6258932009-03-19 00:39:20 +00001889
1890/// \brief Retrieve a version of the type 'T' that is qualified by the
1891/// nested-name-specifier contained in SS.
1892QualType Sema::getQualifiedNameType(const CXXScopeSpec &SS, QualType T) {
1893 if (!SS.isSet() || SS.isInvalid() || T.isNull())
1894 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00001895
Douglas Gregorab452ba2009-03-26 23:50:42 +00001896 NestedNameSpecifier *NNS
Douglas Gregor35073692009-03-26 23:56:24 +00001897 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Douglas Gregorab452ba2009-03-26 23:50:42 +00001898 return Context.getQualifiedNameType(NNS, T);
Douglas Gregore6258932009-03-19 00:39:20 +00001899}
Anders Carlssonaf017e62009-06-29 22:58:55 +00001900
1901QualType Sema::BuildTypeofExprType(Expr *E) {
1902 return Context.getTypeOfExprType(E);
1903}
1904
1905QualType Sema::BuildDecltypeType(Expr *E) {
1906 if (E->getType() == Context.OverloadTy) {
Mike Stump1eb44332009-09-09 15:08:12 +00001907 Diag(E->getLocStart(),
Anders Carlssonaf017e62009-06-29 22:58:55 +00001908 diag::err_cannot_determine_declared_type_of_overloaded_function);
1909 return QualType();
1910 }
1911 return Context.getDecltypeType(E);
1912}