blob: 2bff6838c410c1ef0b89995806896523c1b600b1 [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
John McCall2d887082010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
John McCall7cd088e2010-08-24 07:21:54 +000015#include "clang/Sema/Template.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000016#include "clang/AST/ASTContext.h"
Douglas Gregora8f32e02009-10-06 17:59:45 +000017#include "clang/AST/CXXInheritance.h"
Steve Naroff980e5082007-10-01 19:00:59 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor2943aed2009-03-03 04:44:36 +000019#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +000020#include "clang/AST/TypeLoc.h"
John McCall51bd8032009-10-18 01:05:36 +000021#include "clang/AST/TypeLocVisitor.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000022#include "clang/AST/Expr.h"
Anders Carlsson91a0cc92009-08-26 22:33:56 +000023#include "clang/Basic/PartialDiagnostic.h"
Charles Davisd18f9f92010-08-16 04:01:50 +000024#include "clang/Basic/TargetInfo.h"
John McCall19510852010-08-20 18:27:03 +000025#include "clang/Sema/DeclSpec.h"
Sebastian Redl4994d2d2009-07-04 11:39:00 +000026#include "llvm/ADT/SmallPtrSet.h"
Douglas Gregor87c12c42009-11-04 16:49:01 +000027#include "llvm/Support/ErrorHandling.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000028using namespace clang;
29
Douglas Gregor2dc0e642009-03-23 23:06:20 +000030/// \brief Perform adjustment on the parameter type of a function.
31///
32/// This routine adjusts the given parameter type @p T to the actual
Mike Stump1eb44332009-09-09 15:08:12 +000033/// parameter type used by semantic analysis (C99 6.7.5.3p[7,8],
34/// C++ [dcl.fct]p3). The adjusted parameter type is returned.
Douglas Gregor2dc0e642009-03-23 23:06:20 +000035QualType Sema::adjustParameterType(QualType T) {
36 // C99 6.7.5.3p7:
Chris Lattner778ed742009-10-25 17:36:50 +000037 // A declaration of a parameter as "array of type" shall be
38 // adjusted to "qualified pointer to type", where the type
39 // qualifiers (if any) are those specified within the [ and ] of
40 // the array type derivation.
41 if (T->isArrayType())
Douglas Gregor2dc0e642009-03-23 23:06:20 +000042 return Context.getArrayDecayedType(T);
Chris Lattner778ed742009-10-25 17:36:50 +000043
44 // C99 6.7.5.3p8:
45 // A declaration of a parameter as "function returning type"
46 // shall be adjusted to "pointer to function returning type", as
47 // in 6.3.2.1.
48 if (T->isFunctionType())
Douglas Gregor2dc0e642009-03-23 23:06:20 +000049 return Context.getPointerType(T);
50
51 return T;
52}
53
Chris Lattner5db2bb12009-10-25 18:21:37 +000054
55
56/// isOmittedBlockReturnType - Return true if this declarator is missing a
57/// return type because this is a omitted return type on a block literal.
Sebastian Redl8ce35b02009-10-25 21:45:37 +000058static bool isOmittedBlockReturnType(const Declarator &D) {
Chris Lattner5db2bb12009-10-25 18:21:37 +000059 if (D.getContext() != Declarator::BlockLiteralContext ||
Sebastian Redl8ce35b02009-10-25 21:45:37 +000060 D.getDeclSpec().hasTypeSpecifier())
Chris Lattner5db2bb12009-10-25 18:21:37 +000061 return false;
62
63 if (D.getNumTypeObjects() == 0)
Chris Lattnera64ef0a2009-10-25 22:09:09 +000064 return true; // ^{ ... }
Chris Lattner5db2bb12009-10-25 18:21:37 +000065
66 if (D.getNumTypeObjects() == 1 &&
67 D.getTypeObject(0).Kind == DeclaratorChunk::Function)
Chris Lattnera64ef0a2009-10-25 22:09:09 +000068 return true; // ^(int X, float Y) { ... }
Chris Lattner5db2bb12009-10-25 18:21:37 +000069
70 return false;
71}
72
John McCall04a67a62010-02-05 21:31:56 +000073typedef std::pair<const AttributeList*,QualType> DelayedAttribute;
74typedef llvm::SmallVectorImpl<DelayedAttribute> DelayedAttributeSet;
75
76static void ProcessTypeAttributeList(Sema &S, QualType &Type,
Charles Davis328ce342010-02-24 02:27:18 +000077 bool IsDeclSpec,
John McCall04a67a62010-02-05 21:31:56 +000078 const AttributeList *Attrs,
79 DelayedAttributeSet &DelayedFnAttrs);
80static bool ProcessFnAttr(Sema &S, QualType &Type, const AttributeList &Attr);
81
82static void ProcessDelayedFnAttrs(Sema &S, QualType &Type,
83 DelayedAttributeSet &Attrs) {
84 for (DelayedAttributeSet::iterator I = Attrs.begin(),
85 E = Attrs.end(); I != E; ++I)
Abramo Bagnarae215f722010-04-30 13:10:51 +000086 if (ProcessFnAttr(S, Type, *I->first)) {
John McCall04a67a62010-02-05 21:31:56 +000087 S.Diag(I->first->getLoc(), diag::warn_function_attribute_wrong_type)
88 << I->first->getName() << I->second;
Abramo Bagnarae215f722010-04-30 13:10:51 +000089 // Avoid any further processing of this attribute.
90 I->first->setInvalid();
91 }
John McCall04a67a62010-02-05 21:31:56 +000092 Attrs.clear();
93}
94
95static void DiagnoseDelayedFnAttrs(Sema &S, DelayedAttributeSet &Attrs) {
96 for (DelayedAttributeSet::iterator I = Attrs.begin(),
97 E = Attrs.end(); I != E; ++I) {
98 S.Diag(I->first->getLoc(), diag::warn_function_attribute_wrong_type)
99 << I->first->getName() << I->second;
Abramo Bagnarae215f722010-04-30 13:10:51 +0000100 // Avoid any further processing of this attribute.
101 I->first->setInvalid();
John McCall04a67a62010-02-05 21:31:56 +0000102 }
103 Attrs.clear();
104}
105
Douglas Gregor930d8b52009-01-30 22:09:00 +0000106/// \brief Convert the specified declspec to the appropriate type
107/// object.
Chris Lattner5db2bb12009-10-25 18:21:37 +0000108/// \param D the declarator containing the declaration specifier.
Chris Lattner5153ee62009-04-25 08:47:54 +0000109/// \returns The type described by the declaration specifiers. This function
110/// never returns null.
John McCall04a67a62010-02-05 21:31:56 +0000111static QualType ConvertDeclSpecToType(Sema &TheSema,
112 Declarator &TheDeclarator,
113 DelayedAttributeSet &Delayed) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 // FIXME: Should move the logic from DeclSpec::Finish to here for validity
115 // checking.
Chris Lattner5db2bb12009-10-25 18:21:37 +0000116 const DeclSpec &DS = TheDeclarator.getDeclSpec();
117 SourceLocation DeclLoc = TheDeclarator.getIdentifierLoc();
118 if (DeclLoc.isInvalid())
119 DeclLoc = DS.getSourceRange().getBegin();
Chris Lattner1564e392009-10-25 18:07:27 +0000120
121 ASTContext &Context = TheSema.Context;
Mike Stump1eb44332009-09-09 15:08:12 +0000122
Chris Lattner5db2bb12009-10-25 18:21:37 +0000123 QualType Result;
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 switch (DS.getTypeSpecType()) {
Chris Lattner96b77fc2008-04-02 06:50:17 +0000125 case DeclSpec::TST_void:
126 Result = Context.VoidTy;
127 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000128 case DeclSpec::TST_char:
129 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000130 Result = Context.CharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000131 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000132 Result = Context.SignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000133 else {
134 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
135 "Unknown TSS value");
Chris Lattnerfab5b452008-02-20 23:53:49 +0000136 Result = Context.UnsignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000137 }
Chris Lattner958858e2008-02-20 21:40:32 +0000138 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000139 case DeclSpec::TST_wchar:
140 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
141 Result = Context.WCharTy;
142 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
Chris Lattner1564e392009-10-25 18:07:27 +0000143 TheSema.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000144 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000145 Result = Context.getSignedWCharType();
146 } else {
147 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
148 "Unknown TSS value");
Chris Lattner1564e392009-10-25 18:07:27 +0000149 TheSema.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000150 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000151 Result = Context.getUnsignedWCharType();
152 }
153 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000154 case DeclSpec::TST_char16:
155 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
156 "Unknown TSS value");
157 Result = Context.Char16Ty;
158 break;
159 case DeclSpec::TST_char32:
160 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified &&
161 "Unknown TSS value");
162 Result = Context.Char32Ty;
163 break;
Chris Lattnerd658b562008-04-05 06:32:51 +0000164 case DeclSpec::TST_unspecified:
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000165 // "<proto1,proto2>" is an objc qualified ID with a missing id.
Chris Lattner097e9162008-10-20 02:01:50 +0000166 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
John McCallc12c5bb2010-05-15 11:32:37 +0000167 Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy,
168 (ObjCProtocolDecl**)PQ,
169 DS.getNumProtocolQualifiers());
170 Result = Context.getObjCObjectPointerType(Result);
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000171 break;
172 }
Chris Lattner5db2bb12009-10-25 18:21:37 +0000173
174 // If this is a missing declspec in a block literal return context, then it
175 // is inferred from the return statements inside the block.
Sebastian Redl8ce35b02009-10-25 21:45:37 +0000176 if (isOmittedBlockReturnType(TheDeclarator)) {
Chris Lattner5db2bb12009-10-25 18:21:37 +0000177 Result = Context.DependentTy;
178 break;
179 }
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Chris Lattnerd658b562008-04-05 06:32:51 +0000181 // Unspecified typespec defaults to int in C90. However, the C90 grammar
182 // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
183 // type-qualifier, or storage-class-specifier. If not, emit an extwarn.
184 // Note that the one exception to this is function definitions, which are
185 // allowed to be completely missing a declspec. This is handled in the
186 // parser already though by it pretending to have seen an 'int' in this
187 // case.
Chris Lattner1564e392009-10-25 18:07:27 +0000188 if (TheSema.getLangOptions().ImplicitInt) {
Chris Lattner35d276f2009-02-27 18:53:28 +0000189 // In C89 mode, we only warn if there is a completely missing declspec
190 // when one is not allowed.
Chris Lattner3f84ad22009-04-22 05:27:59 +0000191 if (DS.isEmpty()) {
Chris Lattner1564e392009-10-25 18:07:27 +0000192 TheSema.Diag(DeclLoc, diag::ext_missing_declspec)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000193 << DS.getSourceRange()
Douglas Gregor849b2432010-03-31 17:46:05 +0000194 << FixItHint::CreateInsertion(DS.getSourceRange().getBegin(), "int");
Chris Lattner3f84ad22009-04-22 05:27:59 +0000195 }
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000196 } else if (!DS.hasTypeSpecifier()) {
Chris Lattnerd658b562008-04-05 06:32:51 +0000197 // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says:
198 // "At least one type specifier shall be given in the declaration
199 // specifiers in each declaration, and in the specifier-qualifier list in
200 // each struct declaration and type name."
Douglas Gregor4310f4e2009-02-16 22:38:20 +0000201 // FIXME: Does Microsoft really have the implicit int extension in C++?
Chris Lattner1564e392009-10-25 18:07:27 +0000202 if (TheSema.getLangOptions().CPlusPlus &&
203 !TheSema.getLangOptions().Microsoft) {
204 TheSema.Diag(DeclLoc, diag::err_missing_type_specifier)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000205 << DS.getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000206
Chris Lattnerb78d8332009-06-26 04:45:06 +0000207 // When this occurs in C++ code, often something is very broken with the
208 // value being declared, poison it as invalid so we don't get chains of
209 // errors.
Chris Lattner5db2bb12009-10-25 18:21:37 +0000210 TheDeclarator.setInvalidType(true);
Chris Lattnerb78d8332009-06-26 04:45:06 +0000211 } else {
Chris Lattner1564e392009-10-25 18:07:27 +0000212 TheSema.Diag(DeclLoc, diag::ext_missing_type_specifier)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000213 << DS.getSourceRange();
Chris Lattnerb78d8332009-06-26 04:45:06 +0000214 }
Chris Lattnerd658b562008-04-05 06:32:51 +0000215 }
Mike Stump1eb44332009-09-09 15:08:12 +0000216
217 // FALL THROUGH.
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000218 case DeclSpec::TST_int: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000219 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
220 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000221 case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
222 case DeclSpec::TSW_short: Result = Context.ShortTy; break;
223 case DeclSpec::TSW_long: Result = Context.LongTy; break;
Chris Lattner311157f2009-10-25 18:25:04 +0000224 case DeclSpec::TSW_longlong:
225 Result = Context.LongLongTy;
226
227 // long long is a C99 feature.
228 if (!TheSema.getLangOptions().C99 &&
229 !TheSema.getLangOptions().CPlusPlus0x)
230 TheSema.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong);
231 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000232 }
233 } else {
234 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000235 case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
236 case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break;
237 case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break;
Chris Lattner311157f2009-10-25 18:25:04 +0000238 case DeclSpec::TSW_longlong:
239 Result = Context.UnsignedLongLongTy;
240
241 // long long is a C99 feature.
242 if (!TheSema.getLangOptions().C99 &&
243 !TheSema.getLangOptions().CPlusPlus0x)
244 TheSema.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong);
245 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000246 }
247 }
Chris Lattner958858e2008-02-20 21:40:32 +0000248 break;
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000249 }
Chris Lattnerfab5b452008-02-20 23:53:49 +0000250 case DeclSpec::TST_float: Result = Context.FloatTy; break;
Chris Lattner958858e2008-02-20 21:40:32 +0000251 case DeclSpec::TST_double:
252 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000253 Result = Context.LongDoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000254 else
Chris Lattnerfab5b452008-02-20 23:53:49 +0000255 Result = Context.DoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000256 break;
Chris Lattnerfab5b452008-02-20 23:53:49 +0000257 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
Reid Spencer5f016e22007-07-11 17:01:13 +0000258 case DeclSpec::TST_decimal32: // _Decimal32
259 case DeclSpec::TST_decimal64: // _Decimal64
260 case DeclSpec::TST_decimal128: // _Decimal128
Chris Lattner1564e392009-10-25 18:07:27 +0000261 TheSema.Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported);
Chris Lattner8f12f652009-05-13 05:02:08 +0000262 Result = Context.IntTy;
Chris Lattner5db2bb12009-10-25 18:21:37 +0000263 TheDeclarator.setInvalidType(true);
Chris Lattner8f12f652009-05-13 05:02:08 +0000264 break;
Chris Lattner99dc9142008-04-13 18:59:07 +0000265 case DeclSpec::TST_class:
Reid Spencer5f016e22007-07-11 17:01:13 +0000266 case DeclSpec::TST_enum:
267 case DeclSpec::TST_union:
268 case DeclSpec::TST_struct: {
John McCallb3d87482010-08-24 05:47:05 +0000269 TypeDecl *D = dyn_cast_or_null<TypeDecl>(DS.getRepAsDecl());
John McCall6e247262009-10-10 05:48:19 +0000270 if (!D) {
271 // This can happen in C++ with ambiguous lookups.
272 Result = Context.IntTy;
Chris Lattner5db2bb12009-10-25 18:21:37 +0000273 TheDeclarator.setInvalidType(true);
John McCall6e247262009-10-10 05:48:19 +0000274 break;
275 }
276
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000277 // If the type is deprecated or unavailable, diagnose it.
John McCall54abf7d2009-11-04 02:18:39 +0000278 TheSema.DiagnoseUseOfDecl(D, DS.getTypeSpecTypeLoc());
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000279
Reid Spencer5f016e22007-07-11 17:01:13 +0000280 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000281 DS.getTypeSpecSign() == 0 && "No qualifiers on tag names!");
282
Reid Spencer5f016e22007-07-11 17:01:13 +0000283 // TypeQuals handled by caller.
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000284 Result = Context.getTypeDeclType(D);
John McCall2191b202009-09-05 06:31:47 +0000285
286 // In C++, make an ElaboratedType.
Chris Lattner1564e392009-10-25 18:07:27 +0000287 if (TheSema.getLangOptions().CPlusPlus) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000288 ElaboratedTypeKeyword Keyword
289 = ElaboratedType::getKeywordForTypeSpec(DS.getTypeSpecType());
290 Result = TheSema.getElaboratedType(Keyword, DS.getTypeSpecScope(),
291 Result);
John McCall2191b202009-09-05 06:31:47 +0000292 }
Chris Lattner5153ee62009-04-25 08:47:54 +0000293 if (D->isInvalidDecl())
Chris Lattner5db2bb12009-10-25 18:21:37 +0000294 TheDeclarator.setInvalidType(true);
Chris Lattner958858e2008-02-20 21:40:32 +0000295 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000296 }
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000297 case DeclSpec::TST_typename: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000298 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
299 DS.getTypeSpecSign() == 0 &&
300 "Can't handle qualifiers on typedef names yet!");
John McCallb3d87482010-08-24 05:47:05 +0000301 Result = TheSema.GetTypeFromParser(DS.getRepAsType());
John McCall27940d22010-07-30 05:17:22 +0000302 if (Result.isNull())
303 TheDeclarator.setInvalidType(true);
304 else if (DeclSpec::ProtocolQualifierListTy PQ
305 = DS.getProtocolQualifiers()) {
John McCallc12c5bb2010-05-15 11:32:37 +0000306 if (const ObjCObjectType *ObjT = Result->getAs<ObjCObjectType>()) {
307 // Silently drop any existing protocol qualifiers.
308 // TODO: determine whether that's the right thing to do.
309 if (ObjT->getNumProtocols())
310 Result = ObjT->getBaseType();
311
312 if (DS.getNumProtocolQualifiers())
313 Result = Context.getObjCObjectType(Result,
314 (ObjCProtocolDecl**) PQ,
315 DS.getNumProtocolQualifiers());
316 } else if (Result->isObjCIdType()) {
Chris Lattnerae4da612008-07-26 01:53:50 +0000317 // id<protocol-list>
John McCallc12c5bb2010-05-15 11:32:37 +0000318 Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy,
319 (ObjCProtocolDecl**) PQ,
320 DS.getNumProtocolQualifiers());
321 Result = Context.getObjCObjectPointerType(Result);
322 } else if (Result->isObjCClassType()) {
Steve Naroff4262a072009-02-23 18:53:24 +0000323 // Class<protocol-list>
John McCallc12c5bb2010-05-15 11:32:37 +0000324 Result = Context.getObjCObjectType(Context.ObjCBuiltinClassTy,
325 (ObjCProtocolDecl**) PQ,
326 DS.getNumProtocolQualifiers());
327 Result = Context.getObjCObjectPointerType(Result);
Chris Lattner3f84ad22009-04-22 05:27:59 +0000328 } else {
Chris Lattner1564e392009-10-25 18:07:27 +0000329 TheSema.Diag(DeclLoc, diag::err_invalid_protocol_qualifiers)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000330 << DS.getSourceRange();
Chris Lattner5db2bb12009-10-25 18:21:37 +0000331 TheDeclarator.setInvalidType(true);
Chris Lattner3f84ad22009-04-22 05:27:59 +0000332 }
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000333 }
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Reid Spencer5f016e22007-07-11 17:01:13 +0000335 // TypeQuals handled by caller.
Chris Lattner958858e2008-02-20 21:40:32 +0000336 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000337 }
Chris Lattner958858e2008-02-20 21:40:32 +0000338 case DeclSpec::TST_typeofType:
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000339 // FIXME: Preserve type source info.
John McCallb3d87482010-08-24 05:47:05 +0000340 Result = TheSema.GetTypeFromParser(DS.getRepAsType());
Chris Lattner958858e2008-02-20 21:40:32 +0000341 assert(!Result.isNull() && "Didn't get a type for typeof?");
Steve Naroffd1861fd2007-07-31 12:34:36 +0000342 // TypeQuals handled by caller.
Chris Lattnerfab5b452008-02-20 23:53:49 +0000343 Result = Context.getTypeOfType(Result);
Chris Lattner958858e2008-02-20 21:40:32 +0000344 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000345 case DeclSpec::TST_typeofExpr: {
John McCallb3d87482010-08-24 05:47:05 +0000346 Expr *E = DS.getRepAsExpr();
Steve Naroffd1861fd2007-07-31 12:34:36 +0000347 assert(E && "Didn't get an expression for typeof?");
348 // TypeQuals handled by caller.
Douglas Gregor4b52e252009-12-21 23:17:24 +0000349 Result = TheSema.BuildTypeofExprType(E);
350 if (Result.isNull()) {
351 Result = Context.IntTy;
352 TheDeclarator.setInvalidType(true);
353 }
Chris Lattner958858e2008-02-20 21:40:32 +0000354 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000355 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000356 case DeclSpec::TST_decltype: {
John McCallb3d87482010-08-24 05:47:05 +0000357 Expr *E = DS.getRepAsExpr();
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000358 assert(E && "Didn't get an expression for decltype?");
359 // TypeQuals handled by caller.
Chris Lattner1564e392009-10-25 18:07:27 +0000360 Result = TheSema.BuildDecltypeType(E);
Anders Carlssonaf017e62009-06-29 22:58:55 +0000361 if (Result.isNull()) {
362 Result = Context.IntTy;
Chris Lattner5db2bb12009-10-25 18:21:37 +0000363 TheDeclarator.setInvalidType(true);
Anders Carlssonaf017e62009-06-29 22:58:55 +0000364 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000365 break;
366 }
Anders Carlssone89d1592009-06-26 18:41:36 +0000367 case DeclSpec::TST_auto: {
368 // TypeQuals handled by caller.
369 Result = Context.UndeducedAutoTy;
370 break;
371 }
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Douglas Gregor809070a2009-02-18 17:45:20 +0000373 case DeclSpec::TST_error:
Chris Lattner5153ee62009-04-25 08:47:54 +0000374 Result = Context.IntTy;
Chris Lattner5db2bb12009-10-25 18:21:37 +0000375 TheDeclarator.setInvalidType(true);
Chris Lattner5153ee62009-04-25 08:47:54 +0000376 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000377 }
Mike Stump1eb44332009-09-09 15:08:12 +0000378
Chris Lattner958858e2008-02-20 21:40:32 +0000379 // Handle complex types.
Douglas Gregorf244cd72009-02-14 21:06:05 +0000380 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
Chris Lattner1564e392009-10-25 18:07:27 +0000381 if (TheSema.getLangOptions().Freestanding)
382 TheSema.Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
Chris Lattnerfab5b452008-02-20 23:53:49 +0000383 Result = Context.getComplexType(Result);
John Thompson82287d12010-02-05 00:12:22 +0000384 } else if (DS.isTypeAltiVecVector()) {
385 unsigned typeSize = static_cast<unsigned>(Context.getTypeSize(Result));
386 assert(typeSize > 0 && "type size for vector must be greater than 0 bits");
Chris Lattner788b0fd2010-06-23 06:00:24 +0000387 VectorType::AltiVecSpecific AltiVecSpec = VectorType::AltiVec;
388 if (DS.isTypeAltiVecPixel())
389 AltiVecSpec = VectorType::Pixel;
390 else if (DS.isTypeAltiVecBool())
391 AltiVecSpec = VectorType::Bool;
392 Result = Context.getVectorType(Result, 128/typeSize, AltiVecSpec);
Douglas Gregorf244cd72009-02-14 21:06:05 +0000393 }
Mike Stump1eb44332009-09-09 15:08:12 +0000394
Chris Lattner958858e2008-02-20 21:40:32 +0000395 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
396 "FIXME: imaginary types not supported yet!");
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Chris Lattner38d8b982008-02-20 22:04:11 +0000398 // See if there are any attributes on the declspec that apply to the type (as
399 // opposed to the decl).
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000400 if (const AttributeList *AL = DS.getAttributes())
Charles Davis328ce342010-02-24 02:27:18 +0000401 ProcessTypeAttributeList(TheSema, Result, true, AL, Delayed);
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Chris Lattner96b77fc2008-04-02 06:50:17 +0000403 // Apply const/volatile/restrict qualifiers to T.
404 if (unsigned TypeQuals = DS.getTypeQualifiers()) {
405
406 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
407 // or incomplete types shall not be restrict-qualified." C++ also allows
408 // restrict-qualified references.
John McCall0953e762009-09-24 19:53:00 +0000409 if (TypeQuals & DeclSpec::TQ_restrict) {
Fariborz Jahanian2b5ff1a2009-12-07 18:08:58 +0000410 if (Result->isAnyPointerType() || Result->isReferenceType()) {
411 QualType EltTy;
412 if (Result->isObjCObjectPointerType())
413 EltTy = Result;
414 else
415 EltTy = Result->isPointerType() ?
416 Result->getAs<PointerType>()->getPointeeType() :
417 Result->getAs<ReferenceType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000418
Douglas Gregorbad0e652009-03-24 20:32:41 +0000419 // If we have a pointer or reference, the pointee must have an object
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000420 // incomplete type.
421 if (!EltTy->isIncompleteOrObjectType()) {
Chris Lattner1564e392009-10-25 18:07:27 +0000422 TheSema.Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000423 diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattnerd1625842008-11-24 06:25:27 +0000424 << EltTy << DS.getSourceRange();
John McCall0953e762009-09-24 19:53:00 +0000425 TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000426 }
427 } else {
Chris Lattner1564e392009-10-25 18:07:27 +0000428 TheSema.Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000429 diag::err_typecheck_invalid_restrict_not_pointer)
Chris Lattnerd1625842008-11-24 06:25:27 +0000430 << Result << DS.getSourceRange();
John McCall0953e762009-09-24 19:53:00 +0000431 TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
Chris Lattner96b77fc2008-04-02 06:50:17 +0000432 }
Chris Lattner96b77fc2008-04-02 06:50:17 +0000433 }
Mike Stump1eb44332009-09-09 15:08:12 +0000434
Chris Lattner96b77fc2008-04-02 06:50:17 +0000435 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
436 // of a function type includes any type qualifiers, the behavior is
437 // undefined."
438 if (Result->isFunctionType() && TypeQuals) {
439 // Get some location to point at, either the C or V location.
440 SourceLocation Loc;
John McCall0953e762009-09-24 19:53:00 +0000441 if (TypeQuals & DeclSpec::TQ_const)
Chris Lattner96b77fc2008-04-02 06:50:17 +0000442 Loc = DS.getConstSpecLoc();
John McCall0953e762009-09-24 19:53:00 +0000443 else if (TypeQuals & DeclSpec::TQ_volatile)
Chris Lattner96b77fc2008-04-02 06:50:17 +0000444 Loc = DS.getVolatileSpecLoc();
John McCall0953e762009-09-24 19:53:00 +0000445 else {
446 assert((TypeQuals & DeclSpec::TQ_restrict) &&
447 "Has CVR quals but not C, V, or R?");
448 Loc = DS.getRestrictSpecLoc();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000449 }
Chris Lattner1564e392009-10-25 18:07:27 +0000450 TheSema.Diag(Loc, diag::warn_typecheck_function_qualifiers)
Chris Lattnerd1625842008-11-24 06:25:27 +0000451 << Result << DS.getSourceRange();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000452 }
Mike Stump1eb44332009-09-09 15:08:12 +0000453
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000454 // C++ [dcl.ref]p1:
455 // Cv-qualified references are ill-formed except when the
456 // cv-qualifiers are introduced through the use of a typedef
457 // (7.1.3) or of a template type argument (14.3), in which
458 // case the cv-qualifiers are ignored.
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000459 // FIXME: Shouldn't we be checking SCS_typedef here?
460 if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000461 TypeQuals && Result->isReferenceType()) {
John McCall0953e762009-09-24 19:53:00 +0000462 TypeQuals &= ~DeclSpec::TQ_const;
463 TypeQuals &= ~DeclSpec::TQ_volatile;
Mike Stump1eb44332009-09-09 15:08:12 +0000464 }
465
John McCall0953e762009-09-24 19:53:00 +0000466 Qualifiers Quals = Qualifiers::fromCVRMask(TypeQuals);
467 Result = Context.getQualifiedType(Result, Quals);
Chris Lattner96b77fc2008-04-02 06:50:17 +0000468 }
John McCall0953e762009-09-24 19:53:00 +0000469
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000470 return Result;
471}
472
Douglas Gregorcd281c32009-02-28 00:25:32 +0000473static std::string getPrintableNameForEntity(DeclarationName Entity) {
474 if (Entity)
475 return Entity.getAsString();
Mike Stump1eb44332009-09-09 15:08:12 +0000476
Douglas Gregorcd281c32009-02-28 00:25:32 +0000477 return "type name";
478}
479
John McCall28654742010-06-05 06:41:15 +0000480QualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc,
481 Qualifiers Qs) {
482 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
483 // object or incomplete types shall not be restrict-qualified."
484 if (Qs.hasRestrict()) {
485 unsigned DiagID = 0;
486 QualType ProblemTy;
487
488 const Type *Ty = T->getCanonicalTypeInternal().getTypePtr();
489 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(Ty)) {
490 if (!RTy->getPointeeType()->isIncompleteOrObjectType()) {
491 DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
492 ProblemTy = T->getAs<ReferenceType>()->getPointeeType();
493 }
494 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
495 if (!PTy->getPointeeType()->isIncompleteOrObjectType()) {
496 DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
497 ProblemTy = T->getAs<PointerType>()->getPointeeType();
498 }
499 } else if (const MemberPointerType *PTy = dyn_cast<MemberPointerType>(Ty)) {
500 if (!PTy->getPointeeType()->isIncompleteOrObjectType()) {
501 DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
502 ProblemTy = T->getAs<PointerType>()->getPointeeType();
503 }
504 } else if (!Ty->isDependentType()) {
505 // FIXME: this deserves a proper diagnostic
506 DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
507 ProblemTy = T;
508 }
509
510 if (DiagID) {
511 Diag(Loc, DiagID) << ProblemTy;
512 Qs.removeRestrict();
513 }
514 }
515
516 return Context.getQualifiedType(T, Qs);
517}
518
Douglas Gregorcd281c32009-02-28 00:25:32 +0000519/// \brief Build a pointer type.
520///
521/// \param T The type to which we'll be building a pointer.
522///
Douglas Gregorcd281c32009-02-28 00:25:32 +0000523/// \param Loc The location of the entity whose type involves this
524/// pointer type or, if there is no such entity, the location of the
525/// type that will have pointer type.
526///
527/// \param Entity The name of the entity that involves the pointer
528/// type, if known.
529///
530/// \returns A suitable pointer type, if there are no
531/// errors. Otherwise, returns a NULL type.
John McCall28654742010-06-05 06:41:15 +0000532QualType Sema::BuildPointerType(QualType T,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000533 SourceLocation Loc, DeclarationName Entity) {
534 if (T->isReferenceType()) {
535 // C++ 8.3.2p4: There shall be no ... pointers to references ...
536 Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
John McCallac406052009-10-30 00:37:20 +0000537 << getPrintableNameForEntity(Entity) << T;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000538 return QualType();
539 }
540
John McCallc12c5bb2010-05-15 11:32:37 +0000541 assert(!T->isObjCObjectType() && "Should build ObjCObjectPointerType");
Douglas Gregor92e986e2010-04-22 16:44:27 +0000542
Douglas Gregorcd281c32009-02-28 00:25:32 +0000543 // Build the pointer type.
John McCall28654742010-06-05 06:41:15 +0000544 return Context.getPointerType(T);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000545}
546
547/// \brief Build a reference type.
548///
549/// \param T The type to which we'll be building a reference.
550///
Douglas Gregorcd281c32009-02-28 00:25:32 +0000551/// \param Loc The location of the entity whose type involves this
552/// reference type or, if there is no such entity, the location of the
553/// type that will have reference type.
554///
555/// \param Entity The name of the entity that involves the reference
556/// type, if known.
557///
558/// \returns A suitable reference type, if there are no
559/// errors. Otherwise, returns a NULL type.
John McCall54e14c42009-10-22 22:37:11 +0000560QualType Sema::BuildReferenceType(QualType T, bool SpelledAsLValue,
John McCall28654742010-06-05 06:41:15 +0000561 SourceLocation Loc,
John McCall54e14c42009-10-22 22:37:11 +0000562 DeclarationName Entity) {
John McCall54e14c42009-10-22 22:37:11 +0000563 bool LValueRef = SpelledAsLValue || T->getAs<LValueReferenceType>();
564
565 // C++0x [dcl.typedef]p9: If a typedef TD names a type that is a
566 // reference to a type T, and attempt to create the type "lvalue
567 // reference to cv TD" creates the type "lvalue reference to T".
568 // We use the qualifiers (restrict or none) of the original reference,
569 // not the new ones. This is consistent with GCC.
570
571 // C++ [dcl.ref]p4: There shall be no references to references.
572 //
573 // According to C++ DR 106, references to references are only
574 // diagnosed when they are written directly (e.g., "int & &"),
575 // but not when they happen via a typedef:
576 //
577 // typedef int& intref;
578 // typedef intref& intref2;
579 //
580 // Parser::ParseDeclaratorInternal diagnoses the case where
581 // references are written directly; here, we handle the
582 // collapsing of references-to-references as described in C++
583 // DR 106 and amended by C++ DR 540.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000584
585 // C++ [dcl.ref]p1:
Eli Friedman33a31382009-08-05 19:21:58 +0000586 // A declarator that specifies the type "reference to cv void"
Douglas Gregorcd281c32009-02-28 00:25:32 +0000587 // is ill-formed.
588 if (T->isVoidType()) {
589 Diag(Loc, diag::err_reference_to_void);
590 return QualType();
591 }
592
Douglas Gregorcd281c32009-02-28 00:25:32 +0000593 // Handle restrict on references.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000594 if (LValueRef)
John McCall28654742010-06-05 06:41:15 +0000595 return Context.getLValueReferenceType(T, SpelledAsLValue);
596 return Context.getRValueReferenceType(T);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000597}
598
599/// \brief Build an array type.
600///
601/// \param T The type of each element in the array.
602///
603/// \param ASM C99 array size modifier (e.g., '*', 'static').
Mike Stump1eb44332009-09-09 15:08:12 +0000604///
605/// \param ArraySize Expression describing the size of the array.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000606///
Douglas Gregorcd281c32009-02-28 00:25:32 +0000607/// \param Loc The location of the entity whose type involves this
608/// array type or, if there is no such entity, the location of the
609/// type that will have array type.
610///
611/// \param Entity The name of the entity that involves the array
612/// type, if known.
613///
614/// \returns A suitable array type, if there are no errors. Otherwise,
615/// returns a NULL type.
616QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
617 Expr *ArraySize, unsigned Quals,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000618 SourceRange Brackets, DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000619
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000620 SourceLocation Loc = Brackets.getBegin();
Sebastian Redl923d56d2009-11-05 15:52:31 +0000621 if (getLangOptions().CPlusPlus) {
Douglas Gregor138bb232010-04-27 19:38:14 +0000622 // C++ [dcl.array]p1:
623 // T is called the array element type; this type shall not be a reference
624 // type, the (possibly cv-qualified) type void, a function type or an
625 // abstract class type.
626 //
627 // Note: function types are handled in the common path with C.
628 if (T->isReferenceType()) {
629 Diag(Loc, diag::err_illegal_decl_array_of_references)
630 << getPrintableNameForEntity(Entity) << T;
631 return QualType();
632 }
633
Sebastian Redl923d56d2009-11-05 15:52:31 +0000634 if (T->isVoidType()) {
635 Diag(Loc, diag::err_illegal_decl_array_incomplete_type) << T;
636 return QualType();
637 }
Douglas Gregor138bb232010-04-27 19:38:14 +0000638
639 if (RequireNonAbstractType(Brackets.getBegin(), T,
640 diag::err_array_of_abstract_type))
641 return QualType();
642
Sebastian Redl923d56d2009-11-05 15:52:31 +0000643 } else {
Douglas Gregor138bb232010-04-27 19:38:14 +0000644 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
645 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
Sebastian Redl923d56d2009-11-05 15:52:31 +0000646 if (RequireCompleteType(Loc, T,
647 diag::err_illegal_decl_array_incomplete_type))
648 return QualType();
649 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000650
651 if (T->isFunctionType()) {
652 Diag(Loc, diag::err_illegal_decl_array_of_functions)
John McCallac406052009-10-30 00:37:20 +0000653 << getPrintableNameForEntity(Entity) << T;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000654 return QualType();
655 }
Mike Stump1eb44332009-09-09 15:08:12 +0000656
Anders Carlssone7cf07d2009-06-26 19:33:28 +0000657 if (Context.getCanonicalType(T) == Context.UndeducedAutoTy) {
Mike Stump1eb44332009-09-09 15:08:12 +0000658 Diag(Loc, diag::err_illegal_decl_array_of_auto)
Anders Carlssone7cf07d2009-06-26 19:33:28 +0000659 << getPrintableNameForEntity(Entity);
660 return QualType();
661 }
Mike Stump1eb44332009-09-09 15:08:12 +0000662
Ted Kremenek6217b802009-07-29 21:53:49 +0000663 if (const RecordType *EltTy = T->getAs<RecordType>()) {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000664 // If the element type is a struct or union that contains a variadic
665 // array, accept it as a GNU extension: C99 6.7.2.1p2.
666 if (EltTy->getDecl()->hasFlexibleArrayMember())
667 Diag(Loc, diag::ext_flexible_array_in_array) << T;
John McCallc12c5bb2010-05-15 11:32:37 +0000668 } else if (T->isObjCObjectType()) {
Chris Lattnerc7c11b12009-04-27 01:55:56 +0000669 Diag(Loc, diag::err_objc_array_of_interfaces) << T;
670 return QualType();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000671 }
Mike Stump1eb44332009-09-09 15:08:12 +0000672
Douglas Gregorcd281c32009-02-28 00:25:32 +0000673 // C99 6.7.5.2p1: The size expression shall have integer type.
674 if (ArraySize && !ArraySize->isTypeDependent() &&
675 !ArraySize->getType()->isIntegerType()) {
676 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
677 << ArraySize->getType() << ArraySize->getSourceRange();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000678 return QualType();
679 }
Douglas Gregor2767ce22010-08-18 00:39:00 +0000680 llvm::APSInt ConstVal(Context.getTypeSize(Context.getSizeType()));
Douglas Gregorcd281c32009-02-28 00:25:32 +0000681 if (!ArraySize) {
Eli Friedmanf91f5c82009-04-26 21:57:51 +0000682 if (ASM == ArrayType::Star)
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000683 T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets);
Eli Friedmanf91f5c82009-04-26 21:57:51 +0000684 else
685 T = Context.getIncompleteArrayType(T, ASM, Quals);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000686 } else if (ArraySize->isTypeDependent() || ArraySize->isValueDependent()) {
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000687 T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000688 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
Sebastian Redl923d56d2009-11-05 15:52:31 +0000689 (!T->isDependentType() && !T->isIncompleteType() &&
690 !T->isConstantSizeType())) {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000691 // Per C99, a variable array is an array with either a non-constant
692 // size or an element type that has a non-constant-size
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000693 T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000694 } else {
695 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
696 // have a value greater than zero.
Sebastian Redl923d56d2009-11-05 15:52:31 +0000697 if (ConstVal.isSigned() && ConstVal.isNegative()) {
698 Diag(ArraySize->getLocStart(),
699 diag::err_typecheck_negative_array_size)
700 << ArraySize->getSourceRange();
701 return QualType();
702 }
703 if (ConstVal == 0) {
Douglas Gregor02024a92010-03-28 02:42:43 +0000704 // GCC accepts zero sized static arrays. We allow them when
705 // we're not in a SFINAE context.
706 Diag(ArraySize->getLocStart(),
707 isSFINAEContext()? diag::err_typecheck_zero_array_size
708 : diag::ext_typecheck_zero_array_size)
Sebastian Redl923d56d2009-11-05 15:52:31 +0000709 << ArraySize->getSourceRange();
Douglas Gregor2767ce22010-08-18 00:39:00 +0000710 } else if (!T->isDependentType() && !T->isVariablyModifiedType() &&
711 !T->isIncompleteType()) {
712 // Is the array too large?
713 unsigned ActiveSizeBits
714 = ConstantArrayType::getNumAddressingBits(Context, T, ConstVal);
715 if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context))
716 Diag(ArraySize->getLocStart(), diag::err_array_too_large)
717 << ConstVal.toString(10)
718 << ArraySize->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000719 }
Douglas Gregor2767ce22010-08-18 00:39:00 +0000720
John McCall46a617a2009-10-16 00:14:28 +0000721 T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000722 }
David Chisnallaf407762010-01-11 23:08:08 +0000723 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
724 if (!getLangOptions().C99) {
Douglas Gregor0fddb972010-05-22 16:17:30 +0000725 if (T->isVariableArrayType()) {
726 // Prohibit the use of non-POD types in VLAs.
Douglas Gregor204ce172010-05-24 20:42:30 +0000727 if (!T->isDependentType() &&
728 !Context.getBaseElementType(T)->isPODType()) {
Douglas Gregor0fddb972010-05-22 16:17:30 +0000729 Diag(Loc, diag::err_vla_non_pod)
730 << Context.getBaseElementType(T);
731 return QualType();
732 }
Douglas Gregora481ec42010-05-23 19:57:01 +0000733 // Prohibit the use of VLAs during template argument deduction.
734 else if (isSFINAEContext()) {
735 Diag(Loc, diag::err_vla_in_sfinae);
736 return QualType();
737 }
Douglas Gregor0fddb972010-05-22 16:17:30 +0000738 // Just extwarn about VLAs.
739 else
740 Diag(Loc, diag::ext_vla);
741 } else if (ASM != ArrayType::Normal || Quals != 0)
Douglas Gregor043cad22009-09-11 00:18:58 +0000742 Diag(Loc,
743 getLangOptions().CPlusPlus? diag::err_c99_array_usage_cxx
744 : diag::ext_c99_array_usage);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000745 }
746
747 return T;
748}
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000749
750/// \brief Build an ext-vector type.
751///
752/// Run the required checks for the extended vector type.
John McCall9ae2f072010-08-23 23:25:46 +0000753QualType Sema::BuildExtVectorType(QualType T, Expr *ArraySize,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000754 SourceLocation AttrLoc) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000755 // unlike gcc's vector_size attribute, we do not allow vectors to be defined
756 // in conjunction with complex types (pointers, arrays, functions, etc.).
Mike Stump1eb44332009-09-09 15:08:12 +0000757 if (!T->isDependentType() &&
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000758 !T->isIntegerType() && !T->isRealFloatingType()) {
759 Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T;
760 return QualType();
761 }
762
John McCall9ae2f072010-08-23 23:25:46 +0000763 if (!ArraySize->isTypeDependent() && !ArraySize->isValueDependent()) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000764 llvm::APSInt vecSize(32);
John McCall9ae2f072010-08-23 23:25:46 +0000765 if (!ArraySize->isIntegerConstantExpr(vecSize, Context)) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000766 Diag(AttrLoc, diag::err_attribute_argument_not_int)
John McCall9ae2f072010-08-23 23:25:46 +0000767 << "ext_vector_type" << ArraySize->getSourceRange();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000768 return QualType();
769 }
Mike Stump1eb44332009-09-09 15:08:12 +0000770
771 // unlike gcc's vector_size attribute, the size is specified as the
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000772 // number of elements, not the number of bytes.
Mike Stump1eb44332009-09-09 15:08:12 +0000773 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
774
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000775 if (vectorSize == 0) {
776 Diag(AttrLoc, diag::err_attribute_zero_size)
John McCall9ae2f072010-08-23 23:25:46 +0000777 << ArraySize->getSourceRange();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000778 return QualType();
779 }
Mike Stump1eb44332009-09-09 15:08:12 +0000780
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000781 if (!T->isDependentType())
782 return Context.getExtVectorType(T, vectorSize);
Mike Stump1eb44332009-09-09 15:08:12 +0000783 }
784
John McCall9ae2f072010-08-23 23:25:46 +0000785 return Context.getDependentSizedExtVectorType(T, ArraySize, AttrLoc);
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000786}
Mike Stump1eb44332009-09-09 15:08:12 +0000787
Douglas Gregor724651c2009-02-28 01:04:19 +0000788/// \brief Build a function type.
789///
790/// This routine checks the function type according to C++ rules and
791/// under the assumption that the result type and parameter types have
792/// just been instantiated from a template. It therefore duplicates
Douglas Gregor2943aed2009-03-03 04:44:36 +0000793/// some of the behavior of GetTypeForDeclarator, but in a much
Douglas Gregor724651c2009-02-28 01:04:19 +0000794/// simpler form that is only suitable for this narrow use case.
795///
796/// \param T The return type of the function.
797///
798/// \param ParamTypes The parameter types of the function. This array
799/// will be modified to account for adjustments to the types of the
800/// function parameters.
801///
802/// \param NumParamTypes The number of parameter types in ParamTypes.
803///
804/// \param Variadic Whether this is a variadic function type.
805///
806/// \param Quals The cvr-qualifiers to be applied to the function type.
807///
808/// \param Loc The location of the entity whose type involves this
809/// function type or, if there is no such entity, the location of the
810/// type that will have function type.
811///
812/// \param Entity The name of the entity that involves the function
813/// type, if known.
814///
815/// \returns A suitable function type, if there are no
816/// errors. Otherwise, returns a NULL type.
817QualType Sema::BuildFunctionType(QualType T,
Mike Stump1eb44332009-09-09 15:08:12 +0000818 QualType *ParamTypes,
Douglas Gregor724651c2009-02-28 01:04:19 +0000819 unsigned NumParamTypes,
820 bool Variadic, unsigned Quals,
Eli Friedmanfa869542010-08-05 02:54:05 +0000821 SourceLocation Loc, DeclarationName Entity,
822 const FunctionType::ExtInfo &Info) {
Douglas Gregor724651c2009-02-28 01:04:19 +0000823 if (T->isArrayType() || T->isFunctionType()) {
Douglas Gregor58408bc2010-01-11 18:46:21 +0000824 Diag(Loc, diag::err_func_returning_array_function)
825 << T->isFunctionType() << T;
Douglas Gregor724651c2009-02-28 01:04:19 +0000826 return QualType();
827 }
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000828
Douglas Gregor724651c2009-02-28 01:04:19 +0000829 bool Invalid = false;
830 for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) {
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000831 QualType ParamType = adjustParameterType(ParamTypes[Idx]);
832 if (ParamType->isVoidType()) {
Douglas Gregor724651c2009-02-28 01:04:19 +0000833 Diag(Loc, diag::err_param_with_void_type);
834 Invalid = true;
835 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000836
John McCall54e14c42009-10-22 22:37:11 +0000837 ParamTypes[Idx] = ParamType;
Douglas Gregor724651c2009-02-28 01:04:19 +0000838 }
839
840 if (Invalid)
841 return QualType();
842
Mike Stump1eb44332009-09-09 15:08:12 +0000843 return Context.getFunctionType(T, ParamTypes, NumParamTypes, Variadic,
Eli Friedmanfa869542010-08-05 02:54:05 +0000844 Quals, false, false, 0, 0, Info);
Douglas Gregor724651c2009-02-28 01:04:19 +0000845}
Mike Stump1eb44332009-09-09 15:08:12 +0000846
Douglas Gregor949bf692009-06-09 22:17:39 +0000847/// \brief Build a member pointer type \c T Class::*.
848///
849/// \param T the type to which the member pointer refers.
850/// \param Class the class type into which the member pointer points.
John McCall0953e762009-09-24 19:53:00 +0000851/// \param CVR Qualifiers applied to the member pointer type
Douglas Gregor949bf692009-06-09 22:17:39 +0000852/// \param Loc the location where this type begins
853/// \param Entity the name of the entity that will have this member pointer type
854///
855/// \returns a member pointer type, if successful, or a NULL type if there was
856/// an error.
Mike Stump1eb44332009-09-09 15:08:12 +0000857QualType Sema::BuildMemberPointerType(QualType T, QualType Class,
John McCall28654742010-06-05 06:41:15 +0000858 SourceLocation Loc,
Douglas Gregor949bf692009-06-09 22:17:39 +0000859 DeclarationName Entity) {
860 // Verify that we're not building a pointer to pointer to function with
861 // exception specification.
862 if (CheckDistantExceptionSpec(T)) {
863 Diag(Loc, diag::err_distant_exception_spec);
864
865 // FIXME: If we're doing this as part of template instantiation,
866 // we should return immediately.
867
868 // Build the type anyway, but use the canonical type so that the
869 // exception specifiers are stripped off.
870 T = Context.getCanonicalType(T);
871 }
872
Sebastian Redl73780122010-06-09 21:19:43 +0000873 // C++ 8.3.3p3: A pointer to member shall not point to ... a member
Douglas Gregor949bf692009-06-09 22:17:39 +0000874 // with reference type, or "cv void."
875 if (T->isReferenceType()) {
Anders Carlsson8d4655d2009-06-30 00:06:57 +0000876 Diag(Loc, diag::err_illegal_decl_mempointer_to_reference)
John McCallac406052009-10-30 00:37:20 +0000877 << (Entity? Entity.getAsString() : "type name") << T;
Douglas Gregor949bf692009-06-09 22:17:39 +0000878 return QualType();
879 }
880
881 if (T->isVoidType()) {
882 Diag(Loc, diag::err_illegal_decl_mempointer_to_void)
883 << (Entity? Entity.getAsString() : "type name");
884 return QualType();
885 }
886
Douglas Gregor949bf692009-06-09 22:17:39 +0000887 if (!Class->isDependentType() && !Class->isRecordType()) {
888 Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class;
889 return QualType();
890 }
891
Charles Davisd18f9f92010-08-16 04:01:50 +0000892 // In the Microsoft ABI, the class is allowed to be an incomplete
893 // type. In such cases, the compiler makes a worst-case assumption.
894 // We make no such assumption right now, so emit an error if the
895 // class isn't a complete type.
Charles Davis20cf7172010-08-19 02:18:14 +0000896 if (Context.Target.getCXXABI() == CXXABI_Microsoft &&
Charles Davisd18f9f92010-08-16 04:01:50 +0000897 RequireCompleteType(Loc, Class, diag::err_incomplete_type))
898 return QualType();
899
John McCall28654742010-06-05 06:41:15 +0000900 return Context.getMemberPointerType(T, Class.getTypePtr());
Douglas Gregor949bf692009-06-09 22:17:39 +0000901}
Mike Stump1eb44332009-09-09 15:08:12 +0000902
Anders Carlsson9a917e42009-06-12 22:56:54 +0000903/// \brief Build a block pointer type.
904///
905/// \param T The type to which we'll be building a block pointer.
906///
John McCall0953e762009-09-24 19:53:00 +0000907/// \param CVR The cvr-qualifiers to be applied to the block pointer type.
Anders Carlsson9a917e42009-06-12 22:56:54 +0000908///
909/// \param Loc The location of the entity whose type involves this
910/// block pointer type or, if there is no such entity, the location of the
911/// type that will have block pointer type.
912///
913/// \param Entity The name of the entity that involves the block pointer
914/// type, if known.
915///
916/// \returns A suitable block pointer type, if there are no
917/// errors. Otherwise, returns a NULL type.
John McCall28654742010-06-05 06:41:15 +0000918QualType Sema::BuildBlockPointerType(QualType T,
Mike Stump1eb44332009-09-09 15:08:12 +0000919 SourceLocation Loc,
Anders Carlsson9a917e42009-06-12 22:56:54 +0000920 DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000921 if (!T->isFunctionType()) {
Anders Carlsson9a917e42009-06-12 22:56:54 +0000922 Diag(Loc, diag::err_nonfunction_block_type);
923 return QualType();
924 }
Mike Stump1eb44332009-09-09 15:08:12 +0000925
John McCall28654742010-06-05 06:41:15 +0000926 return Context.getBlockPointerType(T);
Anders Carlsson9a917e42009-06-12 22:56:54 +0000927}
928
John McCallb3d87482010-08-24 05:47:05 +0000929QualType Sema::GetTypeFromParser(ParsedType Ty, TypeSourceInfo **TInfo) {
930 QualType QT = Ty.get();
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000931 if (QT.isNull()) {
John McCalla93c9342009-12-07 02:54:59 +0000932 if (TInfo) *TInfo = 0;
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000933 return QualType();
934 }
935
John McCalla93c9342009-12-07 02:54:59 +0000936 TypeSourceInfo *DI = 0;
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000937 if (LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) {
938 QT = LIT->getType();
John McCalla93c9342009-12-07 02:54:59 +0000939 DI = LIT->getTypeSourceInfo();
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000940 }
Mike Stump1eb44332009-09-09 15:08:12 +0000941
John McCalla93c9342009-12-07 02:54:59 +0000942 if (TInfo) *TInfo = DI;
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000943 return QT;
944}
945
Mike Stump98eb8a72009-02-04 22:31:32 +0000946/// GetTypeForDeclarator - Convert the type for the specified
Sebastian Redl8ce35b02009-10-25 21:45:37 +0000947/// declarator to Type instances.
Douglas Gregor402abb52009-05-28 23:31:59 +0000948///
949/// If OwnedDecl is non-NULL, and this declarator's decl-specifier-seq
950/// owns the declaration of a type (e.g., the definition of a struct
951/// type), then *OwnedDecl will receive the owned declaration.
John McCallbf1a0282010-06-04 23:28:52 +0000952///
953/// The result of this call will never be null, but the associated
954/// type may be a null type if there's an unrecoverable error.
955TypeSourceInfo *Sema::GetTypeForDeclarator(Declarator &D, Scope *S,
956 TagDecl **OwnedDecl) {
Douglas Gregor930d8b52009-01-30 22:09:00 +0000957 // Determine the type of the declarator. Not all forms of declarator
958 // have a type.
959 QualType T;
Douglas Gregor05baacb2010-04-12 23:19:01 +0000960 TypeSourceInfo *ReturnTypeInfo = 0;
961
John McCall04a67a62010-02-05 21:31:56 +0000962 llvm::SmallVector<DelayedAttribute,4> FnAttrsFromDeclSpec;
963
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000964 switch (D.getName().getKind()) {
965 case UnqualifiedId::IK_Identifier:
966 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunt0486d742009-11-28 04:44:28 +0000967 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000968 case UnqualifiedId::IK_TemplateId:
John McCall04a67a62010-02-05 21:31:56 +0000969 T = ConvertDeclSpecToType(*this, D, FnAttrsFromDeclSpec);
Chris Lattner5db2bb12009-10-25 18:21:37 +0000970
Douglas Gregor591bd3c2010-02-08 22:07:33 +0000971 if (!D.isInvalidType() && D.getDeclSpec().isTypeSpecOwned()) {
John McCallb3d87482010-08-24 05:47:05 +0000972 TagDecl* Owned = cast<TagDecl>(D.getDeclSpec().getRepAsDecl());
Douglas Gregorb37b6482010-02-12 17:40:34 +0000973 // Owned is embedded if it was defined here, or if it is the
974 // very first (i.e., canonical) declaration of this tag type.
975 Owned->setEmbeddedInDeclarator(Owned->isDefinition() ||
976 Owned->isCanonicalDecl());
Douglas Gregor591bd3c2010-02-08 22:07:33 +0000977 if (OwnedDecl) *OwnedDecl = Owned;
978 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000979 break;
980
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000981 case UnqualifiedId::IK_ConstructorName:
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000982 case UnqualifiedId::IK_ConstructorTemplateId:
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000983 case UnqualifiedId::IK_DestructorName:
Douglas Gregor930d8b52009-01-30 22:09:00 +0000984 // Constructors and destructors don't have return types. Use
Douglas Gregor48026d22010-01-11 18:40:55 +0000985 // "void" instead.
Douglas Gregor930d8b52009-01-30 22:09:00 +0000986 T = Context.VoidTy;
987 break;
Douglas Gregor48026d22010-01-11 18:40:55 +0000988
989 case UnqualifiedId::IK_ConversionFunctionId:
990 // The result type of a conversion function is the type that it
991 // converts to.
Douglas Gregor05baacb2010-04-12 23:19:01 +0000992 T = GetTypeFromParser(D.getName().ConversionFunctionId,
John McCallbf1a0282010-06-04 23:28:52 +0000993 &ReturnTypeInfo);
Douglas Gregor48026d22010-01-11 18:40:55 +0000994 break;
Douglas Gregor930d8b52009-01-30 22:09:00 +0000995 }
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000996
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000997 if (T.isNull())
John McCallbf1a0282010-06-04 23:28:52 +0000998 return Context.getNullTypeSourceInfo();
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000999
Anders Carlssonbaf45d32009-06-26 22:18:59 +00001000 if (T == Context.UndeducedAutoTy) {
1001 int Error = -1;
Mike Stump1eb44332009-09-09 15:08:12 +00001002
Anders Carlssonbaf45d32009-06-26 22:18:59 +00001003 switch (D.getContext()) {
1004 case Declarator::KNRTypeListContext:
1005 assert(0 && "K&R type lists aren't allowed in C++");
1006 break;
Anders Carlssonbaf45d32009-06-26 22:18:59 +00001007 case Declarator::PrototypeContext:
1008 Error = 0; // Function prototype
1009 break;
1010 case Declarator::MemberContext:
1011 switch (cast<TagDecl>(CurContext)->getTagKind()) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001012 case TTK_Enum: assert(0 && "unhandled tag kind"); break;
1013 case TTK_Struct: Error = 1; /* Struct member */ break;
1014 case TTK_Union: Error = 2; /* Union member */ break;
1015 case TTK_Class: Error = 3; /* Class member */ break;
Mike Stump1eb44332009-09-09 15:08:12 +00001016 }
Anders Carlssonbaf45d32009-06-26 22:18:59 +00001017 break;
1018 case Declarator::CXXCatchContext:
1019 Error = 4; // Exception declaration
1020 break;
1021 case Declarator::TemplateParamContext:
1022 Error = 5; // Template parameter
1023 break;
1024 case Declarator::BlockLiteralContext:
1025 Error = 6; // Block literal
1026 break;
1027 case Declarator::FileContext:
1028 case Declarator::BlockContext:
1029 case Declarator::ForContext:
1030 case Declarator::ConditionContext:
1031 case Declarator::TypeNameContext:
1032 break;
1033 }
1034
1035 if (Error != -1) {
1036 Diag(D.getDeclSpec().getTypeSpecTypeLoc(), diag::err_auto_not_allowed)
1037 << Error;
1038 T = Context.IntTy;
1039 D.setInvalidType(true);
1040 }
1041 }
Mike Stump1eb44332009-09-09 15:08:12 +00001042
Douglas Gregorcd281c32009-02-28 00:25:32 +00001043 // The name we're declaring, if any.
1044 DeclarationName Name;
1045 if (D.getIdentifier())
1046 Name = D.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00001047
John McCall04a67a62010-02-05 21:31:56 +00001048 llvm::SmallVector<DelayedAttribute,4> FnAttrsFromPreviousChunk;
1049
Mike Stump98eb8a72009-02-04 22:31:32 +00001050 // Walk the DeclTypeInfo, building the recursive type as we go.
1051 // DeclTypeInfos are ordered from the identifier out, which is
1052 // opposite of what we want :).
Sebastian Redl8ce35b02009-10-25 21:45:37 +00001053 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
1054 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1);
Reid Spencer5f016e22007-07-11 17:01:13 +00001055 switch (DeclType.Kind) {
1056 default: assert(0 && "Unknown decltype!");
Steve Naroff5618bd42008-08-27 16:04:49 +00001057 case DeclaratorChunk::BlockPointer:
Chris Lattner9af55002009-03-27 04:18:06 +00001058 // If blocks are disabled, emit an error.
1059 if (!LangOpts.Blocks)
1060 Diag(DeclType.Loc, diag::err_blocks_disable);
Mike Stump1eb44332009-09-09 15:08:12 +00001061
John McCall28654742010-06-05 06:41:15 +00001062 T = BuildBlockPointerType(T, D.getIdentifierLoc(), Name);
1063 if (DeclType.Cls.TypeQuals)
1064 T = BuildQualifiedType(T, DeclType.Loc, DeclType.Cls.TypeQuals);
Steve Naroff5618bd42008-08-27 16:04:49 +00001065 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001066 case DeclaratorChunk::Pointer:
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001067 // Verify that we're not building a pointer to pointer to function with
1068 // exception specification.
1069 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1070 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1071 D.setInvalidType(true);
1072 // Build the type anyway.
1073 }
John McCallc12c5bb2010-05-15 11:32:37 +00001074 if (getLangOptions().ObjC1 && T->getAs<ObjCObjectType>()) {
1075 T = Context.getObjCObjectPointerType(T);
John McCall28654742010-06-05 06:41:15 +00001076 if (DeclType.Ptr.TypeQuals)
1077 T = BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals);
Steve Naroff14108da2009-07-10 23:34:53 +00001078 break;
1079 }
John McCall28654742010-06-05 06:41:15 +00001080 T = BuildPointerType(T, DeclType.Loc, Name);
1081 if (DeclType.Ptr.TypeQuals)
1082 T = BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001083 break;
John McCall0953e762009-09-24 19:53:00 +00001084 case DeclaratorChunk::Reference: {
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001085 // Verify that we're not building a reference to pointer to function with
1086 // exception specification.
1087 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1088 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1089 D.setInvalidType(true);
1090 // Build the type anyway.
1091 }
John McCall28654742010-06-05 06:41:15 +00001092 T = BuildReferenceType(T, DeclType.Ref.LValueRef, DeclType.Loc, Name);
1093
1094 Qualifiers Quals;
1095 if (DeclType.Ref.HasRestrict)
1096 T = BuildQualifiedType(T, DeclType.Loc, Qualifiers::Restrict);
Reid Spencer5f016e22007-07-11 17:01:13 +00001097 break;
John McCall0953e762009-09-24 19:53:00 +00001098 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001099 case DeclaratorChunk::Array: {
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001100 // Verify that we're not building an array of pointers to function with
1101 // exception specification.
1102 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1103 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1104 D.setInvalidType(true);
1105 // Build the type anyway.
1106 }
Chris Lattnerfd89bc82008-04-02 01:05:10 +00001107 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner94f81fd2007-08-28 16:54:00 +00001108 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001109 ArrayType::ArraySizeModifier ASM;
1110 if (ATI.isStar)
1111 ASM = ArrayType::Star;
1112 else if (ATI.hasStatic)
1113 ASM = ArrayType::Static;
1114 else
1115 ASM = ArrayType::Normal;
Eli Friedmanf91f5c82009-04-26 21:57:51 +00001116 if (ASM == ArrayType::Star &&
1117 D.getContext() != Declarator::PrototypeContext) {
1118 // FIXME: This check isn't quite right: it allows star in prototypes
1119 // for function definitions, and disallows some edge cases detailed
1120 // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html
1121 Diag(DeclType.Loc, diag::err_array_star_outside_prototype);
1122 ASM = ArrayType::Normal;
1123 D.setInvalidType(true);
1124 }
John McCall0953e762009-09-24 19:53:00 +00001125 T = BuildArrayType(T, ASM, ArraySize,
1126 Qualifiers::fromCVRMask(ATI.TypeQuals),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001127 SourceRange(DeclType.Loc, DeclType.EndLoc), Name);
Reid Spencer5f016e22007-07-11 17:01:13 +00001128 break;
1129 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001130 case DeclaratorChunk::Function: {
Reid Spencer5f016e22007-07-11 17:01:13 +00001131 // If the function declarator has a prototype (i.e. it is not () and
1132 // does not have a K&R-style identifier list), then the arguments are part
1133 // of the type, otherwise the argument list is ().
1134 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Sebastian Redl3cc97262009-05-31 11:47:27 +00001135
Chris Lattnercd881292007-12-19 05:31:29 +00001136 // C99 6.7.5.3p1: The return type may not be a function or array type.
Douglas Gregor58408bc2010-01-11 18:46:21 +00001137 // For conversion functions, we'll diagnose this particular error later.
Douglas Gregor48026d22010-01-11 18:40:55 +00001138 if ((T->isArrayType() || T->isFunctionType()) &&
1139 (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId)) {
Douglas Gregor58408bc2010-01-11 18:46:21 +00001140 Diag(DeclType.Loc, diag::err_func_returning_array_function)
1141 << T->isFunctionType() << T;
Chris Lattnercd881292007-12-19 05:31:29 +00001142 T = Context.IntTy;
1143 D.setInvalidType(true);
1144 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001145
Douglas Gregor5291c3c2010-07-13 08:18:22 +00001146 // cv-qualifiers on return types are pointless except when the type is a
1147 // class type in C++.
1148 if (T.getCVRQualifiers() && D.getDeclSpec().getTypeQualifiers() &&
1149 (!getLangOptions().CPlusPlus ||
1150 (!T->isDependentType() && !T->isRecordType()))) {
1151 unsigned Quals = D.getDeclSpec().getTypeQualifiers();
Douglas Gregorde80ec12010-07-13 08:50:30 +00001152 std::string QualStr;
1153 unsigned NumQuals = 0;
Douglas Gregor5291c3c2010-07-13 08:18:22 +00001154 SourceLocation Loc;
Douglas Gregorde80ec12010-07-13 08:50:30 +00001155 if (Quals & Qualifiers::Const) {
Douglas Gregor5291c3c2010-07-13 08:18:22 +00001156 Loc = D.getDeclSpec().getConstSpecLoc();
Douglas Gregorde80ec12010-07-13 08:50:30 +00001157 ++NumQuals;
1158 QualStr = "const";
Douglas Gregor5291c3c2010-07-13 08:18:22 +00001159 }
Douglas Gregorde80ec12010-07-13 08:50:30 +00001160 if (Quals & Qualifiers::Volatile) {
1161 if (NumQuals == 0) {
1162 Loc = D.getDeclSpec().getVolatileSpecLoc();
1163 QualStr = "volatile";
1164 } else
1165 QualStr += " volatile";
1166 ++NumQuals;
1167 }
1168 if (Quals & Qualifiers::Restrict) {
1169 if (NumQuals == 0) {
1170 Loc = D.getDeclSpec().getRestrictSpecLoc();
1171 QualStr = "restrict";
1172 } else
1173 QualStr += " restrict";
1174 ++NumQuals;
1175 }
1176 assert(NumQuals > 0 && "No known qualifiers?");
1177
Douglas Gregor5291c3c2010-07-13 08:18:22 +00001178 SemaDiagnosticBuilder DB = Diag(Loc, diag::warn_qual_return_type);
Douglas Gregorde80ec12010-07-13 08:50:30 +00001179 DB << QualStr << NumQuals;
Douglas Gregor5291c3c2010-07-13 08:18:22 +00001180 if (Quals & Qualifiers::Const)
1181 DB << FixItHint::CreateRemoval(D.getDeclSpec().getConstSpecLoc());
1182 if (Quals & Qualifiers::Volatile)
1183 DB << FixItHint::CreateRemoval(D.getDeclSpec().getVolatileSpecLoc());
1184 if (Quals & Qualifiers::Restrict)
1185 DB << FixItHint::CreateRemoval(D.getDeclSpec().getRestrictSpecLoc());
1186 }
1187
Douglas Gregor402abb52009-05-28 23:31:59 +00001188 if (getLangOptions().CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) {
1189 // C++ [dcl.fct]p6:
1190 // Types shall not be defined in return or parameter types.
John McCallb3d87482010-08-24 05:47:05 +00001191 TagDecl *Tag = cast<TagDecl>(D.getDeclSpec().getRepAsDecl());
Douglas Gregor402abb52009-05-28 23:31:59 +00001192 if (Tag->isDefinition())
1193 Diag(Tag->getLocation(), diag::err_type_defined_in_result_type)
1194 << Context.getTypeDeclType(Tag);
1195 }
1196
Sebastian Redl3cc97262009-05-31 11:47:27 +00001197 // Exception specs are not allowed in typedefs. Complain, but add it
1198 // anyway.
1199 if (FTI.hasExceptionSpec &&
1200 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
1201 Diag(FTI.getThrowLoc(), diag::err_exception_spec_in_typedef);
1202
John McCall28654742010-06-05 06:41:15 +00001203 if (!FTI.NumArgs && !FTI.isVariadic && !getLangOptions().CPlusPlus) {
1204 // Simple void foo(), where the incoming T is the result type.
1205 T = Context.getFunctionNoProtoType(T);
1206 } else {
1207 // We allow a zero-parameter variadic function in C if the
1208 // function is marked with the "overloadable" attribute. Scan
1209 // for this attribute now.
1210 if (!FTI.NumArgs && FTI.isVariadic && !getLangOptions().CPlusPlus) {
Douglas Gregor965acbb2009-02-18 07:07:28 +00001211 bool Overloadable = false;
1212 for (const AttributeList *Attrs = D.getAttributes();
1213 Attrs; Attrs = Attrs->getNext()) {
1214 if (Attrs->getKind() == AttributeList::AT_overloadable) {
1215 Overloadable = true;
1216 break;
1217 }
1218 }
1219
1220 if (!Overloadable)
1221 Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001222 }
John McCall28654742010-06-05 06:41:15 +00001223
1224 if (FTI.NumArgs && FTI.ArgInfo[0].Param == 0) {
Chris Lattner788b0fd2010-06-23 06:00:24 +00001225 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function
1226 // definition.
John McCall28654742010-06-05 06:41:15 +00001227 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
1228 D.setInvalidType(true);
1229 break;
1230 }
1231
Reid Spencer5f016e22007-07-11 17:01:13 +00001232 // Otherwise, we have a function with an argument list that is
1233 // potentially variadic.
1234 llvm::SmallVector<QualType, 16> ArgTys;
John McCall28654742010-06-05 06:41:15 +00001235 ArgTys.reserve(FTI.NumArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001236
Reid Spencer5f016e22007-07-11 17:01:13 +00001237 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00001238 ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param);
Chris Lattner8123a952008-04-10 02:22:51 +00001239 QualType ArgTy = Param->getType();
Chris Lattner78c75fb2007-07-21 05:30:18 +00001240 assert(!ArgTy.isNull() && "Couldn't parse type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001241
1242 // Adjust the parameter type.
Douglas Gregorbeb58cb2009-03-23 23:17:00 +00001243 assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001244
Reid Spencer5f016e22007-07-11 17:01:13 +00001245 // Look for 'void'. void is allowed only as a single argument to a
1246 // function with no other parameters (C99 6.7.5.3p10). We record
Douglas Gregor72564e72009-02-26 23:50:07 +00001247 // int(void) as a FunctionProtoType with an empty argument list.
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001248 if (ArgTy->isVoidType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001249 // If this is something like 'float(int, void)', reject it. 'void'
1250 // is an incomplete type (C99 6.2.5p19) and function decls cannot
1251 // have arguments of incomplete type.
1252 if (FTI.NumArgs != 1 || FTI.isVariadic) {
1253 Diag(DeclType.Loc, diag::err_void_only_param);
Chris Lattner2ff54262007-07-21 05:18:12 +00001254 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00001255 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00001256 } else if (FTI.ArgInfo[i].Ident) {
1257 // Reject, but continue to parse 'int(void abc)'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001258 Diag(FTI.ArgInfo[i].IdentLoc,
Chris Lattner4565d4e2007-07-21 05:26:43 +00001259 diag::err_param_with_void_type);
Chris Lattner2ff54262007-07-21 05:18:12 +00001260 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00001261 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00001262 } else {
1263 // Reject, but continue to parse 'float(const void)'.
John McCall0953e762009-09-24 19:53:00 +00001264 if (ArgTy.hasQualifiers())
Chris Lattner2ff54262007-07-21 05:18:12 +00001265 Diag(DeclType.Loc, diag::err_void_param_qualified);
Mike Stump1eb44332009-09-09 15:08:12 +00001266
Chris Lattner2ff54262007-07-21 05:18:12 +00001267 // Do not add 'void' to the ArgTys list.
1268 break;
1269 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001270 } else if (!FTI.hasPrototype) {
1271 if (ArgTy->isPromotableIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00001272 ArgTy = Context.getPromotedIntegerType(ArgTy);
John McCall183700f2009-09-21 23:43:11 +00001273 } else if (const BuiltinType* BTy = ArgTy->getAs<BuiltinType>()) {
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001274 if (BTy->getKind() == BuiltinType::Float)
1275 ArgTy = Context.DoubleTy;
1276 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001277 }
Mike Stump1eb44332009-09-09 15:08:12 +00001278
John McCall54e14c42009-10-22 22:37:11 +00001279 ArgTys.push_back(ArgTy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001280 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001281
1282 llvm::SmallVector<QualType, 4> Exceptions;
1283 Exceptions.reserve(FTI.NumExceptions);
Mike Stump1eb44332009-09-09 15:08:12 +00001284 for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001285 // FIXME: Preserve type source info.
1286 QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty);
Sebastian Redlef65f062009-05-29 18:02:33 +00001287 // Check that the type is valid for an exception spec, and drop it if
1288 // not.
1289 if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1290 Exceptions.push_back(ET);
1291 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001292
Jay Foadbeaaccd2009-05-21 09:52:38 +00001293 T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(),
Sebastian Redl465226e2009-05-27 22:11:52 +00001294 FTI.isVariadic, FTI.TypeQuals,
1295 FTI.hasExceptionSpec,
1296 FTI.hasAnyExceptionSpec,
Douglas Gregorce056bc2010-02-21 22:15:06 +00001297 Exceptions.size(), Exceptions.data(),
Rafael Espindola264ba482010-03-30 20:24:48 +00001298 FunctionType::ExtInfo());
Reid Spencer5f016e22007-07-11 17:01:13 +00001299 }
John McCall04a67a62010-02-05 21:31:56 +00001300
1301 // For GCC compatibility, we allow attributes that apply only to
1302 // function types to be placed on a function's return type
1303 // instead (as long as that type doesn't happen to be function
1304 // or function-pointer itself).
1305 ProcessDelayedFnAttrs(*this, T, FnAttrsFromPreviousChunk);
1306
Reid Spencer5f016e22007-07-11 17:01:13 +00001307 break;
1308 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001309 case DeclaratorChunk::MemberPointer:
1310 // The scope spec must refer to a class, or be dependent.
Abramo Bagnara7bd06762010-08-13 12:56:25 +00001311 CXXScopeSpec &SS = DeclType.Mem.Scope();
Sebastian Redlf30208a2009-01-24 21:16:55 +00001312 QualType ClsType;
Abramo Bagnara7bd06762010-08-13 12:56:25 +00001313 if (SS.isInvalid()) {
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00001314 // Avoid emitting extra errors if we already errored on the scope.
1315 D.setInvalidType(true);
Abramo Bagnara7bd06762010-08-13 12:56:25 +00001316 } else if (isDependentScopeSpecifier(SS) ||
1317 dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS))) {
Mike Stump1eb44332009-09-09 15:08:12 +00001318 NestedNameSpecifier *NNS
Abramo Bagnara7bd06762010-08-13 12:56:25 +00001319 = static_cast<NestedNameSpecifier*>(SS.getScopeRep());
Douglas Gregor87c12c42009-11-04 16:49:01 +00001320 NestedNameSpecifier *NNSPrefix = NNS->getPrefix();
1321 switch (NNS->getKind()) {
1322 case NestedNameSpecifier::Identifier:
Abramo Bagnara7bd06762010-08-13 12:56:25 +00001323 ClsType = Context.getDependentNameType(ETK_None, NNSPrefix,
Douglas Gregor4a2023f2010-03-31 20:19:30 +00001324 NNS->getAsIdentifier());
Douglas Gregor87c12c42009-11-04 16:49:01 +00001325 break;
1326
1327 case NestedNameSpecifier::Namespace:
1328 case NestedNameSpecifier::Global:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001329 llvm_unreachable("Nested-name-specifier must name a type");
Douglas Gregor87c12c42009-11-04 16:49:01 +00001330 break;
Abramo Bagnara7bd06762010-08-13 12:56:25 +00001331
Douglas Gregor87c12c42009-11-04 16:49:01 +00001332 case NestedNameSpecifier::TypeSpec:
1333 case NestedNameSpecifier::TypeSpecWithTemplate:
1334 ClsType = QualType(NNS->getAsType(), 0);
Abramo Bagnara7bd06762010-08-13 12:56:25 +00001335 // Note: if NNS is dependent, then its prefix (if any) is already
1336 // included in ClsType; this does not hold if the NNS is
1337 // nondependent: in this case (if there is indeed a prefix)
1338 // ClsType needs to be wrapped into an elaborated type.
1339 if (NNSPrefix && !NNS->isDependent())
1340 ClsType = Context.getElaboratedType(ETK_None, NNSPrefix, ClsType);
Douglas Gregor87c12c42009-11-04 16:49:01 +00001341 break;
1342 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001343 } else {
Douglas Gregor949bf692009-06-09 22:17:39 +00001344 Diag(DeclType.Mem.Scope().getBeginLoc(),
1345 diag::err_illegal_decl_mempointer_in_nonclass)
1346 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
1347 << DeclType.Mem.Scope().getRange();
Sebastian Redlf30208a2009-01-24 21:16:55 +00001348 D.setInvalidType(true);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001349 }
1350
Douglas Gregor949bf692009-06-09 22:17:39 +00001351 if (!ClsType.isNull())
John McCall28654742010-06-05 06:41:15 +00001352 T = BuildMemberPointerType(T, ClsType, DeclType.Loc, D.getIdentifier());
Douglas Gregor949bf692009-06-09 22:17:39 +00001353 if (T.isNull()) {
1354 T = Context.IntTy;
Sebastian Redlf30208a2009-01-24 21:16:55 +00001355 D.setInvalidType(true);
John McCall28654742010-06-05 06:41:15 +00001356 } else if (DeclType.Mem.TypeQuals) {
1357 T = BuildQualifiedType(T, DeclType.Loc, DeclType.Mem.TypeQuals);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001358 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001359 break;
1360 }
1361
Douglas Gregorcd281c32009-02-28 00:25:32 +00001362 if (T.isNull()) {
1363 D.setInvalidType(true);
1364 T = Context.IntTy;
1365 }
1366
John McCall04a67a62010-02-05 21:31:56 +00001367 DiagnoseDelayedFnAttrs(*this, FnAttrsFromPreviousChunk);
1368
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001369 // See if there are any attributes on this declarator chunk.
1370 if (const AttributeList *AL = DeclType.getAttrs())
Charles Davis328ce342010-02-24 02:27:18 +00001371 ProcessTypeAttributeList(*this, T, false, AL, FnAttrsFromPreviousChunk);
Reid Spencer5f016e22007-07-11 17:01:13 +00001372 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001373
1374 if (getLangOptions().CPlusPlus && T->isFunctionType()) {
John McCall183700f2009-09-21 23:43:11 +00001375 const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>();
Chris Lattner778ed742009-10-25 17:36:50 +00001376 assert(FnTy && "Why oh why is there not a FunctionProtoType here?");
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001377
1378 // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type
1379 // for a nonstatic member function, the function type to which a pointer
1380 // to member refers, or the top-level function type of a function typedef
1381 // declaration.
Sebastian Redlc61bb202010-07-09 21:26:08 +00001382 bool FreeFunction = (D.getContext() != Declarator::MemberContext &&
1383 (!D.getCXXScopeSpec().isSet() ||
1384 !computeDeclContext(D.getCXXScopeSpec(), /*FIXME:*/true)->isRecord()));
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001385 if (FnTy->getTypeQuals() != 0 &&
1386 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
Sebastian Redlc61bb202010-07-09 21:26:08 +00001387 (FreeFunction ||
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001388 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001389 if (D.isFunctionDeclarator())
1390 Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
1391 else
1392 Diag(D.getIdentifierLoc(),
Sebastian Redlc61bb202010-07-09 21:26:08 +00001393 diag::err_invalid_qualified_typedef_function_type_use)
1394 << FreeFunction;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001395
1396 // Strip the cv-quals from the type.
1397 T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
Douglas Gregorce056bc2010-02-21 22:15:06 +00001398 FnTy->getNumArgs(), FnTy->isVariadic(), 0,
Rafael Espindola264ba482010-03-30 20:24:48 +00001399 false, false, 0, 0, FunctionType::ExtInfo());
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001400 }
1401 }
Mike Stump1eb44332009-09-09 15:08:12 +00001402
Sebastian Redl73780122010-06-09 21:19:43 +00001403 // If there's a constexpr specifier, treat it as a top-level const.
1404 if (D.getDeclSpec().isConstexprSpecified()) {
1405 T.addConst();
1406 }
1407
John McCall04a67a62010-02-05 21:31:56 +00001408 // Process any function attributes we might have delayed from the
1409 // declaration-specifiers.
1410 ProcessDelayedFnAttrs(*this, T, FnAttrsFromDeclSpec);
1411
1412 // If there were any type attributes applied to the decl itself, not
1413 // the type, apply them to the result type. But don't do this for
1414 // block-literal expressions, which are parsed wierdly.
1415 if (D.getContext() != Declarator::BlockLiteralContext)
1416 if (const AttributeList *Attrs = D.getAttributes())
Charles Davis328ce342010-02-24 02:27:18 +00001417 ProcessTypeAttributeList(*this, T, false, Attrs,
1418 FnAttrsFromPreviousChunk);
John McCall04a67a62010-02-05 21:31:56 +00001419
1420 DiagnoseDelayedFnAttrs(*this, FnAttrsFromPreviousChunk);
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001421
John McCallbf1a0282010-06-04 23:28:52 +00001422 if (T.isNull())
1423 return Context.getNullTypeSourceInfo();
1424 else if (D.isInvalidType())
1425 return Context.getTrivialTypeSourceInfo(T);
1426 return GetTypeSourceInfoForDeclarator(D, T, ReturnTypeInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +00001427}
1428
John McCall51bd8032009-10-18 01:05:36 +00001429namespace {
1430 class TypeSpecLocFiller : public TypeLocVisitor<TypeSpecLocFiller> {
1431 const DeclSpec &DS;
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001432
John McCall51bd8032009-10-18 01:05:36 +00001433 public:
1434 TypeSpecLocFiller(const DeclSpec &DS) : DS(DS) {}
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001435
John McCall51bd8032009-10-18 01:05:36 +00001436 void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
1437 Visit(TL.getUnqualifiedLoc());
1438 }
1439 void VisitTypedefTypeLoc(TypedefTypeLoc TL) {
1440 TL.setNameLoc(DS.getTypeSpecTypeLoc());
1441 }
1442 void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
1443 TL.setNameLoc(DS.getTypeSpecTypeLoc());
John McCallc12c5bb2010-05-15 11:32:37 +00001444 }
1445 void VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
1446 // Handle the base type, which might not have been written explicitly.
1447 if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) {
1448 TL.setHasBaseTypeAsWritten(false);
1449 TL.getBaseLoc().initialize(SourceLocation());
1450 } else {
1451 TL.setHasBaseTypeAsWritten(true);
1452 Visit(TL.getBaseLoc());
1453 }
Argyrios Kyrtzidiseb667592009-09-29 19:45:22 +00001454
John McCallc12c5bb2010-05-15 11:32:37 +00001455 // Protocol qualifiers.
John McCall54e14c42009-10-22 22:37:11 +00001456 if (DS.getProtocolQualifiers()) {
1457 assert(TL.getNumProtocols() > 0);
1458 assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
1459 TL.setLAngleLoc(DS.getProtocolLAngleLoc());
1460 TL.setRAngleLoc(DS.getSourceRange().getEnd());
1461 for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i)
1462 TL.setProtocolLoc(i, DS.getProtocolLocs()[i]);
1463 } else {
1464 assert(TL.getNumProtocols() == 0);
1465 TL.setLAngleLoc(SourceLocation());
1466 TL.setRAngleLoc(SourceLocation());
1467 }
1468 }
1469 void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
John McCall54e14c42009-10-22 22:37:11 +00001470 TL.setStarLoc(SourceLocation());
John McCallc12c5bb2010-05-15 11:32:37 +00001471 Visit(TL.getPointeeLoc());
John McCall51bd8032009-10-18 01:05:36 +00001472 }
John McCall833ca992009-10-29 08:12:44 +00001473 void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) {
John McCalla93c9342009-12-07 02:54:59 +00001474 TypeSourceInfo *TInfo = 0;
John McCallb3d87482010-08-24 05:47:05 +00001475 Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
John McCall833ca992009-10-29 08:12:44 +00001476
1477 // If we got no declarator info from previous Sema routines,
1478 // just fill with the typespec loc.
John McCalla93c9342009-12-07 02:54:59 +00001479 if (!TInfo) {
John McCall833ca992009-10-29 08:12:44 +00001480 TL.initialize(DS.getTypeSpecTypeLoc());
1481 return;
1482 }
1483
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00001484 TypeLoc OldTL = TInfo->getTypeLoc();
1485 if (TInfo->getType()->getAs<ElaboratedType>()) {
1486 ElaboratedTypeLoc ElabTL = cast<ElaboratedTypeLoc>(OldTL);
1487 TemplateSpecializationTypeLoc NamedTL =
1488 cast<TemplateSpecializationTypeLoc>(ElabTL.getNamedTypeLoc());
1489 TL.copy(NamedTL);
1490 }
1491 else
1492 TL.copy(cast<TemplateSpecializationTypeLoc>(OldTL));
John McCall833ca992009-10-29 08:12:44 +00001493 }
John McCallcfb708c2010-01-13 20:03:27 +00001494 void VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
1495 assert(DS.getTypeSpecType() == DeclSpec::TST_typeofExpr);
1496 TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
1497 TL.setParensRange(DS.getTypeofParensRange());
1498 }
1499 void VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
1500 assert(DS.getTypeSpecType() == DeclSpec::TST_typeofType);
1501 TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
1502 TL.setParensRange(DS.getTypeofParensRange());
John McCallb3d87482010-08-24 05:47:05 +00001503 assert(DS.getRepAsType());
John McCallcfb708c2010-01-13 20:03:27 +00001504 TypeSourceInfo *TInfo = 0;
John McCallb3d87482010-08-24 05:47:05 +00001505 Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
John McCallcfb708c2010-01-13 20:03:27 +00001506 TL.setUnderlyingTInfo(TInfo);
1507 }
Douglas Gregorddf889a2010-01-18 18:04:31 +00001508 void VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
1509 // By default, use the source location of the type specifier.
1510 TL.setBuiltinLoc(DS.getTypeSpecTypeLoc());
1511 if (TL.needsExtraLocalData()) {
1512 // Set info for the written builtin specifiers.
1513 TL.getWrittenBuiltinSpecs() = DS.getWrittenBuiltinSpecs();
1514 // Try to have a meaningful source location.
1515 if (TL.getWrittenSignSpec() != TSS_unspecified)
1516 // Sign spec loc overrides the others (e.g., 'unsigned long').
1517 TL.setBuiltinLoc(DS.getTypeSpecSignLoc());
1518 else if (TL.getWrittenWidthSpec() != TSW_unspecified)
1519 // Width spec loc overrides type spec loc (e.g., 'short int').
1520 TL.setBuiltinLoc(DS.getTypeSpecWidthLoc());
1521 }
1522 }
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00001523 void VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
1524 ElaboratedTypeKeyword Keyword
1525 = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
1526 if (Keyword == ETK_Typename) {
1527 TypeSourceInfo *TInfo = 0;
John McCallb3d87482010-08-24 05:47:05 +00001528 Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00001529 if (TInfo) {
1530 TL.copy(cast<ElaboratedTypeLoc>(TInfo->getTypeLoc()));
1531 return;
1532 }
1533 }
1534 TL.setKeywordLoc(Keyword != ETK_None
1535 ? DS.getTypeSpecTypeLoc()
1536 : SourceLocation());
1537 const CXXScopeSpec& SS = DS.getTypeSpecScope();
1538 TL.setQualifierRange(SS.isEmpty() ? SourceRange(): SS.getRange());
1539 Visit(TL.getNextTypeLoc().getUnqualifiedLoc());
1540 }
1541 void VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
1542 ElaboratedTypeKeyword Keyword
1543 = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
1544 if (Keyword == ETK_Typename) {
1545 TypeSourceInfo *TInfo = 0;
John McCallb3d87482010-08-24 05:47:05 +00001546 Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00001547 if (TInfo) {
1548 TL.copy(cast<DependentNameTypeLoc>(TInfo->getTypeLoc()));
1549 return;
1550 }
1551 }
1552 TL.setKeywordLoc(Keyword != ETK_None
1553 ? DS.getTypeSpecTypeLoc()
1554 : SourceLocation());
1555 const CXXScopeSpec& SS = DS.getTypeSpecScope();
1556 TL.setQualifierRange(SS.isEmpty() ? SourceRange() : SS.getRange());
1557 // FIXME: load appropriate source location.
1558 TL.setNameLoc(DS.getTypeSpecTypeLoc());
1559 }
John McCall33500952010-06-11 00:33:02 +00001560 void VisitDependentTemplateSpecializationTypeLoc(
1561 DependentTemplateSpecializationTypeLoc TL) {
1562 ElaboratedTypeKeyword Keyword
1563 = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
1564 if (Keyword == ETK_Typename) {
1565 TypeSourceInfo *TInfo = 0;
John McCallb3d87482010-08-24 05:47:05 +00001566 Sema::GetTypeFromParser(DS.getRepAsType(), &TInfo);
John McCall33500952010-06-11 00:33:02 +00001567 if (TInfo) {
1568 TL.copy(cast<DependentTemplateSpecializationTypeLoc>(
1569 TInfo->getTypeLoc()));
1570 return;
1571 }
1572 }
1573 TL.initializeLocal(SourceLocation());
1574 TL.setKeywordLoc(Keyword != ETK_None
1575 ? DS.getTypeSpecTypeLoc()
1576 : SourceLocation());
1577 const CXXScopeSpec& SS = DS.getTypeSpecScope();
1578 TL.setQualifierRange(SS.isEmpty() ? SourceRange() : SS.getRange());
1579 // FIXME: load appropriate source location.
1580 TL.setNameLoc(DS.getTypeSpecTypeLoc());
1581 }
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00001582
John McCall51bd8032009-10-18 01:05:36 +00001583 void VisitTypeLoc(TypeLoc TL) {
1584 // FIXME: add other typespec types and change this to an assert.
1585 TL.initialize(DS.getTypeSpecTypeLoc());
1586 }
1587 };
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001588
John McCall51bd8032009-10-18 01:05:36 +00001589 class DeclaratorLocFiller : public TypeLocVisitor<DeclaratorLocFiller> {
1590 const DeclaratorChunk &Chunk;
1591
1592 public:
1593 DeclaratorLocFiller(const DeclaratorChunk &Chunk) : Chunk(Chunk) {}
1594
1595 void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001596 llvm_unreachable("qualified type locs not expected here!");
John McCall51bd8032009-10-18 01:05:36 +00001597 }
1598
1599 void VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
1600 assert(Chunk.Kind == DeclaratorChunk::BlockPointer);
1601 TL.setCaretLoc(Chunk.Loc);
1602 }
1603 void VisitPointerTypeLoc(PointerTypeLoc TL) {
1604 assert(Chunk.Kind == DeclaratorChunk::Pointer);
1605 TL.setStarLoc(Chunk.Loc);
1606 }
1607 void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
1608 assert(Chunk.Kind == DeclaratorChunk::Pointer);
1609 TL.setStarLoc(Chunk.Loc);
1610 }
1611 void VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
1612 assert(Chunk.Kind == DeclaratorChunk::MemberPointer);
1613 TL.setStarLoc(Chunk.Loc);
1614 // FIXME: nested name specifier
1615 }
1616 void VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
1617 assert(Chunk.Kind == DeclaratorChunk::Reference);
John McCall54e14c42009-10-22 22:37:11 +00001618 // 'Amp' is misleading: this might have been originally
1619 /// spelled with AmpAmp.
John McCall51bd8032009-10-18 01:05:36 +00001620 TL.setAmpLoc(Chunk.Loc);
1621 }
1622 void VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
1623 assert(Chunk.Kind == DeclaratorChunk::Reference);
1624 assert(!Chunk.Ref.LValueRef);
1625 TL.setAmpAmpLoc(Chunk.Loc);
1626 }
1627 void VisitArrayTypeLoc(ArrayTypeLoc TL) {
1628 assert(Chunk.Kind == DeclaratorChunk::Array);
1629 TL.setLBracketLoc(Chunk.Loc);
1630 TL.setRBracketLoc(Chunk.EndLoc);
1631 TL.setSizeExpr(static_cast<Expr*>(Chunk.Arr.NumElts));
1632 }
1633 void VisitFunctionTypeLoc(FunctionTypeLoc TL) {
1634 assert(Chunk.Kind == DeclaratorChunk::Function);
1635 TL.setLParenLoc(Chunk.Loc);
1636 TL.setRParenLoc(Chunk.EndLoc);
1637
1638 const DeclaratorChunk::FunctionTypeInfo &FTI = Chunk.Fun;
John McCall54e14c42009-10-22 22:37:11 +00001639 for (unsigned i = 0, e = TL.getNumArgs(), tpi = 0; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00001640 ParmVarDecl *Param = cast<ParmVarDecl>(FTI.ArgInfo[i].Param);
John McCall54e14c42009-10-22 22:37:11 +00001641 TL.setArg(tpi++, Param);
John McCall51bd8032009-10-18 01:05:36 +00001642 }
1643 // FIXME: exception specs
1644 }
1645
1646 void VisitTypeLoc(TypeLoc TL) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001647 llvm_unreachable("unsupported TypeLoc kind in declarator!");
John McCall51bd8032009-10-18 01:05:36 +00001648 }
1649 };
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001650}
1651
John McCalla93c9342009-12-07 02:54:59 +00001652/// \brief Create and instantiate a TypeSourceInfo with type source information.
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001653///
1654/// \param T QualType referring to the type as written in source code.
Douglas Gregor05baacb2010-04-12 23:19:01 +00001655///
1656/// \param ReturnTypeInfo For declarators whose return type does not show
1657/// up in the normal place in the declaration specifiers (such as a C++
1658/// conversion function), this pointer will refer to a type source information
1659/// for that return type.
John McCalla93c9342009-12-07 02:54:59 +00001660TypeSourceInfo *
Douglas Gregor05baacb2010-04-12 23:19:01 +00001661Sema::GetTypeSourceInfoForDeclarator(Declarator &D, QualType T,
1662 TypeSourceInfo *ReturnTypeInfo) {
John McCalla93c9342009-12-07 02:54:59 +00001663 TypeSourceInfo *TInfo = Context.CreateTypeSourceInfo(T);
1664 UnqualTypeLoc CurrTL = TInfo->getTypeLoc().getUnqualifiedLoc();
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001665
Sebastian Redl8ce35b02009-10-25 21:45:37 +00001666 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
John McCall51bd8032009-10-18 01:05:36 +00001667 DeclaratorLocFiller(D.getTypeObject(i)).Visit(CurrTL);
1668 CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001669 }
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001670
John McCallb3d87482010-08-24 05:47:05 +00001671 // If we have different source information for the return type, use
1672 // that. This really only applies to C++ conversion functions.
1673 if (ReturnTypeInfo) {
Douglas Gregor05baacb2010-04-12 23:19:01 +00001674 TypeLoc TL = ReturnTypeInfo->getTypeLoc();
1675 assert(TL.getFullDataSize() == CurrTL.getFullDataSize());
1676 memcpy(CurrTL.getOpaqueData(), TL.getOpaqueData(), TL.getFullDataSize());
John McCallb3d87482010-08-24 05:47:05 +00001677 } else {
1678 TypeSpecLocFiller(D.getDeclSpec()).Visit(CurrTL);
Douglas Gregor05baacb2010-04-12 23:19:01 +00001679 }
1680
John McCalla93c9342009-12-07 02:54:59 +00001681 return TInfo;
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001682}
1683
John McCalla93c9342009-12-07 02:54:59 +00001684/// \brief Create a LocInfoType to hold the given QualType and TypeSourceInfo.
John McCallb3d87482010-08-24 05:47:05 +00001685ParsedType Sema::CreateParsedType(QualType T, TypeSourceInfo *TInfo) {
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001686 // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser
1687 // and Sema during declaration parsing. Try deallocating/caching them when
1688 // it's appropriate, instead of allocating them and keeping them around.
1689 LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType), 8);
John McCalla93c9342009-12-07 02:54:59 +00001690 new (LocT) LocInfoType(T, TInfo);
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001691 assert(LocT->getTypeClass() != T->getTypeClass() &&
1692 "LocInfoType's TypeClass conflicts with an existing Type class");
John McCallb3d87482010-08-24 05:47:05 +00001693 return ParsedType::make(QualType(LocT, 0));
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001694}
1695
1696void LocInfoType::getAsStringInternal(std::string &Str,
1697 const PrintingPolicy &Policy) const {
Argyrios Kyrtzidis35d44e52009-08-19 01:46:06 +00001698 assert(false && "LocInfoType leaked into the type system; an opaque TypeTy*"
1699 " was used directly instead of getting the QualType through"
1700 " GetTypeFromParser");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001701}
1702
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001703Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001704 // C99 6.7.6: Type names have no identifier. This is already validated by
1705 // the parser.
1706 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00001707
Douglas Gregor402abb52009-05-28 23:31:59 +00001708 TagDecl *OwnedTag = 0;
John McCallbf1a0282010-06-04 23:28:52 +00001709 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S, &OwnedTag);
1710 QualType T = TInfo->getType();
Chris Lattner5153ee62009-04-25 08:47:54 +00001711 if (D.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +00001712 return true;
Steve Naroff5912a352007-08-28 20:14:24 +00001713
Douglas Gregor402abb52009-05-28 23:31:59 +00001714 if (getLangOptions().CPlusPlus) {
1715 // Check that there are no default arguments (C++ only).
Douglas Gregor6d6eb572008-05-07 04:49:29 +00001716 CheckExtraCXXDefaultArguments(D);
1717
Douglas Gregor402abb52009-05-28 23:31:59 +00001718 // C++0x [dcl.type]p3:
1719 // A type-specifier-seq shall not define a class or enumeration
1720 // unless it appears in the type-id of an alias-declaration
1721 // (7.1.3).
1722 if (OwnedTag && OwnedTag->isDefinition())
1723 Diag(OwnedTag->getLocation(), diag::err_type_defined_in_type_specifier)
1724 << Context.getTypeDeclType(OwnedTag);
1725 }
1726
John McCallb3d87482010-08-24 05:47:05 +00001727 return CreateParsedType(T, TInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +00001728}
1729
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001730
1731
1732//===----------------------------------------------------------------------===//
1733// Type Attribute Processing
1734//===----------------------------------------------------------------------===//
1735
1736/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
1737/// specified type. The attribute contains 1 argument, the id of the address
1738/// space for the type.
Mike Stump1eb44332009-09-09 15:08:12 +00001739static void HandleAddressSpaceTypeAttribute(QualType &Type,
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001740 const AttributeList &Attr, Sema &S){
John McCall0953e762009-09-24 19:53:00 +00001741
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001742 // If this type is already address space qualified, reject it.
1743 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
1744 // for two or more different address spaces."
1745 if (Type.getAddressSpace()) {
1746 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
Abramo Bagnarae215f722010-04-30 13:10:51 +00001747 Attr.setInvalid();
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001748 return;
1749 }
Mike Stump1eb44332009-09-09 15:08:12 +00001750
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001751 // Check the attribute arguments.
1752 if (Attr.getNumArgs() != 1) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +00001753 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Abramo Bagnarae215f722010-04-30 13:10:51 +00001754 Attr.setInvalid();
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001755 return;
1756 }
1757 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
1758 llvm::APSInt addrSpace(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001759 if (ASArgExpr->isTypeDependent() || ASArgExpr->isValueDependent() ||
1760 !ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001761 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
1762 << ASArgExpr->getSourceRange();
Abramo Bagnarae215f722010-04-30 13:10:51 +00001763 Attr.setInvalid();
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001764 return;
1765 }
1766
John McCallefadb772009-07-28 06:52:18 +00001767 // Bounds checking.
1768 if (addrSpace.isSigned()) {
1769 if (addrSpace.isNegative()) {
1770 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative)
1771 << ASArgExpr->getSourceRange();
Abramo Bagnarae215f722010-04-30 13:10:51 +00001772 Attr.setInvalid();
John McCallefadb772009-07-28 06:52:18 +00001773 return;
1774 }
1775 addrSpace.setIsSigned(false);
1776 }
1777 llvm::APSInt max(addrSpace.getBitWidth());
John McCall0953e762009-09-24 19:53:00 +00001778 max = Qualifiers::MaxAddressSpace;
John McCallefadb772009-07-28 06:52:18 +00001779 if (addrSpace > max) {
1780 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high)
John McCall0953e762009-09-24 19:53:00 +00001781 << Qualifiers::MaxAddressSpace << ASArgExpr->getSourceRange();
Abramo Bagnarae215f722010-04-30 13:10:51 +00001782 Attr.setInvalid();
John McCallefadb772009-07-28 06:52:18 +00001783 return;
1784 }
1785
Mike Stump1eb44332009-09-09 15:08:12 +00001786 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001787 Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001788}
1789
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001790/// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the
1791/// specified type. The attribute contains 1 argument, weak or strong.
Mike Stump1eb44332009-09-09 15:08:12 +00001792static void HandleObjCGCTypeAttribute(QualType &Type,
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001793 const AttributeList &Attr, Sema &S) {
John McCall0953e762009-09-24 19:53:00 +00001794 if (Type.getObjCGCAttr() != Qualifiers::GCNone) {
Fariborz Jahanian5934e752009-02-18 18:52:41 +00001795 S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc);
Abramo Bagnarae215f722010-04-30 13:10:51 +00001796 Attr.setInvalid();
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001797 return;
1798 }
Mike Stump1eb44332009-09-09 15:08:12 +00001799
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001800 // Check the attribute arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001801 if (!Attr.getParameterName()) {
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001802 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1803 << "objc_gc" << 1;
Abramo Bagnarae215f722010-04-30 13:10:51 +00001804 Attr.setInvalid();
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001805 return;
1806 }
John McCall0953e762009-09-24 19:53:00 +00001807 Qualifiers::GC GCAttr;
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001808 if (Attr.getNumArgs() != 0) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001809 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Abramo Bagnarae215f722010-04-30 13:10:51 +00001810 Attr.setInvalid();
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001811 return;
1812 }
Mike Stump1eb44332009-09-09 15:08:12 +00001813 if (Attr.getParameterName()->isStr("weak"))
John McCall0953e762009-09-24 19:53:00 +00001814 GCAttr = Qualifiers::Weak;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001815 else if (Attr.getParameterName()->isStr("strong"))
John McCall0953e762009-09-24 19:53:00 +00001816 GCAttr = Qualifiers::Strong;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001817 else {
1818 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1819 << "objc_gc" << Attr.getParameterName();
Abramo Bagnarae215f722010-04-30 13:10:51 +00001820 Attr.setInvalid();
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001821 return;
1822 }
Mike Stump1eb44332009-09-09 15:08:12 +00001823
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001824 Type = S.Context.getObjCGCQualType(Type, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001825}
1826
Ted Kremenek58f281f2010-08-19 00:52:13 +00001827static QualType GetResultType(QualType T) {
1828 if (const PointerType *PT = T->getAs<PointerType>())
1829 T = PT->getPointeeType();
1830 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
1831 T = BT->getPointeeType();
1832 return T->getAs<FunctionType>()->getResultType();
1833}
1834
John McCall04a67a62010-02-05 21:31:56 +00001835/// Process an individual function attribute. Returns true if the
1836/// attribute does not make sense to apply to this type.
1837bool ProcessFnAttr(Sema &S, QualType &Type, const AttributeList &Attr) {
1838 if (Attr.getKind() == AttributeList::AT_noreturn) {
1839 // Complain immediately if the arg count is wrong.
1840 if (Attr.getNumArgs() != 0) {
1841 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Abramo Bagnarae215f722010-04-30 13:10:51 +00001842 Attr.setInvalid();
John McCall04a67a62010-02-05 21:31:56 +00001843 return false;
1844 }
Mike Stump24556362009-07-25 21:26:53 +00001845
John McCall04a67a62010-02-05 21:31:56 +00001846 // Delay if this is not a function or pointer to block.
1847 if (!Type->isFunctionPointerType()
1848 && !Type->isBlockPointerType()
1849 && !Type->isFunctionType())
1850 return true;
Ted Kremenek58f281f2010-08-19 00:52:13 +00001851
1852 if (!GetResultType(Type)->isVoidType()) {
1853 S.Diag(Attr.getLoc(), diag::warn_noreturn_function_has_nonvoid_result)
1854 << (Type->isBlockPointerType() ? /* blocks */ 1 : /* functions */ 0);
1855 }
1856
John McCall04a67a62010-02-05 21:31:56 +00001857 // Otherwise we can process right away.
1858 Type = S.Context.getNoReturnType(Type);
1859 return false;
1860 }
Mike Stump24556362009-07-25 21:26:53 +00001861
Rafael Espindola425ef722010-03-30 22:15:11 +00001862 if (Attr.getKind() == AttributeList::AT_regparm) {
1863 // The warning is emitted elsewhere
1864 if (Attr.getNumArgs() != 1) {
1865 return false;
1866 }
1867
1868 // Delay if this is not a function or pointer to block.
1869 if (!Type->isFunctionPointerType()
1870 && !Type->isBlockPointerType()
1871 && !Type->isFunctionType())
1872 return true;
1873
1874 // Otherwise we can process right away.
1875 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1876 llvm::APSInt NumParams(32);
1877
1878 // The warning is emitted elsewhere
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001879 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
1880 !NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context))
Rafael Espindola425ef722010-03-30 22:15:11 +00001881 return false;
1882
1883 Type = S.Context.getRegParmType(Type, NumParams.getZExtValue());
1884 return false;
1885 }
1886
John McCall04a67a62010-02-05 21:31:56 +00001887 // Otherwise, a calling convention.
1888 if (Attr.getNumArgs() != 0) {
1889 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Abramo Bagnarae215f722010-04-30 13:10:51 +00001890 Attr.setInvalid();
John McCall04a67a62010-02-05 21:31:56 +00001891 return false;
1892 }
John McCallf82b4e82010-02-04 05:44:44 +00001893
John McCall04a67a62010-02-05 21:31:56 +00001894 QualType T = Type;
1895 if (const PointerType *PT = Type->getAs<PointerType>())
1896 T = PT->getPointeeType();
1897 const FunctionType *Fn = T->getAs<FunctionType>();
John McCallf82b4e82010-02-04 05:44:44 +00001898
John McCall04a67a62010-02-05 21:31:56 +00001899 // Delay if the type didn't work out to a function.
1900 if (!Fn) return true;
1901
1902 // TODO: diagnose uses of these conventions on the wrong target.
1903 CallingConv CC;
1904 switch (Attr.getKind()) {
1905 case AttributeList::AT_cdecl: CC = CC_C; break;
1906 case AttributeList::AT_fastcall: CC = CC_X86FastCall; break;
1907 case AttributeList::AT_stdcall: CC = CC_X86StdCall; break;
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001908 case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break;
John McCall04a67a62010-02-05 21:31:56 +00001909 default: llvm_unreachable("unexpected attribute kind"); return false;
1910 }
1911
1912 CallingConv CCOld = Fn->getCallConv();
Charles Davis064f7db2010-02-23 06:13:55 +00001913 if (S.Context.getCanonicalCallConv(CC) ==
Abramo Bagnarae215f722010-04-30 13:10:51 +00001914 S.Context.getCanonicalCallConv(CCOld)) {
1915 Attr.setInvalid();
1916 return false;
1917 }
John McCall04a67a62010-02-05 21:31:56 +00001918
1919 if (CCOld != CC_Default) {
1920 // Should we diagnose reapplications of the same convention?
1921 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1922 << FunctionType::getNameForCallConv(CC)
1923 << FunctionType::getNameForCallConv(CCOld);
Abramo Bagnarae215f722010-04-30 13:10:51 +00001924 Attr.setInvalid();
John McCall04a67a62010-02-05 21:31:56 +00001925 return false;
1926 }
1927
1928 // Diagnose the use of X86 fastcall on varargs or unprototyped functions.
1929 if (CC == CC_X86FastCall) {
1930 if (isa<FunctionNoProtoType>(Fn)) {
1931 S.Diag(Attr.getLoc(), diag::err_cconv_knr)
1932 << FunctionType::getNameForCallConv(CC);
Abramo Bagnarae215f722010-04-30 13:10:51 +00001933 Attr.setInvalid();
John McCall04a67a62010-02-05 21:31:56 +00001934 return false;
1935 }
1936
1937 const FunctionProtoType *FnP = cast<FunctionProtoType>(Fn);
1938 if (FnP->isVariadic()) {
1939 S.Diag(Attr.getLoc(), diag::err_cconv_varargs)
1940 << FunctionType::getNameForCallConv(CC);
Abramo Bagnarae215f722010-04-30 13:10:51 +00001941 Attr.setInvalid();
John McCall04a67a62010-02-05 21:31:56 +00001942 return false;
1943 }
1944 }
1945
1946 Type = S.Context.getCallConvType(Type, CC);
1947 return false;
John McCallf82b4e82010-02-04 05:44:44 +00001948}
1949
John Thompson6e132aa2009-12-04 21:51:28 +00001950/// HandleVectorSizeAttribute - this attribute is only applicable to integral
1951/// and float scalars, although arrays, pointers, and function return values are
1952/// allowed in conjunction with this construct. Aggregates with this attribute
1953/// are invalid, even if they are of the same size as a corresponding scalar.
1954/// The raw attribute should contain precisely 1 argument, the vector size for
1955/// the variable, measured in bytes. If curType and rawAttr are well formed,
1956/// this routine will return a new vector type.
Chris Lattner788b0fd2010-06-23 06:00:24 +00001957static void HandleVectorSizeAttr(QualType& CurType, const AttributeList &Attr,
1958 Sema &S) {
John Thompson6e132aa2009-12-04 21:51:28 +00001959 // Check the attribute arugments.
1960 if (Attr.getNumArgs() != 1) {
1961 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Abramo Bagnarae215f722010-04-30 13:10:51 +00001962 Attr.setInvalid();
John Thompson6e132aa2009-12-04 21:51:28 +00001963 return;
1964 }
1965 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
1966 llvm::APSInt vecSize(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001967 if (sizeExpr->isTypeDependent() || sizeExpr->isValueDependent() ||
1968 !sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
John Thompson6e132aa2009-12-04 21:51:28 +00001969 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1970 << "vector_size" << sizeExpr->getSourceRange();
Abramo Bagnarae215f722010-04-30 13:10:51 +00001971 Attr.setInvalid();
John Thompson6e132aa2009-12-04 21:51:28 +00001972 return;
1973 }
1974 // the base type must be integer or float, and can't already be a vector.
Douglas Gregorf6094622010-07-23 15:58:24 +00001975 if (!CurType->isIntegerType() && !CurType->isRealFloatingType()) {
John Thompson6e132aa2009-12-04 21:51:28 +00001976 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
Abramo Bagnarae215f722010-04-30 13:10:51 +00001977 Attr.setInvalid();
John Thompson6e132aa2009-12-04 21:51:28 +00001978 return;
1979 }
1980 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
1981 // vecSize is specified in bytes - convert to bits.
1982 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
1983
1984 // the vector size needs to be an integral multiple of the type size.
1985 if (vectorSize % typeSize) {
1986 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
1987 << sizeExpr->getSourceRange();
Abramo Bagnarae215f722010-04-30 13:10:51 +00001988 Attr.setInvalid();
John Thompson6e132aa2009-12-04 21:51:28 +00001989 return;
1990 }
1991 if (vectorSize == 0) {
1992 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
1993 << sizeExpr->getSourceRange();
Abramo Bagnarae215f722010-04-30 13:10:51 +00001994 Attr.setInvalid();
John Thompson6e132aa2009-12-04 21:51:28 +00001995 return;
1996 }
1997
1998 // Success! Instantiate the vector type, the number of elements is > 0, and
1999 // not required to be a power of 2, unlike GCC.
Chris Lattner788b0fd2010-06-23 06:00:24 +00002000 CurType = S.Context.getVectorType(CurType, vectorSize/typeSize,
2001 VectorType::NotAltiVec);
John Thompson6e132aa2009-12-04 21:51:28 +00002002}
2003
John McCall04a67a62010-02-05 21:31:56 +00002004void ProcessTypeAttributeList(Sema &S, QualType &Result,
Charles Davis328ce342010-02-24 02:27:18 +00002005 bool IsDeclSpec, const AttributeList *AL,
John McCall04a67a62010-02-05 21:31:56 +00002006 DelayedAttributeSet &FnAttrs) {
Chris Lattner232e8822008-02-21 01:08:11 +00002007 // Scan through and apply attributes to this type where it makes sense. Some
2008 // attributes (such as __address_space__, __vector_size__, etc) apply to the
2009 // type, but others can be present in the type specifiers even though they
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00002010 // apply to the decl. Here we apply type attributes and ignore the rest.
2011 for (; AL; AL = AL->getNext()) {
Abramo Bagnarae215f722010-04-30 13:10:51 +00002012 // Skip attributes that were marked to be invalid.
2013 if (AL->isInvalid())
2014 continue;
2015
Abramo Bagnarab1f1b262010-04-30 09:13:03 +00002016 // If this is an attribute we can handle, do so now,
2017 // otherwise, add it to the FnAttrs list for rechaining.
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00002018 switch (AL->getKind()) {
Chris Lattner232e8822008-02-21 01:08:11 +00002019 default: break;
John McCall04a67a62010-02-05 21:31:56 +00002020
Chris Lattner232e8822008-02-21 01:08:11 +00002021 case AttributeList::AT_address_space:
John McCall04a67a62010-02-05 21:31:56 +00002022 HandleAddressSpaceTypeAttribute(Result, *AL, S);
Chris Lattnerc9b346d2008-06-29 00:50:08 +00002023 break;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00002024 case AttributeList::AT_objc_gc:
John McCall04a67a62010-02-05 21:31:56 +00002025 HandleObjCGCTypeAttribute(Result, *AL, S);
Mike Stump24556362009-07-25 21:26:53 +00002026 break;
John Thompson6e132aa2009-12-04 21:51:28 +00002027 case AttributeList::AT_vector_size:
John McCall04a67a62010-02-05 21:31:56 +00002028 HandleVectorSizeAttr(Result, *AL, S);
2029 break;
2030
2031 case AttributeList::AT_noreturn:
2032 case AttributeList::AT_cdecl:
2033 case AttributeList::AT_fastcall:
2034 case AttributeList::AT_stdcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002035 case AttributeList::AT_thiscall:
Rafael Espindola425ef722010-03-30 22:15:11 +00002036 case AttributeList::AT_regparm:
Charles Davis328ce342010-02-24 02:27:18 +00002037 // Don't process these on the DeclSpec.
2038 if (IsDeclSpec ||
2039 ProcessFnAttr(S, Result, *AL))
John McCall04a67a62010-02-05 21:31:56 +00002040 FnAttrs.push_back(DelayedAttribute(AL, Result));
John Thompson6e132aa2009-12-04 21:51:28 +00002041 break;
Chris Lattner232e8822008-02-21 01:08:11 +00002042 }
Chris Lattner232e8822008-02-21 01:08:11 +00002043 }
Chris Lattner232e8822008-02-21 01:08:11 +00002044}
2045
Mike Stump1eb44332009-09-09 15:08:12 +00002046/// @brief Ensure that the type T is a complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00002047///
2048/// This routine checks whether the type @p T is complete in any
2049/// context where a complete type is required. If @p T is a complete
Douglas Gregor86447ec2009-03-09 16:13:40 +00002050/// type, returns false. If @p T is a class template specialization,
2051/// this routine then attempts to perform class template
2052/// instantiation. If instantiation fails, or if @p T is incomplete
2053/// and cannot be completed, issues the diagnostic @p diag (giving it
2054/// the type @p T) and returns true.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00002055///
2056/// @param Loc The location in the source that the incomplete type
2057/// diagnostic should refer to.
2058///
2059/// @param T The type that this routine is examining for completeness.
2060///
Mike Stump1eb44332009-09-09 15:08:12 +00002061/// @param PD The partial diagnostic that will be printed out if T is not a
Anders Carlssonb7906612009-08-26 23:45:07 +00002062/// complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00002063///
2064/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
2065/// @c false otherwise.
Anders Carlsson91a0cc92009-08-26 22:33:56 +00002066bool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
Anders Carlsson8c8d9192009-10-09 23:51:55 +00002067 const PartialDiagnostic &PD,
2068 std::pair<SourceLocation,
2069 PartialDiagnostic> Note) {
Anders Carlsson91a0cc92009-08-26 22:33:56 +00002070 unsigned diag = PD.getDiagID();
Mike Stump1eb44332009-09-09 15:08:12 +00002071
Douglas Gregor573d9c32009-10-21 23:19:44 +00002072 // FIXME: Add this assertion to make sure we always get instantiation points.
2073 // assert(!Loc.isInvalid() && "Invalid location in RequireCompleteType");
Douglas Gregor690dc7f2009-05-21 23:48:18 +00002074 // FIXME: Add this assertion to help us flush out problems with
2075 // checking for dependent types and type-dependent expressions.
2076 //
Mike Stump1eb44332009-09-09 15:08:12 +00002077 // assert(!T->isDependentType() &&
Douglas Gregor690dc7f2009-05-21 23:48:18 +00002078 // "Can't ask whether a dependent type is complete");
2079
Douglas Gregor4ec339f2009-01-19 19:26:10 +00002080 // If we have a complete type, we're done.
2081 if (!T->isIncompleteType())
2082 return false;
Eli Friedman3c0eb162008-05-27 03:33:27 +00002083
Douglas Gregord475b8d2009-03-25 21:17:03 +00002084 // If we have a class template specialization or a class member of a
Sebastian Redl923d56d2009-11-05 15:52:31 +00002085 // class template specialization, or an array with known size of such,
2086 // try to instantiate it.
2087 QualType MaybeTemplate = T;
Douglas Gregor89c49f02009-11-09 22:08:55 +00002088 if (const ConstantArrayType *Array = Context.getAsConstantArrayType(T))
Sebastian Redl923d56d2009-11-05 15:52:31 +00002089 MaybeTemplate = Array->getElementType();
2090 if (const RecordType *Record = MaybeTemplate->getAs<RecordType>()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00002091 if (ClassTemplateSpecializationDecl *ClassTemplateSpec
Douglas Gregord475b8d2009-03-25 21:17:03 +00002092 = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
Douglas Gregor972e6ce2009-10-27 06:26:26 +00002093 if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared)
2094 return InstantiateClassTemplateSpecialization(Loc, ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002095 TSK_ImplicitInstantiation,
Douglas Gregor5842ba92009-08-24 15:23:48 +00002096 /*Complain=*/diag != 0);
Mike Stump1eb44332009-09-09 15:08:12 +00002097 } else if (CXXRecordDecl *Rec
Douglas Gregord475b8d2009-03-25 21:17:03 +00002098 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
2099 if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00002100 MemberSpecializationInfo *MSInfo = Rec->getMemberSpecializationInfo();
2101 assert(MSInfo && "Missing member specialization information?");
Douglas Gregor357bbd02009-08-28 20:50:45 +00002102 // This record was instantiated from a class within a template.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00002103 if (MSInfo->getTemplateSpecializationKind()
Douglas Gregor972e6ce2009-10-27 06:26:26 +00002104 != TSK_ExplicitSpecialization)
Douglas Gregorf6b11852009-10-08 15:14:33 +00002105 return InstantiateClass(Loc, Rec, Pattern,
2106 getTemplateInstantiationArgs(Rec),
2107 TSK_ImplicitInstantiation,
2108 /*Complain=*/diag != 0);
Douglas Gregord475b8d2009-03-25 21:17:03 +00002109 }
2110 }
2111 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00002112
Douglas Gregor5842ba92009-08-24 15:23:48 +00002113 if (diag == 0)
2114 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002115
Rafael Espindola01620702010-03-21 22:56:43 +00002116 const TagType *Tag = 0;
2117 if (const RecordType *Record = T->getAs<RecordType>())
2118 Tag = Record;
2119 else if (const EnumType *Enum = T->getAs<EnumType>())
2120 Tag = Enum;
2121
2122 // Avoid diagnosing invalid decls as incomplete.
2123 if (Tag && Tag->getDecl()->isInvalidDecl())
2124 return true;
2125
Douglas Gregor4ec339f2009-01-19 19:26:10 +00002126 // We have an incomplete type. Produce a diagnostic.
Anders Carlsson91a0cc92009-08-26 22:33:56 +00002127 Diag(Loc, PD) << T;
Douglas Gregor4ec339f2009-01-19 19:26:10 +00002128
Anders Carlsson8c8d9192009-10-09 23:51:55 +00002129 // If we have a note, produce it.
2130 if (!Note.first.isInvalid())
2131 Diag(Note.first, Note.second);
2132
Douglas Gregor4ec339f2009-01-19 19:26:10 +00002133 // If the type was a forward declaration of a class/struct/union
Rafael Espindola01620702010-03-21 22:56:43 +00002134 // type, produce a note.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00002135 if (Tag && !Tag->getDecl()->isInvalidDecl())
Mike Stump1eb44332009-09-09 15:08:12 +00002136 Diag(Tag->getDecl()->getLocation(),
Douglas Gregor4ec339f2009-01-19 19:26:10 +00002137 Tag->isBeingDefined() ? diag::note_type_being_defined
2138 : diag::note_forward_declaration)
2139 << QualType(Tag, 0);
2140
2141 return true;
2142}
Douglas Gregore6258932009-03-19 00:39:20 +00002143
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00002144bool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
2145 const PartialDiagnostic &PD) {
2146 return RequireCompleteType(Loc, T, PD,
2147 std::make_pair(SourceLocation(), PDiag(0)));
2148}
2149
2150bool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
2151 unsigned DiagID) {
2152 return RequireCompleteType(Loc, T, PDiag(DiagID),
2153 std::make_pair(SourceLocation(), PDiag(0)));
2154}
2155
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002156/// \brief Retrieve a version of the type 'T' that is elaborated by Keyword
2157/// and qualified by the nested-name-specifier contained in SS.
2158QualType Sema::getElaboratedType(ElaboratedTypeKeyword Keyword,
2159 const CXXScopeSpec &SS, QualType T) {
2160 if (T.isNull())
Douglas Gregore6258932009-03-19 00:39:20 +00002161 return T;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002162 NestedNameSpecifier *NNS;
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002163 if (SS.isValid())
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002164 NNS = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
2165 else {
2166 if (Keyword == ETK_None)
2167 return T;
2168 NNS = 0;
2169 }
2170 return Context.getElaboratedType(Keyword, NNS, T);
Douglas Gregore6258932009-03-19 00:39:20 +00002171}
Anders Carlssonaf017e62009-06-29 22:58:55 +00002172
2173QualType Sema::BuildTypeofExprType(Expr *E) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00002174 if (E->getType() == Context.OverloadTy) {
2175 // C++ [temp.arg.explicit]p3 allows us to resolve a template-id to a
2176 // function template specialization wherever deduction cannot occur.
2177 if (FunctionDecl *Specialization
2178 = ResolveSingleFunctionTemplateSpecialization(E)) {
John McCall161755a2010-04-06 21:38:20 +00002179 // The access doesn't really matter in this case.
2180 DeclAccessPair Found = DeclAccessPair::make(Specialization,
2181 Specialization->getAccess());
2182 E = FixOverloadedFunctionReference(E, Found, Specialization);
Douglas Gregor4b52e252009-12-21 23:17:24 +00002183 if (!E)
2184 return QualType();
2185 } else {
2186 Diag(E->getLocStart(),
2187 diag::err_cannot_determine_declared_type_of_overloaded_function)
2188 << false << E->getSourceRange();
2189 return QualType();
2190 }
2191 }
2192
Anders Carlssonaf017e62009-06-29 22:58:55 +00002193 return Context.getTypeOfExprType(E);
2194}
2195
2196QualType Sema::BuildDecltypeType(Expr *E) {
2197 if (E->getType() == Context.OverloadTy) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00002198 // C++ [temp.arg.explicit]p3 allows us to resolve a template-id to a
2199 // function template specialization wherever deduction cannot occur.
2200 if (FunctionDecl *Specialization
2201 = ResolveSingleFunctionTemplateSpecialization(E)) {
John McCall161755a2010-04-06 21:38:20 +00002202 // The access doesn't really matter in this case.
2203 DeclAccessPair Found = DeclAccessPair::make(Specialization,
2204 Specialization->getAccess());
2205 E = FixOverloadedFunctionReference(E, Found, Specialization);
Douglas Gregor4b52e252009-12-21 23:17:24 +00002206 if (!E)
2207 return QualType();
2208 } else {
2209 Diag(E->getLocStart(),
2210 diag::err_cannot_determine_declared_type_of_overloaded_function)
2211 << true << E->getSourceRange();
2212 return QualType();
2213 }
Anders Carlssonaf017e62009-06-29 22:58:55 +00002214 }
Douglas Gregor4b52e252009-12-21 23:17:24 +00002215
Anders Carlssonaf017e62009-06-29 22:58:55 +00002216 return Context.getDecltypeType(E);
2217}