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