blob: 0003b1b0c4193bd7c734cba621c91df53bcb6d1c [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- SemaType.cpp - Semantic Analysis for Types -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements type-related semantic analysis.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
15#include "clang/AST/ASTContext.h"
Douglas Gregora8f32e02009-10-06 17:59:45 +000016#include "clang/AST/CXXInheritance.h"
Steve Naroff980e5082007-10-01 19:00:59 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregor2943aed2009-03-03 04:44:36 +000018#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +000019#include "clang/AST/TypeLoc.h"
John McCall51bd8032009-10-18 01:05:36 +000020#include "clang/AST/TypeLocVisitor.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000021#include "clang/AST/Expr.h"
Anders Carlsson91a0cc92009-08-26 22:33:56 +000022#include "clang/Basic/PartialDiagnostic.h"
Daniel Dunbare4858a62008-08-11 03:45:03 +000023#include "clang/Parse/DeclSpec.h"
Sebastian Redl4994d2d2009-07-04 11:39:00 +000024#include "llvm/ADT/SmallPtrSet.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000025using namespace clang;
26
Douglas Gregor2dc0e642009-03-23 23:06:20 +000027/// \brief Perform adjustment on the parameter type of a function.
28///
29/// This routine adjusts the given parameter type @p T to the actual
Mike Stump1eb44332009-09-09 15:08:12 +000030/// parameter type used by semantic analysis (C99 6.7.5.3p[7,8],
31/// C++ [dcl.fct]p3). The adjusted parameter type is returned.
Douglas Gregor2dc0e642009-03-23 23:06:20 +000032QualType Sema::adjustParameterType(QualType T) {
33 // C99 6.7.5.3p7:
Chris Lattner778ed742009-10-25 17:36:50 +000034 // A declaration of a parameter as "array of type" shall be
35 // adjusted to "qualified pointer to type", where the type
36 // qualifiers (if any) are those specified within the [ and ] of
37 // the array type derivation.
38 if (T->isArrayType())
Douglas Gregor2dc0e642009-03-23 23:06:20 +000039 return Context.getArrayDecayedType(T);
Chris Lattner778ed742009-10-25 17:36:50 +000040
41 // C99 6.7.5.3p8:
42 // A declaration of a parameter as "function returning type"
43 // shall be adjusted to "pointer to function returning type", as
44 // in 6.3.2.1.
45 if (T->isFunctionType())
Douglas Gregor2dc0e642009-03-23 23:06:20 +000046 return Context.getPointerType(T);
47
48 return T;
49}
50
Chris Lattner5db2bb12009-10-25 18:21:37 +000051
52
53/// isOmittedBlockReturnType - Return true if this declarator is missing a
54/// return type because this is a omitted return type on a block literal.
Sebastian Redl8ce35b02009-10-25 21:45:37 +000055static bool isOmittedBlockReturnType(const Declarator &D) {
Chris Lattner5db2bb12009-10-25 18:21:37 +000056 if (D.getContext() != Declarator::BlockLiteralContext ||
Sebastian Redl8ce35b02009-10-25 21:45:37 +000057 D.getDeclSpec().hasTypeSpecifier())
Chris Lattner5db2bb12009-10-25 18:21:37 +000058 return false;
59
60 if (D.getNumTypeObjects() == 0)
Chris Lattnera64ef0a2009-10-25 22:09:09 +000061 return true; // ^{ ... }
Chris Lattner5db2bb12009-10-25 18:21:37 +000062
63 if (D.getNumTypeObjects() == 1 &&
64 D.getTypeObject(0).Kind == DeclaratorChunk::Function)
Chris Lattnera64ef0a2009-10-25 22:09:09 +000065 return true; // ^(int X, float Y) { ... }
Chris Lattner5db2bb12009-10-25 18:21:37 +000066
67 return false;
68}
69
Douglas Gregor930d8b52009-01-30 22:09:00 +000070/// \brief Convert the specified declspec to the appropriate type
71/// object.
Chris Lattner5db2bb12009-10-25 18:21:37 +000072/// \param D the declarator containing the declaration specifier.
Chris Lattner5153ee62009-04-25 08:47:54 +000073/// \returns The type described by the declaration specifiers. This function
74/// never returns null.
Sebastian Redl8ce35b02009-10-25 21:45:37 +000075static QualType ConvertDeclSpecToType(Declarator &TheDeclarator, Sema &TheSema){
Reid Spencer5f016e22007-07-11 17:01:13 +000076 // FIXME: Should move the logic from DeclSpec::Finish to here for validity
77 // checking.
Chris Lattner5db2bb12009-10-25 18:21:37 +000078 const DeclSpec &DS = TheDeclarator.getDeclSpec();
79 SourceLocation DeclLoc = TheDeclarator.getIdentifierLoc();
80 if (DeclLoc.isInvalid())
81 DeclLoc = DS.getSourceRange().getBegin();
Chris Lattner1564e392009-10-25 18:07:27 +000082
83 ASTContext &Context = TheSema.Context;
Mike Stump1eb44332009-09-09 15:08:12 +000084
Chris Lattner5db2bb12009-10-25 18:21:37 +000085 QualType Result;
Reid Spencer5f016e22007-07-11 17:01:13 +000086 switch (DS.getTypeSpecType()) {
Chris Lattner96b77fc2008-04-02 06:50:17 +000087 case DeclSpec::TST_void:
88 Result = Context.VoidTy;
89 break;
Reid Spencer5f016e22007-07-11 17:01:13 +000090 case DeclSpec::TST_char:
91 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
Chris Lattnerfab5b452008-02-20 23:53:49 +000092 Result = Context.CharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000093 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
Chris Lattnerfab5b452008-02-20 23:53:49 +000094 Result = Context.SignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000095 else {
96 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
97 "Unknown TSS value");
Chris Lattnerfab5b452008-02-20 23:53:49 +000098 Result = Context.UnsignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000099 }
Chris Lattner958858e2008-02-20 21:40:32 +0000100 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000101 case DeclSpec::TST_wchar:
102 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
103 Result = Context.WCharTy;
104 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
Chris Lattner1564e392009-10-25 18:07:27 +0000105 TheSema.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000106 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000107 Result = Context.getSignedWCharType();
108 } else {
109 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
110 "Unknown TSS value");
Chris Lattner1564e392009-10-25 18:07:27 +0000111 TheSema.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000112 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000113 Result = Context.getUnsignedWCharType();
114 }
115 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000116 case DeclSpec::TST_char16:
117 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
118 "Unknown TSS value");
119 Result = Context.Char16Ty;
120 break;
121 case DeclSpec::TST_char32:
122 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
123 "Unknown TSS value");
124 Result = Context.Char32Ty;
125 break;
Chris Lattnerd658b562008-04-05 06:32:51 +0000126 case DeclSpec::TST_unspecified:
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000127 // "<proto1,proto2>" is an objc qualified ID with a missing id.
Chris Lattner097e9162008-10-20 02:01:50 +0000128 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000129 Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy,
Steve Naroff14108da2009-07-10 23:34:53 +0000130 (ObjCProtocolDecl**)PQ,
Steve Naroff683087f2009-06-29 16:22:52 +0000131 DS.getNumProtocolQualifiers());
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000132 break;
133 }
Chris Lattner5db2bb12009-10-25 18:21:37 +0000134
135 // If this is a missing declspec in a block literal return context, then it
136 // is inferred from the return statements inside the block.
Sebastian Redl8ce35b02009-10-25 21:45:37 +0000137 if (isOmittedBlockReturnType(TheDeclarator)) {
Chris Lattner5db2bb12009-10-25 18:21:37 +0000138 Result = Context.DependentTy;
139 break;
140 }
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Chris Lattnerd658b562008-04-05 06:32:51 +0000142 // Unspecified typespec defaults to int in C90. However, the C90 grammar
143 // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
144 // type-qualifier, or storage-class-specifier. If not, emit an extwarn.
145 // Note that the one exception to this is function definitions, which are
146 // allowed to be completely missing a declspec. This is handled in the
147 // parser already though by it pretending to have seen an 'int' in this
148 // case.
Chris Lattner1564e392009-10-25 18:07:27 +0000149 if (TheSema.getLangOptions().ImplicitInt) {
Chris Lattner35d276f2009-02-27 18:53:28 +0000150 // In C89 mode, we only warn if there is a completely missing declspec
151 // when one is not allowed.
Chris Lattner3f84ad22009-04-22 05:27:59 +0000152 if (DS.isEmpty()) {
Chris Lattner1564e392009-10-25 18:07:27 +0000153 TheSema.Diag(DeclLoc, diag::ext_missing_declspec)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000154 << DS.getSourceRange()
Chris Lattner173144a2009-02-27 22:31:56 +0000155 << CodeModificationHint::CreateInsertion(DS.getSourceRange().getBegin(),
156 "int");
Chris Lattner3f84ad22009-04-22 05:27:59 +0000157 }
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000158 } else if (!DS.hasTypeSpecifier()) {
Chris Lattnerd658b562008-04-05 06:32:51 +0000159 // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says:
160 // "At least one type specifier shall be given in the declaration
161 // specifiers in each declaration, and in the specifier-qualifier list in
162 // each struct declaration and type name."
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000163 // FIXME: Does Microsoft really have the implicit int extension in C++?
Chris Lattner1564e392009-10-25 18:07:27 +0000164 if (TheSema.getLangOptions().CPlusPlus &&
165 !TheSema.getLangOptions().Microsoft) {
166 TheSema.Diag(DeclLoc, diag::err_missing_type_specifier)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000167 << DS.getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000168
Chris Lattnerb78d8332009-06-26 04:45:06 +0000169 // When this occurs in C++ code, often something is very broken with the
170 // value being declared, poison it as invalid so we don't get chains of
171 // errors.
Chris Lattner5db2bb12009-10-25 18:21:37 +0000172 TheDeclarator.setInvalidType(true);
Chris Lattnerb78d8332009-06-26 04:45:06 +0000173 } else {
Chris Lattner1564e392009-10-25 18:07:27 +0000174 TheSema.Diag(DeclLoc, diag::ext_missing_type_specifier)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000175 << DS.getSourceRange();
Chris Lattnerb78d8332009-06-26 04:45:06 +0000176 }
Chris Lattnerd658b562008-04-05 06:32:51 +0000177 }
Mike Stump1eb44332009-09-09 15:08:12 +0000178
179 // FALL THROUGH.
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000180 case DeclSpec::TST_int: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000181 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
182 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000183 case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
184 case DeclSpec::TSW_short: Result = Context.ShortTy; break;
185 case DeclSpec::TSW_long: Result = Context.LongTy; break;
Chris Lattner311157f2009-10-25 18:25:04 +0000186 case DeclSpec::TSW_longlong:
187 Result = Context.LongLongTy;
188
189 // long long is a C99 feature.
190 if (!TheSema.getLangOptions().C99 &&
191 !TheSema.getLangOptions().CPlusPlus0x)
192 TheSema.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong);
193 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000194 }
195 } else {
196 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000197 case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
198 case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break;
199 case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break;
Chris Lattner311157f2009-10-25 18:25:04 +0000200 case DeclSpec::TSW_longlong:
201 Result = Context.UnsignedLongLongTy;
202
203 // long long is a C99 feature.
204 if (!TheSema.getLangOptions().C99 &&
205 !TheSema.getLangOptions().CPlusPlus0x)
206 TheSema.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong);
207 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000208 }
209 }
Chris Lattner958858e2008-02-20 21:40:32 +0000210 break;
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000211 }
Chris Lattnerfab5b452008-02-20 23:53:49 +0000212 case DeclSpec::TST_float: Result = Context.FloatTy; break;
Chris Lattner958858e2008-02-20 21:40:32 +0000213 case DeclSpec::TST_double:
214 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000215 Result = Context.LongDoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000216 else
Chris Lattnerfab5b452008-02-20 23:53:49 +0000217 Result = Context.DoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000218 break;
Chris Lattnerfab5b452008-02-20 23:53:49 +0000219 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
Reid Spencer5f016e22007-07-11 17:01:13 +0000220 case DeclSpec::TST_decimal32: // _Decimal32
221 case DeclSpec::TST_decimal64: // _Decimal64
222 case DeclSpec::TST_decimal128: // _Decimal128
Chris Lattner1564e392009-10-25 18:07:27 +0000223 TheSema.Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported);
Chris Lattner8f12f652009-05-13 05:02:08 +0000224 Result = Context.IntTy;
Chris Lattner5db2bb12009-10-25 18:21:37 +0000225 TheDeclarator.setInvalidType(true);
Chris Lattner8f12f652009-05-13 05:02:08 +0000226 break;
Chris Lattner99dc9142008-04-13 18:59:07 +0000227 case DeclSpec::TST_class:
Reid Spencer5f016e22007-07-11 17:01:13 +0000228 case DeclSpec::TST_enum:
229 case DeclSpec::TST_union:
230 case DeclSpec::TST_struct: {
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000231 TypeDecl *D = cast_or_null<TypeDecl>(static_cast<Decl *>(DS.getTypeRep()));
John McCall6e247262009-10-10 05:48:19 +0000232 if (!D) {
233 // This can happen in C++ with ambiguous lookups.
234 Result = Context.IntTy;
Chris Lattner5db2bb12009-10-25 18:21:37 +0000235 TheDeclarator.setInvalidType(true);
John McCall6e247262009-10-10 05:48:19 +0000236 break;
237 }
238
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000239 // If the type is deprecated or unavailable, diagnose it.
John McCall54abf7d2009-11-04 02:18:39 +0000240 TheSema.DiagnoseUseOfDecl(D, DS.getTypeSpecTypeLoc());
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000241
Reid Spencer5f016e22007-07-11 17:01:13 +0000242 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000243 DS.getTypeSpecSign() == 0 && "No qualifiers on tag names!");
244
Reid Spencer5f016e22007-07-11 17:01:13 +0000245 // TypeQuals handled by caller.
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000246 Result = Context.getTypeDeclType(D);
John McCall2191b202009-09-05 06:31:47 +0000247
248 // In C++, make an ElaboratedType.
Chris Lattner1564e392009-10-25 18:07:27 +0000249 if (TheSema.getLangOptions().CPlusPlus) {
John McCall2191b202009-09-05 06:31:47 +0000250 TagDecl::TagKind Tag
251 = TagDecl::getTagKindForTypeSpec(DS.getTypeSpecType());
252 Result = Context.getElaboratedType(Result, Tag);
253 }
Mike Stump1eb44332009-09-09 15:08:12 +0000254
Chris Lattner5153ee62009-04-25 08:47:54 +0000255 if (D->isInvalidDecl())
Chris Lattner5db2bb12009-10-25 18:21:37 +0000256 TheDeclarator.setInvalidType(true);
Chris Lattner958858e2008-02-20 21:40:32 +0000257 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000258 }
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000259 case DeclSpec::TST_typename: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000260 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
261 DS.getTypeSpecSign() == 0 &&
262 "Can't handle qualifiers on typedef names yet!");
Chris Lattner1564e392009-10-25 18:07:27 +0000263 Result = TheSema.GetTypeFromParser(DS.getTypeRep());
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000264
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000265 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000266 if (const ObjCInterfaceType *
267 Interface = Result->getAs<ObjCInterfaceType>()) {
Steve Naroff67ef8ea2009-07-20 17:56:53 +0000268 // It would be nice if protocol qualifiers were only stored with the
269 // ObjCObjectPointerType. Unfortunately, this isn't possible due
270 // to the following typedef idiom (which is uncommon, but allowed):
Mike Stump1eb44332009-09-09 15:08:12 +0000271 //
Steve Naroff67ef8ea2009-07-20 17:56:53 +0000272 // typedef Foo<P> T;
273 // static void func() {
274 // Foo<P> *yy;
275 // T *zz;
276 // }
Steve Naroffc15cb2a2009-07-18 15:33:26 +0000277 Result = Context.getObjCInterfaceType(Interface->getDecl(),
278 (ObjCProtocolDecl**)PQ,
279 DS.getNumProtocolQualifiers());
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000280 } else if (Result->isObjCIdType())
Chris Lattnerae4da612008-07-26 01:53:50 +0000281 // id<protocol-list>
Mike Stump1eb44332009-09-09 15:08:12 +0000282 Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy,
Steve Naroff14108da2009-07-10 23:34:53 +0000283 (ObjCProtocolDecl**)PQ, DS.getNumProtocolQualifiers());
284 else if (Result->isObjCClassType()) {
Steve Naroff4262a072009-02-23 18:53:24 +0000285 // Class<protocol-list>
Mike Stump1eb44332009-09-09 15:08:12 +0000286 Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinClassTy,
Steve Naroff470301b2009-07-22 16:07:01 +0000287 (ObjCProtocolDecl**)PQ, DS.getNumProtocolQualifiers());
Chris Lattner3f84ad22009-04-22 05:27:59 +0000288 } else {
Chris Lattner1564e392009-10-25 18:07:27 +0000289 TheSema.Diag(DeclLoc, diag::err_invalid_protocol_qualifiers)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000290 << DS.getSourceRange();
Chris Lattner5db2bb12009-10-25 18:21:37 +0000291 TheDeclarator.setInvalidType(true);
Chris Lattner3f84ad22009-04-22 05:27:59 +0000292 }
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000293 }
Mike Stump1eb44332009-09-09 15:08:12 +0000294
Reid Spencer5f016e22007-07-11 17:01:13 +0000295 // TypeQuals handled by caller.
Chris Lattner958858e2008-02-20 21:40:32 +0000296 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000297 }
Chris Lattner958858e2008-02-20 21:40:32 +0000298 case DeclSpec::TST_typeofType:
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000299 // FIXME: Preserve type source info.
Chris Lattner1564e392009-10-25 18:07:27 +0000300 Result = TheSema.GetTypeFromParser(DS.getTypeRep());
Chris Lattner958858e2008-02-20 21:40:32 +0000301 assert(!Result.isNull() && "Didn't get a type for typeof?");
Steve Naroffd1861fd2007-07-31 12:34:36 +0000302 // TypeQuals handled by caller.
Chris Lattnerfab5b452008-02-20 23:53:49 +0000303 Result = Context.getTypeOfType(Result);
Chris Lattner958858e2008-02-20 21:40:32 +0000304 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000305 case DeclSpec::TST_typeofExpr: {
306 Expr *E = static_cast<Expr *>(DS.getTypeRep());
307 assert(E && "Didn't get an expression for typeof?");
308 // TypeQuals handled by caller.
Douglas Gregor72564e72009-02-26 23:50:07 +0000309 Result = Context.getTypeOfExprType(E);
Chris Lattner958858e2008-02-20 21:40:32 +0000310 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000311 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000312 case DeclSpec::TST_decltype: {
313 Expr *E = static_cast<Expr *>(DS.getTypeRep());
314 assert(E && "Didn't get an expression for decltype?");
315 // TypeQuals handled by caller.
Chris Lattner1564e392009-10-25 18:07:27 +0000316 Result = TheSema.BuildDecltypeType(E);
Anders Carlssonaf017e62009-06-29 22:58:55 +0000317 if (Result.isNull()) {
318 Result = Context.IntTy;
Chris Lattner5db2bb12009-10-25 18:21:37 +0000319 TheDeclarator.setInvalidType(true);
Anders Carlssonaf017e62009-06-29 22:58:55 +0000320 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000321 break;
322 }
Anders Carlssone89d1592009-06-26 18:41:36 +0000323 case DeclSpec::TST_auto: {
324 // TypeQuals handled by caller.
325 Result = Context.UndeducedAutoTy;
326 break;
327 }
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Douglas Gregor809070a2009-02-18 17:45:20 +0000329 case DeclSpec::TST_error:
Chris Lattner5153ee62009-04-25 08:47:54 +0000330 Result = Context.IntTy;
Chris Lattner5db2bb12009-10-25 18:21:37 +0000331 TheDeclarator.setInvalidType(true);
Chris Lattner5153ee62009-04-25 08:47:54 +0000332 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000333 }
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Chris Lattner958858e2008-02-20 21:40:32 +0000335 // Handle complex types.
Douglas Gregorf244cd72009-02-14 21:06:05 +0000336 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
Chris Lattner1564e392009-10-25 18:07:27 +0000337 if (TheSema.getLangOptions().Freestanding)
338 TheSema.Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
Chris Lattnerfab5b452008-02-20 23:53:49 +0000339 Result = Context.getComplexType(Result);
Douglas Gregorf244cd72009-02-14 21:06:05 +0000340 }
Mike Stump1eb44332009-09-09 15:08:12 +0000341
Chris Lattner958858e2008-02-20 21:40:32 +0000342 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
343 "FIXME: imaginary types not supported yet!");
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Chris Lattner38d8b982008-02-20 22:04:11 +0000345 // See if there are any attributes on the declspec that apply to the type (as
346 // opposed to the decl).
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000347 if (const AttributeList *AL = DS.getAttributes())
Chris Lattner1564e392009-10-25 18:07:27 +0000348 TheSema.ProcessTypeAttributeList(Result, AL);
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Chris Lattner96b77fc2008-04-02 06:50:17 +0000350 // Apply const/volatile/restrict qualifiers to T.
351 if (unsigned TypeQuals = DS.getTypeQualifiers()) {
352
353 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
354 // or incomplete types shall not be restrict-qualified." C++ also allows
355 // restrict-qualified references.
John McCall0953e762009-09-24 19:53:00 +0000356 if (TypeQuals & DeclSpec::TQ_restrict) {
Daniel Dunbarbb710012009-02-26 19:13:44 +0000357 if (Result->isPointerType() || Result->isReferenceType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000358 QualType EltTy = Result->isPointerType() ?
Ted Kremenek6217b802009-07-29 21:53:49 +0000359 Result->getAs<PointerType>()->getPointeeType() :
360 Result->getAs<ReferenceType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Douglas Gregorbad0e652009-03-24 20:32:41 +0000362 // If we have a pointer or reference, the pointee must have an object
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000363 // incomplete type.
364 if (!EltTy->isIncompleteOrObjectType()) {
Chris Lattner1564e392009-10-25 18:07:27 +0000365 TheSema.Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000366 diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattnerd1625842008-11-24 06:25:27 +0000367 << EltTy << DS.getSourceRange();
John McCall0953e762009-09-24 19:53:00 +0000368 TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000369 }
370 } else {
Chris Lattner1564e392009-10-25 18:07:27 +0000371 TheSema.Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000372 diag::err_typecheck_invalid_restrict_not_pointer)
Chris Lattnerd1625842008-11-24 06:25:27 +0000373 << Result << DS.getSourceRange();
John McCall0953e762009-09-24 19:53:00 +0000374 TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
Chris Lattner96b77fc2008-04-02 06:50:17 +0000375 }
Chris Lattner96b77fc2008-04-02 06:50:17 +0000376 }
Mike Stump1eb44332009-09-09 15:08:12 +0000377
Chris Lattner96b77fc2008-04-02 06:50:17 +0000378 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
379 // of a function type includes any type qualifiers, the behavior is
380 // undefined."
381 if (Result->isFunctionType() && TypeQuals) {
382 // Get some location to point at, either the C or V location.
383 SourceLocation Loc;
John McCall0953e762009-09-24 19:53:00 +0000384 if (TypeQuals & DeclSpec::TQ_const)
Chris Lattner96b77fc2008-04-02 06:50:17 +0000385 Loc = DS.getConstSpecLoc();
John McCall0953e762009-09-24 19:53:00 +0000386 else if (TypeQuals & DeclSpec::TQ_volatile)
Chris Lattner96b77fc2008-04-02 06:50:17 +0000387 Loc = DS.getVolatileSpecLoc();
John McCall0953e762009-09-24 19:53:00 +0000388 else {
389 assert((TypeQuals & DeclSpec::TQ_restrict) &&
390 "Has CVR quals but not C, V, or R?");
391 Loc = DS.getRestrictSpecLoc();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000392 }
Chris Lattner1564e392009-10-25 18:07:27 +0000393 TheSema.Diag(Loc, diag::warn_typecheck_function_qualifiers)
Chris Lattnerd1625842008-11-24 06:25:27 +0000394 << Result << DS.getSourceRange();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000395 }
Mike Stump1eb44332009-09-09 15:08:12 +0000396
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000397 // C++ [dcl.ref]p1:
398 // Cv-qualified references are ill-formed except when the
399 // cv-qualifiers are introduced through the use of a typedef
400 // (7.1.3) or of a template type argument (14.3), in which
401 // case the cv-qualifiers are ignored.
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000402 // FIXME: Shouldn't we be checking SCS_typedef here?
403 if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000404 TypeQuals && Result->isReferenceType()) {
John McCall0953e762009-09-24 19:53:00 +0000405 TypeQuals &= ~DeclSpec::TQ_const;
406 TypeQuals &= ~DeclSpec::TQ_volatile;
Mike Stump1eb44332009-09-09 15:08:12 +0000407 }
408
John McCall0953e762009-09-24 19:53:00 +0000409 Qualifiers Quals = Qualifiers::fromCVRMask(TypeQuals);
410 Result = Context.getQualifiedType(Result, Quals);
Chris Lattner96b77fc2008-04-02 06:50:17 +0000411 }
John McCall0953e762009-09-24 19:53:00 +0000412
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000413 return Result;
414}
415
Douglas Gregorcd281c32009-02-28 00:25:32 +0000416static std::string getPrintableNameForEntity(DeclarationName Entity) {
417 if (Entity)
418 return Entity.getAsString();
Mike Stump1eb44332009-09-09 15:08:12 +0000419
Douglas Gregorcd281c32009-02-28 00:25:32 +0000420 return "type name";
421}
422
423/// \brief Build a pointer type.
424///
425/// \param T The type to which we'll be building a pointer.
426///
427/// \param Quals The cvr-qualifiers to be applied to the pointer type.
428///
429/// \param Loc The location of the entity whose type involves this
430/// pointer type or, if there is no such entity, the location of the
431/// type that will have pointer type.
432///
433/// \param Entity The name of the entity that involves the pointer
434/// type, if known.
435///
436/// \returns A suitable pointer type, if there are no
437/// errors. Otherwise, returns a NULL type.
Mike Stump1eb44332009-09-09 15:08:12 +0000438QualType Sema::BuildPointerType(QualType T, unsigned Quals,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000439 SourceLocation Loc, DeclarationName Entity) {
440 if (T->isReferenceType()) {
441 // C++ 8.3.2p4: There shall be no ... pointers to references ...
442 Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
John McCallac406052009-10-30 00:37:20 +0000443 << getPrintableNameForEntity(Entity) << T;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000444 return QualType();
445 }
446
John McCall0953e762009-09-24 19:53:00 +0000447 Qualifiers Qs = Qualifiers::fromCVRMask(Quals);
448
Douglas Gregorcd281c32009-02-28 00:25:32 +0000449 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
450 // object or incomplete types shall not be restrict-qualified."
John McCall0953e762009-09-24 19:53:00 +0000451 if (Qs.hasRestrict() && !T->isIncompleteOrObjectType()) {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000452 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
453 << T;
John McCall0953e762009-09-24 19:53:00 +0000454 Qs.removeRestrict();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000455 }
456
457 // Build the pointer type.
John McCall0953e762009-09-24 19:53:00 +0000458 return Context.getQualifiedType(Context.getPointerType(T), Qs);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000459}
460
461/// \brief Build a reference type.
462///
463/// \param T The type to which we'll be building a reference.
464///
John McCall0953e762009-09-24 19:53:00 +0000465/// \param CVR The cvr-qualifiers to be applied to the reference type.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000466///
467/// \param Loc The location of the entity whose type involves this
468/// reference type or, if there is no such entity, the location of the
469/// type that will have reference type.
470///
471/// \param Entity The name of the entity that involves the reference
472/// type, if known.
473///
474/// \returns A suitable reference type, if there are no
475/// errors. Otherwise, returns a NULL type.
John McCall54e14c42009-10-22 22:37:11 +0000476QualType Sema::BuildReferenceType(QualType T, bool SpelledAsLValue,
477 unsigned CVR, SourceLocation Loc,
478 DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000479 Qualifiers Quals = Qualifiers::fromCVRMask(CVR);
John McCall54e14c42009-10-22 22:37:11 +0000480
481 bool LValueRef = SpelledAsLValue || T->getAs<LValueReferenceType>();
482
483 // C++0x [dcl.typedef]p9: If a typedef TD names a type that is a
484 // reference to a type T, and attempt to create the type "lvalue
485 // reference to cv TD" creates the type "lvalue reference to T".
486 // We use the qualifiers (restrict or none) of the original reference,
487 // not the new ones. This is consistent with GCC.
488
489 // C++ [dcl.ref]p4: There shall be no references to references.
490 //
491 // According to C++ DR 106, references to references are only
492 // diagnosed when they are written directly (e.g., "int & &"),
493 // but not when they happen via a typedef:
494 //
495 // typedef int& intref;
496 // typedef intref& intref2;
497 //
498 // Parser::ParseDeclaratorInternal diagnoses the case where
499 // references are written directly; here, we handle the
500 // collapsing of references-to-references as described in C++
501 // DR 106 and amended by C++ DR 540.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000502
503 // C++ [dcl.ref]p1:
Eli Friedman33a31382009-08-05 19:21:58 +0000504 // A declarator that specifies the type "reference to cv void"
Douglas Gregorcd281c32009-02-28 00:25:32 +0000505 // is ill-formed.
506 if (T->isVoidType()) {
507 Diag(Loc, diag::err_reference_to_void);
508 return QualType();
509 }
510
511 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
512 // object or incomplete types shall not be restrict-qualified."
John McCall0953e762009-09-24 19:53:00 +0000513 if (Quals.hasRestrict() && !T->isIncompleteOrObjectType()) {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000514 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
515 << T;
John McCall0953e762009-09-24 19:53:00 +0000516 Quals.removeRestrict();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000517 }
518
519 // C++ [dcl.ref]p1:
520 // [...] Cv-qualified references are ill-formed except when the
521 // cv-qualifiers are introduced through the use of a typedef
522 // (7.1.3) or of a template type argument (14.3), in which case
523 // the cv-qualifiers are ignored.
524 //
525 // We diagnose extraneous cv-qualifiers for the non-typedef,
526 // non-template type argument case within the parser. Here, we just
527 // ignore any extraneous cv-qualifiers.
John McCall0953e762009-09-24 19:53:00 +0000528 Quals.removeConst();
529 Quals.removeVolatile();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000530
531 // Handle restrict on references.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000532 if (LValueRef)
John McCall54e14c42009-10-22 22:37:11 +0000533 return Context.getQualifiedType(
534 Context.getLValueReferenceType(T, SpelledAsLValue), Quals);
John McCall0953e762009-09-24 19:53:00 +0000535 return Context.getQualifiedType(Context.getRValueReferenceType(T), Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000536}
537
538/// \brief Build an array type.
539///
540/// \param T The type of each element in the array.
541///
542/// \param ASM C99 array size modifier (e.g., '*', 'static').
Mike Stump1eb44332009-09-09 15:08:12 +0000543///
544/// \param ArraySize Expression describing the size of the array.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000545///
546/// \param Quals The cvr-qualifiers to be applied to the array's
547/// element type.
548///
549/// \param Loc The location of the entity whose type involves this
550/// array type or, if there is no such entity, the location of the
551/// type that will have array type.
552///
553/// \param Entity The name of the entity that involves the array
554/// type, if known.
555///
556/// \returns A suitable array type, if there are no errors. Otherwise,
557/// returns a NULL type.
558QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
559 Expr *ArraySize, unsigned Quals,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000560 SourceRange Brackets, DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000561
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000562 SourceLocation Loc = Brackets.getBegin();
Mike Stump1eb44332009-09-09 15:08:12 +0000563 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000564 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
Mike Stump1eb44332009-09-09 15:08:12 +0000565 if (RequireCompleteType(Loc, T,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000566 diag::err_illegal_decl_array_incomplete_type))
567 return QualType();
568
569 if (T->isFunctionType()) {
570 Diag(Loc, diag::err_illegal_decl_array_of_functions)
John McCallac406052009-10-30 00:37:20 +0000571 << getPrintableNameForEntity(Entity) << T;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000572 return QualType();
573 }
Mike Stump1eb44332009-09-09 15:08:12 +0000574
Douglas Gregorcd281c32009-02-28 00:25:32 +0000575 // C++ 8.3.2p4: There shall be no ... arrays of references ...
576 if (T->isReferenceType()) {
577 Diag(Loc, diag::err_illegal_decl_array_of_references)
John McCallac406052009-10-30 00:37:20 +0000578 << getPrintableNameForEntity(Entity) << T;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000579 return QualType();
Mike Stump1eb44332009-09-09 15:08:12 +0000580 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000581
Anders Carlssone7cf07d2009-06-26 19:33:28 +0000582 if (Context.getCanonicalType(T) == Context.UndeducedAutoTy) {
Mike Stump1eb44332009-09-09 15:08:12 +0000583 Diag(Loc, diag::err_illegal_decl_array_of_auto)
Anders Carlssone7cf07d2009-06-26 19:33:28 +0000584 << getPrintableNameForEntity(Entity);
585 return QualType();
586 }
Mike Stump1eb44332009-09-09 15:08:12 +0000587
Ted Kremenek6217b802009-07-29 21:53:49 +0000588 if (const RecordType *EltTy = T->getAs<RecordType>()) {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000589 // If the element type is a struct or union that contains a variadic
590 // array, accept it as a GNU extension: C99 6.7.2.1p2.
591 if (EltTy->getDecl()->hasFlexibleArrayMember())
592 Diag(Loc, diag::ext_flexible_array_in_array) << T;
593 } else if (T->isObjCInterfaceType()) {
Chris Lattnerc7c11b12009-04-27 01:55:56 +0000594 Diag(Loc, diag::err_objc_array_of_interfaces) << T;
595 return QualType();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000596 }
Mike Stump1eb44332009-09-09 15:08:12 +0000597
Douglas Gregorcd281c32009-02-28 00:25:32 +0000598 // C99 6.7.5.2p1: The size expression shall have integer type.
599 if (ArraySize && !ArraySize->isTypeDependent() &&
600 !ArraySize->getType()->isIntegerType()) {
601 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
602 << ArraySize->getType() << ArraySize->getSourceRange();
603 ArraySize->Destroy(Context);
604 return QualType();
605 }
606 llvm::APSInt ConstVal(32);
607 if (!ArraySize) {
Eli Friedmanf91f5c82009-04-26 21:57:51 +0000608 if (ASM == ArrayType::Star)
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000609 T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets);
Eli Friedmanf91f5c82009-04-26 21:57:51 +0000610 else
611 T = Context.getIncompleteArrayType(T, ASM, Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000612 } else if (ArraySize->isValueDependent()) {
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000613 T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000614 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
615 (!T->isDependentType() && !T->isConstantSizeType())) {
616 // Per C99, a variable array is an array with either a non-constant
617 // size or an element type that has a non-constant-size
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000618 T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000619 } else {
620 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
621 // have a value greater than zero.
622 if (ConstVal.isSigned()) {
623 if (ConstVal.isNegative()) {
624 Diag(ArraySize->getLocStart(),
625 diag::err_typecheck_negative_array_size)
626 << ArraySize->getSourceRange();
627 return QualType();
628 } else if (ConstVal == 0) {
629 // GCC accepts zero sized static arrays.
630 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size)
631 << ArraySize->getSourceRange();
632 }
Mike Stump1eb44332009-09-09 15:08:12 +0000633 }
John McCall46a617a2009-10-16 00:14:28 +0000634 T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000635 }
636 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
637 if (!getLangOptions().C99) {
Mike Stump1eb44332009-09-09 15:08:12 +0000638 if (ArraySize && !ArraySize->isTypeDependent() &&
639 !ArraySize->isValueDependent() &&
Douglas Gregorcd281c32009-02-28 00:25:32 +0000640 !ArraySize->isIntegerConstantExpr(Context))
Douglas Gregor043cad22009-09-11 00:18:58 +0000641 Diag(Loc, getLangOptions().CPlusPlus? diag::err_vla_cxx : diag::ext_vla);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000642 else if (ASM != ArrayType::Normal || Quals != 0)
Douglas Gregor043cad22009-09-11 00:18:58 +0000643 Diag(Loc,
644 getLangOptions().CPlusPlus? diag::err_c99_array_usage_cxx
645 : diag::ext_c99_array_usage);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000646 }
647
648 return T;
649}
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000650
651/// \brief Build an ext-vector type.
652///
653/// Run the required checks for the extended vector type.
Mike Stump1eb44332009-09-09 15:08:12 +0000654QualType Sema::BuildExtVectorType(QualType T, ExprArg ArraySize,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000655 SourceLocation AttrLoc) {
656
657 Expr *Arg = (Expr *)ArraySize.get();
658
659 // unlike gcc's vector_size attribute, we do not allow vectors to be defined
660 // in conjunction with complex types (pointers, arrays, functions, etc.).
Mike Stump1eb44332009-09-09 15:08:12 +0000661 if (!T->isDependentType() &&
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000662 !T->isIntegerType() && !T->isRealFloatingType()) {
663 Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T;
664 return QualType();
665 }
666
667 if (!Arg->isTypeDependent() && !Arg->isValueDependent()) {
668 llvm::APSInt vecSize(32);
669 if (!Arg->isIntegerConstantExpr(vecSize, Context)) {
670 Diag(AttrLoc, diag::err_attribute_argument_not_int)
671 << "ext_vector_type" << Arg->getSourceRange();
672 return QualType();
673 }
Mike Stump1eb44332009-09-09 15:08:12 +0000674
675 // unlike gcc's vector_size attribute, the size is specified as the
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000676 // number of elements, not the number of bytes.
Mike Stump1eb44332009-09-09 15:08:12 +0000677 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
678
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000679 if (vectorSize == 0) {
680 Diag(AttrLoc, diag::err_attribute_zero_size)
681 << Arg->getSourceRange();
682 return QualType();
683 }
Mike Stump1eb44332009-09-09 15:08:12 +0000684
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000685 if (!T->isDependentType())
686 return Context.getExtVectorType(T, vectorSize);
Mike Stump1eb44332009-09-09 15:08:12 +0000687 }
688
689 return Context.getDependentSizedExtVectorType(T, ArraySize.takeAs<Expr>(),
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000690 AttrLoc);
691}
Mike Stump1eb44332009-09-09 15:08:12 +0000692
Douglas Gregor724651c2009-02-28 01:04:19 +0000693/// \brief Build a function type.
694///
695/// This routine checks the function type according to C++ rules and
696/// under the assumption that the result type and parameter types have
697/// just been instantiated from a template. It therefore duplicates
Douglas Gregor2943aed2009-03-03 04:44:36 +0000698/// some of the behavior of GetTypeForDeclarator, but in a much
Douglas Gregor724651c2009-02-28 01:04:19 +0000699/// simpler form that is only suitable for this narrow use case.
700///
701/// \param T The return type of the function.
702///
703/// \param ParamTypes The parameter types of the function. This array
704/// will be modified to account for adjustments to the types of the
705/// function parameters.
706///
707/// \param NumParamTypes The number of parameter types in ParamTypes.
708///
709/// \param Variadic Whether this is a variadic function type.
710///
711/// \param Quals The cvr-qualifiers to be applied to the function type.
712///
713/// \param Loc The location of the entity whose type involves this
714/// function type or, if there is no such entity, the location of the
715/// type that will have function type.
716///
717/// \param Entity The name of the entity that involves the function
718/// type, if known.
719///
720/// \returns A suitable function type, if there are no
721/// errors. Otherwise, returns a NULL type.
722QualType Sema::BuildFunctionType(QualType T,
Mike Stump1eb44332009-09-09 15:08:12 +0000723 QualType *ParamTypes,
Douglas Gregor724651c2009-02-28 01:04:19 +0000724 unsigned NumParamTypes,
725 bool Variadic, unsigned Quals,
726 SourceLocation Loc, DeclarationName Entity) {
727 if (T->isArrayType() || T->isFunctionType()) {
728 Diag(Loc, diag::err_func_returning_array_function) << T;
729 return QualType();
730 }
Mike Stump1eb44332009-09-09 15:08:12 +0000731
Douglas Gregor724651c2009-02-28 01:04:19 +0000732 bool Invalid = false;
733 for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) {
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000734 QualType ParamType = adjustParameterType(ParamTypes[Idx]);
735 if (ParamType->isVoidType()) {
Douglas Gregor724651c2009-02-28 01:04:19 +0000736 Diag(Loc, diag::err_param_with_void_type);
737 Invalid = true;
738 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000739
John McCall54e14c42009-10-22 22:37:11 +0000740 ParamTypes[Idx] = ParamType;
Douglas Gregor724651c2009-02-28 01:04:19 +0000741 }
742
743 if (Invalid)
744 return QualType();
745
Mike Stump1eb44332009-09-09 15:08:12 +0000746 return Context.getFunctionType(T, ParamTypes, NumParamTypes, Variadic,
Douglas Gregor724651c2009-02-28 01:04:19 +0000747 Quals);
748}
Mike Stump1eb44332009-09-09 15:08:12 +0000749
Douglas Gregor949bf692009-06-09 22:17:39 +0000750/// \brief Build a member pointer type \c T Class::*.
751///
752/// \param T the type to which the member pointer refers.
753/// \param Class the class type into which the member pointer points.
John McCall0953e762009-09-24 19:53:00 +0000754/// \param CVR Qualifiers applied to the member pointer type
Douglas Gregor949bf692009-06-09 22:17:39 +0000755/// \param Loc the location where this type begins
756/// \param Entity the name of the entity that will have this member pointer type
757///
758/// \returns a member pointer type, if successful, or a NULL type if there was
759/// an error.
Mike Stump1eb44332009-09-09 15:08:12 +0000760QualType Sema::BuildMemberPointerType(QualType T, QualType Class,
John McCall0953e762009-09-24 19:53:00 +0000761 unsigned CVR, SourceLocation Loc,
Douglas Gregor949bf692009-06-09 22:17:39 +0000762 DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000763 Qualifiers Quals = Qualifiers::fromCVRMask(CVR);
764
Douglas Gregor949bf692009-06-09 22:17:39 +0000765 // Verify that we're not building a pointer to pointer to function with
766 // exception specification.
767 if (CheckDistantExceptionSpec(T)) {
768 Diag(Loc, diag::err_distant_exception_spec);
769
770 // FIXME: If we're doing this as part of template instantiation,
771 // we should return immediately.
772
773 // Build the type anyway, but use the canonical type so that the
774 // exception specifiers are stripped off.
775 T = Context.getCanonicalType(T);
776 }
777
778 // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member
779 // with reference type, or "cv void."
780 if (T->isReferenceType()) {
Anders Carlsson8d4655d2009-06-30 00:06:57 +0000781 Diag(Loc, diag::err_illegal_decl_mempointer_to_reference)
John McCallac406052009-10-30 00:37:20 +0000782 << (Entity? Entity.getAsString() : "type name") << T;
Douglas Gregor949bf692009-06-09 22:17:39 +0000783 return QualType();
784 }
785
786 if (T->isVoidType()) {
787 Diag(Loc, diag::err_illegal_decl_mempointer_to_void)
788 << (Entity? Entity.getAsString() : "type name");
789 return QualType();
790 }
791
792 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
793 // object or incomplete types shall not be restrict-qualified."
John McCall0953e762009-09-24 19:53:00 +0000794 if (Quals.hasRestrict() && !T->isIncompleteOrObjectType()) {
Douglas Gregor949bf692009-06-09 22:17:39 +0000795 Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
796 << T;
797
798 // FIXME: If we're doing this as part of template instantiation,
799 // we should return immediately.
John McCall0953e762009-09-24 19:53:00 +0000800 Quals.removeRestrict();
Douglas Gregor949bf692009-06-09 22:17:39 +0000801 }
802
803 if (!Class->isDependentType() && !Class->isRecordType()) {
804 Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class;
805 return QualType();
806 }
807
John McCall0953e762009-09-24 19:53:00 +0000808 return Context.getQualifiedType(
809 Context.getMemberPointerType(T, Class.getTypePtr()), Quals);
Douglas Gregor949bf692009-06-09 22:17:39 +0000810}
Mike Stump1eb44332009-09-09 15:08:12 +0000811
Anders Carlsson9a917e42009-06-12 22:56:54 +0000812/// \brief Build a block pointer type.
813///
814/// \param T The type to which we'll be building a block pointer.
815///
John McCall0953e762009-09-24 19:53:00 +0000816/// \param CVR The cvr-qualifiers to be applied to the block pointer type.
Anders Carlsson9a917e42009-06-12 22:56:54 +0000817///
818/// \param Loc The location of the entity whose type involves this
819/// block pointer type or, if there is no such entity, the location of the
820/// type that will have block pointer type.
821///
822/// \param Entity The name of the entity that involves the block pointer
823/// type, if known.
824///
825/// \returns A suitable block pointer type, if there are no
826/// errors. Otherwise, returns a NULL type.
John McCall0953e762009-09-24 19:53:00 +0000827QualType Sema::BuildBlockPointerType(QualType T, unsigned CVR,
Mike Stump1eb44332009-09-09 15:08:12 +0000828 SourceLocation Loc,
Anders Carlsson9a917e42009-06-12 22:56:54 +0000829 DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000830 if (!T->isFunctionType()) {
Anders Carlsson9a917e42009-06-12 22:56:54 +0000831 Diag(Loc, diag::err_nonfunction_block_type);
832 return QualType();
833 }
Mike Stump1eb44332009-09-09 15:08:12 +0000834
John McCall0953e762009-09-24 19:53:00 +0000835 Qualifiers Quals = Qualifiers::fromCVRMask(CVR);
836 return Context.getQualifiedType(Context.getBlockPointerType(T), Quals);
Anders Carlsson9a917e42009-06-12 22:56:54 +0000837}
838
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000839QualType Sema::GetTypeFromParser(TypeTy *Ty, DeclaratorInfo **DInfo) {
840 QualType QT = QualType::getFromOpaquePtr(Ty);
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000841 if (QT.isNull()) {
842 if (DInfo) *DInfo = 0;
843 return QualType();
844 }
845
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000846 DeclaratorInfo *DI = 0;
847 if (LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) {
848 QT = LIT->getType();
849 DI = LIT->getDeclaratorInfo();
850 }
Mike Stump1eb44332009-09-09 15:08:12 +0000851
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000852 if (DInfo) *DInfo = DI;
853 return QT;
854}
855
Mike Stump98eb8a72009-02-04 22:31:32 +0000856/// GetTypeForDeclarator - Convert the type for the specified
Sebastian Redl8ce35b02009-10-25 21:45:37 +0000857/// declarator to Type instances.
Douglas Gregor402abb52009-05-28 23:31:59 +0000858///
859/// If OwnedDecl is non-NULL, and this declarator's decl-specifier-seq
860/// owns the declaration of a type (e.g., the definition of a struct
861/// type), then *OwnedDecl will receive the owned declaration.
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +0000862QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S,
Sebastian Redl8ce35b02009-10-25 21:45:37 +0000863 DeclaratorInfo **DInfo,
Douglas Gregor402abb52009-05-28 23:31:59 +0000864 TagDecl **OwnedDecl) {
Douglas Gregor930d8b52009-01-30 22:09:00 +0000865 // Determine the type of the declarator. Not all forms of declarator
866 // have a type.
867 QualType T;
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000868
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000869 switch (D.getName().getKind()) {
870 case UnqualifiedId::IK_Identifier:
871 case UnqualifiedId::IK_OperatorFunctionId:
872 case UnqualifiedId::IK_TemplateId:
Sebastian Redl8ce35b02009-10-25 21:45:37 +0000873 T = ConvertDeclSpecToType(D, *this);
Chris Lattner5db2bb12009-10-25 18:21:37 +0000874
875 if (!D.isInvalidType() && OwnedDecl && D.getDeclSpec().isTypeSpecOwned())
876 *OwnedDecl = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep());
Douglas Gregor930d8b52009-01-30 22:09:00 +0000877 break;
878
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000879 case UnqualifiedId::IK_ConstructorName:
880 case UnqualifiedId::IK_DestructorName:
881 case UnqualifiedId::IK_ConversionFunctionId:
Douglas Gregor930d8b52009-01-30 22:09:00 +0000882 // Constructors and destructors don't have return types. Use
883 // "void" instead. Conversion operators will check their return
884 // types separately.
885 T = Context.VoidTy;
886 break;
887 }
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000888
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000889 if (T == Context.UndeducedAutoTy) {
890 int Error = -1;
Mike Stump1eb44332009-09-09 15:08:12 +0000891
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000892 switch (D.getContext()) {
893 case Declarator::KNRTypeListContext:
894 assert(0 && "K&R type lists aren't allowed in C++");
895 break;
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000896 case Declarator::PrototypeContext:
897 Error = 0; // Function prototype
898 break;
899 case Declarator::MemberContext:
900 switch (cast<TagDecl>(CurContext)->getTagKind()) {
901 case TagDecl::TK_enum: assert(0 && "unhandled tag kind"); break;
902 case TagDecl::TK_struct: Error = 1; /* Struct member */ break;
903 case TagDecl::TK_union: Error = 2; /* Union member */ break;
904 case TagDecl::TK_class: Error = 3; /* Class member */ break;
Mike Stump1eb44332009-09-09 15:08:12 +0000905 }
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000906 break;
907 case Declarator::CXXCatchContext:
908 Error = 4; // Exception declaration
909 break;
910 case Declarator::TemplateParamContext:
911 Error = 5; // Template parameter
912 break;
913 case Declarator::BlockLiteralContext:
914 Error = 6; // Block literal
915 break;
916 case Declarator::FileContext:
917 case Declarator::BlockContext:
918 case Declarator::ForContext:
919 case Declarator::ConditionContext:
920 case Declarator::TypeNameContext:
921 break;
922 }
923
924 if (Error != -1) {
925 Diag(D.getDeclSpec().getTypeSpecTypeLoc(), diag::err_auto_not_allowed)
926 << Error;
927 T = Context.IntTy;
928 D.setInvalidType(true);
929 }
930 }
Mike Stump1eb44332009-09-09 15:08:12 +0000931
Douglas Gregorcd281c32009-02-28 00:25:32 +0000932 // The name we're declaring, if any.
933 DeclarationName Name;
934 if (D.getIdentifier())
935 Name = D.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +0000936
Mike Stump98eb8a72009-02-04 22:31:32 +0000937 // Walk the DeclTypeInfo, building the recursive type as we go.
938 // DeclTypeInfos are ordered from the identifier out, which is
939 // opposite of what we want :).
Sebastian Redl8ce35b02009-10-25 21:45:37 +0000940 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
941 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000942 switch (DeclType.Kind) {
943 default: assert(0 && "Unknown decltype!");
Steve Naroff5618bd42008-08-27 16:04:49 +0000944 case DeclaratorChunk::BlockPointer:
Chris Lattner9af55002009-03-27 04:18:06 +0000945 // If blocks are disabled, emit an error.
946 if (!LangOpts.Blocks)
947 Diag(DeclType.Loc, diag::err_blocks_disable);
Mike Stump1eb44332009-09-09 15:08:12 +0000948
949 T = BuildBlockPointerType(T, DeclType.Cls.TypeQuals, D.getIdentifierLoc(),
Anders Carlsson9a917e42009-06-12 22:56:54 +0000950 Name);
Steve Naroff5618bd42008-08-27 16:04:49 +0000951 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000952 case DeclaratorChunk::Pointer:
Sebastian Redl6a7330c2009-05-29 15:01:05 +0000953 // Verify that we're not building a pointer to pointer to function with
954 // exception specification.
955 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
956 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
957 D.setInvalidType(true);
958 // Build the type anyway.
959 }
Steve Naroff14108da2009-07-10 23:34:53 +0000960 if (getLangOptions().ObjC1 && T->isObjCInterfaceType()) {
John McCall183700f2009-09-21 23:43:11 +0000961 const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>();
Steve Naroff14108da2009-07-10 23:34:53 +0000962 T = Context.getObjCObjectPointerType(T,
Steve Naroff67ef8ea2009-07-20 17:56:53 +0000963 (ObjCProtocolDecl **)OIT->qual_begin(),
964 OIT->getNumProtocols());
Steve Naroff14108da2009-07-10 23:34:53 +0000965 break;
966 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000967 T = BuildPointerType(T, DeclType.Ptr.TypeQuals, DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000968 break;
John McCall0953e762009-09-24 19:53:00 +0000969 case DeclaratorChunk::Reference: {
970 Qualifiers Quals;
971 if (DeclType.Ref.HasRestrict) Quals.addRestrict();
972
Sebastian Redl6a7330c2009-05-29 15:01:05 +0000973 // Verify that we're not building a reference to pointer to function with
974 // exception specification.
975 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
976 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
977 D.setInvalidType(true);
978 // Build the type anyway.
979 }
John McCall0953e762009-09-24 19:53:00 +0000980 T = BuildReferenceType(T, DeclType.Ref.LValueRef, Quals,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000981 DeclType.Loc, Name);
Reid Spencer5f016e22007-07-11 17:01:13 +0000982 break;
John McCall0953e762009-09-24 19:53:00 +0000983 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000984 case DeclaratorChunk::Array: {
Sebastian Redl6a7330c2009-05-29 15:01:05 +0000985 // Verify that we're not building an array of pointers to function with
986 // exception specification.
987 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
988 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
989 D.setInvalidType(true);
990 // Build the type anyway.
991 }
Chris Lattnerfd89bc82008-04-02 01:05:10 +0000992 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner94f81fd2007-08-28 16:54:00 +0000993 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000994 ArrayType::ArraySizeModifier ASM;
995 if (ATI.isStar)
996 ASM = ArrayType::Star;
997 else if (ATI.hasStatic)
998 ASM = ArrayType::Static;
999 else
1000 ASM = ArrayType::Normal;
Eli Friedmanf91f5c82009-04-26 21:57:51 +00001001 if (ASM == ArrayType::Star &&
1002 D.getContext() != Declarator::PrototypeContext) {
1003 // FIXME: This check isn't quite right: it allows star in prototypes
1004 // for function definitions, and disallows some edge cases detailed
1005 // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html
1006 Diag(DeclType.Loc, diag::err_array_star_outside_prototype);
1007 ASM = ArrayType::Normal;
1008 D.setInvalidType(true);
1009 }
John McCall0953e762009-09-24 19:53:00 +00001010 T = BuildArrayType(T, ASM, ArraySize,
1011 Qualifiers::fromCVRMask(ATI.TypeQuals),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001012 SourceRange(DeclType.Loc, DeclType.EndLoc), Name);
Reid Spencer5f016e22007-07-11 17:01:13 +00001013 break;
1014 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001015 case DeclaratorChunk::Function: {
Reid Spencer5f016e22007-07-11 17:01:13 +00001016 // If the function declarator has a prototype (i.e. it is not () and
1017 // does not have a K&R-style identifier list), then the arguments are part
1018 // of the type, otherwise the argument list is ().
1019 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Sebastian Redl3cc97262009-05-31 11:47:27 +00001020
Chris Lattnercd881292007-12-19 05:31:29 +00001021 // C99 6.7.5.3p1: The return type may not be a function or array type.
Chris Lattner68cfd492007-12-19 18:01:43 +00001022 if (T->isArrayType() || T->isFunctionType()) {
Chris Lattnerd1625842008-11-24 06:25:27 +00001023 Diag(DeclType.Loc, diag::err_func_returning_array_function) << T;
Chris Lattnercd881292007-12-19 05:31:29 +00001024 T = Context.IntTy;
1025 D.setInvalidType(true);
1026 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001027
Douglas Gregor402abb52009-05-28 23:31:59 +00001028 if (getLangOptions().CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) {
1029 // C++ [dcl.fct]p6:
1030 // Types shall not be defined in return or parameter types.
1031 TagDecl *Tag = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep());
1032 if (Tag->isDefinition())
1033 Diag(Tag->getLocation(), diag::err_type_defined_in_result_type)
1034 << Context.getTypeDeclType(Tag);
1035 }
1036
Sebastian Redl3cc97262009-05-31 11:47:27 +00001037 // Exception specs are not allowed in typedefs. Complain, but add it
1038 // anyway.
1039 if (FTI.hasExceptionSpec &&
1040 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
1041 Diag(FTI.getThrowLoc(), diag::err_exception_spec_in_typedef);
1042
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001043 if (FTI.NumArgs == 0) {
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001044 if (getLangOptions().CPlusPlus) {
1045 // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the
1046 // function takes no arguments.
Sebastian Redl465226e2009-05-27 22:11:52 +00001047 llvm::SmallVector<QualType, 4> Exceptions;
1048 Exceptions.reserve(FTI.NumExceptions);
Mike Stump1eb44332009-09-09 15:08:12 +00001049 for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001050 // FIXME: Preserve type source info.
1051 QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty);
Sebastian Redlef65f062009-05-29 18:02:33 +00001052 // Check that the type is valid for an exception spec, and drop it
1053 // if not.
1054 if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1055 Exceptions.push_back(ET);
1056 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001057 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, FTI.TypeQuals,
1058 FTI.hasExceptionSpec,
1059 FTI.hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00001060 Exceptions.size(), Exceptions.data());
Douglas Gregor965acbb2009-02-18 07:07:28 +00001061 } else if (FTI.isVariadic) {
1062 // We allow a zero-parameter variadic function in C if the
1063 // function is marked with the "overloadable"
1064 // attribute. Scan for this attribute now.
1065 bool Overloadable = false;
1066 for (const AttributeList *Attrs = D.getAttributes();
1067 Attrs; Attrs = Attrs->getNext()) {
1068 if (Attrs->getKind() == AttributeList::AT_overloadable) {
1069 Overloadable = true;
1070 break;
1071 }
1072 }
1073
1074 if (!Overloadable)
1075 Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
1076 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, 0);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001077 } else {
1078 // Simple void foo(), where the incoming T is the result type.
Douglas Gregor72564e72009-02-26 23:50:07 +00001079 T = Context.getFunctionNoProtoType(T);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001080 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001081 } else if (FTI.ArgInfo[0].Param == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001082 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
Mike Stump1eb44332009-09-09 15:08:12 +00001083 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
John McCall54e14c42009-10-22 22:37:11 +00001084 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001085 } else {
1086 // Otherwise, we have a function with an argument list that is
1087 // potentially variadic.
1088 llvm::SmallVector<QualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +00001089
Reid Spencer5f016e22007-07-11 17:01:13 +00001090 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001091 ParmVarDecl *Param =
1092 cast<ParmVarDecl>(FTI.ArgInfo[i].Param.getAs<Decl>());
Chris Lattner8123a952008-04-10 02:22:51 +00001093 QualType ArgTy = Param->getType();
Chris Lattner78c75fb2007-07-21 05:30:18 +00001094 assert(!ArgTy.isNull() && "Couldn't parse type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001095
1096 // Adjust the parameter type.
Douglas Gregorbeb58cb2009-03-23 23:17:00 +00001097 assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001098
Reid Spencer5f016e22007-07-11 17:01:13 +00001099 // Look for 'void'. void is allowed only as a single argument to a
1100 // function with no other parameters (C99 6.7.5.3p10). We record
Douglas Gregor72564e72009-02-26 23:50:07 +00001101 // int(void) as a FunctionProtoType with an empty argument list.
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001102 if (ArgTy->isVoidType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001103 // If this is something like 'float(int, void)', reject it. 'void'
1104 // is an incomplete type (C99 6.2.5p19) and function decls cannot
1105 // have arguments of incomplete type.
1106 if (FTI.NumArgs != 1 || FTI.isVariadic) {
1107 Diag(DeclType.Loc, diag::err_void_only_param);
Chris Lattner2ff54262007-07-21 05:18:12 +00001108 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00001109 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00001110 } else if (FTI.ArgInfo[i].Ident) {
1111 // Reject, but continue to parse 'int(void abc)'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001112 Diag(FTI.ArgInfo[i].IdentLoc,
Chris Lattner4565d4e2007-07-21 05:26:43 +00001113 diag::err_param_with_void_type);
Chris Lattner2ff54262007-07-21 05:18:12 +00001114 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00001115 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00001116 } else {
1117 // Reject, but continue to parse 'float(const void)'.
John McCall0953e762009-09-24 19:53:00 +00001118 if (ArgTy.hasQualifiers())
Chris Lattner2ff54262007-07-21 05:18:12 +00001119 Diag(DeclType.Loc, diag::err_void_param_qualified);
Mike Stump1eb44332009-09-09 15:08:12 +00001120
Chris Lattner2ff54262007-07-21 05:18:12 +00001121 // Do not add 'void' to the ArgTys list.
1122 break;
1123 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001124 } else if (!FTI.hasPrototype) {
1125 if (ArgTy->isPromotableIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00001126 ArgTy = Context.getPromotedIntegerType(ArgTy);
John McCall183700f2009-09-21 23:43:11 +00001127 } else if (const BuiltinType* BTy = ArgTy->getAs<BuiltinType>()) {
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001128 if (BTy->getKind() == BuiltinType::Float)
1129 ArgTy = Context.DoubleTy;
1130 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001131 }
Mike Stump1eb44332009-09-09 15:08:12 +00001132
John McCall54e14c42009-10-22 22:37:11 +00001133 ArgTys.push_back(ArgTy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001134 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001135
1136 llvm::SmallVector<QualType, 4> Exceptions;
1137 Exceptions.reserve(FTI.NumExceptions);
Mike Stump1eb44332009-09-09 15:08:12 +00001138 for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001139 // FIXME: Preserve type source info.
1140 QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty);
Sebastian Redlef65f062009-05-29 18:02:33 +00001141 // Check that the type is valid for an exception spec, and drop it if
1142 // not.
1143 if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1144 Exceptions.push_back(ET);
1145 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001146
Jay Foadbeaaccd2009-05-21 09:52:38 +00001147 T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(),
Sebastian Redl465226e2009-05-27 22:11:52 +00001148 FTI.isVariadic, FTI.TypeQuals,
1149 FTI.hasExceptionSpec,
1150 FTI.hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00001151 Exceptions.size(), Exceptions.data());
Reid Spencer5f016e22007-07-11 17:01:13 +00001152 }
1153 break;
1154 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001155 case DeclaratorChunk::MemberPointer:
Sebastian Redl4994d2d2009-07-04 11:39:00 +00001156 // Verify that we're not building a pointer to pointer to function with
1157 // exception specification.
1158 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1159 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1160 D.setInvalidType(true);
1161 // Build the type anyway.
1162 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001163 // The scope spec must refer to a class, or be dependent.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001164 QualType ClsType;
Douglas Gregor949bf692009-06-09 22:17:39 +00001165 if (isDependentScopeSpecifier(DeclType.Mem.Scope())) {
Mike Stump1eb44332009-09-09 15:08:12 +00001166 NestedNameSpecifier *NNS
Douglas Gregor949bf692009-06-09 22:17:39 +00001167 = (NestedNameSpecifier *)DeclType.Mem.Scope().getScopeRep();
1168 assert(NNS->getAsType() && "Nested-name-specifier must name a type");
1169 ClsType = QualType(NNS->getAsType(), 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001170 } else if (CXXRecordDecl *RD
Douglas Gregor949bf692009-06-09 22:17:39 +00001171 = dyn_cast_or_null<CXXRecordDecl>(
1172 computeDeclContext(DeclType.Mem.Scope()))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00001173 ClsType = Context.getTagDeclType(RD);
1174 } else {
Douglas Gregor949bf692009-06-09 22:17:39 +00001175 Diag(DeclType.Mem.Scope().getBeginLoc(),
1176 diag::err_illegal_decl_mempointer_in_nonclass)
1177 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
1178 << DeclType.Mem.Scope().getRange();
Sebastian Redlf30208a2009-01-24 21:16:55 +00001179 D.setInvalidType(true);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001180 }
1181
Douglas Gregor949bf692009-06-09 22:17:39 +00001182 if (!ClsType.isNull())
1183 T = BuildMemberPointerType(T, ClsType, DeclType.Mem.TypeQuals,
1184 DeclType.Loc, D.getIdentifier());
1185 if (T.isNull()) {
1186 T = Context.IntTy;
Sebastian Redlf30208a2009-01-24 21:16:55 +00001187 D.setInvalidType(true);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001188 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001189 break;
1190 }
1191
Douglas Gregorcd281c32009-02-28 00:25:32 +00001192 if (T.isNull()) {
1193 D.setInvalidType(true);
1194 T = Context.IntTy;
1195 }
1196
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001197 // See if there are any attributes on this declarator chunk.
1198 if (const AttributeList *AL = DeclType.getAttrs())
1199 ProcessTypeAttributeList(T, AL);
Reid Spencer5f016e22007-07-11 17:01:13 +00001200 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001201
1202 if (getLangOptions().CPlusPlus && T->isFunctionType()) {
John McCall183700f2009-09-21 23:43:11 +00001203 const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>();
Chris Lattner778ed742009-10-25 17:36:50 +00001204 assert(FnTy && "Why oh why is there not a FunctionProtoType here?");
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001205
1206 // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type
1207 // for a nonstatic member function, the function type to which a pointer
1208 // to member refers, or the top-level function type of a function typedef
1209 // declaration.
1210 if (FnTy->getTypeQuals() != 0 &&
1211 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
Douglas Gregor584049d2008-12-15 23:53:10 +00001212 ((D.getContext() != Declarator::MemberContext &&
1213 (!D.getCXXScopeSpec().isSet() ||
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001214 !computeDeclContext(D.getCXXScopeSpec(), /*FIXME:*/true)
1215 ->isRecord())) ||
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001216 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001217 if (D.isFunctionDeclarator())
1218 Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
1219 else
1220 Diag(D.getIdentifierLoc(),
1221 diag::err_invalid_qualified_typedef_function_type_use);
1222
1223 // Strip the cv-quals from the type.
1224 T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001225 FnTy->getNumArgs(), FnTy->isVariadic(), 0);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001226 }
1227 }
Mike Stump1eb44332009-09-09 15:08:12 +00001228
Chris Lattner0bf29ad2008-06-29 00:19:33 +00001229 // If there were any type attributes applied to the decl itself (not the
1230 // type, apply the type attribute to the type!)
1231 if (const AttributeList *Attrs = D.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001232 ProcessTypeAttributeList(T, Attrs);
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001233
John McCall54e14c42009-10-22 22:37:11 +00001234 if (DInfo) {
1235 if (D.isInvalidType())
1236 *DInfo = 0;
1237 else
Sebastian Redl8ce35b02009-10-25 21:45:37 +00001238 *DInfo = GetDeclaratorInfoForDeclarator(D, T);
John McCall54e14c42009-10-22 22:37:11 +00001239 }
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001240
Reid Spencer5f016e22007-07-11 17:01:13 +00001241 return T;
1242}
1243
John McCall51bd8032009-10-18 01:05:36 +00001244namespace {
1245 class TypeSpecLocFiller : public TypeLocVisitor<TypeSpecLocFiller> {
1246 const DeclSpec &DS;
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001247
John McCall51bd8032009-10-18 01:05:36 +00001248 public:
1249 TypeSpecLocFiller(const DeclSpec &DS) : DS(DS) {}
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001250
John McCall51bd8032009-10-18 01:05:36 +00001251 void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
1252 Visit(TL.getUnqualifiedLoc());
1253 }
1254 void VisitTypedefTypeLoc(TypedefTypeLoc TL) {
1255 TL.setNameLoc(DS.getTypeSpecTypeLoc());
1256 }
1257 void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
1258 TL.setNameLoc(DS.getTypeSpecTypeLoc());
Argyrios Kyrtzidiseb667592009-09-29 19:45:22 +00001259
John McCall54e14c42009-10-22 22:37:11 +00001260 if (DS.getProtocolQualifiers()) {
1261 assert(TL.getNumProtocols() > 0);
1262 assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
1263 TL.setLAngleLoc(DS.getProtocolLAngleLoc());
1264 TL.setRAngleLoc(DS.getSourceRange().getEnd());
1265 for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i)
1266 TL.setProtocolLoc(i, DS.getProtocolLocs()[i]);
1267 } else {
1268 assert(TL.getNumProtocols() == 0);
1269 TL.setLAngleLoc(SourceLocation());
1270 TL.setRAngleLoc(SourceLocation());
1271 }
1272 }
1273 void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
1274 assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
1275
1276 TL.setStarLoc(SourceLocation());
1277
1278 if (DS.getProtocolQualifiers()) {
1279 assert(TL.getNumProtocols() > 0);
1280 assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
1281 TL.setHasProtocolsAsWritten(true);
1282 TL.setLAngleLoc(DS.getProtocolLAngleLoc());
1283 TL.setRAngleLoc(DS.getSourceRange().getEnd());
1284 for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i)
1285 TL.setProtocolLoc(i, DS.getProtocolLocs()[i]);
1286
1287 } else {
1288 assert(TL.getNumProtocols() == 0);
1289 TL.setHasProtocolsAsWritten(false);
1290 TL.setLAngleLoc(SourceLocation());
1291 TL.setRAngleLoc(SourceLocation());
1292 }
1293
1294 // This might not have been written with an inner type.
1295 if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) {
1296 TL.setHasBaseTypeAsWritten(false);
1297 TL.getBaseTypeLoc().initialize(SourceLocation());
1298 } else {
1299 TL.setHasBaseTypeAsWritten(true);
John McCall51bd8032009-10-18 01:05:36 +00001300 Visit(TL.getBaseTypeLoc());
John McCall54e14c42009-10-22 22:37:11 +00001301 }
John McCall51bd8032009-10-18 01:05:36 +00001302 }
John McCall833ca992009-10-29 08:12:44 +00001303 void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) {
1304 DeclaratorInfo *DInfo = 0;
1305 Sema::GetTypeFromParser(DS.getTypeRep(), &DInfo);
1306
1307 // If we got no declarator info from previous Sema routines,
1308 // just fill with the typespec loc.
1309 if (!DInfo) {
1310 TL.initialize(DS.getTypeSpecTypeLoc());
1311 return;
1312 }
1313
1314 TemplateSpecializationTypeLoc OldTL =
1315 cast<TemplateSpecializationTypeLoc>(DInfo->getTypeLoc());
1316 TL.copy(OldTL);
1317 }
John McCall51bd8032009-10-18 01:05:36 +00001318 void VisitTypeLoc(TypeLoc TL) {
1319 // FIXME: add other typespec types and change this to an assert.
1320 TL.initialize(DS.getTypeSpecTypeLoc());
1321 }
1322 };
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001323
John McCall51bd8032009-10-18 01:05:36 +00001324 class DeclaratorLocFiller : public TypeLocVisitor<DeclaratorLocFiller> {
1325 const DeclaratorChunk &Chunk;
1326
1327 public:
1328 DeclaratorLocFiller(const DeclaratorChunk &Chunk) : Chunk(Chunk) {}
1329
1330 void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
1331 llvm::llvm_unreachable("qualified type locs not expected here!");
1332 }
1333
1334 void VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
1335 assert(Chunk.Kind == DeclaratorChunk::BlockPointer);
1336 TL.setCaretLoc(Chunk.Loc);
1337 }
1338 void VisitPointerTypeLoc(PointerTypeLoc TL) {
1339 assert(Chunk.Kind == DeclaratorChunk::Pointer);
1340 TL.setStarLoc(Chunk.Loc);
1341 }
1342 void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
1343 assert(Chunk.Kind == DeclaratorChunk::Pointer);
1344 TL.setStarLoc(Chunk.Loc);
John McCall54e14c42009-10-22 22:37:11 +00001345 TL.setHasBaseTypeAsWritten(true);
1346 TL.setHasProtocolsAsWritten(false);
1347 TL.setLAngleLoc(SourceLocation());
1348 TL.setRAngleLoc(SourceLocation());
John McCall51bd8032009-10-18 01:05:36 +00001349 }
1350 void VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
1351 assert(Chunk.Kind == DeclaratorChunk::MemberPointer);
1352 TL.setStarLoc(Chunk.Loc);
1353 // FIXME: nested name specifier
1354 }
1355 void VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
1356 assert(Chunk.Kind == DeclaratorChunk::Reference);
John McCall54e14c42009-10-22 22:37:11 +00001357 // 'Amp' is misleading: this might have been originally
1358 /// spelled with AmpAmp.
John McCall51bd8032009-10-18 01:05:36 +00001359 TL.setAmpLoc(Chunk.Loc);
1360 }
1361 void VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
1362 assert(Chunk.Kind == DeclaratorChunk::Reference);
1363 assert(!Chunk.Ref.LValueRef);
1364 TL.setAmpAmpLoc(Chunk.Loc);
1365 }
1366 void VisitArrayTypeLoc(ArrayTypeLoc TL) {
1367 assert(Chunk.Kind == DeclaratorChunk::Array);
1368 TL.setLBracketLoc(Chunk.Loc);
1369 TL.setRBracketLoc(Chunk.EndLoc);
1370 TL.setSizeExpr(static_cast<Expr*>(Chunk.Arr.NumElts));
1371 }
1372 void VisitFunctionTypeLoc(FunctionTypeLoc TL) {
1373 assert(Chunk.Kind == DeclaratorChunk::Function);
1374 TL.setLParenLoc(Chunk.Loc);
1375 TL.setRParenLoc(Chunk.EndLoc);
1376
1377 const DeclaratorChunk::FunctionTypeInfo &FTI = Chunk.Fun;
John McCall54e14c42009-10-22 22:37:11 +00001378 for (unsigned i = 0, e = TL.getNumArgs(), tpi = 0; i != e; ++i) {
John McCall51bd8032009-10-18 01:05:36 +00001379 ParmVarDecl *Param = FTI.ArgInfo[i].Param.getAs<ParmVarDecl>();
John McCall54e14c42009-10-22 22:37:11 +00001380 TL.setArg(tpi++, Param);
John McCall51bd8032009-10-18 01:05:36 +00001381 }
1382 // FIXME: exception specs
1383 }
1384
1385 void VisitTypeLoc(TypeLoc TL) {
1386 llvm::llvm_unreachable("unsupported TypeLoc kind in declarator!");
1387 }
1388 };
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001389}
1390
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001391/// \brief Create and instantiate a DeclaratorInfo with type source information.
1392///
1393/// \param T QualType referring to the type as written in source code.
1394DeclaratorInfo *
Sebastian Redl8ce35b02009-10-25 21:45:37 +00001395Sema::GetDeclaratorInfoForDeclarator(Declarator &D, QualType T) {
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001396 DeclaratorInfo *DInfo = Context.CreateDeclaratorInfo(T);
John McCall51bd8032009-10-18 01:05:36 +00001397 UnqualTypeLoc CurrTL = DInfo->getTypeLoc().getUnqualifiedLoc();
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001398
Sebastian Redl8ce35b02009-10-25 21:45:37 +00001399 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
John McCall51bd8032009-10-18 01:05:36 +00001400 DeclaratorLocFiller(D.getTypeObject(i)).Visit(CurrTL);
1401 CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001402 }
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001403
John McCall51bd8032009-10-18 01:05:36 +00001404 TypeSpecLocFiller(D.getDeclSpec()).Visit(CurrTL);
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001405
1406 return DInfo;
1407}
1408
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001409/// \brief Create a LocInfoType to hold the given QualType and DeclaratorInfo.
1410QualType Sema::CreateLocInfoType(QualType T, DeclaratorInfo *DInfo) {
1411 // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser
1412 // and Sema during declaration parsing. Try deallocating/caching them when
1413 // it's appropriate, instead of allocating them and keeping them around.
1414 LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType), 8);
1415 new (LocT) LocInfoType(T, DInfo);
1416 assert(LocT->getTypeClass() != T->getTypeClass() &&
1417 "LocInfoType's TypeClass conflicts with an existing Type class");
1418 return QualType(LocT, 0);
1419}
1420
1421void LocInfoType::getAsStringInternal(std::string &Str,
1422 const PrintingPolicy &Policy) const {
Argyrios Kyrtzidis35d44e52009-08-19 01:46:06 +00001423 assert(false && "LocInfoType leaked into the type system; an opaque TypeTy*"
1424 " was used directly instead of getting the QualType through"
1425 " GetTypeFromParser");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001426}
1427
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001428/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
Fariborz Jahanian360300c2007-11-09 22:27:59 +00001429/// declarator
Chris Lattnerb28317a2009-03-28 19:18:32 +00001430QualType Sema::ObjCGetTypeForMethodDefinition(DeclPtrTy D) {
1431 ObjCMethodDecl *MDecl = cast<ObjCMethodDecl>(D.getAs<Decl>());
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001432 QualType T = MDecl->getResultType();
1433 llvm::SmallVector<QualType, 16> ArgTys;
Mike Stump1eb44332009-09-09 15:08:12 +00001434
Fariborz Jahanian35600022007-11-09 17:18:29 +00001435 // Add the first two invisible argument types for self and _cmd.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001436 if (MDecl->isInstanceMethod()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001437 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +00001438 selfTy = Context.getPointerType(selfTy);
1439 ArgTys.push_back(selfTy);
Chris Lattner89951a82009-02-20 18:43:26 +00001440 } else
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001441 ArgTys.push_back(Context.getObjCIdType());
1442 ArgTys.push_back(Context.getObjCSelType());
Mike Stump1eb44332009-09-09 15:08:12 +00001443
Chris Lattner89951a82009-02-20 18:43:26 +00001444 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
1445 E = MDecl->param_end(); PI != E; ++PI) {
1446 QualType ArgTy = (*PI)->getType();
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001447 assert(!ArgTy.isNull() && "Couldn't parse type?");
Douglas Gregorbeb58cb2009-03-23 23:17:00 +00001448 ArgTy = adjustParameterType(ArgTy);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001449 ArgTys.push_back(ArgTy);
1450 }
1451 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001452 MDecl->isVariadic(), 0);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +00001453 return T;
1454}
1455
Sebastian Redl9e5e4aa2009-01-26 19:54:48 +00001456/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that
1457/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
1458/// they point to and return true. If T1 and T2 aren't pointer types
1459/// or pointer-to-member types, or if they are not similar at this
1460/// level, returns false and leaves T1 and T2 unchanged. Top-level
1461/// qualifiers on T1 and T2 are ignored. This function will typically
1462/// be called in a loop that successively "unwraps" pointer and
1463/// pointer-to-member types to compare them at each level.
Chris Lattnerecb81f22009-02-16 21:43:00 +00001464bool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) {
Ted Kremenek6217b802009-07-29 21:53:49 +00001465 const PointerType *T1PtrType = T1->getAs<PointerType>(),
1466 *T2PtrType = T2->getAs<PointerType>();
Douglas Gregor57373262008-10-22 14:17:15 +00001467 if (T1PtrType && T2PtrType) {
1468 T1 = T1PtrType->getPointeeType();
1469 T2 = T2PtrType->getPointeeType();
1470 return true;
1471 }
1472
Ted Kremenek6217b802009-07-29 21:53:49 +00001473 const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(),
1474 *T2MPType = T2->getAs<MemberPointerType>();
Sebastian Redl21593ac2009-01-28 18:33:18 +00001475 if (T1MPType && T2MPType &&
1476 Context.getCanonicalType(T1MPType->getClass()) ==
1477 Context.getCanonicalType(T2MPType->getClass())) {
Sebastian Redl4433aaf2009-01-25 19:43:20 +00001478 T1 = T1MPType->getPointeeType();
1479 T2 = T2MPType->getPointeeType();
1480 return true;
1481 }
Douglas Gregor57373262008-10-22 14:17:15 +00001482 return false;
1483}
1484
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001485Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001486 // C99 6.7.6: Type names have no identifier. This is already validated by
1487 // the parser.
1488 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00001489
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001490 DeclaratorInfo *DInfo = 0;
Douglas Gregor402abb52009-05-28 23:31:59 +00001491 TagDecl *OwnedTag = 0;
Sebastian Redl8ce35b02009-10-25 21:45:37 +00001492 QualType T = GetTypeForDeclarator(D, S, &DInfo, &OwnedTag);
Chris Lattner5153ee62009-04-25 08:47:54 +00001493 if (D.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +00001494 return true;
Steve Naroff5912a352007-08-28 20:14:24 +00001495
Douglas Gregor402abb52009-05-28 23:31:59 +00001496 if (getLangOptions().CPlusPlus) {
1497 // Check that there are no default arguments (C++ only).
Douglas Gregor6d6eb572008-05-07 04:49:29 +00001498 CheckExtraCXXDefaultArguments(D);
1499
Douglas Gregor402abb52009-05-28 23:31:59 +00001500 // C++0x [dcl.type]p3:
1501 // A type-specifier-seq shall not define a class or enumeration
1502 // unless it appears in the type-id of an alias-declaration
1503 // (7.1.3).
1504 if (OwnedTag && OwnedTag->isDefinition())
1505 Diag(OwnedTag->getLocation(), diag::err_type_defined_in_type_specifier)
1506 << Context.getTypeDeclType(OwnedTag);
1507 }
1508
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001509 if (DInfo)
1510 T = CreateLocInfoType(T, DInfo);
1511
Reid Spencer5f016e22007-07-11 17:01:13 +00001512 return T.getAsOpaquePtr();
1513}
1514
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001515
1516
1517//===----------------------------------------------------------------------===//
1518// Type Attribute Processing
1519//===----------------------------------------------------------------------===//
1520
1521/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
1522/// specified type. The attribute contains 1 argument, the id of the address
1523/// space for the type.
Mike Stump1eb44332009-09-09 15:08:12 +00001524static void HandleAddressSpaceTypeAttribute(QualType &Type,
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001525 const AttributeList &Attr, Sema &S){
John McCall0953e762009-09-24 19:53:00 +00001526
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001527 // If this type is already address space qualified, reject it.
1528 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
1529 // for two or more different address spaces."
1530 if (Type.getAddressSpace()) {
1531 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
1532 return;
1533 }
Mike Stump1eb44332009-09-09 15:08:12 +00001534
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001535 // Check the attribute arguments.
1536 if (Attr.getNumArgs() != 1) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +00001537 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001538 return;
1539 }
1540 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
1541 llvm::APSInt addrSpace(32);
1542 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001543 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
1544 << ASArgExpr->getSourceRange();
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001545 return;
1546 }
1547
John McCallefadb772009-07-28 06:52:18 +00001548 // Bounds checking.
1549 if (addrSpace.isSigned()) {
1550 if (addrSpace.isNegative()) {
1551 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative)
1552 << ASArgExpr->getSourceRange();
1553 return;
1554 }
1555 addrSpace.setIsSigned(false);
1556 }
1557 llvm::APSInt max(addrSpace.getBitWidth());
John McCall0953e762009-09-24 19:53:00 +00001558 max = Qualifiers::MaxAddressSpace;
John McCallefadb772009-07-28 06:52:18 +00001559 if (addrSpace > max) {
1560 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high)
John McCall0953e762009-09-24 19:53:00 +00001561 << Qualifiers::MaxAddressSpace << ASArgExpr->getSourceRange();
John McCallefadb772009-07-28 06:52:18 +00001562 return;
1563 }
1564
Mike Stump1eb44332009-09-09 15:08:12 +00001565 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001566 Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001567}
1568
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001569/// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the
1570/// specified type. The attribute contains 1 argument, weak or strong.
Mike Stump1eb44332009-09-09 15:08:12 +00001571static void HandleObjCGCTypeAttribute(QualType &Type,
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001572 const AttributeList &Attr, Sema &S) {
John McCall0953e762009-09-24 19:53:00 +00001573 if (Type.getObjCGCAttr() != Qualifiers::GCNone) {
Fariborz Jahanian5934e752009-02-18 18:52:41 +00001574 S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001575 return;
1576 }
Mike Stump1eb44332009-09-09 15:08:12 +00001577
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001578 // Check the attribute arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001579 if (!Attr.getParameterName()) {
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001580 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1581 << "objc_gc" << 1;
1582 return;
1583 }
John McCall0953e762009-09-24 19:53:00 +00001584 Qualifiers::GC GCAttr;
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001585 if (Attr.getNumArgs() != 0) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001586 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1587 return;
1588 }
Mike Stump1eb44332009-09-09 15:08:12 +00001589 if (Attr.getParameterName()->isStr("weak"))
John McCall0953e762009-09-24 19:53:00 +00001590 GCAttr = Qualifiers::Weak;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001591 else if (Attr.getParameterName()->isStr("strong"))
John McCall0953e762009-09-24 19:53:00 +00001592 GCAttr = Qualifiers::Strong;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001593 else {
1594 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1595 << "objc_gc" << Attr.getParameterName();
1596 return;
1597 }
Mike Stump1eb44332009-09-09 15:08:12 +00001598
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001599 Type = S.Context.getObjCGCQualType(Type, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001600}
1601
Mike Stump24556362009-07-25 21:26:53 +00001602/// HandleNoReturnTypeAttribute - Process the noreturn attribute on the
1603/// specified type. The attribute contains 0 arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001604static void HandleNoReturnTypeAttribute(QualType &Type,
Mike Stump24556362009-07-25 21:26:53 +00001605 const AttributeList &Attr, Sema &S) {
1606 if (Attr.getNumArgs() != 0)
1607 return;
1608
1609 // We only apply this to a pointer to function or a pointer to block.
1610 if (!Type->isFunctionPointerType()
1611 && !Type->isBlockPointerType()
1612 && !Type->isFunctionType())
1613 return;
1614
1615 Type = S.Context.getNoReturnType(Type);
1616}
1617
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001618void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) {
Chris Lattner232e8822008-02-21 01:08:11 +00001619 // Scan through and apply attributes to this type where it makes sense. Some
1620 // attributes (such as __address_space__, __vector_size__, etc) apply to the
1621 // type, but others can be present in the type specifiers even though they
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00001622 // apply to the decl. Here we apply type attributes and ignore the rest.
1623 for (; AL; AL = AL->getNext()) {
Chris Lattner232e8822008-02-21 01:08:11 +00001624 // If this is an attribute we can handle, do so now, otherwise, add it to
1625 // the LeftOverAttrs list for rechaining.
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00001626 switch (AL->getKind()) {
Chris Lattner232e8822008-02-21 01:08:11 +00001627 default: break;
1628 case AttributeList::AT_address_space:
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001629 HandleAddressSpaceTypeAttribute(Result, *AL, *this);
1630 break;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001631 case AttributeList::AT_objc_gc:
1632 HandleObjCGCTypeAttribute(Result, *AL, *this);
1633 break;
Mike Stump24556362009-07-25 21:26:53 +00001634 case AttributeList::AT_noreturn:
1635 HandleNoReturnTypeAttribute(Result, *AL, *this);
1636 break;
Chris Lattner232e8822008-02-21 01:08:11 +00001637 }
Chris Lattner232e8822008-02-21 01:08:11 +00001638 }
Chris Lattner232e8822008-02-21 01:08:11 +00001639}
1640
Mike Stump1eb44332009-09-09 15:08:12 +00001641/// @brief Ensure that the type T is a complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001642///
1643/// This routine checks whether the type @p T is complete in any
1644/// context where a complete type is required. If @p T is a complete
Douglas Gregor86447ec2009-03-09 16:13:40 +00001645/// type, returns false. If @p T is a class template specialization,
1646/// this routine then attempts to perform class template
1647/// instantiation. If instantiation fails, or if @p T is incomplete
1648/// and cannot be completed, issues the diagnostic @p diag (giving it
1649/// the type @p T) and returns true.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001650///
1651/// @param Loc The location in the source that the incomplete type
1652/// diagnostic should refer to.
1653///
1654/// @param T The type that this routine is examining for completeness.
1655///
Mike Stump1eb44332009-09-09 15:08:12 +00001656/// @param PD The partial diagnostic that will be printed out if T is not a
Anders Carlssonb7906612009-08-26 23:45:07 +00001657/// complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001658///
1659/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
1660/// @c false otherwise.
Anders Carlsson91a0cc92009-08-26 22:33:56 +00001661bool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
Anders Carlsson8c8d9192009-10-09 23:51:55 +00001662 const PartialDiagnostic &PD,
1663 std::pair<SourceLocation,
1664 PartialDiagnostic> Note) {
Anders Carlsson91a0cc92009-08-26 22:33:56 +00001665 unsigned diag = PD.getDiagID();
Mike Stump1eb44332009-09-09 15:08:12 +00001666
Douglas Gregor573d9c32009-10-21 23:19:44 +00001667 // FIXME: Add this assertion to make sure we always get instantiation points.
1668 // assert(!Loc.isInvalid() && "Invalid location in RequireCompleteType");
Douglas Gregor690dc7f2009-05-21 23:48:18 +00001669 // FIXME: Add this assertion to help us flush out problems with
1670 // checking for dependent types and type-dependent expressions.
1671 //
Mike Stump1eb44332009-09-09 15:08:12 +00001672 // assert(!T->isDependentType() &&
Douglas Gregor690dc7f2009-05-21 23:48:18 +00001673 // "Can't ask whether a dependent type is complete");
1674
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001675 // If we have a complete type, we're done.
1676 if (!T->isIncompleteType())
1677 return false;
Eli Friedman3c0eb162008-05-27 03:33:27 +00001678
Douglas Gregord475b8d2009-03-25 21:17:03 +00001679 // If we have a class template specialization or a class member of a
1680 // class template specialization, try to instantiate it.
Ted Kremenek6217b802009-07-29 21:53:49 +00001681 if (const RecordType *Record = T->getAs<RecordType>()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00001682 if (ClassTemplateSpecializationDecl *ClassTemplateSpec
Douglas Gregord475b8d2009-03-25 21:17:03 +00001683 = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001684 if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared)
1685 return InstantiateClassTemplateSpecialization(Loc, ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001686 TSK_ImplicitInstantiation,
Douglas Gregor5842ba92009-08-24 15:23:48 +00001687 /*Complain=*/diag != 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001688 } else if (CXXRecordDecl *Rec
Douglas Gregord475b8d2009-03-25 21:17:03 +00001689 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
1690 if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001691 MemberSpecializationInfo *MSInfo = Rec->getMemberSpecializationInfo();
1692 assert(MSInfo && "Missing member specialization information?");
Douglas Gregor357bbd02009-08-28 20:50:45 +00001693 // This record was instantiated from a class within a template.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001694 if (MSInfo->getTemplateSpecializationKind()
Douglas Gregor972e6ce2009-10-27 06:26:26 +00001695 != TSK_ExplicitSpecialization)
Douglas Gregorf6b11852009-10-08 15:14:33 +00001696 return InstantiateClass(Loc, Rec, Pattern,
1697 getTemplateInstantiationArgs(Rec),
1698 TSK_ImplicitInstantiation,
1699 /*Complain=*/diag != 0);
Douglas Gregord475b8d2009-03-25 21:17:03 +00001700 }
1701 }
1702 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00001703
Douglas Gregor5842ba92009-08-24 15:23:48 +00001704 if (diag == 0)
1705 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001706
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001707 // We have an incomplete type. Produce a diagnostic.
Anders Carlsson91a0cc92009-08-26 22:33:56 +00001708 Diag(Loc, PD) << T;
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001709
Anders Carlsson8c8d9192009-10-09 23:51:55 +00001710 // If we have a note, produce it.
1711 if (!Note.first.isInvalid())
1712 Diag(Note.first, Note.second);
1713
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001714 // If the type was a forward declaration of a class/struct/union
Mike Stump1eb44332009-09-09 15:08:12 +00001715 // type, produce
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001716 const TagType *Tag = 0;
Ted Kremenek6217b802009-07-29 21:53:49 +00001717 if (const RecordType *Record = T->getAs<RecordType>())
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001718 Tag = Record;
John McCall183700f2009-09-21 23:43:11 +00001719 else if (const EnumType *Enum = T->getAs<EnumType>())
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001720 Tag = Enum;
1721
1722 if (Tag && !Tag->getDecl()->isInvalidDecl())
Mike Stump1eb44332009-09-09 15:08:12 +00001723 Diag(Tag->getDecl()->getLocation(),
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001724 Tag->isBeingDefined() ? diag::note_type_being_defined
1725 : diag::note_forward_declaration)
1726 << QualType(Tag, 0);
1727
1728 return true;
1729}
Douglas Gregore6258932009-03-19 00:39:20 +00001730
1731/// \brief Retrieve a version of the type 'T' that is qualified by the
1732/// nested-name-specifier contained in SS.
1733QualType Sema::getQualifiedNameType(const CXXScopeSpec &SS, QualType T) {
1734 if (!SS.isSet() || SS.isInvalid() || T.isNull())
1735 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00001736
Douglas Gregorab452ba2009-03-26 23:50:42 +00001737 NestedNameSpecifier *NNS
Douglas Gregor35073692009-03-26 23:56:24 +00001738 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Douglas Gregorab452ba2009-03-26 23:50:42 +00001739 return Context.getQualifiedNameType(NNS, T);
Douglas Gregore6258932009-03-19 00:39:20 +00001740}
Anders Carlssonaf017e62009-06-29 22:58:55 +00001741
1742QualType Sema::BuildTypeofExprType(Expr *E) {
1743 return Context.getTypeOfExprType(E);
1744}
1745
1746QualType Sema::BuildDecltypeType(Expr *E) {
1747 if (E->getType() == Context.OverloadTy) {
Mike Stump1eb44332009-09-09 15:08:12 +00001748 Diag(E->getLocStart(),
Anders Carlssonaf017e62009-06-29 22:58:55 +00001749 diag::err_cannot_determine_declared_type_of_overloaded_function);
1750 return QualType();
1751 }
1752 return Context.getDecltypeType(E);
1753}