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