blob: cf72e38e2f1b2b5eaf1c1023120db6ab0f7a0ae0 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- SemaType.cpp - Semantic Analysis for Types -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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"
17#include "clang/Parse/DeclSpec.h"
18#include "clang/Lex/IdentifierTable.h"
19using namespace clang;
20
21/// ConvertDeclSpecToType - Convert the specified declspec to the appropriate
22/// type object. This returns null on error.
23static QualType ConvertDeclSpecToType(const DeclSpec &DS, ASTContext &Ctx) {
24 // FIXME: Should move the logic from DeclSpec::Finish to here for validity
25 // checking.
26
27 switch (DS.getTypeSpecType()) {
28 default: return QualType(); // FIXME: Handle unimp cases!
29 case DeclSpec::TST_void: return Ctx.VoidTy;
30 case DeclSpec::TST_char:
31 if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified)
32 return Ctx.CharTy;
33 else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed)
34 return Ctx.SignedCharTy;
35 else {
36 assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned &&
37 "Unknown TSS value");
38 return Ctx.UnsignedCharTy;
39 }
40 case DeclSpec::TST_unspecified: // Unspecific typespec defaults to int.
41 case DeclSpec::TST_int:
42 if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) {
43 switch (DS.getTypeSpecWidth()) {
44 case DeclSpec::TSW_unspecified: return Ctx.IntTy;
45 case DeclSpec::TSW_short: return Ctx.ShortTy;
46 case DeclSpec::TSW_long: return Ctx.LongTy;
47 case DeclSpec::TSW_longlong: return Ctx.LongLongTy;
48 }
49 } else {
50 switch (DS.getTypeSpecWidth()) {
51 case DeclSpec::TSW_unspecified: return Ctx.UnsignedIntTy;
52 case DeclSpec::TSW_short: return Ctx.UnsignedShortTy;
53 case DeclSpec::TSW_long: return Ctx.UnsignedLongTy;
54 case DeclSpec::TSW_longlong: return Ctx.UnsignedLongLongTy;
55 }
56 }
57 case DeclSpec::TST_float:
58 if (DS.getTypeSpecComplex() == DeclSpec::TSC_unspecified)
59 return Ctx.FloatTy;
60 assert(DS.getTypeSpecComplex() == DeclSpec::TSC_complex &&
61 "FIXME: imaginary types not supported yet!");
62 return Ctx.FloatComplexTy;
63
64 case DeclSpec::TST_double: {
65 bool isLong = DS.getTypeSpecWidth() == DeclSpec::TSW_long;
66 if (DS.getTypeSpecComplex() == DeclSpec::TSC_unspecified)
67 return isLong ? Ctx.LongDoubleTy : Ctx.DoubleTy;
68 assert(DS.getTypeSpecComplex() == DeclSpec::TSC_complex &&
69 "FIXME: imaginary types not supported yet!");
70 return isLong ? Ctx.LongDoubleComplexTy : Ctx.DoubleComplexTy;
71 }
72 case DeclSpec::TST_bool: // _Bool or bool
73 return Ctx.BoolTy;
74 case DeclSpec::TST_decimal32: // _Decimal32
75 case DeclSpec::TST_decimal64: // _Decimal64
76 case DeclSpec::TST_decimal128: // _Decimal128
77 assert(0 && "FIXME: GNU decimal extensions not supported yet!");
78 case DeclSpec::TST_enum:
79 case DeclSpec::TST_union:
80 case DeclSpec::TST_struct: {
81 Decl *D = static_cast<Decl *>(DS.getTypeRep());
82 assert(D && "Didn't get a decl for a enum/union/struct?");
83 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
84 DS.getTypeSpecSign() == 0 &&
85 "Can't handle qualifiers on typedef names yet!");
86 // TypeQuals handled by caller.
87 return Ctx.getTagDeclType(cast<TagDecl>(D));
88 }
89 case DeclSpec::TST_typedef: {
90 Decl *D = static_cast<Decl *>(DS.getTypeRep());
91 assert(D && "Didn't get a decl for a typedef?");
92 assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 &&
93 DS.getTypeSpecSign() == 0 &&
94 "Can't handle qualifiers on typedef names yet!");
95 // TypeQuals handled by caller.
96 return Ctx.getTypedefType(cast<TypedefDecl>(D));
97 }
98 }
99}
100
101/// GetTypeForDeclarator - Convert the type for the specified declarator to Type
102/// instances.
103QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S) {
104 QualType T = ConvertDeclSpecToType(D.getDeclSpec(), Context);
105
106 // Apply const/volatile/restrict qualifiers to T.
107 T = T.getQualifiedType(D.getDeclSpec().getTypeQualifiers());
108
109 // Walk the DeclTypeInfo, building the recursive type as we go. DeclTypeInfos
110 // are ordered from the identifier out, which is opposite of what we want :).
111 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
112 const DeclaratorChunk &DeclType = D.getTypeObject(e-i-1);
113 switch (DeclType.Kind) {
114 default: assert(0 && "Unknown decltype!");
115 case DeclaratorChunk::Pointer:
116 if (isa<ReferenceType>(T.getCanonicalType().getTypePtr())) {
117 // C++ 8.3.2p4: There shall be no ... pointers to references ...
118 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_pointer_to_reference,
119 D.getIdentifier()->getName());
120 T = Context.IntTy;
121 }
122
123 // Apply the pointer typequals to the pointer object.
124 T = Context.getPointerType(T).getQualifiedType(DeclType.Ptr.TypeQuals);
125 break;
126 case DeclaratorChunk::Reference:
127 if (const ReferenceType *RT = T->isReferenceType()) {
128 // C++ 8.3.2p4: There shall be no references to references ...
129 Diag(D.getIdentifierLoc(),
130 diag::err_illegal_decl_reference_to_reference,
131 D.getIdentifier()->getName());
132 T = RT->getReferenceeType();
133 }
134
135 T = Context.getReferenceType(T);
136 break;
137 case DeclaratorChunk::Array: {
138 const DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr;
139 ArrayType::ArraySizeModifier ASM;
140 if (ATI.isStar)
141 ASM = ArrayType::Star;
142 else if (ATI.hasStatic)
143 ASM = ArrayType::Static;
144 else
145 ASM = ArrayType::Normal;
146
147 // C99 6.7.5.2p1: If the element type is an incomplete or function type,
148 // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]())
149 if (T->isIncompleteType()) {
150 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_incomplete_type,
151 T.getAsString());
152 T = Context.IntTy;
153 } else if (T->isFunctionType()) {
154 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_functions,
155 D.getIdentifier()->getName());
156 T = Context.getPointerType(T);
157 } else if (const ReferenceType *RT = T->isReferenceType()) {
158 // C++ 8.3.2p4: There shall be no ... arrays of references ...
159 Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_references,
160 D.getIdentifier()->getName());
161 T = RT->getReferenceeType();
162 } else if (RecordType *EltTy =dyn_cast<RecordType>(T.getCanonicalType())){
163 // If the element type is a struct or union that contains a variadic
164 // array, reject it: C99 6.7.2.1p2.
165 if (EltTy->getDecl()->hasFlexibleArrayMember()) {
166 Diag(DeclType.Loc, diag::err_flexible_array_in_array,
167 T.getAsString());
168 T = Context.IntTy;
169 }
170 }
171 T = Context.getArrayType(T, ASM, ATI.TypeQuals,
172 static_cast<Expr *>(ATI.NumElts));
173 break;
174 }
175 case DeclaratorChunk::Function:
176 // If the function declarator has a prototype (i.e. it is not () and
177 // does not have a K&R-style identifier list), then the arguments are part
178 // of the type, otherwise the argument list is ().
179 const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun;
180 if (!FTI.hasPrototype) {
181 // Simple void foo(), where the incoming T is the result type.
182 T = Context.getFunctionTypeNoProto(T);
183
184 // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
185 if (FTI.NumArgs != 0)
186 Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
187
188 } else {
189 // Otherwise, we have a function with an argument list that is
190 // potentially variadic.
191 llvm::SmallVector<QualType, 16> ArgTys;
192
193 for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) {
194 QualType ArgTy = QualType::getFromOpaquePtr(FTI.ArgInfo[i].TypeInfo);
195 assert(!ArgTy.isNull() && "Couldn't parse type?");
196
197 // Look for 'void'. void is allowed only as a single argument to a
198 // function with no other parameters (C99 6.7.5.3p10). We record
199 // int(void) as a FunctionTypeProto with an empty argument list.
200 if (ArgTy->isVoidType()) {
201 // If this is something like 'float(int, void)', reject it. 'void'
202 // is an incomplete type (C99 6.2.5p19) and function decls cannot
203 // have arguments of incomplete type.
204 if (FTI.NumArgs != 1 || FTI.isVariadic) {
205 Diag(DeclType.Loc, diag::err_void_only_param);
206 ArgTy = Context.IntTy;
207 FTI.ArgInfo[i].TypeInfo = ArgTy.getAsOpaquePtr();
208 } else if (FTI.ArgInfo[i].Ident) {
209 // Reject, but continue to parse 'int(void abc)'.
210 Diag(FTI.ArgInfo[i].IdentLoc,
211 diag::err_param_with_void_type);
212 ArgTy = Context.IntTy;
213 FTI.ArgInfo[i].TypeInfo = ArgTy.getAsOpaquePtr();
214 } else {
215 // Reject, but continue to parse 'float(const void)'.
216 if (ArgTy.getQualifiers())
217 Diag(DeclType.Loc, diag::err_void_param_qualified);
218
219 // Do not add 'void' to the ArgTys list.
220 break;
221 }
222 }
223
224 ArgTys.push_back(ArgTy);
225 }
226 T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(),
227 FTI.isVariadic);
228 }
229 break;
230 }
231 }
232
233 return T;
234}
235
236Sema::TypeResult Sema::ParseTypeName(Scope *S, Declarator &D) {
237 // C99 6.7.6: Type names have no identifier. This is already validated by
238 // the parser.
239 assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
240
241 QualType T = GetTypeForDeclarator(D, S);
242
243 // If the type of the declarator was invalid, this is an invalid typename.
244 if (T.isNull())
245 return true;
246
247 return T.getAsOpaquePtr();
248}
249
250Sema::TypeResult Sema::ParseParamDeclaratorType(Scope *S, Declarator &D) {
251 // Note: parameters have identifiers, but we don't care about them here, we
252 // just want the type converted.
253 QualType T = GetTypeForDeclarator(D, S);
254
255 // If the type of the declarator was invalid, this is an invalid typename.
256 if (T.isNull())
257 return true;
258
259 return T.getAsOpaquePtr();
260}