blob: a1c5dbc4d10c8867a7b97e86f02dda8fd624d649 [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"
Steve Naroff980e5082007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000017#include "clang/AST/Expr.h"
Daniel Dunbare4858a62008-08-11 03:45:03 +000018#include "clang/Parse/DeclSpec.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019using namespace clang;
20
Douglas Gregor930d8b52009-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 Lattnerfca0ddd2008-06-26 06:27:57 +000026QualType Sema::ConvertDeclSpecToType(const DeclSpec &DS) {
Reid Spencer5f016e22007-07-11 17:01:13 +000027 // FIXME: Should move the logic from DeclSpec::Finish to here for validity
28 // checking.
Chris Lattner958858e2008-02-20 21:40:32 +000029 QualType Result;
Reid Spencer5f016e22007-07-11 17:01:13 +000030
31 switch (DS.getTypeSpecType()) {
Chris Lattner96b77fc2008-04-02 06:50:17 +000032 case DeclSpec::TST_void:
33 Result = Context.VoidTy;
34 break;
Reid Spencer5f016e22007-07-11 17:01:13 +000035 case DeclSpec::TST_char:
36 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
Chris Lattnerfab5b452008-02-20 23:53:49 +000037 Result = Context.CharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000038 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
Chris Lattnerfab5b452008-02-20 23:53:49 +000039 Result = Context.SignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000040 else {
41 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
42 "Unknown TSS value");
Chris Lattnerfab5b452008-02-20 23:53:49 +000043 Result = Context.UnsignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000044 }
Chris Lattner958858e2008-02-20 21:40:32 +000045 break;
Argyrios Kyrtzidis64c438a2008-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 Lattnerf3a41af2008-11-20 06:38:18 +000050 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
51 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +000052 Result = Context.getSignedWCharType();
53 } else {
54 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
55 "Unknown TSS value");
Chris Lattnerf3a41af2008-11-20 06:38:18 +000056 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec)
57 << DS.getSpecifierName(DS.getTypeSpecType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +000058 Result = Context.getUnsignedWCharType();
59 }
60 break;
Chris Lattnerd658b562008-04-05 06:32:51 +000061 case DeclSpec::TST_unspecified:
Chris Lattner62f5f7f2008-07-26 00:46:50 +000062 // "<proto1,proto2>" is an objc qualified ID with a missing id.
Chris Lattner097e9162008-10-20 02:01:50 +000063 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
Chris Lattnerae4da612008-07-26 01:53:50 +000064 Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ,
Chris Lattner62f5f7f2008-07-26 00:46:50 +000065 DS.getNumProtocolQualifiers());
66 break;
67 }
68
Chris Lattnerd658b562008-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) {
Chris Lattner35d276f2009-02-27 18:53:28 +000077 // In C89 mode, we only warn if there is a completely missing declspec
78 // when one is not allowed.
79 if (DS.isEmpty())
Chris Lattner173144a2009-02-27 22:31:56 +000080 Diag(DS.getSourceRange().getBegin(), diag::warn_missing_declspec)
81 << CodeModificationHint::CreateInsertion(DS.getSourceRange().getBegin(),
82 "int");
Douglas Gregor4310f4e2009-02-16 22:38:20 +000083 } else if (!DS.hasTypeSpecifier()) {
Chris Lattnerd658b562008-04-05 06:32:51 +000084 // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says:
85 // "At least one type specifier shall be given in the declaration
86 // specifiers in each declaration, and in the specifier-qualifier list in
87 // each struct declaration and type name."
Douglas Gregor4310f4e2009-02-16 22:38:20 +000088 // FIXME: Does Microsoft really have the implicit int extension in C++?
89 unsigned DK = getLangOptions().CPlusPlus && !getLangOptions().Microsoft?
90 diag::err_missing_type_specifier
Chris Lattner35d276f2009-02-27 18:53:28 +000091 : diag::warn_missing_type_specifier;
Chris Lattner173144a2009-02-27 22:31:56 +000092 Diag(DS.getSourceRange().getBegin(), DK)
93 << CodeModificationHint::CreateInsertion(DS.getSourceRange().getBegin(),
94 "int");
Chris Lattnerd658b562008-04-05 06:32:51 +000095 }
96
97 // FALL THROUGH.
Chris Lattner3cbc38b2007-08-21 17:02:28 +000098 case DeclSpec::TST_int: {
Reid Spencer5f016e22007-07-11 17:01:13 +000099 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
100 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000101 case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
102 case DeclSpec::TSW_short: Result = Context.ShortTy; break;
103 case DeclSpec::TSW_long: Result = Context.LongTy; break;
104 case DeclSpec::TSW_longlong: Result = Context.LongLongTy; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000105 }
106 } else {
107 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000108 case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
109 case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break;
110 case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break;
111 case DeclSpec::TSW_longlong: Result =Context.UnsignedLongLongTy; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000112 }
113 }
Chris Lattner958858e2008-02-20 21:40:32 +0000114 break;
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000115 }
Chris Lattnerfab5b452008-02-20 23:53:49 +0000116 case DeclSpec::TST_float: Result = Context.FloatTy; break;
Chris Lattner958858e2008-02-20 21:40:32 +0000117 case DeclSpec::TST_double:
118 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000119 Result = Context.LongDoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000120 else
Chris Lattnerfab5b452008-02-20 23:53:49 +0000121 Result = Context.DoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000122 break;
Chris Lattnerfab5b452008-02-20 23:53:49 +0000123 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 case DeclSpec::TST_decimal32: // _Decimal32
125 case DeclSpec::TST_decimal64: // _Decimal64
126 case DeclSpec::TST_decimal128: // _Decimal128
127 assert(0 && "FIXME: GNU decimal extensions not supported yet!");
Chris Lattner99dc9142008-04-13 18:59:07 +0000128 case DeclSpec::TST_class:
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 case DeclSpec::TST_enum:
130 case DeclSpec::TST_union:
131 case DeclSpec::TST_struct: {
132 Decl *D = static_cast<Decl *>(DS.getTypeRep());
Chris Lattner99dc9142008-04-13 18:59:07 +0000133 assert(D && "Didn't get a decl for a class/enum/union/struct?");
Reid Spencer5f016e22007-07-11 17:01:13 +0000134 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
135 DS.getTypeSpecSign() == 0 &&
136 "Can't handle qualifiers on typedef names yet!");
137 // TypeQuals handled by caller.
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000138 Result = Context.getTypeDeclType(cast<TypeDecl>(D));
Chris Lattner958858e2008-02-20 21:40:32 +0000139 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000140 }
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000141 case DeclSpec::TST_typename: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000142 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
143 DS.getTypeSpecSign() == 0 &&
144 "Can't handle qualifiers on typedef names yet!");
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000145 Result = QualType::getFromOpaquePtr(DS.getTypeRep());
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000146
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000147 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
148 // FIXME: Adding a TST_objcInterface clause doesn't seem ideal, so
149 // we have this "hack" for now...
150 if (const ObjCInterfaceType *Interface = Result->getAsObjCInterfaceType())
151 Result = Context.getObjCQualifiedInterfaceType(Interface->getDecl(),
152 (ObjCProtocolDecl**)PQ,
153 DS.getNumProtocolQualifiers());
154 else if (Result == Context.getObjCIdType())
Chris Lattnerae4da612008-07-26 01:53:50 +0000155 // id<protocol-list>
156 Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ,
157 DS.getNumProtocolQualifiers());
Steve Naroff4262a072009-02-23 18:53:24 +0000158 else if (Result == Context.getObjCClassType())
159 // Class<protocol-list>
Steve Naroff8dfb0c52009-02-21 19:50:43 +0000160 Diag(DS.getSourceRange().getBegin(),
Steve Naroff4262a072009-02-23 18:53:24 +0000161 diag::err_qualified_class_unsupported) << DS.getSourceRange();
162 else
163 Diag(DS.getSourceRange().getBegin(),
164 diag::err_invalid_protocol_qualifiers) << DS.getSourceRange();
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000165 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 // TypeQuals handled by caller.
Chris Lattner958858e2008-02-20 21:40:32 +0000167 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000168 }
Chris Lattner958858e2008-02-20 21:40:32 +0000169 case DeclSpec::TST_typeofType:
170 Result = QualType::getFromOpaquePtr(DS.getTypeRep());
171 assert(!Result.isNull() && "Didn't get a type for typeof?");
Steve Naroffd1861fd2007-07-31 12:34:36 +0000172 // TypeQuals handled by caller.
Chris Lattnerfab5b452008-02-20 23:53:49 +0000173 Result = Context.getTypeOfType(Result);
Chris Lattner958858e2008-02-20 21:40:32 +0000174 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000175 case DeclSpec::TST_typeofExpr: {
176 Expr *E = static_cast<Expr *>(DS.getTypeRep());
177 assert(E && "Didn't get an expression for typeof?");
178 // TypeQuals handled by caller.
Douglas Gregor72564e72009-02-26 23:50:07 +0000179 Result = Context.getTypeOfExprType(E);
Chris Lattner958858e2008-02-20 21:40:32 +0000180 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000181 }
Douglas Gregor809070a2009-02-18 17:45:20 +0000182 case DeclSpec::TST_error:
183 return QualType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000184 }
Chris Lattner958858e2008-02-20 21:40:32 +0000185
186 // Handle complex types.
Douglas Gregorf244cd72009-02-14 21:06:05 +0000187 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) {
188 if (getLangOptions().Freestanding)
189 Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex);
Chris Lattnerfab5b452008-02-20 23:53:49 +0000190 Result = Context.getComplexType(Result);
Douglas Gregorf244cd72009-02-14 21:06:05 +0000191 }
Chris Lattner958858e2008-02-20 21:40:32 +0000192
193 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
194 "FIXME: imaginary types not supported yet!");
195
Chris Lattner38d8b982008-02-20 22:04:11 +0000196 // See if there are any attributes on the declspec that apply to the type (as
197 // opposed to the decl).
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000198 if (const AttributeList *AL = DS.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000199 ProcessTypeAttributeList(Result, AL);
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000200
Chris Lattner96b77fc2008-04-02 06:50:17 +0000201 // Apply const/volatile/restrict qualifiers to T.
202 if (unsigned TypeQuals = DS.getTypeQualifiers()) {
203
204 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
205 // or incomplete types shall not be restrict-qualified." C++ also allows
206 // restrict-qualified references.
207 if (TypeQuals & QualType::Restrict) {
Daniel Dunbarbb710012009-02-26 19:13:44 +0000208 if (Result->isPointerType() || Result->isReferenceType()) {
209 QualType EltTy = Result->isPointerType() ?
210 Result->getAsPointerType()->getPointeeType() :
211 Result->getAsReferenceType()->getPointeeType();
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000212
213 // If we have a pointer or reference, the pointee must have an object or
214 // incomplete type.
215 if (!EltTy->isIncompleteOrObjectType()) {
216 Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000217 diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattnerd1625842008-11-24 06:25:27 +0000218 << EltTy << DS.getSourceRange();
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000219 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
220 }
221 } else {
Chris Lattner96b77fc2008-04-02 06:50:17 +0000222 Diag(DS.getRestrictSpecLoc(),
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000223 diag::err_typecheck_invalid_restrict_not_pointer)
Chris Lattnerd1625842008-11-24 06:25:27 +0000224 << Result << DS.getSourceRange();
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000225 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
Chris Lattner96b77fc2008-04-02 06:50:17 +0000226 }
Chris Lattner96b77fc2008-04-02 06:50:17 +0000227 }
228
229 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
230 // of a function type includes any type qualifiers, the behavior is
231 // undefined."
232 if (Result->isFunctionType() && TypeQuals) {
233 // Get some location to point at, either the C or V location.
234 SourceLocation Loc;
235 if (TypeQuals & QualType::Const)
236 Loc = DS.getConstSpecLoc();
237 else {
238 assert((TypeQuals & QualType::Volatile) &&
239 "Has CV quals but not C or V?");
240 Loc = DS.getVolatileSpecLoc();
241 }
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000242 Diag(Loc, diag::warn_typecheck_function_qualifiers)
Chris Lattnerd1625842008-11-24 06:25:27 +0000243 << Result << DS.getSourceRange();
Chris Lattner96b77fc2008-04-02 06:50:17 +0000244 }
245
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000246 // C++ [dcl.ref]p1:
247 // Cv-qualified references are ill-formed except when the
248 // cv-qualifiers are introduced through the use of a typedef
249 // (7.1.3) or of a template type argument (14.3), in which
250 // case the cv-qualifiers are ignored.
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000251 // FIXME: Shouldn't we be checking SCS_typedef here?
252 if (DS.getTypeSpecType() == DeclSpec::TST_typename &&
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000253 TypeQuals && Result->isReferenceType()) {
254 TypeQuals &= ~QualType::Const;
255 TypeQuals &= ~QualType::Volatile;
256 }
257
Chris Lattner96b77fc2008-04-02 06:50:17 +0000258 Result = Result.getQualifiedType(TypeQuals);
259 }
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000260 return Result;
261}
262
Mike Stump98eb8a72009-02-04 22:31:32 +0000263/// GetTypeForDeclarator - Convert the type for the specified
264/// declarator to Type instances. Skip the outermost Skip type
265/// objects.
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000266QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S, unsigned Skip) {
Mike Stump98eb8a72009-02-04 22:31:32 +0000267 bool OmittedReturnType = false;
268
269 if (D.getContext() == Declarator::BlockLiteralContext
270 && Skip == 0
271 && !D.getDeclSpec().hasTypeSpecifier()
272 && (D.getNumTypeObjects() == 0
273 || (D.getNumTypeObjects() == 1
274 && D.getTypeObject(0).Kind == DeclaratorChunk::Function)))
275 OmittedReturnType = true;
276
Chris Lattnerb23deda2007-08-28 16:40:32 +0000277 // long long is a C99 feature.
Chris Lattnerd1eb3322007-08-28 16:41:29 +0000278 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Chris Lattnerb23deda2007-08-28 16:40:32 +0000279 D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
280 Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
Douglas Gregor930d8b52009-01-30 22:09:00 +0000281
282 // Determine the type of the declarator. Not all forms of declarator
283 // have a type.
284 QualType T;
285 switch (D.getKind()) {
286 case Declarator::DK_Abstract:
287 case Declarator::DK_Normal:
Mike Stump98eb8a72009-02-04 22:31:32 +0000288 case Declarator::DK_Operator: {
289 const DeclSpec& DS = D.getDeclSpec();
290 if (OmittedReturnType)
291 // We default to a dependent type initially. Can be modified by
292 // the first return statement.
293 T = Context.DependentTy;
Douglas Gregor809070a2009-02-18 17:45:20 +0000294 else {
Mike Stump98eb8a72009-02-04 22:31:32 +0000295 T = ConvertDeclSpecToType(DS);
Douglas Gregor809070a2009-02-18 17:45:20 +0000296 if (T.isNull())
297 return T;
298 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000299 break;
Mike Stump98eb8a72009-02-04 22:31:32 +0000300 }
Douglas Gregor930d8b52009-01-30 22:09:00 +0000301
302 case Declarator::DK_Constructor:
303 case Declarator::DK_Destructor:
304 case Declarator::DK_Conversion:
305 // Constructors and destructors don't have return types. Use
306 // "void" instead. Conversion operators will check their return
307 // types separately.
308 T = Context.VoidTy;
309 break;
310 }
Sebastian Redl4c5d3202008-11-21 19:14:01 +0000311
Mike Stump98eb8a72009-02-04 22:31:32 +0000312 // Walk the DeclTypeInfo, building the recursive type as we go.
313 // DeclTypeInfos are ordered from the identifier out, which is
314 // opposite of what we want :).
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000315 for (unsigned i = Skip, e = D.getNumTypeObjects(); i != e; ++i) {
316 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1+Skip);
Reid Spencer5f016e22007-07-11 17:01:13 +0000317 switch (DeclType.Kind) {
318 default: assert(0 && "Unknown decltype!");
Steve Naroff5618bd42008-08-27 16:04:49 +0000319 case DeclaratorChunk::BlockPointer:
320 if (DeclType.Cls.TypeQuals)
321 Diag(D.getIdentifierLoc(), diag::err_qualified_block_pointer_type);
322 if (!T.getTypePtr()->isFunctionType())
323 Diag(D.getIdentifierLoc(), diag::err_nonfunction_block_type);
324 else
325 T = Context.getBlockPointerType(T);
326 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000327 case DeclaratorChunk::Pointer:
Chris Lattner02c642e2007-07-31 21:33:24 +0000328 if (T->isReferenceType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000329 // C++ 8.3.2p4: There shall be no ... pointers to references ...
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000330 Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference)
331 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroffe1223f72007-08-28 03:03:08 +0000332 D.setInvalidType(true);
Chris Lattner5265af52007-07-19 00:42:40 +0000333 T = Context.IntTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000334 }
335
Chris Lattner96b77fc2008-04-02 06:50:17 +0000336 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
337 // object or incomplete types shall not be restrict-qualified."
338 if ((DeclType.Ptr.TypeQuals & QualType::Restrict) &&
Chris Lattnerd805bec2008-04-02 06:59:01 +0000339 !T->isIncompleteOrObjectType()) {
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000340 Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattnerd1625842008-11-24 06:25:27 +0000341 << T;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000342 DeclType.Ptr.TypeQuals &= ~QualType::Restrict;
343 }
344
Reid Spencer5f016e22007-07-11 17:01:13 +0000345 // Apply the pointer typequals to the pointer object.
346 T = Context.getPointerType(T).getQualifiedType(DeclType.Ptr.TypeQuals);
347 break;
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000348 case DeclaratorChunk::Reference: {
349 // Whether we should suppress the creation of the reference.
350 bool SuppressReference = false;
351 if (T->isReferenceType()) {
352 // C++ [dcl.ref]p4: There shall be no references to references.
353 //
354 // According to C++ DR 106, references to references are only
355 // diagnosed when they are written directly (e.g., "int & &"),
356 // but not when they happen via a typedef:
357 //
358 // typedef int& intref;
359 // typedef intref& intref2;
360 //
361 // Parser::ParserDeclaratorInternal diagnoses the case where
362 // references are written directly; here, we handle the
363 // collapsing of references-to-references as described in C++
364 // DR 106 and amended by C++ DR 540.
365 SuppressReference = true;
366 }
367
368 // C++ [dcl.ref]p1:
369 // A declarator that specifies the type “reference to cv void”
370 // is ill-formed.
371 if (T->isVoidType()) {
372 Diag(DeclType.Loc, diag::err_reference_to_void);
Steve Naroffe1223f72007-08-28 03:03:08 +0000373 D.setInvalidType(true);
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000374 T = Context.IntTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000375 }
376
Chris Lattner96b77fc2008-04-02 06:50:17 +0000377 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
378 // object or incomplete types shall not be restrict-qualified."
379 if (DeclType.Ref.HasRestrict &&
Chris Lattnerd805bec2008-04-02 06:59:01 +0000380 !T->isIncompleteOrObjectType()) {
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000381 Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
Chris Lattnerd1625842008-11-24 06:25:27 +0000382 << T;
Chris Lattner96b77fc2008-04-02 06:50:17 +0000383 DeclType.Ref.HasRestrict = false;
384 }
385
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000386 if (!SuppressReference)
387 T = Context.getReferenceType(T);
Chris Lattner96b77fc2008-04-02 06:50:17 +0000388
389 // Handle restrict on references.
390 if (DeclType.Ref.HasRestrict)
391 T.addRestrict();
Reid Spencer5f016e22007-07-11 17:01:13 +0000392 break;
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +0000393 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000394 case DeclaratorChunk::Array: {
Chris Lattnerfd89bc82008-04-02 01:05:10 +0000395 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner94f81fd2007-08-28 16:54:00 +0000396 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000397 ArrayType::ArraySizeModifier ASM;
398 if (ATI.isStar)
399 ASM = ArrayType::Star;
400 else if (ATI.hasStatic)
401 ASM = ArrayType::Static;
402 else
403 ASM = ArrayType::Normal;
Chris Lattner5265af52007-07-19 00:42:40 +0000404
Reid Spencer5f016e22007-07-11 17:01:13 +0000405 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
406 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
Douglas Gregor4ec339f2009-01-19 19:26:10 +0000407 if (DiagnoseIncompleteType(D.getIdentifierLoc(), T,
408 diag::err_illegal_decl_array_incomplete_type)) {
Steve Naroffe1223f72007-08-28 03:03:08 +0000409 T = Context.IntTy;
410 D.setInvalidType(true);
Chris Lattner5265af52007-07-19 00:42:40 +0000411 } else if (T->isFunctionType()) {
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000412 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_functions)
413 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroffe1223f72007-08-28 03:03:08 +0000414 T = Context.getPointerType(T);
415 D.setInvalidType(true);
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000416 } else if (const ReferenceType *RT = T->getAsReferenceType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000417 // C++ 8.3.2p4: There shall be no ... arrays of references ...
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000418 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_references)
419 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000420 T = RT->getPointeeType();
Steve Naroffe1223f72007-08-28 03:03:08 +0000421 D.setInvalidType(true);
Chris Lattner02c642e2007-07-31 21:33:24 +0000422 } else if (const RecordType *EltTy = T->getAsRecordType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000423 // If the element type is a struct or union that contains a variadic
Douglas Gregor0bfe54f2009-02-10 21:49:46 +0000424 // array, accept it as a GNU extension: C99 6.7.2.1p2.
425 if (EltTy->getDecl()->hasFlexibleArrayMember())
426 Diag(DeclType.Loc, diag::ext_flexible_array_in_array) << T;
Chris Lattner43477ca2008-08-18 22:49:54 +0000427 } else if (T->isObjCInterfaceType()) {
Chris Lattnerd1625842008-11-24 06:25:27 +0000428 Diag(DeclType.Loc, diag::warn_objc_array_of_interfaces) << T;
Reid Spencer5f016e22007-07-11 17:01:13 +0000429 }
Chris Lattner43477ca2008-08-18 22:49:54 +0000430
Steve Naroff42471f82007-08-30 22:35:45 +0000431 // C99 6.7.5.2p1: The size expression shall have integer type.
432 if (ArraySize && !ArraySize->getType()->isIntegerType()) {
Chris Lattnerd3a94e22008-11-20 06:06:08 +0000433 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int)
Chris Lattnerd1625842008-11-24 06:25:27 +0000434 << ArraySize->getType() << ArraySize->getSourceRange();
Steve Naroff42471f82007-08-30 22:35:45 +0000435 D.setInvalidType(true);
Ted Kremenek169a2662009-02-07 01:51:40 +0000436 ArraySize->Destroy(Context);
Chris Lattnerfd89bc82008-04-02 01:05:10 +0000437 ATI.NumElts = ArraySize = 0;
Steve Naroff42471f82007-08-30 22:35:45 +0000438 }
Eli Friedman1ca48132009-02-21 00:44:51 +0000439 llvm::APSInt ConstVal(32);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000440 if (!ArraySize) {
441 T = Context.getIncompleteArrayType(T, ASM, ATI.TypeQuals);
Douglas Gregor898574e2008-12-05 23:32:09 +0000442 } else if (ArraySize->isValueDependent()) {
443 T = Context.getDependentSizedArrayType(T, ArraySize, ASM, ATI.TypeQuals);
Eli Friedman1ca48132009-02-21 00:44:51 +0000444 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000445 !T->isConstantSizeType()) {
Eli Friedman37148aa2008-05-14 00:40:18 +0000446 // Per C99, a variable array is an array with either a non-constant
447 // size or an element type that has a non-constant-size
Steve Naroffc9406122007-08-30 18:10:14 +0000448 T = Context.getVariableArrayType(T, ArraySize, ASM, ATI.TypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000449 } else {
Steve Naroff42471f82007-08-30 22:35:45 +0000450 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
451 // have a value greater than zero.
452 if (ConstVal.isSigned()) {
453 if (ConstVal.isNegative()) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000454 Diag(ArraySize->getLocStart(),
455 diag::err_typecheck_negative_array_size)
456 << ArraySize->getSourceRange();
Steve Naroff42471f82007-08-30 22:35:45 +0000457 D.setInvalidType(true);
458 } else if (ConstVal == 0) {
459 // GCC accepts zero sized static arrays.
Eli Friedman1ca48132009-02-21 00:44:51 +0000460 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size)
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000461 << ArraySize->getSourceRange();
Steve Naroff42471f82007-08-30 22:35:45 +0000462 }
463 }
Steve Naroffc9406122007-08-30 18:10:14 +0000464 T = Context.getConstantArrayType(T, ConstVal, ASM, ATI.TypeQuals);
Steve Naroff42471f82007-08-30 22:35:45 +0000465 }
Chris Lattner94f81fd2007-08-28 16:54:00 +0000466 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
Chris Lattnera1fcbad2008-12-18 06:50:14 +0000467 if (!getLangOptions().C99) {
468 if (ArraySize && !ArraySize->isValueDependent() &&
469 !ArraySize->isIntegerConstantExpr(Context))
470 Diag(D.getIdentifierLoc(), diag::ext_vla);
471 else if (ASM != ArrayType::Normal || ATI.TypeQuals != 0)
472 Diag(D.getIdentifierLoc(), diag::ext_c99_array_usage);
473 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000474 break;
475 }
Sebastian Redlf30208a2009-01-24 21:16:55 +0000476 case DeclaratorChunk::Function: {
Reid Spencer5f016e22007-07-11 17:01:13 +0000477 // If the function declarator has a prototype (i.e. it is not () and
478 // does not have a K&R-style identifier list), then the arguments are part
479 // of the type, otherwise the argument list is ().
480 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Chris Lattner68cfd492007-12-19 18:01:43 +0000481
Chris Lattnercd881292007-12-19 05:31:29 +0000482 // C99 6.7.5.3p1: The return type may not be a function or array type.
Chris Lattner68cfd492007-12-19 18:01:43 +0000483 if (T->isArrayType() || T->isFunctionType()) {
Chris Lattnerd1625842008-11-24 06:25:27 +0000484 Diag(DeclType.Loc, diag::err_func_returning_array_function) << T;
Chris Lattnercd881292007-12-19 05:31:29 +0000485 T = Context.IntTy;
486 D.setInvalidType(true);
487 }
488
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000489 if (FTI.NumArgs == 0) {
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +0000490 if (getLangOptions().CPlusPlus) {
491 // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the
492 // function takes no arguments.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000493 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic,FTI.TypeQuals);
Douglas Gregor965acbb2009-02-18 07:07:28 +0000494 } else if (FTI.isVariadic) {
495 // We allow a zero-parameter variadic function in C if the
496 // function is marked with the "overloadable"
497 // attribute. Scan for this attribute now.
498 bool Overloadable = false;
499 for (const AttributeList *Attrs = D.getAttributes();
500 Attrs; Attrs = Attrs->getNext()) {
501 if (Attrs->getKind() == AttributeList::AT_overloadable) {
502 Overloadable = true;
503 break;
504 }
505 }
506
507 if (!Overloadable)
508 Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg);
509 T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, 0);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +0000510 } else {
511 // Simple void foo(), where the incoming T is the result type.
Douglas Gregor72564e72009-02-26 23:50:07 +0000512 T = Context.getFunctionNoProtoType(T);
Argyrios Kyrtzidisc6f73452008-10-16 17:31:08 +0000513 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000514 } else if (FTI.ArgInfo[0].Param == 0) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000515 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000516 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
Reid Spencer5f016e22007-07-11 17:01:13 +0000517 } else {
518 // Otherwise, we have a function with an argument list that is
519 // potentially variadic.
520 llvm::SmallVector<QualType, 16> ArgTys;
521
522 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Chris Lattner8123a952008-04-10 02:22:51 +0000523 ParmVarDecl *Param = (ParmVarDecl *)FTI.ArgInfo[i].Param;
524 QualType ArgTy = Param->getType();
Chris Lattner78c75fb2007-07-21 05:30:18 +0000525 assert(!ArgTy.isNull() && "Couldn't parse type?");
Steve Naroff08d51392007-09-10 22:17:00 +0000526 //
527 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
528 // This matches the conversion that is done in
Nate Begemanbff5f5c2007-11-13 21:49:48 +0000529 // Sema::ActOnParamDeclarator(). Without this conversion, the
Steve Naroff08d51392007-09-10 22:17:00 +0000530 // argument type in the function prototype *will not* match the
531 // type in ParmVarDecl (which makes the code generator unhappy).
532 //
533 // FIXME: We still apparently need the conversion in
Chris Lattnere6327742008-04-02 05:18:44 +0000534 // Sema::ActOnParamDeclarator(). This doesn't make any sense, since
Steve Naroff08d51392007-09-10 22:17:00 +0000535 // it should be driving off the type being created here.
536 //
537 // FIXME: If a source translation tool needs to see the original type,
538 // then we need to consider storing both types somewhere...
539 //
Chris Lattnere6327742008-04-02 05:18:44 +0000540 if (ArgTy->isArrayType()) {
541 ArgTy = Context.getArrayDecayedType(ArgTy);
Chris Lattner529bd022008-01-02 22:50:48 +0000542 } else if (ArgTy->isFunctionType())
Steve Naroff08d51392007-09-10 22:17:00 +0000543 ArgTy = Context.getPointerType(ArgTy);
Chris Lattnere6327742008-04-02 05:18:44 +0000544
Reid Spencer5f016e22007-07-11 17:01:13 +0000545 // Look for 'void'. void is allowed only as a single argument to a
546 // function with no other parameters (C99 6.7.5.3p10). We record
Douglas Gregor72564e72009-02-26 23:50:07 +0000547 // int(void) as a FunctionProtoType with an empty argument list.
Steve Naroff08d51392007-09-10 22:17:00 +0000548 else if (ArgTy->isVoidType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000549 // If this is something like 'float(int, void)', reject it. 'void'
550 // is an incomplete type (C99 6.2.5p19) and function decls cannot
551 // have arguments of incomplete type.
552 if (FTI.NumArgs != 1 || FTI.isVariadic) {
553 Diag(DeclType.Loc, diag::err_void_only_param);
Chris Lattner2ff54262007-07-21 05:18:12 +0000554 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +0000555 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +0000556 } else if (FTI.ArgInfo[i].Ident) {
557 // Reject, but continue to parse 'int(void abc)'.
Reid Spencer5f016e22007-07-11 17:01:13 +0000558 Diag(FTI.ArgInfo[i].IdentLoc,
Chris Lattner4565d4e2007-07-21 05:26:43 +0000559 diag::err_param_with_void_type);
Chris Lattner2ff54262007-07-21 05:18:12 +0000560 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +0000561 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +0000562 } else {
563 // Reject, but continue to parse 'float(const void)'.
Chris Lattnerf46699c2008-02-20 20:55:12 +0000564 if (ArgTy.getCVRQualifiers())
Chris Lattner2ff54262007-07-21 05:18:12 +0000565 Diag(DeclType.Loc, diag::err_void_param_qualified);
566
567 // Do not add 'void' to the ArgTys list.
568 break;
569 }
Eli Friedmaneb4b7052008-08-25 21:31:01 +0000570 } else if (!FTI.hasPrototype) {
571 if (ArgTy->isPromotableIntegerType()) {
572 ArgTy = Context.IntTy;
573 } else if (const BuiltinType* BTy = ArgTy->getAsBuiltinType()) {
574 if (BTy->getKind() == BuiltinType::Float)
575 ArgTy = Context.DoubleTy;
576 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000577 }
578
579 ArgTys.push_back(ArgTy);
580 }
581 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000582 FTI.isVariadic, FTI.TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000583 }
584 break;
585 }
Sebastian Redlf30208a2009-01-24 21:16:55 +0000586 case DeclaratorChunk::MemberPointer:
587 // The scope spec must refer to a class, or be dependent.
588 DeclContext *DC = static_cast<DeclContext*>(
589 DeclType.Mem.Scope().getScopeRep());
590 QualType ClsType;
591 // FIXME: Extend for dependent types when it's actually supported.
592 // See ActOnCXXNestedNameSpecifier.
593 if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(DC)) {
594 ClsType = Context.getTagDeclType(RD);
595 } else {
596 if (DC) {
597 Diag(DeclType.Mem.Scope().getBeginLoc(),
598 diag::err_illegal_decl_mempointer_in_nonclass)
599 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name")
600 << DeclType.Mem.Scope().getRange();
601 }
602 D.setInvalidType(true);
603 ClsType = Context.IntTy;
604 }
605
606 // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member
607 // with reference type, or "cv void."
608 if (T->isReferenceType()) {
609 Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference)
610 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
611 D.setInvalidType(true);
612 T = Context.IntTy;
613 }
614 if (T->isVoidType()) {
615 Diag(DeclType.Loc, diag::err_illegal_decl_mempointer_to_void)
616 << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
617 T = Context.IntTy;
618 }
619
620 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
621 // object or incomplete types shall not be restrict-qualified."
622 if ((DeclType.Mem.TypeQuals & QualType::Restrict) &&
623 !T->isIncompleteOrObjectType()) {
624 Diag(DeclType.Loc, diag::err_typecheck_invalid_restrict_invalid_pointee)
625 << T;
626 DeclType.Mem.TypeQuals &= ~QualType::Restrict;
627 }
628
Sebastian Redl4433aaf2009-01-25 19:43:20 +0000629 T = Context.getMemberPointerType(T, ClsType.getTypePtr()).
630 getQualifiedType(DeclType.Mem.TypeQuals);
Sebastian Redlf30208a2009-01-24 21:16:55 +0000631
632 break;
633 }
634
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000635 // See if there are any attributes on this declarator chunk.
636 if (const AttributeList *AL = DeclType.getAttrs())
637 ProcessTypeAttributeList(T, AL);
Reid Spencer5f016e22007-07-11 17:01:13 +0000638 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000639
640 if (getLangOptions().CPlusPlus && T->isFunctionType()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000641 const FunctionProtoType *FnTy = T->getAsFunctionProtoType();
642 assert(FnTy && "Why oh why is there not a FunctionProtoType here ?");
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000643
644 // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type
645 // for a nonstatic member function, the function type to which a pointer
646 // to member refers, or the top-level function type of a function typedef
647 // declaration.
648 if (FnTy->getTypeQuals() != 0 &&
649 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
Douglas Gregor584049d2008-12-15 23:53:10 +0000650 ((D.getContext() != Declarator::MemberContext &&
651 (!D.getCXXScopeSpec().isSet() ||
652 !static_cast<DeclContext*>(D.getCXXScopeSpec().getScopeRep())
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000653 ->isRecord())) ||
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000654 D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) {
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000655 if (D.isFunctionDeclarator())
656 Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type);
657 else
658 Diag(D.getIdentifierLoc(),
659 diag::err_invalid_qualified_typedef_function_type_use);
660
661 // Strip the cv-quals from the type.
662 T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +0000663 FnTy->getNumArgs(), FnTy->isVariadic(), 0);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +0000664 }
665 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000666
Chris Lattner0bf29ad2008-06-29 00:19:33 +0000667 // If there were any type attributes applied to the decl itself (not the
668 // type, apply the type attribute to the type!)
669 if (const AttributeList *Attrs = D.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000670 ProcessTypeAttributeList(T, Attrs);
Chris Lattner0bf29ad2008-06-29 00:19:33 +0000671
Reid Spencer5f016e22007-07-11 17:01:13 +0000672 return T;
673}
674
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000675/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
Fariborz Jahanian360300c2007-11-09 22:27:59 +0000676/// declarator
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000677QualType Sema::ObjCGetTypeForMethodDefinition(DeclTy *D) {
Chris Lattner89951a82009-02-20 18:43:26 +0000678 ObjCMethodDecl *MDecl = cast<ObjCMethodDecl>(static_cast<Decl *>(D));
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000679 QualType T = MDecl->getResultType();
680 llvm::SmallVector<QualType, 16> ArgTys;
681
Fariborz Jahanian35600022007-11-09 17:18:29 +0000682 // Add the first two invisible argument types for self and _cmd.
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000683 if (MDecl->isInstanceMethod()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000684 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000685 selfTy = Context.getPointerType(selfTy);
686 ArgTys.push_back(selfTy);
Chris Lattner89951a82009-02-20 18:43:26 +0000687 } else
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000688 ArgTys.push_back(Context.getObjCIdType());
689 ArgTys.push_back(Context.getObjCSelType());
Fariborz Jahanian35600022007-11-09 17:18:29 +0000690
Chris Lattner89951a82009-02-20 18:43:26 +0000691 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
692 E = MDecl->param_end(); PI != E; ++PI) {
693 QualType ArgTy = (*PI)->getType();
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000694 assert(!ArgTy.isNull() && "Couldn't parse type?");
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000695 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
696 // This matches the conversion that is done in
Chris Lattnere6327742008-04-02 05:18:44 +0000697 // Sema::ActOnParamDeclarator().
698 if (ArgTy->isArrayType())
699 ArgTy = Context.getArrayDecayedType(ArgTy);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000700 else if (ArgTy->isFunctionType())
701 ArgTy = Context.getPointerType(ArgTy);
702 ArgTys.push_back(ArgTy);
703 }
704 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +0000705 MDecl->isVariadic(), 0);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000706 return T;
707}
708
Sebastian Redl9e5e4aa2009-01-26 19:54:48 +0000709/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that
710/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
711/// they point to and return true. If T1 and T2 aren't pointer types
712/// or pointer-to-member types, or if they are not similar at this
713/// level, returns false and leaves T1 and T2 unchanged. Top-level
714/// qualifiers on T1 and T2 are ignored. This function will typically
715/// be called in a loop that successively "unwraps" pointer and
716/// pointer-to-member types to compare them at each level.
Chris Lattnerecb81f22009-02-16 21:43:00 +0000717bool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) {
Douglas Gregor57373262008-10-22 14:17:15 +0000718 const PointerType *T1PtrType = T1->getAsPointerType(),
719 *T2PtrType = T2->getAsPointerType();
720 if (T1PtrType && T2PtrType) {
721 T1 = T1PtrType->getPointeeType();
722 T2 = T2PtrType->getPointeeType();
723 return true;
724 }
725
Sebastian Redl4433aaf2009-01-25 19:43:20 +0000726 const MemberPointerType *T1MPType = T1->getAsMemberPointerType(),
727 *T2MPType = T2->getAsMemberPointerType();
Sebastian Redl21593ac2009-01-28 18:33:18 +0000728 if (T1MPType && T2MPType &&
729 Context.getCanonicalType(T1MPType->getClass()) ==
730 Context.getCanonicalType(T2MPType->getClass())) {
Sebastian Redl4433aaf2009-01-25 19:43:20 +0000731 T1 = T1MPType->getPointeeType();
732 T2 = T2MPType->getPointeeType();
733 return true;
734 }
Douglas Gregor57373262008-10-22 14:17:15 +0000735 return false;
736}
737
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000738Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000739 // C99 6.7.6: Type names have no identifier. This is already validated by
740 // the parser.
741 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
742
Sebastian Redlcee63fb2008-12-02 14:43:59 +0000743 QualType T = GetTypeForDeclarator(D, S);
Douglas Gregor809070a2009-02-18 17:45:20 +0000744 if (T.isNull())
745 return true;
Steve Naroff5912a352007-08-28 20:14:24 +0000746
Douglas Gregor6d6eb572008-05-07 04:49:29 +0000747 // Check that there are no default arguments (C++ only).
748 if (getLangOptions().CPlusPlus)
749 CheckExtraCXXDefaultArguments(D);
750
Reid Spencer5f016e22007-07-11 17:01:13 +0000751 return T.getAsOpaquePtr();
752}
753
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000754
755
756//===----------------------------------------------------------------------===//
757// Type Attribute Processing
758//===----------------------------------------------------------------------===//
759
760/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
761/// specified type. The attribute contains 1 argument, the id of the address
762/// space for the type.
763static void HandleAddressSpaceTypeAttribute(QualType &Type,
764 const AttributeList &Attr, Sema &S){
765 // If this type is already address space qualified, reject it.
766 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
767 // for two or more different address spaces."
768 if (Type.getAddressSpace()) {
769 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
770 return;
771 }
772
773 // Check the attribute arguments.
774 if (Attr.getNumArgs() != 1) {
Chris Lattnerf3a41af2008-11-20 06:38:18 +0000775 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000776 return;
777 }
778 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
779 llvm::APSInt addrSpace(32);
780 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
Chris Lattnerdcd5ef12008-11-19 05:27:50 +0000781 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int)
782 << ASArgExpr->getSourceRange();
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000783 return;
784 }
785
786 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000787 Type = S.Context.getAddrSpaceQualType(Type, ASIdx);
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000788}
789
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000790/// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the
791/// specified type. The attribute contains 1 argument, weak or strong.
792static void HandleObjCGCTypeAttribute(QualType &Type,
Chris Lattner3b6b83b2009-02-18 22:58:38 +0000793 const AttributeList &Attr, Sema &S) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000794 if (Type.getObjCGCAttr() != QualType::GCNone) {
Fariborz Jahanian5934e752009-02-18 18:52:41 +0000795 S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000796 return;
797 }
798
799 // Check the attribute arguments.
Fariborz Jahanianba372b82009-02-18 17:52:36 +0000800 if (!Attr.getParameterName()) {
801 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
802 << "objc_gc" << 1;
803 return;
804 }
Chris Lattner3b6b83b2009-02-18 22:58:38 +0000805 QualType::GCAttrTypes GCAttr;
Fariborz Jahanianba372b82009-02-18 17:52:36 +0000806 if (Attr.getNumArgs() != 0) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000807 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
808 return;
809 }
810 if (Attr.getParameterName()->isStr("weak"))
Chris Lattner3b6b83b2009-02-18 22:58:38 +0000811 GCAttr = QualType::Weak;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000812 else if (Attr.getParameterName()->isStr("strong"))
Chris Lattner3b6b83b2009-02-18 22:58:38 +0000813 GCAttr = QualType::Strong;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000814 else {
815 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
816 << "objc_gc" << Attr.getParameterName();
817 return;
818 }
819
Chris Lattner3b6b83b2009-02-18 22:58:38 +0000820 Type = S.Context.getObjCGCQualType(Type, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000821}
822
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000823void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) {
Chris Lattner232e8822008-02-21 01:08:11 +0000824 // Scan through and apply attributes to this type where it makes sense. Some
825 // attributes (such as __address_space__, __vector_size__, etc) apply to the
826 // type, but others can be present in the type specifiers even though they
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000827 // apply to the decl. Here we apply type attributes and ignore the rest.
828 for (; AL; AL = AL->getNext()) {
Chris Lattner232e8822008-02-21 01:08:11 +0000829 // If this is an attribute we can handle, do so now, otherwise, add it to
830 // the LeftOverAttrs list for rechaining.
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000831 switch (AL->getKind()) {
Chris Lattner232e8822008-02-21 01:08:11 +0000832 default: break;
833 case AttributeList::AT_address_space:
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000834 HandleAddressSpaceTypeAttribute(Result, *AL, *this);
835 break;
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000836 case AttributeList::AT_objc_gc:
837 HandleObjCGCTypeAttribute(Result, *AL, *this);
838 break;
Chris Lattner232e8822008-02-21 01:08:11 +0000839 }
Chris Lattner232e8822008-02-21 01:08:11 +0000840 }
Chris Lattner232e8822008-02-21 01:08:11 +0000841}
842
Douglas Gregor4ec339f2009-01-19 19:26:10 +0000843/// @brief If the type T is incomplete and cannot be completed,
844/// produce a suitable diagnostic.
845///
846/// This routine checks whether the type @p T is complete in any
847/// context where a complete type is required. If @p T is a complete
848/// type, returns false. If @p T is incomplete, issues the diagnostic
849/// @p diag (giving it the type @p T) and returns true.
850///
851/// @param Loc The location in the source that the incomplete type
852/// diagnostic should refer to.
853///
854/// @param T The type that this routine is examining for completeness.
855///
856/// @param diag The diagnostic value (e.g.,
857/// @c diag::err_typecheck_decl_incomplete_type) that will be used
858/// for the error message if @p T is incomplete.
859///
860/// @param Range1 An optional range in the source code that will be a
861/// part of the "incomplete type" error message.
862///
863/// @param Range2 An optional range in the source code that will be a
864/// part of the "incomplete type" error message.
865///
866/// @param PrintType If non-NULL, the type that should be printed
867/// instead of @p T. This parameter should be used when the type that
868/// we're checking for incompleteness isn't the type that should be
869/// displayed to the user, e.g., when T is a type and PrintType is a
870/// pointer to T.
871///
872/// @returns @c true if @p T is incomplete and a diagnostic was emitted,
873/// @c false otherwise.
874///
875/// @todo When Clang gets proper support for C++ templates, this
876/// routine will also be able perform template instantiation when @p T
877/// is a class template specialization.
878bool Sema::DiagnoseIncompleteType(SourceLocation Loc, QualType T, unsigned diag,
879 SourceRange Range1, SourceRange Range2,
880 QualType PrintType) {
881 // If we have a complete type, we're done.
882 if (!T->isIncompleteType())
883 return false;
Eli Friedman3c0eb162008-05-27 03:33:27 +0000884
Douglas Gregor4ec339f2009-01-19 19:26:10 +0000885 if (PrintType.isNull())
886 PrintType = T;
887
888 // We have an incomplete type. Produce a diagnostic.
889 Diag(Loc, diag) << PrintType << Range1 << Range2;
890
891 // If the type was a forward declaration of a class/struct/union
892 // type, produce
893 const TagType *Tag = 0;
894 if (const RecordType *Record = T->getAsRecordType())
895 Tag = Record;
896 else if (const EnumType *Enum = T->getAsEnumType())
897 Tag = Enum;
898
899 if (Tag && !Tag->getDecl()->isInvalidDecl())
900 Diag(Tag->getDecl()->getLocation(),
901 Tag->isBeingDefined() ? diag::note_type_being_defined
902 : diag::note_forward_declaration)
903 << QualType(Tag, 0);
904
905 return true;
906}