blob: f0e6c81fdc121f8c9ec1213bc5a932ef2a49f25c [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- SemaType.cpp - Semantic Analysis for Types -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements type-related semantic analysis.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
15#include "clang/AST/ASTContext.h"
Steve Naroff3fafa102007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000017#include "clang/AST/Expr.h"
Daniel Dunbarcc7b1602008-08-11 03:45:03 +000018#include "clang/Parse/DeclSpec.h"
Chris Lattner4b009652007-07-25 00:24:17 +000019using namespace clang;
20
Douglas Gregord45210d2009-01-30 22:09:00 +000021/// \brief Convert the specified declspec to the appropriate type
22/// object.
23/// \param DS the declaration specifiers
24/// \returns The type described by the declaration specifiers, or NULL
25/// if there was an error.
Chris Lattner99dbc962008-06-26 06:27:57 +000026QualType Sema::ConvertDeclSpecToType(const DeclSpec &DS) {
Chris Lattner4b009652007-07-25 00:24:17 +000027 // FIXME: Should move the logic from DeclSpec::Finish to here for validity
28 // checking.
Chris Lattner06fb8672008-02-20 21:40:32 +000029 QualType Result;
Chris Lattner4b009652007-07-25 00:24:17 +000030
31 switch (DS.getTypeSpecType()) {
Chris Lattner5fcb38b2008-04-02 06:50:17 +000032 case DeclSpec::TST_void:
33 Result = Context.VoidTy;
34 break;
Chris Lattner4b009652007-07-25 00:24:17 +000035 case DeclSpec::TST_char:
36 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
Chris Lattner726c5452008-02-20 23:53:49 +000037 Result = Context.CharTy;
Chris Lattner4b009652007-07-25 00:24:17 +000038 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
Chris Lattner726c5452008-02-20 23:53:49 +000039 Result = Context.SignedCharTy;
Chris Lattner4b009652007-07-25 00:24:17 +000040 else {
41 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
42 "Unknown TSS value");
Chris Lattner726c5452008-02-20 23:53:49 +000043 Result = Context.UnsignedCharTy;
Chris Lattner4b009652007-07-25 00:24:17 +000044 }
Chris Lattner06fb8672008-02-20 21:40:32 +000045 break;
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +000046 case DeclSpec::TST_wchar:
47 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
48 Result = Context.WCharTy;
49 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
Chris Lattner10f2c2e2008-11-20 06:38:18 +000050 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
51 << DS.getSpecifierName(DS.getTypeSpecType());
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +000052 Result = Context.getSignedWCharType();
53 } else {
54 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
55 "Unknown TSS value");
Chris Lattner10f2c2e2008-11-20 06:38:18 +000056 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
57 << DS.getSpecifierName(DS.getTypeSpecType());
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +000058 Result = Context.getUnsignedWCharType();
59 }
60 break;
Chris Lattner6ab935b2008-04-05 06:32:51 +000061 case DeclSpec::TST_unspecified:
Chris Lattner4a68fe02008-07-26 00:46:50 +000062 // "<proto1,proto2>" is an objc qualified ID with a missing id.
Chris Lattnercce42242008-10-20 02:01:50 +000063 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
Chris Lattnerada63792008-07-26 01:53:50 +000064 Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ,
Chris Lattner4a68fe02008-07-26 00:46:50 +000065 DS.getNumProtocolQualifiers());
66 break;
67 }
68
Chris Lattner6ab935b2008-04-05 06:32:51 +000069 // Unspecified typespec defaults to int in C90. However, the C90 grammar
70 // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
71 // type-qualifier, or storage-class-specifier. If not, emit an extwarn.
72 // Note that the one exception to this is function definitions, which are
73 // allowed to be completely missing a declspec. This is handled in the
74 // parser already though by it pretending to have seen an 'int' in this
75 // case.
76 if (getLangOptions().ImplicitInt) {
77 if ((DS.getParsedSpecifiers() & (DeclSpec::PQ_StorageClassSpecifier |
78 DeclSpec::PQ_TypeSpecifier |
79 DeclSpec::PQ_TypeQualifier)) == 0)
80 Diag(DS.getSourceRange().getBegin(), diag::ext_missing_declspec);
Douglas Gregorae17c7a2009-02-16 22:38:20 +000081 } else if (!DS.hasTypeSpecifier()) {
Chris Lattner6ab935b2008-04-05 06:32:51 +000082 // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says:
83 // "At least one type specifier shall be given in the declaration
84 // specifiers in each declaration, and in the specifier-qualifier list in
85 // each struct declaration and type name."
Douglas Gregorae17c7a2009-02-16 22:38:20 +000086 // FIXME: Does Microsoft really have the implicit int extension in C++?
87 unsigned DK = getLangOptions().CPlusPlus && !getLangOptions().Microsoft?
88 diag::err_missing_type_specifier
89 : diag::ext_missing_type_specifier;
90 Diag(DS.getSourceRange().getBegin(), DK);
Chris Lattner6ab935b2008-04-05 06:32:51 +000091 }
92
93 // FALL THROUGH.
Chris Lattner5328f312007-08-21 17:02:28 +000094 case DeclSpec::TST_int: {
Chris Lattner4b009652007-07-25 00:24:17 +000095 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
96 switch (DS.getTypeSpecWidth()) {
Chris Lattner726c5452008-02-20 23:53:49 +000097 case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
98 case DeclSpec::TSW_short: Result = Context.ShortTy; break;
99 case DeclSpec::TSW_long: Result = Context.LongTy; break;
100 case DeclSpec::TSW_longlong: Result = Context.LongLongTy; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000101 }
102 } else {
103 switch (DS.getTypeSpecWidth()) {
Chris Lattner726c5452008-02-20 23:53:49 +0000104 case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
105 case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break;
106 case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break;
107 case DeclSpec::TSW_longlong: Result =Context.UnsignedLongLongTy; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000108 }
109 }
Chris Lattner06fb8672008-02-20 21:40:32 +0000110 break;
Chris Lattner5328f312007-08-21 17:02:28 +0000111 }
Chris Lattner726c5452008-02-20 23:53:49 +0000112 case DeclSpec::TST_float: Result = Context.FloatTy; break;
Chris Lattner06fb8672008-02-20 21:40:32 +0000113 case DeclSpec::TST_double:
114 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
Chris Lattner726c5452008-02-20 23:53:49 +0000115 Result = Context.LongDoubleTy;
Chris Lattner06fb8672008-02-20 21:40:32 +0000116 else
Chris Lattner726c5452008-02-20 23:53:49 +0000117 Result = Context.DoubleTy;
Chris Lattner06fb8672008-02-20 21:40:32 +0000118 break;
Chris Lattner726c5452008-02-20 23:53:49 +0000119 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
Chris Lattner4b009652007-07-25 00:24:17 +0000120 case DeclSpec::TST_decimal32: // _Decimal32
121 case DeclSpec::TST_decimal64: // _Decimal64
122 case DeclSpec::TST_decimal128: // _Decimal128
123 assert(0 && "FIXME: GNU decimal extensions not supported yet!");
Chris Lattner2e78db32008-04-13 18:59:07 +0000124 case DeclSpec::TST_class:
Chris Lattner4b009652007-07-25 00:24:17 +0000125 case DeclSpec::TST_enum:
126 case DeclSpec::TST_union:
127 case DeclSpec::TST_struct: {
128 Decl *D = static_cast<Decl *>(DS.getTypeRep());
Chris Lattner2e78db32008-04-13 18:59:07 +0000129 assert(D && "Didn't get a decl for a class/enum/union/struct?");
Chris Lattner4b009652007-07-25 00:24:17 +0000130 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
131 DS.getTypeSpecSign() == 0 &&
132 "Can't handle qualifiers on typedef names yet!");
133 // TypeQuals handled by caller.
Douglas Gregor1d661552008-04-13 21:07:44 +0000134 Result = Context.getTypeDeclType(cast<TypeDecl>(D));
Chris Lattner06fb8672008-02-20 21:40:32 +0000135 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000136 }
Douglas Gregora60c62e2009-02-09 15:09:02 +0000137 case DeclSpec::TST_typename: {
Chris Lattner4b009652007-07-25 00:24:17 +0000138 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
139 DS.getTypeSpecSign() == 0 &&
140 "Can't handle qualifiers on typedef names yet!");
Douglas Gregora60c62e2009-02-09 15:09:02 +0000141 Result = QualType::getFromOpaquePtr(DS.getTypeRep());
Douglas Gregor1d661552008-04-13 21:07:44 +0000142
Douglas Gregora60c62e2009-02-09 15:09:02 +0000143 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
144 // FIXME: Adding a TST_objcInterface clause doesn't seem ideal, so
145 // we have this "hack" for now...
146 if (const ObjCInterfaceType *Interface = Result->getAsObjCInterfaceType())
147 Result = Context.getObjCQualifiedInterfaceType(Interface->getDecl(),
148 (ObjCProtocolDecl**)PQ,
149 DS.getNumProtocolQualifiers());
150 else if (Result == Context.getObjCIdType())
Chris Lattnerada63792008-07-26 01:53:50 +0000151 // id<protocol-list>
152 Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ,
153 DS.getNumProtocolQualifiers());
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000154 }
Chris Lattner4b009652007-07-25 00:24:17 +0000155 // TypeQuals handled by caller.
Chris Lattner06fb8672008-02-20 21:40:32 +0000156 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000157 }
Chris Lattner06fb8672008-02-20 21:40:32 +0000158 case DeclSpec::TST_typeofType:
159 Result = QualType::getFromOpaquePtr(DS.getTypeRep());
160 assert(!Result.isNull() && "Didn't get a type for typeof?");
Steve Naroff7cbb1462007-07-31 12:34:36 +0000161 // TypeQuals handled by caller.
Chris Lattner726c5452008-02-20 23:53:49 +0000162 Result = Context.getTypeOfType(Result);
Chris Lattner06fb8672008-02-20 21:40:32 +0000163 break;
Steve Naroff7cbb1462007-07-31 12:34:36 +0000164 case DeclSpec::TST_typeofExpr: {
165 Expr *E = static_cast<Expr *>(DS.getTypeRep());
166 assert(E && "Didn't get an expression for typeof?");
167 // TypeQuals handled by caller.
Chris Lattner726c5452008-02-20 23:53:49 +0000168 Result = Context.getTypeOfExpr(E);
Chris Lattner06fb8672008-02-20 21:40:32 +0000169 break;
Steve Naroff7cbb1462007-07-31 12:34:36 +0000170 }
Douglas Gregor6c0f4062009-02-18 17:45:20 +0000171 case DeclSpec::TST_error:
172 return QualType();
Chris Lattner4b009652007-07-25 00:24:17 +0000173 }
Chris Lattner06fb8672008-02-20 21:40:32 +0000174
175 // Handle complex types.
Douglas Gregor91ca7f02009-02-14 21:06:05 +0000176 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
177 if (getLangOptions().Freestanding)
178 Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
Chris Lattner726c5452008-02-20 23:53:49 +0000179 Result = Context.getComplexType(Result);
Douglas Gregor91ca7f02009-02-14 21:06:05 +0000180 }
Chris Lattner06fb8672008-02-20 21:40:32 +0000181
182 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
183 "FIXME: imaginary types not supported yet!");
184
Chris Lattnerd496fb92008-02-20 22:04:11 +0000185 // See if there are any attributes on the declspec that apply to the type (as
186 // opposed to the decl).
Chris Lattner99dbc962008-06-26 06:27:57 +0000187 if (const AttributeList *AL = DS.getAttributes())
Chris Lattner65a57042008-06-29 00:50:08 +0000188 ProcessTypeAttributeList(Result, AL);
Chris Lattner9e982502008-02-21 01:07:18 +0000189
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000190 // Apply const/volatile/restrict qualifiers to T.
191 if (unsigned TypeQuals = DS.getTypeQualifiers()) {
192
193 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
194 // or incomplete types shall not be restrict-qualified." C++ also allows
195 // restrict-qualified references.
196 if (TypeQuals & QualType::Restrict) {
Chris Lattnercfac88d2008-04-02 17:35:06 +0000197 if (const PointerLikeType *PT = Result->getAsPointerLikeType()) {
198 QualType EltTy = PT->getPointeeType();
199
200 // If we have a pointer or reference, the pointee must have an object or
201 // incomplete type.
202 if (!EltTy->isIncompleteOrObjectType()) {
203 Diag(DS.getRestrictSpecLoc(),
Chris Lattner77d52da2008-11-20 06:06:08 +0000204 diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000205 << EltTy << DS.getSourceRange();
Chris Lattnercfac88d2008-04-02 17:35:06 +0000206 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
207 }
208 } else {
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000209 Diag(DS.getRestrictSpecLoc(),
Chris Lattner77d52da2008-11-20 06:06:08 +0000210 diag::err_typecheck_invalid_restrict_not_pointer)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000211 << Result << DS.getSourceRange();
Chris Lattnercfac88d2008-04-02 17:35:06 +0000212 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000213 }
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000214 }
215
216 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
217 // of a function type includes any type qualifiers, the behavior is
218 // undefined."
219 if (Result->isFunctionType() && TypeQuals) {
220 // Get some location to point at, either the C or V location.
221 SourceLocation Loc;
222 if (TypeQuals & QualType::Const)
223 Loc = DS.getConstSpecLoc();
224 else {
225 assert((TypeQuals & QualType::Volatile) &&
226 "Has CV quals but not C or V?");
227 Loc = DS.getVolatileSpecLoc();
228 }
Chris Lattner77d52da2008-11-20 06:06:08 +0000229 Diag(Loc, diag::warn_typecheck_function_qualifiers)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000230 << Result << DS.getSourceRange();
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000231 }
232
Douglas Gregorb7b28a22008-11-03 15:51:28 +0000233 // C++ [dcl.ref]p1:
234 // Cv-qualified references are ill-formed except when the
235 // cv-qualifiers are introduced through the use of a typedef
236 // (7.1.3) or of a template type argument (14.3), in which
237 // case the cv-qualifiers are ignored.
Douglas Gregora60c62e2009-02-09 15:09:02 +0000238 // FIXME: Shouldn't we be checking SCS_typedef here?
239 if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
Douglas Gregorb7b28a22008-11-03 15:51:28 +0000240 TypeQuals && Result->isReferenceType()) {
241 TypeQuals &= ~QualType::Const;
242 TypeQuals &= ~QualType::Volatile;
243 }
244
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000245 Result = Result.getQualifiedType(TypeQuals);
246 }
Chris Lattner9e982502008-02-21 01:07:18 +0000247 return Result;
248}
249
Mike Stumpc1fddff2009-02-04 22:31:32 +0000250/// GetTypeForDeclarator - Convert the type for the specified
251/// declarator to Type instances. Skip the outermost Skip type
252/// objects.
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000253QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S, unsigned Skip) {
Mike Stumpc1fddff2009-02-04 22:31:32 +0000254 bool OmittedReturnType = false;
255
256 if (D.getContext() == Declarator::BlockLiteralContext
257 && Skip == 0
258 && !D.getDeclSpec().hasTypeSpecifier()
259 && (D.getNumTypeObjects() == 0
260 || (D.getNumTypeObjects() == 1
261 && D.getTypeObject(0).Kind == DeclaratorChunk::Function)))
262 OmittedReturnType = true;
263
Chris Lattner11f20f92007-08-28 16:40:32 +0000264 // long long is a C99 feature.
Chris Lattner1a7d9912007-08-28 16:41:29 +0000265 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Chris Lattner11f20f92007-08-28 16:40:32 +0000266 D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
267 Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
Douglas Gregord45210d2009-01-30 22:09:00 +0000268
269 // Determine the type of the declarator. Not all forms of declarator
270 // have a type.
271 QualType T;
272 switch (D.getKind()) {
273 case Declarator::DK_Abstract:
274 case Declarator::DK_Normal:
Mike Stumpc1fddff2009-02-04 22:31:32 +0000275 case Declarator::DK_Operator: {
276 const DeclSpec& DS = D.getDeclSpec();
277 if (OmittedReturnType)
278 // We default to a dependent type initially. Can be modified by
279 // the first return statement.
280 T = Context.DependentTy;
Douglas Gregor6c0f4062009-02-18 17:45:20 +0000281 else {
Mike Stumpc1fddff2009-02-04 22:31:32 +0000282 T = ConvertDeclSpecToType(DS);
Douglas Gregor6c0f4062009-02-18 17:45:20 +0000283 if (T.isNull())
284 return T;
285 }
Douglas Gregord45210d2009-01-30 22:09:00 +0000286 break;
Mike Stumpc1fddff2009-02-04 22:31:32 +0000287 }
Douglas Gregord45210d2009-01-30 22:09:00 +0000288
289 case Declarator::DK_Constructor:
290 case Declarator::DK_Destructor:
291 case Declarator::DK_Conversion:
292 // Constructors and destructors don't have return types. Use
293 // "void" instead. Conversion operators will check their return
294 // types separately.
295 T = Context.VoidTy;
296 break;
297 }
Sebastian Redl19fec9d2008-11-21 19:14:01 +0000298
Mike Stumpc1fddff2009-02-04 22:31:32 +0000299 // Walk the DeclTypeInfo, building the recursive type as we go.
300 // DeclTypeInfos are ordered from the identifier out, which is
301 // opposite of what we want :).
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000302 for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) {
303 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1+Skip);
Chris Lattner4b009652007-07-25 00:24:17 +0000304 switch (DeclType.Kind) {
305 default: assert(0 && "Unknown decltype!");
Steve Naroff7aa54752008-08-27 16:04:49 +0000306 case DeclaratorChunk::BlockPointer:
307 if (DeclType.Cls.TypeQuals)
308 Diag(D.getIdentifierLoc(), diag::err_qualified_block_pointer_type);
309 if (!T.getTypePtr()->isFunctionType())
310 Diag(D.getIdentifierLoc(), diag::err_nonfunction_block_type);
311 else
312 T = Context.getBlockPointerType(T);
313 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000314 case DeclaratorChunk::Pointer:
Chris Lattner36be3d82007-07-31 21:33:24 +0000315 if (T->isReferenceType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000316 // C++ 8.3.2p4: There shall be no ... pointers to references ...
Chris Lattner77d52da2008-11-20 06:06:08 +0000317 Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference)
318 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroff91b03f72007-08-28 03:03:08 +0000319 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000320 T = Context.IntTy;
321 }
322
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000323 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
324 // object or incomplete types shall not be restrict-qualified."
325 if ((DeclType.Ptr.TypeQuals & QualType::Restrict) &&
Chris Lattner9db553e2008-04-02 06:59:01 +0000326 !T->isIncompleteOrObjectType()) {
Chris Lattner77d52da2008-11-20 06:06:08 +0000327 Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000328 << T;
Sebastian Redl75555032009-01-24 21:16:55 +0000329 DeclType.Ptr.TypeQuals &= ~QualType::Restrict;
330 }
331
Chris Lattner4b009652007-07-25 00:24:17 +0000332 // Apply the pointer typequals to the pointer object.
333 T = Context.getPointerType(T).getQualifiedType(DeclType.Ptr.TypeQuals);
334 break;
Douglas Gregorb7b28a22008-11-03 15:51:28 +0000335 case DeclaratorChunk::Reference: {
336 // Whether we should suppress the creation of the reference.
337 bool SuppressReference = false;
338 if (T->isReferenceType()) {
339 // C++ [dcl.ref]p4: There shall be no references to references.
340 //
341 // According to C++ DR 106, references to references are only
342 // diagnosed when they are written directly (e.g., "int & &"),
343 // but not when they happen via a typedef:
344 //
345 // typedef int& intref;
346 // typedef intref& intref2;
347 //
348 // Parser::ParserDeclaratorInternal diagnoses the case where
349 // references are written directly; here, we handle the
350 // collapsing of references-to-references as described in C++
351 // DR 106 and amended by C++ DR 540.
352 SuppressReference = true;
353 }
354
355 // C++ [dcl.ref]p1:
356 // A declarator that specifies the type “reference to cv void”
357 // is ill-formed.
358 if (T->isVoidType()) {
359 Diag(DeclType.Loc, diag::err_reference_to_void);
Steve Naroff91b03f72007-08-28 03:03:08 +0000360 D.setInvalidType(true);
Douglas Gregorb7b28a22008-11-03 15:51:28 +0000361 T = Context.IntTy;
Chris Lattner4b009652007-07-25 00:24:17 +0000362 }
363
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000364 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
365 // object or incomplete types shall not be restrict-qualified."
366 if (DeclType.Ref.HasRestrict &&
Chris Lattner9db553e2008-04-02 06:59:01 +0000367 !T->isIncompleteOrObjectType()) {
Chris Lattner77d52da2008-11-20 06:06:08 +0000368 Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000369 << T;
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000370 DeclType.Ref.HasRestrict = false;
371 }
372
Douglas Gregorb7b28a22008-11-03 15:51:28 +0000373 if (!SuppressReference)
374 T = Context.getReferenceType(T);
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000375
376 // Handle restrict on references.
377 if (DeclType.Ref.HasRestrict)
378 T.addRestrict();
Chris Lattner4b009652007-07-25 00:24:17 +0000379 break;
Douglas Gregorb7b28a22008-11-03 15:51:28 +0000380 }
Chris Lattner4b009652007-07-25 00:24:17 +0000381 case DeclaratorChunk::Array: {
Chris Lattner67d3c8d2008-04-02 01:05:10 +0000382 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner47958f62007-08-28 16:54:00 +0000383 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000384 ArrayType::ArraySizeModifier ASM;
385 if (ATI.isStar)
386 ASM = ArrayType::Star;
387 else if (ATI.hasStatic)
388 ASM = ArrayType::Static;
389 else
390 ASM = ArrayType::Normal;
391
392 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
393 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
Douglas Gregor46fe06e2009-01-19 19:26:10 +0000394 if (DiagnoseIncompleteType(D.getIdentifierLoc(), T,
395 diag::err_illegal_decl_array_incomplete_type)) {
Steve Naroff91b03f72007-08-28 03:03:08 +0000396 T = Context.IntTy;
397 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000398 } else if (T->isFunctionType()) {
Chris Lattner77d52da2008-11-20 06:06:08 +0000399 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_functions)
400 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroff91b03f72007-08-28 03:03:08 +0000401 T = Context.getPointerType(T);
402 D.setInvalidType(true);
Chris Lattnerf0c4a0a2007-07-31 16:56:34 +0000403 } else if (const ReferenceType *RT = T->getAsReferenceType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000404 // C++ 8.3.2p4: There shall be no ... arrays of references ...
Chris Lattner77d52da2008-11-20 06:06:08 +0000405 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_references)
406 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Chris Lattnercfac88d2008-04-02 17:35:06 +0000407 T = RT->getPointeeType();
Steve Naroff91b03f72007-08-28 03:03:08 +0000408 D.setInvalidType(true);
Chris Lattner36be3d82007-07-31 21:33:24 +0000409 } else if (const RecordType *EltTy = T->getAsRecordType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000410 // If the element type is a struct or union that contains a variadic
Douglas Gregorec793e82009-02-10 21:49:46 +0000411 // array, accept it as a GNU extension: C99 6.7.2.1p2.
412 if (EltTy->getDecl()->hasFlexibleArrayMember())
413 Diag(DeclType.Loc, diag::ext_flexible_array_in_array) << T;
Chris Lattner31fccaf2008-08-18 22:49:54 +0000414 } else if (T->isObjCInterfaceType()) {
Chris Lattner4bfd2232008-11-24 06:25:27 +0000415 Diag(DeclType.Loc, diag::warn_objc_array_of_interfaces) << T;
Chris Lattner4b009652007-07-25 00:24:17 +0000416 }
Chris Lattner31fccaf2008-08-18 22:49:54 +0000417
Steve Naroff63b6a642007-08-30 22:35:45 +0000418 // C99 6.7.5.2p1: The size expression shall have integer type.
419 if (ArraySize && !ArraySize->getType()->isIntegerType()) {
Chris Lattner77d52da2008-11-20 06:06:08 +0000420 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
Chris Lattner4bfd2232008-11-24 06:25:27 +0000421 << ArraySize->getType() << ArraySize->getSourceRange();
Steve Naroff63b6a642007-08-30 22:35:45 +0000422 D.setInvalidType(true);
Ted Kremenek06f217c2009-02-07 01:51:40 +0000423 ArraySize->Destroy(Context);
Chris Lattner67d3c8d2008-04-02 01:05:10 +0000424 ATI.NumElts = ArraySize = 0;
Steve Naroff63b6a642007-08-30 22:35:45 +0000425 }
Steve Naroff24c9b982007-08-30 18:10:14 +0000426 llvm::APSInt ConstVal(32);
Eli Friedman8ff07782008-02-15 18:16:39 +0000427 if (!ArraySize) {
428 T = Context.getIncompleteArrayType(T, ASM, ATI.TypeQuals);
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000429 } else if (ArraySize->isValueDependent()) {
430 T = Context.getDependentSizedArrayType(T, ArraySize, ASM, ATI.TypeQuals);
Eli Friedman2ff28d12008-05-14 00:40:18 +0000431 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000432 !T->isConstantSizeType()) {
Eli Friedman2ff28d12008-05-14 00:40:18 +0000433 // Per C99, a variable array is an array with either a non-constant
434 // size or an element type that has a non-constant-size
Steve Naroff24c9b982007-08-30 18:10:14 +0000435 T = Context.getVariableArrayType(T, ArraySize, ASM, ATI.TypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000436 } else {
Steve Naroff63b6a642007-08-30 22:35:45 +0000437 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
438 // have a value greater than zero.
439 if (ConstVal.isSigned()) {
440 if (ConstVal.isNegative()) {
Chris Lattner9d2cf082008-11-19 05:27:50 +0000441 Diag(ArraySize->getLocStart(),
442 diag::err_typecheck_negative_array_size)
443 << ArraySize->getSourceRange();
Steve Naroff63b6a642007-08-30 22:35:45 +0000444 D.setInvalidType(true);
445 } else if (ConstVal == 0) {
446 // GCC accepts zero sized static arrays.
Chris Lattner9d2cf082008-11-19 05:27:50 +0000447 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size)
448 << ArraySize->getSourceRange();
Steve Naroff63b6a642007-08-30 22:35:45 +0000449 }
450 }
Steve Naroff24c9b982007-08-30 18:10:14 +0000451 T = Context.getConstantArrayType(T, ConstVal, ASM, ATI.TypeQuals);
Steve Naroff63b6a642007-08-30 22:35:45 +0000452 }
Chris Lattner47958f62007-08-28 16:54:00 +0000453 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
Chris Lattner306d4df2008-12-18 06:50:14 +0000454 if (!getLangOptions().C99) {
455 if (ArraySize && !ArraySize->isValueDependent() &&
456 !ArraySize->isIntegerConstantExpr(Context))
457 Diag(D.getIdentifierLoc(), diag::ext_vla);
458 else if (ASM != ArrayType::Normal || ATI.TypeQuals != 0)
459 Diag(D.getIdentifierLoc(), diag::ext_c99_array_usage);
460 }
Chris Lattner4b009652007-07-25 00:24:17 +0000461 break;
462 }
Sebastian Redl75555032009-01-24 21:16:55 +0000463 case DeclaratorChunk::Function: {
Chris Lattner4b009652007-07-25 00:24:17 +0000464 // If the function declarator has a prototype (i.e. it is not () and
465 // does not have a K&R-style identifier list), then the arguments are part
466 // of the type, otherwise the argument list is ().
467 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Chris Lattnerbccfc152007-12-19 18:01:43 +0000468
Chris Lattner6ad7e882007-12-19 05:31:29 +0000469 // C99 6.7.5.3p1: The return type may not be a function or array type.
Chris Lattnerbccfc152007-12-19 18:01:43 +0000470 if (T->isArrayType() || T->isFunctionType()) {
Chris Lattner4bfd2232008-11-24 06:25:27 +0000471 Diag(DeclType.Loc, diag::err_func_returning_array_function) << T;
Chris Lattner6ad7e882007-12-19 05:31:29 +0000472 T = Context.IntTy;
473 D.setInvalidType(true);
474 }
475
Eli Friedman769e7302008-08-25 21:31:01 +0000476 if (FTI.NumArgs == 0) {
Argiris Kirtzidis1c51d472008-10-16 17:31:08 +0000477 if (getLangOptions().CPlusPlus) {
478 // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the
479 // function takes no arguments.
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +0000480 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic,FTI.TypeQuals);
Douglas Gregor88a25f82009-02-18 07:07:28 +0000481 } else if (FTI.isVariadic) {
482 // We allow a zero-parameter variadic function in C if the
483 // function is marked with the "overloadable"
484 // attribute. Scan for this attribute now.
485 bool Overloadable = false;
486 for (const AttributeList *Attrs = D.getAttributes();
487 Attrs; Attrs = Attrs->getNext()) {
488 if (Attrs->getKind() == AttributeList::AT_overloadable) {
489 Overloadable = true;
490 break;
491 }
492 }
493
494 if (!Overloadable)
495 Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
496 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, 0);
Argiris Kirtzidis1c51d472008-10-16 17:31:08 +0000497 } else {
498 // Simple void foo(), where the incoming T is the result type.
499 T = Context.getFunctionTypeNoProto(T);
500 }
Eli Friedman769e7302008-08-25 21:31:01 +0000501 } else if (FTI.ArgInfo[0].Param == 0) {
Chris Lattner4b009652007-07-25 00:24:17 +0000502 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
Eli Friedman769e7302008-08-25 21:31:01 +0000503 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
Chris Lattner4b009652007-07-25 00:24:17 +0000504 } else {
505 // Otherwise, we have a function with an argument list that is
506 // potentially variadic.
507 llvm::SmallVector<QualType, 16> ArgTys;
508
509 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Chris Lattner97316c02008-04-10 02:22:51 +0000510 ParmVarDecl *Param = (ParmVarDecl *)FTI.ArgInfo[i].Param;
511 QualType ArgTy = Param->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000512 assert(!ArgTy.isNull() && "Couldn't parse type?");
Steve Naroff1830be72007-09-10 22:17:00 +0000513 //
514 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
515 // This matches the conversion that is done in
Nate Begeman2240f542007-11-13 21:49:48 +0000516 // Sema::ActOnParamDeclarator(). Without this conversion, the
Steve Naroff1830be72007-09-10 22:17:00 +0000517 // argument type in the function prototype *will not* match the
518 // type in ParmVarDecl (which makes the code generator unhappy).
519 //
520 // FIXME: We still apparently need the conversion in
Chris Lattner19eb97e2008-04-02 05:18:44 +0000521 // Sema::ActOnParamDeclarator(). This doesn't make any sense, since
Steve Naroff1830be72007-09-10 22:17:00 +0000522 // it should be driving off the type being created here.
523 //
524 // FIXME: If a source translation tool needs to see the original type,
525 // then we need to consider storing both types somewhere...
526 //
Chris Lattner19eb97e2008-04-02 05:18:44 +0000527 if (ArgTy->isArrayType()) {
528 ArgTy = Context.getArrayDecayedType(ArgTy);
Chris Lattnerc08564a2008-01-02 22:50:48 +0000529 } else if (ArgTy->isFunctionType())
Steve Naroff1830be72007-09-10 22:17:00 +0000530 ArgTy = Context.getPointerType(ArgTy);
Chris Lattner19eb97e2008-04-02 05:18:44 +0000531
Chris Lattner4b009652007-07-25 00:24:17 +0000532 // Look for 'void'. void is allowed only as a single argument to a
533 // function with no other parameters (C99 6.7.5.3p10). We record
534 // int(void) as a FunctionTypeProto with an empty argument list.
Steve Naroff1830be72007-09-10 22:17:00 +0000535 else if (ArgTy->isVoidType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000536 // If this is something like 'float(int, void)', reject it. 'void'
537 // is an incomplete type (C99 6.2.5p19) and function decls cannot
538 // have arguments of incomplete type.
539 if (FTI.NumArgs != 1 || FTI.isVariadic) {
540 Diag(DeclType.Loc, diag::err_void_only_param);
541 ArgTy = Context.IntTy;
Chris Lattner97316c02008-04-10 02:22:51 +0000542 Param->setType(ArgTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000543 } else if (FTI.ArgInfo[i].Ident) {
544 // Reject, but continue to parse 'int(void abc)'.
545 Diag(FTI.ArgInfo[i].IdentLoc,
546 diag::err_param_with_void_type);
547 ArgTy = Context.IntTy;
Chris Lattner97316c02008-04-10 02:22:51 +0000548 Param->setType(ArgTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000549 } else {
550 // Reject, but continue to parse 'float(const void)'.
Chris Lattner35fef522008-02-20 20:55:12 +0000551 if (ArgTy.getCVRQualifiers())
Chris Lattner4b009652007-07-25 00:24:17 +0000552 Diag(DeclType.Loc, diag::err_void_param_qualified);
553
554 // Do not add 'void' to the ArgTys list.
555 break;
556 }
Eli Friedman769e7302008-08-25 21:31:01 +0000557 } else if (!FTI.hasPrototype) {
558 if (ArgTy->isPromotableIntegerType()) {
559 ArgTy = Context.IntTy;
560 } else if (const BuiltinType* BTy = ArgTy->getAsBuiltinType()) {
561 if (BTy->getKind() == BuiltinType::Float)
562 ArgTy = Context.DoubleTy;
563 }
Chris Lattner4b009652007-07-25 00:24:17 +0000564 }
565
566 ArgTys.push_back(ArgTy);
567 }
568 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +0000569 FTI.isVariadic, FTI.TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000570 }
571 break;
572 }
Sebastian Redl75555032009-01-24 21:16:55 +0000573 case DeclaratorChunk::MemberPointer:
574 // The scope spec must refer to a class, or be dependent.
575 DeclContext *DC = static_cast<DeclContext*>(
576 DeclType.Mem.Scope().getScopeRep());
577 QualType ClsType;
578 // FIXME: Extend for dependent types when it's actually supported.
579 // See ActOnCXXNestedNameSpecifier.
580 if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(DC)) {
581 ClsType = Context.getTagDeclType(RD);
582 } else {
583 if (DC) {
584 Diag(DeclType.Mem.Scope().getBeginLoc(),
585 diag::err_illegal_decl_mempointer_in_nonclass)
586 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
587 << DeclType.Mem.Scope().getRange();
588 }
589 D.setInvalidType(true);
590 ClsType = Context.IntTy;
591 }
592
593 // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member
594 // with reference type, or "cv void."
595 if (T->isReferenceType()) {
596 Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference)
597 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
598 D.setInvalidType(true);
599 T = Context.IntTy;
600 }
601 if (T->isVoidType()) {
602 Diag(DeclType.Loc, diag::err_illegal_decl_mempointer_to_void)
603 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
604 T = Context.IntTy;
605 }
606
607 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
608 // object or incomplete types shall not be restrict-qualified."
609 if ((DeclType.Mem.TypeQuals & QualType::Restrict) &&
610 !T->isIncompleteOrObjectType()) {
611 Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
612 << T;
613 DeclType.Mem.TypeQuals &= ~QualType::Restrict;
614 }
615
Sebastian Redlba387562009-01-25 19:43:20 +0000616 T = Context.getMemberPointerType(T, ClsType.getTypePtr()).
617 getQualifiedType(DeclType.Mem.TypeQuals);
Sebastian Redl75555032009-01-24 21:16:55 +0000618
619 break;
620 }
621
Chris Lattner65a57042008-06-29 00:50:08 +0000622 // See if there are any attributes on this declarator chunk.
623 if (const AttributeList *AL = DeclType.getAttrs())
624 ProcessTypeAttributeList(T, AL);
Chris Lattner4b009652007-07-25 00:24:17 +0000625 }
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +0000626
627 if (getLangOptions().CPlusPlus && T->isFunctionType()) {
628 const FunctionTypeProto *FnTy = T->getAsFunctionTypeProto();
629 assert(FnTy && "Why oh why is there not a FunctionTypeProto here ?");
630
631 // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type
632 // for a nonstatic member function, the function type to which a pointer
633 // to member refers, or the top-level function type of a function typedef
634 // declaration.
635 if (FnTy->getTypeQuals() != 0 &&
636 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
Douglas Gregorc5cbc242008-12-15 23:53:10 +0000637 ((D.getContext() != Declarator::MemberContext &&
638 (!D.getCXXScopeSpec().isSet() ||
639 !static_cast<DeclContext*>(D.getCXXScopeSpec().getScopeRep())
Douglas Gregor723d3332009-01-07 00:43:41 +0000640 ->isRecord())) ||
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +0000641 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +0000642 if (D.isFunctionDeclarator())
643 Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
644 else
645 Diag(D.getIdentifierLoc(),
646 diag::err_invalid_qualified_typedef_function_type_use);
647
648 // Strip the cv-quals from the type.
649 T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +0000650 FnTy->getNumArgs(), FnTy->isVariadic(), 0);
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +0000651 }
652 }
Chris Lattner4b009652007-07-25 00:24:17 +0000653
Chris Lattnerf9e90cc2008-06-29 00:19:33 +0000654 // If there were any type attributes applied to the decl itself (not the
655 // type, apply the type attribute to the type!)
656 if (const AttributeList *Attrs = D.getAttributes())
Chris Lattner65a57042008-06-29 00:50:08 +0000657 ProcessTypeAttributeList(T, Attrs);
Chris Lattnerf9e90cc2008-06-29 00:19:33 +0000658
Chris Lattner4b009652007-07-25 00:24:17 +0000659 return T;
660}
661
Ted Kremenek42730c52008-01-07 19:49:32 +0000662/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
Fariborz Jahanianff746bc2007-11-09 22:27:59 +0000663/// declarator
Ted Kremenek42730c52008-01-07 19:49:32 +0000664QualType Sema::ObjCGetTypeForMethodDefinition(DeclTy *D) {
665 ObjCMethodDecl *MDecl = dyn_cast<ObjCMethodDecl>(static_cast<Decl *>(D));
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000666 QualType T = MDecl->getResultType();
667 llvm::SmallVector<QualType, 16> ArgTys;
668
Fariborz Jahanianea86cb82007-11-09 17:18:29 +0000669 // Add the first two invisible argument types for self and _cmd.
Douglas Gregor5d764842009-01-09 17:18:27 +0000670 if (MDecl->isInstanceMethod()) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000671 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000672 selfTy = Context.getPointerType(selfTy);
673 ArgTys.push_back(selfTy);
674 }
Fariborz Jahanianea86cb82007-11-09 17:18:29 +0000675 else
Ted Kremenek42730c52008-01-07 19:49:32 +0000676 ArgTys.push_back(Context.getObjCIdType());
677 ArgTys.push_back(Context.getObjCSelType());
Fariborz Jahanianea86cb82007-11-09 17:18:29 +0000678
Chris Lattner685d7922008-03-16 01:07:14 +0000679 for (int i = 0, e = MDecl->getNumParams(); i != e; ++i) {
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000680 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
681 QualType ArgTy = PDecl->getType();
682 assert(!ArgTy.isNull() && "Couldn't parse type?");
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000683 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
684 // This matches the conversion that is done in
Chris Lattner19eb97e2008-04-02 05:18:44 +0000685 // Sema::ActOnParamDeclarator().
686 if (ArgTy->isArrayType())
687 ArgTy = Context.getArrayDecayedType(ArgTy);
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000688 else if (ArgTy->isFunctionType())
689 ArgTy = Context.getPointerType(ArgTy);
690 ArgTys.push_back(ArgTy);
691 }
692 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +0000693 MDecl->isVariadic(), 0);
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000694 return T;
695}
696
Sebastian Redl8ebd8fb2009-01-26 19:54:48 +0000697/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that
698/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
699/// they point to and return true. If T1 and T2 aren't pointer types
700/// or pointer-to-member types, or if they are not similar at this
701/// level, returns false and leaves T1 and T2 unchanged. Top-level
702/// qualifiers on T1 and T2 are ignored. This function will typically
703/// be called in a loop that successively "unwraps" pointer and
704/// pointer-to-member types to compare them at each level.
Chris Lattner6d0d8712009-02-16 21:43:00 +0000705bool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) {
Douglas Gregorccc0ccc2008-10-22 14:17:15 +0000706 const PointerType *T1PtrType = T1->getAsPointerType(),
707 *T2PtrType = T2->getAsPointerType();
708 if (T1PtrType && T2PtrType) {
709 T1 = T1PtrType->getPointeeType();
710 T2 = T2PtrType->getPointeeType();
711 return true;
712 }
713
Sebastian Redlba387562009-01-25 19:43:20 +0000714 const MemberPointerType *T1MPType = T1->getAsMemberPointerType(),
715 *T2MPType = T2->getAsMemberPointerType();
Sebastian Redlf41a58c2009-01-28 18:33:18 +0000716 if (T1MPType && T2MPType &&
717 Context.getCanonicalType(T1MPType->getClass()) ==
718 Context.getCanonicalType(T2MPType->getClass())) {
Sebastian Redlba387562009-01-25 19:43:20 +0000719 T1 = T1MPType->getPointeeType();
720 T2 = T2MPType->getPointeeType();
721 return true;
722 }
Douglas Gregorccc0ccc2008-10-22 14:17:15 +0000723 return false;
724}
725
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000726Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Chris Lattner4b009652007-07-25 00:24:17 +0000727 // C99 6.7.6: Type names have no identifier. This is already validated by
728 // the parser.
729 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
730
Sebastian Redl66df3ef2008-12-02 14:43:59 +0000731 QualType T = GetTypeForDeclarator(D, S);
Douglas Gregor6c0f4062009-02-18 17:45:20 +0000732 if (T.isNull())
733 return true;
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000734
Douglas Gregor2b9422f2008-05-07 04:49:29 +0000735 // Check that there are no default arguments (C++ only).
736 if (getLangOptions().CPlusPlus)
737 CheckExtraCXXDefaultArguments(D);
738
Chris Lattner4b009652007-07-25 00:24:17 +0000739 return T.getAsOpaquePtr();
740}
741
Chris Lattner65a57042008-06-29 00:50:08 +0000742
743
744//===----------------------------------------------------------------------===//
745// Type Attribute Processing
746//===----------------------------------------------------------------------===//
747
748/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
749/// specified type. The attribute contains 1 argument, the id of the address
750/// space for the type.
751static void HandleAddressSpaceTypeAttribute(QualType &Type,
752 const AttributeList &Attr, Sema &S){
753 // If this type is already address space qualified, reject it.
754 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
755 // for two or more different address spaces."
756 if (Type.getAddressSpace()) {
757 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
758 return;
759 }
760
761 // Check the attribute arguments.
762 if (Attr.getNumArgs() != 1) {
Chris Lattner10f2c2e2008-11-20 06:38:18 +0000763 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner65a57042008-06-29 00:50:08 +0000764 return;
765 }
766 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
767 llvm::APSInt addrSpace(32);
768 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
Chris Lattner9d2cf082008-11-19 05:27:50 +0000769 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
770 << ASArgExpr->getSourceRange();
Chris Lattner65a57042008-06-29 00:50:08 +0000771 return;
772 }
773
774 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000775 Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
Chris Lattner65a57042008-06-29 00:50:08 +0000776}
777
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000778/// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the
779/// specified type. The attribute contains 1 argument, weak or strong.
780static void HandleObjCGCTypeAttribute(QualType &Type,
Chris Lattnerd9b85c32009-02-18 22:58:38 +0000781 const AttributeList &Attr, Sema &S) {
Fariborz Jahanian80ff83c2009-02-18 17:52:36 +0000782 // FIXME. change error code.
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000783 if (Type.getObjCGCAttr() != QualType::GCNone) {
Fariborz Jahanian31804e12009-02-18 18:52:41 +0000784 S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000785 return;
786 }
787
788 // Check the attribute arguments.
Fariborz Jahanian80ff83c2009-02-18 17:52:36 +0000789 if (!Attr.getParameterName()) {
790 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
791 << "objc_gc" << 1;
792 return;
793 }
Chris Lattnerd9b85c32009-02-18 22:58:38 +0000794 QualType::GCAttrTypes GCAttr;
Fariborz Jahanian80ff83c2009-02-18 17:52:36 +0000795 if (Attr.getNumArgs() != 0) {
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000796 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
797 return;
798 }
799 if (Attr.getParameterName()->isStr("weak"))
Chris Lattnerd9b85c32009-02-18 22:58:38 +0000800 GCAttr = QualType::Weak;
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000801 else if (Attr.getParameterName()->isStr("strong"))
Chris Lattnerd9b85c32009-02-18 22:58:38 +0000802 GCAttr = QualType::Strong;
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000803 else {
804 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
805 << "objc_gc" << Attr.getParameterName();
806 return;
807 }
808
Chris Lattnerd9b85c32009-02-18 22:58:38 +0000809 Type = S.Context.getObjCGCQualType(Type, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000810}
811
Chris Lattner65a57042008-06-29 00:50:08 +0000812void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) {
Chris Lattner1aaeeb92008-02-21 01:08:11 +0000813 // Scan through and apply attributes to this type where it makes sense. Some
814 // attributes (such as __address_space__, __vector_size__, etc) apply to the
815 // type, but others can be present in the type specifiers even though they
Chris Lattner99dbc962008-06-26 06:27:57 +0000816 // apply to the decl. Here we apply type attributes and ignore the rest.
817 for (; AL; AL = AL->getNext()) {
Chris Lattner1aaeeb92008-02-21 01:08:11 +0000818 // If this is an attribute we can handle, do so now, otherwise, add it to
819 // the LeftOverAttrs list for rechaining.
Chris Lattner99dbc962008-06-26 06:27:57 +0000820 switch (AL->getKind()) {
Chris Lattner1aaeeb92008-02-21 01:08:11 +0000821 default: break;
822 case AttributeList::AT_address_space:
Chris Lattner65a57042008-06-29 00:50:08 +0000823 HandleAddressSpaceTypeAttribute(Result, *AL, *this);
824 break;
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000825 case AttributeList::AT_objc_gc:
826 HandleObjCGCTypeAttribute(Result, *AL, *this);
827 break;
Chris Lattner1aaeeb92008-02-21 01:08:11 +0000828 }
Chris Lattner1aaeeb92008-02-21 01:08:11 +0000829 }
Chris Lattner1aaeeb92008-02-21 01:08:11 +0000830}
831
Douglas Gregor46fe06e2009-01-19 19:26:10 +0000832/// @brief If the type T is incomplete and cannot be completed,
833/// produce a suitable diagnostic.
834///
835/// This routine checks whether the type @p T is complete in any
836/// context where a complete type is required. If @p T is a complete
837/// type, returns false. If @p T is incomplete, issues the diagnostic
838/// @p diag (giving it the type @p T) and returns true.
839///
840/// @param Loc The location in the source that the incomplete type
841/// diagnostic should refer to.
842///
843/// @param T The type that this routine is examining for completeness.
844///
845/// @param diag The diagnostic value (e.g.,
846/// @c diag::err_typecheck_decl_incomplete_type) that will be used
847/// for the error message if @p T is incomplete.
848///
849/// @param Range1 An optional range in the source code that will be a
850/// part of the "incomplete type" error message.
851///
852/// @param Range2 An optional range in the source code that will be a
853/// part of the "incomplete type" error message.
854///
855/// @param PrintType If non-NULL, the type that should be printed
856/// instead of @p T. This parameter should be used when the type that
857/// we're checking for incompleteness isn't the type that should be
858/// displayed to the user, e.g., when T is a type and PrintType is a
859/// pointer to T.
860///
861/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
862/// @c false otherwise.
863///
864/// @todo When Clang gets proper support for C++ templates, this
865/// routine will also be able perform template instantiation when @p T
866/// is a class template specialization.
867bool Sema::DiagnoseIncompleteType(SourceLocation Loc, QualType T, unsigned diag,
868 SourceRange Range1, SourceRange Range2,
869 QualType PrintType) {
870 // If we have a complete type, we're done.
871 if (!T->isIncompleteType())
872 return false;
Eli Friedman86ad5222008-05-27 03:33:27 +0000873
Douglas Gregor46fe06e2009-01-19 19:26:10 +0000874 if (PrintType.isNull())
875 PrintType = T;
876
877 // We have an incomplete type. Produce a diagnostic.
878 Diag(Loc, diag) << PrintType << Range1 << Range2;
879
880 // If the type was a forward declaration of a class/struct/union
881 // type, produce
882 const TagType *Tag = 0;
883 if (const RecordType *Record = T->getAsRecordType())
884 Tag = Record;
885 else if (const EnumType *Enum = T->getAsEnumType())
886 Tag = Enum;
887
888 if (Tag && !Tag->getDecl()->isInvalidDecl())
889 Diag(Tag->getDecl()->getLocation(),
890 Tag->isBeingDefined() ? diag::note_type_being_defined
891 : diag::note_forward_declaration)
892 << QualType(Tag, 0);
893
894 return true;
895}