blob: 1a8111c55106ca75e4db4282facf82e13910fd6c [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"
Douglas Gregor87c12c42009-11-04 16:49:01 +000025#include "llvm/Support/ErrorHandling.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000026using namespace clang;
27
Douglas Gregor2dc0e642009-03-23 23:06:20 +000028/// \brief Perform adjustment on the parameter type of a function.
29///
30/// This routine adjusts the given parameter type @p T to the actual
Mike Stump1eb44332009-09-09 15:08:12 +000031/// parameter type used by semantic analysis (C99 6.7.5.3p[7,8],
32/// C++ [dcl.fct]p3). The adjusted parameter type is returned.
Douglas Gregor2dc0e642009-03-23 23:06:20 +000033QualType Sema::adjustParameterType(QualType T) {
34 // C99 6.7.5.3p7:
Chris Lattner778ed742009-10-25 17:36:50 +000035 // A declaration of a parameter as "array of type" shall be
36 // adjusted to "qualified pointer to type", where the type
37 // qualifiers (if any) are those specified within the [ and ] of
38 // the array type derivation.
39 if (T->isArrayType())
Douglas Gregor2dc0e642009-03-23 23:06:20 +000040 return Context.getArrayDecayedType(T);
Chris Lattner778ed742009-10-25 17:36:50 +000041
42 // C99 6.7.5.3p8:
43 // A declaration of a parameter as "function returning type"
44 // shall be adjusted to "pointer to function returning type", as
45 // in 6.3.2.1.
46 if (T->isFunctionType())
Douglas Gregor2dc0e642009-03-23 23:06:20 +000047 return Context.getPointerType(T);
48
49 return T;
50}
51
Chris Lattner5db2bb12009-10-25 18:21:37 +000052
53
54/// isOmittedBlockReturnType - Return true if this declarator is missing a
55/// return type because this is a omitted return type on a block literal.
Sebastian Redl8ce35b02009-10-25 21:45:37 +000056static bool isOmittedBlockReturnType(const Declarator &D) {
Chris Lattner5db2bb12009-10-25 18:21:37 +000057 if (D.getContext() != Declarator::BlockLiteralContext ||
Sebastian Redl8ce35b02009-10-25 21:45:37 +000058 D.getDeclSpec().hasTypeSpecifier())
Chris Lattner5db2bb12009-10-25 18:21:37 +000059 return false;
60
61 if (D.getNumTypeObjects() == 0)
Chris Lattnera64ef0a2009-10-25 22:09:09 +000062 return true; // ^{ ... }
Chris Lattner5db2bb12009-10-25 18:21:37 +000063
64 if (D.getNumTypeObjects() == 1 &&
65 D.getTypeObject(0).Kind == DeclaratorChunk::Function)
Chris Lattnera64ef0a2009-10-25 22:09:09 +000066 return true; // ^(int X, float Y) { ... }
Chris Lattner5db2bb12009-10-25 18:21:37 +000067
68 return false;
69}
70
Douglas Gregor930d8b52009-01-30 22:09:00 +000071/// \brief Convert the specified declspec to the appropriate type
72/// object.
Chris Lattner5db2bb12009-10-25 18:21:37 +000073/// \param D the declarator containing the declaration specifier.
Chris Lattner5153ee62009-04-25 08:47:54 +000074/// \returns The type described by the declaration specifiers. This function
75/// never returns null.
Sebastian Redl8ce35b02009-10-25 21:45:37 +000076static QualType ConvertDeclSpecToType(Declarator &TheDeclarator, Sema &TheSema){
Reid Spencer5f016e22007-07-11 17:01:13 +000077 // FIXME: Should move the logic from DeclSpec::Finish to here for validity
78 // checking.
Chris Lattner5db2bb12009-10-25 18:21:37 +000079 const DeclSpec &DS = TheDeclarator.getDeclSpec();
80 SourceLocation DeclLoc = TheDeclarator.getIdentifierLoc();
81 if (DeclLoc.isInvalid())
82 DeclLoc = DS.getSourceRange().getBegin();
Chris Lattner1564e392009-10-25 18:07:27 +000083
84 ASTContext &Context = TheSema.Context;
Mike Stump1eb44332009-09-09 15:08:12 +000085
Chris Lattner5db2bb12009-10-25 18:21:37 +000086 QualType Result;
Reid Spencer5f016e22007-07-11 17:01:13 +000087 switch (DS.getTypeSpecType()) {
Chris Lattner96b77fc2008-04-02 06:50:17 +000088 case DeclSpec::TST_void:
89 Result = Context.VoidTy;
90 break;
Reid Spencer5f016e22007-07-11 17:01:13 +000091 case DeclSpec::TST_char:
92 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
Chris Lattnerfab5b452008-02-20 23:53:49 +000093 Result = Context.CharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000094 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
Chris Lattnerfab5b452008-02-20 23:53:49 +000095 Result = Context.SignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000096 else {
97 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
98 "Unknown TSS value");
Chris Lattnerfab5b452008-02-20 23:53:49 +000099 Result = Context.UnsignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000100 }
Chris Lattner958858e2008-02-20 21:40:32 +0000101 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000102 case DeclSpec::TST_wchar:
103 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
104 Result = Context.WCharTy;
105 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
Chris Lattner1564e392009-10-25 18:07:27 +0000106 TheSema.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000107 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000108 Result = Context.getSignedWCharType();
109 } else {
110 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
111 "Unknown TSS value");
Chris Lattner1564e392009-10-25 18:07:27 +0000112 TheSema.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000113 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000114 Result = Context.getUnsignedWCharType();
115 }
116 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000117 case DeclSpec::TST_char16:
118 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
119 "Unknown TSS value");
120 Result = Context.Char16Ty;
121 break;
122 case DeclSpec::TST_char32:
123 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
124 "Unknown TSS value");
125 Result = Context.Char32Ty;
126 break;
Chris Lattnerd658b562008-04-05 06:32:51 +0000127 case DeclSpec::TST_unspecified:
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000128 // "<proto1,proto2>" is an objc qualified ID with a missing id.
Chris Lattner097e9162008-10-20 02:01:50 +0000129 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000130 Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy,
Steve Naroff14108da2009-07-10 23:34:53 +0000131 (ObjCProtocolDecl**)PQ,
Steve Naroff683087f2009-06-29 16:22:52 +0000132 DS.getNumProtocolQualifiers());
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000133 break;
134 }
Chris Lattner5db2bb12009-10-25 18:21:37 +0000135
136 // If this is a missing declspec in a block literal return context, then it
137 // is inferred from the return statements inside the block.
Sebastian Redl8ce35b02009-10-25 21:45:37 +0000138 if (isOmittedBlockReturnType(TheDeclarator)) {
Chris Lattner5db2bb12009-10-25 18:21:37 +0000139 Result = Context.DependentTy;
140 break;
141 }
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Chris Lattnerd658b562008-04-05 06:32:51 +0000143 // Unspecified typespec defaults to int in C90. However, the C90 grammar
144 // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
145 // type-qualifier, or storage-class-specifier. If not, emit an extwarn.
146 // Note that the one exception to this is function definitions, which are
147 // allowed to be completely missing a declspec. This is handled in the
148 // parser already though by it pretending to have seen an 'int' in this
149 // case.
Chris Lattner1564e392009-10-25 18:07:27 +0000150 if (TheSema.getLangOptions().ImplicitInt) {
Chris Lattner35d276f2009-02-27 18:53:28 +0000151 // In C89 mode, we only warn if there is a completely missing declspec
152 // when one is not allowed.
Chris Lattner3f84ad22009-04-22 05:27:59 +0000153 if (DS.isEmpty()) {
Chris Lattner1564e392009-10-25 18:07:27 +0000154 TheSema.Diag(DeclLoc, diag::ext_missing_declspec)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000155 << DS.getSourceRange()
Chris Lattner173144a2009-02-27 22:31:56 +0000156 << CodeModificationHint::CreateInsertion(DS.getSourceRange().getBegin(),
157 "int");
Chris Lattner3f84ad22009-04-22 05:27:59 +0000158 }
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000159 } else if (!DS.hasTypeSpecifier()) {
Chris Lattnerd658b562008-04-05 06:32:51 +0000160 // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says:
161 // "At least one type specifier shall be given in the declaration
162 // specifiers in each declaration, and in the specifier-qualifier list in
163 // each struct declaration and type name."
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000164 // FIXME: Does Microsoft really have the implicit int extension in C++?
Chris Lattner1564e392009-10-25 18:07:27 +0000165 if (TheSema.getLangOptions().CPlusPlus &&
166 !TheSema.getLangOptions().Microsoft) {
167 TheSema.Diag(DeclLoc, diag::err_missing_type_specifier)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000168 << DS.getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000169
Chris Lattnerb78d8332009-06-26 04:45:06 +0000170 // When this occurs in C++ code, often something is very broken with the
171 // value being declared, poison it as invalid so we don't get chains of
172 // errors.
Chris Lattner5db2bb12009-10-25 18:21:37 +0000173 TheDeclarator.setInvalidType(true);
Chris Lattnerb78d8332009-06-26 04:45:06 +0000174 } else {
Chris Lattner1564e392009-10-25 18:07:27 +0000175 TheSema.Diag(DeclLoc, diag::ext_missing_type_specifier)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000176 << DS.getSourceRange();
Chris Lattnerb78d8332009-06-26 04:45:06 +0000177 }
Chris Lattnerd658b562008-04-05 06:32:51 +0000178 }
Mike Stump1eb44332009-09-09 15:08:12 +0000179
180 // FALL THROUGH.
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000181 case DeclSpec::TST_int: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000182 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
183 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000184 case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
185 case DeclSpec::TSW_short: Result = Context.ShortTy; break;
186 case DeclSpec::TSW_long: Result = Context.LongTy; break;
Chris Lattner311157f2009-10-25 18:25:04 +0000187 case DeclSpec::TSW_longlong:
188 Result = Context.LongLongTy;
189
190 // long long is a C99 feature.
191 if (!TheSema.getLangOptions().C99 &&
192 !TheSema.getLangOptions().CPlusPlus0x)
193 TheSema.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong);
194 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000195 }
196 } else {
197 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000198 case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
199 case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break;
200 case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break;
Chris Lattner311157f2009-10-25 18:25:04 +0000201 case DeclSpec::TSW_longlong:
202 Result = Context.UnsignedLongLongTy;
203
204 // long long is a C99 feature.
205 if (!TheSema.getLangOptions().C99 &&
206 !TheSema.getLangOptions().CPlusPlus0x)
207 TheSema.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong);
208 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000209 }
210 }
Chris Lattner958858e2008-02-20 21:40:32 +0000211 break;
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000212 }
Chris Lattnerfab5b452008-02-20 23:53:49 +0000213 case DeclSpec::TST_float: Result = Context.FloatTy; break;
Chris Lattner958858e2008-02-20 21:40:32 +0000214 case DeclSpec::TST_double:
215 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000216 Result = Context.LongDoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000217 else
Chris Lattnerfab5b452008-02-20 23:53:49 +0000218 Result = Context.DoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000219 break;
Chris Lattnerfab5b452008-02-20 23:53:49 +0000220 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
Reid Spencer5f016e22007-07-11 17:01:13 +0000221 case DeclSpec::TST_decimal32: // _Decimal32
222 case DeclSpec::TST_decimal64: // _Decimal64
223 case DeclSpec::TST_decimal128: // _Decimal128
Chris Lattner1564e392009-10-25 18:07:27 +0000224 TheSema.Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported);
Chris Lattner8f12f652009-05-13 05:02:08 +0000225 Result = Context.IntTy;
Chris Lattner5db2bb12009-10-25 18:21:37 +0000226 TheDeclarator.setInvalidType(true);
Chris Lattner8f12f652009-05-13 05:02:08 +0000227 break;
Chris Lattner99dc9142008-04-13 18:59:07 +0000228 case DeclSpec::TST_class:
Reid Spencer5f016e22007-07-11 17:01:13 +0000229 case DeclSpec::TST_enum:
230 case DeclSpec::TST_union:
231 case DeclSpec::TST_struct: {
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000232 TypeDecl *D = cast_or_null<TypeDecl>(static_cast<Decl *>(DS.getTypeRep()));
John McCall6e247262009-10-10 05:48:19 +0000233 if (!D) {
234 // This can happen in C++ with ambiguous lookups.
235 Result = Context.IntTy;
Chris Lattner5db2bb12009-10-25 18:21:37 +0000236 TheDeclarator.setInvalidType(true);
John McCall6e247262009-10-10 05:48:19 +0000237 break;
238 }
239
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000240 // If the type is deprecated or unavailable, diagnose it.
John McCall54abf7d2009-11-04 02:18:39 +0000241 TheSema.DiagnoseUseOfDecl(D, DS.getTypeSpecTypeLoc());
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000242
Reid Spencer5f016e22007-07-11 17:01:13 +0000243 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000244 DS.getTypeSpecSign() == 0 && "No qualifiers on tag names!");
245
Reid Spencer5f016e22007-07-11 17:01:13 +0000246 // TypeQuals handled by caller.
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000247 Result = Context.getTypeDeclType(D);
John McCall2191b202009-09-05 06:31:47 +0000248
249 // In C++, make an ElaboratedType.
Chris Lattner1564e392009-10-25 18:07:27 +0000250 if (TheSema.getLangOptions().CPlusPlus) {
John McCall2191b202009-09-05 06:31:47 +0000251 TagDecl::TagKind Tag
252 = TagDecl::getTagKindForTypeSpec(DS.getTypeSpecType());
253 Result = Context.getElaboratedType(Result, Tag);
254 }
Mike Stump1eb44332009-09-09 15:08:12 +0000255
Chris Lattner5153ee62009-04-25 08:47:54 +0000256 if (D->isInvalidDecl())
Chris Lattner5db2bb12009-10-25 18:21:37 +0000257 TheDeclarator.setInvalidType(true);
Chris Lattner958858e2008-02-20 21:40:32 +0000258 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000259 }
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000260 case DeclSpec::TST_typename: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000261 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
262 DS.getTypeSpecSign() == 0 &&
263 "Can't handle qualifiers on typedef names yet!");
Chris Lattner1564e392009-10-25 18:07:27 +0000264 Result = TheSema.GetTypeFromParser(DS.getTypeRep());
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000265
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000266 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000267 if (const ObjCInterfaceType *
268 Interface = Result->getAs<ObjCInterfaceType>()) {
Steve Naroff67ef8ea2009-07-20 17:56:53 +0000269 // It would be nice if protocol qualifiers were only stored with the
270 // ObjCObjectPointerType. Unfortunately, this isn't possible due
271 // to the following typedef idiom (which is uncommon, but allowed):
Mike Stump1eb44332009-09-09 15:08:12 +0000272 //
Steve Naroff67ef8ea2009-07-20 17:56:53 +0000273 // typedef Foo<P> T;
274 // static void func() {
275 // Foo<P> *yy;
276 // T *zz;
277 // }
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000278 Result = Context.getObjCInterfaceType(Interface->getDecl(),
279 (ObjCProtocolDecl**)PQ,
280 DS.getNumProtocolQualifiers());
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000281 } else if (Result->isObjCIdType())
Chris Lattnerae4da612008-07-26 01:53:50 +0000282 // id<protocol-list>
Mike Stump1eb44332009-09-09 15:08:12 +0000283 Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy,
Steve Naroff14108da2009-07-10 23:34:53 +0000284 (ObjCProtocolDecl**)PQ, DS.getNumProtocolQualifiers());
285 else if (Result->isObjCClassType()) {
Steve Naroff4262a072009-02-23 18:53:24 +0000286 // Class<protocol-list>
Mike Stump1eb44332009-09-09 15:08:12 +0000287 Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinClassTy,
Steve Naroff470301b2009-07-22 16:07:01 +0000288 (ObjCProtocolDecl**)PQ, DS.getNumProtocolQualifiers());
Chris Lattner3f84ad22009-04-22 05:27:59 +0000289 } else {
Chris Lattner1564e392009-10-25 18:07:27 +0000290 TheSema.Diag(DeclLoc, diag::err_invalid_protocol_qualifiers)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000291 << DS.getSourceRange();
Chris Lattner5db2bb12009-10-25 18:21:37 +0000292 TheDeclarator.setInvalidType(true);
Chris Lattner3f84ad22009-04-22 05:27:59 +0000293 }
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000294 }
Mike Stump1eb44332009-09-09 15:08:12 +0000295
Reid Spencer5f016e22007-07-11 17:01:13 +0000296 // TypeQuals handled by caller.
Chris Lattner958858e2008-02-20 21:40:32 +0000297 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000298 }
Chris Lattner958858e2008-02-20 21:40:32 +0000299 case DeclSpec::TST_typeofType:
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000300 // FIXME: Preserve type source info.
Chris Lattner1564e392009-10-25 18:07:27 +0000301 Result = TheSema.GetTypeFromParser(DS.getTypeRep());
Chris Lattner958858e2008-02-20 21:40:32 +0000302 assert(!Result.isNull() && "Didn't get a type for typeof?");
Steve Naroffd1861fd2007-07-31 12:34:36 +0000303 // TypeQuals handled by caller.
Chris Lattnerfab5b452008-02-20 23:53:49 +0000304 Result = Context.getTypeOfType(Result);
Chris Lattner958858e2008-02-20 21:40:32 +0000305 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000306 case DeclSpec::TST_typeofExpr: {
307 Expr *E = static_cast<Expr *>(DS.getTypeRep());
308 assert(E && "Didn't get an expression for typeof?");
309 // TypeQuals handled by caller.
Douglas Gregor72564e72009-02-26 23:50:07 +0000310 Result = Context.getTypeOfExprType(E);
Chris Lattner958858e2008-02-20 21:40:32 +0000311 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000312 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000313 case DeclSpec::TST_decltype: {
314 Expr *E = static_cast<Expr *>(DS.getTypeRep());
315 assert(E && "Didn't get an expression for decltype?");
316 // TypeQuals handled by caller.
Chris Lattner1564e392009-10-25 18:07:27 +0000317 Result = TheSema.BuildDecltypeType(E);
Anders Carlssonaf017e62009-06-29 22:58:55 +0000318 if (Result.isNull()) {
319 Result = Context.IntTy;
Chris Lattner5db2bb12009-10-25 18:21:37 +0000320 TheDeclarator.setInvalidType(true);
Anders Carlssonaf017e62009-06-29 22:58:55 +0000321 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000322 break;
323 }
Anders Carlssone89d1592009-06-26 18:41:36 +0000324 case DeclSpec::TST_auto: {
325 // TypeQuals handled by caller.
326 Result = Context.UndeducedAutoTy;
327 break;
328 }
Mike Stump1eb44332009-09-09 15:08:12 +0000329
Douglas Gregor809070a2009-02-18 17:45:20 +0000330 case DeclSpec::TST_error:
Chris Lattner5153ee62009-04-25 08:47:54 +0000331 Result = Context.IntTy;
Chris Lattner5db2bb12009-10-25 18:21:37 +0000332 TheDeclarator.setInvalidType(true);
Chris Lattner5153ee62009-04-25 08:47:54 +0000333 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000334 }
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Chris Lattner958858e2008-02-20 21:40:32 +0000336 // Handle complex types.
Douglas Gregorf244cd72009-02-14 21:06:05 +0000337 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
Chris Lattner1564e392009-10-25 18:07:27 +0000338 if (TheSema.getLangOptions().Freestanding)
339 TheSema.Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
Chris Lattnerfab5b452008-02-20 23:53:49 +0000340 Result = Context.getComplexType(Result);
Douglas Gregorf244cd72009-02-14 21:06:05 +0000341 }
Mike Stump1eb44332009-09-09 15:08:12 +0000342
Chris Lattner958858e2008-02-20 21:40:32 +0000343 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
344 "FIXME: imaginary types not supported yet!");
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Chris Lattner38d8b982008-02-20 22:04:11 +0000346 // See if there are any attributes on the declspec that apply to the type (as
347 // opposed to the decl).
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000348 if (const AttributeList *AL = DS.getAttributes())
Chris Lattner1564e392009-10-25 18:07:27 +0000349 TheSema.ProcessTypeAttributeList(Result, AL);
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Chris Lattner96b77fc2008-04-02 06:50:17 +0000351 // Apply const/volatile/restrict qualifiers to T.
352 if (unsigned TypeQuals = DS.getTypeQualifiers()) {
353
354 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
355 // or incomplete types shall not be restrict-qualified." C++ also allows
356 // restrict-qualified references.
John McCall0953e762009-09-24 19:53:00 +0000357 if (TypeQuals & DeclSpec::TQ_restrict) {
Daniel Dunbarbb710012009-02-26 19:13:44 +0000358 if (Result->isPointerType() || Result->isReferenceType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000359 QualType EltTy = Result->isPointerType() ?
Ted Kremenek6217b802009-07-29 21:53:49 +0000360 Result->getAs<PointerType>()->getPointeeType() :
361 Result->getAs<ReferenceType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Douglas Gregorbad0e652009-03-24 20:32:41 +0000363 // If we have a pointer or reference, the pointee must have an object
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000364 // incomplete type.
365 if (!EltTy->isIncompleteOrObjectType()) {
Chris Lattner1564e392009-10-25 18:07:27 +0000366 TheSema.Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000367 diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattnerd1625842008-11-24 06:25:27 +0000368 << EltTy << DS.getSourceRange();
John McCall0953e762009-09-24 19:53:00 +0000369 TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000370 }
371 } else {
Chris Lattner1564e392009-10-25 18:07:27 +0000372 TheSema.Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000373 diag::err_typecheck_invalid_restrict_not_pointer)
Chris Lattnerd1625842008-11-24 06:25:27 +0000374 << Result << DS.getSourceRange();
John McCall0953e762009-09-24 19:53:00 +0000375 TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
Chris Lattner96b77fc2008-04-02 06:50:17 +0000376 }
Chris Lattner96b77fc2008-04-02 06:50:17 +0000377 }
Mike Stump1eb44332009-09-09 15:08:12 +0000378
Chris Lattner96b77fc2008-04-02 06:50:17 +0000379 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
380 // of a function type includes any type qualifiers, the behavior is
381 // undefined."
382 if (Result->isFunctionType() && TypeQuals) {
383 // Get some location to point at, either the C or V location.
384 SourceLocation Loc;
John McCall0953e762009-09-24 19:53:00 +0000385 if (TypeQuals & DeclSpec::TQ_const)
Chris Lattner96b77fc2008-04-02 06:50:17 +0000386 Loc = DS.getConstSpecLoc();
John McCall0953e762009-09-24 19:53:00 +0000387 else if (TypeQuals & DeclSpec::TQ_volatile)
Chris Lattner96b77fc2008-04-02 06:50:17 +0000388 Loc = DS.getVolatileSpecLoc();
John McCall0953e762009-09-24 19:53:00 +0000389 else {
390 assert((TypeQuals & DeclSpec::TQ_restrict) &&
391 "Has CVR quals but not C, V, or R?");
392 Loc = DS.getRestrictSpecLoc();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000393 }
Chris Lattner1564e392009-10-25 18:07:27 +0000394 TheSema.Diag(Loc, diag::warn_typecheck_function_qualifiers)
Chris Lattnerd1625842008-11-24 06:25:27 +0000395 << Result << DS.getSourceRange();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000396 }
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000398 // C++ [dcl.ref]p1:
399 // Cv-qualified references are ill-formed except when the
400 // cv-qualifiers are introduced through the use of a typedef
401 // (7.1.3) or of a template type argument (14.3), in which
402 // case the cv-qualifiers are ignored.
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000403 // FIXME: Shouldn't we be checking SCS_typedef here?
404 if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000405 TypeQuals && Result->isReferenceType()) {
John McCall0953e762009-09-24 19:53:00 +0000406 TypeQuals &= ~DeclSpec::TQ_const;
407 TypeQuals &= ~DeclSpec::TQ_volatile;
Mike Stump1eb44332009-09-09 15:08:12 +0000408 }
409
John McCall0953e762009-09-24 19:53:00 +0000410 Qualifiers Quals = Qualifiers::fromCVRMask(TypeQuals);
411 Result = Context.getQualifiedType(Result, Quals);
Chris Lattner96b77fc2008-04-02 06:50:17 +0000412 }
John McCall0953e762009-09-24 19:53:00 +0000413
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000414 return Result;
415}
416
Douglas Gregorcd281c32009-02-28 00:25:32 +0000417static std::string getPrintableNameForEntity(DeclarationName Entity) {
418 if (Entity)
419 return Entity.getAsString();
Mike Stump1eb44332009-09-09 15:08:12 +0000420
Douglas Gregorcd281c32009-02-28 00:25:32 +0000421 return "type name";
422}
423
424/// \brief Build a pointer type.
425///
426/// \param T The type to which we'll be building a pointer.
427///
428/// \param Quals The cvr-qualifiers to be applied to the pointer type.
429///
430/// \param Loc The location of the entity whose type involves this
431/// pointer type or, if there is no such entity, the location of the
432/// type that will have pointer type.
433///
434/// \param Entity The name of the entity that involves the pointer
435/// type, if known.
436///
437/// \returns A suitable pointer type, if there are no
438/// errors. Otherwise, returns a NULL type.
Mike Stump1eb44332009-09-09 15:08:12 +0000439QualType Sema::BuildPointerType(QualType T, unsigned Quals,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000440 SourceLocation Loc, DeclarationName Entity) {
441 if (T->isReferenceType()) {
442 // C++ 8.3.2p4: There shall be no ... pointers to references ...
443 Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
John McCallac406052009-10-30 00:37:20 +0000444 << getPrintableNameForEntity(Entity) << T;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000445 return QualType();
446 }
447
John McCall0953e762009-09-24 19:53:00 +0000448 Qualifiers Qs = Qualifiers::fromCVRMask(Quals);
449
Douglas Gregorcd281c32009-02-28 00:25:32 +0000450 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
451 // object or incomplete types shall not be restrict-qualified."
John McCall0953e762009-09-24 19:53:00 +0000452 if (Qs.hasRestrict() && !T->isIncompleteOrObjectType()) {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000453 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
454 << T;
John McCall0953e762009-09-24 19:53:00 +0000455 Qs.removeRestrict();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000456 }
457
458 // Build the pointer type.
John McCall0953e762009-09-24 19:53:00 +0000459 return Context.getQualifiedType(Context.getPointerType(T), Qs);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000460}
461
462/// \brief Build a reference type.
463///
464/// \param T The type to which we'll be building a reference.
465///
John McCall0953e762009-09-24 19:53:00 +0000466/// \param CVR The cvr-qualifiers to be applied to the reference type.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000467///
468/// \param Loc The location of the entity whose type involves this
469/// reference type or, if there is no such entity, the location of the
470/// type that will have reference type.
471///
472/// \param Entity The name of the entity that involves the reference
473/// type, if known.
474///
475/// \returns A suitable reference type, if there are no
476/// errors. Otherwise, returns a NULL type.
John McCall54e14c42009-10-22 22:37:11 +0000477QualType Sema::BuildReferenceType(QualType T, bool SpelledAsLValue,
478 unsigned CVR, SourceLocation Loc,
479 DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000480 Qualifiers Quals = Qualifiers::fromCVRMask(CVR);
John McCall54e14c42009-10-22 22:37:11 +0000481
482 bool LValueRef = SpelledAsLValue || T->getAs<LValueReferenceType>();
483
484 // C++0x [dcl.typedef]p9: If a typedef TD names a type that is a
485 // reference to a type T, and attempt to create the type "lvalue
486 // reference to cv TD" creates the type "lvalue reference to T".
487 // We use the qualifiers (restrict or none) of the original reference,
488 // not the new ones. This is consistent with GCC.
489
490 // C++ [dcl.ref]p4: There shall be no references to references.
491 //
492 // According to C++ DR 106, references to references are only
493 // diagnosed when they are written directly (e.g., "int & &"),
494 // but not when they happen via a typedef:
495 //
496 // typedef int& intref;
497 // typedef intref& intref2;
498 //
499 // Parser::ParseDeclaratorInternal diagnoses the case where
500 // references are written directly; here, we handle the
501 // collapsing of references-to-references as described in C++
502 // DR 106 and amended by C++ DR 540.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000503
504 // C++ [dcl.ref]p1:
Eli Friedman33a31382009-08-05 19:21:58 +0000505 // A declarator that specifies the type "reference to cv void"
Douglas Gregorcd281c32009-02-28 00:25:32 +0000506 // is ill-formed.
507 if (T->isVoidType()) {
508 Diag(Loc, diag::err_reference_to_void);
509 return QualType();
510 }
511
512 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
513 // object or incomplete types shall not be restrict-qualified."
John McCall0953e762009-09-24 19:53:00 +0000514 if (Quals.hasRestrict() && !T->isIncompleteOrObjectType()) {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000515 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
516 << T;
John McCall0953e762009-09-24 19:53:00 +0000517 Quals.removeRestrict();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000518 }
519
520 // C++ [dcl.ref]p1:
521 // [...] Cv-qualified references are ill-formed except when the
522 // cv-qualifiers are introduced through the use of a typedef
523 // (7.1.3) or of a template type argument (14.3), in which case
524 // the cv-qualifiers are ignored.
525 //
526 // We diagnose extraneous cv-qualifiers for the non-typedef,
527 // non-template type argument case within the parser. Here, we just
528 // ignore any extraneous cv-qualifiers.
John McCall0953e762009-09-24 19:53:00 +0000529 Quals.removeConst();
530 Quals.removeVolatile();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000531
532 // Handle restrict on references.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000533 if (LValueRef)
John McCall54e14c42009-10-22 22:37:11 +0000534 return Context.getQualifiedType(
535 Context.getLValueReferenceType(T, SpelledAsLValue), Quals);
John McCall0953e762009-09-24 19:53:00 +0000536 return Context.getQualifiedType(Context.getRValueReferenceType(T), Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000537}
538
539/// \brief Build an array type.
540///
541/// \param T The type of each element in the array.
542///
543/// \param ASM C99 array size modifier (e.g., '*', 'static').
Mike Stump1eb44332009-09-09 15:08:12 +0000544///
545/// \param ArraySize Expression describing the size of the array.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000546///
547/// \param Quals The cvr-qualifiers to be applied to the array's
548/// element type.
549///
550/// \param Loc The location of the entity whose type involves this
551/// array type or, if there is no such entity, the location of the
552/// type that will have array type.
553///
554/// \param Entity The name of the entity that involves the array
555/// type, if known.
556///
557/// \returns A suitable array type, if there are no errors. Otherwise,
558/// returns a NULL type.
559QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
560 Expr *ArraySize, unsigned Quals,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000561 SourceRange Brackets, DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000562
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000563 SourceLocation Loc = Brackets.getBegin();
Mike Stump1eb44332009-09-09 15:08:12 +0000564 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000565 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
Mike Stump1eb44332009-09-09 15:08:12 +0000566 if (RequireCompleteType(Loc, T,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000567 diag::err_illegal_decl_array_incomplete_type))
568 return QualType();
569
570 if (T->isFunctionType()) {
571 Diag(Loc, diag::err_illegal_decl_array_of_functions)
John McCallac406052009-10-30 00:37:20 +0000572 << getPrintableNameForEntity(Entity) << T;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000573 return QualType();
574 }
Mike Stump1eb44332009-09-09 15:08:12 +0000575
Douglas Gregorcd281c32009-02-28 00:25:32 +0000576 // C++ 8.3.2p4: There shall be no ... arrays of references ...
577 if (T->isReferenceType()) {
578 Diag(Loc, diag::err_illegal_decl_array_of_references)
John McCallac406052009-10-30 00:37:20 +0000579 << getPrintableNameForEntity(Entity) << T;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000580 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000581 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000582
Anders Carlssone7cf07d2009-06-26 19:33:28 +0000583 if (Context.getCanonicalType(T) == Context.UndeducedAutoTy) {
Mike Stump1eb44332009-09-09 15:08:12 +0000584 Diag(Loc, diag::err_illegal_decl_array_of_auto)
Anders Carlssone7cf07d2009-06-26 19:33:28 +0000585 << getPrintableNameForEntity(Entity);
586 return QualType();
587 }
Mike Stump1eb44332009-09-09 15:08:12 +0000588
Ted Kremenek6217b802009-07-29 21:53:49 +0000589 if (const RecordType *EltTy = T->getAs<RecordType>()) {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000590 // If the element type is a struct or union that contains a variadic
591 // array, accept it as a GNU extension: C99 6.7.2.1p2.
592 if (EltTy->getDecl()->hasFlexibleArrayMember())
593 Diag(Loc, diag::ext_flexible_array_in_array) << T;
594 } else if (T->isObjCInterfaceType()) {
Chris Lattnerc7c11b12009-04-27 01:55:56 +0000595 Diag(Loc, diag::err_objc_array_of_interfaces) << T;
596 return QualType();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000597 }
Mike Stump1eb44332009-09-09 15:08:12 +0000598
Douglas Gregorcd281c32009-02-28 00:25:32 +0000599 // C99 6.7.5.2p1: The size expression shall have integer type.
600 if (ArraySize && !ArraySize->isTypeDependent() &&
601 !ArraySize->getType()->isIntegerType()) {
602 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
603 << ArraySize->getType() << ArraySize->getSourceRange();
604 ArraySize->Destroy(Context);
605 return QualType();
606 }
607 llvm::APSInt ConstVal(32);
608 if (!ArraySize) {
Eli Friedmanf91f5c82009-04-26 21:57:51 +0000609 if (ASM == ArrayType::Star)
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000610 T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets);
Eli Friedmanf91f5c82009-04-26 21:57:51 +0000611 else
612 T = Context.getIncompleteArrayType(T, ASM, Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000613 } else if (ArraySize->isValueDependent()) {
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000614 T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000615 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
616 (!T->isDependentType() && !T->isConstantSizeType())) {
617 // Per C99, a variable array is an array with either a non-constant
618 // size or an element type that has a non-constant-size
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000619 T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000620 } else {
621 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
622 // have a value greater than zero.
623 if (ConstVal.isSigned()) {
624 if (ConstVal.isNegative()) {
625 Diag(ArraySize->getLocStart(),
626 diag::err_typecheck_negative_array_size)
627 << ArraySize->getSourceRange();
628 return QualType();
629 } else if (ConstVal == 0) {
630 // GCC accepts zero sized static arrays.
631 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size)
632 << ArraySize->getSourceRange();
633 }
Mike Stump1eb44332009-09-09 15:08:12 +0000634 }
John McCall46a617a2009-10-16 00:14:28 +0000635 T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000636 }
637 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
638 if (!getLangOptions().C99) {
Mike Stump1eb44332009-09-09 15:08:12 +0000639 if (ArraySize && !ArraySize->isTypeDependent() &&
640 !ArraySize->isValueDependent() &&
Douglas Gregorcd281c32009-02-28 00:25:32 +0000641 !ArraySize->isIntegerConstantExpr(Context))
Douglas Gregor043cad22009-09-11 00:18:58 +0000642 Diag(Loc, getLangOptions().CPlusPlus? diag::err_vla_cxx : diag::ext_vla);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000643 else if (ASM != ArrayType::Normal || Quals != 0)
Douglas Gregor043cad22009-09-11 00:18:58 +0000644 Diag(Loc,
645 getLangOptions().CPlusPlus? diag::err_c99_array_usage_cxx
646 : diag::ext_c99_array_usage);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000647 }
648
649 return T;
650}
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000651
652/// \brief Build an ext-vector type.
653///
654/// Run the required checks for the extended vector type.
Mike Stump1eb44332009-09-09 15:08:12 +0000655QualType Sema::BuildExtVectorType(QualType T, ExprArg ArraySize,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000656 SourceLocation AttrLoc) {
657
658 Expr *Arg = (Expr *)ArraySize.get();
659
660 // unlike gcc's vector_size attribute, we do not allow vectors to be defined
661 // in conjunction with complex types (pointers, arrays, functions, etc.).
Mike Stump1eb44332009-09-09 15:08:12 +0000662 if (!T->isDependentType() &&
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000663 !T->isIntegerType() && !T->isRealFloatingType()) {
664 Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T;
665 return QualType();
666 }
667
668 if (!Arg->isTypeDependent() && !Arg->isValueDependent()) {
669 llvm::APSInt vecSize(32);
670 if (!Arg->isIntegerConstantExpr(vecSize, Context)) {
671 Diag(AttrLoc, diag::err_attribute_argument_not_int)
672 << "ext_vector_type" << Arg->getSourceRange();
673 return QualType();
674 }
Mike Stump1eb44332009-09-09 15:08:12 +0000675
676 // unlike gcc's vector_size attribute, the size is specified as the
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000677 // number of elements, not the number of bytes.
Mike Stump1eb44332009-09-09 15:08:12 +0000678 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
679
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000680 if (vectorSize == 0) {
681 Diag(AttrLoc, diag::err_attribute_zero_size)
682 << Arg->getSourceRange();
683 return QualType();
684 }
Mike Stump1eb44332009-09-09 15:08:12 +0000685
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000686 if (!T->isDependentType())
687 return Context.getExtVectorType(T, vectorSize);
Mike Stump1eb44332009-09-09 15:08:12 +0000688 }
689
690 return Context.getDependentSizedExtVectorType(T, ArraySize.takeAs<Expr>(),
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000691 AttrLoc);
692}
Mike Stump1eb44332009-09-09 15:08:12 +0000693
Douglas Gregor724651c2009-02-28 01:04:19 +0000694/// \brief Build a function type.
695///
696/// This routine checks the function type according to C++ rules and
697/// under the assumption that the result type and parameter types have
698/// just been instantiated from a template. It therefore duplicates
Douglas Gregor2943aed2009-03-03 04:44:36 +0000699/// some of the behavior of GetTypeForDeclarator, but in a much
Douglas Gregor724651c2009-02-28 01:04:19 +0000700/// simpler form that is only suitable for this narrow use case.
701///
702/// \param T The return type of the function.
703///
704/// \param ParamTypes The parameter types of the function. This array
705/// will be modified to account for adjustments to the types of the
706/// function parameters.
707///
708/// \param NumParamTypes The number of parameter types in ParamTypes.
709///
710/// \param Variadic Whether this is a variadic function type.
711///
712/// \param Quals The cvr-qualifiers to be applied to the function type.
713///
714/// \param Loc The location of the entity whose type involves this
715/// function type or, if there is no such entity, the location of the
716/// type that will have function type.
717///
718/// \param Entity The name of the entity that involves the function
719/// type, if known.
720///
721/// \returns A suitable function type, if there are no
722/// errors. Otherwise, returns a NULL type.
723QualType Sema::BuildFunctionType(QualType T,
Mike Stump1eb44332009-09-09 15:08:12 +0000724 QualType *ParamTypes,
Douglas Gregor724651c2009-02-28 01:04:19 +0000725 unsigned NumParamTypes,
726 bool Variadic, unsigned Quals,
727 SourceLocation Loc, DeclarationName Entity) {
728 if (T->isArrayType() || T->isFunctionType()) {
729 Diag(Loc, diag::err_func_returning_array_function) << T;
730 return QualType();
731 }
Mike Stump1eb44332009-09-09 15:08:12 +0000732
Douglas Gregor724651c2009-02-28 01:04:19 +0000733 bool Invalid = false;
734 for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) {
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000735 QualType ParamType = adjustParameterType(ParamTypes[Idx]);
736 if (ParamType->isVoidType()) {
Douglas Gregor724651c2009-02-28 01:04:19 +0000737 Diag(Loc, diag::err_param_with_void_type);
738 Invalid = true;
739 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000740
John McCall54e14c42009-10-22 22:37:11 +0000741 ParamTypes[Idx] = ParamType;
Douglas Gregor724651c2009-02-28 01:04:19 +0000742 }
743
744 if (Invalid)
745 return QualType();
746
Mike Stump1eb44332009-09-09 15:08:12 +0000747 return Context.getFunctionType(T, ParamTypes, NumParamTypes, Variadic,
Douglas Gregor724651c2009-02-28 01:04:19 +0000748 Quals);
749}
Mike Stump1eb44332009-09-09 15:08:12 +0000750
Douglas Gregor949bf692009-06-09 22:17:39 +0000751/// \brief Build a member pointer type \c T Class::*.
752///
753/// \param T the type to which the member pointer refers.
754/// \param Class the class type into which the member pointer points.
John McCall0953e762009-09-24 19:53:00 +0000755/// \param CVR Qualifiers applied to the member pointer type
Douglas Gregor949bf692009-06-09 22:17:39 +0000756/// \param Loc the location where this type begins
757/// \param Entity the name of the entity that will have this member pointer type
758///
759/// \returns a member pointer type, if successful, or a NULL type if there was
760/// an error.
Mike Stump1eb44332009-09-09 15:08:12 +0000761QualType Sema::BuildMemberPointerType(QualType T, QualType Class,
John McCall0953e762009-09-24 19:53:00 +0000762 unsigned CVR, SourceLocation Loc,
Douglas Gregor949bf692009-06-09 22:17:39 +0000763 DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000764 Qualifiers Quals = Qualifiers::fromCVRMask(CVR);
765
Douglas Gregor949bf692009-06-09 22:17:39 +0000766 // Verify that we're not building a pointer to pointer to function with
767 // exception specification.
768 if (CheckDistantExceptionSpec(T)) {
769 Diag(Loc, diag::err_distant_exception_spec);
770
771 // FIXME: If we're doing this as part of template instantiation,
772 // we should return immediately.
773
774 // Build the type anyway, but use the canonical type so that the
775 // exception specifiers are stripped off.
776 T = Context.getCanonicalType(T);
777 }
778
779 // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member
780 // with reference type, or "cv void."
781 if (T->isReferenceType()) {
Anders Carlsson8d4655d2009-06-30 00:06:57 +0000782 Diag(Loc, diag::err_illegal_decl_mempointer_to_reference)
John McCallac406052009-10-30 00:37:20 +0000783 << (Entity? Entity.getAsString() : "type name") << T;
Douglas Gregor949bf692009-06-09 22:17:39 +0000784 return QualType();
785 }
786
787 if (T->isVoidType()) {
788 Diag(Loc, diag::err_illegal_decl_mempointer_to_void)
789 << (Entity? Entity.getAsString() : "type name");
790 return QualType();
791 }
792
793 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
794 // object or incomplete types shall not be restrict-qualified."
John McCall0953e762009-09-24 19:53:00 +0000795 if (Quals.hasRestrict() && !T->isIncompleteOrObjectType()) {
Douglas Gregor949bf692009-06-09 22:17:39 +0000796 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
797 << T;
798
799 // FIXME: If we're doing this as part of template instantiation,
800 // we should return immediately.
John McCall0953e762009-09-24 19:53:00 +0000801 Quals.removeRestrict();
Douglas Gregor949bf692009-06-09 22:17:39 +0000802 }
803
804 if (!Class->isDependentType() && !Class->isRecordType()) {
805 Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class;
806 return QualType();
807 }
808
John McCall0953e762009-09-24 19:53:00 +0000809 return Context.getQualifiedType(
810 Context.getMemberPointerType(T, Class.getTypePtr()), Quals);
Douglas Gregor949bf692009-06-09 22:17:39 +0000811}
Mike Stump1eb44332009-09-09 15:08:12 +0000812
Anders Carlsson9a917e42009-06-12 22:56:54 +0000813/// \brief Build a block pointer type.
814///
815/// \param T The type to which we'll be building a block pointer.
816///
John McCall0953e762009-09-24 19:53:00 +0000817/// \param CVR The cvr-qualifiers to be applied to the block pointer type.
Anders Carlsson9a917e42009-06-12 22:56:54 +0000818///
819/// \param Loc The location of the entity whose type involves this
820/// block pointer type or, if there is no such entity, the location of the
821/// type that will have block pointer type.
822///
823/// \param Entity The name of the entity that involves the block pointer
824/// type, if known.
825///
826/// \returns A suitable block pointer type, if there are no
827/// errors. Otherwise, returns a NULL type.
John McCall0953e762009-09-24 19:53:00 +0000828QualType Sema::BuildBlockPointerType(QualType T, unsigned CVR,
Mike Stump1eb44332009-09-09 15:08:12 +0000829 SourceLocation Loc,
Anders Carlsson9a917e42009-06-12 22:56:54 +0000830 DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000831 if (!T->isFunctionType()) {
Anders Carlsson9a917e42009-06-12 22:56:54 +0000832 Diag(Loc, diag::err_nonfunction_block_type);
833 return QualType();
834 }
Mike Stump1eb44332009-09-09 15:08:12 +0000835
John McCall0953e762009-09-24 19:53:00 +0000836 Qualifiers Quals = Qualifiers::fromCVRMask(CVR);
837 return Context.getQualifiedType(Context.getBlockPointerType(T), Quals);
Anders Carlsson9a917e42009-06-12 22:56:54 +0000838}
839
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000840QualType Sema::GetTypeFromParser(TypeTy *Ty, DeclaratorInfo **DInfo) {
841 QualType QT = QualType::getFromOpaquePtr(Ty);
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000842 if (QT.isNull()) {
843 if (DInfo) *DInfo = 0;
844 return QualType();
845 }
846
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000847 DeclaratorInfo *DI = 0;
848 if (LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) {
849 QT = LIT->getType();
850 DI = LIT->getDeclaratorInfo();
851 }
Mike Stump1eb44332009-09-09 15:08:12 +0000852
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000853 if (DInfo) *DInfo = DI;
854 return QT;
855}
856
Mike Stump98eb8a72009-02-04 22:31:32 +0000857/// GetTypeForDeclarator - Convert the type for the specified
Sebastian Redl8ce35b02009-10-25 21:45:37 +0000858/// declarator to Type instances.
Douglas Gregor402abb52009-05-28 23:31:59 +0000859///
860/// If OwnedDecl is non-NULL, and this declarator's decl-specifier-seq
861/// owns the declaration of a type (e.g., the definition of a struct
862/// type), then *OwnedDecl will receive the owned declaration.
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000863QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S,
Sebastian Redl8ce35b02009-10-25 21:45:37 +0000864 DeclaratorInfo **DInfo,
Douglas Gregor402abb52009-05-28 23:31:59 +0000865 TagDecl **OwnedDecl) {
Douglas Gregor930d8b52009-01-30 22:09:00 +0000866 // Determine the type of the declarator. Not all forms of declarator
867 // have a type.
868 QualType T;
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000869
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000870 switch (D.getName().getKind()) {
871 case UnqualifiedId::IK_Identifier:
872 case UnqualifiedId::IK_OperatorFunctionId:
873 case UnqualifiedId::IK_TemplateId:
Sebastian Redl8ce35b02009-10-25 21:45:37 +0000874 T = ConvertDeclSpecToType(D, *this);
Chris Lattner5db2bb12009-10-25 18:21:37 +0000875
876 if (!D.isInvalidType() && OwnedDecl && D.getDeclSpec().isTypeSpecOwned())
877 *OwnedDecl = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep());
Douglas Gregor930d8b52009-01-30 22:09:00 +0000878 break;
879
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000880 case UnqualifiedId::IK_ConstructorName:
881 case UnqualifiedId::IK_DestructorName:
882 case UnqualifiedId::IK_ConversionFunctionId:
Douglas Gregor930d8b52009-01-30 22:09:00 +0000883 // Constructors and destructors don't have return types. Use
884 // "void" instead. Conversion operators will check their return
885 // types separately.
886 T = Context.VoidTy;
887 break;
888 }
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000889
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000890 if (T == Context.UndeducedAutoTy) {
891 int Error = -1;
Mike Stump1eb44332009-09-09 15:08:12 +0000892
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000893 switch (D.getContext()) {
894 case Declarator::KNRTypeListContext:
895 assert(0 && "K&R type lists aren't allowed in C++");
896 break;
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000897 case Declarator::PrototypeContext:
898 Error = 0; // Function prototype
899 break;
900 case Declarator::MemberContext:
901 switch (cast<TagDecl>(CurContext)->getTagKind()) {
902 case TagDecl::TK_enum: assert(0 && "unhandled tag kind"); break;
903 case TagDecl::TK_struct: Error = 1; /* Struct member */ break;
904 case TagDecl::TK_union: Error = 2; /* Union member */ break;
905 case TagDecl::TK_class: Error = 3; /* Class member */ break;
Mike Stump1eb44332009-09-09 15:08:12 +0000906 }
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000907 break;
908 case Declarator::CXXCatchContext:
909 Error = 4; // Exception declaration
910 break;
911 case Declarator::TemplateParamContext:
912 Error = 5; // Template parameter
913 break;
914 case Declarator::BlockLiteralContext:
915 Error = 6; // Block literal
916 break;
917 case Declarator::FileContext:
918 case Declarator::BlockContext:
919 case Declarator::ForContext:
920 case Declarator::ConditionContext:
921 case Declarator::TypeNameContext:
922 break;
923 }
924
925 if (Error != -1) {
926 Diag(D.getDeclSpec().getTypeSpecTypeLoc(), diag::err_auto_not_allowed)
927 << Error;
928 T = Context.IntTy;
929 D.setInvalidType(true);
930 }
931 }
Mike Stump1eb44332009-09-09 15:08:12 +0000932
Douglas Gregorcd281c32009-02-28 00:25:32 +0000933 // The name we're declaring, if any.
934 DeclarationName Name;
935 if (D.getIdentifier())
936 Name = D.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +0000937
Mike Stump98eb8a72009-02-04 22:31:32 +0000938 // Walk the DeclTypeInfo, building the recursive type as we go.
939 // DeclTypeInfos are ordered from the identifier out, which is
940 // opposite of what we want :).
Sebastian Redl8ce35b02009-10-25 21:45:37 +0000941 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
942 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000943 switch (DeclType.Kind) {
944 default: assert(0 && "Unknown decltype!");
Steve Naroff5618bd42008-08-27 16:04:49 +0000945 case DeclaratorChunk::BlockPointer:
Chris Lattner9af55002009-03-27 04:18:06 +0000946 // If blocks are disabled, emit an error.
947 if (!LangOpts.Blocks)
948 Diag(DeclType.Loc, diag::err_blocks_disable);
Mike Stump1eb44332009-09-09 15:08:12 +0000949
950 T = BuildBlockPointerType(T, DeclType.Cls.TypeQuals, D.getIdentifierLoc(),
Anders Carlsson9a917e42009-06-12 22:56:54 +0000951 Name);
Steve Naroff5618bd42008-08-27 16:04:49 +0000952 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000953 case DeclaratorChunk::Pointer:
Sebastian Redl6a7330c2009-05-29 15:01:05 +0000954 // Verify that we're not building a pointer to pointer to function with
955 // exception specification.
956 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
957 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
958 D.setInvalidType(true);
959 // Build the type anyway.
960 }
Steve Naroff14108da2009-07-10 23:34:53 +0000961 if (getLangOptions().ObjC1 && T->isObjCInterfaceType()) {
John McCall183700f2009-09-21 23:43:11 +0000962 const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>();
Steve Naroff14108da2009-07-10 23:34:53 +0000963 T = Context.getObjCObjectPointerType(T,
Steve Naroff67ef8ea2009-07-20 17:56:53 +0000964 (ObjCProtocolDecl **)OIT->qual_begin(),
965 OIT->getNumProtocols());
Steve Naroff14108da2009-07-10 23:34:53 +0000966 break;
967 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000968 T = BuildPointerType(T, DeclType.Ptr.TypeQuals, DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000969 break;
John McCall0953e762009-09-24 19:53:00 +0000970 case DeclaratorChunk::Reference: {
971 Qualifiers Quals;
972 if (DeclType.Ref.HasRestrict) Quals.addRestrict();
973
Sebastian Redl6a7330c2009-05-29 15:01:05 +0000974 // Verify that we're not building a reference to pointer to function with
975 // exception specification.
976 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
977 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
978 D.setInvalidType(true);
979 // Build the type anyway.
980 }
John McCall0953e762009-09-24 19:53:00 +0000981 T = BuildReferenceType(T, DeclType.Ref.LValueRef, Quals,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000982 DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000983 break;
John McCall0953e762009-09-24 19:53:00 +0000984 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000985 case DeclaratorChunk::Array: {
Sebastian Redl6a7330c2009-05-29 15:01:05 +0000986 // Verify that we're not building an array of pointers to function with
987 // exception specification.
988 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
989 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
990 D.setInvalidType(true);
991 // Build the type anyway.
992 }
Chris Lattnerfd89bc82008-04-02 01:05:10 +0000993 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner94f81fd2007-08-28 16:54:00 +0000994 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000995 ArrayType::ArraySizeModifier ASM;
996 if (ATI.isStar)
997 ASM = ArrayType::Star;
998 else if (ATI.hasStatic)
999 ASM = ArrayType::Static;
1000 else
1001 ASM = ArrayType::Normal;
Eli Friedmanf91f5c82009-04-26 21:57:51 +00001002 if (ASM == ArrayType::Star &&
1003 D.getContext() != Declarator::PrototypeContext) {
1004 // FIXME: This check isn't quite right: it allows star in prototypes
1005 // for function definitions, and disallows some edge cases detailed
1006 // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html
1007 Diag(DeclType.Loc, diag::err_array_star_outside_prototype);
1008 ASM = ArrayType::Normal;
1009 D.setInvalidType(true);
1010 }
John McCall0953e762009-09-24 19:53:00 +00001011 T = BuildArrayType(T, ASM, ArraySize,
1012 Qualifiers::fromCVRMask(ATI.TypeQuals),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001013 SourceRange(DeclType.Loc, DeclType.EndLoc), Name);
Reid Spencer5f016e22007-07-11 17:01:13 +00001014 break;
1015 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001016 case DeclaratorChunk::Function: {
Reid Spencer5f016e22007-07-11 17:01:13 +00001017 // If the function declarator has a prototype (i.e. it is not () and
1018 // does not have a K&R-style identifier list), then the arguments are part
1019 // of the type, otherwise the argument list is ().
1020 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Sebastian Redl3cc97262009-05-31 11:47:27 +00001021
Chris Lattnercd881292007-12-19 05:31:29 +00001022 // C99 6.7.5.3p1: The return type may not be a function or array type.
Chris Lattner68cfd492007-12-19 18:01:43 +00001023 if (T->isArrayType() || T->isFunctionType()) {
Chris Lattnerd1625842008-11-24 06:25:27 +00001024 Diag(DeclType.Loc, diag::err_func_returning_array_function) << T;
Chris Lattnercd881292007-12-19 05:31:29 +00001025 T = Context.IntTy;
1026 D.setInvalidType(true);
1027 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001028
Douglas Gregor402abb52009-05-28 23:31:59 +00001029 if (getLangOptions().CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) {
1030 // C++ [dcl.fct]p6:
1031 // Types shall not be defined in return or parameter types.
1032 TagDecl *Tag = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep());
1033 if (Tag->isDefinition())
1034 Diag(Tag->getLocation(), diag::err_type_defined_in_result_type)
1035 << Context.getTypeDeclType(Tag);
1036 }
1037
Sebastian Redl3cc97262009-05-31 11:47:27 +00001038 // Exception specs are not allowed in typedefs. Complain, but add it
1039 // anyway.
1040 if (FTI.hasExceptionSpec &&
1041 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
1042 Diag(FTI.getThrowLoc(), diag::err_exception_spec_in_typedef);
1043
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001044 if (FTI.NumArgs == 0) {
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001045 if (getLangOptions().CPlusPlus) {
1046 // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the
1047 // function takes no arguments.
Sebastian Redl465226e2009-05-27 22:11:52 +00001048 llvm::SmallVector<QualType, 4> Exceptions;
1049 Exceptions.reserve(FTI.NumExceptions);
Mike Stump1eb44332009-09-09 15:08:12 +00001050 for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001051 // FIXME: Preserve type source info.
1052 QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty);
Sebastian Redlef65f062009-05-29 18:02:33 +00001053 // Check that the type is valid for an exception spec, and drop it
1054 // if not.
1055 if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1056 Exceptions.push_back(ET);
1057 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001058 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, FTI.TypeQuals,
1059 FTI.hasExceptionSpec,
1060 FTI.hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00001061 Exceptions.size(), Exceptions.data());
Douglas Gregor965acbb2009-02-18 07:07:28 +00001062 } else if (FTI.isVariadic) {
1063 // We allow a zero-parameter variadic function in C if the
1064 // function is marked with the "overloadable"
1065 // attribute. Scan for this attribute now.
1066 bool Overloadable = false;
1067 for (const AttributeList *Attrs = D.getAttributes();
1068 Attrs; Attrs = Attrs->getNext()) {
1069 if (Attrs->getKind() == AttributeList::AT_overloadable) {
1070 Overloadable = true;
1071 break;
1072 }
1073 }
1074
1075 if (!Overloadable)
1076 Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
1077 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, 0);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001078 } else {
1079 // Simple void foo(), where the incoming T is the result type.
Douglas Gregor72564e72009-02-26 23:50:07 +00001080 T = Context.getFunctionNoProtoType(T);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001081 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001082 } else if (FTI.ArgInfo[0].Param == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001083 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
Mike Stump1eb44332009-09-09 15:08:12 +00001084 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
John McCall54e14c42009-10-22 22:37:11 +00001085 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001086 } else {
1087 // Otherwise, we have a function with an argument list that is
1088 // potentially variadic.
1089 llvm::SmallVector<QualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +00001090
Reid Spencer5f016e22007-07-11 17:01:13 +00001091 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001092 ParmVarDecl *Param =
1093 cast<ParmVarDecl>(FTI.ArgInfo[i].Param.getAs<Decl>());
Chris Lattner8123a952008-04-10 02:22:51 +00001094 QualType ArgTy = Param->getType();
Chris Lattner78c75fb2007-07-21 05:30:18 +00001095 assert(!ArgTy.isNull() && "Couldn't parse type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001096
1097 // Adjust the parameter type.
Douglas Gregorbeb58cb2009-03-23 23:17:00 +00001098 assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001099
Reid Spencer5f016e22007-07-11 17:01:13 +00001100 // Look for 'void'. void is allowed only as a single argument to a
1101 // function with no other parameters (C99 6.7.5.3p10). We record
Douglas Gregor72564e72009-02-26 23:50:07 +00001102 // int(void) as a FunctionProtoType with an empty argument list.
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001103 if (ArgTy->isVoidType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001104 // If this is something like 'float(int, void)', reject it. 'void'
1105 // is an incomplete type (C99 6.2.5p19) and function decls cannot
1106 // have arguments of incomplete type.
1107 if (FTI.NumArgs != 1 || FTI.isVariadic) {
1108 Diag(DeclType.Loc, diag::err_void_only_param);
Chris Lattner2ff54262007-07-21 05:18:12 +00001109 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00001110 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00001111 } else if (FTI.ArgInfo[i].Ident) {
1112 // Reject, but continue to parse 'int(void abc)'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001113 Diag(FTI.ArgInfo[i].IdentLoc,
Chris Lattner4565d4e2007-07-21 05:26:43 +00001114 diag::err_param_with_void_type);
Chris Lattner2ff54262007-07-21 05:18:12 +00001115 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00001116 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00001117 } else {
1118 // Reject, but continue to parse 'float(const void)'.
John McCall0953e762009-09-24 19:53:00 +00001119 if (ArgTy.hasQualifiers())
Chris Lattner2ff54262007-07-21 05:18:12 +00001120 Diag(DeclType.Loc, diag::err_void_param_qualified);
Mike Stump1eb44332009-09-09 15:08:12 +00001121
Chris Lattner2ff54262007-07-21 05:18:12 +00001122 // Do not add 'void' to the ArgTys list.
1123 break;
1124 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001125 } else if (!FTI.hasPrototype) {
1126 if (ArgTy->isPromotableIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00001127 ArgTy = Context.getPromotedIntegerType(ArgTy);
John McCall183700f2009-09-21 23:43:11 +00001128 } else if (const BuiltinType* BTy = ArgTy->getAs<BuiltinType>()) {
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001129 if (BTy->getKind() == BuiltinType::Float)
1130 ArgTy = Context.DoubleTy;
1131 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001132 }
Mike Stump1eb44332009-09-09 15:08:12 +00001133
John McCall54e14c42009-10-22 22:37:11 +00001134 ArgTys.push_back(ArgTy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001135 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001136
1137 llvm::SmallVector<QualType, 4> Exceptions;
1138 Exceptions.reserve(FTI.NumExceptions);
Mike Stump1eb44332009-09-09 15:08:12 +00001139 for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001140 // FIXME: Preserve type source info.
1141 QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty);
Sebastian Redlef65f062009-05-29 18:02:33 +00001142 // Check that the type is valid for an exception spec, and drop it if
1143 // not.
1144 if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1145 Exceptions.push_back(ET);
1146 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001147
Jay Foadbeaaccd2009-05-21 09:52:38 +00001148 T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(),
Sebastian Redl465226e2009-05-27 22:11:52 +00001149 FTI.isVariadic, FTI.TypeQuals,
1150 FTI.hasExceptionSpec,
1151 FTI.hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00001152 Exceptions.size(), Exceptions.data());
Reid Spencer5f016e22007-07-11 17:01:13 +00001153 }
1154 break;
1155 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001156 case DeclaratorChunk::MemberPointer:
Sebastian Redl4994d2d2009-07-04 11:39:00 +00001157 // Verify that we're not building a pointer to pointer to function with
1158 // exception specification.
1159 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1160 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1161 D.setInvalidType(true);
1162 // Build the type anyway.
1163 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001164 // The scope spec must refer to a class, or be dependent.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001165 QualType ClsType;
Douglas Gregor87c12c42009-11-04 16:49:01 +00001166 if (isDependentScopeSpecifier(DeclType.Mem.Scope())
1167 || dyn_cast_or_null<CXXRecordDecl>(
1168 computeDeclContext(DeclType.Mem.Scope()))) {
Mike Stump1eb44332009-09-09 15:08:12 +00001169 NestedNameSpecifier *NNS
Douglas Gregor949bf692009-06-09 22:17:39 +00001170 = (NestedNameSpecifier *)DeclType.Mem.Scope().getScopeRep();
Douglas Gregor87c12c42009-11-04 16:49:01 +00001171 NestedNameSpecifier *NNSPrefix = NNS->getPrefix();
1172 switch (NNS->getKind()) {
1173 case NestedNameSpecifier::Identifier:
1174 ClsType = Context.getTypenameType(NNSPrefix, NNS->getAsIdentifier());
1175 break;
1176
1177 case NestedNameSpecifier::Namespace:
1178 case NestedNameSpecifier::Global:
1179 llvm::llvm_unreachable("Nested-name-specifier must name a type");
1180 break;
1181
1182 case NestedNameSpecifier::TypeSpec:
1183 case NestedNameSpecifier::TypeSpecWithTemplate:
1184 ClsType = QualType(NNS->getAsType(), 0);
1185 if (NNSPrefix)
1186 ClsType = Context.getQualifiedNameType(NNSPrefix, ClsType);
1187 break;
1188 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001189 } else {
Douglas Gregor949bf692009-06-09 22:17:39 +00001190 Diag(DeclType.Mem.Scope().getBeginLoc(),
1191 diag::err_illegal_decl_mempointer_in_nonclass)
1192 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
1193 << DeclType.Mem.Scope().getRange();
Sebastian Redlf30208a2009-01-24 21:16:55 +00001194 D.setInvalidType(true);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001195 }
1196
Douglas Gregor949bf692009-06-09 22:17:39 +00001197 if (!ClsType.isNull())
1198 T = BuildMemberPointerType(T, ClsType, DeclType.Mem.TypeQuals,
1199 DeclType.Loc, D.getIdentifier());
1200 if (T.isNull()) {
1201 T = Context.IntTy;
Sebastian Redlf30208a2009-01-24 21:16:55 +00001202 D.setInvalidType(true);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001203 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001204 break;
1205 }
1206
Douglas Gregorcd281c32009-02-28 00:25:32 +00001207 if (T.isNull()) {
1208 D.setInvalidType(true);
1209 T = Context.IntTy;
1210 }
1211
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001212 // See if there are any attributes on this declarator chunk.
1213 if (const AttributeList *AL = DeclType.getAttrs())
1214 ProcessTypeAttributeList(T, AL);
Reid Spencer5f016e22007-07-11 17:01:13 +00001215 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001216
1217 if (getLangOptions().CPlusPlus && T->isFunctionType()) {
John McCall183700f2009-09-21 23:43:11 +00001218 const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>();
Chris Lattner778ed742009-10-25 17:36:50 +00001219 assert(FnTy && "Why oh why is there not a FunctionProtoType here?");
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001220
1221 // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type
1222 // for a nonstatic member function, the function type to which a pointer
1223 // to member refers, or the top-level function type of a function typedef
1224 // declaration.
1225 if (FnTy->getTypeQuals() != 0 &&
1226 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
Douglas Gregor584049d2008-12-15 23:53:10 +00001227 ((D.getContext() != Declarator::MemberContext &&
1228 (!D.getCXXScopeSpec().isSet() ||
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001229 !computeDeclContext(D.getCXXScopeSpec(), /*FIXME:*/true)
1230 ->isRecord())) ||
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001231 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001232 if (D.isFunctionDeclarator())
1233 Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
1234 else
1235 Diag(D.getIdentifierLoc(),
1236 diag::err_invalid_qualified_typedef_function_type_use);
1237
1238 // Strip the cv-quals from the type.
1239 T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001240 FnTy->getNumArgs(), FnTy->isVariadic(), 0);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001241 }
1242 }
Mike Stump1eb44332009-09-09 15:08:12 +00001243
Chris Lattner0bf29ad2008-06-29 00:19:33 +00001244 // If there were any type attributes applied to the decl itself (not the
1245 // type, apply the type attribute to the type!)
1246 if (const AttributeList *Attrs = D.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001247 ProcessTypeAttributeList(T, Attrs);
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001248
John McCall54e14c42009-10-22 22:37:11 +00001249 if (DInfo) {
1250 if (D.isInvalidType())
1251 *DInfo = 0;
1252 else
Sebastian Redl8ce35b02009-10-25 21:45:37 +00001253 *DInfo = GetDeclaratorInfoForDeclarator(D, T);
John McCall54e14c42009-10-22 22:37:11 +00001254 }
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001255
Reid Spencer5f016e22007-07-11 17:01:13 +00001256 return T;
1257}
1258
John McCall51bd8032009-10-18 01:05:36 +00001259namespace {
1260 class TypeSpecLocFiller : public TypeLocVisitor<TypeSpecLocFiller> {
1261 const DeclSpec &DS;
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001262
John McCall51bd8032009-10-18 01:05:36 +00001263 public:
1264 TypeSpecLocFiller(const DeclSpec &DS) : DS(DS) {}
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001265
John McCall51bd8032009-10-18 01:05:36 +00001266 void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
1267 Visit(TL.getUnqualifiedLoc());
1268 }
1269 void VisitTypedefTypeLoc(TypedefTypeLoc TL) {
1270 TL.setNameLoc(DS.getTypeSpecTypeLoc());
1271 }
1272 void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
1273 TL.setNameLoc(DS.getTypeSpecTypeLoc());
Argyrios Kyrtzidiseb667592009-09-29 19:45:22 +00001274
John McCall54e14c42009-10-22 22:37:11 +00001275 if (DS.getProtocolQualifiers()) {
1276 assert(TL.getNumProtocols() > 0);
1277 assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
1278 TL.setLAngleLoc(DS.getProtocolLAngleLoc());
1279 TL.setRAngleLoc(DS.getSourceRange().getEnd());
1280 for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i)
1281 TL.setProtocolLoc(i, DS.getProtocolLocs()[i]);
1282 } else {
1283 assert(TL.getNumProtocols() == 0);
1284 TL.setLAngleLoc(SourceLocation());
1285 TL.setRAngleLoc(SourceLocation());
1286 }
1287 }
1288 void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
1289 assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
1290
1291 TL.setStarLoc(SourceLocation());
1292
1293 if (DS.getProtocolQualifiers()) {
1294 assert(TL.getNumProtocols() > 0);
1295 assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
1296 TL.setHasProtocolsAsWritten(true);
1297 TL.setLAngleLoc(DS.getProtocolLAngleLoc());
1298 TL.setRAngleLoc(DS.getSourceRange().getEnd());
1299 for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i)
1300 TL.setProtocolLoc(i, DS.getProtocolLocs()[i]);
1301
1302 } else {
1303 assert(TL.getNumProtocols() == 0);
1304 TL.setHasProtocolsAsWritten(false);
1305 TL.setLAngleLoc(SourceLocation());
1306 TL.setRAngleLoc(SourceLocation());
1307 }
1308
1309 // This might not have been written with an inner type.
1310 if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) {
1311 TL.setHasBaseTypeAsWritten(false);
1312 TL.getBaseTypeLoc().initialize(SourceLocation());
1313 } else {
1314 TL.setHasBaseTypeAsWritten(true);
John McCall51bd8032009-10-18 01:05:36 +00001315 Visit(TL.getBaseTypeLoc());
John McCall54e14c42009-10-22 22:37:11 +00001316 }
John McCall51bd8032009-10-18 01:05:36 +00001317 }
John McCall833ca992009-10-29 08:12:44 +00001318 void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) {
1319 DeclaratorInfo *DInfo = 0;
1320 Sema::GetTypeFromParser(DS.getTypeRep(), &DInfo);
1321
1322 // If we got no declarator info from previous Sema routines,
1323 // just fill with the typespec loc.
1324 if (!DInfo) {
1325 TL.initialize(DS.getTypeSpecTypeLoc());
1326 return;
1327 }
1328
1329 TemplateSpecializationTypeLoc OldTL =
1330 cast<TemplateSpecializationTypeLoc>(DInfo->getTypeLoc());
1331 TL.copy(OldTL);
1332 }
John McCall51bd8032009-10-18 01:05:36 +00001333 void VisitTypeLoc(TypeLoc TL) {
1334 // FIXME: add other typespec types and change this to an assert.
1335 TL.initialize(DS.getTypeSpecTypeLoc());
1336 }
1337 };
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001338
John McCall51bd8032009-10-18 01:05:36 +00001339 class DeclaratorLocFiller : public TypeLocVisitor<DeclaratorLocFiller> {
1340 const DeclaratorChunk &Chunk;
1341
1342 public:
1343 DeclaratorLocFiller(const DeclaratorChunk &Chunk) : Chunk(Chunk) {}
1344
1345 void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
1346 llvm::llvm_unreachable("qualified type locs not expected here!");
1347 }
1348
1349 void VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
1350 assert(Chunk.Kind == DeclaratorChunk::BlockPointer);
1351 TL.setCaretLoc(Chunk.Loc);
1352 }
1353 void VisitPointerTypeLoc(PointerTypeLoc TL) {
1354 assert(Chunk.Kind == DeclaratorChunk::Pointer);
1355 TL.setStarLoc(Chunk.Loc);
1356 }
1357 void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
1358 assert(Chunk.Kind == DeclaratorChunk::Pointer);
1359 TL.setStarLoc(Chunk.Loc);
John McCall54e14c42009-10-22 22:37:11 +00001360 TL.setHasBaseTypeAsWritten(true);
1361 TL.setHasProtocolsAsWritten(false);
1362 TL.setLAngleLoc(SourceLocation());
1363 TL.setRAngleLoc(SourceLocation());
John McCall51bd8032009-10-18 01:05:36 +00001364 }
1365 void VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
1366 assert(Chunk.Kind == DeclaratorChunk::MemberPointer);
1367 TL.setStarLoc(Chunk.Loc);
1368 // FIXME: nested name specifier
1369 }
1370 void VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
1371 assert(Chunk.Kind == DeclaratorChunk::Reference);
John McCall54e14c42009-10-22 22:37:11 +00001372 // 'Amp' is misleading: this might have been originally
1373 /// spelled with AmpAmp.
John McCall51bd8032009-10-18 01:05:36 +00001374 TL.setAmpLoc(Chunk.Loc);
1375 }
1376 void VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
1377 assert(Chunk.Kind == DeclaratorChunk::Reference);
1378 assert(!Chunk.Ref.LValueRef);
1379 TL.setAmpAmpLoc(Chunk.Loc);
1380 }
1381 void VisitArrayTypeLoc(ArrayTypeLoc TL) {
1382 assert(Chunk.Kind == DeclaratorChunk::Array);
1383 TL.setLBracketLoc(Chunk.Loc);
1384 TL.setRBracketLoc(Chunk.EndLoc);
1385 TL.setSizeExpr(static_cast<Expr*>(Chunk.Arr.NumElts));
1386 }
1387 void VisitFunctionTypeLoc(FunctionTypeLoc TL) {
1388 assert(Chunk.Kind == DeclaratorChunk::Function);
1389 TL.setLParenLoc(Chunk.Loc);
1390 TL.setRParenLoc(Chunk.EndLoc);
1391
1392 const DeclaratorChunk::FunctionTypeInfo &FTI = Chunk.Fun;
John McCall54e14c42009-10-22 22:37:11 +00001393 for (unsigned i = 0, e = TL.getNumArgs(), tpi = 0; i != e; ++i) {
John McCall51bd8032009-10-18 01:05:36 +00001394 ParmVarDecl *Param = FTI.ArgInfo[i].Param.getAs<ParmVarDecl>();
John McCall54e14c42009-10-22 22:37:11 +00001395 TL.setArg(tpi++, Param);
John McCall51bd8032009-10-18 01:05:36 +00001396 }
1397 // FIXME: exception specs
1398 }
1399
1400 void VisitTypeLoc(TypeLoc TL) {
1401 llvm::llvm_unreachable("unsupported TypeLoc kind in declarator!");
1402 }
1403 };
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001404}
1405
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001406/// \brief Create and instantiate a DeclaratorInfo with type source information.
1407///
1408/// \param T QualType referring to the type as written in source code.
1409DeclaratorInfo *
Sebastian Redl8ce35b02009-10-25 21:45:37 +00001410Sema::GetDeclaratorInfoForDeclarator(Declarator &D, QualType T) {
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001411 DeclaratorInfo *DInfo = Context.CreateDeclaratorInfo(T);
John McCall51bd8032009-10-18 01:05:36 +00001412 UnqualTypeLoc CurrTL = DInfo->getTypeLoc().getUnqualifiedLoc();
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001413
Sebastian Redl8ce35b02009-10-25 21:45:37 +00001414 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
John McCall51bd8032009-10-18 01:05:36 +00001415 DeclaratorLocFiller(D.getTypeObject(i)).Visit(CurrTL);
1416 CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001417 }
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001418
John McCall51bd8032009-10-18 01:05:36 +00001419 TypeSpecLocFiller(D.getDeclSpec()).Visit(CurrTL);
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001420
1421 return DInfo;
1422}
1423
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001424/// \brief Create a LocInfoType to hold the given QualType and DeclaratorInfo.
1425QualType Sema::CreateLocInfoType(QualType T, DeclaratorInfo *DInfo) {
1426 // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser
1427 // and Sema during declaration parsing. Try deallocating/caching them when
1428 // it's appropriate, instead of allocating them and keeping them around.
1429 LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType), 8);
1430 new (LocT) LocInfoType(T, DInfo);
1431 assert(LocT->getTypeClass() != T->getTypeClass() &&
1432 "LocInfoType's TypeClass conflicts with an existing Type class");
1433 return QualType(LocT, 0);
1434}
1435
1436void LocInfoType::getAsStringInternal(std::string &Str,
1437 const PrintingPolicy &Policy) const {
Argyrios Kyrtzidis35d44e52009-08-19 01:46:06 +00001438 assert(false && "LocInfoType leaked into the type system; an opaque TypeTy*"
1439 " was used directly instead of getting the QualType through"
1440 " GetTypeFromParser");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001441}
1442
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001443/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
Fariborz Jahanian360300c2007-11-09 22:27:59 +00001444/// declarator
Chris Lattnerb28317a2009-03-28 19:18:32 +00001445QualType Sema::ObjCGetTypeForMethodDefinition(DeclPtrTy D) {
1446 ObjCMethodDecl *MDecl = cast<ObjCMethodDecl>(D.getAs<Decl>());
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001447 QualType T = MDecl->getResultType();
1448 llvm::SmallVector<QualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +00001449
Fariborz Jahanian35600022007-11-09 17:18:29 +00001450 // Add the first two invisible argument types for self and _cmd.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001451 if (MDecl->isInstanceMethod()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001452 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +00001453 selfTy = Context.getPointerType(selfTy);
1454 ArgTys.push_back(selfTy);
Chris Lattner89951a82009-02-20 18:43:26 +00001455 } else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001456 ArgTys.push_back(Context.getObjCIdType());
1457 ArgTys.push_back(Context.getObjCSelType());
Mike Stump1eb44332009-09-09 15:08:12 +00001458
Chris Lattner89951a82009-02-20 18:43:26 +00001459 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
1460 E = MDecl->param_end(); PI != E; ++PI) {
1461 QualType ArgTy = (*PI)->getType();
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001462 assert(!ArgTy.isNull() && "Couldn't parse type?");
Douglas Gregorbeb58cb2009-03-23 23:17:00 +00001463 ArgTy = adjustParameterType(ArgTy);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001464 ArgTys.push_back(ArgTy);
1465 }
1466 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001467 MDecl->isVariadic(), 0);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001468 return T;
1469}
1470
Sebastian Redl9e5e4aa2009-01-26 19:54:48 +00001471/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that
1472/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
1473/// they point to and return true. If T1 and T2 aren't pointer types
1474/// or pointer-to-member types, or if they are not similar at this
1475/// level, returns false and leaves T1 and T2 unchanged. Top-level
1476/// qualifiers on T1 and T2 are ignored. This function will typically
1477/// be called in a loop that successively "unwraps" pointer and
1478/// pointer-to-member types to compare them at each level.
Chris Lattnerecb81f22009-02-16 21:43:00 +00001479bool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001480 const PointerType *T1PtrType = T1->getAs<PointerType>(),
1481 *T2PtrType = T2->getAs<PointerType>();
Douglas Gregor57373262008-10-22 14:17:15 +00001482 if (T1PtrType && T2PtrType) {
1483 T1 = T1PtrType->getPointeeType();
1484 T2 = T2PtrType->getPointeeType();
1485 return true;
1486 }
1487
Ted Kremenek6217b802009-07-29 21:53:49 +00001488 const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(),
1489 *T2MPType = T2->getAs<MemberPointerType>();
Sebastian Redl21593ac2009-01-28 18:33:18 +00001490 if (T1MPType && T2MPType &&
1491 Context.getCanonicalType(T1MPType->getClass()) ==
1492 Context.getCanonicalType(T2MPType->getClass())) {
Sebastian Redl4433aaf2009-01-25 19:43:20 +00001493 T1 = T1MPType->getPointeeType();
1494 T2 = T2MPType->getPointeeType();
1495 return true;
1496 }
Douglas Gregor57373262008-10-22 14:17:15 +00001497 return false;
1498}
1499
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001500Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001501 // C99 6.7.6: Type names have no identifier. This is already validated by
1502 // the parser.
1503 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00001504
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001505 DeclaratorInfo *DInfo = 0;
Douglas Gregor402abb52009-05-28 23:31:59 +00001506 TagDecl *OwnedTag = 0;
Sebastian Redl8ce35b02009-10-25 21:45:37 +00001507 QualType T = GetTypeForDeclarator(D, S, &DInfo, &OwnedTag);
Chris Lattner5153ee62009-04-25 08:47:54 +00001508 if (D.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +00001509 return true;
Steve Naroff5912a352007-08-28 20:14:24 +00001510
Douglas Gregor402abb52009-05-28 23:31:59 +00001511 if (getLangOptions().CPlusPlus) {
1512 // Check that there are no default arguments (C++ only).
Douglas Gregor6d6eb572008-05-07 04:49:29 +00001513 CheckExtraCXXDefaultArguments(D);
1514
Douglas Gregor402abb52009-05-28 23:31:59 +00001515 // C++0x [dcl.type]p3:
1516 // A type-specifier-seq shall not define a class or enumeration
1517 // unless it appears in the type-id of an alias-declaration
1518 // (7.1.3).
1519 if (OwnedTag && OwnedTag->isDefinition())
1520 Diag(OwnedTag->getLocation(), diag::err_type_defined_in_type_specifier)
1521 << Context.getTypeDeclType(OwnedTag);
1522 }
1523
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001524 if (DInfo)
1525 T = CreateLocInfoType(T, DInfo);
1526
Reid Spencer5f016e22007-07-11 17:01:13 +00001527 return T.getAsOpaquePtr();
1528}
1529
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001530
1531
1532//===----------------------------------------------------------------------===//
1533// Type Attribute Processing
1534//===----------------------------------------------------------------------===//
1535
1536/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
1537/// specified type. The attribute contains 1 argument, the id of the address
1538/// space for the type.
Mike Stump1eb44332009-09-09 15:08:12 +00001539static void HandleAddressSpaceTypeAttribute(QualType &Type,
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001540 const AttributeList &Attr, Sema &S){
John McCall0953e762009-09-24 19:53:00 +00001541
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001542 // If this type is already address space qualified, reject it.
1543 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
1544 // for two or more different address spaces."
1545 if (Type.getAddressSpace()) {
1546 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
1547 return;
1548 }
Mike Stump1eb44332009-09-09 15:08:12 +00001549
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001550 // Check the attribute arguments.
1551 if (Attr.getNumArgs() != 1) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +00001552 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001553 return;
1554 }
1555 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
1556 llvm::APSInt addrSpace(32);
1557 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001558 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
1559 << ASArgExpr->getSourceRange();
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001560 return;
1561 }
1562
John McCallefadb772009-07-28 06:52:18 +00001563 // Bounds checking.
1564 if (addrSpace.isSigned()) {
1565 if (addrSpace.isNegative()) {
1566 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative)
1567 << ASArgExpr->getSourceRange();
1568 return;
1569 }
1570 addrSpace.setIsSigned(false);
1571 }
1572 llvm::APSInt max(addrSpace.getBitWidth());
John McCall0953e762009-09-24 19:53:00 +00001573 max = Qualifiers::MaxAddressSpace;
John McCallefadb772009-07-28 06:52:18 +00001574 if (addrSpace > max) {
1575 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high)
John McCall0953e762009-09-24 19:53:00 +00001576 << Qualifiers::MaxAddressSpace << ASArgExpr->getSourceRange();
John McCallefadb772009-07-28 06:52:18 +00001577 return;
1578 }
1579
Mike Stump1eb44332009-09-09 15:08:12 +00001580 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001581 Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001582}
1583
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001584/// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the
1585/// specified type. The attribute contains 1 argument, weak or strong.
Mike Stump1eb44332009-09-09 15:08:12 +00001586static void HandleObjCGCTypeAttribute(QualType &Type,
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001587 const AttributeList &Attr, Sema &S) {
John McCall0953e762009-09-24 19:53:00 +00001588 if (Type.getObjCGCAttr() != Qualifiers::GCNone) {
Fariborz Jahanian5934e752009-02-18 18:52:41 +00001589 S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001590 return;
1591 }
Mike Stump1eb44332009-09-09 15:08:12 +00001592
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001593 // Check the attribute arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001594 if (!Attr.getParameterName()) {
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001595 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1596 << "objc_gc" << 1;
1597 return;
1598 }
John McCall0953e762009-09-24 19:53:00 +00001599 Qualifiers::GC GCAttr;
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001600 if (Attr.getNumArgs() != 0) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001601 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1602 return;
1603 }
Mike Stump1eb44332009-09-09 15:08:12 +00001604 if (Attr.getParameterName()->isStr("weak"))
John McCall0953e762009-09-24 19:53:00 +00001605 GCAttr = Qualifiers::Weak;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001606 else if (Attr.getParameterName()->isStr("strong"))
John McCall0953e762009-09-24 19:53:00 +00001607 GCAttr = Qualifiers::Strong;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001608 else {
1609 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1610 << "objc_gc" << Attr.getParameterName();
1611 return;
1612 }
Mike Stump1eb44332009-09-09 15:08:12 +00001613
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001614 Type = S.Context.getObjCGCQualType(Type, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001615}
1616
Mike Stump24556362009-07-25 21:26:53 +00001617/// HandleNoReturnTypeAttribute - Process the noreturn attribute on the
1618/// specified type. The attribute contains 0 arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001619static void HandleNoReturnTypeAttribute(QualType &Type,
Mike Stump24556362009-07-25 21:26:53 +00001620 const AttributeList &Attr, Sema &S) {
1621 if (Attr.getNumArgs() != 0)
1622 return;
1623
1624 // We only apply this to a pointer to function or a pointer to block.
1625 if (!Type->isFunctionPointerType()
1626 && !Type->isBlockPointerType()
1627 && !Type->isFunctionType())
1628 return;
1629
1630 Type = S.Context.getNoReturnType(Type);
1631}
1632
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001633void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) {
Chris Lattner232e8822008-02-21 01:08:11 +00001634 // Scan through and apply attributes to this type where it makes sense. Some
1635 // attributes (such as __address_space__, __vector_size__, etc) apply to the
1636 // type, but others can be present in the type specifiers even though they
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00001637 // apply to the decl. Here we apply type attributes and ignore the rest.
1638 for (; AL; AL = AL->getNext()) {
Chris Lattner232e8822008-02-21 01:08:11 +00001639 // If this is an attribute we can handle, do so now, otherwise, add it to
1640 // the LeftOverAttrs list for rechaining.
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00001641 switch (AL->getKind()) {
Chris Lattner232e8822008-02-21 01:08:11 +00001642 default: break;
1643 case AttributeList::AT_address_space:
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001644 HandleAddressSpaceTypeAttribute(Result, *AL, *this);
1645 break;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001646 case AttributeList::AT_objc_gc:
1647 HandleObjCGCTypeAttribute(Result, *AL, *this);
1648 break;
Mike Stump24556362009-07-25 21:26:53 +00001649 case AttributeList::AT_noreturn:
1650 HandleNoReturnTypeAttribute(Result, *AL, *this);
1651 break;
Chris Lattner232e8822008-02-21 01:08:11 +00001652 }
Chris Lattner232e8822008-02-21 01:08:11 +00001653 }
Chris Lattner232e8822008-02-21 01:08:11 +00001654}
1655
Mike Stump1eb44332009-09-09 15:08:12 +00001656/// @brief Ensure that the type T is a complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001657///
1658/// This routine checks whether the type @p T is complete in any
1659/// context where a complete type is required. If @p T is a complete
Douglas Gregor86447ec2009-03-09 16:13:40 +00001660/// type, returns false. If @p T is a class template specialization,
1661/// this routine then attempts to perform class template
1662/// instantiation. If instantiation fails, or if @p T is incomplete
1663/// and cannot be completed, issues the diagnostic @p diag (giving it
1664/// the type @p T) and returns true.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001665///
1666/// @param Loc The location in the source that the incomplete type
1667/// diagnostic should refer to.
1668///
1669/// @param T The type that this routine is examining for completeness.
1670///
Mike Stump1eb44332009-09-09 15:08:12 +00001671/// @param PD The partial diagnostic that will be printed out if T is not a
Anders Carlssonb7906612009-08-26 23:45:07 +00001672/// complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001673///
1674/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
1675/// @c false otherwise.
Anders Carlsson91a0cc92009-08-26 22:33:56 +00001676bool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
Anders Carlsson8c8d9192009-10-09 23:51:55 +00001677 const PartialDiagnostic &PD,
1678 std::pair<SourceLocation,
1679 PartialDiagnostic> Note) {
Anders Carlsson91a0cc92009-08-26 22:33:56 +00001680 unsigned diag = PD.getDiagID();
Mike Stump1eb44332009-09-09 15:08:12 +00001681
Douglas Gregor573d9c32009-10-21 23:19:44 +00001682 // FIXME: Add this assertion to make sure we always get instantiation points.
1683 // assert(!Loc.isInvalid() && "Invalid location in RequireCompleteType");
Douglas Gregor690dc7f2009-05-21 23:48:18 +00001684 // FIXME: Add this assertion to help us flush out problems with
1685 // checking for dependent types and type-dependent expressions.
1686 //
Mike Stump1eb44332009-09-09 15:08:12 +00001687 // assert(!T->isDependentType() &&
Douglas Gregor690dc7f2009-05-21 23:48:18 +00001688 // "Can't ask whether a dependent type is complete");
1689
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001690 // If we have a complete type, we're done.
1691 if (!T->isIncompleteType())
1692 return false;
Eli Friedman3c0eb162008-05-27 03:33:27 +00001693
Douglas Gregord475b8d2009-03-25 21:17:03 +00001694 // If we have a class template specialization or a class member of a
1695 // class template specialization, try to instantiate it.
Ted Kremenek6217b802009-07-29 21:53:49 +00001696 if (const RecordType *Record = T->getAs<RecordType>()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001697 if (ClassTemplateSpecializationDecl *ClassTemplateSpec
Douglas Gregord475b8d2009-03-25 21:17:03 +00001698 = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001699 if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared)
1700 return InstantiateClassTemplateSpecialization(Loc, ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001701 TSK_ImplicitInstantiation,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001702 /*Complain=*/diag != 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001703 } else if (CXXRecordDecl *Rec
Douglas Gregord475b8d2009-03-25 21:17:03 +00001704 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
1705 if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001706 MemberSpecializationInfo *MSInfo = Rec->getMemberSpecializationInfo();
1707 assert(MSInfo && "Missing member specialization information?");
Douglas Gregor357bbd02009-08-28 20:50:45 +00001708 // This record was instantiated from a class within a template.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001709 if (MSInfo->getTemplateSpecializationKind()
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001710 != TSK_ExplicitSpecialization)
Douglas Gregorf6b11852009-10-08 15:14:33 +00001711 return InstantiateClass(Loc, Rec, Pattern,
1712 getTemplateInstantiationArgs(Rec),
1713 TSK_ImplicitInstantiation,
1714 /*Complain=*/diag != 0);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001715 }
1716 }
1717 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00001718
Douglas Gregor5842ba92009-08-24 15:23:48 +00001719 if (diag == 0)
1720 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001721
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001722 // We have an incomplete type. Produce a diagnostic.
Anders Carlsson91a0cc92009-08-26 22:33:56 +00001723 Diag(Loc, PD) << T;
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001724
Anders Carlsson8c8d9192009-10-09 23:51:55 +00001725 // If we have a note, produce it.
1726 if (!Note.first.isInvalid())
1727 Diag(Note.first, Note.second);
1728
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001729 // If the type was a forward declaration of a class/struct/union
Mike Stump1eb44332009-09-09 15:08:12 +00001730 // type, produce
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001731 const TagType *Tag = 0;
Ted Kremenek6217b802009-07-29 21:53:49 +00001732 if (const RecordType *Record = T->getAs<RecordType>())
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001733 Tag = Record;
John McCall183700f2009-09-21 23:43:11 +00001734 else if (const EnumType *Enum = T->getAs<EnumType>())
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001735 Tag = Enum;
1736
1737 if (Tag && !Tag->getDecl()->isInvalidDecl())
Mike Stump1eb44332009-09-09 15:08:12 +00001738 Diag(Tag->getDecl()->getLocation(),
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001739 Tag->isBeingDefined() ? diag::note_type_being_defined
1740 : diag::note_forward_declaration)
1741 << QualType(Tag, 0);
1742
1743 return true;
1744}
Douglas Gregore6258932009-03-19 00:39:20 +00001745
1746/// \brief Retrieve a version of the type 'T' that is qualified by the
1747/// nested-name-specifier contained in SS.
1748QualType Sema::getQualifiedNameType(const CXXScopeSpec &SS, QualType T) {
1749 if (!SS.isSet() || SS.isInvalid() || T.isNull())
1750 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00001751
Douglas Gregorab452ba2009-03-26 23:50:42 +00001752 NestedNameSpecifier *NNS
Douglas Gregor35073692009-03-26 23:56:24 +00001753 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Douglas Gregorab452ba2009-03-26 23:50:42 +00001754 return Context.getQualifiedNameType(NNS, T);
Douglas Gregore6258932009-03-19 00:39:20 +00001755}
Anders Carlssonaf017e62009-06-29 22:58:55 +00001756
1757QualType Sema::BuildTypeofExprType(Expr *E) {
1758 return Context.getTypeOfExprType(E);
1759}
1760
1761QualType Sema::BuildDecltypeType(Expr *E) {
1762 if (E->getType() == Context.OverloadTy) {
Mike Stump1eb44332009-09-09 15:08:12 +00001763 Diag(E->getLocStart(),
Anders Carlssonaf017e62009-06-29 22:58:55 +00001764 diag::err_cannot_determine_declared_type_of_overloaded_function);
1765 return QualType();
1766 }
1767 return Context.getDecltypeType(E);
1768}