blob: 48bf7cbac4e82e68086c7e3444f33725484b3e18 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- SemaType.cpp - Semantic Analysis for Types -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements type-related semantic analysis.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
15#include "clang/AST/ASTContext.h"
Douglas Gregora8f32e02009-10-06 17:59:45 +000016#include "clang/AST/CXXInheritance.h"
Steve Naroff980e5082007-10-01 19:00:59 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregor2943aed2009-03-03 04:44:36 +000018#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +000019#include "clang/AST/TypeLoc.h"
John McCall51bd8032009-10-18 01:05:36 +000020#include "clang/AST/TypeLocVisitor.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000021#include "clang/AST/Expr.h"
Anders Carlsson91a0cc92009-08-26 22:33:56 +000022#include "clang/Basic/PartialDiagnostic.h"
Daniel Dunbare4858a62008-08-11 03:45:03 +000023#include "clang/Parse/DeclSpec.h"
Sebastian Redl4994d2d2009-07-04 11:39:00 +000024#include "llvm/ADT/SmallPtrSet.h"
Douglas Gregor87c12c42009-11-04 16:49:01 +000025#include "llvm/Support/ErrorHandling.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000026using namespace clang;
27
Abramo Bagnarae4da7a02010-05-19 21:37:53 +000028#include <iostream>
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: {
Douglas Gregorc7621a62009-11-05 20:54:04 +0000269 TypeDecl *D
270 = dyn_cast_or_null<TypeDecl>(static_cast<Decl *>(DS.getTypeRep()));
John McCall6e247262009-10-10 05:48:19 +0000271 if (!D) {
272 // This can happen in C++ with ambiguous lookups.
273 Result = Context.IntTy;
Chris Lattner5db2bb12009-10-25 18:21:37 +0000274 TheDeclarator.setInvalidType(true);
John McCall6e247262009-10-10 05:48:19 +0000275 break;
276 }
277
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000278 // If the type is deprecated or unavailable, diagnose it.
John McCall54abf7d2009-11-04 02:18:39 +0000279 TheSema.DiagnoseUseOfDecl(D, DS.getTypeSpecTypeLoc());
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000280
Reid Spencer5f016e22007-07-11 17:01:13 +0000281 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000282 DS.getTypeSpecSign() == 0 && "No qualifiers on tag names!");
283
Reid Spencer5f016e22007-07-11 17:01:13 +0000284 // TypeQuals handled by caller.
Chris Lattnera64ef0a2009-10-25 22:09:09 +0000285 Result = Context.getTypeDeclType(D);
John McCall2191b202009-09-05 06:31:47 +0000286
287 // In C++, make an ElaboratedType.
Chris Lattner1564e392009-10-25 18:07:27 +0000288 if (TheSema.getLangOptions().CPlusPlus) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000289 ElaboratedTypeKeyword Keyword
290 = ElaboratedType::getKeywordForTypeSpec(DS.getTypeSpecType());
291 Result = TheSema.getElaboratedType(Keyword, DS.getTypeSpecScope(),
292 Result);
John McCall2191b202009-09-05 06:31:47 +0000293 }
Chris Lattner5153ee62009-04-25 08:47:54 +0000294 if (D->isInvalidDecl())
Chris Lattner5db2bb12009-10-25 18:21:37 +0000295 TheDeclarator.setInvalidType(true);
Chris Lattner958858e2008-02-20 21:40:32 +0000296 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000297 }
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000298 case DeclSpec::TST_typename: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000299 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
300 DS.getTypeSpecSign() == 0 &&
301 "Can't handle qualifiers on typedef names yet!");
Chris Lattner1564e392009-10-25 18:07:27 +0000302 Result = TheSema.GetTypeFromParser(DS.getTypeRep());
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000303
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000304 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
John McCallc12c5bb2010-05-15 11:32:37 +0000305 if (const ObjCObjectType *ObjT = Result->getAs<ObjCObjectType>()) {
306 // Silently drop any existing protocol qualifiers.
307 // TODO: determine whether that's the right thing to do.
308 if (ObjT->getNumProtocols())
309 Result = ObjT->getBaseType();
310
311 if (DS.getNumProtocolQualifiers())
312 Result = Context.getObjCObjectType(Result,
313 (ObjCProtocolDecl**) PQ,
314 DS.getNumProtocolQualifiers());
315 } else if (Result->isObjCIdType()) {
Chris Lattnerae4da612008-07-26 01:53:50 +0000316 // id<protocol-list>
John McCallc12c5bb2010-05-15 11:32:37 +0000317 Result = Context.getObjCObjectType(Context.ObjCBuiltinIdTy,
318 (ObjCProtocolDecl**) PQ,
319 DS.getNumProtocolQualifiers());
320 Result = Context.getObjCObjectPointerType(Result);
321 } else if (Result->isObjCClassType()) {
Steve Naroff4262a072009-02-23 18:53:24 +0000322 // Class<protocol-list>
John McCallc12c5bb2010-05-15 11:32:37 +0000323 Result = Context.getObjCObjectType(Context.ObjCBuiltinClassTy,
324 (ObjCProtocolDecl**) PQ,
325 DS.getNumProtocolQualifiers());
326 Result = Context.getObjCObjectPointerType(Result);
Chris Lattner3f84ad22009-04-22 05:27:59 +0000327 } else {
Chris Lattner1564e392009-10-25 18:07:27 +0000328 TheSema.Diag(DeclLoc, diag::err_invalid_protocol_qualifiers)
Chris Lattner3f84ad22009-04-22 05:27:59 +0000329 << DS.getSourceRange();
Chris Lattner5db2bb12009-10-25 18:21:37 +0000330 TheDeclarator.setInvalidType(true);
Chris Lattner3f84ad22009-04-22 05:27:59 +0000331 }
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000332 }
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Reid Spencer5f016e22007-07-11 17:01:13 +0000334 // TypeQuals handled by caller.
Chris Lattner958858e2008-02-20 21:40:32 +0000335 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000336 }
Chris Lattner958858e2008-02-20 21:40:32 +0000337 case DeclSpec::TST_typeofType:
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000338 // FIXME: Preserve type source info.
Chris Lattner1564e392009-10-25 18:07:27 +0000339 Result = TheSema.GetTypeFromParser(DS.getTypeRep());
Chris Lattner958858e2008-02-20 21:40:32 +0000340 assert(!Result.isNull() && "Didn't get a type for typeof?");
Steve Naroffd1861fd2007-07-31 12:34:36 +0000341 // TypeQuals handled by caller.
Chris Lattnerfab5b452008-02-20 23:53:49 +0000342 Result = Context.getTypeOfType(Result);
Chris Lattner958858e2008-02-20 21:40:32 +0000343 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000344 case DeclSpec::TST_typeofExpr: {
345 Expr *E = static_cast<Expr *>(DS.getTypeRep());
346 assert(E && "Didn't get an expression for typeof?");
347 // TypeQuals handled by caller.
Douglas Gregor4b52e252009-12-21 23:17:24 +0000348 Result = TheSema.BuildTypeofExprType(E);
349 if (Result.isNull()) {
350 Result = Context.IntTy;
351 TheDeclarator.setInvalidType(true);
352 }
Chris Lattner958858e2008-02-20 21:40:32 +0000353 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000354 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000355 case DeclSpec::TST_decltype: {
356 Expr *E = static_cast<Expr *>(DS.getTypeRep());
357 assert(E && "Didn't get an expression for decltype?");
358 // TypeQuals handled by caller.
Chris Lattner1564e392009-10-25 18:07:27 +0000359 Result = TheSema.BuildDecltypeType(E);
Anders Carlssonaf017e62009-06-29 22:58:55 +0000360 if (Result.isNull()) {
361 Result = Context.IntTy;
Chris Lattner5db2bb12009-10-25 18:21:37 +0000362 TheDeclarator.setInvalidType(true);
Anders Carlssonaf017e62009-06-29 22:58:55 +0000363 }
Anders Carlsson6fd634f2009-06-24 17:47:40 +0000364 break;
365 }
Anders Carlssone89d1592009-06-26 18:41:36 +0000366 case DeclSpec::TST_auto: {
367 // TypeQuals handled by caller.
368 Result = Context.UndeducedAutoTy;
369 break;
370 }
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Douglas Gregor809070a2009-02-18 17:45:20 +0000372 case DeclSpec::TST_error:
Chris Lattner5153ee62009-04-25 08:47:54 +0000373 Result = Context.IntTy;
Chris Lattner5db2bb12009-10-25 18:21:37 +0000374 TheDeclarator.setInvalidType(true);
Chris Lattner5153ee62009-04-25 08:47:54 +0000375 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000376 }
Mike Stump1eb44332009-09-09 15:08:12 +0000377
Chris Lattner958858e2008-02-20 21:40:32 +0000378 // Handle complex types.
Douglas Gregorf244cd72009-02-14 21:06:05 +0000379 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
Chris Lattner1564e392009-10-25 18:07:27 +0000380 if (TheSema.getLangOptions().Freestanding)
381 TheSema.Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
Chris Lattnerfab5b452008-02-20 23:53:49 +0000382 Result = Context.getComplexType(Result);
John Thompson82287d12010-02-05 00:12:22 +0000383 } else if (DS.isTypeAltiVecVector()) {
384 unsigned typeSize = static_cast<unsigned>(Context.getTypeSize(Result));
385 assert(typeSize > 0 && "type size for vector must be greater than 0 bits");
Chris Lattner788b0fd2010-06-23 06:00:24 +0000386 VectorType::AltiVecSpecific AltiVecSpec = VectorType::AltiVec;
387 if (DS.isTypeAltiVecPixel())
388 AltiVecSpec = VectorType::Pixel;
389 else if (DS.isTypeAltiVecBool())
390 AltiVecSpec = VectorType::Bool;
391 Result = Context.getVectorType(Result, 128/typeSize, AltiVecSpec);
Douglas Gregorf244cd72009-02-14 21:06:05 +0000392 }
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Chris Lattner958858e2008-02-20 21:40:32 +0000394 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
395 "FIXME: imaginary types not supported yet!");
Mike Stump1eb44332009-09-09 15:08:12 +0000396
Chris Lattner38d8b982008-02-20 22:04:11 +0000397 // See if there are any attributes on the declspec that apply to the type (as
398 // opposed to the decl).
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000399 if (const AttributeList *AL = DS.getAttributes())
Charles Davis328ce342010-02-24 02:27:18 +0000400 ProcessTypeAttributeList(TheSema, Result, true, AL, Delayed);
Mike Stump1eb44332009-09-09 15:08:12 +0000401
Chris Lattner96b77fc2008-04-02 06:50:17 +0000402 // Apply const/volatile/restrict qualifiers to T.
403 if (unsigned TypeQuals = DS.getTypeQualifiers()) {
404
405 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
406 // or incomplete types shall not be restrict-qualified." C++ also allows
407 // restrict-qualified references.
John McCall0953e762009-09-24 19:53:00 +0000408 if (TypeQuals & DeclSpec::TQ_restrict) {
Fariborz Jahanian2b5ff1a2009-12-07 18:08:58 +0000409 if (Result->isAnyPointerType() || Result->isReferenceType()) {
410 QualType EltTy;
411 if (Result->isObjCObjectPointerType())
412 EltTy = Result;
413 else
414 EltTy = Result->isPointerType() ?
415 Result->getAs<PointerType>()->getPointeeType() :
416 Result->getAs<ReferenceType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000417
Douglas Gregorbad0e652009-03-24 20:32:41 +0000418 // If we have a pointer or reference, the pointee must have an object
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000419 // incomplete type.
420 if (!EltTy->isIncompleteOrObjectType()) {
Chris Lattner1564e392009-10-25 18:07:27 +0000421 TheSema.Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000422 diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattnerd1625842008-11-24 06:25:27 +0000423 << EltTy << DS.getSourceRange();
John McCall0953e762009-09-24 19:53:00 +0000424 TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000425 }
426 } else {
Chris Lattner1564e392009-10-25 18:07:27 +0000427 TheSema.Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000428 diag::err_typecheck_invalid_restrict_not_pointer)
Chris Lattnerd1625842008-11-24 06:25:27 +0000429 << Result << DS.getSourceRange();
John McCall0953e762009-09-24 19:53:00 +0000430 TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier.
Chris Lattner96b77fc2008-04-02 06:50:17 +0000431 }
Chris Lattner96b77fc2008-04-02 06:50:17 +0000432 }
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Chris Lattner96b77fc2008-04-02 06:50:17 +0000434 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
435 // of a function type includes any type qualifiers, the behavior is
436 // undefined."
437 if (Result->isFunctionType() && TypeQuals) {
438 // Get some location to point at, either the C or V location.
439 SourceLocation Loc;
John McCall0953e762009-09-24 19:53:00 +0000440 if (TypeQuals & DeclSpec::TQ_const)
Chris Lattner96b77fc2008-04-02 06:50:17 +0000441 Loc = DS.getConstSpecLoc();
John McCall0953e762009-09-24 19:53:00 +0000442 else if (TypeQuals & DeclSpec::TQ_volatile)
Chris Lattner96b77fc2008-04-02 06:50:17 +0000443 Loc = DS.getVolatileSpecLoc();
John McCall0953e762009-09-24 19:53:00 +0000444 else {
445 assert((TypeQuals & DeclSpec::TQ_restrict) &&
446 "Has CVR quals but not C, V, or R?");
447 Loc = DS.getRestrictSpecLoc();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000448 }
Chris Lattner1564e392009-10-25 18:07:27 +0000449 TheSema.Diag(Loc, diag::warn_typecheck_function_qualifiers)
Chris Lattnerd1625842008-11-24 06:25:27 +0000450 << Result << DS.getSourceRange();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000451 }
Mike Stump1eb44332009-09-09 15:08:12 +0000452
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000453 // C++ [dcl.ref]p1:
454 // Cv-qualified references are ill-formed except when the
455 // cv-qualifiers are introduced through the use of a typedef
456 // (7.1.3) or of a template type argument (14.3), in which
457 // case the cv-qualifiers are ignored.
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000458 // FIXME: Shouldn't we be checking SCS_typedef here?
459 if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000460 TypeQuals && Result->isReferenceType()) {
John McCall0953e762009-09-24 19:53:00 +0000461 TypeQuals &= ~DeclSpec::TQ_const;
462 TypeQuals &= ~DeclSpec::TQ_volatile;
Mike Stump1eb44332009-09-09 15:08:12 +0000463 }
464
John McCall0953e762009-09-24 19:53:00 +0000465 Qualifiers Quals = Qualifiers::fromCVRMask(TypeQuals);
466 Result = Context.getQualifiedType(Result, Quals);
Chris Lattner96b77fc2008-04-02 06:50:17 +0000467 }
John McCall0953e762009-09-24 19:53:00 +0000468
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000469 return Result;
470}
471
Douglas Gregorcd281c32009-02-28 00:25:32 +0000472static std::string getPrintableNameForEntity(DeclarationName Entity) {
473 if (Entity)
474 return Entity.getAsString();
Mike Stump1eb44332009-09-09 15:08:12 +0000475
Douglas Gregorcd281c32009-02-28 00:25:32 +0000476 return "type name";
477}
478
John McCall28654742010-06-05 06:41:15 +0000479QualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc,
480 Qualifiers Qs) {
481 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
482 // object or incomplete types shall not be restrict-qualified."
483 if (Qs.hasRestrict()) {
484 unsigned DiagID = 0;
485 QualType ProblemTy;
486
487 const Type *Ty = T->getCanonicalTypeInternal().getTypePtr();
488 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(Ty)) {
489 if (!RTy->getPointeeType()->isIncompleteOrObjectType()) {
490 DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
491 ProblemTy = T->getAs<ReferenceType>()->getPointeeType();
492 }
493 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
494 if (!PTy->getPointeeType()->isIncompleteOrObjectType()) {
495 DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
496 ProblemTy = T->getAs<PointerType>()->getPointeeType();
497 }
498 } else if (const MemberPointerType *PTy = dyn_cast<MemberPointerType>(Ty)) {
499 if (!PTy->getPointeeType()->isIncompleteOrObjectType()) {
500 DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
501 ProblemTy = T->getAs<PointerType>()->getPointeeType();
502 }
503 } else if (!Ty->isDependentType()) {
504 // FIXME: this deserves a proper diagnostic
505 DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
506 ProblemTy = T;
507 }
508
509 if (DiagID) {
510 Diag(Loc, DiagID) << ProblemTy;
511 Qs.removeRestrict();
512 }
513 }
514
515 return Context.getQualifiedType(T, Qs);
516}
517
Douglas Gregorcd281c32009-02-28 00:25:32 +0000518/// \brief Build a pointer type.
519///
520/// \param T The type to which we'll be building a pointer.
521///
Douglas Gregorcd281c32009-02-28 00:25:32 +0000522/// \param Loc The location of the entity whose type involves this
523/// pointer type or, if there is no such entity, the location of the
524/// type that will have pointer type.
525///
526/// \param Entity The name of the entity that involves the pointer
527/// type, if known.
528///
529/// \returns A suitable pointer type, if there are no
530/// errors. Otherwise, returns a NULL type.
John McCall28654742010-06-05 06:41:15 +0000531QualType Sema::BuildPointerType(QualType T,
Douglas Gregorcd281c32009-02-28 00:25:32 +0000532 SourceLocation Loc, DeclarationName Entity) {
533 if (T->isReferenceType()) {
534 // C++ 8.3.2p4: There shall be no ... pointers to references ...
535 Diag(Loc, diag::err_illegal_decl_pointer_to_reference)
John McCallac406052009-10-30 00:37:20 +0000536 << getPrintableNameForEntity(Entity) << T;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000537 return QualType();
538 }
539
John McCallc12c5bb2010-05-15 11:32:37 +0000540 assert(!T->isObjCObjectType() && "Should build ObjCObjectPointerType");
Douglas Gregor92e986e2010-04-22 16:44:27 +0000541
Douglas Gregorcd281c32009-02-28 00:25:32 +0000542 // Build the pointer type.
John McCall28654742010-06-05 06:41:15 +0000543 return Context.getPointerType(T);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000544}
545
546/// \brief Build a reference type.
547///
548/// \param T The type to which we'll be building a reference.
549///
Douglas Gregorcd281c32009-02-28 00:25:32 +0000550/// \param Loc The location of the entity whose type involves this
551/// reference type or, if there is no such entity, the location of the
552/// type that will have reference type.
553///
554/// \param Entity The name of the entity that involves the reference
555/// type, if known.
556///
557/// \returns A suitable reference type, if there are no
558/// errors. Otherwise, returns a NULL type.
John McCall54e14c42009-10-22 22:37:11 +0000559QualType Sema::BuildReferenceType(QualType T, bool SpelledAsLValue,
John McCall28654742010-06-05 06:41:15 +0000560 SourceLocation Loc,
John McCall54e14c42009-10-22 22:37:11 +0000561 DeclarationName Entity) {
John McCall54e14c42009-10-22 22:37:11 +0000562 bool LValueRef = SpelledAsLValue || T->getAs<LValueReferenceType>();
563
564 // C++0x [dcl.typedef]p9: If a typedef TD names a type that is a
565 // reference to a type T, and attempt to create the type "lvalue
566 // reference to cv TD" creates the type "lvalue reference to T".
567 // We use the qualifiers (restrict or none) of the original reference,
568 // not the new ones. This is consistent with GCC.
569
570 // C++ [dcl.ref]p4: There shall be no references to references.
571 //
572 // According to C++ DR 106, references to references are only
573 // diagnosed when they are written directly (e.g., "int & &"),
574 // but not when they happen via a typedef:
575 //
576 // typedef int& intref;
577 // typedef intref& intref2;
578 //
579 // Parser::ParseDeclaratorInternal diagnoses the case where
580 // references are written directly; here, we handle the
581 // collapsing of references-to-references as described in C++
582 // DR 106 and amended by C++ DR 540.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000583
584 // C++ [dcl.ref]p1:
Eli Friedman33a31382009-08-05 19:21:58 +0000585 // A declarator that specifies the type "reference to cv void"
Douglas Gregorcd281c32009-02-28 00:25:32 +0000586 // is ill-formed.
587 if (T->isVoidType()) {
588 Diag(Loc, diag::err_reference_to_void);
589 return QualType();
590 }
591
Douglas Gregorcd281c32009-02-28 00:25:32 +0000592 // Handle restrict on references.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000593 if (LValueRef)
John McCall28654742010-06-05 06:41:15 +0000594 return Context.getLValueReferenceType(T, SpelledAsLValue);
595 return Context.getRValueReferenceType(T);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000596}
597
598/// \brief Build an array type.
599///
600/// \param T The type of each element in the array.
601///
602/// \param ASM C99 array size modifier (e.g., '*', 'static').
Mike Stump1eb44332009-09-09 15:08:12 +0000603///
604/// \param ArraySize Expression describing the size of the array.
Douglas Gregorcd281c32009-02-28 00:25:32 +0000605///
Douglas Gregorcd281c32009-02-28 00:25:32 +0000606/// \param Loc The location of the entity whose type involves this
607/// array type or, if there is no such entity, the location of the
608/// type that will have array type.
609///
610/// \param Entity The name of the entity that involves the array
611/// type, if known.
612///
613/// \returns A suitable array type, if there are no errors. Otherwise,
614/// returns a NULL type.
615QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM,
616 Expr *ArraySize, unsigned Quals,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000617 SourceRange Brackets, DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000618
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000619 SourceLocation Loc = Brackets.getBegin();
Sebastian Redl923d56d2009-11-05 15:52:31 +0000620 if (getLangOptions().CPlusPlus) {
Douglas Gregor138bb232010-04-27 19:38:14 +0000621 // C++ [dcl.array]p1:
622 // T is called the array element type; this type shall not be a reference
623 // type, the (possibly cv-qualified) type void, a function type or an
624 // abstract class type.
625 //
626 // Note: function types are handled in the common path with C.
627 if (T->isReferenceType()) {
628 Diag(Loc, diag::err_illegal_decl_array_of_references)
629 << getPrintableNameForEntity(Entity) << T;
630 return QualType();
631 }
632
Sebastian Redl923d56d2009-11-05 15:52:31 +0000633 if (T->isVoidType()) {
634 Diag(Loc, diag::err_illegal_decl_array_incomplete_type) << T;
635 return QualType();
636 }
Douglas Gregor138bb232010-04-27 19:38:14 +0000637
638 if (RequireNonAbstractType(Brackets.getBegin(), T,
639 diag::err_array_of_abstract_type))
640 return QualType();
641
Sebastian Redl923d56d2009-11-05 15:52:31 +0000642 } else {
Douglas Gregor138bb232010-04-27 19:38:14 +0000643 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
644 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
Sebastian Redl923d56d2009-11-05 15:52:31 +0000645 if (RequireCompleteType(Loc, T,
646 diag::err_illegal_decl_array_incomplete_type))
647 return QualType();
648 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000649
650 if (T->isFunctionType()) {
651 Diag(Loc, diag::err_illegal_decl_array_of_functions)
John McCallac406052009-10-30 00:37:20 +0000652 << getPrintableNameForEntity(Entity) << T;
Douglas Gregorcd281c32009-02-28 00:25:32 +0000653 return QualType();
654 }
Mike Stump1eb44332009-09-09 15:08:12 +0000655
Anders Carlssone7cf07d2009-06-26 19:33:28 +0000656 if (Context.getCanonicalType(T) == Context.UndeducedAutoTy) {
Mike Stump1eb44332009-09-09 15:08:12 +0000657 Diag(Loc, diag::err_illegal_decl_array_of_auto)
Anders Carlssone7cf07d2009-06-26 19:33:28 +0000658 << getPrintableNameForEntity(Entity);
659 return QualType();
660 }
Mike Stump1eb44332009-09-09 15:08:12 +0000661
Ted Kremenek6217b802009-07-29 21:53:49 +0000662 if (const RecordType *EltTy = T->getAs<RecordType>()) {
Douglas Gregorcd281c32009-02-28 00:25:32 +0000663 // If the element type is a struct or union that contains a variadic
664 // array, accept it as a GNU extension: C99 6.7.2.1p2.
665 if (EltTy->getDecl()->hasFlexibleArrayMember())
666 Diag(Loc, diag::ext_flexible_array_in_array) << T;
John McCallc12c5bb2010-05-15 11:32:37 +0000667 } else if (T->isObjCObjectType()) {
Chris Lattnerc7c11b12009-04-27 01:55:56 +0000668 Diag(Loc, diag::err_objc_array_of_interfaces) << T;
669 return QualType();
Douglas Gregorcd281c32009-02-28 00:25:32 +0000670 }
Mike Stump1eb44332009-09-09 15:08:12 +0000671
Douglas Gregorcd281c32009-02-28 00:25:32 +0000672 // C99 6.7.5.2p1: The size expression shall have integer type.
673 if (ArraySize && !ArraySize->isTypeDependent() &&
674 !ArraySize->getType()->isIntegerType()) {
675 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
676 << ArraySize->getType() << ArraySize->getSourceRange();
677 ArraySize->Destroy(Context);
678 return QualType();
679 }
680 llvm::APSInt ConstVal(32);
681 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();
Mike Stump1eb44332009-09-09 15:08:12 +0000710 }
John McCall46a617a2009-10-16 00:14:28 +0000711 T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000712 }
David Chisnallaf407762010-01-11 23:08:08 +0000713 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
714 if (!getLangOptions().C99) {
Douglas Gregor0fddb972010-05-22 16:17:30 +0000715 if (T->isVariableArrayType()) {
716 // Prohibit the use of non-POD types in VLAs.
Douglas Gregor204ce172010-05-24 20:42:30 +0000717 if (!T->isDependentType() &&
718 !Context.getBaseElementType(T)->isPODType()) {
Douglas Gregor0fddb972010-05-22 16:17:30 +0000719 Diag(Loc, diag::err_vla_non_pod)
720 << Context.getBaseElementType(T);
721 return QualType();
722 }
Douglas Gregora481ec42010-05-23 19:57:01 +0000723 // Prohibit the use of VLAs during template argument deduction.
724 else if (isSFINAEContext()) {
725 Diag(Loc, diag::err_vla_in_sfinae);
726 return QualType();
727 }
Douglas Gregor0fddb972010-05-22 16:17:30 +0000728 // Just extwarn about VLAs.
729 else
730 Diag(Loc, diag::ext_vla);
731 } else if (ASM != ArrayType::Normal || Quals != 0)
Douglas Gregor043cad22009-09-11 00:18:58 +0000732 Diag(Loc,
733 getLangOptions().CPlusPlus? diag::err_c99_array_usage_cxx
734 : diag::ext_c99_array_usage);
Douglas Gregorcd281c32009-02-28 00:25:32 +0000735 }
736
737 return T;
738}
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000739
740/// \brief Build an ext-vector type.
741///
742/// Run the required checks for the extended vector type.
Mike Stump1eb44332009-09-09 15:08:12 +0000743QualType Sema::BuildExtVectorType(QualType T, ExprArg ArraySize,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000744 SourceLocation AttrLoc) {
745
746 Expr *Arg = (Expr *)ArraySize.get();
747
748 // unlike gcc's vector_size attribute, we do not allow vectors to be defined
749 // in conjunction with complex types (pointers, arrays, functions, etc.).
Mike Stump1eb44332009-09-09 15:08:12 +0000750 if (!T->isDependentType() &&
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000751 !T->isIntegerType() && !T->isRealFloatingType()) {
752 Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T;
753 return QualType();
754 }
755
756 if (!Arg->isTypeDependent() && !Arg->isValueDependent()) {
757 llvm::APSInt vecSize(32);
758 if (!Arg->isIntegerConstantExpr(vecSize, Context)) {
759 Diag(AttrLoc, diag::err_attribute_argument_not_int)
760 << "ext_vector_type" << Arg->getSourceRange();
761 return QualType();
762 }
Mike Stump1eb44332009-09-09 15:08:12 +0000763
764 // unlike gcc's vector_size attribute, the size is specified as the
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000765 // number of elements, not the number of bytes.
Mike Stump1eb44332009-09-09 15:08:12 +0000766 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
767
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000768 if (vectorSize == 0) {
769 Diag(AttrLoc, diag::err_attribute_zero_size)
770 << Arg->getSourceRange();
771 return QualType();
772 }
Mike Stump1eb44332009-09-09 15:08:12 +0000773
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000774 if (!T->isDependentType())
775 return Context.getExtVectorType(T, vectorSize);
Mike Stump1eb44332009-09-09 15:08:12 +0000776 }
777
778 return Context.getDependentSizedExtVectorType(T, ArraySize.takeAs<Expr>(),
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000779 AttrLoc);
780}
Mike Stump1eb44332009-09-09 15:08:12 +0000781
Douglas Gregor724651c2009-02-28 01:04:19 +0000782/// \brief Build a function type.
783///
784/// This routine checks the function type according to C++ rules and
785/// under the assumption that the result type and parameter types have
786/// just been instantiated from a template. It therefore duplicates
Douglas Gregor2943aed2009-03-03 04:44:36 +0000787/// some of the behavior of GetTypeForDeclarator, but in a much
Douglas Gregor724651c2009-02-28 01:04:19 +0000788/// simpler form that is only suitable for this narrow use case.
789///
790/// \param T The return type of the function.
791///
792/// \param ParamTypes The parameter types of the function. This array
793/// will be modified to account for adjustments to the types of the
794/// function parameters.
795///
796/// \param NumParamTypes The number of parameter types in ParamTypes.
797///
798/// \param Variadic Whether this is a variadic function type.
799///
800/// \param Quals The cvr-qualifiers to be applied to the function type.
801///
802/// \param Loc The location of the entity whose type involves this
803/// function type or, if there is no such entity, the location of the
804/// type that will have function type.
805///
806/// \param Entity The name of the entity that involves the function
807/// type, if known.
808///
809/// \returns A suitable function type, if there are no
810/// errors. Otherwise, returns a NULL type.
811QualType Sema::BuildFunctionType(QualType T,
Mike Stump1eb44332009-09-09 15:08:12 +0000812 QualType *ParamTypes,
Douglas Gregor724651c2009-02-28 01:04:19 +0000813 unsigned NumParamTypes,
814 bool Variadic, unsigned Quals,
815 SourceLocation Loc, DeclarationName Entity) {
816 if (T->isArrayType() || T->isFunctionType()) {
Douglas Gregor58408bc2010-01-11 18:46:21 +0000817 Diag(Loc, diag::err_func_returning_array_function)
818 << T->isFunctionType() << T;
Douglas Gregor724651c2009-02-28 01:04:19 +0000819 return QualType();
820 }
Mike Stump1eb44332009-09-09 15:08:12 +0000821
Douglas Gregor724651c2009-02-28 01:04:19 +0000822 bool Invalid = false;
823 for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) {
Douglas Gregor2dc0e642009-03-23 23:06:20 +0000824 QualType ParamType = adjustParameterType(ParamTypes[Idx]);
825 if (ParamType->isVoidType()) {
Douglas Gregor724651c2009-02-28 01:04:19 +0000826 Diag(Loc, diag::err_param_with_void_type);
827 Invalid = true;
828 }
Douglas Gregorcd281c32009-02-28 00:25:32 +0000829
John McCall54e14c42009-10-22 22:37:11 +0000830 ParamTypes[Idx] = ParamType;
Douglas Gregor724651c2009-02-28 01:04:19 +0000831 }
832
833 if (Invalid)
834 return QualType();
835
Mike Stump1eb44332009-09-09 15:08:12 +0000836 return Context.getFunctionType(T, ParamTypes, NumParamTypes, Variadic,
Rafael Espindola264ba482010-03-30 20:24:48 +0000837 Quals, false, false, 0, 0,
838 FunctionType::ExtInfo());
Douglas Gregor724651c2009-02-28 01:04:19 +0000839}
Mike Stump1eb44332009-09-09 15:08:12 +0000840
Douglas Gregor949bf692009-06-09 22:17:39 +0000841/// \brief Build a member pointer type \c T Class::*.
842///
843/// \param T the type to which the member pointer refers.
844/// \param Class the class type into which the member pointer points.
John McCall0953e762009-09-24 19:53:00 +0000845/// \param CVR Qualifiers applied to the member pointer type
Douglas Gregor949bf692009-06-09 22:17:39 +0000846/// \param Loc the location where this type begins
847/// \param Entity the name of the entity that will have this member pointer type
848///
849/// \returns a member pointer type, if successful, or a NULL type if there was
850/// an error.
Mike Stump1eb44332009-09-09 15:08:12 +0000851QualType Sema::BuildMemberPointerType(QualType T, QualType Class,
John McCall28654742010-06-05 06:41:15 +0000852 SourceLocation Loc,
Douglas Gregor949bf692009-06-09 22:17:39 +0000853 DeclarationName Entity) {
854 // Verify that we're not building a pointer to pointer to function with
855 // exception specification.
856 if (CheckDistantExceptionSpec(T)) {
857 Diag(Loc, diag::err_distant_exception_spec);
858
859 // FIXME: If we're doing this as part of template instantiation,
860 // we should return immediately.
861
862 // Build the type anyway, but use the canonical type so that the
863 // exception specifiers are stripped off.
864 T = Context.getCanonicalType(T);
865 }
866
Sebastian Redl73780122010-06-09 21:19:43 +0000867 // C++ 8.3.3p3: A pointer to member shall not point to ... a member
Douglas Gregor949bf692009-06-09 22:17:39 +0000868 // with reference type, or "cv void."
869 if (T->isReferenceType()) {
Anders Carlsson8d4655d2009-06-30 00:06:57 +0000870 Diag(Loc, diag::err_illegal_decl_mempointer_to_reference)
John McCallac406052009-10-30 00:37:20 +0000871 << (Entity? Entity.getAsString() : "type name") << T;
Douglas Gregor949bf692009-06-09 22:17:39 +0000872 return QualType();
873 }
874
875 if (T->isVoidType()) {
876 Diag(Loc, diag::err_illegal_decl_mempointer_to_void)
877 << (Entity? Entity.getAsString() : "type name");
878 return QualType();
879 }
880
Douglas Gregor949bf692009-06-09 22:17:39 +0000881 if (!Class->isDependentType() && !Class->isRecordType()) {
882 Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class;
883 return QualType();
884 }
885
John McCall28654742010-06-05 06:41:15 +0000886 return Context.getMemberPointerType(T, Class.getTypePtr());
Douglas Gregor949bf692009-06-09 22:17:39 +0000887}
Mike Stump1eb44332009-09-09 15:08:12 +0000888
Anders Carlsson9a917e42009-06-12 22:56:54 +0000889/// \brief Build a block pointer type.
890///
891/// \param T The type to which we'll be building a block pointer.
892///
John McCall0953e762009-09-24 19:53:00 +0000893/// \param CVR The cvr-qualifiers to be applied to the block pointer type.
Anders Carlsson9a917e42009-06-12 22:56:54 +0000894///
895/// \param Loc The location of the entity whose type involves this
896/// block pointer type or, if there is no such entity, the location of the
897/// type that will have block pointer type.
898///
899/// \param Entity The name of the entity that involves the block pointer
900/// type, if known.
901///
902/// \returns A suitable block pointer type, if there are no
903/// errors. Otherwise, returns a NULL type.
John McCall28654742010-06-05 06:41:15 +0000904QualType Sema::BuildBlockPointerType(QualType T,
Mike Stump1eb44332009-09-09 15:08:12 +0000905 SourceLocation Loc,
Anders Carlsson9a917e42009-06-12 22:56:54 +0000906 DeclarationName Entity) {
John McCall0953e762009-09-24 19:53:00 +0000907 if (!T->isFunctionType()) {
Anders Carlsson9a917e42009-06-12 22:56:54 +0000908 Diag(Loc, diag::err_nonfunction_block_type);
909 return QualType();
910 }
Mike Stump1eb44332009-09-09 15:08:12 +0000911
John McCall28654742010-06-05 06:41:15 +0000912 return Context.getBlockPointerType(T);
Anders Carlsson9a917e42009-06-12 22:56:54 +0000913}
914
John McCalla93c9342009-12-07 02:54:59 +0000915QualType Sema::GetTypeFromParser(TypeTy *Ty, TypeSourceInfo **TInfo) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000916 QualType QT = QualType::getFromOpaquePtr(Ty);
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000917 if (QT.isNull()) {
John McCalla93c9342009-12-07 02:54:59 +0000918 if (TInfo) *TInfo = 0;
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000919 return QualType();
920 }
921
John McCalla93c9342009-12-07 02:54:59 +0000922 TypeSourceInfo *DI = 0;
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000923 if (LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) {
924 QT = LIT->getType();
John McCalla93c9342009-12-07 02:54:59 +0000925 DI = LIT->getTypeSourceInfo();
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000926 }
Mike Stump1eb44332009-09-09 15:08:12 +0000927
John McCalla93c9342009-12-07 02:54:59 +0000928 if (TInfo) *TInfo = DI;
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +0000929 return QT;
930}
931
Mike Stump98eb8a72009-02-04 22:31:32 +0000932/// GetTypeForDeclarator - Convert the type for the specified
Sebastian Redl8ce35b02009-10-25 21:45:37 +0000933/// declarator to Type instances.
Douglas Gregor402abb52009-05-28 23:31:59 +0000934///
935/// If OwnedDecl is non-NULL, and this declarator's decl-specifier-seq
936/// owns the declaration of a type (e.g., the definition of a struct
937/// type), then *OwnedDecl will receive the owned declaration.
John McCallbf1a0282010-06-04 23:28:52 +0000938///
939/// The result of this call will never be null, but the associated
940/// type may be a null type if there's an unrecoverable error.
941TypeSourceInfo *Sema::GetTypeForDeclarator(Declarator &D, Scope *S,
942 TagDecl **OwnedDecl) {
Douglas Gregor930d8b52009-01-30 22:09:00 +0000943 // Determine the type of the declarator. Not all forms of declarator
944 // have a type.
945 QualType T;
Douglas Gregor05baacb2010-04-12 23:19:01 +0000946 TypeSourceInfo *ReturnTypeInfo = 0;
947
John McCall04a67a62010-02-05 21:31:56 +0000948 llvm::SmallVector<DelayedAttribute,4> FnAttrsFromDeclSpec;
949
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000950 switch (D.getName().getKind()) {
951 case UnqualifiedId::IK_Identifier:
952 case UnqualifiedId::IK_OperatorFunctionId:
Sean Hunt0486d742009-11-28 04:44:28 +0000953 case UnqualifiedId::IK_LiteralOperatorId:
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000954 case UnqualifiedId::IK_TemplateId:
John McCall04a67a62010-02-05 21:31:56 +0000955 T = ConvertDeclSpecToType(*this, D, FnAttrsFromDeclSpec);
Chris Lattner5db2bb12009-10-25 18:21:37 +0000956
Douglas Gregor591bd3c2010-02-08 22:07:33 +0000957 if (!D.isInvalidType() && D.getDeclSpec().isTypeSpecOwned()) {
958 TagDecl* Owned = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep());
Douglas Gregorb37b6482010-02-12 17:40:34 +0000959 // Owned is embedded if it was defined here, or if it is the
960 // very first (i.e., canonical) declaration of this tag type.
961 Owned->setEmbeddedInDeclarator(Owned->isDefinition() ||
962 Owned->isCanonicalDecl());
Douglas Gregor591bd3c2010-02-08 22:07:33 +0000963 if (OwnedDecl) *OwnedDecl = Owned;
964 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000965 break;
966
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000967 case UnqualifiedId::IK_ConstructorName:
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000968 case UnqualifiedId::IK_ConstructorTemplateId:
Douglas Gregor3f9a0562009-11-03 01:35:08 +0000969 case UnqualifiedId::IK_DestructorName:
Douglas Gregor930d8b52009-01-30 22:09:00 +0000970 // Constructors and destructors don't have return types. Use
Douglas Gregor48026d22010-01-11 18:40:55 +0000971 // "void" instead.
Douglas Gregor930d8b52009-01-30 22:09:00 +0000972 T = Context.VoidTy;
973 break;
Douglas Gregor48026d22010-01-11 18:40:55 +0000974
975 case UnqualifiedId::IK_ConversionFunctionId:
976 // The result type of a conversion function is the type that it
977 // converts to.
Douglas Gregor05baacb2010-04-12 23:19:01 +0000978 T = GetTypeFromParser(D.getName().ConversionFunctionId,
John McCallbf1a0282010-06-04 23:28:52 +0000979 &ReturnTypeInfo);
Douglas Gregor48026d22010-01-11 18:40:55 +0000980 break;
Douglas Gregor930d8b52009-01-30 22:09:00 +0000981 }
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000982
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000983 if (T.isNull())
John McCallbf1a0282010-06-04 23:28:52 +0000984 return Context.getNullTypeSourceInfo();
Douglas Gregor1f5f3a42009-12-03 17:10:37 +0000985
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000986 if (T == Context.UndeducedAutoTy) {
987 int Error = -1;
Mike Stump1eb44332009-09-09 15:08:12 +0000988
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000989 switch (D.getContext()) {
990 case Declarator::KNRTypeListContext:
991 assert(0 && "K&R type lists aren't allowed in C++");
992 break;
Anders Carlssonbaf45d32009-06-26 22:18:59 +0000993 case Declarator::PrototypeContext:
994 Error = 0; // Function prototype
995 break;
996 case Declarator::MemberContext:
997 switch (cast<TagDecl>(CurContext)->getTagKind()) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000998 case TTK_Enum: assert(0 && "unhandled tag kind"); break;
999 case TTK_Struct: Error = 1; /* Struct member */ break;
1000 case TTK_Union: Error = 2; /* Union member */ break;
1001 case TTK_Class: Error = 3; /* Class member */ break;
Mike Stump1eb44332009-09-09 15:08:12 +00001002 }
Anders Carlssonbaf45d32009-06-26 22:18:59 +00001003 break;
1004 case Declarator::CXXCatchContext:
1005 Error = 4; // Exception declaration
1006 break;
1007 case Declarator::TemplateParamContext:
1008 Error = 5; // Template parameter
1009 break;
1010 case Declarator::BlockLiteralContext:
1011 Error = 6; // Block literal
1012 break;
1013 case Declarator::FileContext:
1014 case Declarator::BlockContext:
1015 case Declarator::ForContext:
1016 case Declarator::ConditionContext:
1017 case Declarator::TypeNameContext:
1018 break;
1019 }
1020
1021 if (Error != -1) {
1022 Diag(D.getDeclSpec().getTypeSpecTypeLoc(), diag::err_auto_not_allowed)
1023 << Error;
1024 T = Context.IntTy;
1025 D.setInvalidType(true);
1026 }
1027 }
Mike Stump1eb44332009-09-09 15:08:12 +00001028
Douglas Gregorcd281c32009-02-28 00:25:32 +00001029 // The name we're declaring, if any.
1030 DeclarationName Name;
1031 if (D.getIdentifier())
1032 Name = D.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00001033
John McCall04a67a62010-02-05 21:31:56 +00001034 llvm::SmallVector<DelayedAttribute,4> FnAttrsFromPreviousChunk;
1035
Mike Stump98eb8a72009-02-04 22:31:32 +00001036 // Walk the DeclTypeInfo, building the recursive type as we go.
1037 // DeclTypeInfos are ordered from the identifier out, which is
1038 // opposite of what we want :).
Sebastian Redl8ce35b02009-10-25 21:45:37 +00001039 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
1040 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1);
Reid Spencer5f016e22007-07-11 17:01:13 +00001041 switch (DeclType.Kind) {
1042 default: assert(0 && "Unknown decltype!");
Steve Naroff5618bd42008-08-27 16:04:49 +00001043 case DeclaratorChunk::BlockPointer:
Chris Lattner9af55002009-03-27 04:18:06 +00001044 // If blocks are disabled, emit an error.
1045 if (!LangOpts.Blocks)
1046 Diag(DeclType.Loc, diag::err_blocks_disable);
Mike Stump1eb44332009-09-09 15:08:12 +00001047
John McCall28654742010-06-05 06:41:15 +00001048 T = BuildBlockPointerType(T, D.getIdentifierLoc(), Name);
1049 if (DeclType.Cls.TypeQuals)
1050 T = BuildQualifiedType(T, DeclType.Loc, DeclType.Cls.TypeQuals);
Steve Naroff5618bd42008-08-27 16:04:49 +00001051 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001052 case DeclaratorChunk::Pointer:
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001053 // Verify that we're not building a pointer to pointer to function with
1054 // exception specification.
1055 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1056 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1057 D.setInvalidType(true);
1058 // Build the type anyway.
1059 }
John McCallc12c5bb2010-05-15 11:32:37 +00001060 if (getLangOptions().ObjC1 && T->getAs<ObjCObjectType>()) {
1061 T = Context.getObjCObjectPointerType(T);
John McCall28654742010-06-05 06:41:15 +00001062 if (DeclType.Ptr.TypeQuals)
1063 T = BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals);
Steve Naroff14108da2009-07-10 23:34:53 +00001064 break;
1065 }
John McCall28654742010-06-05 06:41:15 +00001066 T = BuildPointerType(T, DeclType.Loc, Name);
1067 if (DeclType.Ptr.TypeQuals)
1068 T = BuildQualifiedType(T, DeclType.Loc, DeclType.Ptr.TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001069 break;
John McCall0953e762009-09-24 19:53:00 +00001070 case DeclaratorChunk::Reference: {
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001071 // Verify that we're not building a reference to pointer to function with
1072 // exception specification.
1073 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1074 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1075 D.setInvalidType(true);
1076 // Build the type anyway.
1077 }
John McCall28654742010-06-05 06:41:15 +00001078 T = BuildReferenceType(T, DeclType.Ref.LValueRef, DeclType.Loc, Name);
1079
1080 Qualifiers Quals;
1081 if (DeclType.Ref.HasRestrict)
1082 T = BuildQualifiedType(T, DeclType.Loc, Qualifiers::Restrict);
Reid Spencer5f016e22007-07-11 17:01:13 +00001083 break;
John McCall0953e762009-09-24 19:53:00 +00001084 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001085 case DeclaratorChunk::Array: {
Sebastian Redl6a7330c2009-05-29 15:01:05 +00001086 // Verify that we're not building an array of pointers to function with
1087 // exception specification.
1088 if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) {
1089 Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);
1090 D.setInvalidType(true);
1091 // Build the type anyway.
1092 }
Chris Lattnerfd89bc82008-04-02 01:05:10 +00001093 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner94f81fd2007-08-28 16:54:00 +00001094 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001095 ArrayType::ArraySizeModifier ASM;
1096 if (ATI.isStar)
1097 ASM = ArrayType::Star;
1098 else if (ATI.hasStatic)
1099 ASM = ArrayType::Static;
1100 else
1101 ASM = ArrayType::Normal;
Eli Friedmanf91f5c82009-04-26 21:57:51 +00001102 if (ASM == ArrayType::Star &&
1103 D.getContext() != Declarator::PrototypeContext) {
1104 // FIXME: This check isn't quite right: it allows star in prototypes
1105 // for function definitions, and disallows some edge cases detailed
1106 // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html
1107 Diag(DeclType.Loc, diag::err_array_star_outside_prototype);
1108 ASM = ArrayType::Normal;
1109 D.setInvalidType(true);
1110 }
John McCall0953e762009-09-24 19:53:00 +00001111 T = BuildArrayType(T, ASM, ArraySize,
1112 Qualifiers::fromCVRMask(ATI.TypeQuals),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001113 SourceRange(DeclType.Loc, DeclType.EndLoc), Name);
Reid Spencer5f016e22007-07-11 17:01:13 +00001114 break;
1115 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001116 case DeclaratorChunk::Function: {
Reid Spencer5f016e22007-07-11 17:01:13 +00001117 // If the function declarator has a prototype (i.e. it is not () and
1118 // does not have a K&R-style identifier list), then the arguments are part
1119 // of the type, otherwise the argument list is ().
1120 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Sebastian Redl3cc97262009-05-31 11:47:27 +00001121
Chris Lattnercd881292007-12-19 05:31:29 +00001122 // C99 6.7.5.3p1: The return type may not be a function or array type.
Douglas Gregor58408bc2010-01-11 18:46:21 +00001123 // For conversion functions, we'll diagnose this particular error later.
Douglas Gregor48026d22010-01-11 18:40:55 +00001124 if ((T->isArrayType() || T->isFunctionType()) &&
1125 (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId)) {
Douglas Gregor58408bc2010-01-11 18:46:21 +00001126 Diag(DeclType.Loc, diag::err_func_returning_array_function)
1127 << T->isFunctionType() << T;
Chris Lattnercd881292007-12-19 05:31:29 +00001128 T = Context.IntTy;
1129 D.setInvalidType(true);
1130 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001131
Douglas Gregor402abb52009-05-28 23:31:59 +00001132 if (getLangOptions().CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) {
1133 // C++ [dcl.fct]p6:
1134 // Types shall not be defined in return or parameter types.
1135 TagDecl *Tag = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep());
1136 if (Tag->isDefinition())
1137 Diag(Tag->getLocation(), diag::err_type_defined_in_result_type)
1138 << Context.getTypeDeclType(Tag);
1139 }
1140
Sebastian Redl3cc97262009-05-31 11:47:27 +00001141 // Exception specs are not allowed in typedefs. Complain, but add it
1142 // anyway.
1143 if (FTI.hasExceptionSpec &&
1144 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
1145 Diag(FTI.getThrowLoc(), diag::err_exception_spec_in_typedef);
1146
John McCall28654742010-06-05 06:41:15 +00001147 if (!FTI.NumArgs && !FTI.isVariadic && !getLangOptions().CPlusPlus) {
1148 // Simple void foo(), where the incoming T is the result type.
1149 T = Context.getFunctionNoProtoType(T);
1150 } else {
1151 // We allow a zero-parameter variadic function in C if the
1152 // function is marked with the "overloadable" attribute. Scan
1153 // for this attribute now.
1154 if (!FTI.NumArgs && FTI.isVariadic && !getLangOptions().CPlusPlus) {
Douglas Gregor965acbb2009-02-18 07:07:28 +00001155 bool Overloadable = false;
1156 for (const AttributeList *Attrs = D.getAttributes();
1157 Attrs; Attrs = Attrs->getNext()) {
1158 if (Attrs->getKind() == AttributeList::AT_overloadable) {
1159 Overloadable = true;
1160 break;
1161 }
1162 }
1163
1164 if (!Overloadable)
1165 Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +00001166 }
John McCall28654742010-06-05 06:41:15 +00001167
1168 if (FTI.NumArgs && FTI.ArgInfo[0].Param == 0) {
Chris Lattner788b0fd2010-06-23 06:00:24 +00001169 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function
1170 // definition.
John McCall28654742010-06-05 06:41:15 +00001171 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
1172 D.setInvalidType(true);
1173 break;
1174 }
1175
Reid Spencer5f016e22007-07-11 17:01:13 +00001176 // Otherwise, we have a function with an argument list that is
1177 // potentially variadic.
1178 llvm::SmallVector<QualType, 16> ArgTys;
John McCall28654742010-06-05 06:41:15 +00001179 ArgTys.reserve(FTI.NumArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001180
Reid Spencer5f016e22007-07-11 17:01:13 +00001181 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001182 ParmVarDecl *Param =
1183 cast<ParmVarDecl>(FTI.ArgInfo[i].Param.getAs<Decl>());
Chris Lattner8123a952008-04-10 02:22:51 +00001184 QualType ArgTy = Param->getType();
Chris Lattner78c75fb2007-07-21 05:30:18 +00001185 assert(!ArgTy.isNull() && "Couldn't parse type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001186
1187 // Adjust the parameter type.
Douglas Gregorbeb58cb2009-03-23 23:17:00 +00001188 assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?");
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001189
Reid Spencer5f016e22007-07-11 17:01:13 +00001190 // Look for 'void'. void is allowed only as a single argument to a
1191 // function with no other parameters (C99 6.7.5.3p10). We record
Douglas Gregor72564e72009-02-26 23:50:07 +00001192 // int(void) as a FunctionProtoType with an empty argument list.
Douglas Gregor2dc0e642009-03-23 23:06:20 +00001193 if (ArgTy->isVoidType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001194 // If this is something like 'float(int, void)', reject it. 'void'
1195 // is an incomplete type (C99 6.2.5p19) and function decls cannot
1196 // have arguments of incomplete type.
1197 if (FTI.NumArgs != 1 || FTI.isVariadic) {
1198 Diag(DeclType.Loc, diag::err_void_only_param);
Chris Lattner2ff54262007-07-21 05:18:12 +00001199 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00001200 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00001201 } else if (FTI.ArgInfo[i].Ident) {
1202 // Reject, but continue to parse 'int(void abc)'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001203 Diag(FTI.ArgInfo[i].IdentLoc,
Chris Lattner4565d4e2007-07-21 05:26:43 +00001204 diag::err_param_with_void_type);
Chris Lattner2ff54262007-07-21 05:18:12 +00001205 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +00001206 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +00001207 } else {
1208 // Reject, but continue to parse 'float(const void)'.
John McCall0953e762009-09-24 19:53:00 +00001209 if (ArgTy.hasQualifiers())
Chris Lattner2ff54262007-07-21 05:18:12 +00001210 Diag(DeclType.Loc, diag::err_void_param_qualified);
Mike Stump1eb44332009-09-09 15:08:12 +00001211
Chris Lattner2ff54262007-07-21 05:18:12 +00001212 // Do not add 'void' to the ArgTys list.
1213 break;
1214 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001215 } else if (!FTI.hasPrototype) {
1216 if (ArgTy->isPromotableIntegerType()) {
Eli Friedmana95d7572009-08-19 07:44:53 +00001217 ArgTy = Context.getPromotedIntegerType(ArgTy);
John McCall183700f2009-09-21 23:43:11 +00001218 } else if (const BuiltinType* BTy = ArgTy->getAs<BuiltinType>()) {
Eli Friedmaneb4b7052008-08-25 21:31:01 +00001219 if (BTy->getKind() == BuiltinType::Float)
1220 ArgTy = Context.DoubleTy;
1221 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001222 }
Mike Stump1eb44332009-09-09 15:08:12 +00001223
John McCall54e14c42009-10-22 22:37:11 +00001224 ArgTys.push_back(ArgTy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001225 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001226
1227 llvm::SmallVector<QualType, 4> Exceptions;
1228 Exceptions.reserve(FTI.NumExceptions);
Mike Stump1eb44332009-09-09 15:08:12 +00001229 for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) {
Argyrios Kyrtzidise8661902009-08-19 01:28:28 +00001230 // FIXME: Preserve type source info.
1231 QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty);
Sebastian Redlef65f062009-05-29 18:02:33 +00001232 // Check that the type is valid for an exception spec, and drop it if
1233 // not.
1234 if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range))
1235 Exceptions.push_back(ET);
1236 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001237
Jay Foadbeaaccd2009-05-21 09:52:38 +00001238 T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(),
Sebastian Redl465226e2009-05-27 22:11:52 +00001239 FTI.isVariadic, FTI.TypeQuals,
1240 FTI.hasExceptionSpec,
1241 FTI.hasAnyExceptionSpec,
Douglas Gregorce056bc2010-02-21 22:15:06 +00001242 Exceptions.size(), Exceptions.data(),
Rafael Espindola264ba482010-03-30 20:24:48 +00001243 FunctionType::ExtInfo());
Reid Spencer5f016e22007-07-11 17:01:13 +00001244 }
John McCall04a67a62010-02-05 21:31:56 +00001245
1246 // For GCC compatibility, we allow attributes that apply only to
1247 // function types to be placed on a function's return type
1248 // instead (as long as that type doesn't happen to be function
1249 // or function-pointer itself).
1250 ProcessDelayedFnAttrs(*this, T, FnAttrsFromPreviousChunk);
1251
Reid Spencer5f016e22007-07-11 17:01:13 +00001252 break;
1253 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001254 case DeclaratorChunk::MemberPointer:
1255 // The scope spec must refer to a class, or be dependent.
Sebastian Redlf30208a2009-01-24 21:16:55 +00001256 QualType ClsType;
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00001257 if (DeclType.Mem.Scope().isInvalid()) {
1258 // Avoid emitting extra errors if we already errored on the scope.
1259 D.setInvalidType(true);
1260 } else if (isDependentScopeSpecifier(DeclType.Mem.Scope())
1261 || dyn_cast_or_null<CXXRecordDecl>(
Douglas Gregor87c12c42009-11-04 16:49:01 +00001262 computeDeclContext(DeclType.Mem.Scope()))) {
Mike Stump1eb44332009-09-09 15:08:12 +00001263 NestedNameSpecifier *NNS
Douglas Gregor949bf692009-06-09 22:17:39 +00001264 = (NestedNameSpecifier *)DeclType.Mem.Scope().getScopeRep();
Douglas Gregor87c12c42009-11-04 16:49:01 +00001265 NestedNameSpecifier *NNSPrefix = NNS->getPrefix();
1266 switch (NNS->getKind()) {
1267 case NestedNameSpecifier::Identifier:
Douglas Gregor4a2023f2010-03-31 20:19:30 +00001268 ClsType = Context.getDependentNameType(ETK_None, NNSPrefix,
1269 NNS->getAsIdentifier());
Douglas Gregor87c12c42009-11-04 16:49:01 +00001270 break;
1271
1272 case NestedNameSpecifier::Namespace:
1273 case NestedNameSpecifier::Global:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001274 llvm_unreachable("Nested-name-specifier must name a type");
Douglas Gregor87c12c42009-11-04 16:49:01 +00001275 break;
1276
1277 case NestedNameSpecifier::TypeSpec:
1278 case NestedNameSpecifier::TypeSpecWithTemplate:
1279 ClsType = QualType(NNS->getAsType(), 0);
1280 if (NNSPrefix)
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001281 ClsType = Context.getElaboratedType(ETK_None, NNSPrefix, ClsType);
Douglas Gregor87c12c42009-11-04 16:49:01 +00001282 break;
1283 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001284 } else {
Douglas Gregor949bf692009-06-09 22:17:39 +00001285 Diag(DeclType.Mem.Scope().getBeginLoc(),
1286 diag::err_illegal_decl_mempointer_in_nonclass)
1287 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
1288 << DeclType.Mem.Scope().getRange();
Sebastian Redlf30208a2009-01-24 21:16:55 +00001289 D.setInvalidType(true);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001290 }
1291
Douglas Gregor949bf692009-06-09 22:17:39 +00001292 if (!ClsType.isNull())
John McCall28654742010-06-05 06:41:15 +00001293 T = BuildMemberPointerType(T, ClsType, DeclType.Loc, D.getIdentifier());
Douglas Gregor949bf692009-06-09 22:17:39 +00001294 if (T.isNull()) {
1295 T = Context.IntTy;
Sebastian Redlf30208a2009-01-24 21:16:55 +00001296 D.setInvalidType(true);
John McCall28654742010-06-05 06:41:15 +00001297 } else if (DeclType.Mem.TypeQuals) {
1298 T = BuildQualifiedType(T, DeclType.Loc, DeclType.Mem.TypeQuals);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001299 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00001300 break;
1301 }
1302
Douglas Gregorcd281c32009-02-28 00:25:32 +00001303 if (T.isNull()) {
1304 D.setInvalidType(true);
1305 T = Context.IntTy;
1306 }
1307
John McCall04a67a62010-02-05 21:31:56 +00001308 DiagnoseDelayedFnAttrs(*this, FnAttrsFromPreviousChunk);
1309
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001310 // See if there are any attributes on this declarator chunk.
1311 if (const AttributeList *AL = DeclType.getAttrs())
Charles Davis328ce342010-02-24 02:27:18 +00001312 ProcessTypeAttributeList(*this, T, false, AL, FnAttrsFromPreviousChunk);
Reid Spencer5f016e22007-07-11 17:01:13 +00001313 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001314
1315 if (getLangOptions().CPlusPlus && T->isFunctionType()) {
John McCall183700f2009-09-21 23:43:11 +00001316 const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>();
Chris Lattner778ed742009-10-25 17:36:50 +00001317 assert(FnTy && "Why oh why is there not a FunctionProtoType here?");
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001318
1319 // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type
1320 // for a nonstatic member function, the function type to which a pointer
1321 // to member refers, or the top-level function type of a function typedef
1322 // declaration.
Sebastian Redlc61bb202010-07-09 21:26:08 +00001323 bool FreeFunction = (D.getContext() != Declarator::MemberContext &&
1324 (!D.getCXXScopeSpec().isSet() ||
1325 !computeDeclContext(D.getCXXScopeSpec(), /*FIXME:*/true)->isRecord()));
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001326 if (FnTy->getTypeQuals() != 0 &&
1327 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
Sebastian Redlc61bb202010-07-09 21:26:08 +00001328 (FreeFunction ||
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001329 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001330 if (D.isFunctionDeclarator())
1331 Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
1332 else
1333 Diag(D.getIdentifierLoc(),
Sebastian Redlc61bb202010-07-09 21:26:08 +00001334 diag::err_invalid_qualified_typedef_function_type_use)
1335 << FreeFunction;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001336
1337 // Strip the cv-quals from the type.
1338 T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
Douglas Gregorce056bc2010-02-21 22:15:06 +00001339 FnTy->getNumArgs(), FnTy->isVariadic(), 0,
Rafael Espindola264ba482010-03-30 20:24:48 +00001340 false, false, 0, 0, FunctionType::ExtInfo());
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001341 }
1342 }
Mike Stump1eb44332009-09-09 15:08:12 +00001343
Sebastian Redl73780122010-06-09 21:19:43 +00001344 // If there's a constexpr specifier, treat it as a top-level const.
1345 if (D.getDeclSpec().isConstexprSpecified()) {
1346 T.addConst();
1347 }
1348
John McCall04a67a62010-02-05 21:31:56 +00001349 // Process any function attributes we might have delayed from the
1350 // declaration-specifiers.
1351 ProcessDelayedFnAttrs(*this, T, FnAttrsFromDeclSpec);
1352
1353 // If there were any type attributes applied to the decl itself, not
1354 // the type, apply them to the result type. But don't do this for
1355 // block-literal expressions, which are parsed wierdly.
1356 if (D.getContext() != Declarator::BlockLiteralContext)
1357 if (const AttributeList *Attrs = D.getAttributes())
Charles Davis328ce342010-02-24 02:27:18 +00001358 ProcessTypeAttributeList(*this, T, false, Attrs,
1359 FnAttrsFromPreviousChunk);
John McCall04a67a62010-02-05 21:31:56 +00001360
1361 DiagnoseDelayedFnAttrs(*this, FnAttrsFromPreviousChunk);
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001362
John McCallbf1a0282010-06-04 23:28:52 +00001363 if (T.isNull())
1364 return Context.getNullTypeSourceInfo();
1365 else if (D.isInvalidType())
1366 return Context.getTrivialTypeSourceInfo(T);
1367 return GetTypeSourceInfoForDeclarator(D, T, ReturnTypeInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +00001368}
1369
John McCall51bd8032009-10-18 01:05:36 +00001370namespace {
1371 class TypeSpecLocFiller : public TypeLocVisitor<TypeSpecLocFiller> {
1372 const DeclSpec &DS;
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001373
John McCall51bd8032009-10-18 01:05:36 +00001374 public:
1375 TypeSpecLocFiller(const DeclSpec &DS) : DS(DS) {}
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001376
John McCall51bd8032009-10-18 01:05:36 +00001377 void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
1378 Visit(TL.getUnqualifiedLoc());
1379 }
1380 void VisitTypedefTypeLoc(TypedefTypeLoc TL) {
1381 TL.setNameLoc(DS.getTypeSpecTypeLoc());
1382 }
1383 void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
1384 TL.setNameLoc(DS.getTypeSpecTypeLoc());
John McCallc12c5bb2010-05-15 11:32:37 +00001385 }
1386 void VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
1387 // Handle the base type, which might not have been written explicitly.
1388 if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) {
1389 TL.setHasBaseTypeAsWritten(false);
1390 TL.getBaseLoc().initialize(SourceLocation());
1391 } else {
1392 TL.setHasBaseTypeAsWritten(true);
1393 Visit(TL.getBaseLoc());
1394 }
Argyrios Kyrtzidiseb667592009-09-29 19:45:22 +00001395
John McCallc12c5bb2010-05-15 11:32:37 +00001396 // Protocol qualifiers.
John McCall54e14c42009-10-22 22:37:11 +00001397 if (DS.getProtocolQualifiers()) {
1398 assert(TL.getNumProtocols() > 0);
1399 assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers());
1400 TL.setLAngleLoc(DS.getProtocolLAngleLoc());
1401 TL.setRAngleLoc(DS.getSourceRange().getEnd());
1402 for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i)
1403 TL.setProtocolLoc(i, DS.getProtocolLocs()[i]);
1404 } else {
1405 assert(TL.getNumProtocols() == 0);
1406 TL.setLAngleLoc(SourceLocation());
1407 TL.setRAngleLoc(SourceLocation());
1408 }
1409 }
1410 void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
John McCall54e14c42009-10-22 22:37:11 +00001411 TL.setStarLoc(SourceLocation());
John McCallc12c5bb2010-05-15 11:32:37 +00001412 Visit(TL.getPointeeLoc());
John McCall51bd8032009-10-18 01:05:36 +00001413 }
John McCall833ca992009-10-29 08:12:44 +00001414 void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) {
John McCalla93c9342009-12-07 02:54:59 +00001415 TypeSourceInfo *TInfo = 0;
1416 Sema::GetTypeFromParser(DS.getTypeRep(), &TInfo);
John McCall833ca992009-10-29 08:12:44 +00001417
1418 // If we got no declarator info from previous Sema routines,
1419 // just fill with the typespec loc.
John McCalla93c9342009-12-07 02:54:59 +00001420 if (!TInfo) {
John McCall833ca992009-10-29 08:12:44 +00001421 TL.initialize(DS.getTypeSpecTypeLoc());
1422 return;
1423 }
1424
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00001425 TypeLoc OldTL = TInfo->getTypeLoc();
1426 if (TInfo->getType()->getAs<ElaboratedType>()) {
1427 ElaboratedTypeLoc ElabTL = cast<ElaboratedTypeLoc>(OldTL);
1428 TemplateSpecializationTypeLoc NamedTL =
1429 cast<TemplateSpecializationTypeLoc>(ElabTL.getNamedTypeLoc());
1430 TL.copy(NamedTL);
1431 }
1432 else
1433 TL.copy(cast<TemplateSpecializationTypeLoc>(OldTL));
John McCall833ca992009-10-29 08:12:44 +00001434 }
John McCallcfb708c2010-01-13 20:03:27 +00001435 void VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
1436 assert(DS.getTypeSpecType() == DeclSpec::TST_typeofExpr);
1437 TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
1438 TL.setParensRange(DS.getTypeofParensRange());
1439 }
1440 void VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
1441 assert(DS.getTypeSpecType() == DeclSpec::TST_typeofType);
1442 TL.setTypeofLoc(DS.getTypeSpecTypeLoc());
1443 TL.setParensRange(DS.getTypeofParensRange());
1444 assert(DS.getTypeRep());
1445 TypeSourceInfo *TInfo = 0;
1446 Sema::GetTypeFromParser(DS.getTypeRep(), &TInfo);
1447 TL.setUnderlyingTInfo(TInfo);
1448 }
Douglas Gregorddf889a2010-01-18 18:04:31 +00001449 void VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
1450 // By default, use the source location of the type specifier.
1451 TL.setBuiltinLoc(DS.getTypeSpecTypeLoc());
1452 if (TL.needsExtraLocalData()) {
1453 // Set info for the written builtin specifiers.
1454 TL.getWrittenBuiltinSpecs() = DS.getWrittenBuiltinSpecs();
1455 // Try to have a meaningful source location.
1456 if (TL.getWrittenSignSpec() != TSS_unspecified)
1457 // Sign spec loc overrides the others (e.g., 'unsigned long').
1458 TL.setBuiltinLoc(DS.getTypeSpecSignLoc());
1459 else if (TL.getWrittenWidthSpec() != TSW_unspecified)
1460 // Width spec loc overrides type spec loc (e.g., 'short int').
1461 TL.setBuiltinLoc(DS.getTypeSpecWidthLoc());
1462 }
1463 }
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00001464 void VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
1465 ElaboratedTypeKeyword Keyword
1466 = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
1467 if (Keyword == ETK_Typename) {
1468 TypeSourceInfo *TInfo = 0;
1469 Sema::GetTypeFromParser(DS.getTypeRep(), &TInfo);
1470 if (TInfo) {
1471 TL.copy(cast<ElaboratedTypeLoc>(TInfo->getTypeLoc()));
1472 return;
1473 }
1474 }
1475 TL.setKeywordLoc(Keyword != ETK_None
1476 ? DS.getTypeSpecTypeLoc()
1477 : SourceLocation());
1478 const CXXScopeSpec& SS = DS.getTypeSpecScope();
1479 TL.setQualifierRange(SS.isEmpty() ? SourceRange(): SS.getRange());
1480 Visit(TL.getNextTypeLoc().getUnqualifiedLoc());
1481 }
1482 void VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
1483 ElaboratedTypeKeyword Keyword
1484 = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
1485 if (Keyword == ETK_Typename) {
1486 TypeSourceInfo *TInfo = 0;
1487 Sema::GetTypeFromParser(DS.getTypeRep(), &TInfo);
1488 if (TInfo) {
1489 TL.copy(cast<DependentNameTypeLoc>(TInfo->getTypeLoc()));
1490 return;
1491 }
1492 }
1493 TL.setKeywordLoc(Keyword != ETK_None
1494 ? DS.getTypeSpecTypeLoc()
1495 : SourceLocation());
1496 const CXXScopeSpec& SS = DS.getTypeSpecScope();
1497 TL.setQualifierRange(SS.isEmpty() ? SourceRange() : SS.getRange());
1498 // FIXME: load appropriate source location.
1499 TL.setNameLoc(DS.getTypeSpecTypeLoc());
1500 }
John McCall33500952010-06-11 00:33:02 +00001501 void VisitDependentTemplateSpecializationTypeLoc(
1502 DependentTemplateSpecializationTypeLoc TL) {
1503 ElaboratedTypeKeyword Keyword
1504 = TypeWithKeyword::getKeywordForTypeSpec(DS.getTypeSpecType());
1505 if (Keyword == ETK_Typename) {
1506 TypeSourceInfo *TInfo = 0;
1507 Sema::GetTypeFromParser(DS.getTypeRep(), &TInfo);
1508 if (TInfo) {
1509 TL.copy(cast<DependentTemplateSpecializationTypeLoc>(
1510 TInfo->getTypeLoc()));
1511 return;
1512 }
1513 }
1514 TL.initializeLocal(SourceLocation());
1515 TL.setKeywordLoc(Keyword != ETK_None
1516 ? DS.getTypeSpecTypeLoc()
1517 : SourceLocation());
1518 const CXXScopeSpec& SS = DS.getTypeSpecScope();
1519 TL.setQualifierRange(SS.isEmpty() ? SourceRange() : SS.getRange());
1520 // FIXME: load appropriate source location.
1521 TL.setNameLoc(DS.getTypeSpecTypeLoc());
1522 }
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00001523
John McCall51bd8032009-10-18 01:05:36 +00001524 void VisitTypeLoc(TypeLoc TL) {
1525 // FIXME: add other typespec types and change this to an assert.
1526 TL.initialize(DS.getTypeSpecTypeLoc());
1527 }
1528 };
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001529
John McCall51bd8032009-10-18 01:05:36 +00001530 class DeclaratorLocFiller : public TypeLocVisitor<DeclaratorLocFiller> {
1531 const DeclaratorChunk &Chunk;
1532
1533 public:
1534 DeclaratorLocFiller(const DeclaratorChunk &Chunk) : Chunk(Chunk) {}
1535
1536 void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001537 llvm_unreachable("qualified type locs not expected here!");
John McCall51bd8032009-10-18 01:05:36 +00001538 }
1539
1540 void VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
1541 assert(Chunk.Kind == DeclaratorChunk::BlockPointer);
1542 TL.setCaretLoc(Chunk.Loc);
1543 }
1544 void VisitPointerTypeLoc(PointerTypeLoc TL) {
1545 assert(Chunk.Kind == DeclaratorChunk::Pointer);
1546 TL.setStarLoc(Chunk.Loc);
1547 }
1548 void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
1549 assert(Chunk.Kind == DeclaratorChunk::Pointer);
1550 TL.setStarLoc(Chunk.Loc);
1551 }
1552 void VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
1553 assert(Chunk.Kind == DeclaratorChunk::MemberPointer);
1554 TL.setStarLoc(Chunk.Loc);
1555 // FIXME: nested name specifier
1556 }
1557 void VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
1558 assert(Chunk.Kind == DeclaratorChunk::Reference);
John McCall54e14c42009-10-22 22:37:11 +00001559 // 'Amp' is misleading: this might have been originally
1560 /// spelled with AmpAmp.
John McCall51bd8032009-10-18 01:05:36 +00001561 TL.setAmpLoc(Chunk.Loc);
1562 }
1563 void VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
1564 assert(Chunk.Kind == DeclaratorChunk::Reference);
1565 assert(!Chunk.Ref.LValueRef);
1566 TL.setAmpAmpLoc(Chunk.Loc);
1567 }
1568 void VisitArrayTypeLoc(ArrayTypeLoc TL) {
1569 assert(Chunk.Kind == DeclaratorChunk::Array);
1570 TL.setLBracketLoc(Chunk.Loc);
1571 TL.setRBracketLoc(Chunk.EndLoc);
1572 TL.setSizeExpr(static_cast<Expr*>(Chunk.Arr.NumElts));
1573 }
1574 void VisitFunctionTypeLoc(FunctionTypeLoc TL) {
1575 assert(Chunk.Kind == DeclaratorChunk::Function);
1576 TL.setLParenLoc(Chunk.Loc);
1577 TL.setRParenLoc(Chunk.EndLoc);
1578
1579 const DeclaratorChunk::FunctionTypeInfo &FTI = Chunk.Fun;
John McCall54e14c42009-10-22 22:37:11 +00001580 for (unsigned i = 0, e = TL.getNumArgs(), tpi = 0; i != e; ++i) {
John McCall51bd8032009-10-18 01:05:36 +00001581 ParmVarDecl *Param = FTI.ArgInfo[i].Param.getAs<ParmVarDecl>();
John McCall54e14c42009-10-22 22:37:11 +00001582 TL.setArg(tpi++, Param);
John McCall51bd8032009-10-18 01:05:36 +00001583 }
1584 // FIXME: exception specs
1585 }
1586
1587 void VisitTypeLoc(TypeLoc TL) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00001588 llvm_unreachable("unsupported TypeLoc kind in declarator!");
John McCall51bd8032009-10-18 01:05:36 +00001589 }
1590 };
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001591}
1592
John McCalla93c9342009-12-07 02:54:59 +00001593/// \brief Create and instantiate a TypeSourceInfo with type source information.
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001594///
1595/// \param T QualType referring to the type as written in source code.
Douglas Gregor05baacb2010-04-12 23:19:01 +00001596///
1597/// \param ReturnTypeInfo For declarators whose return type does not show
1598/// up in the normal place in the declaration specifiers (such as a C++
1599/// conversion function), this pointer will refer to a type source information
1600/// for that return type.
John McCalla93c9342009-12-07 02:54:59 +00001601TypeSourceInfo *
Douglas Gregor05baacb2010-04-12 23:19:01 +00001602Sema::GetTypeSourceInfoForDeclarator(Declarator &D, QualType T,
1603 TypeSourceInfo *ReturnTypeInfo) {
John McCalla93c9342009-12-07 02:54:59 +00001604 TypeSourceInfo *TInfo = Context.CreateTypeSourceInfo(T);
1605 UnqualTypeLoc CurrTL = TInfo->getTypeLoc().getUnqualifiedLoc();
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001606
Sebastian Redl8ce35b02009-10-25 21:45:37 +00001607 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
John McCall51bd8032009-10-18 01:05:36 +00001608 DeclaratorLocFiller(D.getTypeObject(i)).Visit(CurrTL);
1609 CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc();
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001610 }
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +00001611
John McCall51bd8032009-10-18 01:05:36 +00001612 TypeSpecLocFiller(D.getDeclSpec()).Visit(CurrTL);
Douglas Gregor05baacb2010-04-12 23:19:01 +00001613
1614 // We have source information for the return type that was not in the
1615 // declaration specifiers; copy that information into the current type
1616 // location so that it will be retained. This occurs, for example, with
1617 // a C++ conversion function, where the return type occurs within the
1618 // declarator-id rather than in the declaration specifiers.
1619 if (ReturnTypeInfo && D.getDeclSpec().getTypeSpecType() == TST_unspecified) {
1620 TypeLoc TL = ReturnTypeInfo->getTypeLoc();
1621 assert(TL.getFullDataSize() == CurrTL.getFullDataSize());
1622 memcpy(CurrTL.getOpaqueData(), TL.getOpaqueData(), TL.getFullDataSize());
1623 }
1624
John McCalla93c9342009-12-07 02:54:59 +00001625 return TInfo;
Argyrios Kyrtzidis4adab7f2009-08-19 01:28:06 +00001626}
1627
John McCalla93c9342009-12-07 02:54:59 +00001628/// \brief Create a LocInfoType to hold the given QualType and TypeSourceInfo.
1629QualType Sema::CreateLocInfoType(QualType T, TypeSourceInfo *TInfo) {
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001630 // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser
1631 // and Sema during declaration parsing. Try deallocating/caching them when
1632 // it's appropriate, instead of allocating them and keeping them around.
1633 LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType), 8);
John McCalla93c9342009-12-07 02:54:59 +00001634 new (LocT) LocInfoType(T, TInfo);
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001635 assert(LocT->getTypeClass() != T->getTypeClass() &&
1636 "LocInfoType's TypeClass conflicts with an existing Type class");
1637 return QualType(LocT, 0);
1638}
1639
1640void LocInfoType::getAsStringInternal(std::string &Str,
1641 const PrintingPolicy &Policy) const {
Argyrios Kyrtzidis35d44e52009-08-19 01:46:06 +00001642 assert(false && "LocInfoType leaked into the type system; an opaque TypeTy*"
1643 " was used directly instead of getting the QualType through"
1644 " GetTypeFromParser");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001645}
1646
Sebastian Redlcee63fb2008-12-02 14:43:59 +00001647Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001648 // C99 6.7.6: Type names have no identifier. This is already validated by
1649 // the parser.
1650 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00001651
Douglas Gregor402abb52009-05-28 23:31:59 +00001652 TagDecl *OwnedTag = 0;
John McCallbf1a0282010-06-04 23:28:52 +00001653 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S, &OwnedTag);
1654 QualType T = TInfo->getType();
Chris Lattner5153ee62009-04-25 08:47:54 +00001655 if (D.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +00001656 return true;
Steve Naroff5912a352007-08-28 20:14:24 +00001657
Douglas Gregor402abb52009-05-28 23:31:59 +00001658 if (getLangOptions().CPlusPlus) {
1659 // Check that there are no default arguments (C++ only).
Douglas Gregor6d6eb572008-05-07 04:49:29 +00001660 CheckExtraCXXDefaultArguments(D);
1661
Douglas Gregor402abb52009-05-28 23:31:59 +00001662 // C++0x [dcl.type]p3:
1663 // A type-specifier-seq shall not define a class or enumeration
1664 // unless it appears in the type-id of an alias-declaration
1665 // (7.1.3).
1666 if (OwnedTag && OwnedTag->isDefinition())
1667 Diag(OwnedTag->getLocation(), diag::err_type_defined_in_type_specifier)
1668 << Context.getTypeDeclType(OwnedTag);
1669 }
1670
John McCallbf1a0282010-06-04 23:28:52 +00001671 T = CreateLocInfoType(T, TInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +00001672 return T.getAsOpaquePtr();
1673}
1674
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001675
1676
1677//===----------------------------------------------------------------------===//
1678// Type Attribute Processing
1679//===----------------------------------------------------------------------===//
1680
1681/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
1682/// specified type. The attribute contains 1 argument, the id of the address
1683/// space for the type.
Mike Stump1eb44332009-09-09 15:08:12 +00001684static void HandleAddressSpaceTypeAttribute(QualType &Type,
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001685 const AttributeList &Attr, Sema &S){
John McCall0953e762009-09-24 19:53:00 +00001686
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001687 // If this type is already address space qualified, reject it.
1688 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
1689 // for two or more different address spaces."
1690 if (Type.getAddressSpace()) {
1691 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
Abramo Bagnarae215f722010-04-30 13:10:51 +00001692 Attr.setInvalid();
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001693 return;
1694 }
Mike Stump1eb44332009-09-09 15:08:12 +00001695
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001696 // Check the attribute arguments.
1697 if (Attr.getNumArgs() != 1) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +00001698 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Abramo Bagnarae215f722010-04-30 13:10:51 +00001699 Attr.setInvalid();
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001700 return;
1701 }
1702 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
1703 llvm::APSInt addrSpace(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001704 if (ASArgExpr->isTypeDependent() || ASArgExpr->isValueDependent() ||
1705 !ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +00001706 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
1707 << ASArgExpr->getSourceRange();
Abramo Bagnarae215f722010-04-30 13:10:51 +00001708 Attr.setInvalid();
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001709 return;
1710 }
1711
John McCallefadb772009-07-28 06:52:18 +00001712 // Bounds checking.
1713 if (addrSpace.isSigned()) {
1714 if (addrSpace.isNegative()) {
1715 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative)
1716 << ASArgExpr->getSourceRange();
Abramo Bagnarae215f722010-04-30 13:10:51 +00001717 Attr.setInvalid();
John McCallefadb772009-07-28 06:52:18 +00001718 return;
1719 }
1720 addrSpace.setIsSigned(false);
1721 }
1722 llvm::APSInt max(addrSpace.getBitWidth());
John McCall0953e762009-09-24 19:53:00 +00001723 max = Qualifiers::MaxAddressSpace;
John McCallefadb772009-07-28 06:52:18 +00001724 if (addrSpace > max) {
1725 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high)
John McCall0953e762009-09-24 19:53:00 +00001726 << Qualifiers::MaxAddressSpace << ASArgExpr->getSourceRange();
Abramo Bagnarae215f722010-04-30 13:10:51 +00001727 Attr.setInvalid();
John McCallefadb772009-07-28 06:52:18 +00001728 return;
1729 }
1730
Mike Stump1eb44332009-09-09 15:08:12 +00001731 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001732 Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001733}
1734
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001735/// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the
1736/// specified type. The attribute contains 1 argument, weak or strong.
Mike Stump1eb44332009-09-09 15:08:12 +00001737static void HandleObjCGCTypeAttribute(QualType &Type,
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001738 const AttributeList &Attr, Sema &S) {
John McCall0953e762009-09-24 19:53:00 +00001739 if (Type.getObjCGCAttr() != Qualifiers::GCNone) {
Fariborz Jahanian5934e752009-02-18 18:52:41 +00001740 S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc);
Abramo Bagnarae215f722010-04-30 13:10:51 +00001741 Attr.setInvalid();
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001742 return;
1743 }
Mike Stump1eb44332009-09-09 15:08:12 +00001744
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001745 // Check the attribute arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001746 if (!Attr.getParameterName()) {
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001747 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1748 << "objc_gc" << 1;
Abramo Bagnarae215f722010-04-30 13:10:51 +00001749 Attr.setInvalid();
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001750 return;
1751 }
John McCall0953e762009-09-24 19:53:00 +00001752 Qualifiers::GC GCAttr;
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001753 if (Attr.getNumArgs() != 0) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001754 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Abramo Bagnarae215f722010-04-30 13:10:51 +00001755 Attr.setInvalid();
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001756 return;
1757 }
Mike Stump1eb44332009-09-09 15:08:12 +00001758 if (Attr.getParameterName()->isStr("weak"))
John McCall0953e762009-09-24 19:53:00 +00001759 GCAttr = Qualifiers::Weak;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001760 else if (Attr.getParameterName()->isStr("strong"))
John McCall0953e762009-09-24 19:53:00 +00001761 GCAttr = Qualifiers::Strong;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001762 else {
1763 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1764 << "objc_gc" << Attr.getParameterName();
Abramo Bagnarae215f722010-04-30 13:10:51 +00001765 Attr.setInvalid();
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001766 return;
1767 }
Mike Stump1eb44332009-09-09 15:08:12 +00001768
Chris Lattner3b6b83b2009-02-18 22:58:38 +00001769 Type = S.Context.getObjCGCQualType(Type, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001770}
1771
John McCall04a67a62010-02-05 21:31:56 +00001772/// Process an individual function attribute. Returns true if the
1773/// attribute does not make sense to apply to this type.
1774bool ProcessFnAttr(Sema &S, QualType &Type, const AttributeList &Attr) {
1775 if (Attr.getKind() == AttributeList::AT_noreturn) {
1776 // Complain immediately if the arg count is wrong.
1777 if (Attr.getNumArgs() != 0) {
1778 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Abramo Bagnarae215f722010-04-30 13:10:51 +00001779 Attr.setInvalid();
John McCall04a67a62010-02-05 21:31:56 +00001780 return false;
1781 }
Mike Stump24556362009-07-25 21:26:53 +00001782
John McCall04a67a62010-02-05 21:31:56 +00001783 // Delay if this is not a function or pointer to block.
1784 if (!Type->isFunctionPointerType()
1785 && !Type->isBlockPointerType()
1786 && !Type->isFunctionType())
1787 return true;
Mike Stump24556362009-07-25 21:26:53 +00001788
John McCall04a67a62010-02-05 21:31:56 +00001789 // Otherwise we can process right away.
1790 Type = S.Context.getNoReturnType(Type);
1791 return false;
1792 }
Mike Stump24556362009-07-25 21:26:53 +00001793
Rafael Espindola425ef722010-03-30 22:15:11 +00001794 if (Attr.getKind() == AttributeList::AT_regparm) {
1795 // The warning is emitted elsewhere
1796 if (Attr.getNumArgs() != 1) {
1797 return false;
1798 }
1799
1800 // Delay if this is not a function or pointer to block.
1801 if (!Type->isFunctionPointerType()
1802 && !Type->isBlockPointerType()
1803 && !Type->isFunctionType())
1804 return true;
1805
1806 // Otherwise we can process right away.
1807 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1808 llvm::APSInt NumParams(32);
1809
1810 // The warning is emitted elsewhere
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001811 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
1812 !NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context))
Rafael Espindola425ef722010-03-30 22:15:11 +00001813 return false;
1814
1815 Type = S.Context.getRegParmType(Type, NumParams.getZExtValue());
1816 return false;
1817 }
1818
John McCall04a67a62010-02-05 21:31:56 +00001819 // Otherwise, a calling convention.
1820 if (Attr.getNumArgs() != 0) {
1821 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Abramo Bagnarae215f722010-04-30 13:10:51 +00001822 Attr.setInvalid();
John McCall04a67a62010-02-05 21:31:56 +00001823 return false;
1824 }
John McCallf82b4e82010-02-04 05:44:44 +00001825
John McCall04a67a62010-02-05 21:31:56 +00001826 QualType T = Type;
1827 if (const PointerType *PT = Type->getAs<PointerType>())
1828 T = PT->getPointeeType();
1829 const FunctionType *Fn = T->getAs<FunctionType>();
John McCallf82b4e82010-02-04 05:44:44 +00001830
John McCall04a67a62010-02-05 21:31:56 +00001831 // Delay if the type didn't work out to a function.
1832 if (!Fn) return true;
1833
1834 // TODO: diagnose uses of these conventions on the wrong target.
1835 CallingConv CC;
1836 switch (Attr.getKind()) {
1837 case AttributeList::AT_cdecl: CC = CC_C; break;
1838 case AttributeList::AT_fastcall: CC = CC_X86FastCall; break;
1839 case AttributeList::AT_stdcall: CC = CC_X86StdCall; break;
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001840 case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break;
John McCall04a67a62010-02-05 21:31:56 +00001841 default: llvm_unreachable("unexpected attribute kind"); return false;
1842 }
1843
1844 CallingConv CCOld = Fn->getCallConv();
Charles Davis064f7db2010-02-23 06:13:55 +00001845 if (S.Context.getCanonicalCallConv(CC) ==
Abramo Bagnarae215f722010-04-30 13:10:51 +00001846 S.Context.getCanonicalCallConv(CCOld)) {
1847 Attr.setInvalid();
1848 return false;
1849 }
John McCall04a67a62010-02-05 21:31:56 +00001850
1851 if (CCOld != CC_Default) {
1852 // Should we diagnose reapplications of the same convention?
1853 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1854 << FunctionType::getNameForCallConv(CC)
1855 << FunctionType::getNameForCallConv(CCOld);
Abramo Bagnarae215f722010-04-30 13:10:51 +00001856 Attr.setInvalid();
John McCall04a67a62010-02-05 21:31:56 +00001857 return false;
1858 }
1859
1860 // Diagnose the use of X86 fastcall on varargs or unprototyped functions.
1861 if (CC == CC_X86FastCall) {
1862 if (isa<FunctionNoProtoType>(Fn)) {
1863 S.Diag(Attr.getLoc(), diag::err_cconv_knr)
1864 << FunctionType::getNameForCallConv(CC);
Abramo Bagnarae215f722010-04-30 13:10:51 +00001865 Attr.setInvalid();
John McCall04a67a62010-02-05 21:31:56 +00001866 return false;
1867 }
1868
1869 const FunctionProtoType *FnP = cast<FunctionProtoType>(Fn);
1870 if (FnP->isVariadic()) {
1871 S.Diag(Attr.getLoc(), diag::err_cconv_varargs)
1872 << FunctionType::getNameForCallConv(CC);
Abramo Bagnarae215f722010-04-30 13:10:51 +00001873 Attr.setInvalid();
John McCall04a67a62010-02-05 21:31:56 +00001874 return false;
1875 }
1876 }
1877
1878 Type = S.Context.getCallConvType(Type, CC);
1879 return false;
John McCallf82b4e82010-02-04 05:44:44 +00001880}
1881
John Thompson6e132aa2009-12-04 21:51:28 +00001882/// HandleVectorSizeAttribute - this attribute is only applicable to integral
1883/// and float scalars, although arrays, pointers, and function return values are
1884/// allowed in conjunction with this construct. Aggregates with this attribute
1885/// are invalid, even if they are of the same size as a corresponding scalar.
1886/// The raw attribute should contain precisely 1 argument, the vector size for
1887/// the variable, measured in bytes. If curType and rawAttr are well formed,
1888/// this routine will return a new vector type.
Chris Lattner788b0fd2010-06-23 06:00:24 +00001889static void HandleVectorSizeAttr(QualType& CurType, const AttributeList &Attr,
1890 Sema &S) {
John Thompson6e132aa2009-12-04 21:51:28 +00001891 // Check the attribute arugments.
1892 if (Attr.getNumArgs() != 1) {
1893 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Abramo Bagnarae215f722010-04-30 13:10:51 +00001894 Attr.setInvalid();
John Thompson6e132aa2009-12-04 21:51:28 +00001895 return;
1896 }
1897 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
1898 llvm::APSInt vecSize(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001899 if (sizeExpr->isTypeDependent() || sizeExpr->isValueDependent() ||
1900 !sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
John Thompson6e132aa2009-12-04 21:51:28 +00001901 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1902 << "vector_size" << sizeExpr->getSourceRange();
Abramo Bagnarae215f722010-04-30 13:10:51 +00001903 Attr.setInvalid();
John Thompson6e132aa2009-12-04 21:51:28 +00001904 return;
1905 }
1906 // the base type must be integer or float, and can't already be a vector.
1907 if (CurType->isVectorType() ||
1908 (!CurType->isIntegerType() && !CurType->isRealFloatingType())) {
1909 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
Abramo Bagnarae215f722010-04-30 13:10:51 +00001910 Attr.setInvalid();
John Thompson6e132aa2009-12-04 21:51:28 +00001911 return;
1912 }
1913 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
1914 // vecSize is specified in bytes - convert to bits.
1915 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
1916
1917 // the vector size needs to be an integral multiple of the type size.
1918 if (vectorSize % typeSize) {
1919 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
1920 << sizeExpr->getSourceRange();
Abramo Bagnarae215f722010-04-30 13:10:51 +00001921 Attr.setInvalid();
John Thompson6e132aa2009-12-04 21:51:28 +00001922 return;
1923 }
1924 if (vectorSize == 0) {
1925 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
1926 << sizeExpr->getSourceRange();
Abramo Bagnarae215f722010-04-30 13:10:51 +00001927 Attr.setInvalid();
John Thompson6e132aa2009-12-04 21:51:28 +00001928 return;
1929 }
1930
1931 // Success! Instantiate the vector type, the number of elements is > 0, and
1932 // not required to be a power of 2, unlike GCC.
Chris Lattner788b0fd2010-06-23 06:00:24 +00001933 CurType = S.Context.getVectorType(CurType, vectorSize/typeSize,
1934 VectorType::NotAltiVec);
John Thompson6e132aa2009-12-04 21:51:28 +00001935}
1936
John McCall04a67a62010-02-05 21:31:56 +00001937void ProcessTypeAttributeList(Sema &S, QualType &Result,
Charles Davis328ce342010-02-24 02:27:18 +00001938 bool IsDeclSpec, const AttributeList *AL,
John McCall04a67a62010-02-05 21:31:56 +00001939 DelayedAttributeSet &FnAttrs) {
Chris Lattner232e8822008-02-21 01:08:11 +00001940 // Scan through and apply attributes to this type where it makes sense. Some
1941 // attributes (such as __address_space__, __vector_size__, etc) apply to the
1942 // type, but others can be present in the type specifiers even though they
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00001943 // apply to the decl. Here we apply type attributes and ignore the rest.
1944 for (; AL; AL = AL->getNext()) {
Abramo Bagnarae215f722010-04-30 13:10:51 +00001945 // Skip attributes that were marked to be invalid.
1946 if (AL->isInvalid())
1947 continue;
1948
Abramo Bagnarab1f1b262010-04-30 09:13:03 +00001949 // If this is an attribute we can handle, do so now,
1950 // otherwise, add it to the FnAttrs list for rechaining.
Chris Lattnerfca0ddd2008-06-26 06:27:57 +00001951 switch (AL->getKind()) {
Chris Lattner232e8822008-02-21 01:08:11 +00001952 default: break;
John McCall04a67a62010-02-05 21:31:56 +00001953
Chris Lattner232e8822008-02-21 01:08:11 +00001954 case AttributeList::AT_address_space:
John McCall04a67a62010-02-05 21:31:56 +00001955 HandleAddressSpaceTypeAttribute(Result, *AL, S);
Chris Lattnerc9b346d2008-06-29 00:50:08 +00001956 break;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001957 case AttributeList::AT_objc_gc:
John McCall04a67a62010-02-05 21:31:56 +00001958 HandleObjCGCTypeAttribute(Result, *AL, S);
Mike Stump24556362009-07-25 21:26:53 +00001959 break;
John Thompson6e132aa2009-12-04 21:51:28 +00001960 case AttributeList::AT_vector_size:
John McCall04a67a62010-02-05 21:31:56 +00001961 HandleVectorSizeAttr(Result, *AL, S);
1962 break;
1963
1964 case AttributeList::AT_noreturn:
1965 case AttributeList::AT_cdecl:
1966 case AttributeList::AT_fastcall:
1967 case AttributeList::AT_stdcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001968 case AttributeList::AT_thiscall:
Rafael Espindola425ef722010-03-30 22:15:11 +00001969 case AttributeList::AT_regparm:
Charles Davis328ce342010-02-24 02:27:18 +00001970 // Don't process these on the DeclSpec.
1971 if (IsDeclSpec ||
1972 ProcessFnAttr(S, Result, *AL))
John McCall04a67a62010-02-05 21:31:56 +00001973 FnAttrs.push_back(DelayedAttribute(AL, Result));
John Thompson6e132aa2009-12-04 21:51:28 +00001974 break;
Chris Lattner232e8822008-02-21 01:08:11 +00001975 }
Chris Lattner232e8822008-02-21 01:08:11 +00001976 }
Chris Lattner232e8822008-02-21 01:08:11 +00001977}
1978
Mike Stump1eb44332009-09-09 15:08:12 +00001979/// @brief Ensure that the type T is a complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001980///
1981/// This routine checks whether the type @p T is complete in any
1982/// context where a complete type is required. If @p T is a complete
Douglas Gregor86447ec2009-03-09 16:13:40 +00001983/// type, returns false. If @p T is a class template specialization,
1984/// this routine then attempts to perform class template
1985/// instantiation. If instantiation fails, or if @p T is incomplete
1986/// and cannot be completed, issues the diagnostic @p diag (giving it
1987/// the type @p T) and returns true.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001988///
1989/// @param Loc The location in the source that the incomplete type
1990/// diagnostic should refer to.
1991///
1992/// @param T The type that this routine is examining for completeness.
1993///
Mike Stump1eb44332009-09-09 15:08:12 +00001994/// @param PD The partial diagnostic that will be printed out if T is not a
Anders Carlssonb7906612009-08-26 23:45:07 +00001995/// complete type.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00001996///
1997/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
1998/// @c false otherwise.
Anders Carlsson91a0cc92009-08-26 22:33:56 +00001999bool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
Anders Carlsson8c8d9192009-10-09 23:51:55 +00002000 const PartialDiagnostic &PD,
2001 std::pair<SourceLocation,
2002 PartialDiagnostic> Note) {
Anders Carlsson91a0cc92009-08-26 22:33:56 +00002003 unsigned diag = PD.getDiagID();
Mike Stump1eb44332009-09-09 15:08:12 +00002004
Douglas Gregor573d9c32009-10-21 23:19:44 +00002005 // FIXME: Add this assertion to make sure we always get instantiation points.
2006 // assert(!Loc.isInvalid() && "Invalid location in RequireCompleteType");
Douglas Gregor690dc7f2009-05-21 23:48:18 +00002007 // FIXME: Add this assertion to help us flush out problems with
2008 // checking for dependent types and type-dependent expressions.
2009 //
Mike Stump1eb44332009-09-09 15:08:12 +00002010 // assert(!T->isDependentType() &&
Douglas Gregor690dc7f2009-05-21 23:48:18 +00002011 // "Can't ask whether a dependent type is complete");
2012
Douglas Gregor4ec339f2009-01-19 19:26:10 +00002013 // If we have a complete type, we're done.
2014 if (!T->isIncompleteType())
2015 return false;
Eli Friedman3c0eb162008-05-27 03:33:27 +00002016
Douglas Gregord475b8d2009-03-25 21:17:03 +00002017 // If we have a class template specialization or a class member of a
Sebastian Redl923d56d2009-11-05 15:52:31 +00002018 // class template specialization, or an array with known size of such,
2019 // try to instantiate it.
2020 QualType MaybeTemplate = T;
Douglas Gregor89c49f02009-11-09 22:08:55 +00002021 if (const ConstantArrayType *Array = Context.getAsConstantArrayType(T))
Sebastian Redl923d56d2009-11-05 15:52:31 +00002022 MaybeTemplate = Array->getElementType();
2023 if (const RecordType *Record = MaybeTemplate->getAs<RecordType>()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +00002024 if (ClassTemplateSpecializationDecl *ClassTemplateSpec
Douglas Gregord475b8d2009-03-25 21:17:03 +00002025 = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) {
Douglas Gregor972e6ce2009-10-27 06:26:26 +00002026 if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared)
2027 return InstantiateClassTemplateSpecialization(Loc, ClassTemplateSpec,
Douglas Gregord0e3daf2009-09-04 22:48:11 +00002028 TSK_ImplicitInstantiation,
Douglas Gregor5842ba92009-08-24 15:23:48 +00002029 /*Complain=*/diag != 0);
Mike Stump1eb44332009-09-09 15:08:12 +00002030 } else if (CXXRecordDecl *Rec
Douglas Gregord475b8d2009-03-25 21:17:03 +00002031 = dyn_cast<CXXRecordDecl>(Record->getDecl())) {
2032 if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00002033 MemberSpecializationInfo *MSInfo = Rec->getMemberSpecializationInfo();
2034 assert(MSInfo && "Missing member specialization information?");
Douglas Gregor357bbd02009-08-28 20:50:45 +00002035 // This record was instantiated from a class within a template.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00002036 if (MSInfo->getTemplateSpecializationKind()
Douglas Gregor972e6ce2009-10-27 06:26:26 +00002037 != TSK_ExplicitSpecialization)
Douglas Gregorf6b11852009-10-08 15:14:33 +00002038 return InstantiateClass(Loc, Rec, Pattern,
2039 getTemplateInstantiationArgs(Rec),
2040 TSK_ImplicitInstantiation,
2041 /*Complain=*/diag != 0);
Douglas Gregord475b8d2009-03-25 21:17:03 +00002042 }
2043 }
2044 }
Douglas Gregor2943aed2009-03-03 04:44:36 +00002045
Douglas Gregor5842ba92009-08-24 15:23:48 +00002046 if (diag == 0)
2047 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002048
Rafael Espindola01620702010-03-21 22:56:43 +00002049 const TagType *Tag = 0;
2050 if (const RecordType *Record = T->getAs<RecordType>())
2051 Tag = Record;
2052 else if (const EnumType *Enum = T->getAs<EnumType>())
2053 Tag = Enum;
2054
2055 // Avoid diagnosing invalid decls as incomplete.
2056 if (Tag && Tag->getDecl()->isInvalidDecl())
2057 return true;
2058
Douglas Gregor4ec339f2009-01-19 19:26:10 +00002059 // We have an incomplete type. Produce a diagnostic.
Anders Carlsson91a0cc92009-08-26 22:33:56 +00002060 Diag(Loc, PD) << T;
Douglas Gregor4ec339f2009-01-19 19:26:10 +00002061
Anders Carlsson8c8d9192009-10-09 23:51:55 +00002062 // If we have a note, produce it.
2063 if (!Note.first.isInvalid())
2064 Diag(Note.first, Note.second);
2065
Douglas Gregor4ec339f2009-01-19 19:26:10 +00002066 // If the type was a forward declaration of a class/struct/union
Rafael Espindola01620702010-03-21 22:56:43 +00002067 // type, produce a note.
Douglas Gregor4ec339f2009-01-19 19:26:10 +00002068 if (Tag && !Tag->getDecl()->isInvalidDecl())
Mike Stump1eb44332009-09-09 15:08:12 +00002069 Diag(Tag->getDecl()->getLocation(),
Douglas Gregor4ec339f2009-01-19 19:26:10 +00002070 Tag->isBeingDefined() ? diag::note_type_being_defined
2071 : diag::note_forward_declaration)
2072 << QualType(Tag, 0);
2073
2074 return true;
2075}
Douglas Gregore6258932009-03-19 00:39:20 +00002076
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00002077bool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
2078 const PartialDiagnostic &PD) {
2079 return RequireCompleteType(Loc, T, PD,
2080 std::make_pair(SourceLocation(), PDiag(0)));
2081}
2082
2083bool Sema::RequireCompleteType(SourceLocation Loc, QualType T,
2084 unsigned DiagID) {
2085 return RequireCompleteType(Loc, T, PDiag(DiagID),
2086 std::make_pair(SourceLocation(), PDiag(0)));
2087}
2088
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002089/// \brief Retrieve a version of the type 'T' that is elaborated by Keyword
2090/// and qualified by the nested-name-specifier contained in SS.
2091QualType Sema::getElaboratedType(ElaboratedTypeKeyword Keyword,
2092 const CXXScopeSpec &SS, QualType T) {
2093 if (T.isNull())
Douglas Gregore6258932009-03-19 00:39:20 +00002094 return T;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002095 NestedNameSpecifier *NNS;
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002096 if (SS.isValid())
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002097 NNS = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
2098 else {
2099 if (Keyword == ETK_None)
2100 return T;
2101 NNS = 0;
2102 }
2103 return Context.getElaboratedType(Keyword, NNS, T);
Douglas Gregore6258932009-03-19 00:39:20 +00002104}
Anders Carlssonaf017e62009-06-29 22:58:55 +00002105
2106QualType Sema::BuildTypeofExprType(Expr *E) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00002107 if (E->getType() == Context.OverloadTy) {
2108 // C++ [temp.arg.explicit]p3 allows us to resolve a template-id to a
2109 // function template specialization wherever deduction cannot occur.
2110 if (FunctionDecl *Specialization
2111 = ResolveSingleFunctionTemplateSpecialization(E)) {
John McCall161755a2010-04-06 21:38:20 +00002112 // The access doesn't really matter in this case.
2113 DeclAccessPair Found = DeclAccessPair::make(Specialization,
2114 Specialization->getAccess());
2115 E = FixOverloadedFunctionReference(E, Found, Specialization);
Douglas Gregor4b52e252009-12-21 23:17:24 +00002116 if (!E)
2117 return QualType();
2118 } else {
2119 Diag(E->getLocStart(),
2120 diag::err_cannot_determine_declared_type_of_overloaded_function)
2121 << false << E->getSourceRange();
2122 return QualType();
2123 }
2124 }
2125
Anders Carlssonaf017e62009-06-29 22:58:55 +00002126 return Context.getTypeOfExprType(E);
2127}
2128
2129QualType Sema::BuildDecltypeType(Expr *E) {
2130 if (E->getType() == Context.OverloadTy) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00002131 // C++ [temp.arg.explicit]p3 allows us to resolve a template-id to a
2132 // function template specialization wherever deduction cannot occur.
2133 if (FunctionDecl *Specialization
2134 = ResolveSingleFunctionTemplateSpecialization(E)) {
John McCall161755a2010-04-06 21:38:20 +00002135 // The access doesn't really matter in this case.
2136 DeclAccessPair Found = DeclAccessPair::make(Specialization,
2137 Specialization->getAccess());
2138 E = FixOverloadedFunctionReference(E, Found, Specialization);
Douglas Gregor4b52e252009-12-21 23:17:24 +00002139 if (!E)
2140 return QualType();
2141 } else {
2142 Diag(E->getLocStart(),
2143 diag::err_cannot_determine_declared_type_of_overloaded_function)
2144 << true << E->getSourceRange();
2145 return QualType();
2146 }
Anders Carlssonaf017e62009-06-29 22:58:55 +00002147 }
Douglas Gregor4b52e252009-12-21 23:17:24 +00002148
Anders Carlssonaf017e62009-06-29 22:58:55 +00002149 return Context.getDecltypeType(E);
2150}