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