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 | // |
Chris Lattner | 959e5be | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 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" |
Steve Naroff | 3fafa10 | 2007-10-01 19:00:59 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 18 | #include "clang/Parse/DeclSpec.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. |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame^] | 24 | QualType Sema::ConvertDeclSpecToType(DeclSpec &DS) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 25 | // FIXME: Should move the logic from DeclSpec::Finish to here for validity |
| 26 | // checking. |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 27 | QualType Result; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 28 | |
| 29 | switch (DS.getTypeSpecType()) { |
| 30 | default: return QualType(); // FIXME: Handle unimp cases! |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame^] | 31 | case DeclSpec::TST_void: return Context.VoidTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 32 | case DeclSpec::TST_char: |
| 33 | if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified) |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame^] | 34 | Result = Context.CharTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 35 | else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame^] | 36 | Result = Context.SignedCharTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 37 | else { |
| 38 | assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned && |
| 39 | "Unknown TSS value"); |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame^] | 40 | Result = Context.UnsignedCharTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 41 | } |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 42 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 43 | case DeclSpec::TST_unspecified: // Unspecific typespec defaults to int. |
Chris Lattner | 5328f31 | 2007-08-21 17:02:28 +0000 | [diff] [blame] | 44 | case DeclSpec::TST_int: { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 45 | if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) { |
| 46 | switch (DS.getTypeSpecWidth()) { |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame^] | 47 | case DeclSpec::TSW_unspecified: Result = Context.IntTy; break; |
| 48 | case DeclSpec::TSW_short: Result = Context.ShortTy; break; |
| 49 | case DeclSpec::TSW_long: Result = Context.LongTy; break; |
| 50 | case DeclSpec::TSW_longlong: Result = Context.LongLongTy; break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 51 | } |
| 52 | } else { |
| 53 | switch (DS.getTypeSpecWidth()) { |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame^] | 54 | case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break; |
| 55 | case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break; |
| 56 | case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break; |
| 57 | case DeclSpec::TSW_longlong: Result =Context.UnsignedLongLongTy; break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 58 | } |
| 59 | } |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 60 | break; |
Chris Lattner | 5328f31 | 2007-08-21 17:02:28 +0000 | [diff] [blame] | 61 | } |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame^] | 62 | case DeclSpec::TST_float: Result = Context.FloatTy; break; |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 63 | case DeclSpec::TST_double: |
| 64 | if (DS.getTypeSpecWidth() == DeclSpec::TSW_long) |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame^] | 65 | Result = Context.LongDoubleTy; |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 66 | else |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame^] | 67 | Result = Context.DoubleTy; |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 68 | break; |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame^] | 69 | case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 70 | case DeclSpec::TST_decimal32: // _Decimal32 |
| 71 | case DeclSpec::TST_decimal64: // _Decimal64 |
| 72 | case DeclSpec::TST_decimal128: // _Decimal128 |
| 73 | assert(0 && "FIXME: GNU decimal extensions not supported yet!"); |
| 74 | case DeclSpec::TST_enum: |
| 75 | case DeclSpec::TST_union: |
| 76 | case DeclSpec::TST_struct: { |
| 77 | Decl *D = static_cast<Decl *>(DS.getTypeRep()); |
| 78 | assert(D && "Didn't get a decl for a enum/union/struct?"); |
| 79 | assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 && |
| 80 | DS.getTypeSpecSign() == 0 && |
| 81 | "Can't handle qualifiers on typedef names yet!"); |
| 82 | // TypeQuals handled by caller. |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame^] | 83 | Result = Context.getTagDeclType(cast<TagDecl>(D)); |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 84 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 85 | } |
| 86 | case DeclSpec::TST_typedef: { |
| 87 | Decl *D = static_cast<Decl *>(DS.getTypeRep()); |
| 88 | assert(D && "Didn't get a decl for a typedef?"); |
| 89 | assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 && |
| 90 | DS.getTypeSpecSign() == 0 && |
| 91 | "Can't handle qualifiers on typedef names yet!"); |
Steve Naroff | 81f1bba | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 92 | // FIXME: Adding a TST_objcInterface clause doesn't seem ideal, so |
| 93 | // we have this "hack" for now... |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 94 | if (ObjCInterfaceDecl *ObjCIntDecl = dyn_cast<ObjCInterfaceDecl>(D)) { |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 95 | if (DS.getProtocolQualifiers() == 0) { |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame^] | 96 | Result = Context.getObjCInterfaceType(ObjCIntDecl); |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 97 | break; |
| 98 | } |
Fariborz Jahanian | 91193f6 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 99 | |
| 100 | Action::DeclTy **PPDecl = &(*DS.getProtocolQualifiers())[0]; |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame^] | 101 | Result = Context.getObjCQualifiedInterfaceType(ObjCIntDecl, |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 102 | reinterpret_cast<ObjCProtocolDecl**>(PPDecl), |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame^] | 103 | DS.NumProtocolQualifiers()); |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 104 | break; |
Fariborz Jahanian | 91193f6 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 105 | } |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 106 | else if (TypedefDecl *typeDecl = dyn_cast<TypedefDecl>(D)) { |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame^] | 107 | if (Context.getObjCIdType() == Context.getTypedefType(typeDecl) |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 108 | && DS.getProtocolQualifiers()) { |
| 109 | // id<protocol-list> |
| 110 | Action::DeclTy **PPDecl = &(*DS.getProtocolQualifiers())[0]; |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame^] | 111 | Result = Context.getObjCQualifiedIdType(typeDecl->getUnderlyingType(), |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 112 | reinterpret_cast<ObjCProtocolDecl**>(PPDecl), |
| 113 | DS.NumProtocolQualifiers()); |
| 114 | break; |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 115 | } |
| 116 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 117 | // TypeQuals handled by caller. |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame^] | 118 | Result = Context.getTypedefType(cast<TypedefDecl>(D)); |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 119 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 120 | } |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 121 | case DeclSpec::TST_typeofType: |
| 122 | Result = QualType::getFromOpaquePtr(DS.getTypeRep()); |
| 123 | assert(!Result.isNull() && "Didn't get a type for typeof?"); |
Steve Naroff | 7cbb146 | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 124 | // TypeQuals handled by caller. |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame^] | 125 | Result = Context.getTypeOfType(Result); |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 126 | break; |
Steve Naroff | 7cbb146 | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 127 | case DeclSpec::TST_typeofExpr: { |
| 128 | Expr *E = static_cast<Expr *>(DS.getTypeRep()); |
| 129 | assert(E && "Didn't get an expression for typeof?"); |
| 130 | // TypeQuals handled by caller. |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame^] | 131 | Result = Context.getTypeOfExpr(E); |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 132 | break; |
Steve Naroff | 7cbb146 | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 133 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 134 | } |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 135 | |
| 136 | // Handle complex types. |
| 137 | if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame^] | 138 | Result = Context.getComplexType(Result); |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 139 | |
| 140 | assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary && |
| 141 | "FIXME: imaginary types not supported yet!"); |
| 142 | |
Chris Lattner | d496fb9 | 2008-02-20 22:04:11 +0000 | [diff] [blame] | 143 | // See if there are any attributes on the declspec that apply to the type (as |
| 144 | // opposed to the decl). |
| 145 | if (!DS.getAttributes()) |
| 146 | return Result; |
| 147 | |
| 148 | // Scan through and apply attributes to this type where it makes sense. Some |
| 149 | // attributes (such as __address_space__, __vector_size__, etc) apply to the |
| 150 | // declspec, but others can be present in the decl spec even though they apply |
| 151 | // to the decl. Here we apply and delete attributes that apply to the |
| 152 | // declspec and leave the others alone. |
| 153 | llvm::SmallVector<AttributeList *, 8> LeftOverAttrs; |
| 154 | AttributeList *AL = DS.getAttributes(); |
| 155 | while (AL) { |
| 156 | AttributeList *ThisAttr = AL; |
| 157 | AL = AL->getNext(); |
| 158 | |
| 159 | LeftOverAttrs.push_back(ThisAttr); |
| 160 | } |
| 161 | |
| 162 | // Rechain any attributes that haven't been deleted to the DeclSpec. |
| 163 | AttributeList *List = 0; |
| 164 | for (unsigned i = 0, e = LeftOverAttrs.size(); i != e; ++i) { |
| 165 | LeftOverAttrs[i]->setNext(List); |
| 166 | List = LeftOverAttrs[i]; |
| 167 | } |
| 168 | |
| 169 | DS.clearAttributes(); |
| 170 | DS.AddAttributes(List); |
| 171 | //DS.setAttributes(List); |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 172 | return Result; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /// GetTypeForDeclarator - Convert the type for the specified declarator to Type |
| 176 | /// instances. |
| 177 | QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S) { |
Chris Lattner | 11f20f9 | 2007-08-28 16:40:32 +0000 | [diff] [blame] | 178 | // long long is a C99 feature. |
Chris Lattner | 1a7d991 | 2007-08-28 16:41:29 +0000 | [diff] [blame] | 179 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Chris Lattner | 11f20f9 | 2007-08-28 16:40:32 +0000 | [diff] [blame] | 180 | D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong) |
| 181 | Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong); |
| 182 | |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame^] | 183 | QualType T = ConvertDeclSpecToType(D.getDeclSpec()); |
Steve Naroff | 91b03f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 184 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 185 | // Apply const/volatile/restrict qualifiers to T. |
| 186 | T = T.getQualifiedType(D.getDeclSpec().getTypeQualifiers()); |
| 187 | |
| 188 | // Walk the DeclTypeInfo, building the recursive type as we go. DeclTypeInfos |
| 189 | // are ordered from the identifier out, which is opposite of what we want :). |
| 190 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { |
| 191 | const DeclaratorChunk &DeclType = D.getTypeObject(e-i-1); |
| 192 | switch (DeclType.Kind) { |
| 193 | default: assert(0 && "Unknown decltype!"); |
| 194 | case DeclaratorChunk::Pointer: |
Chris Lattner | 36be3d8 | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 195 | if (T->isReferenceType()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 196 | // C++ 8.3.2p4: There shall be no ... pointers to references ... |
| 197 | Diag(D.getIdentifierLoc(), diag::err_illegal_decl_pointer_to_reference, |
Steve Naroff | 73458bf | 2008-01-14 23:33:18 +0000 | [diff] [blame] | 198 | D.getIdentifier() ? D.getIdentifier()->getName() : "type name"); |
Steve Naroff | 91b03f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 199 | D.setInvalidType(true); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 200 | T = Context.IntTy; |
| 201 | } |
| 202 | |
| 203 | // Apply the pointer typequals to the pointer object. |
| 204 | T = Context.getPointerType(T).getQualifiedType(DeclType.Ptr.TypeQuals); |
| 205 | break; |
| 206 | case DeclaratorChunk::Reference: |
Chris Lattner | f0c4a0a | 2007-07-31 16:56:34 +0000 | [diff] [blame] | 207 | if (const ReferenceType *RT = T->getAsReferenceType()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 208 | // C++ 8.3.2p4: There shall be no references to references ... |
| 209 | Diag(D.getIdentifierLoc(), |
| 210 | diag::err_illegal_decl_reference_to_reference, |
Steve Naroff | 73458bf | 2008-01-14 23:33:18 +0000 | [diff] [blame] | 211 | D.getIdentifier() ? D.getIdentifier()->getName() : "type name"); |
Steve Naroff | 91b03f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 212 | D.setInvalidType(true); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 213 | T = RT->getReferenceeType(); |
| 214 | } |
| 215 | |
| 216 | T = Context.getReferenceType(T); |
| 217 | break; |
| 218 | case DeclaratorChunk::Array: { |
| 219 | const DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr; |
Chris Lattner | 47958f6 | 2007-08-28 16:54:00 +0000 | [diff] [blame] | 220 | Expr *ArraySize = static_cast<Expr*>(ATI.NumElts); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 221 | ArrayType::ArraySizeModifier ASM; |
| 222 | if (ATI.isStar) |
| 223 | ASM = ArrayType::Star; |
| 224 | else if (ATI.hasStatic) |
| 225 | ASM = ArrayType::Static; |
| 226 | else |
| 227 | ASM = ArrayType::Normal; |
| 228 | |
| 229 | // C99 6.7.5.2p1: If the element type is an incomplete or function type, |
| 230 | // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]()) |
| 231 | if (T->isIncompleteType()) { |
| 232 | Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_incomplete_type, |
| 233 | T.getAsString()); |
Steve Naroff | 91b03f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 234 | T = Context.IntTy; |
| 235 | D.setInvalidType(true); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 236 | } else if (T->isFunctionType()) { |
| 237 | Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_functions, |
Steve Naroff | 73458bf | 2008-01-14 23:33:18 +0000 | [diff] [blame] | 238 | D.getIdentifier() ? D.getIdentifier()->getName() : "type name"); |
Steve Naroff | 91b03f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 239 | T = Context.getPointerType(T); |
| 240 | D.setInvalidType(true); |
Chris Lattner | f0c4a0a | 2007-07-31 16:56:34 +0000 | [diff] [blame] | 241 | } else if (const ReferenceType *RT = T->getAsReferenceType()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 242 | // C++ 8.3.2p4: There shall be no ... arrays of references ... |
| 243 | Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_references, |
Steve Naroff | 73458bf | 2008-01-14 23:33:18 +0000 | [diff] [blame] | 244 | D.getIdentifier() ? D.getIdentifier()->getName() : "type name"); |
Steve Naroff | 91b03f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 245 | T = RT->getReferenceeType(); |
| 246 | D.setInvalidType(true); |
Chris Lattner | 36be3d8 | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 247 | } else if (const RecordType *EltTy = T->getAsRecordType()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 248 | // If the element type is a struct or union that contains a variadic |
| 249 | // array, reject it: C99 6.7.2.1p2. |
| 250 | if (EltTy->getDecl()->hasFlexibleArrayMember()) { |
| 251 | Diag(DeclType.Loc, diag::err_flexible_array_in_array, |
| 252 | T.getAsString()); |
Steve Naroff | 91b03f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 253 | T = Context.IntTy; |
| 254 | D.setInvalidType(true); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 255 | } |
| 256 | } |
Steve Naroff | 63b6a64 | 2007-08-30 22:35:45 +0000 | [diff] [blame] | 257 | // C99 6.7.5.2p1: The size expression shall have integer type. |
| 258 | if (ArraySize && !ArraySize->getType()->isIntegerType()) { |
| 259 | Diag(ArraySize->getLocStart(), diag::err_array_size_non_int, |
| 260 | ArraySize->getType().getAsString(), ArraySize->getSourceRange()); |
| 261 | D.setInvalidType(true); |
| 262 | } |
Steve Naroff | 24c9b98 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 263 | llvm::APSInt ConstVal(32); |
| 264 | // If no expression was provided, we consider it a VLA. |
Eli Friedman | 8ff0778 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 265 | if (!ArraySize) { |
| 266 | T = Context.getIncompleteArrayType(T, ASM, ATI.TypeQuals); |
| 267 | } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context)) { |
Steve Naroff | 24c9b98 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 268 | T = Context.getVariableArrayType(T, ArraySize, ASM, ATI.TypeQuals); |
Eli Friedman | 8ff0778 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 269 | } else { |
Steve Naroff | 63b6a64 | 2007-08-30 22:35:45 +0000 | [diff] [blame] | 270 | // C99 6.7.5.2p1: If the expression is a constant expression, it shall |
| 271 | // have a value greater than zero. |
| 272 | if (ConstVal.isSigned()) { |
| 273 | if (ConstVal.isNegative()) { |
| 274 | Diag(ArraySize->getLocStart(), |
| 275 | diag::err_typecheck_negative_array_size, |
| 276 | ArraySize->getSourceRange()); |
| 277 | D.setInvalidType(true); |
| 278 | } else if (ConstVal == 0) { |
| 279 | // GCC accepts zero sized static arrays. |
| 280 | Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size, |
| 281 | ArraySize->getSourceRange()); |
| 282 | } |
| 283 | } |
Steve Naroff | 24c9b98 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 284 | T = Context.getConstantArrayType(T, ConstVal, ASM, ATI.TypeQuals); |
Steve Naroff | 63b6a64 | 2007-08-30 22:35:45 +0000 | [diff] [blame] | 285 | } |
Chris Lattner | 47958f6 | 2007-08-28 16:54:00 +0000 | [diff] [blame] | 286 | // If this is not C99, extwarn about VLA's and C99 array size modifiers. |
| 287 | if (!getLangOptions().C99 && |
| 288 | (ASM != ArrayType::Normal || |
| 289 | (ArraySize && !ArraySize->isIntegerConstantExpr(Context)))) |
| 290 | Diag(D.getIdentifierLoc(), diag::ext_vla); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 291 | break; |
| 292 | } |
| 293 | case DeclaratorChunk::Function: |
| 294 | // If the function declarator has a prototype (i.e. it is not () and |
| 295 | // does not have a K&R-style identifier list), then the arguments are part |
| 296 | // of the type, otherwise the argument list is (). |
| 297 | const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun; |
Chris Lattner | bccfc15 | 2007-12-19 18:01:43 +0000 | [diff] [blame] | 298 | |
Chris Lattner | 6ad7e88 | 2007-12-19 05:31:29 +0000 | [diff] [blame] | 299 | // C99 6.7.5.3p1: The return type may not be a function or array type. |
Chris Lattner | bccfc15 | 2007-12-19 18:01:43 +0000 | [diff] [blame] | 300 | if (T->isArrayType() || T->isFunctionType()) { |
Chris Lattner | 6ad7e88 | 2007-12-19 05:31:29 +0000 | [diff] [blame] | 301 | Diag(DeclType.Loc, diag::err_func_returning_array_function, |
| 302 | T.getAsString()); |
| 303 | T = Context.IntTy; |
| 304 | D.setInvalidType(true); |
| 305 | } |
| 306 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 307 | if (!FTI.hasPrototype) { |
| 308 | // Simple void foo(), where the incoming T is the result type. |
| 309 | T = Context.getFunctionTypeNoProto(T); |
| 310 | |
| 311 | // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition. |
| 312 | if (FTI.NumArgs != 0) |
| 313 | Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration); |
| 314 | |
| 315 | } else { |
| 316 | // Otherwise, we have a function with an argument list that is |
| 317 | // potentially variadic. |
| 318 | llvm::SmallVector<QualType, 16> ArgTys; |
| 319 | |
| 320 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) { |
| 321 | QualType ArgTy = QualType::getFromOpaquePtr(FTI.ArgInfo[i].TypeInfo); |
| 322 | assert(!ArgTy.isNull() && "Couldn't parse type?"); |
Steve Naroff | 1830be7 | 2007-09-10 22:17:00 +0000 | [diff] [blame] | 323 | // |
| 324 | // Perform the default function/array conversion (C99 6.7.5.3p[7,8]). |
| 325 | // This matches the conversion that is done in |
Nate Begeman | 2240f54 | 2007-11-13 21:49:48 +0000 | [diff] [blame] | 326 | // Sema::ActOnParamDeclarator(). Without this conversion, the |
Steve Naroff | 1830be7 | 2007-09-10 22:17:00 +0000 | [diff] [blame] | 327 | // argument type in the function prototype *will not* match the |
| 328 | // type in ParmVarDecl (which makes the code generator unhappy). |
| 329 | // |
| 330 | // FIXME: We still apparently need the conversion in |
| 331 | // Sema::ParseParamDeclarator(). This doesn't make any sense, since |
| 332 | // it should be driving off the type being created here. |
| 333 | // |
| 334 | // FIXME: If a source translation tool needs to see the original type, |
| 335 | // then we need to consider storing both types somewhere... |
| 336 | // |
Chris Lattner | c08564a | 2008-01-02 22:50:48 +0000 | [diff] [blame] | 337 | if (const ArrayType *AT = ArgTy->getAsArrayType()) { |
| 338 | // int x[restrict 4] -> int *restrict |
Steve Naroff | 1830be7 | 2007-09-10 22:17:00 +0000 | [diff] [blame] | 339 | ArgTy = Context.getPointerType(AT->getElementType()); |
Chris Lattner | c08564a | 2008-01-02 22:50:48 +0000 | [diff] [blame] | 340 | ArgTy = ArgTy.getQualifiedType(AT->getIndexTypeQualifier()); |
| 341 | } else if (ArgTy->isFunctionType()) |
Steve Naroff | 1830be7 | 2007-09-10 22:17:00 +0000 | [diff] [blame] | 342 | ArgTy = Context.getPointerType(ArgTy); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 343 | // Look for 'void'. void is allowed only as a single argument to a |
| 344 | // function with no other parameters (C99 6.7.5.3p10). We record |
| 345 | // int(void) as a FunctionTypeProto with an empty argument list. |
Steve Naroff | 1830be7 | 2007-09-10 22:17:00 +0000 | [diff] [blame] | 346 | else if (ArgTy->isVoidType()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 347 | // If this is something like 'float(int, void)', reject it. 'void' |
| 348 | // is an incomplete type (C99 6.2.5p19) and function decls cannot |
| 349 | // have arguments of incomplete type. |
| 350 | if (FTI.NumArgs != 1 || FTI.isVariadic) { |
| 351 | Diag(DeclType.Loc, diag::err_void_only_param); |
| 352 | ArgTy = Context.IntTy; |
| 353 | FTI.ArgInfo[i].TypeInfo = ArgTy.getAsOpaquePtr(); |
| 354 | } else if (FTI.ArgInfo[i].Ident) { |
| 355 | // Reject, but continue to parse 'int(void abc)'. |
| 356 | Diag(FTI.ArgInfo[i].IdentLoc, |
| 357 | diag::err_param_with_void_type); |
| 358 | ArgTy = Context.IntTy; |
| 359 | FTI.ArgInfo[i].TypeInfo = ArgTy.getAsOpaquePtr(); |
| 360 | } else { |
| 361 | // Reject, but continue to parse 'float(const void)'. |
Chris Lattner | 35fef52 | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 362 | if (ArgTy.getCVRQualifiers()) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 363 | Diag(DeclType.Loc, diag::err_void_param_qualified); |
| 364 | |
| 365 | // Do not add 'void' to the ArgTys list. |
| 366 | break; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | ArgTys.push_back(ArgTy); |
| 371 | } |
| 372 | T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(), |
| 373 | FTI.isVariadic); |
| 374 | } |
| 375 | break; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | return T; |
| 380 | } |
| 381 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 382 | /// ObjCGetTypeForMethodDefinition - Builds the type for a method definition |
Fariborz Jahanian | ff746bc | 2007-11-09 22:27:59 +0000 | [diff] [blame] | 383 | /// declarator |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 384 | QualType Sema::ObjCGetTypeForMethodDefinition(DeclTy *D) { |
| 385 | ObjCMethodDecl *MDecl = dyn_cast<ObjCMethodDecl>(static_cast<Decl *>(D)); |
Fariborz Jahanian | dfb1c37 | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 386 | QualType T = MDecl->getResultType(); |
| 387 | llvm::SmallVector<QualType, 16> ArgTys; |
| 388 | |
Fariborz Jahanian | ea86cb8 | 2007-11-09 17:18:29 +0000 | [diff] [blame] | 389 | // Add the first two invisible argument types for self and _cmd. |
Fariborz Jahanian | 8473b22 | 2007-11-09 19:52:12 +0000 | [diff] [blame] | 390 | if (MDecl->isInstance()) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 391 | QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface()); |
Fariborz Jahanian | 8473b22 | 2007-11-09 19:52:12 +0000 | [diff] [blame] | 392 | selfTy = Context.getPointerType(selfTy); |
| 393 | ArgTys.push_back(selfTy); |
| 394 | } |
Fariborz Jahanian | ea86cb8 | 2007-11-09 17:18:29 +0000 | [diff] [blame] | 395 | else |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 396 | ArgTys.push_back(Context.getObjCIdType()); |
| 397 | ArgTys.push_back(Context.getObjCSelType()); |
Fariborz Jahanian | ea86cb8 | 2007-11-09 17:18:29 +0000 | [diff] [blame] | 398 | |
Fariborz Jahanian | dfb1c37 | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 399 | for (int i = 0; i < MDecl->getNumParams(); i++) { |
| 400 | ParmVarDecl *PDecl = MDecl->getParamDecl(i); |
| 401 | QualType ArgTy = PDecl->getType(); |
| 402 | assert(!ArgTy.isNull() && "Couldn't parse type?"); |
Fariborz Jahanian | dfb1c37 | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 403 | // Perform the default function/array conversion (C99 6.7.5.3p[7,8]). |
| 404 | // This matches the conversion that is done in |
Steve Naroff | 434fa8d | 2007-11-12 03:44:46 +0000 | [diff] [blame] | 405 | // Sema::ParseParamDeclarator(). |
Fariborz Jahanian | dfb1c37 | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 406 | if (const ArrayType *AT = ArgTy->getAsArrayType()) |
| 407 | ArgTy = Context.getPointerType(AT->getElementType()); |
| 408 | else if (ArgTy->isFunctionType()) |
| 409 | ArgTy = Context.getPointerType(ArgTy); |
| 410 | ArgTys.push_back(ArgTy); |
| 411 | } |
| 412 | T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(), |
Steve Naroff | c1a88c1 | 2007-12-18 03:41:15 +0000 | [diff] [blame] | 413 | MDecl->isVariadic()); |
Fariborz Jahanian | dfb1c37 | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 414 | return T; |
| 415 | } |
| 416 | |
Steve Naroff | 0acc9c9 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 417 | Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 418 | // C99 6.7.6: Type names have no identifier. This is already validated by |
| 419 | // the parser. |
| 420 | assert(D.getIdentifier() == 0 && "Type name should have no identifier!"); |
| 421 | |
| 422 | QualType T = GetTypeForDeclarator(D, S); |
Steve Naroff | d1ad6ae | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 423 | |
| 424 | assert(!T.isNull() && "GetTypeForDeclarator() returned null type"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 425 | |
Steve Naroff | d1ad6ae | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 426 | // In this context, we *do not* check D.getInvalidType(). If the declarator |
| 427 | // type was invalid, GetTypeForDeclarator() still returns a "valid" type, |
| 428 | // though it will not reflect the user specified type. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 429 | return T.getAsOpaquePtr(); |
| 430 | } |
| 431 | |
Steve Naroff | 91b03f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 432 | // Called from Parser::ParseParenDeclarator(). |
Steve Naroff | 0acc9c9 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 433 | Sema::TypeResult Sema::ActOnParamDeclaratorType(Scope *S, Declarator &D) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 434 | // Note: parameters have identifiers, but we don't care about them here, we |
| 435 | // just want the type converted. |
| 436 | QualType T = GetTypeForDeclarator(D, S); |
| 437 | |
Steve Naroff | d1ad6ae | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 438 | assert(!T.isNull() && "GetTypeForDeclarator() returned null type"); |
| 439 | |
| 440 | // In this context, we *do not* check D.getInvalidType(). If the declarator |
| 441 | // type was invalid, GetTypeForDeclarator() still returns a "valid" type, |
| 442 | // though it will not reflect the user specified type. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 443 | return T.getAsOpaquePtr(); |
| 444 | } |