blob: d66b41d8e349c99ad46e3d028838964e41374fa3 [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"
16#include "clang/AST/Decl.h"
Steve Naroff3fafa102007-10-01 19:00:59 +000017#include "clang/AST/DeclObjC.h"
Chris Lattner4b009652007-07-25 00:24:17 +000018#include "clang/Parse/DeclSpec.h"
Chris Lattner11f20f92007-08-28 16:40:32 +000019#include "clang/Basic/LangOptions.h"
Chris Lattner4b009652007-07-25 00:24:17 +000020using namespace clang;
21
22/// ConvertDeclSpecToType - Convert the specified declspec to the appropriate
23/// type object. This returns null on error.
Chris Lattner726c5452008-02-20 23:53:49 +000024QualType Sema::ConvertDeclSpecToType(DeclSpec &DS) {
Chris Lattner4b009652007-07-25 00:24:17 +000025 // FIXME: Should move the logic from DeclSpec::Finish to here for validity
26 // checking.
Chris Lattner06fb8672008-02-20 21:40:32 +000027 QualType Result;
Chris Lattner4b009652007-07-25 00:24:17 +000028
29 switch (DS.getTypeSpecType()) {
Chris Lattner6ab935b2008-04-05 06:32:51 +000030 default: assert(0 && "Unknown TypeSpecType!");
Chris Lattner5fcb38b2008-04-02 06:50:17 +000031 case DeclSpec::TST_void:
32 Result = Context.VoidTy;
33 break;
Chris Lattner4b009652007-07-25 00:24:17 +000034 case DeclSpec::TST_char:
35 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
Chris Lattner726c5452008-02-20 23:53:49 +000036 Result = Context.CharTy;
Chris Lattner4b009652007-07-25 00:24:17 +000037 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
Chris Lattner726c5452008-02-20 23:53:49 +000038 Result = Context.SignedCharTy;
Chris Lattner4b009652007-07-25 00:24:17 +000039 else {
40 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
41 "Unknown TSS value");
Chris Lattner726c5452008-02-20 23:53:49 +000042 Result = Context.UnsignedCharTy;
Chris Lattner4b009652007-07-25 00:24:17 +000043 }
Chris Lattner06fb8672008-02-20 21:40:32 +000044 break;
Chris Lattner6ab935b2008-04-05 06:32:51 +000045 case DeclSpec::TST_unspecified:
46 // Unspecified typespec defaults to int in C90. However, the C90 grammar
47 // [C90 6.5] only allows a decl-spec if there was *some* type-specifier,
48 // type-qualifier, or storage-class-specifier. If not, emit an extwarn.
49 // Note that the one exception to this is function definitions, which are
50 // allowed to be completely missing a declspec. This is handled in the
51 // parser already though by it pretending to have seen an 'int' in this
52 // case.
53 if (getLangOptions().ImplicitInt) {
54 if ((DS.getParsedSpecifiers() & (DeclSpec::PQ_StorageClassSpecifier |
55 DeclSpec::PQ_TypeSpecifier |
56 DeclSpec::PQ_TypeQualifier)) == 0)
57 Diag(DS.getSourceRange().getBegin(), diag::ext_missing_declspec);
58 } else {
59 // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says:
60 // "At least one type specifier shall be given in the declaration
61 // specifiers in each declaration, and in the specifier-qualifier list in
62 // each struct declaration and type name."
63 if (!DS.hasTypeSpecifier())
64 Diag(DS.getSourceRange().getBegin(), diag::ext_missing_type_specifier);
65 }
66
67 // FALL THROUGH.
Chris Lattner5328f312007-08-21 17:02:28 +000068 case DeclSpec::TST_int: {
Chris Lattner4b009652007-07-25 00:24:17 +000069 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
70 switch (DS.getTypeSpecWidth()) {
Chris Lattner726c5452008-02-20 23:53:49 +000071 case DeclSpec::TSW_unspecified: Result = Context.IntTy; break;
72 case DeclSpec::TSW_short: Result = Context.ShortTy; break;
73 case DeclSpec::TSW_long: Result = Context.LongTy; break;
74 case DeclSpec::TSW_longlong: Result = Context.LongLongTy; break;
Chris Lattner4b009652007-07-25 00:24:17 +000075 }
76 } else {
77 switch (DS.getTypeSpecWidth()) {
Chris Lattner726c5452008-02-20 23:53:49 +000078 case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break;
79 case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break;
80 case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break;
81 case DeclSpec::TSW_longlong: Result =Context.UnsignedLongLongTy; break;
Chris Lattner4b009652007-07-25 00:24:17 +000082 }
83 }
Chris Lattner06fb8672008-02-20 21:40:32 +000084 break;
Chris Lattner5328f312007-08-21 17:02:28 +000085 }
Chris Lattner726c5452008-02-20 23:53:49 +000086 case DeclSpec::TST_float: Result = Context.FloatTy; break;
Chris Lattner06fb8672008-02-20 21:40:32 +000087 case DeclSpec::TST_double:
88 if (DS.getTypeSpecWidth() == DeclSpec::TSW_long)
Chris Lattner726c5452008-02-20 23:53:49 +000089 Result = Context.LongDoubleTy;
Chris Lattner06fb8672008-02-20 21:40:32 +000090 else
Chris Lattner726c5452008-02-20 23:53:49 +000091 Result = Context.DoubleTy;
Chris Lattner06fb8672008-02-20 21:40:32 +000092 break;
Chris Lattner726c5452008-02-20 23:53:49 +000093 case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool
Chris Lattner4b009652007-07-25 00:24:17 +000094 case DeclSpec::TST_decimal32: // _Decimal32
95 case DeclSpec::TST_decimal64: // _Decimal64
96 case DeclSpec::TST_decimal128: // _Decimal128
97 assert(0 && "FIXME: GNU decimal extensions not supported yet!");
Chris Lattner2e78db32008-04-13 18:59:07 +000098 case DeclSpec::TST_class:
Chris Lattner4b009652007-07-25 00:24:17 +000099 case DeclSpec::TST_enum:
100 case DeclSpec::TST_union:
101 case DeclSpec::TST_struct: {
102 Decl *D = static_cast<Decl *>(DS.getTypeRep());
Chris Lattner2e78db32008-04-13 18:59:07 +0000103 assert(D && "Didn't get a decl for a class/enum/union/struct?");
Chris Lattner4b009652007-07-25 00:24:17 +0000104 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
105 DS.getTypeSpecSign() == 0 &&
106 "Can't handle qualifiers on typedef names yet!");
107 // TypeQuals handled by caller.
Douglas Gregor1d661552008-04-13 21:07:44 +0000108 Result = Context.getTypeDeclType(cast<TypeDecl>(D));
Chris Lattner06fb8672008-02-20 21:40:32 +0000109 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000110 }
111 case DeclSpec::TST_typedef: {
112 Decl *D = static_cast<Decl *>(DS.getTypeRep());
113 assert(D && "Didn't get a decl for a typedef?");
114 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
115 DS.getTypeSpecSign() == 0 &&
116 "Can't handle qualifiers on typedef names yet!");
Douglas Gregor1d661552008-04-13 21:07:44 +0000117
Steve Naroff81f1bba2007-09-06 21:24:23 +0000118 // FIXME: Adding a TST_objcInterface clause doesn't seem ideal, so
119 // we have this "hack" for now...
Ted Kremenek42730c52008-01-07 19:49:32 +0000120 if (ObjCInterfaceDecl *ObjCIntDecl = dyn_cast<ObjCInterfaceDecl>(D)) {
Chris Lattner06fb8672008-02-20 21:40:32 +0000121 if (DS.getProtocolQualifiers() == 0) {
Chris Lattner726c5452008-02-20 23:53:49 +0000122 Result = Context.getObjCInterfaceType(ObjCIntDecl);
Chris Lattner06fb8672008-02-20 21:40:32 +0000123 break;
124 }
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000125
126 Action::DeclTy **PPDecl = &(*DS.getProtocolQualifiers())[0];
Chris Lattner726c5452008-02-20 23:53:49 +0000127 Result = Context.getObjCQualifiedInterfaceType(ObjCIntDecl,
Chris Lattner06fb8672008-02-20 21:40:32 +0000128 reinterpret_cast<ObjCProtocolDecl**>(PPDecl),
Chris Lattner69f01932008-02-21 01:32:26 +0000129 DS.getNumProtocolQualifiers());
Chris Lattner06fb8672008-02-20 21:40:32 +0000130 break;
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000131 }
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000132 else if (TypedefDecl *typeDecl = dyn_cast<TypedefDecl>(D)) {
Chris Lattner726c5452008-02-20 23:53:49 +0000133 if (Context.getObjCIdType() == Context.getTypedefType(typeDecl)
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000134 && DS.getProtocolQualifiers()) {
135 // id<protocol-list>
136 Action::DeclTy **PPDecl = &(*DS.getProtocolQualifiers())[0];
Chris Lattner726c5452008-02-20 23:53:49 +0000137 Result = Context.getObjCQualifiedIdType(typeDecl->getUnderlyingType(),
Chris Lattner06fb8672008-02-20 21:40:32 +0000138 reinterpret_cast<ObjCProtocolDecl**>(PPDecl),
Chris Lattner69f01932008-02-21 01:32:26 +0000139 DS.getNumProtocolQualifiers());
Chris Lattner06fb8672008-02-20 21:40:32 +0000140 break;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000141 }
142 }
Chris Lattner4b009652007-07-25 00:24:17 +0000143 // TypeQuals handled by caller.
Douglas Gregor1d661552008-04-13 21:07:44 +0000144 Result = Context.getTypeDeclType(dyn_cast<TypeDecl>(D));
Chris Lattner06fb8672008-02-20 21:40:32 +0000145 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000146 }
Chris Lattner06fb8672008-02-20 21:40:32 +0000147 case DeclSpec::TST_typeofType:
148 Result = QualType::getFromOpaquePtr(DS.getTypeRep());
149 assert(!Result.isNull() && "Didn't get a type for typeof?");
Steve Naroff7cbb1462007-07-31 12:34:36 +0000150 // TypeQuals handled by caller.
Chris Lattner726c5452008-02-20 23:53:49 +0000151 Result = Context.getTypeOfType(Result);
Chris Lattner06fb8672008-02-20 21:40:32 +0000152 break;
Steve Naroff7cbb1462007-07-31 12:34:36 +0000153 case DeclSpec::TST_typeofExpr: {
154 Expr *E = static_cast<Expr *>(DS.getTypeRep());
155 assert(E && "Didn't get an expression for typeof?");
156 // TypeQuals handled by caller.
Chris Lattner726c5452008-02-20 23:53:49 +0000157 Result = Context.getTypeOfExpr(E);
Chris Lattner06fb8672008-02-20 21:40:32 +0000158 break;
Steve Naroff7cbb1462007-07-31 12:34:36 +0000159 }
Chris Lattner4b009652007-07-25 00:24:17 +0000160 }
Chris Lattner06fb8672008-02-20 21:40:32 +0000161
162 // Handle complex types.
163 if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex)
Chris Lattner726c5452008-02-20 23:53:49 +0000164 Result = Context.getComplexType(Result);
Chris Lattner06fb8672008-02-20 21:40:32 +0000165
166 assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary &&
167 "FIXME: imaginary types not supported yet!");
168
Chris Lattnerd496fb92008-02-20 22:04:11 +0000169 // See if there are any attributes on the declspec that apply to the type (as
170 // opposed to the decl).
Chris Lattner9e982502008-02-21 01:07:18 +0000171 if (AttributeList *AL = DS.getAttributes())
172 DS.SetAttributes(ProcessTypeAttributes(Result, AL));
173
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000174 // Apply const/volatile/restrict qualifiers to T.
175 if (unsigned TypeQuals = DS.getTypeQualifiers()) {
176
177 // Enforce C99 6.7.3p2: "Types other than pointer types derived from object
178 // or incomplete types shall not be restrict-qualified." C++ also allows
179 // restrict-qualified references.
180 if (TypeQuals & QualType::Restrict) {
Chris Lattnercfac88d2008-04-02 17:35:06 +0000181 if (const PointerLikeType *PT = Result->getAsPointerLikeType()) {
182 QualType EltTy = PT->getPointeeType();
183
184 // If we have a pointer or reference, the pointee must have an object or
185 // incomplete type.
186 if (!EltTy->isIncompleteOrObjectType()) {
187 Diag(DS.getRestrictSpecLoc(),
188 diag::err_typecheck_invalid_restrict_invalid_pointee,
189 EltTy.getAsString(), DS.getSourceRange());
190 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
191 }
192 } else {
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000193 Diag(DS.getRestrictSpecLoc(),
194 diag::err_typecheck_invalid_restrict_not_pointer,
195 Result.getAsString(), DS.getSourceRange());
Chris Lattnercfac88d2008-04-02 17:35:06 +0000196 TypeQuals &= ~QualType::Restrict; // Remove the restrict qualifier.
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000197 }
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000198 }
199
200 // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification
201 // of a function type includes any type qualifiers, the behavior is
202 // undefined."
203 if (Result->isFunctionType() && TypeQuals) {
204 // Get some location to point at, either the C or V location.
205 SourceLocation Loc;
206 if (TypeQuals & QualType::Const)
207 Loc = DS.getConstSpecLoc();
208 else {
209 assert((TypeQuals & QualType::Volatile) &&
210 "Has CV quals but not C or V?");
211 Loc = DS.getVolatileSpecLoc();
212 }
213 Diag(Loc, diag::warn_typecheck_function_qualifiers,
214 Result.getAsString(), DS.getSourceRange());
215 }
216
217 Result = Result.getQualifiedType(TypeQuals);
218 }
Chris Lattner9e982502008-02-21 01:07:18 +0000219 return Result;
220}
221
Chris Lattner4b009652007-07-25 00:24:17 +0000222/// GetTypeForDeclarator - Convert the type for the specified declarator to Type
223/// instances.
224QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S) {
Chris Lattner11f20f92007-08-28 16:40:32 +0000225 // long long is a C99 feature.
Chris Lattner1a7d9912007-08-28 16:41:29 +0000226 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Chris Lattner11f20f92007-08-28 16:40:32 +0000227 D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong)
228 Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong);
229
Chris Lattner726c5452008-02-20 23:53:49 +0000230 QualType T = ConvertDeclSpecToType(D.getDeclSpec());
Steve Naroff91b03f72007-08-28 03:03:08 +0000231
Chris Lattner4b009652007-07-25 00:24:17 +0000232 // Walk the DeclTypeInfo, building the recursive type as we go. DeclTypeInfos
233 // are ordered from the identifier out, which is opposite of what we want :).
234 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
Chris Lattner69f01932008-02-21 01:32:26 +0000235 DeclaratorChunk &DeclType = D.getTypeObject(e-i-1);
Chris Lattner4b009652007-07-25 00:24:17 +0000236 switch (DeclType.Kind) {
237 default: assert(0 && "Unknown decltype!");
238 case DeclaratorChunk::Pointer:
Chris Lattner36be3d82007-07-31 21:33:24 +0000239 if (T->isReferenceType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000240 // C++ 8.3.2p4: There shall be no ... pointers to references ...
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000241 Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference,
Steve Naroff73458bf2008-01-14 23:33:18 +0000242 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroff91b03f72007-08-28 03:03:08 +0000243 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000244 T = Context.IntTy;
245 }
246
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000247 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
248 // object or incomplete types shall not be restrict-qualified."
249 if ((DeclType.Ptr.TypeQuals & QualType::Restrict) &&
Chris Lattner9db553e2008-04-02 06:59:01 +0000250 !T->isIncompleteOrObjectType()) {
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000251 Diag(DeclType.Loc,
252 diag::err_typecheck_invalid_restrict_invalid_pointee,
253 T.getAsString());
254 DeclType.Ptr.TypeQuals &= QualType::Restrict;
255 }
256
Chris Lattner4b009652007-07-25 00:24:17 +0000257 // Apply the pointer typequals to the pointer object.
258 T = Context.getPointerType(T).getQualifiedType(DeclType.Ptr.TypeQuals);
Chris Lattner69f01932008-02-21 01:32:26 +0000259
260 // See if there are any attributes on the pointer that apply to it.
261 if (AttributeList *AL = DeclType.Ptr.AttrList)
262 DeclType.Ptr.AttrList = ProcessTypeAttributes(T, AL);
263
Chris Lattner4b009652007-07-25 00:24:17 +0000264 break;
265 case DeclaratorChunk::Reference:
Chris Lattnerf0c4a0a2007-07-31 16:56:34 +0000266 if (const ReferenceType *RT = T->getAsReferenceType()) {
Chris Lattner2e7d57f2008-02-21 01:32:57 +0000267 // C++ 8.3.2p4: There shall be no references to references.
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000268 Diag(DeclType.Loc, diag::err_illegal_decl_reference_to_reference,
Steve Naroff73458bf2008-01-14 23:33:18 +0000269 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroff91b03f72007-08-28 03:03:08 +0000270 D.setInvalidType(true);
Chris Lattnercfac88d2008-04-02 17:35:06 +0000271 T = RT->getPointeeType();
Chris Lattner4b009652007-07-25 00:24:17 +0000272 }
273
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000274 // Enforce C99 6.7.3p2: "Types other than pointer types derived from
275 // object or incomplete types shall not be restrict-qualified."
276 if (DeclType.Ref.HasRestrict &&
Chris Lattner9db553e2008-04-02 06:59:01 +0000277 !T->isIncompleteOrObjectType()) {
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000278 Diag(DeclType.Loc,
279 diag::err_typecheck_invalid_restrict_invalid_pointee,
280 T.getAsString());
281 DeclType.Ref.HasRestrict = false;
282 }
283
Chris Lattner4b009652007-07-25 00:24:17 +0000284 T = Context.getReferenceType(T);
Chris Lattner5fcb38b2008-04-02 06:50:17 +0000285
286 // Handle restrict on references.
287 if (DeclType.Ref.HasRestrict)
288 T.addRestrict();
Chris Lattner2e7d57f2008-02-21 01:32:57 +0000289
Chris Lattner69f01932008-02-21 01:32:26 +0000290 // See if there are any attributes on the pointer that apply to it.
291 if (AttributeList *AL = DeclType.Ref.AttrList)
292 DeclType.Ref.AttrList = ProcessTypeAttributes(T, AL);
Chris Lattner4b009652007-07-25 00:24:17 +0000293 break;
294 case DeclaratorChunk::Array: {
Chris Lattner67d3c8d2008-04-02 01:05:10 +0000295 DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
Chris Lattner47958f62007-08-28 16:54:00 +0000296 Expr *ArraySize = static_cast<Expr*>(ATI.NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000297 ArrayType::ArraySizeModifier ASM;
298 if (ATI.isStar)
299 ASM = ArrayType::Star;
300 else if (ATI.hasStatic)
301 ASM = ArrayType::Static;
302 else
303 ASM = ArrayType::Normal;
304
305 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
306 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
307 if (T->isIncompleteType()) {
308 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_incomplete_type,
309 T.getAsString());
Steve Naroff91b03f72007-08-28 03:03:08 +0000310 T = Context.IntTy;
311 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000312 } else if (T->isFunctionType()) {
313 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_functions,
Steve Naroff73458bf2008-01-14 23:33:18 +0000314 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Steve Naroff91b03f72007-08-28 03:03:08 +0000315 T = Context.getPointerType(T);
316 D.setInvalidType(true);
Chris Lattnerf0c4a0a2007-07-31 16:56:34 +0000317 } else if (const ReferenceType *RT = T->getAsReferenceType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000318 // C++ 8.3.2p4: There shall be no ... arrays of references ...
319 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_references,
Steve Naroff73458bf2008-01-14 23:33:18 +0000320 D.getIdentifier() ? D.getIdentifier()->getName() : "type name");
Chris Lattnercfac88d2008-04-02 17:35:06 +0000321 T = RT->getPointeeType();
Steve Naroff91b03f72007-08-28 03:03:08 +0000322 D.setInvalidType(true);
Chris Lattner36be3d82007-07-31 21:33:24 +0000323 } else if (const RecordType *EltTy = T->getAsRecordType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000324 // If the element type is a struct or union that contains a variadic
325 // array, reject it: C99 6.7.2.1p2.
326 if (EltTy->getDecl()->hasFlexibleArrayMember()) {
327 Diag(DeclType.Loc, diag::err_flexible_array_in_array,
328 T.getAsString());
Steve Naroff91b03f72007-08-28 03:03:08 +0000329 T = Context.IntTy;
330 D.setInvalidType(true);
Chris Lattner4b009652007-07-25 00:24:17 +0000331 }
332 }
Steve Naroff63b6a642007-08-30 22:35:45 +0000333 // C99 6.7.5.2p1: The size expression shall have integer type.
334 if (ArraySize && !ArraySize->getType()->isIntegerType()) {
335 Diag(ArraySize->getLocStart(), diag::err_array_size_non_int,
336 ArraySize->getType().getAsString(), ArraySize->getSourceRange());
337 D.setInvalidType(true);
Chris Lattner67d3c8d2008-04-02 01:05:10 +0000338 delete ArraySize;
339 ATI.NumElts = ArraySize = 0;
Steve Naroff63b6a642007-08-30 22:35:45 +0000340 }
Steve Naroff24c9b982007-08-30 18:10:14 +0000341 llvm::APSInt ConstVal(32);
Eli Friedman8ff07782008-02-15 18:16:39 +0000342 if (!ArraySize) {
343 T = Context.getIncompleteArrayType(T, ASM, ATI.TypeQuals);
Eli Friedman2ff28d12008-05-14 00:40:18 +0000344 } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) ||
345 !T->isConstantSizeType()) {
346 // Per C99, a variable array is an array with either a non-constant
347 // size or an element type that has a non-constant-size
Steve Naroff24c9b982007-08-30 18:10:14 +0000348 T = Context.getVariableArrayType(T, ArraySize, ASM, ATI.TypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000349 } else {
Steve Naroff63b6a642007-08-30 22:35:45 +0000350 // C99 6.7.5.2p1: If the expression is a constant expression, it shall
351 // have a value greater than zero.
352 if (ConstVal.isSigned()) {
353 if (ConstVal.isNegative()) {
354 Diag(ArraySize->getLocStart(),
355 diag::err_typecheck_negative_array_size,
356 ArraySize->getSourceRange());
357 D.setInvalidType(true);
358 } else if (ConstVal == 0) {
359 // GCC accepts zero sized static arrays.
360 Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size,
361 ArraySize->getSourceRange());
362 }
363 }
Steve Naroff24c9b982007-08-30 18:10:14 +0000364 T = Context.getConstantArrayType(T, ConstVal, ASM, ATI.TypeQuals);
Steve Naroff63b6a642007-08-30 22:35:45 +0000365 }
Chris Lattner47958f62007-08-28 16:54:00 +0000366 // If this is not C99, extwarn about VLA's and C99 array size modifiers.
367 if (!getLangOptions().C99 &&
368 (ASM != ArrayType::Normal ||
369 (ArraySize && !ArraySize->isIntegerConstantExpr(Context))))
370 Diag(D.getIdentifierLoc(), diag::ext_vla);
Chris Lattner4b009652007-07-25 00:24:17 +0000371 break;
372 }
373 case DeclaratorChunk::Function:
374 // If the function declarator has a prototype (i.e. it is not () and
375 // does not have a K&R-style identifier list), then the arguments are part
376 // of the type, otherwise the argument list is ().
377 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
Chris Lattnerbccfc152007-12-19 18:01:43 +0000378
Chris Lattner6ad7e882007-12-19 05:31:29 +0000379 // C99 6.7.5.3p1: The return type may not be a function or array type.
Chris Lattnerbccfc152007-12-19 18:01:43 +0000380 if (T->isArrayType() || T->isFunctionType()) {
Chris Lattner6ad7e882007-12-19 05:31:29 +0000381 Diag(DeclType.Loc, diag::err_func_returning_array_function,
382 T.getAsString());
383 T = Context.IntTy;
384 D.setInvalidType(true);
385 }
386
Chris Lattner4b009652007-07-25 00:24:17 +0000387 if (!FTI.hasPrototype) {
388 // Simple void foo(), where the incoming T is the result type.
389 T = Context.getFunctionTypeNoProto(T);
390
391 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
392 if (FTI.NumArgs != 0)
393 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
394
395 } else {
396 // Otherwise, we have a function with an argument list that is
397 // potentially variadic.
398 llvm::SmallVector<QualType, 16> ArgTys;
399
400 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
Chris Lattner97316c02008-04-10 02:22:51 +0000401 ParmVarDecl *Param = (ParmVarDecl *)FTI.ArgInfo[i].Param;
402 QualType ArgTy = Param->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000403 assert(!ArgTy.isNull() && "Couldn't parse type?");
Steve Naroff1830be72007-09-10 22:17:00 +0000404 //
405 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
406 // This matches the conversion that is done in
Nate Begeman2240f542007-11-13 21:49:48 +0000407 // Sema::ActOnParamDeclarator(). Without this conversion, the
Steve Naroff1830be72007-09-10 22:17:00 +0000408 // argument type in the function prototype *will not* match the
409 // type in ParmVarDecl (which makes the code generator unhappy).
410 //
411 // FIXME: We still apparently need the conversion in
Chris Lattner19eb97e2008-04-02 05:18:44 +0000412 // Sema::ActOnParamDeclarator(). This doesn't make any sense, since
Steve Naroff1830be72007-09-10 22:17:00 +0000413 // it should be driving off the type being created here.
414 //
415 // FIXME: If a source translation tool needs to see the original type,
416 // then we need to consider storing both types somewhere...
417 //
Chris Lattner19eb97e2008-04-02 05:18:44 +0000418 if (ArgTy->isArrayType()) {
419 ArgTy = Context.getArrayDecayedType(ArgTy);
Chris Lattnerc08564a2008-01-02 22:50:48 +0000420 } else if (ArgTy->isFunctionType())
Steve Naroff1830be72007-09-10 22:17:00 +0000421 ArgTy = Context.getPointerType(ArgTy);
Chris Lattner19eb97e2008-04-02 05:18:44 +0000422
Chris Lattner4b009652007-07-25 00:24:17 +0000423 // Look for 'void'. void is allowed only as a single argument to a
424 // function with no other parameters (C99 6.7.5.3p10). We record
425 // int(void) as a FunctionTypeProto with an empty argument list.
Steve Naroff1830be72007-09-10 22:17:00 +0000426 else if (ArgTy->isVoidType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000427 // If this is something like 'float(int, void)', reject it. 'void'
428 // is an incomplete type (C99 6.2.5p19) and function decls cannot
429 // have arguments of incomplete type.
430 if (FTI.NumArgs != 1 || FTI.isVariadic) {
431 Diag(DeclType.Loc, diag::err_void_only_param);
432 ArgTy = Context.IntTy;
Chris Lattner97316c02008-04-10 02:22:51 +0000433 Param->setType(ArgTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000434 } else if (FTI.ArgInfo[i].Ident) {
435 // Reject, but continue to parse 'int(void abc)'.
436 Diag(FTI.ArgInfo[i].IdentLoc,
437 diag::err_param_with_void_type);
438 ArgTy = Context.IntTy;
Chris Lattner97316c02008-04-10 02:22:51 +0000439 Param->setType(ArgTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000440 } else {
441 // Reject, but continue to parse 'float(const void)'.
Chris Lattner35fef522008-02-20 20:55:12 +0000442 if (ArgTy.getCVRQualifiers())
Chris Lattner4b009652007-07-25 00:24:17 +0000443 Diag(DeclType.Loc, diag::err_void_param_qualified);
444
445 // Do not add 'void' to the ArgTys list.
446 break;
447 }
448 }
449
450 ArgTys.push_back(ArgTy);
451 }
452 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
453 FTI.isVariadic);
454 }
455 break;
456 }
457 }
458
459 return T;
460}
461
Ted Kremenek42730c52008-01-07 19:49:32 +0000462/// ObjCGetTypeForMethodDefinition - Builds the type for a method definition
Fariborz Jahanianff746bc2007-11-09 22:27:59 +0000463/// declarator
Ted Kremenek42730c52008-01-07 19:49:32 +0000464QualType Sema::ObjCGetTypeForMethodDefinition(DeclTy *D) {
465 ObjCMethodDecl *MDecl = dyn_cast<ObjCMethodDecl>(static_cast<Decl *>(D));
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000466 QualType T = MDecl->getResultType();
467 llvm::SmallVector<QualType, 16> ArgTys;
468
Fariborz Jahanianea86cb82007-11-09 17:18:29 +0000469 // Add the first two invisible argument types for self and _cmd.
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000470 if (MDecl->isInstance()) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000471 QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface());
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000472 selfTy = Context.getPointerType(selfTy);
473 ArgTys.push_back(selfTy);
474 }
Fariborz Jahanianea86cb82007-11-09 17:18:29 +0000475 else
Ted Kremenek42730c52008-01-07 19:49:32 +0000476 ArgTys.push_back(Context.getObjCIdType());
477 ArgTys.push_back(Context.getObjCSelType());
Fariborz Jahanianea86cb82007-11-09 17:18:29 +0000478
Chris Lattner685d7922008-03-16 01:07:14 +0000479 for (int i = 0, e = MDecl->getNumParams(); i != e; ++i) {
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000480 ParmVarDecl *PDecl = MDecl->getParamDecl(i);
481 QualType ArgTy = PDecl->getType();
482 assert(!ArgTy.isNull() && "Couldn't parse type?");
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000483 // Perform the default function/array conversion (C99 6.7.5.3p[7,8]).
484 // This matches the conversion that is done in
Chris Lattner19eb97e2008-04-02 05:18:44 +0000485 // Sema::ActOnParamDeclarator().
486 if (ArgTy->isArrayType())
487 ArgTy = Context.getArrayDecayedType(ArgTy);
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000488 else if (ArgTy->isFunctionType())
489 ArgTy = Context.getPointerType(ArgTy);
490 ArgTys.push_back(ArgTy);
491 }
492 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
Steve Naroffc1a88c12007-12-18 03:41:15 +0000493 MDecl->isVariadic());
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +0000494 return T;
495}
496
Steve Naroff0acc9c92007-09-15 18:49:24 +0000497Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) {
Chris Lattner4b009652007-07-25 00:24:17 +0000498 // C99 6.7.6: Type names have no identifier. This is already validated by
499 // the parser.
500 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
501
502 QualType T = GetTypeForDeclarator(D, S);
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000503
504 assert(!T.isNull() && "GetTypeForDeclarator() returned null type");
Chris Lattner4b009652007-07-25 00:24:17 +0000505
Douglas Gregor2b9422f2008-05-07 04:49:29 +0000506 // Check that there are no default arguments (C++ only).
507 if (getLangOptions().CPlusPlus)
508 CheckExtraCXXDefaultArguments(D);
509
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000510 // In this context, we *do not* check D.getInvalidType(). If the declarator
511 // type was invalid, GetTypeForDeclarator() still returns a "valid" type,
512 // though it will not reflect the user specified type.
Chris Lattner4b009652007-07-25 00:24:17 +0000513 return T.getAsOpaquePtr();
514}
515
Chris Lattner1aaeeb92008-02-21 01:08:11 +0000516AttributeList *Sema::ProcessTypeAttributes(QualType &Result, AttributeList *AL){
517 // Scan through and apply attributes to this type where it makes sense. Some
518 // attributes (such as __address_space__, __vector_size__, etc) apply to the
519 // type, but others can be present in the type specifiers even though they
520 // apply to the decl. Here we apply and delete attributes that apply to the
521 // type and leave the others alone.
522 llvm::SmallVector<AttributeList *, 8> LeftOverAttrs;
523 while (AL) {
524 // Unlink this attribute from the chain, so we can process it independently.
525 AttributeList *ThisAttr = AL;
526 AL = AL->getNext();
527 ThisAttr->setNext(0);
528
529 // If this is an attribute we can handle, do so now, otherwise, add it to
530 // the LeftOverAttrs list for rechaining.
531 switch (ThisAttr->getKind()) {
532 default: break;
533 case AttributeList::AT_address_space:
534 Result = HandleAddressSpaceTypeAttribute(Result, ThisAttr);
535 delete ThisAttr; // Consume the attribute.
536 continue;
537 }
538
539 LeftOverAttrs.push_back(ThisAttr);
540 }
541
542 // Rechain any attributes that haven't been deleted to the DeclSpec.
543 AttributeList *List = 0;
544 for (unsigned i = 0, e = LeftOverAttrs.size(); i != e; ++i) {
545 LeftOverAttrs[i]->setNext(List);
546 List = LeftOverAttrs[i];
547 }
548
549 return List;
550}
551
552/// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the
553/// specified type.
554QualType Sema::HandleAddressSpaceTypeAttribute(QualType Type,
555 AttributeList *Attr) {
556 // If this type is already address space qualified, reject it.
557 // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers
558 // for two or more different address spaces."
559 if (Type.getAddressSpace()) {
560 Diag(Attr->getLoc(), diag::err_attribute_address_multiple_qualifiers);
561 return Type;
562 }
563
564 // Check the attribute arguments.
565 if (Attr->getNumArgs() != 1) {
566 Diag(Attr->getLoc(), diag::err_attribute_wrong_number_arguments,
567 std::string("1"));
568 return Type;
569 }
570 Expr *ASArgExpr = static_cast<Expr *>(Attr->getArg(0));
571 llvm::APSInt addrSpace(32);
572 if (!ASArgExpr->isIntegerConstantExpr(addrSpace, Context)) {
573 Diag(Attr->getLoc(), diag::err_attribute_address_space_not_int,
574 ASArgExpr->getSourceRange());
575 return Type;
576 }
577
578 unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue());
579 return Context.getASQualType(Type, ASIdx);
580}
581
Eli Friedman86ad5222008-05-27 03:33:27 +0000582/// HandleModeTypeAttribute - Process a mode attribute on the
583/// specified type.
584QualType Sema::HandleModeTypeAttribute(QualType Type,
585 AttributeList *Attr) {
586 // This attribute isn't documented, but glibc uses it. It changes
587 // the width of an int or unsigned int to the specified size.
588
589 // Check that there aren't any arguments
590 if (Attr->getNumArgs() != 0) {
591 Diag(Attr->getLoc(), diag::err_attribute_wrong_number_arguments,
592 std::string("0"));
593 return Type;
594 }
595
596 IdentifierInfo * Name = Attr->getParameterName();
597 if (!Name) {
598 Diag(Attr->getLoc(), diag::err_attribute_missing_parameter_name);
599 return Type;
600 }
601 const char *Str = Name->getName();
602 unsigned Len = Name->getLength();
603
604 // Normalize the attribute name, __foo__ becomes foo.
605 if (Len > 4 && Str[0] == '_' && Str[1] == '_' &&
606 Str[Len - 2] == '_' && Str[Len - 1] == '_') {
607 Str += 2;
608 Len -= 4;
609 }
610
611 unsigned DestWidth = 0;
612 bool IntegerMode = true;
613
614 switch (Len) {
615 case 2:
616 if (!memcmp(Str, "QI", 2)) { DestWidth = 8; break; }
617 if (!memcmp(Str, "HI", 2)) { DestWidth = 16; break; }
618 if (!memcmp(Str, "SI", 2)) { DestWidth = 32; break; }
619 if (!memcmp(Str, "DI", 2)) { DestWidth = 64; break; }
620 if (!memcmp(Str, "TI", 2)) { DestWidth = 128; break; }
621 if (!memcmp(Str, "SF", 2)) { DestWidth = 32; IntegerMode = false; break; }
622 if (!memcmp(Str, "DF", 2)) { DestWidth = 64; IntegerMode = false; break; }
623 if (!memcmp(Str, "XF", 2)) { DestWidth = 96; IntegerMode = false; break; }
624 if (!memcmp(Str, "TF", 2)) { DestWidth = 128; IntegerMode = false; break; }
625 break;
626 case 4:
627 if (!memcmp(Str, "word", 4)) {
628 // FIXME: glibc uses this to define register_t; this is
629 // narrover than a pointer on PIC16 and other embedded
630 // platforms
631 DestWidth = Context.getTypeSize(Context.VoidPtrTy);
632 break;
633 }
634 if (!memcmp(Str, "byte", 4)) {
635 DestWidth = Context.getTypeSize(Context.CharTy);
636 break;
637 }
638 break;
639 case 7:
640 if (!memcmp(Str, "pointer", 7)) {
641 DestWidth = Context.getTypeSize(Context.VoidPtrTy);
642 IntegerMode = true;
643 break;
644 }
645 break;
646 }
647
648 // FIXME: Need proper fixed-width types
649 QualType RetTy;
650 switch (DestWidth) {
651 case 0:
652 Diag(Attr->getLoc(), diag::err_unknown_machine_mode,
653 std::string(Str, Len));
654 return Type;
655 case 8:
656 assert(IntegerMode);
657 if (Type->isSignedIntegerType())
658 RetTy = Context.SignedCharTy;
659 else
660 RetTy = Context.UnsignedCharTy;
661 break;
662 case 16:
663 assert(IntegerMode);
664 if (Type->isSignedIntegerType())
665 RetTy = Context.ShortTy;
666 else
667 RetTy = Context.UnsignedShortTy;
668 break;
669 case 32:
670 if (!IntegerMode)
671 RetTy = Context.FloatTy;
672 else if (Type->isSignedIntegerType())
673 RetTy = Context.IntTy;
674 else
675 RetTy = Context.UnsignedIntTy;
676 break;
677 case 64:
678 if (!IntegerMode)
679 RetTy = Context.DoubleTy;
680 else if (Type->isSignedIntegerType())
681 RetTy = Context.LongLongTy;
682 else
683 RetTy = Context.UnsignedLongLongTy;
684 break;
685 default:
686 Diag(Attr->getLoc(), diag::err_unsupported_machine_mode,
687 std::string(Str, Len));
688 return Type;
689 }
690
691 if (!Type->getAsBuiltinType())
692 Diag(Attr->getLoc(), diag::err_mode_not_primitive);
693 else if (!(IntegerMode && Type->isIntegerType()) &&
694 !(!IntegerMode && Type->isFloatingType())) {
695 Diag(Attr->getLoc(), diag::err_mode_wrong_type);
696 }
697
698 return RetTy;
699}
700
701