blob: 99b2a51ce72b16b3c8fa0e34ceaf66099b58bea9 [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());
John McCall6e247262009-10-10 05:48:19 +0000197 if (!D) {
198 // This can happen in C++ with ambiguous lookups.
199 Result = Context.IntTy;
200 isInvalid = true;
201 break;
202 }
203
Reid Spencer5f016e22007-07-11 17:01:13 +0000204 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
205 DS.getTypeSpecSign() == 0 &&
206 "Can't handle qualifiers on typedef names yet!");
207 // TypeQuals handled by caller.
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000208 Result = Context.getTypeDeclType(cast<TypeDecl>(D));
John McCall2191b202009-09-05 06:31:47 +0000209
210 // In C++, make an ElaboratedType.
211 if (getLangOptions().CPlusPlus) {
212 TagDecl::TagKind Tag
213 = TagDecl::getTagKindForTypeSpec(DS.getTypeSpecType());
214 Result = Context.getElaboratedType(Result, Tag);
215 }
Mike Stump1eb44332009-09-09 15:08:12 +0000216
Chris Lattner5153ee62009-04-25 08:47:54 +0000217 if (D->isInvalidDecl())
218 isInvalid = true;
Chris Lattner958858e2008-02-20 21:40:32 +0000219 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000220 }
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000221 case DeclSpec::TST_typename: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000222 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
223 DS.getTypeSpecSign() == 0 &&
224 "Can't handle qualifiers on typedef names yet!");
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000225 Result = GetTypeFromParser(DS.getTypeRep());
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000226
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000227 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000228 SourceTy = Context.getObjCProtocolListType(Result,
229 (ObjCProtocolDecl**)PQ,
230 DS.getNumProtocolQualifiers());
231 if (const ObjCInterfaceType *
232 Interface = Result->getAs<ObjCInterfaceType>()) {
Steve Naroff67ef8ea2009-07-20 17:56:53 +0000233 // It would be nice if protocol qualifiers were only stored with the
234 // ObjCObjectPointerType. Unfortunately, this isn't possible due
235 // to the following typedef idiom (which is uncommon, but allowed):
Mike Stump1eb44332009-09-09 15:08:12 +0000236 //
Steve Naroff67ef8ea2009-07-20 17:56:53 +0000237 // typedef Foo<P> T;
238 // static void func() {
239 // Foo<P> *yy;
240 // T *zz;
241 // }
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000242 Result = Context.getObjCInterfaceType(Interface->getDecl(),
243 (ObjCProtocolDecl**)PQ,
244 DS.getNumProtocolQualifiers());
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000245 } else if (Result->isObjCIdType())
Chris Lattnerae4da612008-07-26 01:53:50 +0000246 // id<protocol-list>
Mike Stump1eb44332009-09-09 15:08:12 +0000247 Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy,
Steve Naroff14108da2009-07-10 23:34:53 +0000248 (ObjCProtocolDecl**)PQ, DS.getNumProtocolQualifiers());
249 else if (Result->isObjCClassType()) {
Chris Lattner3f84ad22009-04-22 05:27:59 +0000250 if (DeclLoc.isInvalid())
251 DeclLoc = DS.getSourceRange().getBegin();
Steve Naroff4262a072009-02-23 18:53:24 +0000252 // Class<protocol-list>
Mike Stump1eb44332009-09-09 15:08:12 +0000253 Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinClassTy,
Steve Naroff470301b2009-07-22 16:07:01 +0000254 (ObjCProtocolDecl**)PQ, DS.getNumProtocolQualifiers());
Chris Lattner3f84ad22009-04-22 05:27:59 +0000255 } else {
256 if (DeclLoc.isInvalid())
257 DeclLoc = DS.getSourceRange().getBegin();
258 Diag(DeclLoc, diag::err_invalid_protocol_qualifiers)
259 << DS.getSourceRange();
Chris Lattnereaaebc72009-04-25 08:06:05 +0000260 isInvalid = true;
Chris Lattner3f84ad22009-04-22 05:27:59 +0000261 }
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000262 }
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Chris Lattnereaaebc72009-04-25 08:06:05 +0000264 // If this is a reference to an invalid typedef, propagate the invalidity.
265 if (TypedefType *TDT = dyn_cast<TypedefType>(Result))
266 if (TDT->getDecl()->isInvalidDecl())
267 isInvalid = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000268
Reid Spencer5f016e22007-07-11 17:01:13 +0000269 // TypeQuals handled by caller.
Chris Lattner958858e2008-02-20 21:40:32 +0000270 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000271 }
Chris Lattner958858e2008-02-20 21:40:32 +0000272 case DeclSpec::TST_typeofType:
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000273 // FIXME: Preserve type source info.
274 Result = GetTypeFromParser(DS.getTypeRep());
Chris Lattner958858e2008-02-20 21:40:32 +0000275 assert(!Result.isNull() && "Didn't get a type for typeof?");
Steve Naroffd1861fd2007-07-31 12:34:36 +0000276 // TypeQuals handled by caller.
Chris Lattnerfab5b452008-02-20 23:53:49 +0000277 Result = Context.getTypeOfType(Result);
Chris Lattner958858e2008-02-20 21:40:32 +0000278 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000279 case DeclSpec::TST_typeofExpr: {
280 Expr *E = static_cast<Expr *>(DS.getTypeRep());
281 assert(E && "Didn't get an expression for typeof?");
282 // TypeQuals handled by caller.
Douglas Gregor72564e72009-02-26 23:50:07 +0000283 Result = Context.getTypeOfExprType(E);
Chris Lattner958858e2008-02-20 21:40:32 +0000284 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000285 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000286 case DeclSpec::TST_decltype: {
287 Expr *E = static_cast<Expr *>(DS.getTypeRep());
288 assert(E && "Didn't get an expression for decltype?");
289 // TypeQuals handled by caller.
Anders Carlssonaf017e62009-06-29 22:58:55 +0000290 Result = BuildDecltypeType(E);
291 if (Result.isNull()) {
292 Result = Context.IntTy;
293 isInvalid = true;
294 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000295 break;
296 }
Anders Carlssone89d1592009-06-26 18:41:36 +0000297 case DeclSpec::TST_auto: {
298 // TypeQuals handled by caller.
299 Result = Context.UndeducedAutoTy;
300 break;
301 }
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Douglas Gregor809070a2009-02-18 17:45:20 +0000303 case DeclSpec::TST_error:
Chris Lattner5153ee62009-04-25 08:47:54 +0000304 Result = Context.IntTy;
305 isInvalid = true;
306 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000307 }
Mike Stump1eb44332009-09-09 15:08:12 +0000308
Chris Lattner958858e2008-02-20 21:40:32 +0000309 // Handle complex types.
Douglas Gregorf244cd72009-02-14 21:06:05 +0000310 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
311 if (getLangOptions().Freestanding)
312 Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
Chris Lattnerfab5b452008-02-20 23:53:49 +0000313 Result = Context.getComplexType(Result);
Douglas Gregorf244cd72009-02-14 21:06:05 +0000314 }
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Chris Lattner958858e2008-02-20 21:40:32 +0000316 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
317 "FIXME: imaginary types not supported yet!");
Mike Stump1eb44332009-09-09 15:08:12 +0000318
Chris Lattner38d8b982008-02-20 22:04:11 +0000319 // See if there are any attributes on the declspec that apply to the type (as
320 // opposed to the decl).
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000321 if (const AttributeList *AL = DS.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000322 ProcessTypeAttributeList(Result, AL);
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Chris Lattner96b77fc2008-04-02 06:50:17 +0000324 // Apply const/volatile/restrict qualifiers to T.
325 if (unsigned TypeQuals = DS.getTypeQualifiers()) {
326
327 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
328 // or incomplete types shall not be restrict-qualified." C++ also allows
329 // restrict-qualified references.
John McCall0953e762009-09-24 19:53:00 +0000330 if (TypeQuals & DeclSpec::TQ_restrict) {
Daniel Dunbarbb710012009-02-26 19:13:44 +0000331 if (Result->isPointerType() || Result->isReferenceType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000332 QualType EltTy = Result->isPointerType() ?
Ted Kremenek6217b802009-07-29 21:53:49 +0000333 Result->getAs<PointerType>()->getPointeeType() :
334 Result->getAs<ReferenceType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Douglas Gregorbad0e652009-03-24 20:32:41 +0000336 // If we have a pointer or reference, the pointee must have an object
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000337 // incomplete type.
338 if (!EltTy->isIncompleteOrObjectType()) {
339 Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000340 diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattnerd1625842008-11-24 06:25:27 +0000341 << EltTy << DS.getSourceRange();
John McCall0953e762009-09-24 19:53:00 +0000342 TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000343 }
344 } else {
Chris Lattner96b77fc2008-04-02 06:50:17 +0000345 Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000346 diag::err_typecheck_invalid_restrict_not_pointer)
Chris Lattnerd1625842008-11-24 06:25:27 +0000347 << Result << DS.getSourceRange();
John McCall0953e762009-09-24 19:53:00 +0000348 TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
Chris Lattner96b77fc2008-04-02 06:50:17 +0000349 }
Chris Lattner96b77fc2008-04-02 06:50:17 +0000350 }
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Chris Lattner96b77fc2008-04-02 06:50:17 +0000352 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
353 // of a function type includes any type qualifiers, the behavior is
354 // undefined."
355 if (Result->isFunctionType() && TypeQuals) {
356 // Get some location to point at, either the C or V location.
357 SourceLocation Loc;
John McCall0953e762009-09-24 19:53:00 +0000358 if (TypeQuals & DeclSpec::TQ_const)
Chris Lattner96b77fc2008-04-02 06:50:17 +0000359 Loc = DS.getConstSpecLoc();
John McCall0953e762009-09-24 19:53:00 +0000360 else if (TypeQuals & DeclSpec::TQ_volatile)
Chris Lattner96b77fc2008-04-02 06:50:17 +0000361 Loc = DS.getVolatileSpecLoc();
John McCall0953e762009-09-24 19:53:00 +0000362 else {
363 assert((TypeQuals & DeclSpec::TQ_restrict) &&
364 "Has CVR quals but not C, V, or R?");
365 Loc = DS.getRestrictSpecLoc();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000366 }
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000367 Diag(Loc, diag::warn_typecheck_function_qualifiers)
Chris Lattnerd1625842008-11-24 06:25:27 +0000368 << Result << DS.getSourceRange();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000369 }
Mike Stump1eb44332009-09-09 15:08:12 +0000370
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000371 // C++ [dcl.ref]p1:
372 // Cv-qualified references are ill-formed except when the
373 // cv-qualifiers are introduced through the use of a typedef
374 // (7.1.3) or of a template type argument (14.3), in which
375 // case the cv-qualifiers are ignored.
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000376 // FIXME: Shouldn't we be checking SCS_typedef here?
377 if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000378 TypeQuals && Result->isReferenceType()) {
John McCall0953e762009-09-24 19:53:00 +0000379 TypeQuals &= ~DeclSpec::TQ_const;
380 TypeQuals &= ~DeclSpec::TQ_volatile;
Mike Stump1eb44332009-09-09 15:08:12 +0000381 }
382
John McCall0953e762009-09-24 19:53:00 +0000383 Qualifiers Quals = Qualifiers::fromCVRMask(TypeQuals);
384 Result = Context.getQualifiedType(Result, Quals);
Chris Lattner96b77fc2008-04-02 06:50:17 +0000385 }
John McCall0953e762009-09-24 19:53:00 +0000386
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000387 if (SourceTy.isNull())
388 SourceTy = Result;
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000389 return Result;
390}
391
Douglas Gregorcd281c32009-02-28 00:25:32 +0000392static std::string getPrintableNameForEntity(DeclarationName Entity) {
393 if (Entity)
394 return Entity.getAsString();
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Douglas Gregorcd281c32009-02-28 00:25:32 +0000396 return "type name";
397}
398
399/// \brief Build a pointer type.
400///
401/// \param T The type to which we'll be building a pointer.
402///
403/// \param Quals The cvr-qualifiers to be applied to the pointer type.
404///
405/// \param Loc The location of the entity whose type involves this
406/// pointer type or, if there is no such entity, the location of the
407/// type that will have pointer type.
408///
409/// \param Entity The name of the entity that involves the pointer
410/// type, if known.
411///
412/// \returns A suitable pointer type, if there are no
413/// errors. Otherwise, returns a NULL type.
Mike Stump1eb44332009-09-09 15:08:12 +0000414QualType Sema::BuildPointerType(QualType T, unsigned Quals,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000415 SourceLocation Loc, DeclarationName Entity) {
416 if (T->isReferenceType()) {
417 // C++ 8.3.2p4: There shall be no ... pointers to references ...
418 Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
419 << getPrintableNameForEntity(Entity);
420 return QualType();
421 }
422
John McCall0953e762009-09-24 19:53:00 +0000423 Qualifiers Qs = Qualifiers::fromCVRMask(Quals);
424
Douglas Gregorcd281c32009-02-28 00:25:32 +0000425 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
426 // object or incomplete types shall not be restrict-qualified."
John McCall0953e762009-09-24 19:53:00 +0000427 if (Qs.hasRestrict() && !T->isIncompleteOrObjectType()) {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000428 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
429 << T;
John McCall0953e762009-09-24 19:53:00 +0000430 Qs.removeRestrict();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000431 }
432
433 // Build the pointer type.
John McCall0953e762009-09-24 19:53:00 +0000434 return Context.getQualifiedType(Context.getPointerType(T), Qs);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000435}
436
437/// \brief Build a reference type.
438///
439/// \param T The type to which we'll be building a reference.
440///
John McCall0953e762009-09-24 19:53:00 +0000441/// \param CVR The cvr-qualifiers to be applied to the reference type.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000442///
443/// \param Loc The location of the entity whose type involves this
444/// reference type or, if there is no such entity, the location of the
445/// type that will have reference type.
446///
447/// \param Entity The name of the entity that involves the reference
448/// type, if known.
449///
450/// \returns A suitable reference type, if there are no
451/// errors. Otherwise, returns a NULL type.
John McCall0953e762009-09-24 19:53:00 +0000452QualType Sema::BuildReferenceType(QualType T, bool LValueRef, unsigned CVR,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000453 SourceLocation Loc, DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000454 Qualifiers Quals = Qualifiers::fromCVRMask(CVR);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000455 if (LValueRef) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000456 if (const RValueReferenceType *R = T->getAs<RValueReferenceType>()) {
Sebastian Redldfe292d2009-03-22 21:28:55 +0000457 // C++0x [dcl.typedef]p9: If a typedef TD names a type that is a
458 // reference to a type T, and attempt to create the type "lvalue
459 // reference to cv TD" creates the type "lvalue reference to T".
460 // We use the qualifiers (restrict or none) of the original reference,
461 // not the new ones. This is consistent with GCC.
John McCall0953e762009-09-24 19:53:00 +0000462 QualType LVRT = Context.getLValueReferenceType(R->getPointeeType());
463 return Context.getQualifiedType(LVRT, T.getQualifiers());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000464 }
465 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000466 if (T->isReferenceType()) {
467 // C++ [dcl.ref]p4: There shall be no references to references.
Mike Stump1eb44332009-09-09 15:08:12 +0000468 //
Douglas Gregorcd281c32009-02-28 00:25:32 +0000469 // According to C++ DR 106, references to references are only
470 // diagnosed when they are written directly (e.g., "int & &"),
471 // but not when they happen via a typedef:
472 //
473 // typedef int& intref;
474 // typedef intref& intref2;
475 //
John McCall0953e762009-09-24 19:53:00 +0000476 // Parser::ParseDeclaratorInternal diagnoses the case where
Douglas Gregorcd281c32009-02-28 00:25:32 +0000477 // references are written directly; here, we handle the
478 // collapsing of references-to-references as described in C++
479 // DR 106 and amended by C++ DR 540.
480 return T;
481 }
482
483 // C++ [dcl.ref]p1:
Eli Friedman33a31382009-08-05 19:21:58 +0000484 // A declarator that specifies the type "reference to cv void"
Douglas Gregorcd281c32009-02-28 00:25:32 +0000485 // is ill-formed.
486 if (T->isVoidType()) {
487 Diag(Loc, diag::err_reference_to_void);
488 return QualType();
489 }
490
491 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
492 // object or incomplete types shall not be restrict-qualified."
John McCall0953e762009-09-24 19:53:00 +0000493 if (Quals.hasRestrict() && !T->isIncompleteOrObjectType()) {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000494 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
495 << T;
John McCall0953e762009-09-24 19:53:00 +0000496 Quals.removeRestrict();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000497 }
498
499 // C++ [dcl.ref]p1:
500 // [...] Cv-qualified references are ill-formed except when the
501 // cv-qualifiers are introduced through the use of a typedef
502 // (7.1.3) or of a template type argument (14.3), in which case
503 // the cv-qualifiers are ignored.
504 //
505 // We diagnose extraneous cv-qualifiers for the non-typedef,
506 // non-template type argument case within the parser. Here, we just
507 // ignore any extraneous cv-qualifiers.
John McCall0953e762009-09-24 19:53:00 +0000508 Quals.removeConst();
509 Quals.removeVolatile();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000510
511 // Handle restrict on references.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000512 if (LValueRef)
John McCall0953e762009-09-24 19:53:00 +0000513 return Context.getQualifiedType(Context.getLValueReferenceType(T), Quals);
514 return Context.getQualifiedType(Context.getRValueReferenceType(T), Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000515}
516
517/// \brief Build an array type.
518///
519/// \param T The type of each element in the array.
520///
521/// \param ASM C99 array size modifier (e.g., '*', 'static').
Mike Stump1eb44332009-09-09 15:08:12 +0000522///
523/// \param ArraySize Expression describing the size of the array.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000524///
525/// \param Quals The cvr-qualifiers to be applied to the array's
526/// element type.
527///
528/// \param Loc The location of the entity whose type involves this
529/// array type or, if there is no such entity, the location of the
530/// type that will have array type.
531///
532/// \param Entity The name of the entity that involves the array
533/// type, if known.
534///
535/// \returns A suitable array type, if there are no errors. Otherwise,
536/// returns a NULL type.
537QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
538 Expr *ArraySize, unsigned Quals,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000539 SourceRange Brackets, DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000540
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000541 SourceLocation Loc = Brackets.getBegin();
Mike Stump1eb44332009-09-09 15:08:12 +0000542 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000543 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
Mike Stump1eb44332009-09-09 15:08:12 +0000544 if (RequireCompleteType(Loc, T,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000545 diag::err_illegal_decl_array_incomplete_type))
546 return QualType();
547
548 if (T->isFunctionType()) {
549 Diag(Loc, diag::err_illegal_decl_array_of_functions)
550 << getPrintableNameForEntity(Entity);
551 return QualType();
552 }
Mike Stump1eb44332009-09-09 15:08:12 +0000553
Douglas Gregorcd281c32009-02-28 00:25:32 +0000554 // C++ 8.3.2p4: There shall be no ... arrays of references ...
555 if (T->isReferenceType()) {
556 Diag(Loc, diag::err_illegal_decl_array_of_references)
557 << getPrintableNameForEntity(Entity);
558 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000559 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000560
Anders Carlssone7cf07d2009-06-26 19:33:28 +0000561 if (Context.getCanonicalType(T) == Context.UndeducedAutoTy) {
Mike Stump1eb44332009-09-09 15:08:12 +0000562 Diag(Loc, diag::err_illegal_decl_array_of_auto)
Anders Carlssone7cf07d2009-06-26 19:33:28 +0000563 << getPrintableNameForEntity(Entity);
564 return QualType();
565 }
Mike Stump1eb44332009-09-09 15:08:12 +0000566
Ted Kremenek6217b802009-07-29 21:53:49 +0000567 if (const RecordType *EltTy = T->getAs<RecordType>()) {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000568 // If the element type is a struct or union that contains a variadic
569 // array, accept it as a GNU extension: C99 6.7.2.1p2.
570 if (EltTy->getDecl()->hasFlexibleArrayMember())
571 Diag(Loc, diag::ext_flexible_array_in_array) << T;
572 } else if (T->isObjCInterfaceType()) {
Chris Lattnerc7c11b12009-04-27 01:55:56 +0000573 Diag(Loc, diag::err_objc_array_of_interfaces) << T;
574 return QualType();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000575 }
Mike Stump1eb44332009-09-09 15:08:12 +0000576
Douglas Gregorcd281c32009-02-28 00:25:32 +0000577 // C99 6.7.5.2p1: The size expression shall have integer type.
578 if (ArraySize && !ArraySize->isTypeDependent() &&
579 !ArraySize->getType()->isIntegerType()) {
580 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
581 << ArraySize->getType() << ArraySize->getSourceRange();
582 ArraySize->Destroy(Context);
583 return QualType();
584 }
585 llvm::APSInt ConstVal(32);
586 if (!ArraySize) {
Eli Friedmanf91f5c82009-04-26 21:57:51 +0000587 if (ASM == ArrayType::Star)
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000588 T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets);
Eli Friedmanf91f5c82009-04-26 21:57:51 +0000589 else
590 T = Context.getIncompleteArrayType(T, ASM, Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000591 } else if (ArraySize->isValueDependent()) {
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000592 T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000593 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
594 (!T->isDependentType() && !T->isConstantSizeType())) {
595 // Per C99, a variable array is an array with either a non-constant
596 // size or an element type that has a non-constant-size
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000597 T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000598 } else {
599 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
600 // have a value greater than zero.
601 if (ConstVal.isSigned()) {
602 if (ConstVal.isNegative()) {
603 Diag(ArraySize->getLocStart(),
604 diag::err_typecheck_negative_array_size)
605 << ArraySize->getSourceRange();
606 return QualType();
607 } else if (ConstVal == 0) {
608 // GCC accepts zero sized static arrays.
609 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size)
610 << ArraySize->getSourceRange();
611 }
Mike Stump1eb44332009-09-09 15:08:12 +0000612 }
John McCall46a617a2009-10-16 00:14:28 +0000613 T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000614 }
615 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
616 if (!getLangOptions().C99) {
Mike Stump1eb44332009-09-09 15:08:12 +0000617 if (ArraySize && !ArraySize->isTypeDependent() &&
618 !ArraySize->isValueDependent() &&
Douglas Gregorcd281c32009-02-28 00:25:32 +0000619 !ArraySize->isIntegerConstantExpr(Context))
Douglas Gregor043cad22009-09-11 00:18:58 +0000620 Diag(Loc, getLangOptions().CPlusPlus? diag::err_vla_cxx : diag::ext_vla);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000621 else if (ASM != ArrayType::Normal || Quals != 0)
Douglas Gregor043cad22009-09-11 00:18:58 +0000622 Diag(Loc,
623 getLangOptions().CPlusPlus? diag::err_c99_array_usage_cxx
624 : diag::ext_c99_array_usage);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000625 }
626
627 return T;
628}
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000629
630/// \brief Build an ext-vector type.
631///
632/// Run the required checks for the extended vector type.
Mike Stump1eb44332009-09-09 15:08:12 +0000633QualType Sema::BuildExtVectorType(QualType T, ExprArg ArraySize,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000634 SourceLocation AttrLoc) {
635
636 Expr *Arg = (Expr *)ArraySize.get();
637
638 // unlike gcc's vector_size attribute, we do not allow vectors to be defined
639 // in conjunction with complex types (pointers, arrays, functions, etc.).
Mike Stump1eb44332009-09-09 15:08:12 +0000640 if (!T->isDependentType() &&
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000641 !T->isIntegerType() && !T->isRealFloatingType()) {
642 Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T;
643 return QualType();
644 }
645
646 if (!Arg->isTypeDependent() && !Arg->isValueDependent()) {
647 llvm::APSInt vecSize(32);
648 if (!Arg->isIntegerConstantExpr(vecSize, Context)) {
649 Diag(AttrLoc, diag::err_attribute_argument_not_int)
650 << "ext_vector_type" << Arg->getSourceRange();
651 return QualType();
652 }
Mike Stump1eb44332009-09-09 15:08:12 +0000653
654 // unlike gcc's vector_size attribute, the size is specified as the
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000655 // number of elements, not the number of bytes.
Mike Stump1eb44332009-09-09 15:08:12 +0000656 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
657
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000658 if (vectorSize == 0) {
659 Diag(AttrLoc, diag::err_attribute_zero_size)
660 << Arg->getSourceRange();
661 return QualType();
662 }
Mike Stump1eb44332009-09-09 15:08:12 +0000663
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000664 if (!T->isDependentType())
665 return Context.getExtVectorType(T, vectorSize);
Mike Stump1eb44332009-09-09 15:08:12 +0000666 }
667
668 return Context.getDependentSizedExtVectorType(T, ArraySize.takeAs<Expr>(),
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000669 AttrLoc);
670}
Mike Stump1eb44332009-09-09 15:08:12 +0000671
Douglas Gregor724651c2009-02-28 01:04:19 +0000672/// \brief Build a function type.
673///
674/// This routine checks the function type according to C++ rules and
675/// under the assumption that the result type and parameter types have
676/// just been instantiated from a template. It therefore duplicates
Douglas Gregor2943aed2009-03-03 04:44:36 +0000677/// some of the behavior of GetTypeForDeclarator, but in a much
Douglas Gregor724651c2009-02-28 01:04:19 +0000678/// simpler form that is only suitable for this narrow use case.
679///
680/// \param T The return type of the function.
681///
682/// \param ParamTypes The parameter types of the function. This array
683/// will be modified to account for adjustments to the types of the
684/// function parameters.
685///
686/// \param NumParamTypes The number of parameter types in ParamTypes.
687///
688/// \param Variadic Whether this is a variadic function type.
689///
690/// \param Quals The cvr-qualifiers to be applied to the function type.
691///
692/// \param Loc The location of the entity whose type involves this
693/// function type or, if there is no such entity, the location of the
694/// type that will have function type.
695///
696/// \param Entity The name of the entity that involves the function
697/// type, if known.
698///
699/// \returns A suitable function type, if there are no
700/// errors. Otherwise, returns a NULL type.
701QualType Sema::BuildFunctionType(QualType T,
Mike Stump1eb44332009-09-09 15:08:12 +0000702 QualType *ParamTypes,
Douglas Gregor724651c2009-02-28 01:04:19 +0000703 unsigned NumParamTypes,
704 bool Variadic, unsigned Quals,
705 SourceLocation Loc, DeclarationName Entity) {
706 if (T->isArrayType() || T->isFunctionType()) {
707 Diag(Loc, diag::err_func_returning_array_function) << T;
708 return QualType();
709 }
Mike Stump1eb44332009-09-09 15:08:12 +0000710
Douglas Gregor724651c2009-02-28 01:04:19 +0000711 bool Invalid = false;
712 for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) {
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000713 QualType ParamType = adjustParameterType(ParamTypes[Idx]);
714 if (ParamType->isVoidType()) {
Douglas Gregor724651c2009-02-28 01:04:19 +0000715 Diag(Loc, diag::err_param_with_void_type);
716 Invalid = true;
717 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000718
Anders Carlsson83913e32009-09-16 23:47:08 +0000719 ParamTypes[Idx] = adjustFunctionParamType(ParamType);
Douglas Gregor724651c2009-02-28 01:04:19 +0000720 }
721
722 if (Invalid)
723 return QualType();
724
Mike Stump1eb44332009-09-09 15:08:12 +0000725 return Context.getFunctionType(T, ParamTypes, NumParamTypes, Variadic,
Douglas Gregor724651c2009-02-28 01:04:19 +0000726 Quals);
727}
Mike Stump1eb44332009-09-09 15:08:12 +0000728
Douglas Gregor949bf692009-06-09 22:17:39 +0000729/// \brief Build a member pointer type \c T Class::*.
730///
731/// \param T the type to which the member pointer refers.
732/// \param Class the class type into which the member pointer points.
John McCall0953e762009-09-24 19:53:00 +0000733/// \param CVR Qualifiers applied to the member pointer type
Douglas Gregor949bf692009-06-09 22:17:39 +0000734/// \param Loc the location where this type begins
735/// \param Entity the name of the entity that will have this member pointer type
736///
737/// \returns a member pointer type, if successful, or a NULL type if there was
738/// an error.
Mike Stump1eb44332009-09-09 15:08:12 +0000739QualType Sema::BuildMemberPointerType(QualType T, QualType Class,
John McCall0953e762009-09-24 19:53:00 +0000740 unsigned CVR, SourceLocation Loc,
Douglas Gregor949bf692009-06-09 22:17:39 +0000741 DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000742 Qualifiers Quals = Qualifiers::fromCVRMask(CVR);
743
Douglas Gregor949bf692009-06-09 22:17:39 +0000744 // Verify that we're not building a pointer to pointer to function with
745 // exception specification.
746 if (CheckDistantExceptionSpec(T)) {
747 Diag(Loc, diag::err_distant_exception_spec);
748
749 // FIXME: If we're doing this as part of template instantiation,
750 // we should return immediately.
751
752 // Build the type anyway, but use the canonical type so that the
753 // exception specifiers are stripped off.
754 T = Context.getCanonicalType(T);
755 }
756
757 // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member
758 // with reference type, or "cv void."
759 if (T->isReferenceType()) {
Anders Carlsson8d4655d2009-06-30 00:06:57 +0000760 Diag(Loc, diag::err_illegal_decl_mempointer_to_reference)
Douglas Gregor949bf692009-06-09 22:17:39 +0000761 << (Entity? Entity.getAsString() : "type name");
762 return QualType();
763 }
764
765 if (T->isVoidType()) {
766 Diag(Loc, diag::err_illegal_decl_mempointer_to_void)
767 << (Entity? Entity.getAsString() : "type name");
768 return QualType();
769 }
770
771 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
772 // object or incomplete types shall not be restrict-qualified."
John McCall0953e762009-09-24 19:53:00 +0000773 if (Quals.hasRestrict() && !T->isIncompleteOrObjectType()) {
Douglas Gregor949bf692009-06-09 22:17:39 +0000774 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
775 << T;
776
777 // FIXME: If we're doing this as part of template instantiation,
778 // we should return immediately.
John McCall0953e762009-09-24 19:53:00 +0000779 Quals.removeRestrict();
Douglas Gregor949bf692009-06-09 22:17:39 +0000780 }
781
782 if (!Class->isDependentType() && !Class->isRecordType()) {
783 Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class;
784 return QualType();
785 }
786
John McCall0953e762009-09-24 19:53:00 +0000787 return Context.getQualifiedType(
788 Context.getMemberPointerType(T, Class.getTypePtr()), Quals);
Douglas Gregor949bf692009-06-09 22:17:39 +0000789}
Mike Stump1eb44332009-09-09 15:08:12 +0000790
Anders Carlsson9a917e42009-06-12 22:56:54 +0000791/// \brief Build a block pointer type.
792///
793/// \param T The type to which we'll be building a block pointer.
794///
John McCall0953e762009-09-24 19:53:00 +0000795/// \param CVR The cvr-qualifiers to be applied to the block pointer type.
Anders Carlsson9a917e42009-06-12 22:56:54 +0000796///
797/// \param Loc The location of the entity whose type involves this
798/// block pointer type or, if there is no such entity, the location of the
799/// type that will have block pointer type.
800///
801/// \param Entity The name of the entity that involves the block pointer
802/// type, if known.
803///
804/// \returns A suitable block pointer type, if there are no
805/// errors. Otherwise, returns a NULL type.
John McCall0953e762009-09-24 19:53:00 +0000806QualType Sema::BuildBlockPointerType(QualType T, unsigned CVR,
Mike Stump1eb44332009-09-09 15:08:12 +0000807 SourceLocation Loc,
Anders Carlsson9a917e42009-06-12 22:56:54 +0000808 DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000809 if (!T->isFunctionType()) {
Anders Carlsson9a917e42009-06-12 22:56:54 +0000810 Diag(Loc, diag::err_nonfunction_block_type);
811 return QualType();
812 }
Mike Stump1eb44332009-09-09 15:08:12 +0000813
John McCall0953e762009-09-24 19:53:00 +0000814 Qualifiers Quals = Qualifiers::fromCVRMask(CVR);
815 return Context.getQualifiedType(Context.getBlockPointerType(T), Quals);
Anders Carlsson9a917e42009-06-12 22:56:54 +0000816}
817
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000818QualType Sema::GetTypeFromParser(TypeTy *Ty, DeclaratorInfo **DInfo) {
819 QualType QT = QualType::getFromOpaquePtr(Ty);
820 DeclaratorInfo *DI = 0;
821 if (LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) {
822 QT = LIT->getType();
823 DI = LIT->getDeclaratorInfo();
824 }
Mike Stump1eb44332009-09-09 15:08:12 +0000825
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000826 if (DInfo) *DInfo = DI;
827 return QT;
828}
829
Mike Stump98eb8a72009-02-04 22:31:32 +0000830/// GetTypeForDeclarator - Convert the type for the specified
831/// declarator to Type instances. Skip the outermost Skip type
832/// objects.
Douglas Gregor402abb52009-05-28 23:31:59 +0000833///
834/// If OwnedDecl is non-NULL, and this declarator's decl-specifier-seq
835/// owns the declaration of a type (e.g., the definition of a struct
836/// type), then *OwnedDecl will receive the owned declaration.
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000837QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S,
838 DeclaratorInfo **DInfo, unsigned Skip,
Douglas Gregor402abb52009-05-28 23:31:59 +0000839 TagDecl **OwnedDecl) {
Mike Stump98eb8a72009-02-04 22:31:32 +0000840 bool OmittedReturnType = false;
841
842 if (D.getContext() == Declarator::BlockLiteralContext
843 && Skip == 0
844 && !D.getDeclSpec().hasTypeSpecifier()
845 && (D.getNumTypeObjects() == 0
846 || (D.getNumTypeObjects() == 1
847 && D.getTypeObject(0).Kind == DeclaratorChunk::Function)))
848 OmittedReturnType = true;
849
Chris Lattnerb23deda2007-08-28 16:40:32 +0000850 // long long is a C99 feature.
Chris Lattnerd1eb3322007-08-28 16:41:29 +0000851 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Chris Lattnerb23deda2007-08-28 16:40:32 +0000852 D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
853 Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
Douglas Gregor930d8b52009-01-30 22:09:00 +0000854
855 // Determine the type of the declarator. Not all forms of declarator
856 // have a type.
857 QualType T;
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000858 // The QualType referring to the type as written in source code. We can't use
859 // T because it can change due to semantic analysis.
860 QualType SourceTy;
861
Douglas Gregor930d8b52009-01-30 22:09:00 +0000862 switch (D.getKind()) {
863 case Declarator::DK_Abstract:
864 case Declarator::DK_Normal:
Douglas Gregordb422df2009-09-25 21:45:23 +0000865 case Declarator::DK_Operator:
866 case Declarator::DK_TemplateId: {
Chris Lattner3f84ad22009-04-22 05:27:59 +0000867 const DeclSpec &DS = D.getDeclSpec();
868 if (OmittedReturnType) {
Mike Stump98eb8a72009-02-04 22:31:32 +0000869 // We default to a dependent type initially. Can be modified by
870 // the first return statement.
871 T = Context.DependentTy;
Chris Lattner3f84ad22009-04-22 05:27:59 +0000872 } else {
Chris Lattnereaaebc72009-04-25 08:06:05 +0000873 bool isInvalid = false;
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000874 T = ConvertDeclSpecToType(DS, D.getIdentifierLoc(), isInvalid, SourceTy);
Chris Lattnereaaebc72009-04-25 08:06:05 +0000875 if (isInvalid)
876 D.setInvalidType(true);
Douglas Gregor402abb52009-05-28 23:31:59 +0000877 else if (OwnedDecl && DS.isTypeSpecOwned())
878 *OwnedDecl = cast<TagDecl>((Decl *)DS.getTypeRep());
Douglas Gregor809070a2009-02-18 17:45:20 +0000879 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000880 break;
Mike Stump98eb8a72009-02-04 22:31:32 +0000881 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000882
883 case Declarator::DK_Constructor:
884 case Declarator::DK_Destructor:
885 case Declarator::DK_Conversion:
886 // Constructors and destructors don't have return types. Use
887 // "void" instead. Conversion operators will check their return
888 // types separately.
889 T = Context.VoidTy;
890 break;
891 }
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000892
893 if (SourceTy.isNull())
894 SourceTy = T;
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000895
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000896 if (T == Context.UndeducedAutoTy) {
897 int Error = -1;
Mike Stump1eb44332009-09-09 15:08:12 +0000898
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000899 switch (D.getContext()) {
900 case Declarator::KNRTypeListContext:
901 assert(0 && "K&R type lists aren't allowed in C++");
902 break;
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000903 case Declarator::PrototypeContext:
904 Error = 0; // Function prototype
905 break;
906 case Declarator::MemberContext:
907 switch (cast<TagDecl>(CurContext)->getTagKind()) {
908 case TagDecl::TK_enum: assert(0 && "unhandled tag kind"); break;
909 case TagDecl::TK_struct: Error = 1; /* Struct member */ break;
910 case TagDecl::TK_union: Error = 2; /* Union member */ break;
911 case TagDecl::TK_class: Error = 3; /* Class member */ break;
Mike Stump1eb44332009-09-09 15:08:12 +0000912 }
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000913 break;
914 case Declarator::CXXCatchContext:
915 Error = 4; // Exception declaration
916 break;
917 case Declarator::TemplateParamContext:
918 Error = 5; // Template parameter
919 break;
920 case Declarator::BlockLiteralContext:
921 Error = 6; // Block literal
922 break;
923 case Declarator::FileContext:
924 case Declarator::BlockContext:
925 case Declarator::ForContext:
926 case Declarator::ConditionContext:
927 case Declarator::TypeNameContext:
928 break;
929 }
930
931 if (Error != -1) {
932 Diag(D.getDeclSpec().getTypeSpecTypeLoc(), diag::err_auto_not_allowed)
933 << Error;
934 T = Context.IntTy;
935 D.setInvalidType(true);
936 }
937 }
Mike Stump1eb44332009-09-09 15:08:12 +0000938
Douglas Gregorcd281c32009-02-28 00:25:32 +0000939 // The name we're declaring, if any.
940 DeclarationName Name;
941 if (D.getIdentifier())
942 Name = D.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +0000943
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +0000944 bool ShouldBuildInfo = DInfo != 0;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000945
Mike Stump98eb8a72009-02-04 22:31:32 +0000946 // Walk the DeclTypeInfo, building the recursive type as we go.
947 // DeclTypeInfos are ordered from the identifier out, which is
948 // opposite of what we want :).
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000949 for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) {
950 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1+Skip);
Reid Spencer5f016e22007-07-11 17:01:13 +0000951 switch (DeclType.Kind) {
952 default: assert(0 && "Unknown decltype!");
Steve Naroff5618bd42008-08-27 16:04:49 +0000953 case DeclaratorChunk::BlockPointer:
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +0000954 if (ShouldBuildInfo) {
955 if (SourceTy->isFunctionType())
John McCall0953e762009-09-24 19:53:00 +0000956 SourceTy
957 = Context.getQualifiedType(Context.getBlockPointerType(SourceTy),
958 Qualifiers::fromCVRMask(DeclType.Cls.TypeQuals));
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +0000959 else
960 // If not function type Context::getBlockPointerType asserts,
961 // so just give up.
962 ShouldBuildInfo = false;
963 }
964
Chris Lattner9af55002009-03-27 04:18:06 +0000965 // If blocks are disabled, emit an error.
966 if (!LangOpts.Blocks)
967 Diag(DeclType.Loc, diag::err_blocks_disable);
Mike Stump1eb44332009-09-09 15:08:12 +0000968
969 T = BuildBlockPointerType(T, DeclType.Cls.TypeQuals, D.getIdentifierLoc(),
Anders Carlsson9a917e42009-06-12 22:56:54 +0000970 Name);
Steve Naroff5618bd42008-08-27 16:04:49 +0000971 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000972 case DeclaratorChunk::Pointer:
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +0000973 //FIXME: Use ObjCObjectPointer for info when appropriate.
974 if (ShouldBuildInfo)
John McCall0953e762009-09-24 19:53:00 +0000975 SourceTy = Context.getQualifiedType(Context.getPointerType(SourceTy),
976 Qualifiers::fromCVRMask(DeclType.Ptr.TypeQuals));
Sebastian Redl6a7330c2009-05-29 15:01:05 +0000977 // Verify that we're not building a pointer to pointer to function with
978 // exception specification.
979 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
980 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
981 D.setInvalidType(true);
982 // Build the type anyway.
983 }
Steve Naroff14108da2009-07-10 23:34:53 +0000984 if (getLangOptions().ObjC1 && T->isObjCInterfaceType()) {
John McCall183700f2009-09-21 23:43:11 +0000985 const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>();
Steve Naroff14108da2009-07-10 23:34:53 +0000986 T = Context.getObjCObjectPointerType(T,
Steve Naroff67ef8ea2009-07-20 17:56:53 +0000987 (ObjCProtocolDecl **)OIT->qual_begin(),
988 OIT->getNumProtocols());
Steve Naroff14108da2009-07-10 23:34:53 +0000989 break;
990 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000991 T = BuildPointerType(T, DeclType.Ptr.TypeQuals, DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000992 break;
John McCall0953e762009-09-24 19:53:00 +0000993 case DeclaratorChunk::Reference: {
994 Qualifiers Quals;
995 if (DeclType.Ref.HasRestrict) Quals.addRestrict();
996
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +0000997 if (ShouldBuildInfo) {
998 if (DeclType.Ref.LValueRef)
999 SourceTy = Context.getLValueReferenceType(SourceTy);
1000 else
1001 SourceTy = Context.getRValueReferenceType(SourceTy);
John McCall0953e762009-09-24 19:53:00 +00001002 SourceTy = Context.getQualifiedType(SourceTy, Quals);
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001003 }
1004
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001005 // Verify that we're not building a reference to pointer to function with
1006 // exception specification.
1007 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1008 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1009 D.setInvalidType(true);
1010 // Build the type anyway.
1011 }
John McCall0953e762009-09-24 19:53:00 +00001012 T = BuildReferenceType(T, DeclType.Ref.LValueRef, Quals,
Douglas Gregorcd281c32009-02-28 00:25:32 +00001013 DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +00001014 break;
John McCall0953e762009-09-24 19:53:00 +00001015 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001016 case DeclaratorChunk::Array: {
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001017 if (ShouldBuildInfo)
1018 // We just need to get an array type, the exact type doesn't matter.
1019 SourceTy = Context.getIncompleteArrayType(SourceTy, ArrayType::Normal,
John McCall0953e762009-09-24 19:53:00 +00001020 DeclType.Arr.TypeQuals);
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001021
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001022 // Verify that we're not building an array of pointers to function with
1023 // exception specification.
1024 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1025 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1026 D.setInvalidType(true);
1027 // Build the type anyway.
1028 }
Chris Lattnerfd89bc82008-04-02 01:05:10 +00001029 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner94f81fd2007-08-28 16:54:00 +00001030 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001031 ArrayType::ArraySizeModifier ASM;
1032 if (ATI.isStar)
1033 ASM = ArrayType::Star;
1034 else if (ATI.hasStatic)
1035 ASM = ArrayType::Static;
1036 else
1037 ASM = ArrayType::Normal;
Eli Friedmanf91f5c82009-04-26 21:57:51 +00001038 if (ASM == ArrayType::Star &&
1039 D.getContext() != Declarator::PrototypeContext) {
1040 // FIXME: This check isn't quite right: it allows star in prototypes
1041 // for function definitions, and disallows some edge cases detailed
1042 // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html
1043 Diag(DeclType.Loc, diag::err_array_star_outside_prototype);
1044 ASM = ArrayType::Normal;
1045 D.setInvalidType(true);
1046 }
John McCall0953e762009-09-24 19:53:00 +00001047 T = BuildArrayType(T, ASM, ArraySize,
1048 Qualifiers::fromCVRMask(ATI.TypeQuals),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001049 SourceRange(DeclType.Loc, DeclType.EndLoc), Name);
Reid Spencer5f016e22007-07-11 17:01:13 +00001050 break;
1051 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001052 case DeclaratorChunk::Function: {
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001053 if (ShouldBuildInfo) {
1054 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
1055 llvm::SmallVector<QualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +00001056
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001057 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
1058 ParmVarDecl *Param = FTI.ArgInfo[i].Param.getAs<ParmVarDecl>();
Anders Carlsson83913e32009-09-16 23:47:08 +00001059 if (Param) {
1060 QualType ArgTy = adjustFunctionParamType(Param->getType());
1061
1062 ArgTys.push_back(ArgTy);
1063 }
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001064 }
1065 SourceTy = Context.getFunctionType(SourceTy, ArgTys.data(),
1066 ArgTys.size(),
John McCall0953e762009-09-24 19:53:00 +00001067 FTI.isVariadic,
1068 FTI.TypeQuals);
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001069 }
1070
Reid Spencer5f016e22007-07-11 17:01:13 +00001071 // If the function declarator has a prototype (i.e. it is not () and
1072 // does not have a K&R-style identifier list), then the arguments are part
1073 // of the type, otherwise the argument list is ().
1074 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Sebastian Redl3cc97262009-05-31 11:47:27 +00001075
Chris Lattnercd881292007-12-19 05:31:29 +00001076 // C99 6.7.5.3p1: The return type may not be a function or array type.
Chris Lattner68cfd492007-12-19 18:01:43 +00001077 if (T->isArrayType() || T->isFunctionType()) {
Chris Lattnerd1625842008-11-24 06:25:27 +00001078 Diag(DeclType.Loc, diag::err_func_returning_array_function) << T;
Chris Lattnercd881292007-12-19 05:31:29 +00001079 T = Context.IntTy;
1080 D.setInvalidType(true);
1081 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001082
Douglas Gregor402abb52009-05-28 23:31:59 +00001083 if (getLangOptions().CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) {
1084 // C++ [dcl.fct]p6:
1085 // Types shall not be defined in return or parameter types.
1086 TagDecl *Tag = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep());
1087 if (Tag->isDefinition())
1088 Diag(Tag->getLocation(), diag::err_type_defined_in_result_type)
1089 << Context.getTypeDeclType(Tag);
1090 }
1091
Sebastian Redl3cc97262009-05-31 11:47:27 +00001092 // Exception specs are not allowed in typedefs. Complain, but add it
1093 // anyway.
1094 if (FTI.hasExceptionSpec &&
1095 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
1096 Diag(FTI.getThrowLoc(), diag::err_exception_spec_in_typedef);
1097
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001098 if (FTI.NumArgs == 0) {
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001099 if (getLangOptions().CPlusPlus) {
1100 // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the
1101 // function takes no arguments.
Sebastian Redl465226e2009-05-27 22:11:52 +00001102 llvm::SmallVector<QualType, 4> Exceptions;
1103 Exceptions.reserve(FTI.NumExceptions);
Mike Stump1eb44332009-09-09 15:08:12 +00001104 for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001105 // FIXME: Preserve type source info.
1106 QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty);
Sebastian Redlef65f062009-05-29 18:02:33 +00001107 // Check that the type is valid for an exception spec, and drop it
1108 // if not.
1109 if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1110 Exceptions.push_back(ET);
1111 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001112 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, FTI.TypeQuals,
1113 FTI.hasExceptionSpec,
1114 FTI.hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00001115 Exceptions.size(), Exceptions.data());
Douglas Gregor965acbb2009-02-18 07:07:28 +00001116 } else if (FTI.isVariadic) {
1117 // We allow a zero-parameter variadic function in C if the
1118 // function is marked with the "overloadable"
1119 // attribute. Scan for this attribute now.
1120 bool Overloadable = false;
1121 for (const AttributeList *Attrs = D.getAttributes();
1122 Attrs; Attrs = Attrs->getNext()) {
1123 if (Attrs->getKind() == AttributeList::AT_overloadable) {
1124 Overloadable = true;
1125 break;
1126 }
1127 }
1128
1129 if (!Overloadable)
1130 Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
1131 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, 0);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001132 } else {
1133 // Simple void foo(), where the incoming T is the result type.
Douglas Gregor72564e72009-02-26 23:50:07 +00001134 T = Context.getFunctionNoProtoType(T);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001135 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001136 } else if (FTI.ArgInfo[0].Param == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001137 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
Mike Stump1eb44332009-09-09 15:08:12 +00001138 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
Reid Spencer5f016e22007-07-11 17:01:13 +00001139 } else {
1140 // Otherwise, we have a function with an argument list that is
1141 // potentially variadic.
1142 llvm::SmallVector<QualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +00001143
Reid Spencer5f016e22007-07-11 17:01:13 +00001144 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001145 ParmVarDecl *Param =
1146 cast<ParmVarDecl>(FTI.ArgInfo[i].Param.getAs<Decl>());
Chris Lattner8123a952008-04-10 02:22:51 +00001147 QualType ArgTy = Param->getType();
Chris Lattner78c75fb2007-07-21 05:30:18 +00001148 assert(!ArgTy.isNull() && "Couldn't parse type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001149
1150 // Adjust the parameter type.
Douglas Gregorbeb58cb2009-03-23 23:17:00 +00001151 assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001152
Reid Spencer5f016e22007-07-11 17:01:13 +00001153 // Look for 'void'. void is allowed only as a single argument to a
1154 // function with no other parameters (C99 6.7.5.3p10). We record
Douglas Gregor72564e72009-02-26 23:50:07 +00001155 // int(void) as a FunctionProtoType with an empty argument list.
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001156 if (ArgTy->isVoidType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001157 // If this is something like 'float(int, void)', reject it. 'void'
1158 // is an incomplete type (C99 6.2.5p19) and function decls cannot
1159 // have arguments of incomplete type.
1160 if (FTI.NumArgs != 1 || FTI.isVariadic) {
1161 Diag(DeclType.Loc, diag::err_void_only_param);
Chris Lattner2ff54262007-07-21 05:18:12 +00001162 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00001163 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00001164 } else if (FTI.ArgInfo[i].Ident) {
1165 // Reject, but continue to parse 'int(void abc)'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001166 Diag(FTI.ArgInfo[i].IdentLoc,
Chris Lattner4565d4e2007-07-21 05:26:43 +00001167 diag::err_param_with_void_type);
Chris Lattner2ff54262007-07-21 05:18:12 +00001168 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00001169 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00001170 } else {
1171 // Reject, but continue to parse 'float(const void)'.
John McCall0953e762009-09-24 19:53:00 +00001172 if (ArgTy.hasQualifiers())
Chris Lattner2ff54262007-07-21 05:18:12 +00001173 Diag(DeclType.Loc, diag::err_void_param_qualified);
Mike Stump1eb44332009-09-09 15:08:12 +00001174
Chris Lattner2ff54262007-07-21 05:18:12 +00001175 // Do not add 'void' to the ArgTys list.
1176 break;
1177 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001178 } else if (!FTI.hasPrototype) {
1179 if (ArgTy->isPromotableIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00001180 ArgTy = Context.getPromotedIntegerType(ArgTy);
John McCall183700f2009-09-21 23:43:11 +00001181 } else if (const BuiltinType* BTy = ArgTy->getAs<BuiltinType>()) {
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001182 if (BTy->getKind() == BuiltinType::Float)
1183 ArgTy = Context.DoubleTy;
1184 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001185 }
Mike Stump1eb44332009-09-09 15:08:12 +00001186
Anders Carlsson83913e32009-09-16 23:47:08 +00001187 ArgTys.push_back(adjustFunctionParamType(ArgTy));
Reid Spencer5f016e22007-07-11 17:01:13 +00001188 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001189
1190 llvm::SmallVector<QualType, 4> Exceptions;
1191 Exceptions.reserve(FTI.NumExceptions);
Mike Stump1eb44332009-09-09 15:08:12 +00001192 for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001193 // FIXME: Preserve type source info.
1194 QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty);
Sebastian Redlef65f062009-05-29 18:02:33 +00001195 // Check that the type is valid for an exception spec, and drop it if
1196 // not.
1197 if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1198 Exceptions.push_back(ET);
1199 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001200
Jay Foadbeaaccd2009-05-21 09:52:38 +00001201 T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(),
Sebastian Redl465226e2009-05-27 22:11:52 +00001202 FTI.isVariadic, FTI.TypeQuals,
1203 FTI.hasExceptionSpec,
1204 FTI.hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00001205 Exceptions.size(), Exceptions.data());
Reid Spencer5f016e22007-07-11 17:01:13 +00001206 }
1207 break;
1208 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001209 case DeclaratorChunk::MemberPointer:
Sebastian Redl4994d2d2009-07-04 11:39:00 +00001210 // Verify that we're not building a pointer to pointer to function with
1211 // exception specification.
1212 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1213 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1214 D.setInvalidType(true);
1215 // Build the type anyway.
1216 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001217 // The scope spec must refer to a class, or be dependent.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001218 QualType ClsType;
Douglas Gregor949bf692009-06-09 22:17:39 +00001219 if (isDependentScopeSpecifier(DeclType.Mem.Scope())) {
Mike Stump1eb44332009-09-09 15:08:12 +00001220 NestedNameSpecifier *NNS
Douglas Gregor949bf692009-06-09 22:17:39 +00001221 = (NestedNameSpecifier *)DeclType.Mem.Scope().getScopeRep();
1222 assert(NNS->getAsType() && "Nested-name-specifier must name a type");
1223 ClsType = QualType(NNS->getAsType(), 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001224 } else if (CXXRecordDecl *RD
Douglas Gregor949bf692009-06-09 22:17:39 +00001225 = dyn_cast_or_null<CXXRecordDecl>(
1226 computeDeclContext(DeclType.Mem.Scope()))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001227 ClsType = Context.getTagDeclType(RD);
1228 } else {
Douglas Gregor949bf692009-06-09 22:17:39 +00001229 Diag(DeclType.Mem.Scope().getBeginLoc(),
1230 diag::err_illegal_decl_mempointer_in_nonclass)
1231 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
1232 << DeclType.Mem.Scope().getRange();
Sebastian Redlf30208a2009-01-24 21:16:55 +00001233 D.setInvalidType(true);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001234 }
1235
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001236 if (ShouldBuildInfo) {
1237 QualType cls = !ClsType.isNull() ? ClsType : Context.IntTy;
John McCall0953e762009-09-24 19:53:00 +00001238 SourceTy = Context.getQualifiedType(
1239 Context.getMemberPointerType(SourceTy, cls.getTypePtr()),
1240 Qualifiers::fromCVRMask(DeclType.Mem.TypeQuals));
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001241 }
1242
Douglas Gregor949bf692009-06-09 22:17:39 +00001243 if (!ClsType.isNull())
1244 T = BuildMemberPointerType(T, ClsType, DeclType.Mem.TypeQuals,
1245 DeclType.Loc, D.getIdentifier());
1246 if (T.isNull()) {
1247 T = Context.IntTy;
Sebastian Redlf30208a2009-01-24 21:16:55 +00001248 D.setInvalidType(true);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001249 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001250 break;
1251 }
1252
Douglas Gregorcd281c32009-02-28 00:25:32 +00001253 if (T.isNull()) {
1254 D.setInvalidType(true);
1255 T = Context.IntTy;
1256 }
1257
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001258 // See if there are any attributes on this declarator chunk.
1259 if (const AttributeList *AL = DeclType.getAttrs())
1260 ProcessTypeAttributeList(T, AL);
Reid Spencer5f016e22007-07-11 17:01:13 +00001261 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001262
1263 if (getLangOptions().CPlusPlus && T->isFunctionType()) {
John McCall183700f2009-09-21 23:43:11 +00001264 const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00001265 assert(FnTy && "Why oh why is there not a FunctionProtoType here ?");
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001266
1267 // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type
1268 // for a nonstatic member function, the function type to which a pointer
1269 // to member refers, or the top-level function type of a function typedef
1270 // declaration.
1271 if (FnTy->getTypeQuals() != 0 &&
1272 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
Douglas Gregor584049d2008-12-15 23:53:10 +00001273 ((D.getContext() != Declarator::MemberContext &&
1274 (!D.getCXXScopeSpec().isSet() ||
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001275 !computeDeclContext(D.getCXXScopeSpec(), /*FIXME:*/true)
1276 ->isRecord())) ||
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001277 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001278 if (D.isFunctionDeclarator())
1279 Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
1280 else
1281 Diag(D.getIdentifierLoc(),
1282 diag::err_invalid_qualified_typedef_function_type_use);
1283
1284 // Strip the cv-quals from the type.
1285 T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001286 FnTy->getNumArgs(), FnTy->isVariadic(), 0);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001287 }
1288 }
Mike Stump1eb44332009-09-09 15:08:12 +00001289
Chris Lattner0bf29ad2008-06-29 00:19:33 +00001290 // If there were any type attributes applied to the decl itself (not the
1291 // type, apply the type attribute to the type!)
1292 if (const AttributeList *Attrs = D.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001293 ProcessTypeAttributeList(T, Attrs);
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001294
1295 if (ShouldBuildInfo)
1296 *DInfo = GetDeclaratorInfoForDeclarator(D, SourceTy, Skip);
1297
Reid Spencer5f016e22007-07-11 17:01:13 +00001298 return T;
1299}
1300
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001301static void FillTypeSpecLoc(TypeLoc TSL, const DeclSpec &DS) {
1302 if (TSL.isNull()) return;
1303
1304 if (TypedefLoc *TL = dyn_cast<TypedefLoc>(&TSL)) {
1305 TL->setNameLoc(DS.getTypeSpecTypeLoc());
1306
Argyrios Kyrtzidiseb667592009-09-29 19:45:22 +00001307 } else if (ObjCInterfaceLoc *TL = dyn_cast<ObjCInterfaceLoc>(&TSL)) {
1308 TL->setNameLoc(DS.getTypeSpecTypeLoc());
1309
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001310 } else if (ObjCProtocolListLoc *PLL = dyn_cast<ObjCProtocolListLoc>(&TSL)) {
1311 assert(PLL->getNumProtocols() == DS.getNumProtocolQualifiers());
1312 PLL->setLAngleLoc(DS.getProtocolLAngleLoc());
1313 PLL->setRAngleLoc(DS.getSourceRange().getEnd());
1314 for (unsigned i = 0; i != DS.getNumProtocolQualifiers(); ++i)
1315 PLL->setProtocolLoc(i, DS.getProtocolLocs()[i]);
1316 FillTypeSpecLoc(PLL->getBaseTypeLoc(), DS);
1317
1318 } else {
1319 //FIXME: Other typespecs.
1320 DefaultTypeSpecLoc &DTL = cast<DefaultTypeSpecLoc>(TSL);
1321 DTL.setStartLoc(DS.getSourceRange().getBegin());
1322 }
1323}
1324
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001325/// \brief Create and instantiate a DeclaratorInfo with type source information.
1326///
1327/// \param T QualType referring to the type as written in source code.
1328DeclaratorInfo *
1329Sema::GetDeclaratorInfoForDeclarator(Declarator &D, QualType T, unsigned Skip) {
1330 DeclaratorInfo *DInfo = Context.CreateDeclaratorInfo(T);
1331 TypeLoc CurrTL = DInfo->getTypeLoc();
1332
1333 for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) {
1334 assert(!CurrTL.isNull());
John McCall34a04472009-10-15 03:50:32 +00001335
1336 // Don't bother recording source locations for qualifiers.
1337 CurrTL = CurrTL.getUnqualifiedLoc();
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001338
1339 DeclaratorChunk &DeclType = D.getTypeObject(i);
1340 switch (DeclType.Kind) {
1341 default: assert(0 && "Unknown decltype!");
1342 case DeclaratorChunk::BlockPointer: {
1343 BlockPointerLoc &BPL = cast<BlockPointerLoc>(CurrTL);
1344 BPL.setCaretLoc(DeclType.Loc);
1345 break;
1346 }
1347 case DeclaratorChunk::Pointer: {
1348 //FIXME: ObjCObject pointers.
1349 PointerLoc &PL = cast<PointerLoc>(CurrTL);
1350 PL.setStarLoc(DeclType.Loc);
1351 break;
1352 }
1353 case DeclaratorChunk::Reference: {
1354 ReferenceLoc &RL = cast<ReferenceLoc>(CurrTL);
1355 RL.setAmpLoc(DeclType.Loc);
1356 break;
1357 }
1358 case DeclaratorChunk::Array: {
1359 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
1360 ArrayLoc &AL = cast<ArrayLoc>(CurrTL);
1361 AL.setLBracketLoc(DeclType.Loc);
1362 AL.setRBracketLoc(DeclType.EndLoc);
1363 AL.setSizeExpr(static_cast<Expr*>(ATI.NumElts));
1364 //FIXME: Star location for [*].
1365 break;
1366 }
1367 case DeclaratorChunk::Function: {
1368 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
1369 FunctionLoc &FL = cast<FunctionLoc>(CurrTL);
1370 FL.setLParenLoc(DeclType.Loc);
1371 FL.setRParenLoc(DeclType.EndLoc);
1372 for (unsigned i = 0, e = FTI.NumArgs, tpi = 0; i != e; ++i) {
1373 ParmVarDecl *Param = FTI.ArgInfo[i].Param.getAs<ParmVarDecl>();
1374 if (Param) {
1375 assert(tpi < FL.getNumArgs());
1376 FL.setArg(tpi++, Param);
1377 }
1378 }
1379 break;
1380 //FIXME: Exception specs.
1381 }
1382 case DeclaratorChunk::MemberPointer: {
1383 MemberPointerLoc &MPL = cast<MemberPointerLoc>(CurrTL);
1384 MPL.setStarLoc(DeclType.Loc);
1385 //FIXME: Class location.
1386 break;
1387 }
Mike Stump1eb44332009-09-09 15:08:12 +00001388
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001389 }
1390
1391 CurrTL = CurrTL.getNextTypeLoc();
1392 }
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001393
1394 FillTypeSpecLoc(CurrTL, D.getDeclSpec());
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001395
1396 return DInfo;
1397}
1398
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001399/// \brief Create a LocInfoType to hold the given QualType and DeclaratorInfo.
1400QualType Sema::CreateLocInfoType(QualType T, DeclaratorInfo *DInfo) {
1401 // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser
1402 // and Sema during declaration parsing. Try deallocating/caching them when
1403 // it's appropriate, instead of allocating them and keeping them around.
1404 LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType), 8);
1405 new (LocT) LocInfoType(T, DInfo);
1406 assert(LocT->getTypeClass() != T->getTypeClass() &&
1407 "LocInfoType's TypeClass conflicts with an existing Type class");
1408 return QualType(LocT, 0);
1409}
1410
1411void LocInfoType::getAsStringInternal(std::string &Str,
1412 const PrintingPolicy &Policy) const {
Argyrios Kyrtzidis35d44e52009-08-19 01:46:06 +00001413 assert(false && "LocInfoType leaked into the type system; an opaque TypeTy*"
1414 " was used directly instead of getting the QualType through"
1415 " GetTypeFromParser");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001416}
1417
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001418/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
Fariborz Jahanian360300c2007-11-09 22:27:59 +00001419/// declarator
Chris Lattnerb28317a2009-03-28 19:18:32 +00001420QualType Sema::ObjCGetTypeForMethodDefinition(DeclPtrTy D) {
1421 ObjCMethodDecl *MDecl = cast<ObjCMethodDecl>(D.getAs<Decl>());
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001422 QualType T = MDecl->getResultType();
1423 llvm::SmallVector<QualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +00001424
Fariborz Jahanian35600022007-11-09 17:18:29 +00001425 // Add the first two invisible argument types for self and _cmd.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001426 if (MDecl->isInstanceMethod()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001427 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +00001428 selfTy = Context.getPointerType(selfTy);
1429 ArgTys.push_back(selfTy);
Chris Lattner89951a82009-02-20 18:43:26 +00001430 } else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001431 ArgTys.push_back(Context.getObjCIdType());
1432 ArgTys.push_back(Context.getObjCSelType());
Mike Stump1eb44332009-09-09 15:08:12 +00001433
Chris Lattner89951a82009-02-20 18:43:26 +00001434 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
1435 E = MDecl->param_end(); PI != E; ++PI) {
1436 QualType ArgTy = (*PI)->getType();
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001437 assert(!ArgTy.isNull() && "Couldn't parse type?");
Douglas Gregorbeb58cb2009-03-23 23:17:00 +00001438 ArgTy = adjustParameterType(ArgTy);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001439 ArgTys.push_back(ArgTy);
1440 }
1441 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001442 MDecl->isVariadic(), 0);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001443 return T;
1444}
1445
Sebastian Redl9e5e4aa2009-01-26 19:54:48 +00001446/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that
1447/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
1448/// they point to and return true. If T1 and T2 aren't pointer types
1449/// or pointer-to-member types, or if they are not similar at this
1450/// level, returns false and leaves T1 and T2 unchanged. Top-level
1451/// qualifiers on T1 and T2 are ignored. This function will typically
1452/// be called in a loop that successively "unwraps" pointer and
1453/// pointer-to-member types to compare them at each level.
Chris Lattnerecb81f22009-02-16 21:43:00 +00001454bool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001455 const PointerType *T1PtrType = T1->getAs<PointerType>(),
1456 *T2PtrType = T2->getAs<PointerType>();
Douglas Gregor57373262008-10-22 14:17:15 +00001457 if (T1PtrType && T2PtrType) {
1458 T1 = T1PtrType->getPointeeType();
1459 T2 = T2PtrType->getPointeeType();
1460 return true;
1461 }
1462
Ted Kremenek6217b802009-07-29 21:53:49 +00001463 const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(),
1464 *T2MPType = T2->getAs<MemberPointerType>();
Sebastian Redl21593ac2009-01-28 18:33:18 +00001465 if (T1MPType && T2MPType &&
1466 Context.getCanonicalType(T1MPType->getClass()) ==
1467 Context.getCanonicalType(T2MPType->getClass())) {
Sebastian Redl4433aaf2009-01-25 19:43:20 +00001468 T1 = T1MPType->getPointeeType();
1469 T2 = T2MPType->getPointeeType();
1470 return true;
1471 }
Douglas Gregor57373262008-10-22 14:17:15 +00001472 return false;
1473}
1474
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001475Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001476 // C99 6.7.6: Type names have no identifier. This is already validated by
1477 // the parser.
1478 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00001479
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001480 DeclaratorInfo *DInfo = 0;
Douglas Gregor402abb52009-05-28 23:31:59 +00001481 TagDecl *OwnedTag = 0;
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001482 QualType T = GetTypeForDeclarator(D, S, &DInfo, /*Skip=*/0, &OwnedTag);
Chris Lattner5153ee62009-04-25 08:47:54 +00001483 if (D.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +00001484 return true;
Steve Naroff5912a352007-08-28 20:14:24 +00001485
Douglas Gregor402abb52009-05-28 23:31:59 +00001486 if (getLangOptions().CPlusPlus) {
1487 // Check that there are no default arguments (C++ only).
Douglas Gregor6d6eb572008-05-07 04:49:29 +00001488 CheckExtraCXXDefaultArguments(D);
1489
Douglas Gregor402abb52009-05-28 23:31:59 +00001490 // C++0x [dcl.type]p3:
1491 // A type-specifier-seq shall not define a class or enumeration
1492 // unless it appears in the type-id of an alias-declaration
1493 // (7.1.3).
1494 if (OwnedTag && OwnedTag->isDefinition())
1495 Diag(OwnedTag->getLocation(), diag::err_type_defined_in_type_specifier)
1496 << Context.getTypeDeclType(OwnedTag);
1497 }
1498
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001499 if (DInfo)
1500 T = CreateLocInfoType(T, DInfo);
1501
Reid Spencer5f016e22007-07-11 17:01:13 +00001502 return T.getAsOpaquePtr();
1503}
1504
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001505
1506
1507//===----------------------------------------------------------------------===//
1508// Type Attribute Processing
1509//===----------------------------------------------------------------------===//
1510
1511/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
1512/// specified type. The attribute contains 1 argument, the id of the address
1513/// space for the type.
Mike Stump1eb44332009-09-09 15:08:12 +00001514static void HandleAddressSpaceTypeAttribute(QualType &Type,
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001515 const AttributeList &Attr, Sema &S){
John McCall0953e762009-09-24 19:53:00 +00001516
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001517 // If this type is already address space qualified, reject it.
1518 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
1519 // for two or more different address spaces."
1520 if (Type.getAddressSpace()) {
1521 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
1522 return;
1523 }
Mike Stump1eb44332009-09-09 15:08:12 +00001524
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001525 // Check the attribute arguments.
1526 if (Attr.getNumArgs() != 1) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +00001527 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001528 return;
1529 }
1530 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
1531 llvm::APSInt addrSpace(32);
1532 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001533 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
1534 << ASArgExpr->getSourceRange();
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001535 return;
1536 }
1537
John McCallefadb772009-07-28 06:52:18 +00001538 // Bounds checking.
1539 if (addrSpace.isSigned()) {
1540 if (addrSpace.isNegative()) {
1541 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative)
1542 << ASArgExpr->getSourceRange();
1543 return;
1544 }
1545 addrSpace.setIsSigned(false);
1546 }
1547 llvm::APSInt max(addrSpace.getBitWidth());
John McCall0953e762009-09-24 19:53:00 +00001548 max = Qualifiers::MaxAddressSpace;
John McCallefadb772009-07-28 06:52:18 +00001549 if (addrSpace > max) {
1550 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high)
John McCall0953e762009-09-24 19:53:00 +00001551 << Qualifiers::MaxAddressSpace << ASArgExpr->getSourceRange();
John McCallefadb772009-07-28 06:52:18 +00001552 return;
1553 }
1554
Mike Stump1eb44332009-09-09 15:08:12 +00001555 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001556 Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001557}
1558
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001559/// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the
1560/// specified type. The attribute contains 1 argument, weak or strong.
Mike Stump1eb44332009-09-09 15:08:12 +00001561static void HandleObjCGCTypeAttribute(QualType &Type,
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001562 const AttributeList &Attr, Sema &S) {
John McCall0953e762009-09-24 19:53:00 +00001563 if (Type.getObjCGCAttr() != Qualifiers::GCNone) {
Fariborz Jahanian5934e752009-02-18 18:52:41 +00001564 S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001565 return;
1566 }
Mike Stump1eb44332009-09-09 15:08:12 +00001567
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001568 // Check the attribute arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001569 if (!Attr.getParameterName()) {
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001570 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1571 << "objc_gc" << 1;
1572 return;
1573 }
John McCall0953e762009-09-24 19:53:00 +00001574 Qualifiers::GC GCAttr;
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001575 if (Attr.getNumArgs() != 0) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001576 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1577 return;
1578 }
Mike Stump1eb44332009-09-09 15:08:12 +00001579 if (Attr.getParameterName()->isStr("weak"))
John McCall0953e762009-09-24 19:53:00 +00001580 GCAttr = Qualifiers::Weak;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001581 else if (Attr.getParameterName()->isStr("strong"))
John McCall0953e762009-09-24 19:53:00 +00001582 GCAttr = Qualifiers::Strong;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001583 else {
1584 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1585 << "objc_gc" << Attr.getParameterName();
1586 return;
1587 }
Mike Stump1eb44332009-09-09 15:08:12 +00001588
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001589 Type = S.Context.getObjCGCQualType(Type, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001590}
1591
Mike Stump24556362009-07-25 21:26:53 +00001592/// HandleNoReturnTypeAttribute - Process the noreturn attribute on the
1593/// specified type. The attribute contains 0 arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001594static void HandleNoReturnTypeAttribute(QualType &Type,
Mike Stump24556362009-07-25 21:26:53 +00001595 const AttributeList &Attr, Sema &S) {
1596 if (Attr.getNumArgs() != 0)
1597 return;
1598
1599 // We only apply this to a pointer to function or a pointer to block.
1600 if (!Type->isFunctionPointerType()
1601 && !Type->isBlockPointerType()
1602 && !Type->isFunctionType())
1603 return;
1604
1605 Type = S.Context.getNoReturnType(Type);
1606}
1607
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001608void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) {
Chris Lattner232e8822008-02-21 01:08:11 +00001609 // Scan through and apply attributes to this type where it makes sense. Some
1610 // attributes (such as __address_space__, __vector_size__, etc) apply to the
1611 // type, but others can be present in the type specifiers even though they
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00001612 // apply to the decl. Here we apply type attributes and ignore the rest.
1613 for (; AL; AL = AL->getNext()) {
Chris Lattner232e8822008-02-21 01:08:11 +00001614 // If this is an attribute we can handle, do so now, otherwise, add it to
1615 // the LeftOverAttrs list for rechaining.
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00001616 switch (AL->getKind()) {
Chris Lattner232e8822008-02-21 01:08:11 +00001617 default: break;
1618 case AttributeList::AT_address_space:
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001619 HandleAddressSpaceTypeAttribute(Result, *AL, *this);
1620 break;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001621 case AttributeList::AT_objc_gc:
1622 HandleObjCGCTypeAttribute(Result, *AL, *this);
1623 break;
Mike Stump24556362009-07-25 21:26:53 +00001624 case AttributeList::AT_noreturn:
1625 HandleNoReturnTypeAttribute(Result, *AL, *this);
1626 break;
Chris Lattner232e8822008-02-21 01:08:11 +00001627 }
Chris Lattner232e8822008-02-21 01:08:11 +00001628 }
Chris Lattner232e8822008-02-21 01:08:11 +00001629}
1630
Mike Stump1eb44332009-09-09 15:08:12 +00001631/// @brief Ensure that the type T is a complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001632///
1633/// This routine checks whether the type @p T is complete in any
1634/// context where a complete type is required. If @p T is a complete
Douglas Gregor86447ec2009-03-09 16:13:40 +00001635/// type, returns false. If @p T is a class template specialization,
1636/// this routine then attempts to perform class template
1637/// instantiation. If instantiation fails, or if @p T is incomplete
1638/// and cannot be completed, issues the diagnostic @p diag (giving it
1639/// the type @p T) and returns true.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001640///
1641/// @param Loc The location in the source that the incomplete type
1642/// diagnostic should refer to.
1643///
1644/// @param T The type that this routine is examining for completeness.
1645///
Mike Stump1eb44332009-09-09 15:08:12 +00001646/// @param PD The partial diagnostic that will be printed out if T is not a
Anders Carlssonb7906612009-08-26 23:45:07 +00001647/// complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001648///
1649/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
1650/// @c false otherwise.
Anders Carlsson91a0cc92009-08-26 22:33:56 +00001651bool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
Anders Carlsson8c8d9192009-10-09 23:51:55 +00001652 const PartialDiagnostic &PD,
1653 std::pair<SourceLocation,
1654 PartialDiagnostic> Note) {
Anders Carlsson91a0cc92009-08-26 22:33:56 +00001655 unsigned diag = PD.getDiagID();
Mike Stump1eb44332009-09-09 15:08:12 +00001656
Douglas Gregor690dc7f2009-05-21 23:48:18 +00001657 // FIXME: Add this assertion to help us flush out problems with
1658 // checking for dependent types and type-dependent expressions.
1659 //
Mike Stump1eb44332009-09-09 15:08:12 +00001660 // assert(!T->isDependentType() &&
Douglas Gregor690dc7f2009-05-21 23:48:18 +00001661 // "Can't ask whether a dependent type is complete");
1662
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001663 // If we have a complete type, we're done.
1664 if (!T->isIncompleteType())
1665 return false;
Eli Friedman3c0eb162008-05-27 03:33:27 +00001666
Douglas Gregord475b8d2009-03-25 21:17:03 +00001667 // If we have a class template specialization or a class member of a
1668 // class template specialization, try to instantiate it.
Ted Kremenek6217b802009-07-29 21:53:49 +00001669 if (const RecordType *Record = T->getAs<RecordType>()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001670 if (ClassTemplateSpecializationDecl *ClassTemplateSpec
Douglas Gregord475b8d2009-03-25 21:17:03 +00001671 = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001672 if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001673 if (Loc.isValid())
John McCall9cc78072009-09-11 07:25:08 +00001674 ClassTemplateSpec->setPointOfInstantiation(Loc);
Douglas Gregor2943aed2009-03-03 04:44:36 +00001675 return InstantiateClassTemplateSpecialization(ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001676 TSK_ImplicitInstantiation,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001677 /*Complain=*/diag != 0);
Douglas Gregor2943aed2009-03-03 04:44:36 +00001678 }
Mike Stump1eb44332009-09-09 15:08:12 +00001679 } else if (CXXRecordDecl *Rec
Douglas Gregord475b8d2009-03-25 21:17:03 +00001680 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
1681 if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001682 MemberSpecializationInfo *MSInfo = Rec->getMemberSpecializationInfo();
1683 assert(MSInfo && "Missing member specialization information?");
Douglas Gregor357bbd02009-08-28 20:50:45 +00001684 // This record was instantiated from a class within a template.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001685 if (MSInfo->getTemplateSpecializationKind()
1686 != TSK_ExplicitSpecialization) {
1687 MSInfo->setPointOfInstantiation(Loc);
Douglas Gregorf6b11852009-10-08 15:14:33 +00001688 return InstantiateClass(Loc, Rec, Pattern,
1689 getTemplateInstantiationArgs(Rec),
1690 TSK_ImplicitInstantiation,
1691 /*Complain=*/diag != 0);
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001692 }
Douglas Gregord475b8d2009-03-25 21:17:03 +00001693 }
1694 }
1695 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00001696
Douglas Gregor5842ba92009-08-24 15:23:48 +00001697 if (diag == 0)
1698 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001699
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001700 // We have an incomplete type. Produce a diagnostic.
Anders Carlsson91a0cc92009-08-26 22:33:56 +00001701 Diag(Loc, PD) << T;
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001702
Anders Carlsson8c8d9192009-10-09 23:51:55 +00001703 // If we have a note, produce it.
1704 if (!Note.first.isInvalid())
1705 Diag(Note.first, Note.second);
1706
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001707 // If the type was a forward declaration of a class/struct/union
Mike Stump1eb44332009-09-09 15:08:12 +00001708 // type, produce
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001709 const TagType *Tag = 0;
Ted Kremenek6217b802009-07-29 21:53:49 +00001710 if (const RecordType *Record = T->getAs<RecordType>())
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001711 Tag = Record;
John McCall183700f2009-09-21 23:43:11 +00001712 else if (const EnumType *Enum = T->getAs<EnumType>())
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001713 Tag = Enum;
1714
1715 if (Tag && !Tag->getDecl()->isInvalidDecl())
Mike Stump1eb44332009-09-09 15:08:12 +00001716 Diag(Tag->getDecl()->getLocation(),
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001717 Tag->isBeingDefined() ? diag::note_type_being_defined
1718 : diag::note_forward_declaration)
1719 << QualType(Tag, 0);
1720
1721 return true;
1722}
Douglas Gregore6258932009-03-19 00:39:20 +00001723
1724/// \brief Retrieve a version of the type 'T' that is qualified by the
1725/// nested-name-specifier contained in SS.
1726QualType Sema::getQualifiedNameType(const CXXScopeSpec &SS, QualType T) {
1727 if (!SS.isSet() || SS.isInvalid() || T.isNull())
1728 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00001729
Douglas Gregorab452ba2009-03-26 23:50:42 +00001730 NestedNameSpecifier *NNS
Douglas Gregor35073692009-03-26 23:56:24 +00001731 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Douglas Gregorab452ba2009-03-26 23:50:42 +00001732 return Context.getQualifiedNameType(NNS, T);
Douglas Gregore6258932009-03-19 00:39:20 +00001733}
Anders Carlssonaf017e62009-06-29 22:58:55 +00001734
1735QualType Sema::BuildTypeofExprType(Expr *E) {
1736 return Context.getTypeOfExprType(E);
1737}
1738
1739QualType Sema::BuildDecltypeType(Expr *E) {
1740 if (E->getType() == Context.OverloadTy) {
Mike Stump1eb44332009-09-09 15:08:12 +00001741 Diag(E->getLocStart(),
Anders Carlssonaf017e62009-06-29 22:58:55 +00001742 diag::err_cannot_determine_declared_type_of_overloaded_function);
1743 return QualType();
1744 }
1745 return Context.getDecltypeType(E);
1746}