blob: 3cdf6154232bd34ada28167e84fad04439849f26 [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 }
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000613 T = Context.getConstantArrayWithExprType(T, ConstVal, ArraySize,
614 ASM, Quals, Brackets);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000615 }
616 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
617 if (!getLangOptions().C99) {
Mike Stump1eb44332009-09-09 15:08:12 +0000618 if (ArraySize && !ArraySize->isTypeDependent() &&
619 !ArraySize->isValueDependent() &&
Douglas Gregorcd281c32009-02-28 00:25:32 +0000620 !ArraySize->isIntegerConstantExpr(Context))
Douglas Gregor043cad22009-09-11 00:18:58 +0000621 Diag(Loc, getLangOptions().CPlusPlus? diag::err_vla_cxx : diag::ext_vla);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000622 else if (ASM != ArrayType::Normal || Quals != 0)
Douglas Gregor043cad22009-09-11 00:18:58 +0000623 Diag(Loc,
624 getLangOptions().CPlusPlus? diag::err_c99_array_usage_cxx
625 : diag::ext_c99_array_usage);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000626 }
627
628 return T;
629}
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000630
631/// \brief Build an ext-vector type.
632///
633/// Run the required checks for the extended vector type.
Mike Stump1eb44332009-09-09 15:08:12 +0000634QualType Sema::BuildExtVectorType(QualType T, ExprArg ArraySize,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000635 SourceLocation AttrLoc) {
636
637 Expr *Arg = (Expr *)ArraySize.get();
638
639 // unlike gcc's vector_size attribute, we do not allow vectors to be defined
640 // in conjunction with complex types (pointers, arrays, functions, etc.).
Mike Stump1eb44332009-09-09 15:08:12 +0000641 if (!T->isDependentType() &&
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000642 !T->isIntegerType() && !T->isRealFloatingType()) {
643 Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T;
644 return QualType();
645 }
646
647 if (!Arg->isTypeDependent() && !Arg->isValueDependent()) {
648 llvm::APSInt vecSize(32);
649 if (!Arg->isIntegerConstantExpr(vecSize, Context)) {
650 Diag(AttrLoc, diag::err_attribute_argument_not_int)
651 << "ext_vector_type" << Arg->getSourceRange();
652 return QualType();
653 }
Mike Stump1eb44332009-09-09 15:08:12 +0000654
655 // unlike gcc's vector_size attribute, the size is specified as the
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000656 // number of elements, not the number of bytes.
Mike Stump1eb44332009-09-09 15:08:12 +0000657 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
658
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000659 if (vectorSize == 0) {
660 Diag(AttrLoc, diag::err_attribute_zero_size)
661 << Arg->getSourceRange();
662 return QualType();
663 }
Mike Stump1eb44332009-09-09 15:08:12 +0000664
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000665 if (!T->isDependentType())
666 return Context.getExtVectorType(T, vectorSize);
Mike Stump1eb44332009-09-09 15:08:12 +0000667 }
668
669 return Context.getDependentSizedExtVectorType(T, ArraySize.takeAs<Expr>(),
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000670 AttrLoc);
671}
Mike Stump1eb44332009-09-09 15:08:12 +0000672
Douglas Gregor724651c2009-02-28 01:04:19 +0000673/// \brief Build a function type.
674///
675/// This routine checks the function type according to C++ rules and
676/// under the assumption that the result type and parameter types have
677/// just been instantiated from a template. It therefore duplicates
Douglas Gregor2943aed2009-03-03 04:44:36 +0000678/// some of the behavior of GetTypeForDeclarator, but in a much
Douglas Gregor724651c2009-02-28 01:04:19 +0000679/// simpler form that is only suitable for this narrow use case.
680///
681/// \param T The return type of the function.
682///
683/// \param ParamTypes The parameter types of the function. This array
684/// will be modified to account for adjustments to the types of the
685/// function parameters.
686///
687/// \param NumParamTypes The number of parameter types in ParamTypes.
688///
689/// \param Variadic Whether this is a variadic function type.
690///
691/// \param Quals The cvr-qualifiers to be applied to the function type.
692///
693/// \param Loc The location of the entity whose type involves this
694/// function type or, if there is no such entity, the location of the
695/// type that will have function type.
696///
697/// \param Entity The name of the entity that involves the function
698/// type, if known.
699///
700/// \returns A suitable function type, if there are no
701/// errors. Otherwise, returns a NULL type.
702QualType Sema::BuildFunctionType(QualType T,
Mike Stump1eb44332009-09-09 15:08:12 +0000703 QualType *ParamTypes,
Douglas Gregor724651c2009-02-28 01:04:19 +0000704 unsigned NumParamTypes,
705 bool Variadic, unsigned Quals,
706 SourceLocation Loc, DeclarationName Entity) {
707 if (T->isArrayType() || T->isFunctionType()) {
708 Diag(Loc, diag::err_func_returning_array_function) << T;
709 return QualType();
710 }
Mike Stump1eb44332009-09-09 15:08:12 +0000711
Douglas Gregor724651c2009-02-28 01:04:19 +0000712 bool Invalid = false;
713 for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) {
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000714 QualType ParamType = adjustParameterType(ParamTypes[Idx]);
715 if (ParamType->isVoidType()) {
Douglas Gregor724651c2009-02-28 01:04:19 +0000716 Diag(Loc, diag::err_param_with_void_type);
717 Invalid = true;
718 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000719
Anders Carlsson83913e32009-09-16 23:47:08 +0000720 ParamTypes[Idx] = adjustFunctionParamType(ParamType);
Douglas Gregor724651c2009-02-28 01:04:19 +0000721 }
722
723 if (Invalid)
724 return QualType();
725
Mike Stump1eb44332009-09-09 15:08:12 +0000726 return Context.getFunctionType(T, ParamTypes, NumParamTypes, Variadic,
Douglas Gregor724651c2009-02-28 01:04:19 +0000727 Quals);
728}
Mike Stump1eb44332009-09-09 15:08:12 +0000729
Douglas Gregor949bf692009-06-09 22:17:39 +0000730/// \brief Build a member pointer type \c T Class::*.
731///
732/// \param T the type to which the member pointer refers.
733/// \param Class the class type into which the member pointer points.
John McCall0953e762009-09-24 19:53:00 +0000734/// \param CVR Qualifiers applied to the member pointer type
Douglas Gregor949bf692009-06-09 22:17:39 +0000735/// \param Loc the location where this type begins
736/// \param Entity the name of the entity that will have this member pointer type
737///
738/// \returns a member pointer type, if successful, or a NULL type if there was
739/// an error.
Mike Stump1eb44332009-09-09 15:08:12 +0000740QualType Sema::BuildMemberPointerType(QualType T, QualType Class,
John McCall0953e762009-09-24 19:53:00 +0000741 unsigned CVR, SourceLocation Loc,
Douglas Gregor949bf692009-06-09 22:17:39 +0000742 DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000743 Qualifiers Quals = Qualifiers::fromCVRMask(CVR);
744
Douglas Gregor949bf692009-06-09 22:17:39 +0000745 // Verify that we're not building a pointer to pointer to function with
746 // exception specification.
747 if (CheckDistantExceptionSpec(T)) {
748 Diag(Loc, diag::err_distant_exception_spec);
749
750 // FIXME: If we're doing this as part of template instantiation,
751 // we should return immediately.
752
753 // Build the type anyway, but use the canonical type so that the
754 // exception specifiers are stripped off.
755 T = Context.getCanonicalType(T);
756 }
757
758 // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member
759 // with reference type, or "cv void."
760 if (T->isReferenceType()) {
Anders Carlsson8d4655d2009-06-30 00:06:57 +0000761 Diag(Loc, diag::err_illegal_decl_mempointer_to_reference)
Douglas Gregor949bf692009-06-09 22:17:39 +0000762 << (Entity? Entity.getAsString() : "type name");
763 return QualType();
764 }
765
766 if (T->isVoidType()) {
767 Diag(Loc, diag::err_illegal_decl_mempointer_to_void)
768 << (Entity? Entity.getAsString() : "type name");
769 return QualType();
770 }
771
772 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
773 // object or incomplete types shall not be restrict-qualified."
John McCall0953e762009-09-24 19:53:00 +0000774 if (Quals.hasRestrict() && !T->isIncompleteOrObjectType()) {
Douglas Gregor949bf692009-06-09 22:17:39 +0000775 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
776 << T;
777
778 // FIXME: If we're doing this as part of template instantiation,
779 // we should return immediately.
John McCall0953e762009-09-24 19:53:00 +0000780 Quals.removeRestrict();
Douglas Gregor949bf692009-06-09 22:17:39 +0000781 }
782
783 if (!Class->isDependentType() && !Class->isRecordType()) {
784 Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class;
785 return QualType();
786 }
787
John McCall0953e762009-09-24 19:53:00 +0000788 return Context.getQualifiedType(
789 Context.getMemberPointerType(T, Class.getTypePtr()), Quals);
Douglas Gregor949bf692009-06-09 22:17:39 +0000790}
Mike Stump1eb44332009-09-09 15:08:12 +0000791
Anders Carlsson9a917e42009-06-12 22:56:54 +0000792/// \brief Build a block pointer type.
793///
794/// \param T The type to which we'll be building a block pointer.
795///
John McCall0953e762009-09-24 19:53:00 +0000796/// \param CVR The cvr-qualifiers to be applied to the block pointer type.
Anders Carlsson9a917e42009-06-12 22:56:54 +0000797///
798/// \param Loc The location of the entity whose type involves this
799/// block pointer type or, if there is no such entity, the location of the
800/// type that will have block pointer type.
801///
802/// \param Entity The name of the entity that involves the block pointer
803/// type, if known.
804///
805/// \returns A suitable block pointer type, if there are no
806/// errors. Otherwise, returns a NULL type.
John McCall0953e762009-09-24 19:53:00 +0000807QualType Sema::BuildBlockPointerType(QualType T, unsigned CVR,
Mike Stump1eb44332009-09-09 15:08:12 +0000808 SourceLocation Loc,
Anders Carlsson9a917e42009-06-12 22:56:54 +0000809 DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000810 if (!T->isFunctionType()) {
Anders Carlsson9a917e42009-06-12 22:56:54 +0000811 Diag(Loc, diag::err_nonfunction_block_type);
812 return QualType();
813 }
Mike Stump1eb44332009-09-09 15:08:12 +0000814
John McCall0953e762009-09-24 19:53:00 +0000815 Qualifiers Quals = Qualifiers::fromCVRMask(CVR);
816 return Context.getQualifiedType(Context.getBlockPointerType(T), Quals);
Anders Carlsson9a917e42009-06-12 22:56:54 +0000817}
818
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000819QualType Sema::GetTypeFromParser(TypeTy *Ty, DeclaratorInfo **DInfo) {
820 QualType QT = QualType::getFromOpaquePtr(Ty);
821 DeclaratorInfo *DI = 0;
822 if (LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) {
823 QT = LIT->getType();
824 DI = LIT->getDeclaratorInfo();
825 }
Mike Stump1eb44332009-09-09 15:08:12 +0000826
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000827 if (DInfo) *DInfo = DI;
828 return QT;
829}
830
Mike Stump98eb8a72009-02-04 22:31:32 +0000831/// GetTypeForDeclarator - Convert the type for the specified
832/// declarator to Type instances. Skip the outermost Skip type
833/// objects.
Douglas Gregor402abb52009-05-28 23:31:59 +0000834///
835/// If OwnedDecl is non-NULL, and this declarator's decl-specifier-seq
836/// owns the declaration of a type (e.g., the definition of a struct
837/// type), then *OwnedDecl will receive the owned declaration.
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000838QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S,
839 DeclaratorInfo **DInfo, unsigned Skip,
Douglas Gregor402abb52009-05-28 23:31:59 +0000840 TagDecl **OwnedDecl) {
Mike Stump98eb8a72009-02-04 22:31:32 +0000841 bool OmittedReturnType = false;
842
843 if (D.getContext() == Declarator::BlockLiteralContext
844 && Skip == 0
845 && !D.getDeclSpec().hasTypeSpecifier()
846 && (D.getNumTypeObjects() == 0
847 || (D.getNumTypeObjects() == 1
848 && D.getTypeObject(0).Kind == DeclaratorChunk::Function)))
849 OmittedReturnType = true;
850
Chris Lattnerb23deda2007-08-28 16:40:32 +0000851 // long long is a C99 feature.
Chris Lattnerd1eb3322007-08-28 16:41:29 +0000852 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Chris Lattnerb23deda2007-08-28 16:40:32 +0000853 D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
854 Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
Douglas Gregor930d8b52009-01-30 22:09:00 +0000855
856 // Determine the type of the declarator. Not all forms of declarator
857 // have a type.
858 QualType T;
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000859 // The QualType referring to the type as written in source code. We can't use
860 // T because it can change due to semantic analysis.
861 QualType SourceTy;
862
Douglas Gregor930d8b52009-01-30 22:09:00 +0000863 switch (D.getKind()) {
864 case Declarator::DK_Abstract:
865 case Declarator::DK_Normal:
Douglas Gregordb422df2009-09-25 21:45:23 +0000866 case Declarator::DK_Operator:
867 case Declarator::DK_TemplateId: {
Chris Lattner3f84ad22009-04-22 05:27:59 +0000868 const DeclSpec &DS = D.getDeclSpec();
869 if (OmittedReturnType) {
Mike Stump98eb8a72009-02-04 22:31:32 +0000870 // We default to a dependent type initially. Can be modified by
871 // the first return statement.
872 T = Context.DependentTy;
Chris Lattner3f84ad22009-04-22 05:27:59 +0000873 } else {
Chris Lattnereaaebc72009-04-25 08:06:05 +0000874 bool isInvalid = false;
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000875 T = ConvertDeclSpecToType(DS, D.getIdentifierLoc(), isInvalid, SourceTy);
Chris Lattnereaaebc72009-04-25 08:06:05 +0000876 if (isInvalid)
877 D.setInvalidType(true);
Douglas Gregor402abb52009-05-28 23:31:59 +0000878 else if (OwnedDecl && DS.isTypeSpecOwned())
879 *OwnedDecl = cast<TagDecl>((Decl *)DS.getTypeRep());
Douglas Gregor809070a2009-02-18 17:45:20 +0000880 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000881 break;
Mike Stump98eb8a72009-02-04 22:31:32 +0000882 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000883
884 case Declarator::DK_Constructor:
885 case Declarator::DK_Destructor:
886 case Declarator::DK_Conversion:
887 // Constructors and destructors don't have return types. Use
888 // "void" instead. Conversion operators will check their return
889 // types separately.
890 T = Context.VoidTy;
891 break;
892 }
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000893
894 if (SourceTy.isNull())
895 SourceTy = T;
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000896
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000897 if (T == Context.UndeducedAutoTy) {
898 int Error = -1;
Mike Stump1eb44332009-09-09 15:08:12 +0000899
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000900 switch (D.getContext()) {
901 case Declarator::KNRTypeListContext:
902 assert(0 && "K&R type lists aren't allowed in C++");
903 break;
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000904 case Declarator::PrototypeContext:
905 Error = 0; // Function prototype
906 break;
907 case Declarator::MemberContext:
908 switch (cast<TagDecl>(CurContext)->getTagKind()) {
909 case TagDecl::TK_enum: assert(0 && "unhandled tag kind"); break;
910 case TagDecl::TK_struct: Error = 1; /* Struct member */ break;
911 case TagDecl::TK_union: Error = 2; /* Union member */ break;
912 case TagDecl::TK_class: Error = 3; /* Class member */ break;
Mike Stump1eb44332009-09-09 15:08:12 +0000913 }
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000914 break;
915 case Declarator::CXXCatchContext:
916 Error = 4; // Exception declaration
917 break;
918 case Declarator::TemplateParamContext:
919 Error = 5; // Template parameter
920 break;
921 case Declarator::BlockLiteralContext:
922 Error = 6; // Block literal
923 break;
924 case Declarator::FileContext:
925 case Declarator::BlockContext:
926 case Declarator::ForContext:
927 case Declarator::ConditionContext:
928 case Declarator::TypeNameContext:
929 break;
930 }
931
932 if (Error != -1) {
933 Diag(D.getDeclSpec().getTypeSpecTypeLoc(), diag::err_auto_not_allowed)
934 << Error;
935 T = Context.IntTy;
936 D.setInvalidType(true);
937 }
938 }
Mike Stump1eb44332009-09-09 15:08:12 +0000939
Douglas Gregorcd281c32009-02-28 00:25:32 +0000940 // The name we're declaring, if any.
941 DeclarationName Name;
942 if (D.getIdentifier())
943 Name = D.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +0000944
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +0000945 bool ShouldBuildInfo = DInfo != 0;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000946
Mike Stump98eb8a72009-02-04 22:31:32 +0000947 // Walk the DeclTypeInfo, building the recursive type as we go.
948 // DeclTypeInfos are ordered from the identifier out, which is
949 // opposite of what we want :).
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000950 for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) {
951 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1+Skip);
Reid Spencer5f016e22007-07-11 17:01:13 +0000952 switch (DeclType.Kind) {
953 default: assert(0 && "Unknown decltype!");
Steve Naroff5618bd42008-08-27 16:04:49 +0000954 case DeclaratorChunk::BlockPointer:
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +0000955 if (ShouldBuildInfo) {
956 if (SourceTy->isFunctionType())
John McCall0953e762009-09-24 19:53:00 +0000957 SourceTy
958 = Context.getQualifiedType(Context.getBlockPointerType(SourceTy),
959 Qualifiers::fromCVRMask(DeclType.Cls.TypeQuals));
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +0000960 else
961 // If not function type Context::getBlockPointerType asserts,
962 // so just give up.
963 ShouldBuildInfo = false;
964 }
965
Chris Lattner9af55002009-03-27 04:18:06 +0000966 // If blocks are disabled, emit an error.
967 if (!LangOpts.Blocks)
968 Diag(DeclType.Loc, diag::err_blocks_disable);
Mike Stump1eb44332009-09-09 15:08:12 +0000969
970 T = BuildBlockPointerType(T, DeclType.Cls.TypeQuals, D.getIdentifierLoc(),
Anders Carlsson9a917e42009-06-12 22:56:54 +0000971 Name);
Steve Naroff5618bd42008-08-27 16:04:49 +0000972 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000973 case DeclaratorChunk::Pointer:
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +0000974 //FIXME: Use ObjCObjectPointer for info when appropriate.
975 if (ShouldBuildInfo)
John McCall0953e762009-09-24 19:53:00 +0000976 SourceTy = Context.getQualifiedType(Context.getPointerType(SourceTy),
977 Qualifiers::fromCVRMask(DeclType.Ptr.TypeQuals));
Sebastian Redl6a7330c2009-05-29 15:01:05 +0000978 // Verify that we're not building a pointer to pointer to function with
979 // exception specification.
980 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
981 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
982 D.setInvalidType(true);
983 // Build the type anyway.
984 }
Steve Naroff14108da2009-07-10 23:34:53 +0000985 if (getLangOptions().ObjC1 && T->isObjCInterfaceType()) {
John McCall183700f2009-09-21 23:43:11 +0000986 const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>();
Steve Naroff14108da2009-07-10 23:34:53 +0000987 T = Context.getObjCObjectPointerType(T,
Steve Naroff67ef8ea2009-07-20 17:56:53 +0000988 (ObjCProtocolDecl **)OIT->qual_begin(),
989 OIT->getNumProtocols());
Steve Naroff14108da2009-07-10 23:34:53 +0000990 break;
991 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000992 T = BuildPointerType(T, DeclType.Ptr.TypeQuals, DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000993 break;
John McCall0953e762009-09-24 19:53:00 +0000994 case DeclaratorChunk::Reference: {
995 Qualifiers Quals;
996 if (DeclType.Ref.HasRestrict) Quals.addRestrict();
997
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +0000998 if (ShouldBuildInfo) {
999 if (DeclType.Ref.LValueRef)
1000 SourceTy = Context.getLValueReferenceType(SourceTy);
1001 else
1002 SourceTy = Context.getRValueReferenceType(SourceTy);
John McCall0953e762009-09-24 19:53:00 +00001003 SourceTy = Context.getQualifiedType(SourceTy, Quals);
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001004 }
1005
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001006 // Verify that we're not building a reference to pointer to function with
1007 // exception specification.
1008 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1009 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1010 D.setInvalidType(true);
1011 // Build the type anyway.
1012 }
John McCall0953e762009-09-24 19:53:00 +00001013 T = BuildReferenceType(T, DeclType.Ref.LValueRef, Quals,
Douglas Gregorcd281c32009-02-28 00:25:32 +00001014 DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +00001015 break;
John McCall0953e762009-09-24 19:53:00 +00001016 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001017 case DeclaratorChunk::Array: {
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001018 if (ShouldBuildInfo)
1019 // We just need to get an array type, the exact type doesn't matter.
1020 SourceTy = Context.getIncompleteArrayType(SourceTy, ArrayType::Normal,
John McCall0953e762009-09-24 19:53:00 +00001021 DeclType.Arr.TypeQuals);
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001022
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001023 // Verify that we're not building an array of pointers to function with
1024 // exception specification.
1025 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1026 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1027 D.setInvalidType(true);
1028 // Build the type anyway.
1029 }
Chris Lattnerfd89bc82008-04-02 01:05:10 +00001030 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner94f81fd2007-08-28 16:54:00 +00001031 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001032 ArrayType::ArraySizeModifier ASM;
1033 if (ATI.isStar)
1034 ASM = ArrayType::Star;
1035 else if (ATI.hasStatic)
1036 ASM = ArrayType::Static;
1037 else
1038 ASM = ArrayType::Normal;
Eli Friedmanf91f5c82009-04-26 21:57:51 +00001039 if (ASM == ArrayType::Star &&
1040 D.getContext() != Declarator::PrototypeContext) {
1041 // FIXME: This check isn't quite right: it allows star in prototypes
1042 // for function definitions, and disallows some edge cases detailed
1043 // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html
1044 Diag(DeclType.Loc, diag::err_array_star_outside_prototype);
1045 ASM = ArrayType::Normal;
1046 D.setInvalidType(true);
1047 }
John McCall0953e762009-09-24 19:53:00 +00001048 T = BuildArrayType(T, ASM, ArraySize,
1049 Qualifiers::fromCVRMask(ATI.TypeQuals),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001050 SourceRange(DeclType.Loc, DeclType.EndLoc), Name);
Reid Spencer5f016e22007-07-11 17:01:13 +00001051 break;
1052 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001053 case DeclaratorChunk::Function: {
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001054 if (ShouldBuildInfo) {
1055 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
1056 llvm::SmallVector<QualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +00001057
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001058 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
1059 ParmVarDecl *Param = FTI.ArgInfo[i].Param.getAs<ParmVarDecl>();
Anders Carlsson83913e32009-09-16 23:47:08 +00001060 if (Param) {
1061 QualType ArgTy = adjustFunctionParamType(Param->getType());
1062
1063 ArgTys.push_back(ArgTy);
1064 }
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001065 }
1066 SourceTy = Context.getFunctionType(SourceTy, ArgTys.data(),
1067 ArgTys.size(),
John McCall0953e762009-09-24 19:53:00 +00001068 FTI.isVariadic,
1069 FTI.TypeQuals);
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001070 }
1071
Reid Spencer5f016e22007-07-11 17:01:13 +00001072 // If the function declarator has a prototype (i.e. it is not () and
1073 // does not have a K&R-style identifier list), then the arguments are part
1074 // of the type, otherwise the argument list is ().
1075 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Sebastian Redl3cc97262009-05-31 11:47:27 +00001076
Chris Lattnercd881292007-12-19 05:31:29 +00001077 // C99 6.7.5.3p1: The return type may not be a function or array type.
Chris Lattner68cfd492007-12-19 18:01:43 +00001078 if (T->isArrayType() || T->isFunctionType()) {
Chris Lattnerd1625842008-11-24 06:25:27 +00001079 Diag(DeclType.Loc, diag::err_func_returning_array_function) << T;
Chris Lattnercd881292007-12-19 05:31:29 +00001080 T = Context.IntTy;
1081 D.setInvalidType(true);
1082 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001083
Douglas Gregor402abb52009-05-28 23:31:59 +00001084 if (getLangOptions().CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) {
1085 // C++ [dcl.fct]p6:
1086 // Types shall not be defined in return or parameter types.
1087 TagDecl *Tag = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep());
1088 if (Tag->isDefinition())
1089 Diag(Tag->getLocation(), diag::err_type_defined_in_result_type)
1090 << Context.getTypeDeclType(Tag);
1091 }
1092
Sebastian Redl3cc97262009-05-31 11:47:27 +00001093 // Exception specs are not allowed in typedefs. Complain, but add it
1094 // anyway.
1095 if (FTI.hasExceptionSpec &&
1096 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
1097 Diag(FTI.getThrowLoc(), diag::err_exception_spec_in_typedef);
1098
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001099 if (FTI.NumArgs == 0) {
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001100 if (getLangOptions().CPlusPlus) {
1101 // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the
1102 // function takes no arguments.
Sebastian Redl465226e2009-05-27 22:11:52 +00001103 llvm::SmallVector<QualType, 4> Exceptions;
1104 Exceptions.reserve(FTI.NumExceptions);
Mike Stump1eb44332009-09-09 15:08:12 +00001105 for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001106 // FIXME: Preserve type source info.
1107 QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty);
Sebastian Redlef65f062009-05-29 18:02:33 +00001108 // Check that the type is valid for an exception spec, and drop it
1109 // if not.
1110 if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1111 Exceptions.push_back(ET);
1112 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001113 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, FTI.TypeQuals,
1114 FTI.hasExceptionSpec,
1115 FTI.hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00001116 Exceptions.size(), Exceptions.data());
Douglas Gregor965acbb2009-02-18 07:07:28 +00001117 } else if (FTI.isVariadic) {
1118 // We allow a zero-parameter variadic function in C if the
1119 // function is marked with the "overloadable"
1120 // attribute. Scan for this attribute now.
1121 bool Overloadable = false;
1122 for (const AttributeList *Attrs = D.getAttributes();
1123 Attrs; Attrs = Attrs->getNext()) {
1124 if (Attrs->getKind() == AttributeList::AT_overloadable) {
1125 Overloadable = true;
1126 break;
1127 }
1128 }
1129
1130 if (!Overloadable)
1131 Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
1132 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, 0);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001133 } else {
1134 // Simple void foo(), where the incoming T is the result type.
Douglas Gregor72564e72009-02-26 23:50:07 +00001135 T = Context.getFunctionNoProtoType(T);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001136 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001137 } else if (FTI.ArgInfo[0].Param == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001138 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
Mike Stump1eb44332009-09-09 15:08:12 +00001139 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
Reid Spencer5f016e22007-07-11 17:01:13 +00001140 } else {
1141 // Otherwise, we have a function with an argument list that is
1142 // potentially variadic.
1143 llvm::SmallVector<QualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +00001144
Reid Spencer5f016e22007-07-11 17:01:13 +00001145 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001146 ParmVarDecl *Param =
1147 cast<ParmVarDecl>(FTI.ArgInfo[i].Param.getAs<Decl>());
Chris Lattner8123a952008-04-10 02:22:51 +00001148 QualType ArgTy = Param->getType();
Chris Lattner78c75fb2007-07-21 05:30:18 +00001149 assert(!ArgTy.isNull() && "Couldn't parse type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001150
1151 // Adjust the parameter type.
Douglas Gregorbeb58cb2009-03-23 23:17:00 +00001152 assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001153
Reid Spencer5f016e22007-07-11 17:01:13 +00001154 // Look for 'void'. void is allowed only as a single argument to a
1155 // function with no other parameters (C99 6.7.5.3p10). We record
Douglas Gregor72564e72009-02-26 23:50:07 +00001156 // int(void) as a FunctionProtoType with an empty argument list.
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001157 if (ArgTy->isVoidType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001158 // If this is something like 'float(int, void)', reject it. 'void'
1159 // is an incomplete type (C99 6.2.5p19) and function decls cannot
1160 // have arguments of incomplete type.
1161 if (FTI.NumArgs != 1 || FTI.isVariadic) {
1162 Diag(DeclType.Loc, diag::err_void_only_param);
Chris Lattner2ff54262007-07-21 05:18:12 +00001163 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00001164 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00001165 } else if (FTI.ArgInfo[i].Ident) {
1166 // Reject, but continue to parse 'int(void abc)'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001167 Diag(FTI.ArgInfo[i].IdentLoc,
Chris Lattner4565d4e2007-07-21 05:26:43 +00001168 diag::err_param_with_void_type);
Chris Lattner2ff54262007-07-21 05:18:12 +00001169 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00001170 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00001171 } else {
1172 // Reject, but continue to parse 'float(const void)'.
John McCall0953e762009-09-24 19:53:00 +00001173 if (ArgTy.hasQualifiers())
Chris Lattner2ff54262007-07-21 05:18:12 +00001174 Diag(DeclType.Loc, diag::err_void_param_qualified);
Mike Stump1eb44332009-09-09 15:08:12 +00001175
Chris Lattner2ff54262007-07-21 05:18:12 +00001176 // Do not add 'void' to the ArgTys list.
1177 break;
1178 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001179 } else if (!FTI.hasPrototype) {
1180 if (ArgTy->isPromotableIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00001181 ArgTy = Context.getPromotedIntegerType(ArgTy);
John McCall183700f2009-09-21 23:43:11 +00001182 } else if (const BuiltinType* BTy = ArgTy->getAs<BuiltinType>()) {
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001183 if (BTy->getKind() == BuiltinType::Float)
1184 ArgTy = Context.DoubleTy;
1185 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001186 }
Mike Stump1eb44332009-09-09 15:08:12 +00001187
Anders Carlsson83913e32009-09-16 23:47:08 +00001188 ArgTys.push_back(adjustFunctionParamType(ArgTy));
Reid Spencer5f016e22007-07-11 17:01:13 +00001189 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001190
1191 llvm::SmallVector<QualType, 4> Exceptions;
1192 Exceptions.reserve(FTI.NumExceptions);
Mike Stump1eb44332009-09-09 15:08:12 +00001193 for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001194 // FIXME: Preserve type source info.
1195 QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty);
Sebastian Redlef65f062009-05-29 18:02:33 +00001196 // Check that the type is valid for an exception spec, and drop it if
1197 // not.
1198 if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1199 Exceptions.push_back(ET);
1200 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001201
Jay Foadbeaaccd2009-05-21 09:52:38 +00001202 T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(),
Sebastian Redl465226e2009-05-27 22:11:52 +00001203 FTI.isVariadic, FTI.TypeQuals,
1204 FTI.hasExceptionSpec,
1205 FTI.hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00001206 Exceptions.size(), Exceptions.data());
Reid Spencer5f016e22007-07-11 17:01:13 +00001207 }
1208 break;
1209 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001210 case DeclaratorChunk::MemberPointer:
Sebastian Redl4994d2d2009-07-04 11:39:00 +00001211 // Verify that we're not building a pointer to pointer to function with
1212 // exception specification.
1213 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1214 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1215 D.setInvalidType(true);
1216 // Build the type anyway.
1217 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001218 // The scope spec must refer to a class, or be dependent.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001219 QualType ClsType;
Douglas Gregor949bf692009-06-09 22:17:39 +00001220 if (isDependentScopeSpecifier(DeclType.Mem.Scope())) {
Mike Stump1eb44332009-09-09 15:08:12 +00001221 NestedNameSpecifier *NNS
Douglas Gregor949bf692009-06-09 22:17:39 +00001222 = (NestedNameSpecifier *)DeclType.Mem.Scope().getScopeRep();
1223 assert(NNS->getAsType() && "Nested-name-specifier must name a type");
1224 ClsType = QualType(NNS->getAsType(), 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001225 } else if (CXXRecordDecl *RD
Douglas Gregor949bf692009-06-09 22:17:39 +00001226 = dyn_cast_or_null<CXXRecordDecl>(
1227 computeDeclContext(DeclType.Mem.Scope()))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001228 ClsType = Context.getTagDeclType(RD);
1229 } else {
Douglas Gregor949bf692009-06-09 22:17:39 +00001230 Diag(DeclType.Mem.Scope().getBeginLoc(),
1231 diag::err_illegal_decl_mempointer_in_nonclass)
1232 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
1233 << DeclType.Mem.Scope().getRange();
Sebastian Redlf30208a2009-01-24 21:16:55 +00001234 D.setInvalidType(true);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001235 }
1236
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001237 if (ShouldBuildInfo) {
1238 QualType cls = !ClsType.isNull() ? ClsType : Context.IntTy;
John McCall0953e762009-09-24 19:53:00 +00001239 SourceTy = Context.getQualifiedType(
1240 Context.getMemberPointerType(SourceTy, cls.getTypePtr()),
1241 Qualifiers::fromCVRMask(DeclType.Mem.TypeQuals));
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001242 }
1243
Douglas Gregor949bf692009-06-09 22:17:39 +00001244 if (!ClsType.isNull())
1245 T = BuildMemberPointerType(T, ClsType, DeclType.Mem.TypeQuals,
1246 DeclType.Loc, D.getIdentifier());
1247 if (T.isNull()) {
1248 T = Context.IntTy;
Sebastian Redlf30208a2009-01-24 21:16:55 +00001249 D.setInvalidType(true);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001250 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001251 break;
1252 }
1253
Douglas Gregorcd281c32009-02-28 00:25:32 +00001254 if (T.isNull()) {
1255 D.setInvalidType(true);
1256 T = Context.IntTy;
1257 }
1258
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001259 // See if there are any attributes on this declarator chunk.
1260 if (const AttributeList *AL = DeclType.getAttrs())
1261 ProcessTypeAttributeList(T, AL);
Reid Spencer5f016e22007-07-11 17:01:13 +00001262 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001263
1264 if (getLangOptions().CPlusPlus && T->isFunctionType()) {
John McCall183700f2009-09-21 23:43:11 +00001265 const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00001266 assert(FnTy && "Why oh why is there not a FunctionProtoType here ?");
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001267
1268 // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type
1269 // for a nonstatic member function, the function type to which a pointer
1270 // to member refers, or the top-level function type of a function typedef
1271 // declaration.
1272 if (FnTy->getTypeQuals() != 0 &&
1273 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
Douglas Gregor584049d2008-12-15 23:53:10 +00001274 ((D.getContext() != Declarator::MemberContext &&
1275 (!D.getCXXScopeSpec().isSet() ||
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001276 !computeDeclContext(D.getCXXScopeSpec(), /*FIXME:*/true)
1277 ->isRecord())) ||
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001278 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001279 if (D.isFunctionDeclarator())
1280 Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
1281 else
1282 Diag(D.getIdentifierLoc(),
1283 diag::err_invalid_qualified_typedef_function_type_use);
1284
1285 // Strip the cv-quals from the type.
1286 T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001287 FnTy->getNumArgs(), FnTy->isVariadic(), 0);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001288 }
1289 }
Mike Stump1eb44332009-09-09 15:08:12 +00001290
Chris Lattner0bf29ad2008-06-29 00:19:33 +00001291 // If there were any type attributes applied to the decl itself (not the
1292 // type, apply the type attribute to the type!)
1293 if (const AttributeList *Attrs = D.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001294 ProcessTypeAttributeList(T, Attrs);
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001295
1296 if (ShouldBuildInfo)
1297 *DInfo = GetDeclaratorInfoForDeclarator(D, SourceTy, Skip);
1298
Reid Spencer5f016e22007-07-11 17:01:13 +00001299 return T;
1300}
1301
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001302static void FillTypeSpecLoc(TypeLoc TSL, const DeclSpec &DS) {
1303 if (TSL.isNull()) return;
1304
1305 if (TypedefLoc *TL = dyn_cast<TypedefLoc>(&TSL)) {
1306 TL->setNameLoc(DS.getTypeSpecTypeLoc());
1307
Argyrios Kyrtzidiseb667592009-09-29 19:45:22 +00001308 } else if (ObjCInterfaceLoc *TL = dyn_cast<ObjCInterfaceLoc>(&TSL)) {
1309 TL->setNameLoc(DS.getTypeSpecTypeLoc());
1310
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001311 } else if (ObjCProtocolListLoc *PLL = dyn_cast<ObjCProtocolListLoc>(&TSL)) {
1312 assert(PLL->getNumProtocols() == DS.getNumProtocolQualifiers());
1313 PLL->setLAngleLoc(DS.getProtocolLAngleLoc());
1314 PLL->setRAngleLoc(DS.getSourceRange().getEnd());
1315 for (unsigned i = 0; i != DS.getNumProtocolQualifiers(); ++i)
1316 PLL->setProtocolLoc(i, DS.getProtocolLocs()[i]);
1317 FillTypeSpecLoc(PLL->getBaseTypeLoc(), DS);
1318
1319 } else {
1320 //FIXME: Other typespecs.
1321 DefaultTypeSpecLoc &DTL = cast<DefaultTypeSpecLoc>(TSL);
1322 DTL.setStartLoc(DS.getSourceRange().getBegin());
1323 }
1324}
1325
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001326/// \brief Create and instantiate a DeclaratorInfo with type source information.
1327///
1328/// \param T QualType referring to the type as written in source code.
1329DeclaratorInfo *
1330Sema::GetDeclaratorInfoForDeclarator(Declarator &D, QualType T, unsigned Skip) {
1331 DeclaratorInfo *DInfo = Context.CreateDeclaratorInfo(T);
1332 TypeLoc CurrTL = DInfo->getTypeLoc();
1333
1334 for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) {
1335 assert(!CurrTL.isNull());
1336
1337 DeclaratorChunk &DeclType = D.getTypeObject(i);
1338 switch (DeclType.Kind) {
1339 default: assert(0 && "Unknown decltype!");
1340 case DeclaratorChunk::BlockPointer: {
1341 BlockPointerLoc &BPL = cast<BlockPointerLoc>(CurrTL);
1342 BPL.setCaretLoc(DeclType.Loc);
1343 break;
1344 }
1345 case DeclaratorChunk::Pointer: {
1346 //FIXME: ObjCObject pointers.
1347 PointerLoc &PL = cast<PointerLoc>(CurrTL);
1348 PL.setStarLoc(DeclType.Loc);
1349 break;
1350 }
1351 case DeclaratorChunk::Reference: {
1352 ReferenceLoc &RL = cast<ReferenceLoc>(CurrTL);
1353 RL.setAmpLoc(DeclType.Loc);
1354 break;
1355 }
1356 case DeclaratorChunk::Array: {
1357 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
1358 ArrayLoc &AL = cast<ArrayLoc>(CurrTL);
1359 AL.setLBracketLoc(DeclType.Loc);
1360 AL.setRBracketLoc(DeclType.EndLoc);
1361 AL.setSizeExpr(static_cast<Expr*>(ATI.NumElts));
1362 //FIXME: Star location for [*].
1363 break;
1364 }
1365 case DeclaratorChunk::Function: {
1366 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
1367 FunctionLoc &FL = cast<FunctionLoc>(CurrTL);
1368 FL.setLParenLoc(DeclType.Loc);
1369 FL.setRParenLoc(DeclType.EndLoc);
1370 for (unsigned i = 0, e = FTI.NumArgs, tpi = 0; i != e; ++i) {
1371 ParmVarDecl *Param = FTI.ArgInfo[i].Param.getAs<ParmVarDecl>();
1372 if (Param) {
1373 assert(tpi < FL.getNumArgs());
1374 FL.setArg(tpi++, Param);
1375 }
1376 }
1377 break;
1378 //FIXME: Exception specs.
1379 }
1380 case DeclaratorChunk::MemberPointer: {
1381 MemberPointerLoc &MPL = cast<MemberPointerLoc>(CurrTL);
1382 MPL.setStarLoc(DeclType.Loc);
1383 //FIXME: Class location.
1384 break;
1385 }
Mike Stump1eb44332009-09-09 15:08:12 +00001386
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001387 }
1388
1389 CurrTL = CurrTL.getNextTypeLoc();
1390 }
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001391
1392 FillTypeSpecLoc(CurrTL, D.getDeclSpec());
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001393
1394 return DInfo;
1395}
1396
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001397/// \brief Create a LocInfoType to hold the given QualType and DeclaratorInfo.
1398QualType Sema::CreateLocInfoType(QualType T, DeclaratorInfo *DInfo) {
1399 // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser
1400 // and Sema during declaration parsing. Try deallocating/caching them when
1401 // it's appropriate, instead of allocating them and keeping them around.
1402 LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType), 8);
1403 new (LocT) LocInfoType(T, DInfo);
1404 assert(LocT->getTypeClass() != T->getTypeClass() &&
1405 "LocInfoType's TypeClass conflicts with an existing Type class");
1406 return QualType(LocT, 0);
1407}
1408
1409void LocInfoType::getAsStringInternal(std::string &Str,
1410 const PrintingPolicy &Policy) const {
Argyrios Kyrtzidis35d44e52009-08-19 01:46:06 +00001411 assert(false && "LocInfoType leaked into the type system; an opaque TypeTy*"
1412 " was used directly instead of getting the QualType through"
1413 " GetTypeFromParser");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001414}
1415
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001416/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
Fariborz Jahanian360300c2007-11-09 22:27:59 +00001417/// declarator
Chris Lattnerb28317a2009-03-28 19:18:32 +00001418QualType Sema::ObjCGetTypeForMethodDefinition(DeclPtrTy D) {
1419 ObjCMethodDecl *MDecl = cast<ObjCMethodDecl>(D.getAs<Decl>());
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001420 QualType T = MDecl->getResultType();
1421 llvm::SmallVector<QualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +00001422
Fariborz Jahanian35600022007-11-09 17:18:29 +00001423 // Add the first two invisible argument types for self and _cmd.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001424 if (MDecl->isInstanceMethod()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001425 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +00001426 selfTy = Context.getPointerType(selfTy);
1427 ArgTys.push_back(selfTy);
Chris Lattner89951a82009-02-20 18:43:26 +00001428 } else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001429 ArgTys.push_back(Context.getObjCIdType());
1430 ArgTys.push_back(Context.getObjCSelType());
Mike Stump1eb44332009-09-09 15:08:12 +00001431
Chris Lattner89951a82009-02-20 18:43:26 +00001432 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
1433 E = MDecl->param_end(); PI != E; ++PI) {
1434 QualType ArgTy = (*PI)->getType();
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001435 assert(!ArgTy.isNull() && "Couldn't parse type?");
Douglas Gregorbeb58cb2009-03-23 23:17:00 +00001436 ArgTy = adjustParameterType(ArgTy);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001437 ArgTys.push_back(ArgTy);
1438 }
1439 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001440 MDecl->isVariadic(), 0);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001441 return T;
1442}
1443
Sebastian Redl9e5e4aa2009-01-26 19:54:48 +00001444/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that
1445/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
1446/// they point to and return true. If T1 and T2 aren't pointer types
1447/// or pointer-to-member types, or if they are not similar at this
1448/// level, returns false and leaves T1 and T2 unchanged. Top-level
1449/// qualifiers on T1 and T2 are ignored. This function will typically
1450/// be called in a loop that successively "unwraps" pointer and
1451/// pointer-to-member types to compare them at each level.
Chris Lattnerecb81f22009-02-16 21:43:00 +00001452bool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001453 const PointerType *T1PtrType = T1->getAs<PointerType>(),
1454 *T2PtrType = T2->getAs<PointerType>();
Douglas Gregor57373262008-10-22 14:17:15 +00001455 if (T1PtrType && T2PtrType) {
1456 T1 = T1PtrType->getPointeeType();
1457 T2 = T2PtrType->getPointeeType();
1458 return true;
1459 }
1460
Ted Kremenek6217b802009-07-29 21:53:49 +00001461 const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(),
1462 *T2MPType = T2->getAs<MemberPointerType>();
Sebastian Redl21593ac2009-01-28 18:33:18 +00001463 if (T1MPType && T2MPType &&
1464 Context.getCanonicalType(T1MPType->getClass()) ==
1465 Context.getCanonicalType(T2MPType->getClass())) {
Sebastian Redl4433aaf2009-01-25 19:43:20 +00001466 T1 = T1MPType->getPointeeType();
1467 T2 = T2MPType->getPointeeType();
1468 return true;
1469 }
Douglas Gregor57373262008-10-22 14:17:15 +00001470 return false;
1471}
1472
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001473Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001474 // C99 6.7.6: Type names have no identifier. This is already validated by
1475 // the parser.
1476 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00001477
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001478 DeclaratorInfo *DInfo = 0;
Douglas Gregor402abb52009-05-28 23:31:59 +00001479 TagDecl *OwnedTag = 0;
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001480 QualType T = GetTypeForDeclarator(D, S, &DInfo, /*Skip=*/0, &OwnedTag);
Chris Lattner5153ee62009-04-25 08:47:54 +00001481 if (D.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +00001482 return true;
Steve Naroff5912a352007-08-28 20:14:24 +00001483
Douglas Gregor402abb52009-05-28 23:31:59 +00001484 if (getLangOptions().CPlusPlus) {
1485 // Check that there are no default arguments (C++ only).
Douglas Gregor6d6eb572008-05-07 04:49:29 +00001486 CheckExtraCXXDefaultArguments(D);
1487
Douglas Gregor402abb52009-05-28 23:31:59 +00001488 // C++0x [dcl.type]p3:
1489 // A type-specifier-seq shall not define a class or enumeration
1490 // unless it appears in the type-id of an alias-declaration
1491 // (7.1.3).
1492 if (OwnedTag && OwnedTag->isDefinition())
1493 Diag(OwnedTag->getLocation(), diag::err_type_defined_in_type_specifier)
1494 << Context.getTypeDeclType(OwnedTag);
1495 }
1496
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001497 if (DInfo)
1498 T = CreateLocInfoType(T, DInfo);
1499
Reid Spencer5f016e22007-07-11 17:01:13 +00001500 return T.getAsOpaquePtr();
1501}
1502
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001503
1504
1505//===----------------------------------------------------------------------===//
1506// Type Attribute Processing
1507//===----------------------------------------------------------------------===//
1508
1509/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
1510/// specified type. The attribute contains 1 argument, the id of the address
1511/// space for the type.
Mike Stump1eb44332009-09-09 15:08:12 +00001512static void HandleAddressSpaceTypeAttribute(QualType &Type,
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001513 const AttributeList &Attr, Sema &S){
John McCall0953e762009-09-24 19:53:00 +00001514
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001515 // If this type is already address space qualified, reject it.
1516 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
1517 // for two or more different address spaces."
1518 if (Type.getAddressSpace()) {
1519 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
1520 return;
1521 }
Mike Stump1eb44332009-09-09 15:08:12 +00001522
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001523 // Check the attribute arguments.
1524 if (Attr.getNumArgs() != 1) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +00001525 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001526 return;
1527 }
1528 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
1529 llvm::APSInt addrSpace(32);
1530 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001531 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
1532 << ASArgExpr->getSourceRange();
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001533 return;
1534 }
1535
John McCallefadb772009-07-28 06:52:18 +00001536 // Bounds checking.
1537 if (addrSpace.isSigned()) {
1538 if (addrSpace.isNegative()) {
1539 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative)
1540 << ASArgExpr->getSourceRange();
1541 return;
1542 }
1543 addrSpace.setIsSigned(false);
1544 }
1545 llvm::APSInt max(addrSpace.getBitWidth());
John McCall0953e762009-09-24 19:53:00 +00001546 max = Qualifiers::MaxAddressSpace;
John McCallefadb772009-07-28 06:52:18 +00001547 if (addrSpace > max) {
1548 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high)
John McCall0953e762009-09-24 19:53:00 +00001549 << Qualifiers::MaxAddressSpace << ASArgExpr->getSourceRange();
John McCallefadb772009-07-28 06:52:18 +00001550 return;
1551 }
1552
Mike Stump1eb44332009-09-09 15:08:12 +00001553 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001554 Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001555}
1556
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001557/// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the
1558/// specified type. The attribute contains 1 argument, weak or strong.
Mike Stump1eb44332009-09-09 15:08:12 +00001559static void HandleObjCGCTypeAttribute(QualType &Type,
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001560 const AttributeList &Attr, Sema &S) {
John McCall0953e762009-09-24 19:53:00 +00001561 if (Type.getObjCGCAttr() != Qualifiers::GCNone) {
Fariborz Jahanian5934e752009-02-18 18:52:41 +00001562 S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001563 return;
1564 }
Mike Stump1eb44332009-09-09 15:08:12 +00001565
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001566 // Check the attribute arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001567 if (!Attr.getParameterName()) {
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001568 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1569 << "objc_gc" << 1;
1570 return;
1571 }
John McCall0953e762009-09-24 19:53:00 +00001572 Qualifiers::GC GCAttr;
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001573 if (Attr.getNumArgs() != 0) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001574 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1575 return;
1576 }
Mike Stump1eb44332009-09-09 15:08:12 +00001577 if (Attr.getParameterName()->isStr("weak"))
John McCall0953e762009-09-24 19:53:00 +00001578 GCAttr = Qualifiers::Weak;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001579 else if (Attr.getParameterName()->isStr("strong"))
John McCall0953e762009-09-24 19:53:00 +00001580 GCAttr = Qualifiers::Strong;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001581 else {
1582 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1583 << "objc_gc" << Attr.getParameterName();
1584 return;
1585 }
Mike Stump1eb44332009-09-09 15:08:12 +00001586
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001587 Type = S.Context.getObjCGCQualType(Type, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001588}
1589
Mike Stump24556362009-07-25 21:26:53 +00001590/// HandleNoReturnTypeAttribute - Process the noreturn attribute on the
1591/// specified type. The attribute contains 0 arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001592static void HandleNoReturnTypeAttribute(QualType &Type,
Mike Stump24556362009-07-25 21:26:53 +00001593 const AttributeList &Attr, Sema &S) {
1594 if (Attr.getNumArgs() != 0)
1595 return;
1596
1597 // We only apply this to a pointer to function or a pointer to block.
1598 if (!Type->isFunctionPointerType()
1599 && !Type->isBlockPointerType()
1600 && !Type->isFunctionType())
1601 return;
1602
1603 Type = S.Context.getNoReturnType(Type);
1604}
1605
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001606void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) {
Chris Lattner232e8822008-02-21 01:08:11 +00001607 // Scan through and apply attributes to this type where it makes sense. Some
1608 // attributes (such as __address_space__, __vector_size__, etc) apply to the
1609 // type, but others can be present in the type specifiers even though they
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00001610 // apply to the decl. Here we apply type attributes and ignore the rest.
1611 for (; AL; AL = AL->getNext()) {
Chris Lattner232e8822008-02-21 01:08:11 +00001612 // If this is an attribute we can handle, do so now, otherwise, add it to
1613 // the LeftOverAttrs list for rechaining.
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00001614 switch (AL->getKind()) {
Chris Lattner232e8822008-02-21 01:08:11 +00001615 default: break;
1616 case AttributeList::AT_address_space:
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001617 HandleAddressSpaceTypeAttribute(Result, *AL, *this);
1618 break;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001619 case AttributeList::AT_objc_gc:
1620 HandleObjCGCTypeAttribute(Result, *AL, *this);
1621 break;
Mike Stump24556362009-07-25 21:26:53 +00001622 case AttributeList::AT_noreturn:
1623 HandleNoReturnTypeAttribute(Result, *AL, *this);
1624 break;
Chris Lattner232e8822008-02-21 01:08:11 +00001625 }
Chris Lattner232e8822008-02-21 01:08:11 +00001626 }
Chris Lattner232e8822008-02-21 01:08:11 +00001627}
1628
Mike Stump1eb44332009-09-09 15:08:12 +00001629/// @brief Ensure that the type T is a complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001630///
1631/// This routine checks whether the type @p T is complete in any
1632/// context where a complete type is required. If @p T is a complete
Douglas Gregor86447ec2009-03-09 16:13:40 +00001633/// type, returns false. If @p T is a class template specialization,
1634/// this routine then attempts to perform class template
1635/// instantiation. If instantiation fails, or if @p T is incomplete
1636/// and cannot be completed, issues the diagnostic @p diag (giving it
1637/// the type @p T) and returns true.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001638///
1639/// @param Loc The location in the source that the incomplete type
1640/// diagnostic should refer to.
1641///
1642/// @param T The type that this routine is examining for completeness.
1643///
Mike Stump1eb44332009-09-09 15:08:12 +00001644/// @param PD The partial diagnostic that will be printed out if T is not a
Anders Carlssonb7906612009-08-26 23:45:07 +00001645/// complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001646///
1647/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
1648/// @c false otherwise.
Anders Carlsson91a0cc92009-08-26 22:33:56 +00001649bool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
Anders Carlsson8c8d9192009-10-09 23:51:55 +00001650 const PartialDiagnostic &PD,
1651 std::pair<SourceLocation,
1652 PartialDiagnostic> Note) {
Anders Carlsson91a0cc92009-08-26 22:33:56 +00001653 unsigned diag = PD.getDiagID();
Mike Stump1eb44332009-09-09 15:08:12 +00001654
Douglas Gregor690dc7f2009-05-21 23:48:18 +00001655 // FIXME: Add this assertion to help us flush out problems with
1656 // checking for dependent types and type-dependent expressions.
1657 //
Mike Stump1eb44332009-09-09 15:08:12 +00001658 // assert(!T->isDependentType() &&
Douglas Gregor690dc7f2009-05-21 23:48:18 +00001659 // "Can't ask whether a dependent type is complete");
1660
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001661 // If we have a complete type, we're done.
1662 if (!T->isIncompleteType())
1663 return false;
Eli Friedman3c0eb162008-05-27 03:33:27 +00001664
Douglas Gregord475b8d2009-03-25 21:17:03 +00001665 // If we have a class template specialization or a class member of a
1666 // class template specialization, try to instantiate it.
Ted Kremenek6217b802009-07-29 21:53:49 +00001667 if (const RecordType *Record = T->getAs<RecordType>()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001668 if (ClassTemplateSpecializationDecl *ClassTemplateSpec
Douglas Gregord475b8d2009-03-25 21:17:03 +00001669 = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001670 if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001671 if (Loc.isValid())
John McCall9cc78072009-09-11 07:25:08 +00001672 ClassTemplateSpec->setPointOfInstantiation(Loc);
Douglas Gregor2943aed2009-03-03 04:44:36 +00001673 return InstantiateClassTemplateSpecialization(ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001674 TSK_ImplicitInstantiation,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001675 /*Complain=*/diag != 0);
Douglas Gregor2943aed2009-03-03 04:44:36 +00001676 }
Mike Stump1eb44332009-09-09 15:08:12 +00001677 } else if (CXXRecordDecl *Rec
Douglas Gregord475b8d2009-03-25 21:17:03 +00001678 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
1679 if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001680 MemberSpecializationInfo *MSInfo = Rec->getMemberSpecializationInfo();
1681 assert(MSInfo && "Missing member specialization information?");
Douglas Gregor357bbd02009-08-28 20:50:45 +00001682 // This record was instantiated from a class within a template.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001683 if (MSInfo->getTemplateSpecializationKind()
1684 != TSK_ExplicitSpecialization) {
1685 MSInfo->setPointOfInstantiation(Loc);
Douglas Gregorf6b11852009-10-08 15:14:33 +00001686 return InstantiateClass(Loc, Rec, Pattern,
1687 getTemplateInstantiationArgs(Rec),
1688 TSK_ImplicitInstantiation,
1689 /*Complain=*/diag != 0);
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001690 }
Douglas Gregord475b8d2009-03-25 21:17:03 +00001691 }
1692 }
1693 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00001694
Douglas Gregor5842ba92009-08-24 15:23:48 +00001695 if (diag == 0)
1696 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001697
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001698 // We have an incomplete type. Produce a diagnostic.
Anders Carlsson91a0cc92009-08-26 22:33:56 +00001699 Diag(Loc, PD) << T;
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001700
Anders Carlsson8c8d9192009-10-09 23:51:55 +00001701 // If we have a note, produce it.
1702 if (!Note.first.isInvalid())
1703 Diag(Note.first, Note.second);
1704
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001705 // If the type was a forward declaration of a class/struct/union
Mike Stump1eb44332009-09-09 15:08:12 +00001706 // type, produce
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001707 const TagType *Tag = 0;
Ted Kremenek6217b802009-07-29 21:53:49 +00001708 if (const RecordType *Record = T->getAs<RecordType>())
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001709 Tag = Record;
John McCall183700f2009-09-21 23:43:11 +00001710 else if (const EnumType *Enum = T->getAs<EnumType>())
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001711 Tag = Enum;
1712
1713 if (Tag && !Tag->getDecl()->isInvalidDecl())
Mike Stump1eb44332009-09-09 15:08:12 +00001714 Diag(Tag->getDecl()->getLocation(),
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001715 Tag->isBeingDefined() ? diag::note_type_being_defined
1716 : diag::note_forward_declaration)
1717 << QualType(Tag, 0);
1718
1719 return true;
1720}
Douglas Gregore6258932009-03-19 00:39:20 +00001721
1722/// \brief Retrieve a version of the type 'T' that is qualified by the
1723/// nested-name-specifier contained in SS.
1724QualType Sema::getQualifiedNameType(const CXXScopeSpec &SS, QualType T) {
1725 if (!SS.isSet() || SS.isInvalid() || T.isNull())
1726 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00001727
Douglas Gregorab452ba2009-03-26 23:50:42 +00001728 NestedNameSpecifier *NNS
Douglas Gregor35073692009-03-26 23:56:24 +00001729 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Douglas Gregorab452ba2009-03-26 23:50:42 +00001730 return Context.getQualifiedNameType(NNS, T);
Douglas Gregore6258932009-03-19 00:39:20 +00001731}
Anders Carlssonaf017e62009-06-29 22:58:55 +00001732
1733QualType Sema::BuildTypeofExprType(Expr *E) {
1734 return Context.getTypeOfExprType(E);
1735}
1736
1737QualType Sema::BuildDecltypeType(Expr *E) {
1738 if (E->getType() == Context.OverloadTy) {
Mike Stump1eb44332009-09-09 15:08:12 +00001739 Diag(E->getLocStart(),
Anders Carlssonaf017e62009-06-29 22:58:55 +00001740 diag::err_cannot_determine_declared_type_of_overloaded_function);
1741 return QualType();
1742 }
1743 return Context.getDecltypeType(E);
1744}