Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- SemaType.cpp - Semantic Analysis for Types -----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 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. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +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 | 980e508 | 2007-10-01 19:00:59 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 18 | #include "clang/Parse/DeclSpec.h" |
Chris Lattner | b23deda | 2007-08-28 16:40:32 +0000 | [diff] [blame] | 19 | #include "clang/Basic/LangOptions.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +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 | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 24 | QualType Sema::ConvertDeclSpecToType(DeclSpec &DS) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 25 | // FIXME: Should move the logic from DeclSpec::Finish to here for validity |
| 26 | // checking. |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 27 | QualType Result; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 28 | |
| 29 | switch (DS.getTypeSpecType()) { |
| 30 | default: return QualType(); // FIXME: Handle unimp cases! |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 31 | case DeclSpec::TST_void: return Context.VoidTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 32 | case DeclSpec::TST_char: |
| 33 | if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified) |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 34 | Result = Context.CharTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 35 | else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 36 | Result = Context.SignedCharTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 37 | else { |
| 38 | assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned && |
| 39 | "Unknown TSS value"); |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 40 | Result = Context.UnsignedCharTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 41 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 42 | break; |
Chris Lattner | 7a543ad | 2007-07-13 21:02:29 +0000 | [diff] [blame] | 43 | case DeclSpec::TST_unspecified: // Unspecific typespec defaults to int. |
Chris Lattner | 3cbc38b | 2007-08-21 17:02:28 +0000 | [diff] [blame] | 44 | case DeclSpec::TST_int: { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 45 | if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) { |
| 46 | switch (DS.getTypeSpecWidth()) { |
Chris Lattner | fab5b45 | 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; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 51 | } |
| 52 | } else { |
| 53 | switch (DS.getTypeSpecWidth()) { |
Chris Lattner | fab5b45 | 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; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 58 | } |
| 59 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 60 | break; |
Chris Lattner | 3cbc38b | 2007-08-21 17:02:28 +0000 | [diff] [blame] | 61 | } |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 62 | case DeclSpec::TST_float: Result = Context.FloatTy; break; |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 63 | case DeclSpec::TST_double: |
| 64 | if (DS.getTypeSpecWidth() == DeclSpec::TSW_long) |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 65 | Result = Context.LongDoubleTy; |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 66 | else |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 67 | Result = Context.DoubleTy; |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 68 | break; |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 69 | case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +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 | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 83 | Result = Context.getTagDeclType(cast<TagDecl>(D)); |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 84 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +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 | 3536b44 | 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 | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 94 | if (ObjCInterfaceDecl *ObjCIntDecl = dyn_cast<ObjCInterfaceDecl>(D)) { |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 95 | if (DS.getProtocolQualifiers() == 0) { |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 96 | Result = Context.getObjCInterfaceType(ObjCIntDecl); |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 97 | break; |
| 98 | } |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 99 | |
| 100 | Action::DeclTy **PPDecl = &(*DS.getProtocolQualifiers())[0]; |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 101 | Result = Context.getObjCQualifiedInterfaceType(ObjCIntDecl, |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 102 | reinterpret_cast<ObjCProtocolDecl**>(PPDecl), |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 103 | DS.getNumProtocolQualifiers()); |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 104 | break; |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 105 | } |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 106 | else if (TypedefDecl *typeDecl = dyn_cast<TypedefDecl>(D)) { |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 107 | if (Context.getObjCIdType() == Context.getTypedefType(typeDecl) |
Fariborz Jahanian | c569249 | 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 | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 111 | Result = Context.getObjCQualifiedIdType(typeDecl->getUnderlyingType(), |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 112 | reinterpret_cast<ObjCProtocolDecl**>(PPDecl), |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 113 | DS.getNumProtocolQualifiers()); |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 114 | break; |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 115 | } |
| 116 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 117 | // TypeQuals handled by caller. |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 118 | Result = Context.getTypedefType(cast<TypedefDecl>(D)); |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 119 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 120 | } |
Chris Lattner | 958858e | 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 | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 124 | // TypeQuals handled by caller. |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 125 | Result = Context.getTypeOfType(Result); |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 126 | break; |
Steve Naroff | d1861fd | 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 | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 131 | Result = Context.getTypeOfExpr(E); |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 132 | break; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 133 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 134 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 135 | |
| 136 | // Handle complex types. |
| 137 | if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 138 | Result = Context.getComplexType(Result); |
Chris Lattner | 958858e | 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 | 38d8b98 | 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). |
Chris Lattner | f1d705c | 2008-02-21 01:07:18 +0000 | [diff] [blame] | 145 | if (AttributeList *AL = DS.getAttributes()) |
| 146 | DS.SetAttributes(ProcessTypeAttributes(Result, AL)); |
| 147 | |
| 148 | return Result; |
| 149 | } |
| 150 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 151 | /// GetTypeForDeclarator - Convert the type for the specified declarator to Type |
| 152 | /// instances. |
| 153 | QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S) { |
Chris Lattner | b23deda | 2007-08-28 16:40:32 +0000 | [diff] [blame] | 154 | // long long is a C99 feature. |
Chris Lattner | d1eb332 | 2007-08-28 16:41:29 +0000 | [diff] [blame] | 155 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Chris Lattner | b23deda | 2007-08-28 16:40:32 +0000 | [diff] [blame] | 156 | D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong) |
| 157 | Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong); |
| 158 | |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 159 | QualType T = ConvertDeclSpecToType(D.getDeclSpec()); |
Steve Naroff | e1223f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 160 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 161 | // Apply const/volatile/restrict qualifiers to T. |
| 162 | T = T.getQualifiedType(D.getDeclSpec().getTypeQualifiers()); |
| 163 | |
| 164 | // Walk the DeclTypeInfo, building the recursive type as we go. DeclTypeInfos |
| 165 | // are ordered from the identifier out, which is opposite of what we want :). |
| 166 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 167 | DeclaratorChunk &DeclType = D.getTypeObject(e-i-1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 168 | switch (DeclType.Kind) { |
| 169 | default: assert(0 && "Unknown decltype!"); |
| 170 | case DeclaratorChunk::Pointer: |
Chris Lattner | 02c642e | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 171 | if (T->isReferenceType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 172 | // C++ 8.3.2p4: There shall be no ... pointers to references ... |
| 173 | Diag(D.getIdentifierLoc(), diag::err_illegal_decl_pointer_to_reference, |
Steve Naroff | 14f3f1b | 2008-01-14 23:33:18 +0000 | [diff] [blame] | 174 | D.getIdentifier() ? D.getIdentifier()->getName() : "type name"); |
Steve Naroff | e1223f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 175 | D.setInvalidType(true); |
Chris Lattner | 5265af5 | 2007-07-19 00:42:40 +0000 | [diff] [blame] | 176 | T = Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | // Apply the pointer typequals to the pointer object. |
| 180 | T = Context.getPointerType(T).getQualifiedType(DeclType.Ptr.TypeQuals); |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 181 | |
| 182 | // See if there are any attributes on the pointer that apply to it. |
| 183 | if (AttributeList *AL = DeclType.Ptr.AttrList) |
| 184 | DeclType.Ptr.AttrList = ProcessTypeAttributes(T, AL); |
| 185 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 186 | break; |
| 187 | case DeclaratorChunk::Reference: |
Chris Lattner | a1d9fde | 2007-07-31 16:56:34 +0000 | [diff] [blame] | 188 | if (const ReferenceType *RT = T->getAsReferenceType()) { |
Chris Lattner | bde7184 | 2008-02-21 01:32:57 +0000 | [diff] [blame^] | 189 | // C++ 8.3.2p4: There shall be no references to references. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 190 | Diag(D.getIdentifierLoc(), |
| 191 | diag::err_illegal_decl_reference_to_reference, |
Steve Naroff | 14f3f1b | 2008-01-14 23:33:18 +0000 | [diff] [blame] | 192 | D.getIdentifier() ? D.getIdentifier()->getName() : "type name"); |
Steve Naroff | e1223f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 193 | D.setInvalidType(true); |
Chris Lattner | 5265af5 | 2007-07-19 00:42:40 +0000 | [diff] [blame] | 194 | T = RT->getReferenceeType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | T = Context.getReferenceType(T); |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 198 | |
Chris Lattner | bde7184 | 2008-02-21 01:32:57 +0000 | [diff] [blame^] | 199 | // FIXME: Handle Ref.Restrict! |
| 200 | |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 201 | // See if there are any attributes on the pointer that apply to it. |
| 202 | if (AttributeList *AL = DeclType.Ref.AttrList) |
| 203 | DeclType.Ref.AttrList = ProcessTypeAttributes(T, AL); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 204 | break; |
| 205 | case DeclaratorChunk::Array: { |
| 206 | const DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr; |
Chris Lattner | 94f81fd | 2007-08-28 16:54:00 +0000 | [diff] [blame] | 207 | Expr *ArraySize = static_cast<Expr*>(ATI.NumElts); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 208 | ArrayType::ArraySizeModifier ASM; |
| 209 | if (ATI.isStar) |
| 210 | ASM = ArrayType::Star; |
| 211 | else if (ATI.hasStatic) |
| 212 | ASM = ArrayType::Static; |
| 213 | else |
| 214 | ASM = ArrayType::Normal; |
Chris Lattner | 5265af5 | 2007-07-19 00:42:40 +0000 | [diff] [blame] | 215 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 216 | // C99 6.7.5.2p1: If the element type is an incomplete or function type, |
| 217 | // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]()) |
| 218 | if (T->isIncompleteType()) { |
| 219 | Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_incomplete_type, |
| 220 | T.getAsString()); |
Steve Naroff | e1223f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 221 | T = Context.IntTy; |
| 222 | D.setInvalidType(true); |
Chris Lattner | 5265af5 | 2007-07-19 00:42:40 +0000 | [diff] [blame] | 223 | } else if (T->isFunctionType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 224 | Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_functions, |
Steve Naroff | 14f3f1b | 2008-01-14 23:33:18 +0000 | [diff] [blame] | 225 | D.getIdentifier() ? D.getIdentifier()->getName() : "type name"); |
Steve Naroff | e1223f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 226 | T = Context.getPointerType(T); |
| 227 | D.setInvalidType(true); |
Chris Lattner | a1d9fde | 2007-07-31 16:56:34 +0000 | [diff] [blame] | 228 | } else if (const ReferenceType *RT = T->getAsReferenceType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 229 | // C++ 8.3.2p4: There shall be no ... arrays of references ... |
| 230 | Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_references, |
Steve Naroff | 14f3f1b | 2008-01-14 23:33:18 +0000 | [diff] [blame] | 231 | D.getIdentifier() ? D.getIdentifier()->getName() : "type name"); |
Steve Naroff | e1223f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 232 | T = RT->getReferenceeType(); |
| 233 | D.setInvalidType(true); |
Chris Lattner | 02c642e | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 234 | } else if (const RecordType *EltTy = T->getAsRecordType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 235 | // If the element type is a struct or union that contains a variadic |
| 236 | // array, reject it: C99 6.7.2.1p2. |
| 237 | if (EltTy->getDecl()->hasFlexibleArrayMember()) { |
| 238 | Diag(DeclType.Loc, diag::err_flexible_array_in_array, |
| 239 | T.getAsString()); |
Steve Naroff | e1223f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 240 | T = Context.IntTy; |
| 241 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 242 | } |
| 243 | } |
Steve Naroff | 42471f8 | 2007-08-30 22:35:45 +0000 | [diff] [blame] | 244 | // C99 6.7.5.2p1: The size expression shall have integer type. |
| 245 | if (ArraySize && !ArraySize->getType()->isIntegerType()) { |
| 246 | Diag(ArraySize->getLocStart(), diag::err_array_size_non_int, |
| 247 | ArraySize->getType().getAsString(), ArraySize->getSourceRange()); |
| 248 | D.setInvalidType(true); |
| 249 | } |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 250 | llvm::APSInt ConstVal(32); |
| 251 | // If no expression was provided, we consider it a VLA. |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 252 | if (!ArraySize) { |
| 253 | T = Context.getIncompleteArrayType(T, ASM, ATI.TypeQuals); |
| 254 | } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context)) { |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 255 | T = Context.getVariableArrayType(T, ArraySize, ASM, ATI.TypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 256 | } else { |
Steve Naroff | 42471f8 | 2007-08-30 22:35:45 +0000 | [diff] [blame] | 257 | // C99 6.7.5.2p1: If the expression is a constant expression, it shall |
| 258 | // have a value greater than zero. |
| 259 | if (ConstVal.isSigned()) { |
| 260 | if (ConstVal.isNegative()) { |
| 261 | Diag(ArraySize->getLocStart(), |
| 262 | diag::err_typecheck_negative_array_size, |
| 263 | ArraySize->getSourceRange()); |
| 264 | D.setInvalidType(true); |
| 265 | } else if (ConstVal == 0) { |
| 266 | // GCC accepts zero sized static arrays. |
| 267 | Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size, |
| 268 | ArraySize->getSourceRange()); |
| 269 | } |
| 270 | } |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 271 | T = Context.getConstantArrayType(T, ConstVal, ASM, ATI.TypeQuals); |
Steve Naroff | 42471f8 | 2007-08-30 22:35:45 +0000 | [diff] [blame] | 272 | } |
Chris Lattner | 94f81fd | 2007-08-28 16:54:00 +0000 | [diff] [blame] | 273 | // If this is not C99, extwarn about VLA's and C99 array size modifiers. |
| 274 | if (!getLangOptions().C99 && |
| 275 | (ASM != ArrayType::Normal || |
| 276 | (ArraySize && !ArraySize->isIntegerConstantExpr(Context)))) |
| 277 | Diag(D.getIdentifierLoc(), diag::ext_vla); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 278 | break; |
| 279 | } |
| 280 | case DeclaratorChunk::Function: |
| 281 | // If the function declarator has a prototype (i.e. it is not () and |
| 282 | // does not have a K&R-style identifier list), then the arguments are part |
| 283 | // of the type, otherwise the argument list is (). |
| 284 | const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun; |
Chris Lattner | 68cfd49 | 2007-12-19 18:01:43 +0000 | [diff] [blame] | 285 | |
Chris Lattner | cd88129 | 2007-12-19 05:31:29 +0000 | [diff] [blame] | 286 | // C99 6.7.5.3p1: The return type may not be a function or array type. |
Chris Lattner | 68cfd49 | 2007-12-19 18:01:43 +0000 | [diff] [blame] | 287 | if (T->isArrayType() || T->isFunctionType()) { |
Chris Lattner | cd88129 | 2007-12-19 05:31:29 +0000 | [diff] [blame] | 288 | Diag(DeclType.Loc, diag::err_func_returning_array_function, |
| 289 | T.getAsString()); |
| 290 | T = Context.IntTy; |
| 291 | D.setInvalidType(true); |
| 292 | } |
| 293 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 294 | if (!FTI.hasPrototype) { |
| 295 | // Simple void foo(), where the incoming T is the result type. |
| 296 | T = Context.getFunctionTypeNoProto(T); |
| 297 | |
| 298 | // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition. |
| 299 | if (FTI.NumArgs != 0) |
| 300 | Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration); |
| 301 | |
| 302 | } else { |
| 303 | // Otherwise, we have a function with an argument list that is |
| 304 | // potentially variadic. |
| 305 | llvm::SmallVector<QualType, 16> ArgTys; |
| 306 | |
| 307 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) { |
| 308 | QualType ArgTy = QualType::getFromOpaquePtr(FTI.ArgInfo[i].TypeInfo); |
Chris Lattner | 78c75fb | 2007-07-21 05:30:18 +0000 | [diff] [blame] | 309 | assert(!ArgTy.isNull() && "Couldn't parse type?"); |
Steve Naroff | 08d5139 | 2007-09-10 22:17:00 +0000 | [diff] [blame] | 310 | // |
| 311 | // Perform the default function/array conversion (C99 6.7.5.3p[7,8]). |
| 312 | // This matches the conversion that is done in |
Nate Begeman | bff5f5c | 2007-11-13 21:49:48 +0000 | [diff] [blame] | 313 | // Sema::ActOnParamDeclarator(). Without this conversion, the |
Steve Naroff | 08d5139 | 2007-09-10 22:17:00 +0000 | [diff] [blame] | 314 | // argument type in the function prototype *will not* match the |
| 315 | // type in ParmVarDecl (which makes the code generator unhappy). |
| 316 | // |
| 317 | // FIXME: We still apparently need the conversion in |
| 318 | // Sema::ParseParamDeclarator(). This doesn't make any sense, since |
| 319 | // it should be driving off the type being created here. |
| 320 | // |
| 321 | // FIXME: If a source translation tool needs to see the original type, |
| 322 | // then we need to consider storing both types somewhere... |
| 323 | // |
Chris Lattner | 529bd02 | 2008-01-02 22:50:48 +0000 | [diff] [blame] | 324 | if (const ArrayType *AT = ArgTy->getAsArrayType()) { |
| 325 | // int x[restrict 4] -> int *restrict |
Steve Naroff | 08d5139 | 2007-09-10 22:17:00 +0000 | [diff] [blame] | 326 | ArgTy = Context.getPointerType(AT->getElementType()); |
Chris Lattner | 529bd02 | 2008-01-02 22:50:48 +0000 | [diff] [blame] | 327 | ArgTy = ArgTy.getQualifiedType(AT->getIndexTypeQualifier()); |
| 328 | } else if (ArgTy->isFunctionType()) |
Steve Naroff | 08d5139 | 2007-09-10 22:17:00 +0000 | [diff] [blame] | 329 | ArgTy = Context.getPointerType(ArgTy); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 330 | // Look for 'void'. void is allowed only as a single argument to a |
| 331 | // function with no other parameters (C99 6.7.5.3p10). We record |
| 332 | // int(void) as a FunctionTypeProto with an empty argument list. |
Steve Naroff | 08d5139 | 2007-09-10 22:17:00 +0000 | [diff] [blame] | 333 | else if (ArgTy->isVoidType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 334 | // If this is something like 'float(int, void)', reject it. 'void' |
| 335 | // is an incomplete type (C99 6.2.5p19) and function decls cannot |
| 336 | // have arguments of incomplete type. |
| 337 | if (FTI.NumArgs != 1 || FTI.isVariadic) { |
| 338 | Diag(DeclType.Loc, diag::err_void_only_param); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 339 | ArgTy = Context.IntTy; |
Chris Lattner | 78c75fb | 2007-07-21 05:30:18 +0000 | [diff] [blame] | 340 | FTI.ArgInfo[i].TypeInfo = ArgTy.getAsOpaquePtr(); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 341 | } else if (FTI.ArgInfo[i].Ident) { |
| 342 | // Reject, but continue to parse 'int(void abc)'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 343 | Diag(FTI.ArgInfo[i].IdentLoc, |
Chris Lattner | 4565d4e | 2007-07-21 05:26:43 +0000 | [diff] [blame] | 344 | diag::err_param_with_void_type); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 345 | ArgTy = Context.IntTy; |
Chris Lattner | 78c75fb | 2007-07-21 05:30:18 +0000 | [diff] [blame] | 346 | FTI.ArgInfo[i].TypeInfo = ArgTy.getAsOpaquePtr(); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 347 | } else { |
| 348 | // Reject, but continue to parse 'float(const void)'. |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 349 | if (ArgTy.getCVRQualifiers()) |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 350 | Diag(DeclType.Loc, diag::err_void_param_qualified); |
| 351 | |
| 352 | // Do not add 'void' to the ArgTys list. |
| 353 | break; |
| 354 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | ArgTys.push_back(ArgTy); |
| 358 | } |
| 359 | T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(), |
| 360 | FTI.isVariadic); |
| 361 | } |
| 362 | break; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | return T; |
| 367 | } |
| 368 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 369 | /// ObjCGetTypeForMethodDefinition - Builds the type for a method definition |
Fariborz Jahanian | 360300c | 2007-11-09 22:27:59 +0000 | [diff] [blame] | 370 | /// declarator |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 371 | QualType Sema::ObjCGetTypeForMethodDefinition(DeclTy *D) { |
| 372 | ObjCMethodDecl *MDecl = dyn_cast<ObjCMethodDecl>(static_cast<Decl *>(D)); |
Fariborz Jahanian | 306d68f | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 373 | QualType T = MDecl->getResultType(); |
| 374 | llvm::SmallVector<QualType, 16> ArgTys; |
| 375 | |
Fariborz Jahanian | 3560002 | 2007-11-09 17:18:29 +0000 | [diff] [blame] | 376 | // Add the first two invisible argument types for self and _cmd. |
Fariborz Jahanian | 1f7b6f8 | 2007-11-09 19:52:12 +0000 | [diff] [blame] | 377 | if (MDecl->isInstance()) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 378 | QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface()); |
Fariborz Jahanian | 1f7b6f8 | 2007-11-09 19:52:12 +0000 | [diff] [blame] | 379 | selfTy = Context.getPointerType(selfTy); |
| 380 | ArgTys.push_back(selfTy); |
| 381 | } |
Fariborz Jahanian | 3560002 | 2007-11-09 17:18:29 +0000 | [diff] [blame] | 382 | else |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 383 | ArgTys.push_back(Context.getObjCIdType()); |
| 384 | ArgTys.push_back(Context.getObjCSelType()); |
Fariborz Jahanian | 3560002 | 2007-11-09 17:18:29 +0000 | [diff] [blame] | 385 | |
Fariborz Jahanian | 306d68f | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 386 | for (int i = 0; i < MDecl->getNumParams(); i++) { |
| 387 | ParmVarDecl *PDecl = MDecl->getParamDecl(i); |
| 388 | QualType ArgTy = PDecl->getType(); |
| 389 | assert(!ArgTy.isNull() && "Couldn't parse type?"); |
Fariborz Jahanian | 306d68f | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 390 | // Perform the default function/array conversion (C99 6.7.5.3p[7,8]). |
| 391 | // This matches the conversion that is done in |
Steve Naroff | 6649992 | 2007-11-12 03:44:46 +0000 | [diff] [blame] | 392 | // Sema::ParseParamDeclarator(). |
Fariborz Jahanian | 306d68f | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 393 | if (const ArrayType *AT = ArgTy->getAsArrayType()) |
| 394 | ArgTy = Context.getPointerType(AT->getElementType()); |
| 395 | else if (ArgTy->isFunctionType()) |
| 396 | ArgTy = Context.getPointerType(ArgTy); |
| 397 | ArgTys.push_back(ArgTy); |
| 398 | } |
| 399 | T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(), |
Steve Naroff | b99a4a3 | 2007-12-18 03:41:15 +0000 | [diff] [blame] | 400 | MDecl->isVariadic()); |
Fariborz Jahanian | 306d68f | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 401 | return T; |
| 402 | } |
| 403 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 404 | Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 405 | // C99 6.7.6: Type names have no identifier. This is already validated by |
| 406 | // the parser. |
| 407 | assert(D.getIdentifier() == 0 && "Type name should have no identifier!"); |
| 408 | |
| 409 | QualType T = GetTypeForDeclarator(D, S); |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 410 | |
| 411 | assert(!T.isNull() && "GetTypeForDeclarator() returned null type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 412 | |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 413 | // In this context, we *do not* check D.getInvalidType(). If the declarator |
| 414 | // type was invalid, GetTypeForDeclarator() still returns a "valid" type, |
| 415 | // though it will not reflect the user specified type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 416 | return T.getAsOpaquePtr(); |
| 417 | } |
| 418 | |
Steve Naroff | e1223f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 419 | // Called from Parser::ParseParenDeclarator(). |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 420 | Sema::TypeResult Sema::ActOnParamDeclaratorType(Scope *S, Declarator &D) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 421 | // Note: parameters have identifiers, but we don't care about them here, we |
| 422 | // just want the type converted. |
| 423 | QualType T = GetTypeForDeclarator(D, S); |
| 424 | |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 425 | assert(!T.isNull() && "GetTypeForDeclarator() returned null type"); |
| 426 | |
| 427 | // In this context, we *do not* check D.getInvalidType(). If the declarator |
| 428 | // type was invalid, GetTypeForDeclarator() still returns a "valid" type, |
| 429 | // though it will not reflect the user specified type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 430 | return T.getAsOpaquePtr(); |
| 431 | } |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 432 | |
| 433 | AttributeList *Sema::ProcessTypeAttributes(QualType &Result, AttributeList *AL){ |
| 434 | // Scan through and apply attributes to this type where it makes sense. Some |
| 435 | // attributes (such as __address_space__, __vector_size__, etc) apply to the |
| 436 | // type, but others can be present in the type specifiers even though they |
| 437 | // apply to the decl. Here we apply and delete attributes that apply to the |
| 438 | // type and leave the others alone. |
| 439 | llvm::SmallVector<AttributeList *, 8> LeftOverAttrs; |
| 440 | while (AL) { |
| 441 | // Unlink this attribute from the chain, so we can process it independently. |
| 442 | AttributeList *ThisAttr = AL; |
| 443 | AL = AL->getNext(); |
| 444 | ThisAttr->setNext(0); |
| 445 | |
| 446 | // If this is an attribute we can handle, do so now, otherwise, add it to |
| 447 | // the LeftOverAttrs list for rechaining. |
| 448 | switch (ThisAttr->getKind()) { |
| 449 | default: break; |
| 450 | case AttributeList::AT_address_space: |
| 451 | Result = HandleAddressSpaceTypeAttribute(Result, ThisAttr); |
| 452 | delete ThisAttr; // Consume the attribute. |
| 453 | continue; |
| 454 | } |
| 455 | |
| 456 | LeftOverAttrs.push_back(ThisAttr); |
| 457 | } |
| 458 | |
| 459 | // Rechain any attributes that haven't been deleted to the DeclSpec. |
| 460 | AttributeList *List = 0; |
| 461 | for (unsigned i = 0, e = LeftOverAttrs.size(); i != e; ++i) { |
| 462 | LeftOverAttrs[i]->setNext(List); |
| 463 | List = LeftOverAttrs[i]; |
| 464 | } |
| 465 | |
| 466 | return List; |
| 467 | } |
| 468 | |
| 469 | /// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the |
| 470 | /// specified type. |
| 471 | QualType Sema::HandleAddressSpaceTypeAttribute(QualType Type, |
| 472 | AttributeList *Attr) { |
| 473 | // If this type is already address space qualified, reject it. |
| 474 | // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers |
| 475 | // for two or more different address spaces." |
| 476 | if (Type.getAddressSpace()) { |
| 477 | Diag(Attr->getLoc(), diag::err_attribute_address_multiple_qualifiers); |
| 478 | return Type; |
| 479 | } |
| 480 | |
| 481 | // Check the attribute arguments. |
| 482 | if (Attr->getNumArgs() != 1) { |
| 483 | Diag(Attr->getLoc(), diag::err_attribute_wrong_number_arguments, |
| 484 | std::string("1")); |
| 485 | return Type; |
| 486 | } |
| 487 | Expr *ASArgExpr = static_cast<Expr *>(Attr->getArg(0)); |
| 488 | llvm::APSInt addrSpace(32); |
| 489 | if (!ASArgExpr->isIntegerConstantExpr(addrSpace, Context)) { |
| 490 | Diag(Attr->getLoc(), diag::err_attribute_address_space_not_int, |
| 491 | ASArgExpr->getSourceRange()); |
| 492 | return Type; |
| 493 | } |
| 494 | |
| 495 | unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue()); |
| 496 | return Context.getASQualType(Type, ASIdx); |
| 497 | } |
| 498 | |