blob: 2315c8fce53f844eb88af6ffb21c9fd0461d4865 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- SemaType.cpp - Semantic Analysis for Types -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements type-related semantic analysis.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
15#include "clang/AST/ASTContext.h"
Douglas Gregora8f32e02009-10-06 17:59:45 +000016#include "clang/AST/CXXInheritance.h"
Steve Naroff980e5082007-10-01 19:00:59 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregor2943aed2009-03-03 04:44:36 +000018#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +000019#include "clang/AST/TypeLoc.h"
John McCall51bd8032009-10-18 01:05:36 +000020#include "clang/AST/TypeLocVisitor.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000021#include "clang/AST/Expr.h"
Anders Carlsson91a0cc92009-08-26 22:33:56 +000022#include "clang/Basic/PartialDiagnostic.h"
Daniel Dunbare4858a62008-08-11 03:45:03 +000023#include "clang/Parse/DeclSpec.h"
Sebastian Redl4994d2d2009-07-04 11:39:00 +000024#include "llvm/ADT/SmallPtrSet.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000025using namespace clang;
26
Douglas Gregor2dc0e642009-03-23 23:06:20 +000027/// \brief Perform adjustment on the parameter type of a function.
28///
29/// This routine adjusts the given parameter type @p T to the actual
Mike Stump1eb44332009-09-09 15:08:12 +000030/// parameter type used by semantic analysis (C99 6.7.5.3p[7,8],
31/// C++ [dcl.fct]p3). The adjusted parameter type is returned.
Douglas Gregor2dc0e642009-03-23 23:06:20 +000032QualType Sema::adjustParameterType(QualType T) {
33 // C99 6.7.5.3p7:
Chris Lattner778ed742009-10-25 17:36:50 +000034 // A declaration of a parameter as "array of type" shall be
35 // adjusted to "qualified pointer to type", where the type
36 // qualifiers (if any) are those specified within the [ and ] of
37 // the array type derivation.
38 if (T->isArrayType())
Douglas Gregor2dc0e642009-03-23 23:06:20 +000039 return Context.getArrayDecayedType(T);
Chris Lattner778ed742009-10-25 17:36:50 +000040
41 // C99 6.7.5.3p8:
42 // A declaration of a parameter as "function returning type"
43 // shall be adjusted to "pointer to function returning type", as
44 // in 6.3.2.1.
45 if (T->isFunctionType())
Douglas Gregor2dc0e642009-03-23 23:06:20 +000046 return Context.getPointerType(T);
47
48 return T;
49}
50
Douglas Gregor930d8b52009-01-30 22:09:00 +000051/// \brief Convert the specified declspec to the appropriate type
52/// object.
53/// \param DS the declaration specifiers
Chris Lattner3f84ad22009-04-22 05:27:59 +000054/// \param DeclLoc The location of the declarator identifier or invalid if none.
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,
John McCall54e14c42009-10-22 22:37:11 +000059 bool &isInvalid) {
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;
Mike Stump1eb44332009-09-09 15:08:12 +000063
Reid Spencer5f016e22007-07-11 17:01:13 +000064 switch (DS.getTypeSpecType()) {
Chris Lattner96b77fc2008-04-02 06:50:17 +000065 case DeclSpec::TST_void:
66 Result = Context.VoidTy;
67 break;
Reid Spencer5f016e22007-07-11 17:01:13 +000068 case DeclSpec::TST_char:
69 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
Chris Lattnerfab5b452008-02-20 23:53:49 +000070 Result = Context.CharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000071 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
Chris Lattnerfab5b452008-02-20 23:53:49 +000072 Result = Context.SignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000073 else {
74 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
75 "Unknown TSS value");
Chris Lattnerfab5b452008-02-20 23:53:49 +000076 Result = Context.UnsignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000077 }
Chris Lattner958858e2008-02-20 21:40:32 +000078 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +000079 case DeclSpec::TST_wchar:
80 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
81 Result = Context.WCharTy;
82 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +000083 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
84 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +000085 Result = Context.getSignedWCharType();
86 } else {
87 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
88 "Unknown TSS value");
Chris Lattnerf3a41af2008-11-20 06:38:18 +000089 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
90 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +000091 Result = Context.getUnsignedWCharType();
92 }
93 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +000094 case DeclSpec::TST_char16:
95 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
96 "Unknown TSS value");
97 Result = Context.Char16Ty;
98 break;
99 case DeclSpec::TST_char32:
100 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
101 "Unknown TSS value");
102 Result = Context.Char32Ty;
103 break;
Chris Lattnerd658b562008-04-05 06:32:51 +0000104 case DeclSpec::TST_unspecified:
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000105 // "<proto1,proto2>" is an objc qualified ID with a missing id.
Chris Lattner097e9162008-10-20 02:01:50 +0000106 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000107 Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy,
Steve Naroff14108da2009-07-10 23:34:53 +0000108 (ObjCProtocolDecl**)PQ,
Steve Naroff683087f2009-06-29 16:22:52 +0000109 DS.getNumProtocolQualifiers());
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000110 break;
111 }
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Chris Lattnerd658b562008-04-05 06:32:51 +0000113 // Unspecified typespec defaults to int in C90. However, the C90 grammar
114 // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
115 // type-qualifier, or storage-class-specifier. If not, emit an extwarn.
116 // Note that the one exception to this is function definitions, which are
117 // allowed to be completely missing a declspec. This is handled in the
118 // parser already though by it pretending to have seen an 'int' in this
119 // case.
120 if (getLangOptions().ImplicitInt) {
Chris Lattner35d276f2009-02-27 18:53:28 +0000121 // In C89 mode, we only warn if there is a completely missing declspec
122 // when one is not allowed.
Chris Lattner3f84ad22009-04-22 05:27:59 +0000123 if (DS.isEmpty()) {
124 if (DeclLoc.isInvalid())
125 DeclLoc = DS.getSourceRange().getBegin();
Eli Friedmanfcff5772009-06-03 12:22:01 +0000126 Diag(DeclLoc, diag::ext_missing_declspec)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000127 << DS.getSourceRange()
Chris Lattner173144a2009-02-27 22:31:56 +0000128 << CodeModificationHint::CreateInsertion(DS.getSourceRange().getBegin(),
129 "int");
Chris Lattner3f84ad22009-04-22 05:27:59 +0000130 }
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000131 } else if (!DS.hasTypeSpecifier()) {
Chris Lattnerd658b562008-04-05 06:32:51 +0000132 // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says:
133 // "At least one type specifier shall be given in the declaration
134 // specifiers in each declaration, and in the specifier-qualifier list in
135 // each struct declaration and type name."
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000136 // FIXME: Does Microsoft really have the implicit int extension in C++?
Chris Lattner3f84ad22009-04-22 05:27:59 +0000137 if (DeclLoc.isInvalid())
138 DeclLoc = DS.getSourceRange().getBegin();
139
Chris Lattnerb78d8332009-06-26 04:45:06 +0000140 if (getLangOptions().CPlusPlus && !getLangOptions().Microsoft) {
Chris Lattner3f84ad22009-04-22 05:27:59 +0000141 Diag(DeclLoc, diag::err_missing_type_specifier)
142 << DS.getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000143
Chris Lattnerb78d8332009-06-26 04:45:06 +0000144 // When this occurs in C++ code, often something is very broken with the
145 // value being declared, poison it as invalid so we don't get chains of
146 // errors.
147 isInvalid = true;
148 } else {
Eli Friedmanfcff5772009-06-03 12:22:01 +0000149 Diag(DeclLoc, diag::ext_missing_type_specifier)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000150 << DS.getSourceRange();
Chris Lattnerb78d8332009-06-26 04:45:06 +0000151 }
Chris Lattnerd658b562008-04-05 06:32:51 +0000152 }
Mike Stump1eb44332009-09-09 15:08:12 +0000153
154 // FALL THROUGH.
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000155 case DeclSpec::TST_int: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000156 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
157 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000158 case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
159 case DeclSpec::TSW_short: Result = Context.ShortTy; break;
160 case DeclSpec::TSW_long: Result = Context.LongTy; break;
161 case DeclSpec::TSW_longlong: Result = Context.LongLongTy; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000162 }
163 } else {
164 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000165 case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
166 case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break;
167 case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break;
168 case DeclSpec::TSW_longlong: Result =Context.UnsignedLongLongTy; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000169 }
170 }
Chris Lattner958858e2008-02-20 21:40:32 +0000171 break;
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000172 }
Chris Lattnerfab5b452008-02-20 23:53:49 +0000173 case DeclSpec::TST_float: Result = Context.FloatTy; break;
Chris Lattner958858e2008-02-20 21:40:32 +0000174 case DeclSpec::TST_double:
175 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000176 Result = Context.LongDoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000177 else
Chris Lattnerfab5b452008-02-20 23:53:49 +0000178 Result = Context.DoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000179 break;
Chris Lattnerfab5b452008-02-20 23:53:49 +0000180 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
Reid Spencer5f016e22007-07-11 17:01:13 +0000181 case DeclSpec::TST_decimal32: // _Decimal32
182 case DeclSpec::TST_decimal64: // _Decimal64
183 case DeclSpec::TST_decimal128: // _Decimal128
Chris Lattner8f12f652009-05-13 05:02:08 +0000184 Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported);
185 Result = Context.IntTy;
186 isInvalid = true;
187 break;
Chris Lattner99dc9142008-04-13 18:59:07 +0000188 case DeclSpec::TST_class:
Reid Spencer5f016e22007-07-11 17:01:13 +0000189 case DeclSpec::TST_enum:
190 case DeclSpec::TST_union:
191 case DeclSpec::TST_struct: {
192 Decl *D = static_cast<Decl *>(DS.getTypeRep());
John McCall6e247262009-10-10 05:48:19 +0000193 if (!D) {
194 // This can happen in C++ with ambiguous lookups.
195 Result = Context.IntTy;
196 isInvalid = true;
197 break;
198 }
199
Reid Spencer5f016e22007-07-11 17:01:13 +0000200 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
201 DS.getTypeSpecSign() == 0 &&
202 "Can't handle qualifiers on typedef names yet!");
203 // TypeQuals handled by caller.
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000204 Result = Context.getTypeDeclType(cast<TypeDecl>(D));
John McCall2191b202009-09-05 06:31:47 +0000205
206 // In C++, make an ElaboratedType.
207 if (getLangOptions().CPlusPlus) {
208 TagDecl::TagKind Tag
209 = TagDecl::getTagKindForTypeSpec(DS.getTypeSpecType());
210 Result = Context.getElaboratedType(Result, Tag);
211 }
Mike Stump1eb44332009-09-09 15:08:12 +0000212
Chris Lattner5153ee62009-04-25 08:47:54 +0000213 if (D->isInvalidDecl())
214 isInvalid = true;
Chris Lattner958858e2008-02-20 21:40:32 +0000215 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000216 }
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000217 case DeclSpec::TST_typename: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000218 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
219 DS.getTypeSpecSign() == 0 &&
220 "Can't handle qualifiers on typedef names yet!");
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000221 Result = GetTypeFromParser(DS.getTypeRep());
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000222
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000223 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000224 if (const ObjCInterfaceType *
225 Interface = Result->getAs<ObjCInterfaceType>()) {
Steve Naroff67ef8ea2009-07-20 17:56:53 +0000226 // It would be nice if protocol qualifiers were only stored with the
227 // ObjCObjectPointerType. Unfortunately, this isn't possible due
228 // to the following typedef idiom (which is uncommon, but allowed):
Mike Stump1eb44332009-09-09 15:08:12 +0000229 //
Steve Naroff67ef8ea2009-07-20 17:56:53 +0000230 // typedef Foo<P> T;
231 // static void func() {
232 // Foo<P> *yy;
233 // T *zz;
234 // }
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000235 Result = Context.getObjCInterfaceType(Interface->getDecl(),
236 (ObjCProtocolDecl**)PQ,
237 DS.getNumProtocolQualifiers());
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000238 } else if (Result->isObjCIdType())
Chris Lattnerae4da612008-07-26 01:53:50 +0000239 // id<protocol-list>
Mike Stump1eb44332009-09-09 15:08:12 +0000240 Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy,
Steve Naroff14108da2009-07-10 23:34:53 +0000241 (ObjCProtocolDecl**)PQ, DS.getNumProtocolQualifiers());
242 else if (Result->isObjCClassType()) {
Chris Lattner3f84ad22009-04-22 05:27:59 +0000243 if (DeclLoc.isInvalid())
244 DeclLoc = DS.getSourceRange().getBegin();
Steve Naroff4262a072009-02-23 18:53:24 +0000245 // Class<protocol-list>
Mike Stump1eb44332009-09-09 15:08:12 +0000246 Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinClassTy,
Steve Naroff470301b2009-07-22 16:07:01 +0000247 (ObjCProtocolDecl**)PQ, DS.getNumProtocolQualifiers());
Chris Lattner3f84ad22009-04-22 05:27:59 +0000248 } else {
249 if (DeclLoc.isInvalid())
250 DeclLoc = DS.getSourceRange().getBegin();
251 Diag(DeclLoc, diag::err_invalid_protocol_qualifiers)
252 << DS.getSourceRange();
Chris Lattnereaaebc72009-04-25 08:06:05 +0000253 isInvalid = true;
Chris Lattner3f84ad22009-04-22 05:27:59 +0000254 }
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000255 }
Mike Stump1eb44332009-09-09 15:08:12 +0000256
Chris Lattnereaaebc72009-04-25 08:06:05 +0000257 // If this is a reference to an invalid typedef, propagate the invalidity.
258 if (TypedefType *TDT = dyn_cast<TypedefType>(Result))
259 if (TDT->getDecl()->isInvalidDecl())
260 isInvalid = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000261
Reid Spencer5f016e22007-07-11 17:01:13 +0000262 // TypeQuals handled by caller.
Chris Lattner958858e2008-02-20 21:40:32 +0000263 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000264 }
Chris Lattner958858e2008-02-20 21:40:32 +0000265 case DeclSpec::TST_typeofType:
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000266 // FIXME: Preserve type source info.
267 Result = GetTypeFromParser(DS.getTypeRep());
Chris Lattner958858e2008-02-20 21:40:32 +0000268 assert(!Result.isNull() && "Didn't get a type for typeof?");
Steve Naroffd1861fd2007-07-31 12:34:36 +0000269 // TypeQuals handled by caller.
Chris Lattnerfab5b452008-02-20 23:53:49 +0000270 Result = Context.getTypeOfType(Result);
Chris Lattner958858e2008-02-20 21:40:32 +0000271 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000272 case DeclSpec::TST_typeofExpr: {
273 Expr *E = static_cast<Expr *>(DS.getTypeRep());
274 assert(E && "Didn't get an expression for typeof?");
275 // TypeQuals handled by caller.
Douglas Gregor72564e72009-02-26 23:50:07 +0000276 Result = Context.getTypeOfExprType(E);
Chris Lattner958858e2008-02-20 21:40:32 +0000277 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000278 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000279 case DeclSpec::TST_decltype: {
280 Expr *E = static_cast<Expr *>(DS.getTypeRep());
281 assert(E && "Didn't get an expression for decltype?");
282 // TypeQuals handled by caller.
Anders Carlssonaf017e62009-06-29 22:58:55 +0000283 Result = BuildDecltypeType(E);
284 if (Result.isNull()) {
285 Result = Context.IntTy;
286 isInvalid = true;
287 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000288 break;
289 }
Anders Carlssone89d1592009-06-26 18:41:36 +0000290 case DeclSpec::TST_auto: {
291 // TypeQuals handled by caller.
292 Result = Context.UndeducedAutoTy;
293 break;
294 }
Mike Stump1eb44332009-09-09 15:08:12 +0000295
Douglas Gregor809070a2009-02-18 17:45:20 +0000296 case DeclSpec::TST_error:
Chris Lattner5153ee62009-04-25 08:47:54 +0000297 Result = Context.IntTy;
298 isInvalid = true;
299 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000300 }
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Chris Lattner958858e2008-02-20 21:40:32 +0000302 // Handle complex types.
Douglas Gregorf244cd72009-02-14 21:06:05 +0000303 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
304 if (getLangOptions().Freestanding)
305 Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
Chris Lattnerfab5b452008-02-20 23:53:49 +0000306 Result = Context.getComplexType(Result);
Douglas Gregorf244cd72009-02-14 21:06:05 +0000307 }
Mike Stump1eb44332009-09-09 15:08:12 +0000308
Chris Lattner958858e2008-02-20 21:40:32 +0000309 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
310 "FIXME: imaginary types not supported yet!");
Mike Stump1eb44332009-09-09 15:08:12 +0000311
Chris Lattner38d8b982008-02-20 22:04:11 +0000312 // See if there are any attributes on the declspec that apply to the type (as
313 // opposed to the decl).
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000314 if (const AttributeList *AL = DS.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000315 ProcessTypeAttributeList(Result, AL);
Mike Stump1eb44332009-09-09 15:08:12 +0000316
Chris Lattner96b77fc2008-04-02 06:50:17 +0000317 // Apply const/volatile/restrict qualifiers to T.
318 if (unsigned TypeQuals = DS.getTypeQualifiers()) {
319
320 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
321 // or incomplete types shall not be restrict-qualified." C++ also allows
322 // restrict-qualified references.
John McCall0953e762009-09-24 19:53:00 +0000323 if (TypeQuals & DeclSpec::TQ_restrict) {
Daniel Dunbarbb710012009-02-26 19:13:44 +0000324 if (Result->isPointerType() || Result->isReferenceType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000325 QualType EltTy = Result->isPointerType() ?
Ted Kremenek6217b802009-07-29 21:53:49 +0000326 Result->getAs<PointerType>()->getPointeeType() :
327 Result->getAs<ReferenceType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Douglas Gregorbad0e652009-03-24 20:32:41 +0000329 // If we have a pointer or reference, the pointee must have an object
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000330 // incomplete type.
331 if (!EltTy->isIncompleteOrObjectType()) {
332 Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000333 diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattnerd1625842008-11-24 06:25:27 +0000334 << EltTy << DS.getSourceRange();
John McCall0953e762009-09-24 19:53:00 +0000335 TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000336 }
337 } else {
Chris Lattner96b77fc2008-04-02 06:50:17 +0000338 Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000339 diag::err_typecheck_invalid_restrict_not_pointer)
Chris Lattnerd1625842008-11-24 06:25:27 +0000340 << Result << DS.getSourceRange();
John McCall0953e762009-09-24 19:53:00 +0000341 TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
Chris Lattner96b77fc2008-04-02 06:50:17 +0000342 }
Chris Lattner96b77fc2008-04-02 06:50:17 +0000343 }
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Chris Lattner96b77fc2008-04-02 06:50:17 +0000345 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
346 // of a function type includes any type qualifiers, the behavior is
347 // undefined."
348 if (Result->isFunctionType() && TypeQuals) {
349 // Get some location to point at, either the C or V location.
350 SourceLocation Loc;
John McCall0953e762009-09-24 19:53:00 +0000351 if (TypeQuals & DeclSpec::TQ_const)
Chris Lattner96b77fc2008-04-02 06:50:17 +0000352 Loc = DS.getConstSpecLoc();
John McCall0953e762009-09-24 19:53:00 +0000353 else if (TypeQuals & DeclSpec::TQ_volatile)
Chris Lattner96b77fc2008-04-02 06:50:17 +0000354 Loc = DS.getVolatileSpecLoc();
John McCall0953e762009-09-24 19:53:00 +0000355 else {
356 assert((TypeQuals & DeclSpec::TQ_restrict) &&
357 "Has CVR quals but not C, V, or R?");
358 Loc = DS.getRestrictSpecLoc();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000359 }
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000360 Diag(Loc, diag::warn_typecheck_function_qualifiers)
Chris Lattnerd1625842008-11-24 06:25:27 +0000361 << Result << DS.getSourceRange();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000362 }
Mike Stump1eb44332009-09-09 15:08:12 +0000363
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000364 // C++ [dcl.ref]p1:
365 // Cv-qualified references are ill-formed except when the
366 // cv-qualifiers are introduced through the use of a typedef
367 // (7.1.3) or of a template type argument (14.3), in which
368 // case the cv-qualifiers are ignored.
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000369 // FIXME: Shouldn't we be checking SCS_typedef here?
370 if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000371 TypeQuals && Result->isReferenceType()) {
John McCall0953e762009-09-24 19:53:00 +0000372 TypeQuals &= ~DeclSpec::TQ_const;
373 TypeQuals &= ~DeclSpec::TQ_volatile;
Mike Stump1eb44332009-09-09 15:08:12 +0000374 }
375
John McCall0953e762009-09-24 19:53:00 +0000376 Qualifiers Quals = Qualifiers::fromCVRMask(TypeQuals);
377 Result = Context.getQualifiedType(Result, Quals);
Chris Lattner96b77fc2008-04-02 06:50:17 +0000378 }
John McCall0953e762009-09-24 19:53:00 +0000379
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000380 return Result;
381}
382
Douglas Gregorcd281c32009-02-28 00:25:32 +0000383static std::string getPrintableNameForEntity(DeclarationName Entity) {
384 if (Entity)
385 return Entity.getAsString();
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Douglas Gregorcd281c32009-02-28 00:25:32 +0000387 return "type name";
388}
389
390/// \brief Build a pointer type.
391///
392/// \param T The type to which we'll be building a pointer.
393///
394/// \param Quals The cvr-qualifiers to be applied to the pointer type.
395///
396/// \param Loc The location of the entity whose type involves this
397/// pointer type or, if there is no such entity, the location of the
398/// type that will have pointer type.
399///
400/// \param Entity The name of the entity that involves the pointer
401/// type, if known.
402///
403/// \returns A suitable pointer type, if there are no
404/// errors. Otherwise, returns a NULL type.
Mike Stump1eb44332009-09-09 15:08:12 +0000405QualType Sema::BuildPointerType(QualType T, unsigned Quals,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000406 SourceLocation Loc, DeclarationName Entity) {
407 if (T->isReferenceType()) {
408 // C++ 8.3.2p4: There shall be no ... pointers to references ...
409 Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
410 << getPrintableNameForEntity(Entity);
411 return QualType();
412 }
413
John McCall0953e762009-09-24 19:53:00 +0000414 Qualifiers Qs = Qualifiers::fromCVRMask(Quals);
415
Douglas Gregorcd281c32009-02-28 00:25:32 +0000416 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
417 // object or incomplete types shall not be restrict-qualified."
John McCall0953e762009-09-24 19:53:00 +0000418 if (Qs.hasRestrict() && !T->isIncompleteOrObjectType()) {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000419 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
420 << T;
John McCall0953e762009-09-24 19:53:00 +0000421 Qs.removeRestrict();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000422 }
423
424 // Build the pointer type.
John McCall0953e762009-09-24 19:53:00 +0000425 return Context.getQualifiedType(Context.getPointerType(T), Qs);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000426}
427
428/// \brief Build a reference type.
429///
430/// \param T The type to which we'll be building a reference.
431///
John McCall0953e762009-09-24 19:53:00 +0000432/// \param CVR The cvr-qualifiers to be applied to the reference type.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000433///
434/// \param Loc The location of the entity whose type involves this
435/// reference type or, if there is no such entity, the location of the
436/// type that will have reference type.
437///
438/// \param Entity The name of the entity that involves the reference
439/// type, if known.
440///
441/// \returns A suitable reference type, if there are no
442/// errors. Otherwise, returns a NULL type.
John McCall54e14c42009-10-22 22:37:11 +0000443QualType Sema::BuildReferenceType(QualType T, bool SpelledAsLValue,
444 unsigned CVR, SourceLocation Loc,
445 DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000446 Qualifiers Quals = Qualifiers::fromCVRMask(CVR);
John McCall54e14c42009-10-22 22:37:11 +0000447
448 bool LValueRef = SpelledAsLValue || T->getAs<LValueReferenceType>();
449
450 // C++0x [dcl.typedef]p9: If a typedef TD names a type that is a
451 // reference to a type T, and attempt to create the type "lvalue
452 // reference to cv TD" creates the type "lvalue reference to T".
453 // We use the qualifiers (restrict or none) of the original reference,
454 // not the new ones. This is consistent with GCC.
455
456 // C++ [dcl.ref]p4: There shall be no references to references.
457 //
458 // According to C++ DR 106, references to references are only
459 // diagnosed when they are written directly (e.g., "int & &"),
460 // but not when they happen via a typedef:
461 //
462 // typedef int& intref;
463 // typedef intref& intref2;
464 //
465 // Parser::ParseDeclaratorInternal diagnoses the case where
466 // references are written directly; here, we handle the
467 // collapsing of references-to-references as described in C++
468 // DR 106 and amended by C++ DR 540.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000469
470 // C++ [dcl.ref]p1:
Eli Friedman33a31382009-08-05 19:21:58 +0000471 // A declarator that specifies the type "reference to cv void"
Douglas Gregorcd281c32009-02-28 00:25:32 +0000472 // is ill-formed.
473 if (T->isVoidType()) {
474 Diag(Loc, diag::err_reference_to_void);
475 return QualType();
476 }
477
478 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
479 // object or incomplete types shall not be restrict-qualified."
John McCall0953e762009-09-24 19:53:00 +0000480 if (Quals.hasRestrict() && !T->isIncompleteOrObjectType()) {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000481 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
482 << T;
John McCall0953e762009-09-24 19:53:00 +0000483 Quals.removeRestrict();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000484 }
485
486 // C++ [dcl.ref]p1:
487 // [...] Cv-qualified references are ill-formed except when the
488 // cv-qualifiers are introduced through the use of a typedef
489 // (7.1.3) or of a template type argument (14.3), in which case
490 // the cv-qualifiers are ignored.
491 //
492 // We diagnose extraneous cv-qualifiers for the non-typedef,
493 // non-template type argument case within the parser. Here, we just
494 // ignore any extraneous cv-qualifiers.
John McCall0953e762009-09-24 19:53:00 +0000495 Quals.removeConst();
496 Quals.removeVolatile();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000497
498 // Handle restrict on references.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000499 if (LValueRef)
John McCall54e14c42009-10-22 22:37:11 +0000500 return Context.getQualifiedType(
501 Context.getLValueReferenceType(T, SpelledAsLValue), Quals);
John McCall0953e762009-09-24 19:53:00 +0000502 return Context.getQualifiedType(Context.getRValueReferenceType(T), Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000503}
504
505/// \brief Build an array type.
506///
507/// \param T The type of each element in the array.
508///
509/// \param ASM C99 array size modifier (e.g., '*', 'static').
Mike Stump1eb44332009-09-09 15:08:12 +0000510///
511/// \param ArraySize Expression describing the size of the array.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000512///
513/// \param Quals The cvr-qualifiers to be applied to the array's
514/// element type.
515///
516/// \param Loc The location of the entity whose type involves this
517/// array type or, if there is no such entity, the location of the
518/// type that will have array type.
519///
520/// \param Entity The name of the entity that involves the array
521/// type, if known.
522///
523/// \returns A suitable array type, if there are no errors. Otherwise,
524/// returns a NULL type.
525QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
526 Expr *ArraySize, unsigned Quals,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000527 SourceRange Brackets, DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000528
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000529 SourceLocation Loc = Brackets.getBegin();
Mike Stump1eb44332009-09-09 15:08:12 +0000530 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000531 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
Mike Stump1eb44332009-09-09 15:08:12 +0000532 if (RequireCompleteType(Loc, T,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000533 diag::err_illegal_decl_array_incomplete_type))
534 return QualType();
535
536 if (T->isFunctionType()) {
537 Diag(Loc, diag::err_illegal_decl_array_of_functions)
538 << getPrintableNameForEntity(Entity);
539 return QualType();
540 }
Mike Stump1eb44332009-09-09 15:08:12 +0000541
Douglas Gregorcd281c32009-02-28 00:25:32 +0000542 // C++ 8.3.2p4: There shall be no ... arrays of references ...
543 if (T->isReferenceType()) {
544 Diag(Loc, diag::err_illegal_decl_array_of_references)
545 << getPrintableNameForEntity(Entity);
546 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000547 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000548
Anders Carlssone7cf07d2009-06-26 19:33:28 +0000549 if (Context.getCanonicalType(T) == Context.UndeducedAutoTy) {
Mike Stump1eb44332009-09-09 15:08:12 +0000550 Diag(Loc, diag::err_illegal_decl_array_of_auto)
Anders Carlssone7cf07d2009-06-26 19:33:28 +0000551 << getPrintableNameForEntity(Entity);
552 return QualType();
553 }
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Ted Kremenek6217b802009-07-29 21:53:49 +0000555 if (const RecordType *EltTy = T->getAs<RecordType>()) {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000556 // If the element type is a struct or union that contains a variadic
557 // array, accept it as a GNU extension: C99 6.7.2.1p2.
558 if (EltTy->getDecl()->hasFlexibleArrayMember())
559 Diag(Loc, diag::ext_flexible_array_in_array) << T;
560 } else if (T->isObjCInterfaceType()) {
Chris Lattnerc7c11b12009-04-27 01:55:56 +0000561 Diag(Loc, diag::err_objc_array_of_interfaces) << T;
562 return QualType();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000563 }
Mike Stump1eb44332009-09-09 15:08:12 +0000564
Douglas Gregorcd281c32009-02-28 00:25:32 +0000565 // C99 6.7.5.2p1: The size expression shall have integer type.
566 if (ArraySize && !ArraySize->isTypeDependent() &&
567 !ArraySize->getType()->isIntegerType()) {
568 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
569 << ArraySize->getType() << ArraySize->getSourceRange();
570 ArraySize->Destroy(Context);
571 return QualType();
572 }
573 llvm::APSInt ConstVal(32);
574 if (!ArraySize) {
Eli Friedmanf91f5c82009-04-26 21:57:51 +0000575 if (ASM == ArrayType::Star)
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000576 T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets);
Eli Friedmanf91f5c82009-04-26 21:57:51 +0000577 else
578 T = Context.getIncompleteArrayType(T, ASM, Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000579 } else if (ArraySize->isValueDependent()) {
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000580 T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000581 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
582 (!T->isDependentType() && !T->isConstantSizeType())) {
583 // Per C99, a variable array is an array with either a non-constant
584 // size or an element type that has a non-constant-size
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000585 T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000586 } else {
587 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
588 // have a value greater than zero.
589 if (ConstVal.isSigned()) {
590 if (ConstVal.isNegative()) {
591 Diag(ArraySize->getLocStart(),
592 diag::err_typecheck_negative_array_size)
593 << ArraySize->getSourceRange();
594 return QualType();
595 } else if (ConstVal == 0) {
596 // GCC accepts zero sized static arrays.
597 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size)
598 << ArraySize->getSourceRange();
599 }
Mike Stump1eb44332009-09-09 15:08:12 +0000600 }
John McCall46a617a2009-10-16 00:14:28 +0000601 T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000602 }
603 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
604 if (!getLangOptions().C99) {
Mike Stump1eb44332009-09-09 15:08:12 +0000605 if (ArraySize && !ArraySize->isTypeDependent() &&
606 !ArraySize->isValueDependent() &&
Douglas Gregorcd281c32009-02-28 00:25:32 +0000607 !ArraySize->isIntegerConstantExpr(Context))
Douglas Gregor043cad22009-09-11 00:18:58 +0000608 Diag(Loc, getLangOptions().CPlusPlus? diag::err_vla_cxx : diag::ext_vla);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000609 else if (ASM != ArrayType::Normal || Quals != 0)
Douglas Gregor043cad22009-09-11 00:18:58 +0000610 Diag(Loc,
611 getLangOptions().CPlusPlus? diag::err_c99_array_usage_cxx
612 : diag::ext_c99_array_usage);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000613 }
614
615 return T;
616}
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000617
618/// \brief Build an ext-vector type.
619///
620/// Run the required checks for the extended vector type.
Mike Stump1eb44332009-09-09 15:08:12 +0000621QualType Sema::BuildExtVectorType(QualType T, ExprArg ArraySize,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000622 SourceLocation AttrLoc) {
623
624 Expr *Arg = (Expr *)ArraySize.get();
625
626 // unlike gcc's vector_size attribute, we do not allow vectors to be defined
627 // in conjunction with complex types (pointers, arrays, functions, etc.).
Mike Stump1eb44332009-09-09 15:08:12 +0000628 if (!T->isDependentType() &&
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000629 !T->isIntegerType() && !T->isRealFloatingType()) {
630 Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T;
631 return QualType();
632 }
633
634 if (!Arg->isTypeDependent() && !Arg->isValueDependent()) {
635 llvm::APSInt vecSize(32);
636 if (!Arg->isIntegerConstantExpr(vecSize, Context)) {
637 Diag(AttrLoc, diag::err_attribute_argument_not_int)
638 << "ext_vector_type" << Arg->getSourceRange();
639 return QualType();
640 }
Mike Stump1eb44332009-09-09 15:08:12 +0000641
642 // unlike gcc's vector_size attribute, the size is specified as the
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000643 // number of elements, not the number of bytes.
Mike Stump1eb44332009-09-09 15:08:12 +0000644 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
645
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000646 if (vectorSize == 0) {
647 Diag(AttrLoc, diag::err_attribute_zero_size)
648 << Arg->getSourceRange();
649 return QualType();
650 }
Mike Stump1eb44332009-09-09 15:08:12 +0000651
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000652 if (!T->isDependentType())
653 return Context.getExtVectorType(T, vectorSize);
Mike Stump1eb44332009-09-09 15:08:12 +0000654 }
655
656 return Context.getDependentSizedExtVectorType(T, ArraySize.takeAs<Expr>(),
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000657 AttrLoc);
658}
Mike Stump1eb44332009-09-09 15:08:12 +0000659
Douglas Gregor724651c2009-02-28 01:04:19 +0000660/// \brief Build a function type.
661///
662/// This routine checks the function type according to C++ rules and
663/// under the assumption that the result type and parameter types have
664/// just been instantiated from a template. It therefore duplicates
Douglas Gregor2943aed2009-03-03 04:44:36 +0000665/// some of the behavior of GetTypeForDeclarator, but in a much
Douglas Gregor724651c2009-02-28 01:04:19 +0000666/// simpler form that is only suitable for this narrow use case.
667///
668/// \param T The return type of the function.
669///
670/// \param ParamTypes The parameter types of the function. This array
671/// will be modified to account for adjustments to the types of the
672/// function parameters.
673///
674/// \param NumParamTypes The number of parameter types in ParamTypes.
675///
676/// \param Variadic Whether this is a variadic function type.
677///
678/// \param Quals The cvr-qualifiers to be applied to the function type.
679///
680/// \param Loc The location of the entity whose type involves this
681/// function type or, if there is no such entity, the location of the
682/// type that will have function type.
683///
684/// \param Entity The name of the entity that involves the function
685/// type, if known.
686///
687/// \returns A suitable function type, if there are no
688/// errors. Otherwise, returns a NULL type.
689QualType Sema::BuildFunctionType(QualType T,
Mike Stump1eb44332009-09-09 15:08:12 +0000690 QualType *ParamTypes,
Douglas Gregor724651c2009-02-28 01:04:19 +0000691 unsigned NumParamTypes,
692 bool Variadic, unsigned Quals,
693 SourceLocation Loc, DeclarationName Entity) {
694 if (T->isArrayType() || T->isFunctionType()) {
695 Diag(Loc, diag::err_func_returning_array_function) << T;
696 return QualType();
697 }
Mike Stump1eb44332009-09-09 15:08:12 +0000698
Douglas Gregor724651c2009-02-28 01:04:19 +0000699 bool Invalid = false;
700 for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) {
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000701 QualType ParamType = adjustParameterType(ParamTypes[Idx]);
702 if (ParamType->isVoidType()) {
Douglas Gregor724651c2009-02-28 01:04:19 +0000703 Diag(Loc, diag::err_param_with_void_type);
704 Invalid = true;
705 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000706
John McCall54e14c42009-10-22 22:37:11 +0000707 ParamTypes[Idx] = ParamType;
Douglas Gregor724651c2009-02-28 01:04:19 +0000708 }
709
710 if (Invalid)
711 return QualType();
712
Mike Stump1eb44332009-09-09 15:08:12 +0000713 return Context.getFunctionType(T, ParamTypes, NumParamTypes, Variadic,
Douglas Gregor724651c2009-02-28 01:04:19 +0000714 Quals);
715}
Mike Stump1eb44332009-09-09 15:08:12 +0000716
Douglas Gregor949bf692009-06-09 22:17:39 +0000717/// \brief Build a member pointer type \c T Class::*.
718///
719/// \param T the type to which the member pointer refers.
720/// \param Class the class type into which the member pointer points.
John McCall0953e762009-09-24 19:53:00 +0000721/// \param CVR Qualifiers applied to the member pointer type
Douglas Gregor949bf692009-06-09 22:17:39 +0000722/// \param Loc the location where this type begins
723/// \param Entity the name of the entity that will have this member pointer type
724///
725/// \returns a member pointer type, if successful, or a NULL type if there was
726/// an error.
Mike Stump1eb44332009-09-09 15:08:12 +0000727QualType Sema::BuildMemberPointerType(QualType T, QualType Class,
John McCall0953e762009-09-24 19:53:00 +0000728 unsigned CVR, SourceLocation Loc,
Douglas Gregor949bf692009-06-09 22:17:39 +0000729 DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000730 Qualifiers Quals = Qualifiers::fromCVRMask(CVR);
731
Douglas Gregor949bf692009-06-09 22:17:39 +0000732 // Verify that we're not building a pointer to pointer to function with
733 // exception specification.
734 if (CheckDistantExceptionSpec(T)) {
735 Diag(Loc, diag::err_distant_exception_spec);
736
737 // FIXME: If we're doing this as part of template instantiation,
738 // we should return immediately.
739
740 // Build the type anyway, but use the canonical type so that the
741 // exception specifiers are stripped off.
742 T = Context.getCanonicalType(T);
743 }
744
745 // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member
746 // with reference type, or "cv void."
747 if (T->isReferenceType()) {
Anders Carlsson8d4655d2009-06-30 00:06:57 +0000748 Diag(Loc, diag::err_illegal_decl_mempointer_to_reference)
Douglas Gregor949bf692009-06-09 22:17:39 +0000749 << (Entity? Entity.getAsString() : "type name");
750 return QualType();
751 }
752
753 if (T->isVoidType()) {
754 Diag(Loc, diag::err_illegal_decl_mempointer_to_void)
755 << (Entity? Entity.getAsString() : "type name");
756 return QualType();
757 }
758
759 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
760 // object or incomplete types shall not be restrict-qualified."
John McCall0953e762009-09-24 19:53:00 +0000761 if (Quals.hasRestrict() && !T->isIncompleteOrObjectType()) {
Douglas Gregor949bf692009-06-09 22:17:39 +0000762 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
763 << T;
764
765 // FIXME: If we're doing this as part of template instantiation,
766 // we should return immediately.
John McCall0953e762009-09-24 19:53:00 +0000767 Quals.removeRestrict();
Douglas Gregor949bf692009-06-09 22:17:39 +0000768 }
769
770 if (!Class->isDependentType() && !Class->isRecordType()) {
771 Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class;
772 return QualType();
773 }
774
John McCall0953e762009-09-24 19:53:00 +0000775 return Context.getQualifiedType(
776 Context.getMemberPointerType(T, Class.getTypePtr()), Quals);
Douglas Gregor949bf692009-06-09 22:17:39 +0000777}
Mike Stump1eb44332009-09-09 15:08:12 +0000778
Anders Carlsson9a917e42009-06-12 22:56:54 +0000779/// \brief Build a block pointer type.
780///
781/// \param T The type to which we'll be building a block pointer.
782///
John McCall0953e762009-09-24 19:53:00 +0000783/// \param CVR The cvr-qualifiers to be applied to the block pointer type.
Anders Carlsson9a917e42009-06-12 22:56:54 +0000784///
785/// \param Loc The location of the entity whose type involves this
786/// block pointer type or, if there is no such entity, the location of the
787/// type that will have block pointer type.
788///
789/// \param Entity The name of the entity that involves the block pointer
790/// type, if known.
791///
792/// \returns A suitable block pointer type, if there are no
793/// errors. Otherwise, returns a NULL type.
John McCall0953e762009-09-24 19:53:00 +0000794QualType Sema::BuildBlockPointerType(QualType T, unsigned CVR,
Mike Stump1eb44332009-09-09 15:08:12 +0000795 SourceLocation Loc,
Anders Carlsson9a917e42009-06-12 22:56:54 +0000796 DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000797 if (!T->isFunctionType()) {
Anders Carlsson9a917e42009-06-12 22:56:54 +0000798 Diag(Loc, diag::err_nonfunction_block_type);
799 return QualType();
800 }
Mike Stump1eb44332009-09-09 15:08:12 +0000801
John McCall0953e762009-09-24 19:53:00 +0000802 Qualifiers Quals = Qualifiers::fromCVRMask(CVR);
803 return Context.getQualifiedType(Context.getBlockPointerType(T), Quals);
Anders Carlsson9a917e42009-06-12 22:56:54 +0000804}
805
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000806QualType Sema::GetTypeFromParser(TypeTy *Ty, DeclaratorInfo **DInfo) {
807 QualType QT = QualType::getFromOpaquePtr(Ty);
808 DeclaratorInfo *DI = 0;
809 if (LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) {
810 QT = LIT->getType();
811 DI = LIT->getDeclaratorInfo();
812 }
Mike Stump1eb44332009-09-09 15:08:12 +0000813
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000814 if (DInfo) *DInfo = DI;
815 return QT;
816}
817
Chris Lattner778ed742009-10-25 17:36:50 +0000818
819/// isOmittedBlockReturnType - Return true if this declarator is missing a
820/// return type because this is a omitted return type on a block literal.
821static bool isOmittedBlockReturnType(const Declarator &D, unsigned Skip) {
822 if (D.getContext() != Declarator::BlockLiteralContext ||
823 Skip != 0 || D.getDeclSpec().hasTypeSpecifier())
824 return false;
825
826 if (D.getNumTypeObjects() == 0)
827 return true;
828
829 if (D.getNumTypeObjects() == 1 &&
830 D.getTypeObject(0).Kind == DeclaratorChunk::Function)
831 return true;
832
833 return false;
834}
835
Mike Stump98eb8a72009-02-04 22:31:32 +0000836/// GetTypeForDeclarator - Convert the type for the specified
837/// declarator to Type instances. Skip the outermost Skip type
838/// objects.
Douglas Gregor402abb52009-05-28 23:31:59 +0000839///
840/// If OwnedDecl is non-NULL, and this declarator's decl-specifier-seq
841/// owns the declaration of a type (e.g., the definition of a struct
842/// type), then *OwnedDecl will receive the owned declaration.
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000843QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S,
844 DeclaratorInfo **DInfo, unsigned Skip,
Douglas Gregor402abb52009-05-28 23:31:59 +0000845 TagDecl **OwnedDecl) {
Chris Lattnerb23deda2007-08-28 16:40:32 +0000846 // long long is a C99 feature.
Chris Lattnerd1eb3322007-08-28 16:41:29 +0000847 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Chris Lattnerb23deda2007-08-28 16:40:32 +0000848 D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
849 Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
Douglas Gregor930d8b52009-01-30 22:09:00 +0000850
851 // Determine the type of the declarator. Not all forms of declarator
852 // have a type.
853 QualType T;
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000854
Douglas Gregor930d8b52009-01-30 22:09:00 +0000855 switch (D.getKind()) {
856 case Declarator::DK_Abstract:
857 case Declarator::DK_Normal:
Douglas Gregordb422df2009-09-25 21:45:23 +0000858 case Declarator::DK_Operator:
859 case Declarator::DK_TemplateId: {
Chris Lattner3f84ad22009-04-22 05:27:59 +0000860 const DeclSpec &DS = D.getDeclSpec();
Chris Lattner778ed742009-10-25 17:36:50 +0000861 if (isOmittedBlockReturnType(D, Skip)) {
Mike Stump98eb8a72009-02-04 22:31:32 +0000862 // We default to a dependent type initially. Can be modified by
863 // the first return statement.
864 T = Context.DependentTy;
Chris Lattner3f84ad22009-04-22 05:27:59 +0000865 } else {
Chris Lattnereaaebc72009-04-25 08:06:05 +0000866 bool isInvalid = false;
John McCall54e14c42009-10-22 22:37:11 +0000867 T = ConvertDeclSpecToType(DS, D.getIdentifierLoc(), isInvalid);
Chris Lattnereaaebc72009-04-25 08:06:05 +0000868 if (isInvalid)
869 D.setInvalidType(true);
Douglas Gregor402abb52009-05-28 23:31:59 +0000870 else if (OwnedDecl && DS.isTypeSpecOwned())
871 *OwnedDecl = cast<TagDecl>((Decl *)DS.getTypeRep());
Douglas Gregor809070a2009-02-18 17:45:20 +0000872 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000873 break;
Mike Stump98eb8a72009-02-04 22:31:32 +0000874 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000875
876 case Declarator::DK_Constructor:
877 case Declarator::DK_Destructor:
878 case Declarator::DK_Conversion:
879 // Constructors and destructors don't have return types. Use
880 // "void" instead. Conversion operators will check their return
881 // types separately.
882 T = Context.VoidTy;
883 break;
884 }
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000885
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000886 if (T == Context.UndeducedAutoTy) {
887 int Error = -1;
Mike Stump1eb44332009-09-09 15:08:12 +0000888
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000889 switch (D.getContext()) {
890 case Declarator::KNRTypeListContext:
891 assert(0 && "K&R type lists aren't allowed in C++");
892 break;
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000893 case Declarator::PrototypeContext:
894 Error = 0; // Function prototype
895 break;
896 case Declarator::MemberContext:
897 switch (cast<TagDecl>(CurContext)->getTagKind()) {
898 case TagDecl::TK_enum: assert(0 && "unhandled tag kind"); break;
899 case TagDecl::TK_struct: Error = 1; /* Struct member */ break;
900 case TagDecl::TK_union: Error = 2; /* Union member */ break;
901 case TagDecl::TK_class: Error = 3; /* Class member */ break;
Mike Stump1eb44332009-09-09 15:08:12 +0000902 }
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000903 break;
904 case Declarator::CXXCatchContext:
905 Error = 4; // Exception declaration
906 break;
907 case Declarator::TemplateParamContext:
908 Error = 5; // Template parameter
909 break;
910 case Declarator::BlockLiteralContext:
911 Error = 6; // Block literal
912 break;
913 case Declarator::FileContext:
914 case Declarator::BlockContext:
915 case Declarator::ForContext:
916 case Declarator::ConditionContext:
917 case Declarator::TypeNameContext:
918 break;
919 }
920
921 if (Error != -1) {
922 Diag(D.getDeclSpec().getTypeSpecTypeLoc(), diag::err_auto_not_allowed)
923 << Error;
924 T = Context.IntTy;
925 D.setInvalidType(true);
926 }
927 }
Mike Stump1eb44332009-09-09 15:08:12 +0000928
Douglas Gregorcd281c32009-02-28 00:25:32 +0000929 // The name we're declaring, if any.
930 DeclarationName Name;
931 if (D.getIdentifier())
932 Name = D.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Mike Stump98eb8a72009-02-04 22:31:32 +0000934 // Walk the DeclTypeInfo, building the recursive type as we go.
935 // DeclTypeInfos are ordered from the identifier out, which is
936 // opposite of what we want :).
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000937 for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) {
938 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1+Skip);
Reid Spencer5f016e22007-07-11 17:01:13 +0000939 switch (DeclType.Kind) {
940 default: assert(0 && "Unknown decltype!");
Steve Naroff5618bd42008-08-27 16:04:49 +0000941 case DeclaratorChunk::BlockPointer:
Chris Lattner9af55002009-03-27 04:18:06 +0000942 // If blocks are disabled, emit an error.
943 if (!LangOpts.Blocks)
944 Diag(DeclType.Loc, diag::err_blocks_disable);
Mike Stump1eb44332009-09-09 15:08:12 +0000945
946 T = BuildBlockPointerType(T, DeclType.Cls.TypeQuals, D.getIdentifierLoc(),
Anders Carlsson9a917e42009-06-12 22:56:54 +0000947 Name);
Steve Naroff5618bd42008-08-27 16:04:49 +0000948 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000949 case DeclaratorChunk::Pointer:
Sebastian Redl6a7330c2009-05-29 15:01:05 +0000950 // Verify that we're not building a pointer to pointer to function with
951 // exception specification.
952 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
953 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
954 D.setInvalidType(true);
955 // Build the type anyway.
956 }
Steve Naroff14108da2009-07-10 23:34:53 +0000957 if (getLangOptions().ObjC1 && T->isObjCInterfaceType()) {
John McCall183700f2009-09-21 23:43:11 +0000958 const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>();
Steve Naroff14108da2009-07-10 23:34:53 +0000959 T = Context.getObjCObjectPointerType(T,
Steve Naroff67ef8ea2009-07-20 17:56:53 +0000960 (ObjCProtocolDecl **)OIT->qual_begin(),
961 OIT->getNumProtocols());
Steve Naroff14108da2009-07-10 23:34:53 +0000962 break;
963 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000964 T = BuildPointerType(T, DeclType.Ptr.TypeQuals, DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000965 break;
John McCall0953e762009-09-24 19:53:00 +0000966 case DeclaratorChunk::Reference: {
967 Qualifiers Quals;
968 if (DeclType.Ref.HasRestrict) Quals.addRestrict();
969
Sebastian Redl6a7330c2009-05-29 15:01:05 +0000970 // Verify that we're not building a reference to pointer to function with
971 // exception specification.
972 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
973 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
974 D.setInvalidType(true);
975 // Build the type anyway.
976 }
John McCall0953e762009-09-24 19:53:00 +0000977 T = BuildReferenceType(T, DeclType.Ref.LValueRef, Quals,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000978 DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000979 break;
John McCall0953e762009-09-24 19:53:00 +0000980 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000981 case DeclaratorChunk::Array: {
Sebastian Redl6a7330c2009-05-29 15:01:05 +0000982 // Verify that we're not building an array of pointers to function with
983 // exception specification.
984 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
985 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
986 D.setInvalidType(true);
987 // Build the type anyway.
988 }
Chris Lattnerfd89bc82008-04-02 01:05:10 +0000989 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner94f81fd2007-08-28 16:54:00 +0000990 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000991 ArrayType::ArraySizeModifier ASM;
992 if (ATI.isStar)
993 ASM = ArrayType::Star;
994 else if (ATI.hasStatic)
995 ASM = ArrayType::Static;
996 else
997 ASM = ArrayType::Normal;
Eli Friedmanf91f5c82009-04-26 21:57:51 +0000998 if (ASM == ArrayType::Star &&
999 D.getContext() != Declarator::PrototypeContext) {
1000 // FIXME: This check isn't quite right: it allows star in prototypes
1001 // for function definitions, and disallows some edge cases detailed
1002 // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html
1003 Diag(DeclType.Loc, diag::err_array_star_outside_prototype);
1004 ASM = ArrayType::Normal;
1005 D.setInvalidType(true);
1006 }
John McCall0953e762009-09-24 19:53:00 +00001007 T = BuildArrayType(T, ASM, ArraySize,
1008 Qualifiers::fromCVRMask(ATI.TypeQuals),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001009 SourceRange(DeclType.Loc, DeclType.EndLoc), Name);
Reid Spencer5f016e22007-07-11 17:01:13 +00001010 break;
1011 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001012 case DeclaratorChunk::Function: {
Reid Spencer5f016e22007-07-11 17:01:13 +00001013 // If the function declarator has a prototype (i.e. it is not () and
1014 // does not have a K&R-style identifier list), then the arguments are part
1015 // of the type, otherwise the argument list is ().
1016 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Sebastian Redl3cc97262009-05-31 11:47:27 +00001017
Chris Lattnercd881292007-12-19 05:31:29 +00001018 // C99 6.7.5.3p1: The return type may not be a function or array type.
Chris Lattner68cfd492007-12-19 18:01:43 +00001019 if (T->isArrayType() || T->isFunctionType()) {
Chris Lattnerd1625842008-11-24 06:25:27 +00001020 Diag(DeclType.Loc, diag::err_func_returning_array_function) << T;
Chris Lattnercd881292007-12-19 05:31:29 +00001021 T = Context.IntTy;
1022 D.setInvalidType(true);
1023 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001024
Douglas Gregor402abb52009-05-28 23:31:59 +00001025 if (getLangOptions().CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) {
1026 // C++ [dcl.fct]p6:
1027 // Types shall not be defined in return or parameter types.
1028 TagDecl *Tag = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep());
1029 if (Tag->isDefinition())
1030 Diag(Tag->getLocation(), diag::err_type_defined_in_result_type)
1031 << Context.getTypeDeclType(Tag);
1032 }
1033
Sebastian Redl3cc97262009-05-31 11:47:27 +00001034 // Exception specs are not allowed in typedefs. Complain, but add it
1035 // anyway.
1036 if (FTI.hasExceptionSpec &&
1037 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
1038 Diag(FTI.getThrowLoc(), diag::err_exception_spec_in_typedef);
1039
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001040 if (FTI.NumArgs == 0) {
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001041 if (getLangOptions().CPlusPlus) {
1042 // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the
1043 // function takes no arguments.
Sebastian Redl465226e2009-05-27 22:11:52 +00001044 llvm::SmallVector<QualType, 4> Exceptions;
1045 Exceptions.reserve(FTI.NumExceptions);
Mike Stump1eb44332009-09-09 15:08:12 +00001046 for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001047 // FIXME: Preserve type source info.
1048 QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty);
Sebastian Redlef65f062009-05-29 18:02:33 +00001049 // Check that the type is valid for an exception spec, and drop it
1050 // if not.
1051 if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1052 Exceptions.push_back(ET);
1053 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001054 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, FTI.TypeQuals,
1055 FTI.hasExceptionSpec,
1056 FTI.hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00001057 Exceptions.size(), Exceptions.data());
Douglas Gregor965acbb2009-02-18 07:07:28 +00001058 } else if (FTI.isVariadic) {
1059 // We allow a zero-parameter variadic function in C if the
1060 // function is marked with the "overloadable"
1061 // attribute. Scan for this attribute now.
1062 bool Overloadable = false;
1063 for (const AttributeList *Attrs = D.getAttributes();
1064 Attrs; Attrs = Attrs->getNext()) {
1065 if (Attrs->getKind() == AttributeList::AT_overloadable) {
1066 Overloadable = true;
1067 break;
1068 }
1069 }
1070
1071 if (!Overloadable)
1072 Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
1073 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, 0);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001074 } else {
1075 // Simple void foo(), where the incoming T is the result type.
Douglas Gregor72564e72009-02-26 23:50:07 +00001076 T = Context.getFunctionNoProtoType(T);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001077 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001078 } else if (FTI.ArgInfo[0].Param == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001079 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
Mike Stump1eb44332009-09-09 15:08:12 +00001080 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
John McCall54e14c42009-10-22 22:37:11 +00001081 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001082 } else {
1083 // Otherwise, we have a function with an argument list that is
1084 // potentially variadic.
1085 llvm::SmallVector<QualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +00001086
Reid Spencer5f016e22007-07-11 17:01:13 +00001087 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001088 ParmVarDecl *Param =
1089 cast<ParmVarDecl>(FTI.ArgInfo[i].Param.getAs<Decl>());
Chris Lattner8123a952008-04-10 02:22:51 +00001090 QualType ArgTy = Param->getType();
Chris Lattner78c75fb2007-07-21 05:30:18 +00001091 assert(!ArgTy.isNull() && "Couldn't parse type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001092
1093 // Adjust the parameter type.
Douglas Gregorbeb58cb2009-03-23 23:17:00 +00001094 assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001095
Reid Spencer5f016e22007-07-11 17:01:13 +00001096 // Look for 'void'. void is allowed only as a single argument to a
1097 // function with no other parameters (C99 6.7.5.3p10). We record
Douglas Gregor72564e72009-02-26 23:50:07 +00001098 // int(void) as a FunctionProtoType with an empty argument list.
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001099 if (ArgTy->isVoidType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001100 // If this is something like 'float(int, void)', reject it. 'void'
1101 // is an incomplete type (C99 6.2.5p19) and function decls cannot
1102 // have arguments of incomplete type.
1103 if (FTI.NumArgs != 1 || FTI.isVariadic) {
1104 Diag(DeclType.Loc, diag::err_void_only_param);
Chris Lattner2ff54262007-07-21 05:18:12 +00001105 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00001106 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00001107 } else if (FTI.ArgInfo[i].Ident) {
1108 // Reject, but continue to parse 'int(void abc)'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001109 Diag(FTI.ArgInfo[i].IdentLoc,
Chris Lattner4565d4e2007-07-21 05:26:43 +00001110 diag::err_param_with_void_type);
Chris Lattner2ff54262007-07-21 05:18:12 +00001111 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00001112 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00001113 } else {
1114 // Reject, but continue to parse 'float(const void)'.
John McCall0953e762009-09-24 19:53:00 +00001115 if (ArgTy.hasQualifiers())
Chris Lattner2ff54262007-07-21 05:18:12 +00001116 Diag(DeclType.Loc, diag::err_void_param_qualified);
Mike Stump1eb44332009-09-09 15:08:12 +00001117
Chris Lattner2ff54262007-07-21 05:18:12 +00001118 // Do not add 'void' to the ArgTys list.
1119 break;
1120 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001121 } else if (!FTI.hasPrototype) {
1122 if (ArgTy->isPromotableIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00001123 ArgTy = Context.getPromotedIntegerType(ArgTy);
John McCall183700f2009-09-21 23:43:11 +00001124 } else if (const BuiltinType* BTy = ArgTy->getAs<BuiltinType>()) {
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001125 if (BTy->getKind() == BuiltinType::Float)
1126 ArgTy = Context.DoubleTy;
1127 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001128 }
Mike Stump1eb44332009-09-09 15:08:12 +00001129
John McCall54e14c42009-10-22 22:37:11 +00001130 ArgTys.push_back(ArgTy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001131 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001132
1133 llvm::SmallVector<QualType, 4> Exceptions;
1134 Exceptions.reserve(FTI.NumExceptions);
Mike Stump1eb44332009-09-09 15:08:12 +00001135 for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001136 // FIXME: Preserve type source info.
1137 QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty);
Sebastian Redlef65f062009-05-29 18:02:33 +00001138 // Check that the type is valid for an exception spec, and drop it if
1139 // not.
1140 if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1141 Exceptions.push_back(ET);
1142 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001143
Jay Foadbeaaccd2009-05-21 09:52:38 +00001144 T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(),
Sebastian Redl465226e2009-05-27 22:11:52 +00001145 FTI.isVariadic, FTI.TypeQuals,
1146 FTI.hasExceptionSpec,
1147 FTI.hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00001148 Exceptions.size(), Exceptions.data());
Reid Spencer5f016e22007-07-11 17:01:13 +00001149 }
1150 break;
1151 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001152 case DeclaratorChunk::MemberPointer:
Sebastian Redl4994d2d2009-07-04 11:39:00 +00001153 // Verify that we're not building a pointer to pointer to function with
1154 // exception specification.
1155 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1156 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1157 D.setInvalidType(true);
1158 // Build the type anyway.
1159 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001160 // The scope spec must refer to a class, or be dependent.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001161 QualType ClsType;
Douglas Gregor949bf692009-06-09 22:17:39 +00001162 if (isDependentScopeSpecifier(DeclType.Mem.Scope())) {
Mike Stump1eb44332009-09-09 15:08:12 +00001163 NestedNameSpecifier *NNS
Douglas Gregor949bf692009-06-09 22:17:39 +00001164 = (NestedNameSpecifier *)DeclType.Mem.Scope().getScopeRep();
1165 assert(NNS->getAsType() && "Nested-name-specifier must name a type");
1166 ClsType = QualType(NNS->getAsType(), 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001167 } else if (CXXRecordDecl *RD
Douglas Gregor949bf692009-06-09 22:17:39 +00001168 = dyn_cast_or_null<CXXRecordDecl>(
1169 computeDeclContext(DeclType.Mem.Scope()))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001170 ClsType = Context.getTagDeclType(RD);
1171 } else {
Douglas Gregor949bf692009-06-09 22:17:39 +00001172 Diag(DeclType.Mem.Scope().getBeginLoc(),
1173 diag::err_illegal_decl_mempointer_in_nonclass)
1174 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
1175 << DeclType.Mem.Scope().getRange();
Sebastian Redlf30208a2009-01-24 21:16:55 +00001176 D.setInvalidType(true);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001177 }
1178
Douglas Gregor949bf692009-06-09 22:17:39 +00001179 if (!ClsType.isNull())
1180 T = BuildMemberPointerType(T, ClsType, DeclType.Mem.TypeQuals,
1181 DeclType.Loc, D.getIdentifier());
1182 if (T.isNull()) {
1183 T = Context.IntTy;
Sebastian Redlf30208a2009-01-24 21:16:55 +00001184 D.setInvalidType(true);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001185 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001186 break;
1187 }
1188
Douglas Gregorcd281c32009-02-28 00:25:32 +00001189 if (T.isNull()) {
1190 D.setInvalidType(true);
1191 T = Context.IntTy;
1192 }
1193
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001194 // See if there are any attributes on this declarator chunk.
1195 if (const AttributeList *AL = DeclType.getAttrs())
1196 ProcessTypeAttributeList(T, AL);
Reid Spencer5f016e22007-07-11 17:01:13 +00001197 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001198
1199 if (getLangOptions().CPlusPlus && T->isFunctionType()) {
John McCall183700f2009-09-21 23:43:11 +00001200 const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>();
Chris Lattner778ed742009-10-25 17:36:50 +00001201 assert(FnTy && "Why oh why is there not a FunctionProtoType here?");
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001202
1203 // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type
1204 // for a nonstatic member function, the function type to which a pointer
1205 // to member refers, or the top-level function type of a function typedef
1206 // declaration.
1207 if (FnTy->getTypeQuals() != 0 &&
1208 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
Douglas Gregor584049d2008-12-15 23:53:10 +00001209 ((D.getContext() != Declarator::MemberContext &&
1210 (!D.getCXXScopeSpec().isSet() ||
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001211 !computeDeclContext(D.getCXXScopeSpec(), /*FIXME:*/true)
1212 ->isRecord())) ||
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001213 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001214 if (D.isFunctionDeclarator())
1215 Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
1216 else
1217 Diag(D.getIdentifierLoc(),
1218 diag::err_invalid_qualified_typedef_function_type_use);
1219
1220 // Strip the cv-quals from the type.
1221 T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001222 FnTy->getNumArgs(), FnTy->isVariadic(), 0);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001223 }
1224 }
Mike Stump1eb44332009-09-09 15:08:12 +00001225
Chris Lattner0bf29ad2008-06-29 00:19:33 +00001226 // If there were any type attributes applied to the decl itself (not the
1227 // type, apply the type attribute to the type!)
1228 if (const AttributeList *Attrs = D.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001229 ProcessTypeAttributeList(T, Attrs);
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001230
John McCall54e14c42009-10-22 22:37:11 +00001231 if (DInfo) {
1232 if (D.isInvalidType())
1233 *DInfo = 0;
1234 else
1235 *DInfo = GetDeclaratorInfoForDeclarator(D, T, Skip);
1236 }
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001237
Reid Spencer5f016e22007-07-11 17:01:13 +00001238 return T;
1239}
1240
John McCall51bd8032009-10-18 01:05:36 +00001241namespace {
1242 class TypeSpecLocFiller : public TypeLocVisitor<TypeSpecLocFiller> {
1243 const DeclSpec &DS;
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001244
John McCall51bd8032009-10-18 01:05:36 +00001245 public:
1246 TypeSpecLocFiller(const DeclSpec &DS) : DS(DS) {}
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001247
John McCall51bd8032009-10-18 01:05:36 +00001248 void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
1249 Visit(TL.getUnqualifiedLoc());
1250 }
1251 void VisitTypedefTypeLoc(TypedefTypeLoc TL) {
1252 TL.setNameLoc(DS.getTypeSpecTypeLoc());
1253 }
1254 void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
1255 TL.setNameLoc(DS.getTypeSpecTypeLoc());
Argyrios Kyrtzidiseb667592009-09-29 19:45:22 +00001256
John McCall54e14c42009-10-22 22:37:11 +00001257 if (DS.getProtocolQualifiers()) {
1258 assert(TL.getNumProtocols() > 0);
1259 assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
1260 TL.setLAngleLoc(DS.getProtocolLAngleLoc());
1261 TL.setRAngleLoc(DS.getSourceRange().getEnd());
1262 for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i)
1263 TL.setProtocolLoc(i, DS.getProtocolLocs()[i]);
1264 } else {
1265 assert(TL.getNumProtocols() == 0);
1266 TL.setLAngleLoc(SourceLocation());
1267 TL.setRAngleLoc(SourceLocation());
1268 }
1269 }
1270 void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
1271 assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
1272
1273 TL.setStarLoc(SourceLocation());
1274
1275 if (DS.getProtocolQualifiers()) {
1276 assert(TL.getNumProtocols() > 0);
1277 assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
1278 TL.setHasProtocolsAsWritten(true);
1279 TL.setLAngleLoc(DS.getProtocolLAngleLoc());
1280 TL.setRAngleLoc(DS.getSourceRange().getEnd());
1281 for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i)
1282 TL.setProtocolLoc(i, DS.getProtocolLocs()[i]);
1283
1284 } else {
1285 assert(TL.getNumProtocols() == 0);
1286 TL.setHasProtocolsAsWritten(false);
1287 TL.setLAngleLoc(SourceLocation());
1288 TL.setRAngleLoc(SourceLocation());
1289 }
1290
1291 // This might not have been written with an inner type.
1292 if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) {
1293 TL.setHasBaseTypeAsWritten(false);
1294 TL.getBaseTypeLoc().initialize(SourceLocation());
1295 } else {
1296 TL.setHasBaseTypeAsWritten(true);
John McCall51bd8032009-10-18 01:05:36 +00001297 Visit(TL.getBaseTypeLoc());
John McCall54e14c42009-10-22 22:37:11 +00001298 }
John McCall51bd8032009-10-18 01:05:36 +00001299 }
1300 void VisitTypeLoc(TypeLoc TL) {
1301 // FIXME: add other typespec types and change this to an assert.
1302 TL.initialize(DS.getTypeSpecTypeLoc());
1303 }
1304 };
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001305
John McCall51bd8032009-10-18 01:05:36 +00001306 class DeclaratorLocFiller : public TypeLocVisitor<DeclaratorLocFiller> {
1307 const DeclaratorChunk &Chunk;
1308
1309 public:
1310 DeclaratorLocFiller(const DeclaratorChunk &Chunk) : Chunk(Chunk) {}
1311
1312 void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
1313 llvm::llvm_unreachable("qualified type locs not expected here!");
1314 }
1315
1316 void VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
1317 assert(Chunk.Kind == DeclaratorChunk::BlockPointer);
1318 TL.setCaretLoc(Chunk.Loc);
1319 }
1320 void VisitPointerTypeLoc(PointerTypeLoc TL) {
1321 assert(Chunk.Kind == DeclaratorChunk::Pointer);
1322 TL.setStarLoc(Chunk.Loc);
1323 }
1324 void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
1325 assert(Chunk.Kind == DeclaratorChunk::Pointer);
1326 TL.setStarLoc(Chunk.Loc);
John McCall54e14c42009-10-22 22:37:11 +00001327 TL.setHasBaseTypeAsWritten(true);
1328 TL.setHasProtocolsAsWritten(false);
1329 TL.setLAngleLoc(SourceLocation());
1330 TL.setRAngleLoc(SourceLocation());
John McCall51bd8032009-10-18 01:05:36 +00001331 }
1332 void VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
1333 assert(Chunk.Kind == DeclaratorChunk::MemberPointer);
1334 TL.setStarLoc(Chunk.Loc);
1335 // FIXME: nested name specifier
1336 }
1337 void VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
1338 assert(Chunk.Kind == DeclaratorChunk::Reference);
John McCall54e14c42009-10-22 22:37:11 +00001339 // 'Amp' is misleading: this might have been originally
1340 /// spelled with AmpAmp.
John McCall51bd8032009-10-18 01:05:36 +00001341 TL.setAmpLoc(Chunk.Loc);
1342 }
1343 void VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
1344 assert(Chunk.Kind == DeclaratorChunk::Reference);
1345 assert(!Chunk.Ref.LValueRef);
1346 TL.setAmpAmpLoc(Chunk.Loc);
1347 }
1348 void VisitArrayTypeLoc(ArrayTypeLoc TL) {
1349 assert(Chunk.Kind == DeclaratorChunk::Array);
1350 TL.setLBracketLoc(Chunk.Loc);
1351 TL.setRBracketLoc(Chunk.EndLoc);
1352 TL.setSizeExpr(static_cast<Expr*>(Chunk.Arr.NumElts));
1353 }
1354 void VisitFunctionTypeLoc(FunctionTypeLoc TL) {
1355 assert(Chunk.Kind == DeclaratorChunk::Function);
1356 TL.setLParenLoc(Chunk.Loc);
1357 TL.setRParenLoc(Chunk.EndLoc);
1358
1359 const DeclaratorChunk::FunctionTypeInfo &FTI = Chunk.Fun;
John McCall54e14c42009-10-22 22:37:11 +00001360 for (unsigned i = 0, e = TL.getNumArgs(), tpi = 0; i != e; ++i) {
John McCall51bd8032009-10-18 01:05:36 +00001361 ParmVarDecl *Param = FTI.ArgInfo[i].Param.getAs<ParmVarDecl>();
John McCall54e14c42009-10-22 22:37:11 +00001362 TL.setArg(tpi++, Param);
John McCall51bd8032009-10-18 01:05:36 +00001363 }
1364 // FIXME: exception specs
1365 }
1366
1367 void VisitTypeLoc(TypeLoc TL) {
1368 llvm::llvm_unreachable("unsupported TypeLoc kind in declarator!");
1369 }
1370 };
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001371}
1372
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001373/// \brief Create and instantiate a DeclaratorInfo with type source information.
1374///
1375/// \param T QualType referring to the type as written in source code.
1376DeclaratorInfo *
1377Sema::GetDeclaratorInfoForDeclarator(Declarator &D, QualType T, unsigned Skip) {
1378 DeclaratorInfo *DInfo = Context.CreateDeclaratorInfo(T);
John McCall51bd8032009-10-18 01:05:36 +00001379 UnqualTypeLoc CurrTL = DInfo->getTypeLoc().getUnqualifiedLoc();
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001380
1381 for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) {
John McCall51bd8032009-10-18 01:05:36 +00001382 DeclaratorLocFiller(D.getTypeObject(i)).Visit(CurrTL);
1383 CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001384 }
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001385
John McCall51bd8032009-10-18 01:05:36 +00001386 TypeSpecLocFiller(D.getDeclSpec()).Visit(CurrTL);
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001387
1388 return DInfo;
1389}
1390
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001391/// \brief Create a LocInfoType to hold the given QualType and DeclaratorInfo.
1392QualType Sema::CreateLocInfoType(QualType T, DeclaratorInfo *DInfo) {
1393 // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser
1394 // and Sema during declaration parsing. Try deallocating/caching them when
1395 // it's appropriate, instead of allocating them and keeping them around.
1396 LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType), 8);
1397 new (LocT) LocInfoType(T, DInfo);
1398 assert(LocT->getTypeClass() != T->getTypeClass() &&
1399 "LocInfoType's TypeClass conflicts with an existing Type class");
1400 return QualType(LocT, 0);
1401}
1402
1403void LocInfoType::getAsStringInternal(std::string &Str,
1404 const PrintingPolicy &Policy) const {
Argyrios Kyrtzidis35d44e52009-08-19 01:46:06 +00001405 assert(false && "LocInfoType leaked into the type system; an opaque TypeTy*"
1406 " was used directly instead of getting the QualType through"
1407 " GetTypeFromParser");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001408}
1409
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001410/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
Fariborz Jahanian360300c2007-11-09 22:27:59 +00001411/// declarator
Chris Lattnerb28317a2009-03-28 19:18:32 +00001412QualType Sema::ObjCGetTypeForMethodDefinition(DeclPtrTy D) {
1413 ObjCMethodDecl *MDecl = cast<ObjCMethodDecl>(D.getAs<Decl>());
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001414 QualType T = MDecl->getResultType();
1415 llvm::SmallVector<QualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +00001416
Fariborz Jahanian35600022007-11-09 17:18:29 +00001417 // Add the first two invisible argument types for self and _cmd.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001418 if (MDecl->isInstanceMethod()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001419 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +00001420 selfTy = Context.getPointerType(selfTy);
1421 ArgTys.push_back(selfTy);
Chris Lattner89951a82009-02-20 18:43:26 +00001422 } else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001423 ArgTys.push_back(Context.getObjCIdType());
1424 ArgTys.push_back(Context.getObjCSelType());
Mike Stump1eb44332009-09-09 15:08:12 +00001425
Chris Lattner89951a82009-02-20 18:43:26 +00001426 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
1427 E = MDecl->param_end(); PI != E; ++PI) {
1428 QualType ArgTy = (*PI)->getType();
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001429 assert(!ArgTy.isNull() && "Couldn't parse type?");
Douglas Gregorbeb58cb2009-03-23 23:17:00 +00001430 ArgTy = adjustParameterType(ArgTy);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001431 ArgTys.push_back(ArgTy);
1432 }
1433 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001434 MDecl->isVariadic(), 0);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001435 return T;
1436}
1437
Sebastian Redl9e5e4aa2009-01-26 19:54:48 +00001438/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that
1439/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
1440/// they point to and return true. If T1 and T2 aren't pointer types
1441/// or pointer-to-member types, or if they are not similar at this
1442/// level, returns false and leaves T1 and T2 unchanged. Top-level
1443/// qualifiers on T1 and T2 are ignored. This function will typically
1444/// be called in a loop that successively "unwraps" pointer and
1445/// pointer-to-member types to compare them at each level.
Chris Lattnerecb81f22009-02-16 21:43:00 +00001446bool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001447 const PointerType *T1PtrType = T1->getAs<PointerType>(),
1448 *T2PtrType = T2->getAs<PointerType>();
Douglas Gregor57373262008-10-22 14:17:15 +00001449 if (T1PtrType && T2PtrType) {
1450 T1 = T1PtrType->getPointeeType();
1451 T2 = T2PtrType->getPointeeType();
1452 return true;
1453 }
1454
Ted Kremenek6217b802009-07-29 21:53:49 +00001455 const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(),
1456 *T2MPType = T2->getAs<MemberPointerType>();
Sebastian Redl21593ac2009-01-28 18:33:18 +00001457 if (T1MPType && T2MPType &&
1458 Context.getCanonicalType(T1MPType->getClass()) ==
1459 Context.getCanonicalType(T2MPType->getClass())) {
Sebastian Redl4433aaf2009-01-25 19:43:20 +00001460 T1 = T1MPType->getPointeeType();
1461 T2 = T2MPType->getPointeeType();
1462 return true;
1463 }
Douglas Gregor57373262008-10-22 14:17:15 +00001464 return false;
1465}
1466
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001467Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001468 // C99 6.7.6: Type names have no identifier. This is already validated by
1469 // the parser.
1470 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00001471
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001472 DeclaratorInfo *DInfo = 0;
Douglas Gregor402abb52009-05-28 23:31:59 +00001473 TagDecl *OwnedTag = 0;
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001474 QualType T = GetTypeForDeclarator(D, S, &DInfo, /*Skip=*/0, &OwnedTag);
Chris Lattner5153ee62009-04-25 08:47:54 +00001475 if (D.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +00001476 return true;
Steve Naroff5912a352007-08-28 20:14:24 +00001477
Douglas Gregor402abb52009-05-28 23:31:59 +00001478 if (getLangOptions().CPlusPlus) {
1479 // Check that there are no default arguments (C++ only).
Douglas Gregor6d6eb572008-05-07 04:49:29 +00001480 CheckExtraCXXDefaultArguments(D);
1481
Douglas Gregor402abb52009-05-28 23:31:59 +00001482 // C++0x [dcl.type]p3:
1483 // A type-specifier-seq shall not define a class or enumeration
1484 // unless it appears in the type-id of an alias-declaration
1485 // (7.1.3).
1486 if (OwnedTag && OwnedTag->isDefinition())
1487 Diag(OwnedTag->getLocation(), diag::err_type_defined_in_type_specifier)
1488 << Context.getTypeDeclType(OwnedTag);
1489 }
1490
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001491 if (DInfo)
1492 T = CreateLocInfoType(T, DInfo);
1493
Reid Spencer5f016e22007-07-11 17:01:13 +00001494 return T.getAsOpaquePtr();
1495}
1496
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001497
1498
1499//===----------------------------------------------------------------------===//
1500// Type Attribute Processing
1501//===----------------------------------------------------------------------===//
1502
1503/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
1504/// specified type. The attribute contains 1 argument, the id of the address
1505/// space for the type.
Mike Stump1eb44332009-09-09 15:08:12 +00001506static void HandleAddressSpaceTypeAttribute(QualType &Type,
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001507 const AttributeList &Attr, Sema &S){
John McCall0953e762009-09-24 19:53:00 +00001508
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001509 // If this type is already address space qualified, reject it.
1510 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
1511 // for two or more different address spaces."
1512 if (Type.getAddressSpace()) {
1513 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
1514 return;
1515 }
Mike Stump1eb44332009-09-09 15:08:12 +00001516
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001517 // Check the attribute arguments.
1518 if (Attr.getNumArgs() != 1) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +00001519 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001520 return;
1521 }
1522 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
1523 llvm::APSInt addrSpace(32);
1524 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001525 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
1526 << ASArgExpr->getSourceRange();
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001527 return;
1528 }
1529
John McCallefadb772009-07-28 06:52:18 +00001530 // Bounds checking.
1531 if (addrSpace.isSigned()) {
1532 if (addrSpace.isNegative()) {
1533 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative)
1534 << ASArgExpr->getSourceRange();
1535 return;
1536 }
1537 addrSpace.setIsSigned(false);
1538 }
1539 llvm::APSInt max(addrSpace.getBitWidth());
John McCall0953e762009-09-24 19:53:00 +00001540 max = Qualifiers::MaxAddressSpace;
John McCallefadb772009-07-28 06:52:18 +00001541 if (addrSpace > max) {
1542 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high)
John McCall0953e762009-09-24 19:53:00 +00001543 << Qualifiers::MaxAddressSpace << ASArgExpr->getSourceRange();
John McCallefadb772009-07-28 06:52:18 +00001544 return;
1545 }
1546
Mike Stump1eb44332009-09-09 15:08:12 +00001547 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001548 Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001549}
1550
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001551/// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the
1552/// specified type. The attribute contains 1 argument, weak or strong.
Mike Stump1eb44332009-09-09 15:08:12 +00001553static void HandleObjCGCTypeAttribute(QualType &Type,
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001554 const AttributeList &Attr, Sema &S) {
John McCall0953e762009-09-24 19:53:00 +00001555 if (Type.getObjCGCAttr() != Qualifiers::GCNone) {
Fariborz Jahanian5934e752009-02-18 18:52:41 +00001556 S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001557 return;
1558 }
Mike Stump1eb44332009-09-09 15:08:12 +00001559
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001560 // Check the attribute arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001561 if (!Attr.getParameterName()) {
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001562 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1563 << "objc_gc" << 1;
1564 return;
1565 }
John McCall0953e762009-09-24 19:53:00 +00001566 Qualifiers::GC GCAttr;
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001567 if (Attr.getNumArgs() != 0) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001568 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1569 return;
1570 }
Mike Stump1eb44332009-09-09 15:08:12 +00001571 if (Attr.getParameterName()->isStr("weak"))
John McCall0953e762009-09-24 19:53:00 +00001572 GCAttr = Qualifiers::Weak;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001573 else if (Attr.getParameterName()->isStr("strong"))
John McCall0953e762009-09-24 19:53:00 +00001574 GCAttr = Qualifiers::Strong;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001575 else {
1576 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1577 << "objc_gc" << Attr.getParameterName();
1578 return;
1579 }
Mike Stump1eb44332009-09-09 15:08:12 +00001580
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001581 Type = S.Context.getObjCGCQualType(Type, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001582}
1583
Mike Stump24556362009-07-25 21:26:53 +00001584/// HandleNoReturnTypeAttribute - Process the noreturn attribute on the
1585/// specified type. The attribute contains 0 arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001586static void HandleNoReturnTypeAttribute(QualType &Type,
Mike Stump24556362009-07-25 21:26:53 +00001587 const AttributeList &Attr, Sema &S) {
1588 if (Attr.getNumArgs() != 0)
1589 return;
1590
1591 // We only apply this to a pointer to function or a pointer to block.
1592 if (!Type->isFunctionPointerType()
1593 && !Type->isBlockPointerType()
1594 && !Type->isFunctionType())
1595 return;
1596
1597 Type = S.Context.getNoReturnType(Type);
1598}
1599
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001600void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) {
Chris Lattner232e8822008-02-21 01:08:11 +00001601 // Scan through and apply attributes to this type where it makes sense. Some
1602 // attributes (such as __address_space__, __vector_size__, etc) apply to the
1603 // type, but others can be present in the type specifiers even though they
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00001604 // apply to the decl. Here we apply type attributes and ignore the rest.
1605 for (; AL; AL = AL->getNext()) {
Chris Lattner232e8822008-02-21 01:08:11 +00001606 // If this is an attribute we can handle, do so now, otherwise, add it to
1607 // the LeftOverAttrs list for rechaining.
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00001608 switch (AL->getKind()) {
Chris Lattner232e8822008-02-21 01:08:11 +00001609 default: break;
1610 case AttributeList::AT_address_space:
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001611 HandleAddressSpaceTypeAttribute(Result, *AL, *this);
1612 break;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001613 case AttributeList::AT_objc_gc:
1614 HandleObjCGCTypeAttribute(Result, *AL, *this);
1615 break;
Mike Stump24556362009-07-25 21:26:53 +00001616 case AttributeList::AT_noreturn:
1617 HandleNoReturnTypeAttribute(Result, *AL, *this);
1618 break;
Chris Lattner232e8822008-02-21 01:08:11 +00001619 }
Chris Lattner232e8822008-02-21 01:08:11 +00001620 }
Chris Lattner232e8822008-02-21 01:08:11 +00001621}
1622
Mike Stump1eb44332009-09-09 15:08:12 +00001623/// @brief Ensure that the type T is a complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001624///
1625/// This routine checks whether the type @p T is complete in any
1626/// context where a complete type is required. If @p T is a complete
Douglas Gregor86447ec2009-03-09 16:13:40 +00001627/// type, returns false. If @p T is a class template specialization,
1628/// this routine then attempts to perform class template
1629/// instantiation. If instantiation fails, or if @p T is incomplete
1630/// and cannot be completed, issues the diagnostic @p diag (giving it
1631/// the type @p T) and returns true.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001632///
1633/// @param Loc The location in the source that the incomplete type
1634/// diagnostic should refer to.
1635///
1636/// @param T The type that this routine is examining for completeness.
1637///
Mike Stump1eb44332009-09-09 15:08:12 +00001638/// @param PD The partial diagnostic that will be printed out if T is not a
Anders Carlssonb7906612009-08-26 23:45:07 +00001639/// complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001640///
1641/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
1642/// @c false otherwise.
Anders Carlsson91a0cc92009-08-26 22:33:56 +00001643bool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
Anders Carlsson8c8d9192009-10-09 23:51:55 +00001644 const PartialDiagnostic &PD,
1645 std::pair<SourceLocation,
1646 PartialDiagnostic> Note) {
Anders Carlsson91a0cc92009-08-26 22:33:56 +00001647 unsigned diag = PD.getDiagID();
Mike Stump1eb44332009-09-09 15:08:12 +00001648
Douglas Gregor573d9c32009-10-21 23:19:44 +00001649 // FIXME: Add this assertion to make sure we always get instantiation points.
1650 // assert(!Loc.isInvalid() && "Invalid location in RequireCompleteType");
Douglas Gregor690dc7f2009-05-21 23:48:18 +00001651 // FIXME: Add this assertion to help us flush out problems with
1652 // checking for dependent types and type-dependent expressions.
1653 //
Mike Stump1eb44332009-09-09 15:08:12 +00001654 // assert(!T->isDependentType() &&
Douglas Gregor690dc7f2009-05-21 23:48:18 +00001655 // "Can't ask whether a dependent type is complete");
1656
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001657 // If we have a complete type, we're done.
1658 if (!T->isIncompleteType())
1659 return false;
Eli Friedman3c0eb162008-05-27 03:33:27 +00001660
Douglas Gregord475b8d2009-03-25 21:17:03 +00001661 // If we have a class template specialization or a class member of a
1662 // class template specialization, try to instantiate it.
Ted Kremenek6217b802009-07-29 21:53:49 +00001663 if (const RecordType *Record = T->getAs<RecordType>()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001664 if (ClassTemplateSpecializationDecl *ClassTemplateSpec
Douglas Gregord475b8d2009-03-25 21:17:03 +00001665 = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001666 if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001667 if (Loc.isValid())
John McCall9cc78072009-09-11 07:25:08 +00001668 ClassTemplateSpec->setPointOfInstantiation(Loc);
Douglas Gregor2943aed2009-03-03 04:44:36 +00001669 return InstantiateClassTemplateSpecialization(ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001670 TSK_ImplicitInstantiation,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001671 /*Complain=*/diag != 0);
Douglas Gregor2943aed2009-03-03 04:44:36 +00001672 }
Mike Stump1eb44332009-09-09 15:08:12 +00001673 } else if (CXXRecordDecl *Rec
Douglas Gregord475b8d2009-03-25 21:17:03 +00001674 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
1675 if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001676 MemberSpecializationInfo *MSInfo = Rec->getMemberSpecializationInfo();
1677 assert(MSInfo && "Missing member specialization information?");
Douglas Gregor357bbd02009-08-28 20:50:45 +00001678 // This record was instantiated from a class within a template.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001679 if (MSInfo->getTemplateSpecializationKind()
1680 != TSK_ExplicitSpecialization) {
1681 MSInfo->setPointOfInstantiation(Loc);
Douglas Gregorf6b11852009-10-08 15:14:33 +00001682 return InstantiateClass(Loc, Rec, Pattern,
1683 getTemplateInstantiationArgs(Rec),
1684 TSK_ImplicitInstantiation,
1685 /*Complain=*/diag != 0);
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001686 }
Douglas Gregord475b8d2009-03-25 21:17:03 +00001687 }
1688 }
1689 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00001690
Douglas Gregor5842ba92009-08-24 15:23:48 +00001691 if (diag == 0)
1692 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001693
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001694 // We have an incomplete type. Produce a diagnostic.
Anders Carlsson91a0cc92009-08-26 22:33:56 +00001695 Diag(Loc, PD) << T;
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001696
Anders Carlsson8c8d9192009-10-09 23:51:55 +00001697 // If we have a note, produce it.
1698 if (!Note.first.isInvalid())
1699 Diag(Note.first, Note.second);
1700
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001701 // If the type was a forward declaration of a class/struct/union
Mike Stump1eb44332009-09-09 15:08:12 +00001702 // type, produce
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001703 const TagType *Tag = 0;
Ted Kremenek6217b802009-07-29 21:53:49 +00001704 if (const RecordType *Record = T->getAs<RecordType>())
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001705 Tag = Record;
John McCall183700f2009-09-21 23:43:11 +00001706 else if (const EnumType *Enum = T->getAs<EnumType>())
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001707 Tag = Enum;
1708
1709 if (Tag && !Tag->getDecl()->isInvalidDecl())
Mike Stump1eb44332009-09-09 15:08:12 +00001710 Diag(Tag->getDecl()->getLocation(),
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001711 Tag->isBeingDefined() ? diag::note_type_being_defined
1712 : diag::note_forward_declaration)
1713 << QualType(Tag, 0);
1714
1715 return true;
1716}
Douglas Gregore6258932009-03-19 00:39:20 +00001717
1718/// \brief Retrieve a version of the type 'T' that is qualified by the
1719/// nested-name-specifier contained in SS.
1720QualType Sema::getQualifiedNameType(const CXXScopeSpec &SS, QualType T) {
1721 if (!SS.isSet() || SS.isInvalid() || T.isNull())
1722 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00001723
Douglas Gregorab452ba2009-03-26 23:50:42 +00001724 NestedNameSpecifier *NNS
Douglas Gregor35073692009-03-26 23:56:24 +00001725 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Douglas Gregorab452ba2009-03-26 23:50:42 +00001726 return Context.getQualifiedNameType(NNS, T);
Douglas Gregore6258932009-03-19 00:39:20 +00001727}
Anders Carlssonaf017e62009-06-29 22:58:55 +00001728
1729QualType Sema::BuildTypeofExprType(Expr *E) {
1730 return Context.getTypeOfExprType(E);
1731}
1732
1733QualType Sema::BuildDecltypeType(Expr *E) {
1734 if (E->getType() == Context.OverloadTy) {
Mike Stump1eb44332009-09-09 15:08:12 +00001735 Diag(E->getLocStart(),
Anders Carlssonaf017e62009-06-29 22:58:55 +00001736 diag::err_cannot_determine_declared_type_of_overloaded_function);
1737 return QualType();
1738 }
1739 return Context.getDecltypeType(E);
1740}