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 | 5fcb38b | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 31 | case DeclSpec::TST_void: |
| 32 | Result = Context.VoidTy; |
| 33 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 34 | case DeclSpec::TST_char: |
| 35 | if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified) |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 36 | Result = Context.CharTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 37 | else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 38 | Result = Context.SignedCharTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 39 | else { |
| 40 | assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned && |
| 41 | "Unknown TSS value"); |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 42 | Result = Context.UnsignedCharTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 43 | } |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 44 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 45 | case DeclSpec::TST_unspecified: // Unspecific typespec defaults to int. |
Chris Lattner | 5328f31 | 2007-08-21 17:02:28 +0000 | [diff] [blame] | 46 | case DeclSpec::TST_int: { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 47 | if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) { |
| 48 | switch (DS.getTypeSpecWidth()) { |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 49 | case DeclSpec::TSW_unspecified: Result = Context.IntTy; break; |
| 50 | case DeclSpec::TSW_short: Result = Context.ShortTy; break; |
| 51 | case DeclSpec::TSW_long: Result = Context.LongTy; break; |
| 52 | case DeclSpec::TSW_longlong: Result = Context.LongLongTy; break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 53 | } |
| 54 | } else { |
| 55 | switch (DS.getTypeSpecWidth()) { |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 56 | case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break; |
| 57 | case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break; |
| 58 | case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break; |
| 59 | case DeclSpec::TSW_longlong: Result =Context.UnsignedLongLongTy; break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 60 | } |
| 61 | } |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 62 | break; |
Chris Lattner | 5328f31 | 2007-08-21 17:02:28 +0000 | [diff] [blame] | 63 | } |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 64 | case DeclSpec::TST_float: Result = Context.FloatTy; break; |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 65 | case DeclSpec::TST_double: |
| 66 | if (DS.getTypeSpecWidth() == DeclSpec::TSW_long) |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 67 | Result = Context.LongDoubleTy; |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 68 | else |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 69 | Result = Context.DoubleTy; |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 70 | break; |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 71 | case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 72 | case DeclSpec::TST_decimal32: // _Decimal32 |
| 73 | case DeclSpec::TST_decimal64: // _Decimal64 |
| 74 | case DeclSpec::TST_decimal128: // _Decimal128 |
| 75 | assert(0 && "FIXME: GNU decimal extensions not supported yet!"); |
| 76 | case DeclSpec::TST_enum: |
| 77 | case DeclSpec::TST_union: |
| 78 | case DeclSpec::TST_struct: { |
| 79 | Decl *D = static_cast<Decl *>(DS.getTypeRep()); |
| 80 | assert(D && "Didn't get a decl for a enum/union/struct?"); |
| 81 | assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 && |
| 82 | DS.getTypeSpecSign() == 0 && |
| 83 | "Can't handle qualifiers on typedef names yet!"); |
| 84 | // TypeQuals handled by caller. |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 85 | Result = Context.getTagDeclType(cast<TagDecl>(D)); |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 86 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 87 | } |
| 88 | case DeclSpec::TST_typedef: { |
| 89 | Decl *D = static_cast<Decl *>(DS.getTypeRep()); |
| 90 | assert(D && "Didn't get a decl for a typedef?"); |
| 91 | assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 && |
| 92 | DS.getTypeSpecSign() == 0 && |
| 93 | "Can't handle qualifiers on typedef names yet!"); |
Steve Naroff | 81f1bba | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 94 | // FIXME: Adding a TST_objcInterface clause doesn't seem ideal, so |
| 95 | // we have this "hack" for now... |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 96 | if (ObjCInterfaceDecl *ObjCIntDecl = dyn_cast<ObjCInterfaceDecl>(D)) { |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 97 | if (DS.getProtocolQualifiers() == 0) { |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 98 | Result = Context.getObjCInterfaceType(ObjCIntDecl); |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 99 | break; |
| 100 | } |
Fariborz Jahanian | 91193f6 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 101 | |
| 102 | Action::DeclTy **PPDecl = &(*DS.getProtocolQualifiers())[0]; |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 103 | Result = Context.getObjCQualifiedInterfaceType(ObjCIntDecl, |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 104 | reinterpret_cast<ObjCProtocolDecl**>(PPDecl), |
Chris Lattner | 69f0193 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 105 | DS.getNumProtocolQualifiers()); |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 106 | break; |
Fariborz Jahanian | 91193f6 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 107 | } |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 108 | else if (TypedefDecl *typeDecl = dyn_cast<TypedefDecl>(D)) { |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 109 | if (Context.getObjCIdType() == Context.getTypedefType(typeDecl) |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 110 | && DS.getProtocolQualifiers()) { |
| 111 | // id<protocol-list> |
| 112 | Action::DeclTy **PPDecl = &(*DS.getProtocolQualifiers())[0]; |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 113 | Result = Context.getObjCQualifiedIdType(typeDecl->getUnderlyingType(), |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 114 | reinterpret_cast<ObjCProtocolDecl**>(PPDecl), |
Chris Lattner | 69f0193 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 115 | DS.getNumProtocolQualifiers()); |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 116 | break; |
Fariborz Jahanian | e76e841 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 117 | } |
| 118 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 119 | // TypeQuals handled by caller. |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 120 | Result = Context.getTypedefType(cast<TypedefDecl>(D)); |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 121 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 122 | } |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 123 | case DeclSpec::TST_typeofType: |
| 124 | Result = QualType::getFromOpaquePtr(DS.getTypeRep()); |
| 125 | assert(!Result.isNull() && "Didn't get a type for typeof?"); |
Steve Naroff | 7cbb146 | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 126 | // TypeQuals handled by caller. |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 127 | Result = Context.getTypeOfType(Result); |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 128 | break; |
Steve Naroff | 7cbb146 | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 129 | case DeclSpec::TST_typeofExpr: { |
| 130 | Expr *E = static_cast<Expr *>(DS.getTypeRep()); |
| 131 | assert(E && "Didn't get an expression for typeof?"); |
| 132 | // TypeQuals handled by caller. |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 133 | Result = Context.getTypeOfExpr(E); |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 134 | break; |
Steve Naroff | 7cbb146 | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 135 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 136 | } |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 137 | |
| 138 | // Handle complex types. |
| 139 | if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 140 | Result = Context.getComplexType(Result); |
Chris Lattner | 06fb867 | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 141 | |
| 142 | assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary && |
| 143 | "FIXME: imaginary types not supported yet!"); |
| 144 | |
Chris Lattner | d496fb9 | 2008-02-20 22:04:11 +0000 | [diff] [blame] | 145 | // See if there are any attributes on the declspec that apply to the type (as |
| 146 | // opposed to the decl). |
Chris Lattner | 9e98250 | 2008-02-21 01:07:18 +0000 | [diff] [blame] | 147 | if (AttributeList *AL = DS.getAttributes()) |
| 148 | DS.SetAttributes(ProcessTypeAttributes(Result, AL)); |
| 149 | |
Chris Lattner | 5fcb38b | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 150 | // Apply const/volatile/restrict qualifiers to T. |
| 151 | if (unsigned TypeQuals = DS.getTypeQualifiers()) { |
| 152 | |
| 153 | // Enforce C99 6.7.3p2: "Types other than pointer types derived from object |
| 154 | // or incomplete types shall not be restrict-qualified." C++ also allows |
| 155 | // restrict-qualified references. |
| 156 | if (TypeQuals & QualType::Restrict) { |
| 157 | QualType EltTy; |
| 158 | if (const PointerType *PT = Result->getAsPointerType()) |
| 159 | EltTy = PT->getPointeeType(); |
| 160 | else if (const ReferenceType *RT = Result->getAsReferenceType()) |
| 161 | EltTy = RT->getReferenceeType(); |
| 162 | else { |
| 163 | Diag(DS.getRestrictSpecLoc(), |
| 164 | diag::err_typecheck_invalid_restrict_not_pointer, |
| 165 | Result.getAsString(), DS.getSourceRange()); |
| 166 | } |
| 167 | |
| 168 | // If we have a pointer or reference, the pointee must have an object or |
| 169 | // incomplete type. |
Chris Lattner | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame^] | 170 | if (!EltTy.isNull() && !EltTy->isIncompleteOrObjectType()) { |
Chris Lattner | 5fcb38b | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 171 | Diag(DS.getRestrictSpecLoc(), |
| 172 | diag::err_typecheck_invalid_restrict_invalid_pointee, |
| 173 | EltTy.getAsString(), DS.getSourceRange()); |
| 174 | EltTy = QualType(); |
| 175 | } |
| 176 | |
| 177 | if (EltTy.isNull()) // Invalid restrict: remove the restrict qualifier. |
| 178 | TypeQuals &= ~QualType::Restrict; |
| 179 | } |
| 180 | |
| 181 | // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification |
| 182 | // of a function type includes any type qualifiers, the behavior is |
| 183 | // undefined." |
| 184 | if (Result->isFunctionType() && TypeQuals) { |
| 185 | // Get some location to point at, either the C or V location. |
| 186 | SourceLocation Loc; |
| 187 | if (TypeQuals & QualType::Const) |
| 188 | Loc = DS.getConstSpecLoc(); |
| 189 | else { |
| 190 | assert((TypeQuals & QualType::Volatile) && |
| 191 | "Has CV quals but not C or V?"); |
| 192 | Loc = DS.getVolatileSpecLoc(); |
| 193 | } |
| 194 | Diag(Loc, diag::warn_typecheck_function_qualifiers, |
| 195 | Result.getAsString(), DS.getSourceRange()); |
| 196 | } |
| 197 | |
| 198 | Result = Result.getQualifiedType(TypeQuals); |
| 199 | } |
Chris Lattner | 9e98250 | 2008-02-21 01:07:18 +0000 | [diff] [blame] | 200 | return Result; |
| 201 | } |
| 202 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 203 | /// GetTypeForDeclarator - Convert the type for the specified declarator to Type |
| 204 | /// instances. |
| 205 | QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S) { |
Chris Lattner | 11f20f9 | 2007-08-28 16:40:32 +0000 | [diff] [blame] | 206 | // long long is a C99 feature. |
Chris Lattner | 1a7d991 | 2007-08-28 16:41:29 +0000 | [diff] [blame] | 207 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Chris Lattner | 11f20f9 | 2007-08-28 16:40:32 +0000 | [diff] [blame] | 208 | D.getDeclSpec().getTypeSpecWidth() == DeclSpec::TSW_longlong) |
| 209 | Diag(D.getDeclSpec().getTypeSpecWidthLoc(), diag::ext_longlong); |
| 210 | |
Chris Lattner | 726c545 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 211 | QualType T = ConvertDeclSpecToType(D.getDeclSpec()); |
Steve Naroff | 91b03f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 212 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 213 | // Walk the DeclTypeInfo, building the recursive type as we go. DeclTypeInfos |
| 214 | // are ordered from the identifier out, which is opposite of what we want :). |
| 215 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { |
Chris Lattner | 69f0193 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 216 | DeclaratorChunk &DeclType = D.getTypeObject(e-i-1); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 217 | switch (DeclType.Kind) { |
| 218 | default: assert(0 && "Unknown decltype!"); |
| 219 | case DeclaratorChunk::Pointer: |
Chris Lattner | 36be3d8 | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 220 | if (T->isReferenceType()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 221 | // C++ 8.3.2p4: There shall be no ... pointers to references ... |
Chris Lattner | 5fcb38b | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 222 | Diag(DeclType.Loc, diag::err_illegal_decl_pointer_to_reference, |
Steve Naroff | 73458bf | 2008-01-14 23:33:18 +0000 | [diff] [blame] | 223 | D.getIdentifier() ? D.getIdentifier()->getName() : "type name"); |
Steve Naroff | 91b03f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 224 | D.setInvalidType(true); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 225 | T = Context.IntTy; |
| 226 | } |
| 227 | |
Chris Lattner | 5fcb38b | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 228 | // Enforce C99 6.7.3p2: "Types other than pointer types derived from |
| 229 | // object or incomplete types shall not be restrict-qualified." |
| 230 | if ((DeclType.Ptr.TypeQuals & QualType::Restrict) && |
Chris Lattner | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame^] | 231 | !T->isIncompleteOrObjectType()) { |
Chris Lattner | 5fcb38b | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 232 | Diag(DeclType.Loc, |
| 233 | diag::err_typecheck_invalid_restrict_invalid_pointee, |
| 234 | T.getAsString()); |
| 235 | DeclType.Ptr.TypeQuals &= QualType::Restrict; |
| 236 | } |
| 237 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 238 | // Apply the pointer typequals to the pointer object. |
| 239 | T = Context.getPointerType(T).getQualifiedType(DeclType.Ptr.TypeQuals); |
Chris Lattner | 69f0193 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 240 | |
| 241 | // See if there are any attributes on the pointer that apply to it. |
| 242 | if (AttributeList *AL = DeclType.Ptr.AttrList) |
| 243 | DeclType.Ptr.AttrList = ProcessTypeAttributes(T, AL); |
| 244 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 245 | break; |
| 246 | case DeclaratorChunk::Reference: |
Chris Lattner | f0c4a0a | 2007-07-31 16:56:34 +0000 | [diff] [blame] | 247 | if (const ReferenceType *RT = T->getAsReferenceType()) { |
Chris Lattner | 2e7d57f | 2008-02-21 01:32:57 +0000 | [diff] [blame] | 248 | // C++ 8.3.2p4: There shall be no references to references. |
Chris Lattner | 5fcb38b | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 249 | Diag(DeclType.Loc, diag::err_illegal_decl_reference_to_reference, |
Steve Naroff | 73458bf | 2008-01-14 23:33:18 +0000 | [diff] [blame] | 250 | D.getIdentifier() ? D.getIdentifier()->getName() : "type name"); |
Steve Naroff | 91b03f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 251 | D.setInvalidType(true); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 252 | T = RT->getReferenceeType(); |
| 253 | } |
| 254 | |
Chris Lattner | 5fcb38b | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 255 | // Enforce C99 6.7.3p2: "Types other than pointer types derived from |
| 256 | // object or incomplete types shall not be restrict-qualified." |
| 257 | if (DeclType.Ref.HasRestrict && |
Chris Lattner | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame^] | 258 | !T->isIncompleteOrObjectType()) { |
Chris Lattner | 5fcb38b | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 259 | Diag(DeclType.Loc, |
| 260 | diag::err_typecheck_invalid_restrict_invalid_pointee, |
| 261 | T.getAsString()); |
| 262 | DeclType.Ref.HasRestrict = false; |
| 263 | } |
| 264 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 265 | T = Context.getReferenceType(T); |
Chris Lattner | 5fcb38b | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 266 | |
| 267 | // Handle restrict on references. |
| 268 | if (DeclType.Ref.HasRestrict) |
| 269 | T.addRestrict(); |
Chris Lattner | 2e7d57f | 2008-02-21 01:32:57 +0000 | [diff] [blame] | 270 | |
Chris Lattner | 69f0193 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 271 | // See if there are any attributes on the pointer that apply to it. |
| 272 | if (AttributeList *AL = DeclType.Ref.AttrList) |
| 273 | DeclType.Ref.AttrList = ProcessTypeAttributes(T, AL); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 274 | break; |
| 275 | case DeclaratorChunk::Array: { |
Chris Lattner | 67d3c8d | 2008-04-02 01:05:10 +0000 | [diff] [blame] | 276 | DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr; |
Chris Lattner | 47958f6 | 2007-08-28 16:54:00 +0000 | [diff] [blame] | 277 | Expr *ArraySize = static_cast<Expr*>(ATI.NumElts); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 278 | ArrayType::ArraySizeModifier ASM; |
| 279 | if (ATI.isStar) |
| 280 | ASM = ArrayType::Star; |
| 281 | else if (ATI.hasStatic) |
| 282 | ASM = ArrayType::Static; |
| 283 | else |
| 284 | ASM = ArrayType::Normal; |
| 285 | |
| 286 | // C99 6.7.5.2p1: If the element type is an incomplete or function type, |
| 287 | // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]()) |
| 288 | if (T->isIncompleteType()) { |
| 289 | Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_incomplete_type, |
| 290 | T.getAsString()); |
Steve Naroff | 91b03f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 291 | T = Context.IntTy; |
| 292 | D.setInvalidType(true); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 293 | } else if (T->isFunctionType()) { |
| 294 | Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_functions, |
Steve Naroff | 73458bf | 2008-01-14 23:33:18 +0000 | [diff] [blame] | 295 | D.getIdentifier() ? D.getIdentifier()->getName() : "type name"); |
Steve Naroff | 91b03f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 296 | T = Context.getPointerType(T); |
| 297 | D.setInvalidType(true); |
Chris Lattner | f0c4a0a | 2007-07-31 16:56:34 +0000 | [diff] [blame] | 298 | } else if (const ReferenceType *RT = T->getAsReferenceType()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 299 | // C++ 8.3.2p4: There shall be no ... arrays of references ... |
| 300 | Diag(D.getIdentifierLoc(), diag::err_illegal_decl_array_of_references, |
Steve Naroff | 73458bf | 2008-01-14 23:33:18 +0000 | [diff] [blame] | 301 | D.getIdentifier() ? D.getIdentifier()->getName() : "type name"); |
Steve Naroff | 91b03f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 302 | T = RT->getReferenceeType(); |
| 303 | D.setInvalidType(true); |
Chris Lattner | 36be3d8 | 2007-07-31 21:33:24 +0000 | [diff] [blame] | 304 | } else if (const RecordType *EltTy = T->getAsRecordType()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 305 | // If the element type is a struct or union that contains a variadic |
| 306 | // array, reject it: C99 6.7.2.1p2. |
| 307 | if (EltTy->getDecl()->hasFlexibleArrayMember()) { |
| 308 | Diag(DeclType.Loc, diag::err_flexible_array_in_array, |
| 309 | T.getAsString()); |
Steve Naroff | 91b03f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 310 | T = Context.IntTy; |
| 311 | D.setInvalidType(true); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 312 | } |
| 313 | } |
Steve Naroff | 63b6a64 | 2007-08-30 22:35:45 +0000 | [diff] [blame] | 314 | // C99 6.7.5.2p1: The size expression shall have integer type. |
| 315 | if (ArraySize && !ArraySize->getType()->isIntegerType()) { |
| 316 | Diag(ArraySize->getLocStart(), diag::err_array_size_non_int, |
| 317 | ArraySize->getType().getAsString(), ArraySize->getSourceRange()); |
| 318 | D.setInvalidType(true); |
Chris Lattner | 67d3c8d | 2008-04-02 01:05:10 +0000 | [diff] [blame] | 319 | delete ArraySize; |
| 320 | ATI.NumElts = ArraySize = 0; |
Steve Naroff | 63b6a64 | 2007-08-30 22:35:45 +0000 | [diff] [blame] | 321 | } |
Steve Naroff | 24c9b98 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 322 | llvm::APSInt ConstVal(32); |
Chris Lattner | 67d3c8d | 2008-04-02 01:05:10 +0000 | [diff] [blame] | 323 | // If no expression was provided, we consider it an incomplete array. |
Eli Friedman | 8ff0778 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 324 | if (!ArraySize) { |
| 325 | T = Context.getIncompleteArrayType(T, ASM, ATI.TypeQuals); |
| 326 | } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context)) { |
Steve Naroff | 24c9b98 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 327 | T = Context.getVariableArrayType(T, ArraySize, ASM, ATI.TypeQuals); |
Eli Friedman | 8ff0778 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 328 | } else { |
Steve Naroff | 63b6a64 | 2007-08-30 22:35:45 +0000 | [diff] [blame] | 329 | // C99 6.7.5.2p1: If the expression is a constant expression, it shall |
| 330 | // have a value greater than zero. |
| 331 | if (ConstVal.isSigned()) { |
| 332 | if (ConstVal.isNegative()) { |
| 333 | Diag(ArraySize->getLocStart(), |
| 334 | diag::err_typecheck_negative_array_size, |
| 335 | ArraySize->getSourceRange()); |
| 336 | D.setInvalidType(true); |
| 337 | } else if (ConstVal == 0) { |
| 338 | // GCC accepts zero sized static arrays. |
| 339 | Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size, |
| 340 | ArraySize->getSourceRange()); |
| 341 | } |
| 342 | } |
Steve Naroff | 24c9b98 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 343 | T = Context.getConstantArrayType(T, ConstVal, ASM, ATI.TypeQuals); |
Steve Naroff | 63b6a64 | 2007-08-30 22:35:45 +0000 | [diff] [blame] | 344 | } |
Chris Lattner | 47958f6 | 2007-08-28 16:54:00 +0000 | [diff] [blame] | 345 | // If this is not C99, extwarn about VLA's and C99 array size modifiers. |
| 346 | if (!getLangOptions().C99 && |
| 347 | (ASM != ArrayType::Normal || |
| 348 | (ArraySize && !ArraySize->isIntegerConstantExpr(Context)))) |
| 349 | Diag(D.getIdentifierLoc(), diag::ext_vla); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 350 | break; |
| 351 | } |
| 352 | case DeclaratorChunk::Function: |
| 353 | // If the function declarator has a prototype (i.e. it is not () and |
| 354 | // does not have a K&R-style identifier list), then the arguments are part |
| 355 | // of the type, otherwise the argument list is (). |
| 356 | const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun; |
Chris Lattner | bccfc15 | 2007-12-19 18:01:43 +0000 | [diff] [blame] | 357 | |
Chris Lattner | 6ad7e88 | 2007-12-19 05:31:29 +0000 | [diff] [blame] | 358 | // 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] | 359 | if (T->isArrayType() || T->isFunctionType()) { |
Chris Lattner | 6ad7e88 | 2007-12-19 05:31:29 +0000 | [diff] [blame] | 360 | Diag(DeclType.Loc, diag::err_func_returning_array_function, |
| 361 | T.getAsString()); |
| 362 | T = Context.IntTy; |
| 363 | D.setInvalidType(true); |
| 364 | } |
| 365 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 366 | if (!FTI.hasPrototype) { |
| 367 | // Simple void foo(), where the incoming T is the result type. |
| 368 | T = Context.getFunctionTypeNoProto(T); |
| 369 | |
| 370 | // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition. |
| 371 | if (FTI.NumArgs != 0) |
| 372 | Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration); |
| 373 | |
| 374 | } else { |
| 375 | // Otherwise, we have a function with an argument list that is |
| 376 | // potentially variadic. |
| 377 | llvm::SmallVector<QualType, 16> ArgTys; |
| 378 | |
| 379 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) { |
| 380 | QualType ArgTy = QualType::getFromOpaquePtr(FTI.ArgInfo[i].TypeInfo); |
| 381 | assert(!ArgTy.isNull() && "Couldn't parse type?"); |
Steve Naroff | 1830be7 | 2007-09-10 22:17:00 +0000 | [diff] [blame] | 382 | // |
| 383 | // Perform the default function/array conversion (C99 6.7.5.3p[7,8]). |
| 384 | // This matches the conversion that is done in |
Nate Begeman | 2240f54 | 2007-11-13 21:49:48 +0000 | [diff] [blame] | 385 | // Sema::ActOnParamDeclarator(). Without this conversion, the |
Steve Naroff | 1830be7 | 2007-09-10 22:17:00 +0000 | [diff] [blame] | 386 | // argument type in the function prototype *will not* match the |
| 387 | // type in ParmVarDecl (which makes the code generator unhappy). |
| 388 | // |
| 389 | // FIXME: We still apparently need the conversion in |
Chris Lattner | 19eb97e | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 390 | // Sema::ActOnParamDeclarator(). This doesn't make any sense, since |
Steve Naroff | 1830be7 | 2007-09-10 22:17:00 +0000 | [diff] [blame] | 391 | // it should be driving off the type being created here. |
| 392 | // |
| 393 | // FIXME: If a source translation tool needs to see the original type, |
| 394 | // then we need to consider storing both types somewhere... |
| 395 | // |
Chris Lattner | 19eb97e | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 396 | if (ArgTy->isArrayType()) { |
| 397 | ArgTy = Context.getArrayDecayedType(ArgTy); |
Chris Lattner | c08564a | 2008-01-02 22:50:48 +0000 | [diff] [blame] | 398 | } else if (ArgTy->isFunctionType()) |
Steve Naroff | 1830be7 | 2007-09-10 22:17:00 +0000 | [diff] [blame] | 399 | ArgTy = Context.getPointerType(ArgTy); |
Chris Lattner | 19eb97e | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 400 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 401 | // Look for 'void'. void is allowed only as a single argument to a |
| 402 | // function with no other parameters (C99 6.7.5.3p10). We record |
| 403 | // int(void) as a FunctionTypeProto with an empty argument list. |
Steve Naroff | 1830be7 | 2007-09-10 22:17:00 +0000 | [diff] [blame] | 404 | else if (ArgTy->isVoidType()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 405 | // If this is something like 'float(int, void)', reject it. 'void' |
| 406 | // is an incomplete type (C99 6.2.5p19) and function decls cannot |
| 407 | // have arguments of incomplete type. |
| 408 | if (FTI.NumArgs != 1 || FTI.isVariadic) { |
| 409 | Diag(DeclType.Loc, diag::err_void_only_param); |
| 410 | ArgTy = Context.IntTy; |
| 411 | FTI.ArgInfo[i].TypeInfo = ArgTy.getAsOpaquePtr(); |
| 412 | } else if (FTI.ArgInfo[i].Ident) { |
| 413 | // Reject, but continue to parse 'int(void abc)'. |
| 414 | Diag(FTI.ArgInfo[i].IdentLoc, |
| 415 | diag::err_param_with_void_type); |
| 416 | ArgTy = Context.IntTy; |
| 417 | FTI.ArgInfo[i].TypeInfo = ArgTy.getAsOpaquePtr(); |
| 418 | } else { |
| 419 | // Reject, but continue to parse 'float(const void)'. |
Chris Lattner | 35fef52 | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 420 | if (ArgTy.getCVRQualifiers()) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 421 | Diag(DeclType.Loc, diag::err_void_param_qualified); |
| 422 | |
| 423 | // Do not add 'void' to the ArgTys list. |
| 424 | break; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | ArgTys.push_back(ArgTy); |
| 429 | } |
| 430 | T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(), |
| 431 | FTI.isVariadic); |
| 432 | } |
| 433 | break; |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | return T; |
| 438 | } |
| 439 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 440 | /// ObjCGetTypeForMethodDefinition - Builds the type for a method definition |
Fariborz Jahanian | ff746bc | 2007-11-09 22:27:59 +0000 | [diff] [blame] | 441 | /// declarator |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 442 | QualType Sema::ObjCGetTypeForMethodDefinition(DeclTy *D) { |
| 443 | ObjCMethodDecl *MDecl = dyn_cast<ObjCMethodDecl>(static_cast<Decl *>(D)); |
Fariborz Jahanian | dfb1c37 | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 444 | QualType T = MDecl->getResultType(); |
| 445 | llvm::SmallVector<QualType, 16> ArgTys; |
| 446 | |
Fariborz Jahanian | ea86cb8 | 2007-11-09 17:18:29 +0000 | [diff] [blame] | 447 | // Add the first two invisible argument types for self and _cmd. |
Fariborz Jahanian | 8473b22 | 2007-11-09 19:52:12 +0000 | [diff] [blame] | 448 | if (MDecl->isInstance()) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 449 | QualType selfTy = Context.getObjCInterfaceType(MDecl->getClassInterface()); |
Fariborz Jahanian | 8473b22 | 2007-11-09 19:52:12 +0000 | [diff] [blame] | 450 | selfTy = Context.getPointerType(selfTy); |
| 451 | ArgTys.push_back(selfTy); |
| 452 | } |
Fariborz Jahanian | ea86cb8 | 2007-11-09 17:18:29 +0000 | [diff] [blame] | 453 | else |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 454 | ArgTys.push_back(Context.getObjCIdType()); |
| 455 | ArgTys.push_back(Context.getObjCSelType()); |
Fariborz Jahanian | ea86cb8 | 2007-11-09 17:18:29 +0000 | [diff] [blame] | 456 | |
Chris Lattner | 685d792 | 2008-03-16 01:07:14 +0000 | [diff] [blame] | 457 | for (int i = 0, e = MDecl->getNumParams(); i != e; ++i) { |
Fariborz Jahanian | dfb1c37 | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 458 | ParmVarDecl *PDecl = MDecl->getParamDecl(i); |
| 459 | QualType ArgTy = PDecl->getType(); |
| 460 | assert(!ArgTy.isNull() && "Couldn't parse type?"); |
Fariborz Jahanian | dfb1c37 | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 461 | // Perform the default function/array conversion (C99 6.7.5.3p[7,8]). |
| 462 | // This matches the conversion that is done in |
Chris Lattner | 19eb97e | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 463 | // Sema::ActOnParamDeclarator(). |
| 464 | if (ArgTy->isArrayType()) |
| 465 | ArgTy = Context.getArrayDecayedType(ArgTy); |
Fariborz Jahanian | dfb1c37 | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 466 | else if (ArgTy->isFunctionType()) |
| 467 | ArgTy = Context.getPointerType(ArgTy); |
| 468 | ArgTys.push_back(ArgTy); |
| 469 | } |
| 470 | T = Context.getFunctionType(T, &ArgTys[0], ArgTys.size(), |
Steve Naroff | c1a88c1 | 2007-12-18 03:41:15 +0000 | [diff] [blame] | 471 | MDecl->isVariadic()); |
Fariborz Jahanian | dfb1c37 | 2007-11-08 23:49:49 +0000 | [diff] [blame] | 472 | return T; |
| 473 | } |
| 474 | |
Steve Naroff | 0acc9c9 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 475 | Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 476 | // C99 6.7.6: Type names have no identifier. This is already validated by |
| 477 | // the parser. |
| 478 | assert(D.getIdentifier() == 0 && "Type name should have no identifier!"); |
| 479 | |
| 480 | QualType T = GetTypeForDeclarator(D, S); |
Steve Naroff | d1ad6ae | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 481 | |
| 482 | assert(!T.isNull() && "GetTypeForDeclarator() returned null type"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 483 | |
Steve Naroff | d1ad6ae | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 484 | // In this context, we *do not* check D.getInvalidType(). If the declarator |
| 485 | // type was invalid, GetTypeForDeclarator() still returns a "valid" type, |
| 486 | // though it will not reflect the user specified type. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 487 | return T.getAsOpaquePtr(); |
| 488 | } |
| 489 | |
Steve Naroff | 91b03f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 490 | // Called from Parser::ParseParenDeclarator(). |
Steve Naroff | 0acc9c9 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 491 | Sema::TypeResult Sema::ActOnParamDeclaratorType(Scope *S, Declarator &D) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 492 | // Note: parameters have identifiers, but we don't care about them here, we |
| 493 | // just want the type converted. |
| 494 | QualType T = GetTypeForDeclarator(D, S); |
| 495 | |
Steve Naroff | d1ad6ae | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 496 | assert(!T.isNull() && "GetTypeForDeclarator() returned null type"); |
| 497 | |
| 498 | // In this context, we *do not* check D.getInvalidType(). If the declarator |
| 499 | // type was invalid, GetTypeForDeclarator() still returns a "valid" type, |
| 500 | // though it will not reflect the user specified type. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 501 | return T.getAsOpaquePtr(); |
| 502 | } |
Chris Lattner | 1aaeeb9 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 503 | |
| 504 | AttributeList *Sema::ProcessTypeAttributes(QualType &Result, AttributeList *AL){ |
| 505 | // Scan through and apply attributes to this type where it makes sense. Some |
| 506 | // attributes (such as __address_space__, __vector_size__, etc) apply to the |
| 507 | // type, but others can be present in the type specifiers even though they |
| 508 | // apply to the decl. Here we apply and delete attributes that apply to the |
| 509 | // type and leave the others alone. |
| 510 | llvm::SmallVector<AttributeList *, 8> LeftOverAttrs; |
| 511 | while (AL) { |
| 512 | // Unlink this attribute from the chain, so we can process it independently. |
| 513 | AttributeList *ThisAttr = AL; |
| 514 | AL = AL->getNext(); |
| 515 | ThisAttr->setNext(0); |
| 516 | |
| 517 | // If this is an attribute we can handle, do so now, otherwise, add it to |
| 518 | // the LeftOverAttrs list for rechaining. |
| 519 | switch (ThisAttr->getKind()) { |
| 520 | default: break; |
| 521 | case AttributeList::AT_address_space: |
| 522 | Result = HandleAddressSpaceTypeAttribute(Result, ThisAttr); |
| 523 | delete ThisAttr; // Consume the attribute. |
| 524 | continue; |
| 525 | } |
| 526 | |
| 527 | LeftOverAttrs.push_back(ThisAttr); |
| 528 | } |
| 529 | |
| 530 | // Rechain any attributes that haven't been deleted to the DeclSpec. |
| 531 | AttributeList *List = 0; |
| 532 | for (unsigned i = 0, e = LeftOverAttrs.size(); i != e; ++i) { |
| 533 | LeftOverAttrs[i]->setNext(List); |
| 534 | List = LeftOverAttrs[i]; |
| 535 | } |
| 536 | |
| 537 | return List; |
| 538 | } |
| 539 | |
| 540 | /// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the |
| 541 | /// specified type. |
| 542 | QualType Sema::HandleAddressSpaceTypeAttribute(QualType Type, |
| 543 | AttributeList *Attr) { |
| 544 | // If this type is already address space qualified, reject it. |
| 545 | // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers |
| 546 | // for two or more different address spaces." |
| 547 | if (Type.getAddressSpace()) { |
| 548 | Diag(Attr->getLoc(), diag::err_attribute_address_multiple_qualifiers); |
| 549 | return Type; |
| 550 | } |
| 551 | |
| 552 | // Check the attribute arguments. |
| 553 | if (Attr->getNumArgs() != 1) { |
| 554 | Diag(Attr->getLoc(), diag::err_attribute_wrong_number_arguments, |
| 555 | std::string("1")); |
| 556 | return Type; |
| 557 | } |
| 558 | Expr *ASArgExpr = static_cast<Expr *>(Attr->getArg(0)); |
| 559 | llvm::APSInt addrSpace(32); |
| 560 | if (!ASArgExpr->isIntegerConstantExpr(addrSpace, Context)) { |
| 561 | Diag(Attr->getLoc(), diag::err_attribute_address_space_not_int, |
| 562 | ASArgExpr->getSourceRange()); |
| 563 | return Type; |
| 564 | } |
| 565 | |
| 566 | unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue()); |
| 567 | return Context.getASQualType(Type, ASIdx); |
| 568 | } |
| 569 | |