blob: 56231c58e73bc653cba27761c2ce7c3e6a6913e8 [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"
16#include "clang/AST/Decl.h"
Steve Naroff980e5082007-10-01 19:00:59 +000017#include "clang/AST/DeclObjC.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000018#include "clang/AST/Expr.h"
Daniel Dunbare4858a62008-08-11 03:45:03 +000019#include "clang/Basic/Diagnostic.h"
Chris Lattnerb23deda2007-08-28 16:40:32 +000020#include "clang/Basic/LangOptions.h"
Daniel Dunbare4858a62008-08-11 03:45:03 +000021#include "clang/Parse/DeclSpec.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022using namespace clang;
23
24/// ConvertDeclSpecToType - Convert the specified declspec to the appropriate
25/// type object. This returns null on 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 Lattnerd658b562008-04-05 06:32:51 +000032 default: assert(0 && "Unknown TypeSpecType!");
Chris Lattner96b77fc2008-04-02 06:50:17 +000033 case DeclSpec::TST_void:
34 Result = Context.VoidTy;
35 break;
Reid Spencer5f016e22007-07-11 17:01:13 +000036 case DeclSpec::TST_char:
37 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
Chris Lattnerfab5b452008-02-20 23:53:49 +000038 Result = Context.CharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000039 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
Chris Lattnerfab5b452008-02-20 23:53:49 +000040 Result = Context.SignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000041 else {
42 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
43 "Unknown TSS value");
Chris Lattnerfab5b452008-02-20 23:53:49 +000044 Result = Context.UnsignedCharTy;
Reid Spencer5f016e22007-07-11 17:01:13 +000045 }
Chris Lattner958858e2008-02-20 21:40:32 +000046 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +000047 case DeclSpec::TST_wchar:
48 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
49 Result = Context.WCharTy;
50 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) {
51 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec,
52 DS.getSpecifierName(DS.getTypeSpecType()));
53 Result = Context.getSignedWCharType();
54 } else {
55 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
56 "Unknown TSS value");
57 Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec,
58 DS.getSpecifierName(DS.getTypeSpecType()));
59 Result = Context.getUnsignedWCharType();
60 }
61 break;
Chris Lattnerd658b562008-04-05 06:32:51 +000062 case DeclSpec::TST_unspecified:
Chris Lattner62f5f7f2008-07-26 00:46:50 +000063 // "<proto1,proto2>" is an objc qualified ID with a missing id.
Chris Lattnerae4da612008-07-26 01:53:50 +000064 if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) {
65 Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ,
Chris Lattner62f5f7f2008-07-26 00:46:50 +000066 DS.getNumProtocolQualifiers());
67 break;
68 }
69
Chris Lattnerd658b562008-04-05 06:32:51 +000070 // Unspecified typespec defaults to int in C90. However, the C90 grammar
71 // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
72 // type-qualifier, or storage-class-specifier. If not, emit an extwarn.
73 // Note that the one exception to this is function definitions, which are
74 // allowed to be completely missing a declspec. This is handled in the
75 // parser already though by it pretending to have seen an 'int' in this
76 // case.
77 if (getLangOptions().ImplicitInt) {
78 if ((DS.getParsedSpecifiers() & (DeclSpec::PQ_StorageClassSpecifier |
79 DeclSpec::PQ_TypeSpecifier |
80 DeclSpec::PQ_TypeQualifier)) == 0)
81 Diag(DS.getSourceRange().getBegin(), diag::ext_missing_declspec);
82 } else {
83 // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says:
84 // "At least one type specifier shall be given in the declaration
85 // specifiers in each declaration, and in the specifier-qualifier list in
86 // each struct declaration and type name."
87 if (!DS.hasTypeSpecifier())
88 Diag(DS.getSourceRange().getBegin(), diag::ext_missing_type_specifier);
89 }
90
91 // FALL THROUGH.
Chris Lattner3cbc38b2007-08-21 17:02:28 +000092 case DeclSpec::TST_int: {
Reid Spencer5f016e22007-07-11 17:01:13 +000093 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
94 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +000095 case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
96 case DeclSpec::TSW_short: Result = Context.ShortTy; break;
97 case DeclSpec::TSW_long: Result = Context.LongTy; break;
98 case DeclSpec::TSW_longlong: Result = Context.LongLongTy; break;
Reid Spencer5f016e22007-07-11 17:01:13 +000099 }
100 } else {
101 switch (DS.getTypeSpecWidth()) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000102 case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
103 case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break;
104 case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break;
105 case DeclSpec::TSW_longlong: Result =Context.UnsignedLongLongTy; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000106 }
107 }
Chris Lattner958858e2008-02-20 21:40:32 +0000108 break;
Chris Lattner3cbc38b2007-08-21 17:02:28 +0000109 }
Chris Lattnerfab5b452008-02-20 23:53:49 +0000110 case DeclSpec::TST_float: Result = Context.FloatTy; break;
Chris Lattner958858e2008-02-20 21:40:32 +0000111 case DeclSpec::TST_double:
112 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000113 Result = Context.LongDoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000114 else
Chris Lattnerfab5b452008-02-20 23:53:49 +0000115 Result = Context.DoubleTy;
Chris Lattner958858e2008-02-20 21:40:32 +0000116 break;
Chris Lattnerfab5b452008-02-20 23:53:49 +0000117 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
Reid Spencer5f016e22007-07-11 17:01:13 +0000118 case DeclSpec::TST_decimal32: // _Decimal32
119 case DeclSpec::TST_decimal64: // _Decimal64
120 case DeclSpec::TST_decimal128: // _Decimal128
121 assert(0 && "FIXME: GNU decimal extensions not supported yet!");
Chris Lattner99dc9142008-04-13 18:59:07 +0000122 case DeclSpec::TST_class:
Reid Spencer5f016e22007-07-11 17:01:13 +0000123 case DeclSpec::TST_enum:
124 case DeclSpec::TST_union:
125 case DeclSpec::TST_struct: {
126 Decl *D = static_cast<Decl *>(DS.getTypeRep());
Chris Lattner99dc9142008-04-13 18:59:07 +0000127 assert(D && "Didn't get a decl for a class/enum/union/struct?");
Reid Spencer5f016e22007-07-11 17:01:13 +0000128 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
129 DS.getTypeSpecSign() == 0 &&
130 "Can't handle qualifiers on typedef names yet!");
131 // TypeQuals handled by caller.
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000132 Result = Context.getTypeDeclType(cast<TypeDecl>(D));
Chris Lattner958858e2008-02-20 21:40:32 +0000133 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000134 }
135 case DeclSpec::TST_typedef: {
136 Decl *D = static_cast<Decl *>(DS.getTypeRep());
137 assert(D && "Didn't get a decl for a typedef?");
138 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
139 DS.getTypeSpecSign() == 0 &&
140 "Can't handle qualifiers on typedef names yet!");
Chris Lattnerae4da612008-07-26 01:53:50 +0000141 DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers();
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000142
Steve Naroff3536b442007-09-06 21:24:23 +0000143 // FIXME: Adding a TST_objcInterface clause doesn't seem ideal, so
144 // we have this "hack" for now...
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000145 if (ObjCInterfaceDecl *ObjCIntDecl = dyn_cast<ObjCInterfaceDecl>(D)) {
Chris Lattnerae4da612008-07-26 01:53:50 +0000146 if (PQ == 0) {
Chris Lattnerfab5b452008-02-20 23:53:49 +0000147 Result = Context.getObjCInterfaceType(ObjCIntDecl);
Chris Lattner958858e2008-02-20 21:40:32 +0000148 break;
149 }
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000150
Chris Lattnerfab5b452008-02-20 23:53:49 +0000151 Result = Context.getObjCQualifiedInterfaceType(ObjCIntDecl,
Chris Lattnerae4da612008-07-26 01:53:50 +0000152 (ObjCProtocolDecl**)PQ,
Chris Lattner76549142008-02-21 01:32:26 +0000153 DS.getNumProtocolQualifiers());
Chris Lattner958858e2008-02-20 21:40:32 +0000154 break;
Chris Lattner62f5f7f2008-07-26 00:46:50 +0000155 } else if (TypedefDecl *typeDecl = dyn_cast<TypedefDecl>(D)) {
Chris Lattnerae4da612008-07-26 01:53:50 +0000156 if (Context.getObjCIdType() == Context.getTypedefType(typeDecl) && PQ) {
157 // id<protocol-list>
158 Result = Context.getObjCQualifiedIdType((ObjCProtocolDecl**)PQ,
159 DS.getNumProtocolQualifiers());
Chris Lattner958858e2008-02-20 21:40:32 +0000160 break;
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000161 }
162 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 // TypeQuals handled by caller.
Douglas Gregor2ce52f32008-04-13 21:07:44 +0000164 Result = Context.getTypeDeclType(dyn_cast<TypeDecl>(D));
Chris Lattner958858e2008-02-20 21:40:32 +0000165 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 }
Chris Lattner958858e2008-02-20 21:40:32 +0000167 case DeclSpec::TST_typeofType:
168 Result = QualType::getFromOpaquePtr(DS.getTypeRep());
169 assert(!Result.isNull() && "Didn't get a type for typeof?");
Steve Naroffd1861fd2007-07-31 12:34:36 +0000170 // TypeQuals handled by caller.
Chris Lattnerfab5b452008-02-20 23:53:49 +0000171 Result = Context.getTypeOfType(Result);
Chris Lattner958858e2008-02-20 21:40:32 +0000172 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000173 case DeclSpec::TST_typeofExpr: {
174 Expr *E = static_cast<Expr *>(DS.getTypeRep());
175 assert(E && "Didn't get an expression for typeof?");
176 // TypeQuals handled by caller.
Chris Lattnerfab5b452008-02-20 23:53:49 +0000177 Result = Context.getTypeOfExpr(E);
Chris Lattner958858e2008-02-20 21:40:32 +0000178 break;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000179 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000180 }
Chris Lattner958858e2008-02-20 21:40:32 +0000181
182 // Handle complex types.
183 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex)
Chris Lattnerfab5b452008-02-20 23:53:49 +0000184 Result = Context.getComplexType(Result);
Chris Lattner958858e2008-02-20 21:40:32 +0000185
186 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
187 "FIXME: imaginary types not supported yet!");
188
Chris Lattner38d8b982008-02-20 22:04:11 +0000189 // See if there are any attributes on the declspec that apply to the type (as
190 // opposed to the decl).
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000191 if (const AttributeList *AL = DS.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000192 ProcessTypeAttributeList(Result, AL);
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000193
Chris Lattner96b77fc2008-04-02 06:50:17 +0000194 // Apply const/volatile/restrict qualifiers to T.
195 if (unsigned TypeQuals = DS.getTypeQualifiers()) {
196
197 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
198 // or incomplete types shall not be restrict-qualified." C++ also allows
199 // restrict-qualified references.
200 if (TypeQuals & QualType::Restrict) {
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000201 if (const PointerLikeType *PT = Result->getAsPointerLikeType()) {
202 QualType EltTy = PT->getPointeeType();
203
204 // If we have a pointer or reference, the pointee must have an object or
205 // incomplete type.
206 if (!EltTy->isIncompleteOrObjectType()) {
207 Diag(DS.getRestrictSpecLoc(),
208 diag::err_typecheck_invalid_restrict_invalid_pointee,
209 EltTy.getAsString(), DS.getSourceRange());
210 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
211 }
212 } else {
Chris Lattner96b77fc2008-04-02 06:50:17 +0000213 Diag(DS.getRestrictSpecLoc(),
214 diag::err_typecheck_invalid_restrict_not_pointer,
215 Result.getAsString(), DS.getSourceRange());
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000216 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
Chris Lattner96b77fc2008-04-02 06:50:17 +0000217 }
Chris Lattner96b77fc2008-04-02 06:50:17 +0000218 }
219
220 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
221 // of a function type includes any type qualifiers, the behavior is
222 // undefined."
223 if (Result->isFunctionType() && TypeQuals) {
224 // Get some location to point at, either the C or V location.
225 SourceLocation Loc;
226 if (TypeQuals & QualType::Const)
227 Loc = DS.getConstSpecLoc();
228 else {
229 assert((TypeQuals & QualType::Volatile) &&
230 "Has CV quals but not C or V?");
231 Loc = DS.getVolatileSpecLoc();
232 }
233 Diag(Loc, diag::warn_typecheck_function_qualifiers,
234 Result.getAsString(), DS.getSourceRange());
235 }
236
237 Result = Result.getQualifiedType(TypeQuals);
238 }
Chris Lattnerf1d705c2008-02-21 01:07:18 +0000239 return Result;
240}
241
Reid Spencer5f016e22007-07-11 17:01:13 +0000242/// GetTypeForDeclarator - Convert the type for the specified declarator to Type
243/// instances.
244QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S) {
Chris Lattnerb23deda2007-08-28 16:40:32 +0000245 // long long is a C99 feature.
Chris Lattnerd1eb3322007-08-28 16:41:29 +0000246 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Chris Lattnerb23deda2007-08-28 16:40:32 +0000247 D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
248 Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
249
Chris Lattnerfab5b452008-02-20 23:53:49 +0000250 QualType T = ConvertDeclSpecToType(D.getDeclSpec());
Steve Naroffe1223f72007-08-28 03:03:08 +0000251
Reid Spencer5f016e22007-07-11 17:01:13 +0000252 // Walk the DeclTypeInfo, building the recursive type as we go. DeclTypeInfos
253 // are ordered from the identifier out, which is opposite of what we want :).
254 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
Chris Lattner76549142008-02-21 01:32:26 +0000255 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000256 switch (DeclType.Kind) {
257 default: assert(0 && "Unknown decltype!");
258 case DeclaratorChunk::Pointer:
Chris Lattner02c642e2007-07-31 21:33:24 +0000259 if (T->isReferenceType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000260 // C++ 8.3.2p4: There shall be no ... pointers to references ...
Chris Lattner96b77fc2008-04-02 06:50:17 +0000261 Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference,
Steve Naroff14f3f1b2008-01-14 23:33:18 +0000262 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroffe1223f72007-08-28 03:03:08 +0000263 D.setInvalidType(true);
Chris Lattner5265af52007-07-19 00:42:40 +0000264 T = Context.IntTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000265 }
266
Chris Lattner96b77fc2008-04-02 06:50:17 +0000267 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
268 // object or incomplete types shall not be restrict-qualified."
269 if ((DeclType.Ptr.TypeQuals & QualType::Restrict) &&
Chris Lattnerd805bec2008-04-02 06:59:01 +0000270 !T->isIncompleteOrObjectType()) {
Chris Lattner96b77fc2008-04-02 06:50:17 +0000271 Diag(DeclType.Loc,
272 diag::err_typecheck_invalid_restrict_invalid_pointee,
273 T.getAsString());
274 DeclType.Ptr.TypeQuals &= QualType::Restrict;
275 }
276
Reid Spencer5f016e22007-07-11 17:01:13 +0000277 // Apply the pointer typequals to the pointer object.
278 T = Context.getPointerType(T).getQualifiedType(DeclType.Ptr.TypeQuals);
279 break;
280 case DeclaratorChunk::Reference:
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000281 if (const ReferenceType *RT = T->getAsReferenceType()) {
Chris Lattnerbde71842008-02-21 01:32:57 +0000282 // C++ 8.3.2p4: There shall be no references to references.
Chris Lattner96b77fc2008-04-02 06:50:17 +0000283 Diag(DeclType.Loc, diag::err_illegal_decl_reference_to_reference,
Steve Naroff14f3f1b2008-01-14 23:33:18 +0000284 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroffe1223f72007-08-28 03:03:08 +0000285 D.setInvalidType(true);
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000286 T = RT->getPointeeType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000287 }
288
Chris Lattner96b77fc2008-04-02 06:50:17 +0000289 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
290 // object or incomplete types shall not be restrict-qualified."
291 if (DeclType.Ref.HasRestrict &&
Chris Lattnerd805bec2008-04-02 06:59:01 +0000292 !T->isIncompleteOrObjectType()) {
Chris Lattner96b77fc2008-04-02 06:50:17 +0000293 Diag(DeclType.Loc,
294 diag::err_typecheck_invalid_restrict_invalid_pointee,
295 T.getAsString());
296 DeclType.Ref.HasRestrict = false;
297 }
298
Reid Spencer5f016e22007-07-11 17:01:13 +0000299 T = Context.getReferenceType(T);
Chris Lattner96b77fc2008-04-02 06:50:17 +0000300
301 // Handle restrict on references.
302 if (DeclType.Ref.HasRestrict)
303 T.addRestrict();
Reid Spencer5f016e22007-07-11 17:01:13 +0000304 break;
305 case DeclaratorChunk::Array: {
Chris Lattnerfd89bc82008-04-02 01:05:10 +0000306 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner94f81fd2007-08-28 16:54:00 +0000307 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000308 ArrayType::ArraySizeModifier ASM;
309 if (ATI.isStar)
310 ASM = ArrayType::Star;
311 else if (ATI.hasStatic)
312 ASM = ArrayType::Static;
313 else
314 ASM = ArrayType::Normal;
Chris Lattner5265af52007-07-19 00:42:40 +0000315
Reid Spencer5f016e22007-07-11 17:01:13 +0000316 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
317 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
318 if (T->isIncompleteType()) {
319 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_incomplete_type,
320 T.getAsString());
Steve Naroffe1223f72007-08-28 03:03:08 +0000321 T = Context.IntTy;
322 D.setInvalidType(true);
Chris Lattner5265af52007-07-19 00:42:40 +0000323 } else if (T->isFunctionType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000324 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_functions,
Steve Naroff14f3f1b2008-01-14 23:33:18 +0000325 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroffe1223f72007-08-28 03:03:08 +0000326 T = Context.getPointerType(T);
327 D.setInvalidType(true);
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000328 } else if (const ReferenceType *RT = T->getAsReferenceType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000329 // C++ 8.3.2p4: There shall be no ... arrays of references ...
330 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_references,
Steve Naroff14f3f1b2008-01-14 23:33:18 +0000331 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000332 T = RT->getPointeeType();
Steve Naroffe1223f72007-08-28 03:03:08 +0000333 D.setInvalidType(true);
Chris Lattner02c642e2007-07-31 21:33:24 +0000334 } else if (const RecordType *EltTy = T->getAsRecordType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000335 // If the element type is a struct or union that contains a variadic
336 // array, reject it: C99 6.7.2.1p2.
337 if (EltTy->getDecl()->hasFlexibleArrayMember()) {
338 Diag(DeclType.Loc, diag::err_flexible_array_in_array,
339 T.getAsString());
Steve Naroffe1223f72007-08-28 03:03:08 +0000340 T = Context.IntTy;
341 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000342 }
343 }
Steve Naroff42471f82007-08-30 22:35:45 +0000344 // C99 6.7.5.2p1: The size expression shall have integer type.
345 if (ArraySize && !ArraySize->getType()->isIntegerType()) {
346 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int,
347 ArraySize->getType().getAsString(), ArraySize->getSourceRange());
348 D.setInvalidType(true);
Chris Lattnerfd89bc82008-04-02 01:05:10 +0000349 delete ArraySize;
350 ATI.NumElts = ArraySize = 0;
Steve Naroff42471f82007-08-30 22:35:45 +0000351 }
Steve Naroffc9406122007-08-30 18:10:14 +0000352 llvm::APSInt ConstVal(32);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000353 if (!ArraySize) {
354 T = Context.getIncompleteArrayType(T, ASM, ATI.TypeQuals);
Eli Friedman37148aa2008-05-14 00:40:18 +0000355 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
356 !T->isConstantSizeType()) {
357 // Per C99, a variable array is an array with either a non-constant
358 // size or an element type that has a non-constant-size
Steve Naroffc9406122007-08-30 18:10:14 +0000359 T = Context.getVariableArrayType(T, ArraySize, ASM, ATI.TypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +0000360 } else {
Steve Naroff42471f82007-08-30 22:35:45 +0000361 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
362 // have a value greater than zero.
363 if (ConstVal.isSigned()) {
364 if (ConstVal.isNegative()) {
365 Diag(ArraySize->getLocStart(),
366 diag::err_typecheck_negative_array_size,
367 ArraySize->getSourceRange());
368 D.setInvalidType(true);
369 } else if (ConstVal == 0) {
370 // GCC accepts zero sized static arrays.
371 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size,
372 ArraySize->getSourceRange());
373 }
374 }
Steve Naroffc9406122007-08-30 18:10:14 +0000375 T = Context.getConstantArrayType(T, ConstVal, ASM, ATI.TypeQuals);
Steve Naroff42471f82007-08-30 22:35:45 +0000376 }
Chris Lattner94f81fd2007-08-28 16:54:00 +0000377 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
378 if (!getLangOptions().C99 &&
379 (ASM != ArrayType::Normal ||
380 (ArraySize && !ArraySize->isIntegerConstantExpr(Context))))
381 Diag(D.getIdentifierLoc(), diag::ext_vla);
Reid Spencer5f016e22007-07-11 17:01:13 +0000382 break;
383 }
384 case DeclaratorChunk::Function:
385 // If the function declarator has a prototype (i.e. it is not () and
386 // does not have a K&R-style identifier list), then the arguments are part
387 // of the type, otherwise the argument list is ().
388 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Chris Lattner68cfd492007-12-19 18:01:43 +0000389
Chris Lattnercd881292007-12-19 05:31:29 +0000390 // C99 6.7.5.3p1: The return type may not be a function or array type.
Chris Lattner68cfd492007-12-19 18:01:43 +0000391 if (T->isArrayType() || T->isFunctionType()) {
Chris Lattnercd881292007-12-19 05:31:29 +0000392 Diag(DeclType.Loc, diag::err_func_returning_array_function,
393 T.getAsString());
394 T = Context.IntTy;
395 D.setInvalidType(true);
396 }
397
Reid Spencer5f016e22007-07-11 17:01:13 +0000398 if (!FTI.hasPrototype) {
399 // Simple void foo(), where the incoming T is the result type.
400 T = Context.getFunctionTypeNoProto(T);
401
402 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
403 if (FTI.NumArgs != 0)
404 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
405
406 } else {
407 // Otherwise, we have a function with an argument list that is
408 // potentially variadic.
409 llvm::SmallVector<QualType, 16> ArgTys;
410
411 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Chris Lattner8123a952008-04-10 02:22:51 +0000412 ParmVarDecl *Param = (ParmVarDecl *)FTI.ArgInfo[i].Param;
413 QualType ArgTy = Param->getType();
Chris Lattner78c75fb2007-07-21 05:30:18 +0000414 assert(!ArgTy.isNull() && "Couldn't parse type?");
Steve Naroff08d51392007-09-10 22:17:00 +0000415 //
416 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
417 // This matches the conversion that is done in
Nate Begemanbff5f5c2007-11-13 21:49:48 +0000418 // Sema::ActOnParamDeclarator(). Without this conversion, the
Steve Naroff08d51392007-09-10 22:17:00 +0000419 // argument type in the function prototype *will not* match the
420 // type in ParmVarDecl (which makes the code generator unhappy).
421 //
422 // FIXME: We still apparently need the conversion in
Chris Lattnere6327742008-04-02 05:18:44 +0000423 // Sema::ActOnParamDeclarator(). This doesn't make any sense, since
Steve Naroff08d51392007-09-10 22:17:00 +0000424 // it should be driving off the type being created here.
425 //
426 // FIXME: If a source translation tool needs to see the original type,
427 // then we need to consider storing both types somewhere...
428 //
Chris Lattnere6327742008-04-02 05:18:44 +0000429 if (ArgTy->isArrayType()) {
430 ArgTy = Context.getArrayDecayedType(ArgTy);
Chris Lattner529bd022008-01-02 22:50:48 +0000431 } else if (ArgTy->isFunctionType())
Steve Naroff08d51392007-09-10 22:17:00 +0000432 ArgTy = Context.getPointerType(ArgTy);
Chris Lattnere6327742008-04-02 05:18:44 +0000433
Reid Spencer5f016e22007-07-11 17:01:13 +0000434 // Look for 'void'. void is allowed only as a single argument to a
435 // function with no other parameters (C99 6.7.5.3p10). We record
436 // int(void) as a FunctionTypeProto with an empty argument list.
Steve Naroff08d51392007-09-10 22:17:00 +0000437 else if (ArgTy->isVoidType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000438 // If this is something like 'float(int, void)', reject it. 'void'
439 // is an incomplete type (C99 6.2.5p19) and function decls cannot
440 // have arguments of incomplete type.
441 if (FTI.NumArgs != 1 || FTI.isVariadic) {
442 Diag(DeclType.Loc, diag::err_void_only_param);
Chris Lattner2ff54262007-07-21 05:18:12 +0000443 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +0000444 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +0000445 } else if (FTI.ArgInfo[i].Ident) {
446 // Reject, but continue to parse 'int(void abc)'.
Reid Spencer5f016e22007-07-11 17:01:13 +0000447 Diag(FTI.ArgInfo[i].IdentLoc,
Chris Lattner4565d4e2007-07-21 05:26:43 +0000448 diag::err_param_with_void_type);
Chris Lattner2ff54262007-07-21 05:18:12 +0000449 ArgTy = Context.IntTy;
Chris Lattner8123a952008-04-10 02:22:51 +0000450 Param->setType(ArgTy);
Chris Lattner2ff54262007-07-21 05:18:12 +0000451 } else {
452 // Reject, but continue to parse 'float(const void)'.
Chris Lattnerf46699c2008-02-20 20:55:12 +0000453 if (ArgTy.getCVRQualifiers())
Chris Lattner2ff54262007-07-21 05:18:12 +0000454 Diag(DeclType.Loc, diag::err_void_param_qualified);
455
456 // Do not add 'void' to the ArgTys list.
457 break;
458 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000459 }
460
461 ArgTys.push_back(ArgTy);
462 }
463 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
464 FTI.isVariadic);
465 }
466 break;
467 }
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000468
469 // See if there are any attributes on this declarator chunk.
470 if (const AttributeList *AL = DeclType.getAttrs())
471 ProcessTypeAttributeList(T, AL);
Reid Spencer5f016e22007-07-11 17:01:13 +0000472 }
473
Chris Lattner0bf29ad2008-06-29 00:19:33 +0000474 // If there were any type attributes applied to the decl itself (not the
475 // type, apply the type attribute to the type!)
476 if (const AttributeList *Attrs = D.getAttributes())
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000477 ProcessTypeAttributeList(T, Attrs);
Chris Lattner0bf29ad2008-06-29 00:19:33 +0000478
Reid Spencer5f016e22007-07-11 17:01:13 +0000479 return T;
480}
481
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000482/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
Fariborz Jahanian360300c2007-11-09 22:27:59 +0000483/// declarator
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000484QualType Sema::ObjCGetTypeForMethodDefinition(DeclTy *D) {
485 ObjCMethodDecl *MDecl = dyn_cast<ObjCMethodDecl>(static_cast<Decl *>(D));
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000486 QualType T = MDecl->getResultType();
487 llvm::SmallVector<QualType, 16> ArgTys;
488
Fariborz Jahanian35600022007-11-09 17:18:29 +0000489 // Add the first two invisible argument types for self and _cmd.
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000490 if (MDecl->isInstance()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000491 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000492 selfTy = Context.getPointerType(selfTy);
493 ArgTys.push_back(selfTy);
494 }
Fariborz Jahanian35600022007-11-09 17:18:29 +0000495 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000496 ArgTys.push_back(Context.getObjCIdType());
497 ArgTys.push_back(Context.getObjCSelType());
Fariborz Jahanian35600022007-11-09 17:18:29 +0000498
Chris Lattner58cce3b2008-03-16 01:07:14 +0000499 for (int i = 0, e = MDecl->getNumParams(); i != e; ++i) {
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000500 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
501 QualType ArgTy = PDecl->getType();
502 assert(!ArgTy.isNull() && "Couldn't parse type?");
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000503 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
504 // This matches the conversion that is done in
Chris Lattnere6327742008-04-02 05:18:44 +0000505 // Sema::ActOnParamDeclarator().
506 if (ArgTy->isArrayType())
507 ArgTy = Context.getArrayDecayedType(ArgTy);
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000508 else if (ArgTy->isFunctionType())
509 ArgTy = Context.getPointerType(ArgTy);
510 ArgTys.push_back(ArgTy);
511 }
512 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Steve Naroffb99a4a32007-12-18 03:41:15 +0000513 MDecl->isVariadic());
Fariborz Jahanian306d68f2007-11-08 23:49:49 +0000514 return T;
515}
516
Steve Naroff08d92e42007-09-15 18:49:24 +0000517Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000518 // C99 6.7.6: Type names have no identifier. This is already validated by
519 // the parser.
520 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
521
522 QualType T = GetTypeForDeclarator(D, S);
Steve Naroff5912a352007-08-28 20:14:24 +0000523
524 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000525
Douglas Gregor6d6eb572008-05-07 04:49:29 +0000526 // Check that there are no default arguments (C++ only).
527 if (getLangOptions().CPlusPlus)
528 CheckExtraCXXDefaultArguments(D);
529
Steve Naroff5912a352007-08-28 20:14:24 +0000530 // In this context, we *do not* check D.getInvalidType(). If the declarator
531 // type was invalid, GetTypeForDeclarator() still returns a "valid" type,
532 // though it will not reflect the user specified type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000533 return T.getAsOpaquePtr();
534}
535
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000536
537
538//===----------------------------------------------------------------------===//
539// Type Attribute Processing
540//===----------------------------------------------------------------------===//
541
542/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
543/// specified type. The attribute contains 1 argument, the id of the address
544/// space for the type.
545static void HandleAddressSpaceTypeAttribute(QualType &Type,
546 const AttributeList &Attr, Sema &S){
547 // If this type is already address space qualified, reject it.
548 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
549 // for two or more different address spaces."
550 if (Type.getAddressSpace()) {
551 S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers);
552 return;
553 }
554
555 // Check the attribute arguments.
556 if (Attr.getNumArgs() != 1) {
557 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments,
558 std::string("1"));
559 return;
560 }
561 Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0));
562 llvm::APSInt addrSpace(32);
563 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) {
564 S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int,
565 ASArgExpr->getSourceRange());
566 return;
567 }
568
569 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
570 Type = S.Context.getASQualType(Type, ASIdx);
571}
572
573void Sema::ProcessTypeAttributeList(QualType &Result, const AttributeList *AL) {
Chris Lattner232e8822008-02-21 01:08:11 +0000574 // Scan through and apply attributes to this type where it makes sense. Some
575 // attributes (such as __address_space__, __vector_size__, etc) apply to the
576 // type, but others can be present in the type specifiers even though they
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000577 // apply to the decl. Here we apply type attributes and ignore the rest.
578 for (; AL; AL = AL->getNext()) {
Chris Lattner232e8822008-02-21 01:08:11 +0000579 // If this is an attribute we can handle, do so now, otherwise, add it to
580 // the LeftOverAttrs list for rechaining.
Chris Lattnerfca0ddd2008-06-26 06:27:57 +0000581 switch (AL->getKind()) {
Chris Lattner232e8822008-02-21 01:08:11 +0000582 default: break;
583 case AttributeList::AT_address_space:
Chris Lattnerc9b346d2008-06-29 00:50:08 +0000584 HandleAddressSpaceTypeAttribute(Result, *AL, *this);
585 break;
Chris Lattner232e8822008-02-21 01:08:11 +0000586 }
Chris Lattner232e8822008-02-21 01:08:11 +0000587 }
Chris Lattner232e8822008-02-21 01:08:11 +0000588}
589
Eli Friedman3c0eb162008-05-27 03:33:27 +0000590