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" |
Douglas Gregor | a8f32e0 | 2009-10-06 17:59:45 +0000 | [diff] [blame] | 16 | #include "clang/AST/CXXInheritance.h" |
Steve Naroff | 980e508 | 2007-10-01 19:00:59 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclTemplate.h" |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 19 | #include "clang/AST/TypeLoc.h" |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 20 | #include "clang/AST/TypeLocVisitor.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 21 | #include "clang/AST/Expr.h" |
Anders Carlsson | 91a0cc9 | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 22 | #include "clang/Basic/PartialDiagnostic.h" |
Daniel Dunbar | e4858a6 | 2008-08-11 03:45:03 +0000 | [diff] [blame] | 23 | #include "clang/Parse/DeclSpec.h" |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallPtrSet.h" |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 25 | #include "llvm/Support/ErrorHandling.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 26 | using namespace clang; |
| 27 | |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 28 | /// \brief Perform adjustment on the parameter type of a function. |
| 29 | /// |
| 30 | /// This routine adjusts the given parameter type @p T to the actual |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 31 | /// parameter type used by semantic analysis (C99 6.7.5.3p[7,8], |
| 32 | /// C++ [dcl.fct]p3). The adjusted parameter type is returned. |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 33 | QualType Sema::adjustParameterType(QualType T) { |
| 34 | // C99 6.7.5.3p7: |
Chris Lattner | 778ed74 | 2009-10-25 17:36:50 +0000 | [diff] [blame] | 35 | // A declaration of a parameter as "array of type" shall be |
| 36 | // adjusted to "qualified pointer to type", where the type |
| 37 | // qualifiers (if any) are those specified within the [ and ] of |
| 38 | // the array type derivation. |
| 39 | if (T->isArrayType()) |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 40 | return Context.getArrayDecayedType(T); |
Chris Lattner | 778ed74 | 2009-10-25 17:36:50 +0000 | [diff] [blame] | 41 | |
| 42 | // C99 6.7.5.3p8: |
| 43 | // A declaration of a parameter as "function returning type" |
| 44 | // shall be adjusted to "pointer to function returning type", as |
| 45 | // in 6.3.2.1. |
| 46 | if (T->isFunctionType()) |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 47 | return Context.getPointerType(T); |
| 48 | |
| 49 | return T; |
| 50 | } |
| 51 | |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 52 | |
| 53 | |
| 54 | /// isOmittedBlockReturnType - Return true if this declarator is missing a |
| 55 | /// return type because this is a omitted return type on a block literal. |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 56 | static bool isOmittedBlockReturnType(const Declarator &D) { |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 57 | if (D.getContext() != Declarator::BlockLiteralContext || |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 58 | D.getDeclSpec().hasTypeSpecifier()) |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 59 | return false; |
| 60 | |
| 61 | if (D.getNumTypeObjects() == 0) |
Chris Lattner | a64ef0a | 2009-10-25 22:09:09 +0000 | [diff] [blame] | 62 | return true; // ^{ ... } |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 63 | |
| 64 | if (D.getNumTypeObjects() == 1 && |
| 65 | D.getTypeObject(0).Kind == DeclaratorChunk::Function) |
Chris Lattner | a64ef0a | 2009-10-25 22:09:09 +0000 | [diff] [blame] | 66 | return true; // ^(int X, float Y) { ... } |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 67 | |
| 68 | return false; |
| 69 | } |
| 70 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 71 | typedef std::pair<const AttributeList*,QualType> DelayedAttribute; |
| 72 | typedef llvm::SmallVectorImpl<DelayedAttribute> DelayedAttributeSet; |
| 73 | |
| 74 | static void ProcessTypeAttributeList(Sema &S, QualType &Type, |
Charles Davis | 328ce34 | 2010-02-24 02:27:18 +0000 | [diff] [blame] | 75 | bool IsDeclSpec, |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 76 | const AttributeList *Attrs, |
| 77 | DelayedAttributeSet &DelayedFnAttrs); |
| 78 | static bool ProcessFnAttr(Sema &S, QualType &Type, const AttributeList &Attr); |
| 79 | |
| 80 | static void ProcessDelayedFnAttrs(Sema &S, QualType &Type, |
| 81 | DelayedAttributeSet &Attrs) { |
| 82 | for (DelayedAttributeSet::iterator I = Attrs.begin(), |
| 83 | E = Attrs.end(); I != E; ++I) |
| 84 | if (ProcessFnAttr(S, Type, *I->first)) |
| 85 | S.Diag(I->first->getLoc(), diag::warn_function_attribute_wrong_type) |
| 86 | << I->first->getName() << I->second; |
| 87 | Attrs.clear(); |
| 88 | } |
| 89 | |
| 90 | static void DiagnoseDelayedFnAttrs(Sema &S, DelayedAttributeSet &Attrs) { |
| 91 | for (DelayedAttributeSet::iterator I = Attrs.begin(), |
| 92 | E = Attrs.end(); I != E; ++I) { |
| 93 | S.Diag(I->first->getLoc(), diag::warn_function_attribute_wrong_type) |
| 94 | << I->first->getName() << I->second; |
| 95 | } |
| 96 | Attrs.clear(); |
| 97 | } |
| 98 | |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 99 | /// \brief Convert the specified declspec to the appropriate type |
| 100 | /// object. |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 101 | /// \param D the declarator containing the declaration specifier. |
Chris Lattner | 5153ee6 | 2009-04-25 08:47:54 +0000 | [diff] [blame] | 102 | /// \returns The type described by the declaration specifiers. This function |
| 103 | /// never returns null. |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 104 | static QualType ConvertDeclSpecToType(Sema &TheSema, |
| 105 | Declarator &TheDeclarator, |
| 106 | DelayedAttributeSet &Delayed) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 107 | // FIXME: Should move the logic from DeclSpec::Finish to here for validity |
| 108 | // checking. |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 109 | const DeclSpec &DS = TheDeclarator.getDeclSpec(); |
| 110 | SourceLocation DeclLoc = TheDeclarator.getIdentifierLoc(); |
| 111 | if (DeclLoc.isInvalid()) |
| 112 | DeclLoc = DS.getSourceRange().getBegin(); |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame] | 113 | |
| 114 | ASTContext &Context = TheSema.Context; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 115 | |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 116 | QualType Result; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 117 | switch (DS.getTypeSpecType()) { |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 118 | case DeclSpec::TST_void: |
| 119 | Result = Context.VoidTy; |
| 120 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 121 | case DeclSpec::TST_char: |
| 122 | if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified) |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 123 | Result = Context.CharTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 124 | else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 125 | Result = Context.SignedCharTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 126 | else { |
| 127 | assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned && |
| 128 | "Unknown TSS value"); |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 129 | Result = Context.UnsignedCharTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 130 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 131 | break; |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 132 | case DeclSpec::TST_wchar: |
| 133 | if (DS.getTypeSpecSign() == DeclSpec::TSS_unspecified) |
| 134 | Result = Context.WCharTy; |
| 135 | else if (DS.getTypeSpecSign() == DeclSpec::TSS_signed) { |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame] | 136 | TheSema.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec) |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 137 | << DS.getSpecifierName(DS.getTypeSpecType()); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 138 | Result = Context.getSignedWCharType(); |
| 139 | } else { |
| 140 | assert(DS.getTypeSpecSign() == DeclSpec::TSS_unsigned && |
| 141 | "Unknown TSS value"); |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame] | 142 | TheSema.Diag(DS.getTypeSpecSignLoc(), diag::ext_invalid_sign_spec) |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 143 | << DS.getSpecifierName(DS.getTypeSpecType()); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 144 | Result = Context.getUnsignedWCharType(); |
| 145 | } |
| 146 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 147 | case DeclSpec::TST_char16: |
| 148 | assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified && |
| 149 | "Unknown TSS value"); |
| 150 | Result = Context.Char16Ty; |
| 151 | break; |
| 152 | case DeclSpec::TST_char32: |
| 153 | assert(DS.getTypeSpecSign() == DeclSpec::TSS_unspecified && |
| 154 | "Unknown TSS value"); |
| 155 | Result = Context.Char32Ty; |
| 156 | break; |
Chris Lattner | d658b56 | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 157 | case DeclSpec::TST_unspecified: |
Chris Lattner | 62f5f7f | 2008-07-26 00:46:50 +0000 | [diff] [blame] | 158 | // "<proto1,proto2>" is an objc qualified ID with a missing id. |
Chris Lattner | 097e916 | 2008-10-20 02:01:50 +0000 | [diff] [blame] | 159 | if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 160 | Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy, |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 161 | (ObjCProtocolDecl**)PQ, |
Steve Naroff | 683087f | 2009-06-29 16:22:52 +0000 | [diff] [blame] | 162 | DS.getNumProtocolQualifiers()); |
Chris Lattner | 62f5f7f | 2008-07-26 00:46:50 +0000 | [diff] [blame] | 163 | break; |
| 164 | } |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 165 | |
| 166 | // If this is a missing declspec in a block literal return context, then it |
| 167 | // is inferred from the return statements inside the block. |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 168 | if (isOmittedBlockReturnType(TheDeclarator)) { |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 169 | Result = Context.DependentTy; |
| 170 | break; |
| 171 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 172 | |
Chris Lattner | d658b56 | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 173 | // Unspecified typespec defaults to int in C90. However, the C90 grammar |
| 174 | // [C90 6.5] only allows a decl-spec if there was *some* type-specifier, |
| 175 | // type-qualifier, or storage-class-specifier. If not, emit an extwarn. |
| 176 | // Note that the one exception to this is function definitions, which are |
| 177 | // allowed to be completely missing a declspec. This is handled in the |
| 178 | // parser already though by it pretending to have seen an 'int' in this |
| 179 | // case. |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame] | 180 | if (TheSema.getLangOptions().ImplicitInt) { |
Chris Lattner | 35d276f | 2009-02-27 18:53:28 +0000 | [diff] [blame] | 181 | // In C89 mode, we only warn if there is a completely missing declspec |
| 182 | // when one is not allowed. |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 183 | if (DS.isEmpty()) { |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame] | 184 | TheSema.Diag(DeclLoc, diag::ext_missing_declspec) |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 185 | << DS.getSourceRange() |
Chris Lattner | 173144a | 2009-02-27 22:31:56 +0000 | [diff] [blame] | 186 | << CodeModificationHint::CreateInsertion(DS.getSourceRange().getBegin(), |
| 187 | "int"); |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 188 | } |
Douglas Gregor | 4310f4e | 2009-02-16 22:38:20 +0000 | [diff] [blame] | 189 | } else if (!DS.hasTypeSpecifier()) { |
Chris Lattner | d658b56 | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 190 | // C99 and C++ require a type specifier. For example, C99 6.7.2p2 says: |
| 191 | // "At least one type specifier shall be given in the declaration |
| 192 | // specifiers in each declaration, and in the specifier-qualifier list in |
| 193 | // each struct declaration and type name." |
Douglas Gregor | 4310f4e | 2009-02-16 22:38:20 +0000 | [diff] [blame] | 194 | // FIXME: Does Microsoft really have the implicit int extension in C++? |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame] | 195 | if (TheSema.getLangOptions().CPlusPlus && |
| 196 | !TheSema.getLangOptions().Microsoft) { |
| 197 | TheSema.Diag(DeclLoc, diag::err_missing_type_specifier) |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 198 | << DS.getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 199 | |
Chris Lattner | b78d833 | 2009-06-26 04:45:06 +0000 | [diff] [blame] | 200 | // When this occurs in C++ code, often something is very broken with the |
| 201 | // value being declared, poison it as invalid so we don't get chains of |
| 202 | // errors. |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 203 | TheDeclarator.setInvalidType(true); |
Chris Lattner | b78d833 | 2009-06-26 04:45:06 +0000 | [diff] [blame] | 204 | } else { |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame] | 205 | TheSema.Diag(DeclLoc, diag::ext_missing_type_specifier) |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 206 | << DS.getSourceRange(); |
Chris Lattner | b78d833 | 2009-06-26 04:45:06 +0000 | [diff] [blame] | 207 | } |
Chris Lattner | d658b56 | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 208 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 209 | |
| 210 | // FALL THROUGH. |
Chris Lattner | 3cbc38b | 2007-08-21 17:02:28 +0000 | [diff] [blame] | 211 | case DeclSpec::TST_int: { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 212 | if (DS.getTypeSpecSign() != DeclSpec::TSS_unsigned) { |
| 213 | switch (DS.getTypeSpecWidth()) { |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 214 | case DeclSpec::TSW_unspecified: Result = Context.IntTy; break; |
| 215 | case DeclSpec::TSW_short: Result = Context.ShortTy; break; |
| 216 | case DeclSpec::TSW_long: Result = Context.LongTy; break; |
Chris Lattner | 311157f | 2009-10-25 18:25:04 +0000 | [diff] [blame] | 217 | case DeclSpec::TSW_longlong: |
| 218 | Result = Context.LongLongTy; |
| 219 | |
| 220 | // long long is a C99 feature. |
| 221 | if (!TheSema.getLangOptions().C99 && |
| 222 | !TheSema.getLangOptions().CPlusPlus0x) |
| 223 | TheSema.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong); |
| 224 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 225 | } |
| 226 | } else { |
| 227 | switch (DS.getTypeSpecWidth()) { |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 228 | case DeclSpec::TSW_unspecified: Result = Context.UnsignedIntTy; break; |
| 229 | case DeclSpec::TSW_short: Result = Context.UnsignedShortTy; break; |
| 230 | case DeclSpec::TSW_long: Result = Context.UnsignedLongTy; break; |
Chris Lattner | 311157f | 2009-10-25 18:25:04 +0000 | [diff] [blame] | 231 | case DeclSpec::TSW_longlong: |
| 232 | Result = Context.UnsignedLongLongTy; |
| 233 | |
| 234 | // long long is a C99 feature. |
| 235 | if (!TheSema.getLangOptions().C99 && |
| 236 | !TheSema.getLangOptions().CPlusPlus0x) |
| 237 | TheSema.Diag(DS.getTypeSpecWidthLoc(), diag::ext_longlong); |
| 238 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 239 | } |
| 240 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 241 | break; |
Chris Lattner | 3cbc38b | 2007-08-21 17:02:28 +0000 | [diff] [blame] | 242 | } |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 243 | case DeclSpec::TST_float: Result = Context.FloatTy; break; |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 244 | case DeclSpec::TST_double: |
| 245 | if (DS.getTypeSpecWidth() == DeclSpec::TSW_long) |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 246 | Result = Context.LongDoubleTy; |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 247 | else |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 248 | Result = Context.DoubleTy; |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 249 | break; |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 250 | case DeclSpec::TST_bool: Result = Context.BoolTy; break; // _Bool or bool |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 251 | case DeclSpec::TST_decimal32: // _Decimal32 |
| 252 | case DeclSpec::TST_decimal64: // _Decimal64 |
| 253 | case DeclSpec::TST_decimal128: // _Decimal128 |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame] | 254 | TheSema.Diag(DS.getTypeSpecTypeLoc(), diag::err_decimal_unsupported); |
Chris Lattner | 8f12f65 | 2009-05-13 05:02:08 +0000 | [diff] [blame] | 255 | Result = Context.IntTy; |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 256 | TheDeclarator.setInvalidType(true); |
Chris Lattner | 8f12f65 | 2009-05-13 05:02:08 +0000 | [diff] [blame] | 257 | break; |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 258 | case DeclSpec::TST_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 259 | case DeclSpec::TST_enum: |
| 260 | case DeclSpec::TST_union: |
| 261 | case DeclSpec::TST_struct: { |
Douglas Gregor | c7621a6 | 2009-11-05 20:54:04 +0000 | [diff] [blame] | 262 | TypeDecl *D |
| 263 | = dyn_cast_or_null<TypeDecl>(static_cast<Decl *>(DS.getTypeRep())); |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 264 | if (!D) { |
| 265 | // This can happen in C++ with ambiguous lookups. |
| 266 | Result = Context.IntTy; |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 267 | TheDeclarator.setInvalidType(true); |
John McCall | 6e24726 | 2009-10-10 05:48:19 +0000 | [diff] [blame] | 268 | break; |
| 269 | } |
| 270 | |
Chris Lattner | a64ef0a | 2009-10-25 22:09:09 +0000 | [diff] [blame] | 271 | // If the type is deprecated or unavailable, diagnose it. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 272 | TheSema.DiagnoseUseOfDecl(D, DS.getTypeSpecTypeLoc()); |
Chris Lattner | a64ef0a | 2009-10-25 22:09:09 +0000 | [diff] [blame] | 273 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 274 | assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 && |
Chris Lattner | a64ef0a | 2009-10-25 22:09:09 +0000 | [diff] [blame] | 275 | DS.getTypeSpecSign() == 0 && "No qualifiers on tag names!"); |
| 276 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 277 | // TypeQuals handled by caller. |
Chris Lattner | a64ef0a | 2009-10-25 22:09:09 +0000 | [diff] [blame] | 278 | Result = Context.getTypeDeclType(D); |
John McCall | 2191b20 | 2009-09-05 06:31:47 +0000 | [diff] [blame] | 279 | |
| 280 | // In C++, make an ElaboratedType. |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame] | 281 | if (TheSema.getLangOptions().CPlusPlus) { |
John McCall | 2191b20 | 2009-09-05 06:31:47 +0000 | [diff] [blame] | 282 | TagDecl::TagKind Tag |
| 283 | = TagDecl::getTagKindForTypeSpec(DS.getTypeSpecType()); |
| 284 | Result = Context.getElaboratedType(Result, Tag); |
| 285 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 286 | |
Chris Lattner | 5153ee6 | 2009-04-25 08:47:54 +0000 | [diff] [blame] | 287 | if (D->isInvalidDecl()) |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 288 | TheDeclarator.setInvalidType(true); |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 289 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 290 | } |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 291 | case DeclSpec::TST_typename: { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 292 | assert(DS.getTypeSpecWidth() == 0 && DS.getTypeSpecComplex() == 0 && |
| 293 | DS.getTypeSpecSign() == 0 && |
| 294 | "Can't handle qualifiers on typedef names yet!"); |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame] | 295 | Result = TheSema.GetTypeFromParser(DS.getTypeRep()); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 296 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 297 | if (DeclSpec::ProtocolQualifierListTy PQ = DS.getProtocolQualifiers()) { |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 298 | if (const ObjCInterfaceType * |
| 299 | Interface = Result->getAs<ObjCInterfaceType>()) { |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 300 | // It would be nice if protocol qualifiers were only stored with the |
| 301 | // ObjCObjectPointerType. Unfortunately, this isn't possible due |
| 302 | // to the following typedef idiom (which is uncommon, but allowed): |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 303 | // |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 304 | // typedef Foo<P> T; |
| 305 | // static void func() { |
| 306 | // Foo<P> *yy; |
| 307 | // T *zz; |
| 308 | // } |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 309 | Result = Context.getObjCInterfaceType(Interface->getDecl(), |
| 310 | (ObjCProtocolDecl**)PQ, |
| 311 | DS.getNumProtocolQualifiers()); |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 312 | } else if (Result->isObjCIdType()) |
Chris Lattner | ae4da61 | 2008-07-26 01:53:50 +0000 | [diff] [blame] | 313 | // id<protocol-list> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 314 | Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinIdTy, |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 315 | (ObjCProtocolDecl**)PQ, DS.getNumProtocolQualifiers()); |
| 316 | else if (Result->isObjCClassType()) { |
Steve Naroff | 4262a07 | 2009-02-23 18:53:24 +0000 | [diff] [blame] | 317 | // Class<protocol-list> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 318 | Result = Context.getObjCObjectPointerType(Context.ObjCBuiltinClassTy, |
Steve Naroff | 470301b | 2009-07-22 16:07:01 +0000 | [diff] [blame] | 319 | (ObjCProtocolDecl**)PQ, DS.getNumProtocolQualifiers()); |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 320 | } else { |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame] | 321 | TheSema.Diag(DeclLoc, diag::err_invalid_protocol_qualifiers) |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 322 | << DS.getSourceRange(); |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 323 | TheDeclarator.setInvalidType(true); |
Chris Lattner | 3f84ad2 | 2009-04-22 05:27:59 +0000 | [diff] [blame] | 324 | } |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 325 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 326 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 327 | // TypeQuals handled by caller. |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 328 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 329 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 330 | case DeclSpec::TST_typeofType: |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 331 | // FIXME: Preserve type source info. |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame] | 332 | Result = TheSema.GetTypeFromParser(DS.getTypeRep()); |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 333 | assert(!Result.isNull() && "Didn't get a type for typeof?"); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 334 | // TypeQuals handled by caller. |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 335 | Result = Context.getTypeOfType(Result); |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 336 | break; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 337 | case DeclSpec::TST_typeofExpr: { |
| 338 | Expr *E = static_cast<Expr *>(DS.getTypeRep()); |
| 339 | assert(E && "Didn't get an expression for typeof?"); |
| 340 | // TypeQuals handled by caller. |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 341 | Result = TheSema.BuildTypeofExprType(E); |
| 342 | if (Result.isNull()) { |
| 343 | Result = Context.IntTy; |
| 344 | TheDeclarator.setInvalidType(true); |
| 345 | } |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 346 | break; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 347 | } |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 348 | case DeclSpec::TST_decltype: { |
| 349 | Expr *E = static_cast<Expr *>(DS.getTypeRep()); |
| 350 | assert(E && "Didn't get an expression for decltype?"); |
| 351 | // TypeQuals handled by caller. |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame] | 352 | Result = TheSema.BuildDecltypeType(E); |
Anders Carlsson | af017e6 | 2009-06-29 22:58:55 +0000 | [diff] [blame] | 353 | if (Result.isNull()) { |
| 354 | Result = Context.IntTy; |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 355 | TheDeclarator.setInvalidType(true); |
Anders Carlsson | af017e6 | 2009-06-29 22:58:55 +0000 | [diff] [blame] | 356 | } |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 357 | break; |
| 358 | } |
Anders Carlsson | e89d159 | 2009-06-26 18:41:36 +0000 | [diff] [blame] | 359 | case DeclSpec::TST_auto: { |
| 360 | // TypeQuals handled by caller. |
| 361 | Result = Context.UndeducedAutoTy; |
| 362 | break; |
| 363 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 364 | |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 365 | case DeclSpec::TST_error: |
Chris Lattner | 5153ee6 | 2009-04-25 08:47:54 +0000 | [diff] [blame] | 366 | Result = Context.IntTy; |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 367 | TheDeclarator.setInvalidType(true); |
Chris Lattner | 5153ee6 | 2009-04-25 08:47:54 +0000 | [diff] [blame] | 368 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 369 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 370 | |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 371 | // Handle complex types. |
Douglas Gregor | f244cd7 | 2009-02-14 21:06:05 +0000 | [diff] [blame] | 372 | if (DS.getTypeSpecComplex() == DeclSpec::TSC_complex) { |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame] | 373 | if (TheSema.getLangOptions().Freestanding) |
| 374 | TheSema.Diag(DS.getTypeSpecComplexLoc(), diag::ext_freestanding_complex); |
Chris Lattner | fab5b45 | 2008-02-20 23:53:49 +0000 | [diff] [blame] | 375 | Result = Context.getComplexType(Result); |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 376 | } else if (DS.isTypeAltiVecVector()) { |
| 377 | unsigned typeSize = static_cast<unsigned>(Context.getTypeSize(Result)); |
| 378 | assert(typeSize > 0 && "type size for vector must be greater than 0 bits"); |
| 379 | Result = Context.getVectorType(Result, 128/typeSize, true, |
| 380 | DS.isTypeAltiVecPixel()); |
Douglas Gregor | f244cd7 | 2009-02-14 21:06:05 +0000 | [diff] [blame] | 381 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 382 | |
Chris Lattner | 958858e | 2008-02-20 21:40:32 +0000 | [diff] [blame] | 383 | assert(DS.getTypeSpecComplex() != DeclSpec::TSC_imaginary && |
| 384 | "FIXME: imaginary types not supported yet!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 385 | |
Chris Lattner | 38d8b98 | 2008-02-20 22:04:11 +0000 | [diff] [blame] | 386 | // See if there are any attributes on the declspec that apply to the type (as |
| 387 | // opposed to the decl). |
Chris Lattner | fca0ddd | 2008-06-26 06:27:57 +0000 | [diff] [blame] | 388 | if (const AttributeList *AL = DS.getAttributes()) |
Charles Davis | 328ce34 | 2010-02-24 02:27:18 +0000 | [diff] [blame] | 389 | ProcessTypeAttributeList(TheSema, Result, true, AL, Delayed); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 390 | |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 391 | // Apply const/volatile/restrict qualifiers to T. |
| 392 | if (unsigned TypeQuals = DS.getTypeQualifiers()) { |
| 393 | |
| 394 | // Enforce C99 6.7.3p2: "Types other than pointer types derived from object |
| 395 | // or incomplete types shall not be restrict-qualified." C++ also allows |
| 396 | // restrict-qualified references. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 397 | if (TypeQuals & DeclSpec::TQ_restrict) { |
Fariborz Jahanian | 2b5ff1a | 2009-12-07 18:08:58 +0000 | [diff] [blame] | 398 | if (Result->isAnyPointerType() || Result->isReferenceType()) { |
| 399 | QualType EltTy; |
| 400 | if (Result->isObjCObjectPointerType()) |
| 401 | EltTy = Result; |
| 402 | else |
| 403 | EltTy = Result->isPointerType() ? |
| 404 | Result->getAs<PointerType>()->getPointeeType() : |
| 405 | Result->getAs<ReferenceType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 406 | |
Douglas Gregor | bad0e65 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 407 | // If we have a pointer or reference, the pointee must have an object |
Chris Lattner | bdcd637 | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 408 | // incomplete type. |
| 409 | if (!EltTy->isIncompleteOrObjectType()) { |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame] | 410 | TheSema.Diag(DS.getRestrictSpecLoc(), |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 411 | diag::err_typecheck_invalid_restrict_invalid_pointee) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 412 | << EltTy << DS.getSourceRange(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 413 | TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier. |
Chris Lattner | bdcd637 | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 414 | } |
| 415 | } else { |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame] | 416 | TheSema.Diag(DS.getRestrictSpecLoc(), |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 417 | diag::err_typecheck_invalid_restrict_not_pointer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 418 | << Result << DS.getSourceRange(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 419 | TypeQuals &= ~DeclSpec::TQ_restrict; // Remove the restrict qualifier. |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 420 | } |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 421 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 422 | |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 423 | // Warn about CV qualifiers on functions: C99 6.7.3p8: "If the specification |
| 424 | // of a function type includes any type qualifiers, the behavior is |
| 425 | // undefined." |
| 426 | if (Result->isFunctionType() && TypeQuals) { |
| 427 | // Get some location to point at, either the C or V location. |
| 428 | SourceLocation Loc; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 429 | if (TypeQuals & DeclSpec::TQ_const) |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 430 | Loc = DS.getConstSpecLoc(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 431 | else if (TypeQuals & DeclSpec::TQ_volatile) |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 432 | Loc = DS.getVolatileSpecLoc(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 433 | else { |
| 434 | assert((TypeQuals & DeclSpec::TQ_restrict) && |
| 435 | "Has CVR quals but not C, V, or R?"); |
| 436 | Loc = DS.getRestrictSpecLoc(); |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 437 | } |
Chris Lattner | 1564e39 | 2009-10-25 18:07:27 +0000 | [diff] [blame] | 438 | TheSema.Diag(Loc, diag::warn_typecheck_function_qualifiers) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 439 | << Result << DS.getSourceRange(); |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 440 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 441 | |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 442 | // C++ [dcl.ref]p1: |
| 443 | // Cv-qualified references are ill-formed except when the |
| 444 | // cv-qualifiers are introduced through the use of a typedef |
| 445 | // (7.1.3) or of a template type argument (14.3), in which |
| 446 | // case the cv-qualifiers are ignored. |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 447 | // FIXME: Shouldn't we be checking SCS_typedef here? |
| 448 | if (DS.getTypeSpecType() == DeclSpec::TST_typename && |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 449 | TypeQuals && Result->isReferenceType()) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 450 | TypeQuals &= ~DeclSpec::TQ_const; |
| 451 | TypeQuals &= ~DeclSpec::TQ_volatile; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 452 | } |
| 453 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 454 | Qualifiers Quals = Qualifiers::fromCVRMask(TypeQuals); |
| 455 | Result = Context.getQualifiedType(Result, Quals); |
Chris Lattner | 96b77fc | 2008-04-02 06:50:17 +0000 | [diff] [blame] | 456 | } |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 457 | |
Chris Lattner | f1d705c | 2008-02-21 01:07:18 +0000 | [diff] [blame] | 458 | return Result; |
| 459 | } |
| 460 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 461 | static std::string getPrintableNameForEntity(DeclarationName Entity) { |
| 462 | if (Entity) |
| 463 | return Entity.getAsString(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 464 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 465 | return "type name"; |
| 466 | } |
| 467 | |
| 468 | /// \brief Build a pointer type. |
| 469 | /// |
| 470 | /// \param T The type to which we'll be building a pointer. |
| 471 | /// |
| 472 | /// \param Quals The cvr-qualifiers to be applied to the pointer type. |
| 473 | /// |
| 474 | /// \param Loc The location of the entity whose type involves this |
| 475 | /// pointer type or, if there is no such entity, the location of the |
| 476 | /// type that will have pointer type. |
| 477 | /// |
| 478 | /// \param Entity The name of the entity that involves the pointer |
| 479 | /// type, if known. |
| 480 | /// |
| 481 | /// \returns A suitable pointer type, if there are no |
| 482 | /// errors. Otherwise, returns a NULL type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 483 | QualType Sema::BuildPointerType(QualType T, unsigned Quals, |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 484 | SourceLocation Loc, DeclarationName Entity) { |
| 485 | if (T->isReferenceType()) { |
| 486 | // C++ 8.3.2p4: There shall be no ... pointers to references ... |
| 487 | Diag(Loc, diag::err_illegal_decl_pointer_to_reference) |
John McCall | ac40605 | 2009-10-30 00:37:20 +0000 | [diff] [blame] | 488 | << getPrintableNameForEntity(Entity) << T; |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 489 | return QualType(); |
| 490 | } |
| 491 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 492 | Qualifiers Qs = Qualifiers::fromCVRMask(Quals); |
| 493 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 494 | // Enforce C99 6.7.3p2: "Types other than pointer types derived from |
| 495 | // object or incomplete types shall not be restrict-qualified." |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 496 | if (Qs.hasRestrict() && !T->isIncompleteOrObjectType()) { |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 497 | Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee) |
| 498 | << T; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 499 | Qs.removeRestrict(); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | // Build the pointer type. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 503 | return Context.getQualifiedType(Context.getPointerType(T), Qs); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | /// \brief Build a reference type. |
| 507 | /// |
| 508 | /// \param T The type to which we'll be building a reference. |
| 509 | /// |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 510 | /// \param CVR The cvr-qualifiers to be applied to the reference type. |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 511 | /// |
| 512 | /// \param Loc The location of the entity whose type involves this |
| 513 | /// reference type or, if there is no such entity, the location of the |
| 514 | /// type that will have reference type. |
| 515 | /// |
| 516 | /// \param Entity The name of the entity that involves the reference |
| 517 | /// type, if known. |
| 518 | /// |
| 519 | /// \returns A suitable reference type, if there are no |
| 520 | /// errors. Otherwise, returns a NULL type. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 521 | QualType Sema::BuildReferenceType(QualType T, bool SpelledAsLValue, |
| 522 | unsigned CVR, SourceLocation Loc, |
| 523 | DeclarationName Entity) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 524 | Qualifiers Quals = Qualifiers::fromCVRMask(CVR); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 525 | |
| 526 | bool LValueRef = SpelledAsLValue || T->getAs<LValueReferenceType>(); |
| 527 | |
| 528 | // C++0x [dcl.typedef]p9: If a typedef TD names a type that is a |
| 529 | // reference to a type T, and attempt to create the type "lvalue |
| 530 | // reference to cv TD" creates the type "lvalue reference to T". |
| 531 | // We use the qualifiers (restrict or none) of the original reference, |
| 532 | // not the new ones. This is consistent with GCC. |
| 533 | |
| 534 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 535 | // |
| 536 | // According to C++ DR 106, references to references are only |
| 537 | // diagnosed when they are written directly (e.g., "int & &"), |
| 538 | // but not when they happen via a typedef: |
| 539 | // |
| 540 | // typedef int& intref; |
| 541 | // typedef intref& intref2; |
| 542 | // |
| 543 | // Parser::ParseDeclaratorInternal diagnoses the case where |
| 544 | // references are written directly; here, we handle the |
| 545 | // collapsing of references-to-references as described in C++ |
| 546 | // DR 106 and amended by C++ DR 540. |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 547 | |
| 548 | // C++ [dcl.ref]p1: |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 549 | // A declarator that specifies the type "reference to cv void" |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 550 | // is ill-formed. |
| 551 | if (T->isVoidType()) { |
| 552 | Diag(Loc, diag::err_reference_to_void); |
| 553 | return QualType(); |
| 554 | } |
| 555 | |
| 556 | // Enforce C99 6.7.3p2: "Types other than pointer types derived from |
| 557 | // object or incomplete types shall not be restrict-qualified." |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 558 | if (Quals.hasRestrict() && !T->isIncompleteOrObjectType()) { |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 559 | Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee) |
| 560 | << T; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 561 | Quals.removeRestrict(); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | // C++ [dcl.ref]p1: |
| 565 | // [...] Cv-qualified references are ill-formed except when the |
| 566 | // cv-qualifiers are introduced through the use of a typedef |
| 567 | // (7.1.3) or of a template type argument (14.3), in which case |
| 568 | // the cv-qualifiers are ignored. |
| 569 | // |
| 570 | // We diagnose extraneous cv-qualifiers for the non-typedef, |
| 571 | // non-template type argument case within the parser. Here, we just |
| 572 | // ignore any extraneous cv-qualifiers. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 573 | Quals.removeConst(); |
| 574 | Quals.removeVolatile(); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 575 | |
| 576 | // Handle restrict on references. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 577 | if (LValueRef) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 578 | return Context.getQualifiedType( |
| 579 | Context.getLValueReferenceType(T, SpelledAsLValue), Quals); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 580 | return Context.getQualifiedType(Context.getRValueReferenceType(T), Quals); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | /// \brief Build an array type. |
| 584 | /// |
| 585 | /// \param T The type of each element in the array. |
| 586 | /// |
| 587 | /// \param ASM C99 array size modifier (e.g., '*', 'static'). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 588 | /// |
| 589 | /// \param ArraySize Expression describing the size of the array. |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 590 | /// |
| 591 | /// \param Quals The cvr-qualifiers to be applied to the array's |
| 592 | /// element type. |
| 593 | /// |
| 594 | /// \param Loc The location of the entity whose type involves this |
| 595 | /// array type or, if there is no such entity, the location of the |
| 596 | /// type that will have array type. |
| 597 | /// |
| 598 | /// \param Entity The name of the entity that involves the array |
| 599 | /// type, if known. |
| 600 | /// |
| 601 | /// \returns A suitable array type, if there are no errors. Otherwise, |
| 602 | /// returns a NULL type. |
| 603 | QualType Sema::BuildArrayType(QualType T, ArrayType::ArraySizeModifier ASM, |
| 604 | Expr *ArraySize, unsigned Quals, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 605 | SourceRange Brackets, DeclarationName Entity) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 606 | |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 607 | SourceLocation Loc = Brackets.getBegin(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 608 | // C99 6.7.5.2p1: If the element type is an incomplete or function type, |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 609 | // reject it (e.g. void ary[7], struct foo ary[7], void ary[7]()) |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 610 | // Not in C++, though. There we only dislike void. |
| 611 | if (getLangOptions().CPlusPlus) { |
| 612 | if (T->isVoidType()) { |
| 613 | Diag(Loc, diag::err_illegal_decl_array_incomplete_type) << T; |
| 614 | return QualType(); |
| 615 | } |
| 616 | } else { |
| 617 | if (RequireCompleteType(Loc, T, |
| 618 | diag::err_illegal_decl_array_incomplete_type)) |
| 619 | return QualType(); |
| 620 | } |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 621 | |
| 622 | if (T->isFunctionType()) { |
| 623 | Diag(Loc, diag::err_illegal_decl_array_of_functions) |
John McCall | ac40605 | 2009-10-30 00:37:20 +0000 | [diff] [blame] | 624 | << getPrintableNameForEntity(Entity) << T; |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 625 | return QualType(); |
| 626 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 627 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 628 | // C++ 8.3.2p4: There shall be no ... arrays of references ... |
| 629 | if (T->isReferenceType()) { |
| 630 | Diag(Loc, diag::err_illegal_decl_array_of_references) |
John McCall | ac40605 | 2009-10-30 00:37:20 +0000 | [diff] [blame] | 631 | << getPrintableNameForEntity(Entity) << T; |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 632 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 633 | } |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 634 | |
Anders Carlsson | e7cf07d | 2009-06-26 19:33:28 +0000 | [diff] [blame] | 635 | if (Context.getCanonicalType(T) == Context.UndeducedAutoTy) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 636 | Diag(Loc, diag::err_illegal_decl_array_of_auto) |
Anders Carlsson | e7cf07d | 2009-06-26 19:33:28 +0000 | [diff] [blame] | 637 | << getPrintableNameForEntity(Entity); |
| 638 | return QualType(); |
| 639 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 640 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 641 | if (const RecordType *EltTy = T->getAs<RecordType>()) { |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 642 | // If the element type is a struct or union that contains a variadic |
| 643 | // array, accept it as a GNU extension: C99 6.7.2.1p2. |
| 644 | if (EltTy->getDecl()->hasFlexibleArrayMember()) |
| 645 | Diag(Loc, diag::ext_flexible_array_in_array) << T; |
| 646 | } else if (T->isObjCInterfaceType()) { |
Chris Lattner | c7c11b1 | 2009-04-27 01:55:56 +0000 | [diff] [blame] | 647 | Diag(Loc, diag::err_objc_array_of_interfaces) << T; |
| 648 | return QualType(); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 649 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 650 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 651 | // C99 6.7.5.2p1: The size expression shall have integer type. |
| 652 | if (ArraySize && !ArraySize->isTypeDependent() && |
| 653 | !ArraySize->getType()->isIntegerType()) { |
| 654 | Diag(ArraySize->getLocStart(), diag::err_array_size_non_int) |
| 655 | << ArraySize->getType() << ArraySize->getSourceRange(); |
| 656 | ArraySize->Destroy(Context); |
| 657 | return QualType(); |
| 658 | } |
| 659 | llvm::APSInt ConstVal(32); |
| 660 | if (!ArraySize) { |
Eli Friedman | f91f5c8 | 2009-04-26 21:57:51 +0000 | [diff] [blame] | 661 | if (ASM == ArrayType::Star) |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 662 | T = Context.getVariableArrayType(T, 0, ASM, Quals, Brackets); |
Eli Friedman | f91f5c8 | 2009-04-26 21:57:51 +0000 | [diff] [blame] | 663 | else |
| 664 | T = Context.getIncompleteArrayType(T, ASM, Quals); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 665 | } else if (ArraySize->isValueDependent()) { |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 666 | T = Context.getDependentSizedArrayType(T, ArraySize, ASM, Quals, Brackets); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 667 | } else if (!ArraySize->isIntegerConstantExpr(ConstVal, Context) || |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 668 | (!T->isDependentType() && !T->isIncompleteType() && |
| 669 | !T->isConstantSizeType())) { |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 670 | // Per C99, a variable array is an array with either a non-constant |
| 671 | // size or an element type that has a non-constant-size |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 672 | T = Context.getVariableArrayType(T, ArraySize, ASM, Quals, Brackets); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 673 | } else { |
| 674 | // C99 6.7.5.2p1: If the expression is a constant expression, it shall |
| 675 | // have a value greater than zero. |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 676 | if (ConstVal.isSigned() && ConstVal.isNegative()) { |
| 677 | Diag(ArraySize->getLocStart(), |
| 678 | diag::err_typecheck_negative_array_size) |
| 679 | << ArraySize->getSourceRange(); |
| 680 | return QualType(); |
| 681 | } |
| 682 | if (ConstVal == 0) { |
| 683 | // GCC accepts zero sized static arrays. |
| 684 | Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size) |
| 685 | << ArraySize->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 686 | } |
John McCall | 46a617a | 2009-10-16 00:14:28 +0000 | [diff] [blame] | 687 | T = Context.getConstantArrayType(T, ConstVal, ASM, Quals); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 688 | } |
David Chisnall | af40776 | 2010-01-11 23:08:08 +0000 | [diff] [blame] | 689 | // If this is not C99, extwarn about VLA's and C99 array size modifiers. |
| 690 | if (!getLangOptions().C99) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 691 | if (ArraySize && !ArraySize->isTypeDependent() && |
| 692 | !ArraySize->isValueDependent() && |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 693 | !ArraySize->isIntegerConstantExpr(Context)) |
Douglas Gregor | 043cad2 | 2009-09-11 00:18:58 +0000 | [diff] [blame] | 694 | Diag(Loc, getLangOptions().CPlusPlus? diag::err_vla_cxx : diag::ext_vla); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 695 | else if (ASM != ArrayType::Normal || Quals != 0) |
Douglas Gregor | 043cad2 | 2009-09-11 00:18:58 +0000 | [diff] [blame] | 696 | Diag(Loc, |
| 697 | getLangOptions().CPlusPlus? diag::err_c99_array_usage_cxx |
| 698 | : diag::ext_c99_array_usage); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | return T; |
| 702 | } |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 703 | |
| 704 | /// \brief Build an ext-vector type. |
| 705 | /// |
| 706 | /// Run the required checks for the extended vector type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 707 | QualType Sema::BuildExtVectorType(QualType T, ExprArg ArraySize, |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 708 | SourceLocation AttrLoc) { |
| 709 | |
| 710 | Expr *Arg = (Expr *)ArraySize.get(); |
| 711 | |
| 712 | // unlike gcc's vector_size attribute, we do not allow vectors to be defined |
| 713 | // in conjunction with complex types (pointers, arrays, functions, etc.). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 714 | if (!T->isDependentType() && |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 715 | !T->isIntegerType() && !T->isRealFloatingType()) { |
| 716 | Diag(AttrLoc, diag::err_attribute_invalid_vector_type) << T; |
| 717 | return QualType(); |
| 718 | } |
| 719 | |
| 720 | if (!Arg->isTypeDependent() && !Arg->isValueDependent()) { |
| 721 | llvm::APSInt vecSize(32); |
| 722 | if (!Arg->isIntegerConstantExpr(vecSize, Context)) { |
| 723 | Diag(AttrLoc, diag::err_attribute_argument_not_int) |
| 724 | << "ext_vector_type" << Arg->getSourceRange(); |
| 725 | return QualType(); |
| 726 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 727 | |
| 728 | // unlike gcc's vector_size attribute, the size is specified as the |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 729 | // number of elements, not the number of bytes. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 730 | unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue()); |
| 731 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 732 | if (vectorSize == 0) { |
| 733 | Diag(AttrLoc, diag::err_attribute_zero_size) |
| 734 | << Arg->getSourceRange(); |
| 735 | return QualType(); |
| 736 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 737 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 738 | if (!T->isDependentType()) |
| 739 | return Context.getExtVectorType(T, vectorSize); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | return Context.getDependentSizedExtVectorType(T, ArraySize.takeAs<Expr>(), |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 743 | AttrLoc); |
| 744 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 745 | |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 746 | /// \brief Build a function type. |
| 747 | /// |
| 748 | /// This routine checks the function type according to C++ rules and |
| 749 | /// under the assumption that the result type and parameter types have |
| 750 | /// just been instantiated from a template. It therefore duplicates |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 751 | /// some of the behavior of GetTypeForDeclarator, but in a much |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 752 | /// simpler form that is only suitable for this narrow use case. |
| 753 | /// |
| 754 | /// \param T The return type of the function. |
| 755 | /// |
| 756 | /// \param ParamTypes The parameter types of the function. This array |
| 757 | /// will be modified to account for adjustments to the types of the |
| 758 | /// function parameters. |
| 759 | /// |
| 760 | /// \param NumParamTypes The number of parameter types in ParamTypes. |
| 761 | /// |
| 762 | /// \param Variadic Whether this is a variadic function type. |
| 763 | /// |
| 764 | /// \param Quals The cvr-qualifiers to be applied to the function type. |
| 765 | /// |
| 766 | /// \param Loc The location of the entity whose type involves this |
| 767 | /// function type or, if there is no such entity, the location of the |
| 768 | /// type that will have function type. |
| 769 | /// |
| 770 | /// \param Entity The name of the entity that involves the function |
| 771 | /// type, if known. |
| 772 | /// |
| 773 | /// \returns A suitable function type, if there are no |
| 774 | /// errors. Otherwise, returns a NULL type. |
| 775 | QualType Sema::BuildFunctionType(QualType T, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 776 | QualType *ParamTypes, |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 777 | unsigned NumParamTypes, |
| 778 | bool Variadic, unsigned Quals, |
| 779 | SourceLocation Loc, DeclarationName Entity) { |
| 780 | if (T->isArrayType() || T->isFunctionType()) { |
Douglas Gregor | 58408bc | 2010-01-11 18:46:21 +0000 | [diff] [blame] | 781 | Diag(Loc, diag::err_func_returning_array_function) |
| 782 | << T->isFunctionType() << T; |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 783 | return QualType(); |
| 784 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 785 | |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 786 | bool Invalid = false; |
| 787 | for (unsigned Idx = 0; Idx < NumParamTypes; ++Idx) { |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 788 | QualType ParamType = adjustParameterType(ParamTypes[Idx]); |
| 789 | if (ParamType->isVoidType()) { |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 790 | Diag(Loc, diag::err_param_with_void_type); |
| 791 | Invalid = true; |
| 792 | } |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 793 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 794 | ParamTypes[Idx] = ParamType; |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | if (Invalid) |
| 798 | return QualType(); |
| 799 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 800 | return Context.getFunctionType(T, ParamTypes, NumParamTypes, Variadic, |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 801 | Quals, false, false, 0, 0, false, CC_Default); |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 802 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 803 | |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 804 | /// \brief Build a member pointer type \c T Class::*. |
| 805 | /// |
| 806 | /// \param T the type to which the member pointer refers. |
| 807 | /// \param Class the class type into which the member pointer points. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 808 | /// \param CVR Qualifiers applied to the member pointer type |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 809 | /// \param Loc the location where this type begins |
| 810 | /// \param Entity the name of the entity that will have this member pointer type |
| 811 | /// |
| 812 | /// \returns a member pointer type, if successful, or a NULL type if there was |
| 813 | /// an error. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 814 | QualType Sema::BuildMemberPointerType(QualType T, QualType Class, |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 815 | unsigned CVR, SourceLocation Loc, |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 816 | DeclarationName Entity) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 817 | Qualifiers Quals = Qualifiers::fromCVRMask(CVR); |
| 818 | |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 819 | // Verify that we're not building a pointer to pointer to function with |
| 820 | // exception specification. |
| 821 | if (CheckDistantExceptionSpec(T)) { |
| 822 | Diag(Loc, diag::err_distant_exception_spec); |
| 823 | |
| 824 | // FIXME: If we're doing this as part of template instantiation, |
| 825 | // we should return immediately. |
| 826 | |
| 827 | // Build the type anyway, but use the canonical type so that the |
| 828 | // exception specifiers are stripped off. |
| 829 | T = Context.getCanonicalType(T); |
| 830 | } |
| 831 | |
| 832 | // C++ 8.3.3p3: A pointer to member shall not pointer to ... a member |
| 833 | // with reference type, or "cv void." |
| 834 | if (T->isReferenceType()) { |
Anders Carlsson | 8d4655d | 2009-06-30 00:06:57 +0000 | [diff] [blame] | 835 | Diag(Loc, diag::err_illegal_decl_mempointer_to_reference) |
John McCall | ac40605 | 2009-10-30 00:37:20 +0000 | [diff] [blame] | 836 | << (Entity? Entity.getAsString() : "type name") << T; |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 837 | return QualType(); |
| 838 | } |
| 839 | |
| 840 | if (T->isVoidType()) { |
| 841 | Diag(Loc, diag::err_illegal_decl_mempointer_to_void) |
| 842 | << (Entity? Entity.getAsString() : "type name"); |
| 843 | return QualType(); |
| 844 | } |
| 845 | |
| 846 | // Enforce C99 6.7.3p2: "Types other than pointer types derived from |
| 847 | // object or incomplete types shall not be restrict-qualified." |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 848 | if (Quals.hasRestrict() && !T->isIncompleteOrObjectType()) { |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 849 | Diag(Loc, diag::err_typecheck_invalid_restrict_invalid_pointee) |
| 850 | << T; |
| 851 | |
| 852 | // FIXME: If we're doing this as part of template instantiation, |
| 853 | // we should return immediately. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 854 | Quals.removeRestrict(); |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | if (!Class->isDependentType() && !Class->isRecordType()) { |
| 858 | Diag(Loc, diag::err_mempointer_in_nonclass_type) << Class; |
| 859 | return QualType(); |
| 860 | } |
| 861 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 862 | return Context.getQualifiedType( |
| 863 | Context.getMemberPointerType(T, Class.getTypePtr()), Quals); |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 864 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 865 | |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 866 | /// \brief Build a block pointer type. |
| 867 | /// |
| 868 | /// \param T The type to which we'll be building a block pointer. |
| 869 | /// |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 870 | /// \param CVR The cvr-qualifiers to be applied to the block pointer type. |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 871 | /// |
| 872 | /// \param Loc The location of the entity whose type involves this |
| 873 | /// block pointer type or, if there is no such entity, the location of the |
| 874 | /// type that will have block pointer type. |
| 875 | /// |
| 876 | /// \param Entity The name of the entity that involves the block pointer |
| 877 | /// type, if known. |
| 878 | /// |
| 879 | /// \returns A suitable block pointer type, if there are no |
| 880 | /// errors. Otherwise, returns a NULL type. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 881 | QualType Sema::BuildBlockPointerType(QualType T, unsigned CVR, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 882 | SourceLocation Loc, |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 883 | DeclarationName Entity) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 884 | if (!T->isFunctionType()) { |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 885 | Diag(Loc, diag::err_nonfunction_block_type); |
| 886 | return QualType(); |
| 887 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 888 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 889 | Qualifiers Quals = Qualifiers::fromCVRMask(CVR); |
| 890 | return Context.getQualifiedType(Context.getBlockPointerType(T), Quals); |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 891 | } |
| 892 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 893 | QualType Sema::GetTypeFromParser(TypeTy *Ty, TypeSourceInfo **TInfo) { |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 894 | QualType QT = QualType::getFromOpaquePtr(Ty); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 895 | if (QT.isNull()) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 896 | if (TInfo) *TInfo = 0; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 897 | return QualType(); |
| 898 | } |
| 899 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 900 | TypeSourceInfo *DI = 0; |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 901 | if (LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) { |
| 902 | QT = LIT->getType(); |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 903 | DI = LIT->getTypeSourceInfo(); |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 904 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 905 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 906 | if (TInfo) *TInfo = DI; |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 907 | return QT; |
| 908 | } |
| 909 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 910 | /// GetTypeForDeclarator - Convert the type for the specified |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 911 | /// declarator to Type instances. |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 912 | /// |
| 913 | /// If OwnedDecl is non-NULL, and this declarator's decl-specifier-seq |
| 914 | /// owns the declaration of a type (e.g., the definition of a struct |
| 915 | /// type), then *OwnedDecl will receive the owned declaration. |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 916 | QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 917 | TypeSourceInfo **TInfo, |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 918 | TagDecl **OwnedDecl) { |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 919 | // Determine the type of the declarator. Not all forms of declarator |
| 920 | // have a type. |
| 921 | QualType T; |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 922 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 923 | llvm::SmallVector<DelayedAttribute,4> FnAttrsFromDeclSpec; |
| 924 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 925 | switch (D.getName().getKind()) { |
| 926 | case UnqualifiedId::IK_Identifier: |
| 927 | case UnqualifiedId::IK_OperatorFunctionId: |
Sean Hunt | 0486d74 | 2009-11-28 04:44:28 +0000 | [diff] [blame] | 928 | case UnqualifiedId::IK_LiteralOperatorId: |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 929 | case UnqualifiedId::IK_TemplateId: |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 930 | T = ConvertDeclSpecToType(*this, D, FnAttrsFromDeclSpec); |
Chris Lattner | 5db2bb1 | 2009-10-25 18:21:37 +0000 | [diff] [blame] | 931 | |
Douglas Gregor | 591bd3c | 2010-02-08 22:07:33 +0000 | [diff] [blame] | 932 | if (!D.isInvalidType() && D.getDeclSpec().isTypeSpecOwned()) { |
| 933 | TagDecl* Owned = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep()); |
Douglas Gregor | b37b648 | 2010-02-12 17:40:34 +0000 | [diff] [blame] | 934 | // Owned is embedded if it was defined here, or if it is the |
| 935 | // very first (i.e., canonical) declaration of this tag type. |
| 936 | Owned->setEmbeddedInDeclarator(Owned->isDefinition() || |
| 937 | Owned->isCanonicalDecl()); |
Douglas Gregor | 591bd3c | 2010-02-08 22:07:33 +0000 | [diff] [blame] | 938 | if (OwnedDecl) *OwnedDecl = Owned; |
| 939 | } |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 940 | break; |
| 941 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 942 | case UnqualifiedId::IK_ConstructorName: |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 943 | case UnqualifiedId::IK_ConstructorTemplateId: |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 944 | case UnqualifiedId::IK_DestructorName: |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 945 | // Constructors and destructors don't have return types. Use |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 946 | // "void" instead. |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 947 | T = Context.VoidTy; |
| 948 | break; |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 949 | |
| 950 | case UnqualifiedId::IK_ConversionFunctionId: |
| 951 | // The result type of a conversion function is the type that it |
| 952 | // converts to. |
| 953 | T = GetTypeFromParser(D.getName().ConversionFunctionId); |
| 954 | break; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 955 | } |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 956 | |
Douglas Gregor | 1f5f3a4 | 2009-12-03 17:10:37 +0000 | [diff] [blame] | 957 | if (T.isNull()) |
| 958 | return T; |
| 959 | |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 960 | if (T == Context.UndeducedAutoTy) { |
| 961 | int Error = -1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 962 | |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 963 | switch (D.getContext()) { |
| 964 | case Declarator::KNRTypeListContext: |
| 965 | assert(0 && "K&R type lists aren't allowed in C++"); |
| 966 | break; |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 967 | case Declarator::PrototypeContext: |
| 968 | Error = 0; // Function prototype |
| 969 | break; |
| 970 | case Declarator::MemberContext: |
| 971 | switch (cast<TagDecl>(CurContext)->getTagKind()) { |
| 972 | case TagDecl::TK_enum: assert(0 && "unhandled tag kind"); break; |
| 973 | case TagDecl::TK_struct: Error = 1; /* Struct member */ break; |
| 974 | case TagDecl::TK_union: Error = 2; /* Union member */ break; |
| 975 | case TagDecl::TK_class: Error = 3; /* Class member */ break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 976 | } |
Anders Carlsson | baf45d3 | 2009-06-26 22:18:59 +0000 | [diff] [blame] | 977 | break; |
| 978 | case Declarator::CXXCatchContext: |
| 979 | Error = 4; // Exception declaration |
| 980 | break; |
| 981 | case Declarator::TemplateParamContext: |
| 982 | Error = 5; // Template parameter |
| 983 | break; |
| 984 | case Declarator::BlockLiteralContext: |
| 985 | Error = 6; // Block literal |
| 986 | break; |
| 987 | case Declarator::FileContext: |
| 988 | case Declarator::BlockContext: |
| 989 | case Declarator::ForContext: |
| 990 | case Declarator::ConditionContext: |
| 991 | case Declarator::TypeNameContext: |
| 992 | break; |
| 993 | } |
| 994 | |
| 995 | if (Error != -1) { |
| 996 | Diag(D.getDeclSpec().getTypeSpecTypeLoc(), diag::err_auto_not_allowed) |
| 997 | << Error; |
| 998 | T = Context.IntTy; |
| 999 | D.setInvalidType(true); |
| 1000 | } |
| 1001 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1002 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1003 | // The name we're declaring, if any. |
| 1004 | DeclarationName Name; |
| 1005 | if (D.getIdentifier()) |
| 1006 | Name = D.getIdentifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1007 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1008 | llvm::SmallVector<DelayedAttribute,4> FnAttrsFromPreviousChunk; |
| 1009 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 1010 | // Walk the DeclTypeInfo, building the recursive type as we go. |
| 1011 | // DeclTypeInfos are ordered from the identifier out, which is |
| 1012 | // opposite of what we want :). |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 1013 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { |
| 1014 | DeclaratorChunk &DeclType = D.getTypeObject(e-i-1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1015 | switch (DeclType.Kind) { |
| 1016 | default: assert(0 && "Unknown decltype!"); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1017 | case DeclaratorChunk::BlockPointer: |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 1018 | // If blocks are disabled, emit an error. |
| 1019 | if (!LangOpts.Blocks) |
| 1020 | Diag(DeclType.Loc, diag::err_blocks_disable); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1021 | |
| 1022 | T = BuildBlockPointerType(T, DeclType.Cls.TypeQuals, D.getIdentifierLoc(), |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 1023 | Name); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1024 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1025 | case DeclaratorChunk::Pointer: |
Sebastian Redl | 6a7330c | 2009-05-29 15:01:05 +0000 | [diff] [blame] | 1026 | // Verify that we're not building a pointer to pointer to function with |
| 1027 | // exception specification. |
| 1028 | if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) { |
| 1029 | Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec); |
| 1030 | D.setInvalidType(true); |
| 1031 | // Build the type anyway. |
| 1032 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1033 | if (getLangOptions().ObjC1 && T->isObjCInterfaceType()) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1034 | const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1035 | T = Context.getObjCObjectPointerType(T, |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 1036 | (ObjCProtocolDecl **)OIT->qual_begin(), |
Fariborz Jahanian | a422864 | 2010-03-05 22:42:55 +0000 | [diff] [blame] | 1037 | OIT->getNumProtocols(), |
| 1038 | DeclType.Ptr.TypeQuals); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1039 | break; |
| 1040 | } |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1041 | T = BuildPointerType(T, DeclType.Ptr.TypeQuals, DeclType.Loc, Name); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1042 | break; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1043 | case DeclaratorChunk::Reference: { |
| 1044 | Qualifiers Quals; |
| 1045 | if (DeclType.Ref.HasRestrict) Quals.addRestrict(); |
| 1046 | |
Sebastian Redl | 6a7330c | 2009-05-29 15:01:05 +0000 | [diff] [blame] | 1047 | // Verify that we're not building a reference to pointer to function with |
| 1048 | // exception specification. |
| 1049 | if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) { |
| 1050 | Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec); |
| 1051 | D.setInvalidType(true); |
| 1052 | // Build the type anyway. |
| 1053 | } |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1054 | T = BuildReferenceType(T, DeclType.Ref.LValueRef, Quals, |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1055 | DeclType.Loc, Name); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1056 | break; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1057 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1058 | case DeclaratorChunk::Array: { |
Sebastian Redl | 6a7330c | 2009-05-29 15:01:05 +0000 | [diff] [blame] | 1059 | // Verify that we're not building an array of pointers to function with |
| 1060 | // exception specification. |
| 1061 | if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) { |
| 1062 | Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec); |
| 1063 | D.setInvalidType(true); |
| 1064 | // Build the type anyway. |
| 1065 | } |
Chris Lattner | fd89bc8 | 2008-04-02 01:05:10 +0000 | [diff] [blame] | 1066 | DeclaratorChunk::ArrayTypeInfo &ATI = DeclType.Arr; |
Chris Lattner | 94f81fd | 2007-08-28 16:54:00 +0000 | [diff] [blame] | 1067 | Expr *ArraySize = static_cast<Expr*>(ATI.NumElts); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1068 | ArrayType::ArraySizeModifier ASM; |
| 1069 | if (ATI.isStar) |
| 1070 | ASM = ArrayType::Star; |
| 1071 | else if (ATI.hasStatic) |
| 1072 | ASM = ArrayType::Static; |
| 1073 | else |
| 1074 | ASM = ArrayType::Normal; |
Eli Friedman | f91f5c8 | 2009-04-26 21:57:51 +0000 | [diff] [blame] | 1075 | if (ASM == ArrayType::Star && |
| 1076 | D.getContext() != Declarator::PrototypeContext) { |
| 1077 | // FIXME: This check isn't quite right: it allows star in prototypes |
| 1078 | // for function definitions, and disallows some edge cases detailed |
| 1079 | // in http://gcc.gnu.org/ml/gcc-patches/2009-02/msg00133.html |
| 1080 | Diag(DeclType.Loc, diag::err_array_star_outside_prototype); |
| 1081 | ASM = ArrayType::Normal; |
| 1082 | D.setInvalidType(true); |
| 1083 | } |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1084 | T = BuildArrayType(T, ASM, ArraySize, |
| 1085 | Qualifiers::fromCVRMask(ATI.TypeQuals), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1086 | SourceRange(DeclType.Loc, DeclType.EndLoc), Name); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1087 | break; |
| 1088 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1089 | case DeclaratorChunk::Function: { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1090 | // If the function declarator has a prototype (i.e. it is not () and |
| 1091 | // does not have a K&R-style identifier list), then the arguments are part |
| 1092 | // of the type, otherwise the argument list is (). |
| 1093 | const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun; |
Sebastian Redl | 3cc9726 | 2009-05-31 11:47:27 +0000 | [diff] [blame] | 1094 | |
Chris Lattner | cd88129 | 2007-12-19 05:31:29 +0000 | [diff] [blame] | 1095 | // C99 6.7.5.3p1: The return type may not be a function or array type. |
Douglas Gregor | 58408bc | 2010-01-11 18:46:21 +0000 | [diff] [blame] | 1096 | // For conversion functions, we'll diagnose this particular error later. |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 1097 | if ((T->isArrayType() || T->isFunctionType()) && |
| 1098 | (D.getName().getKind() != UnqualifiedId::IK_ConversionFunctionId)) { |
Douglas Gregor | 58408bc | 2010-01-11 18:46:21 +0000 | [diff] [blame] | 1099 | Diag(DeclType.Loc, diag::err_func_returning_array_function) |
| 1100 | << T->isFunctionType() << T; |
Chris Lattner | cd88129 | 2007-12-19 05:31:29 +0000 | [diff] [blame] | 1101 | T = Context.IntTy; |
| 1102 | D.setInvalidType(true); |
| 1103 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1104 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 1105 | if (getLangOptions().CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) { |
| 1106 | // C++ [dcl.fct]p6: |
| 1107 | // Types shall not be defined in return or parameter types. |
| 1108 | TagDecl *Tag = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep()); |
| 1109 | if (Tag->isDefinition()) |
| 1110 | Diag(Tag->getLocation(), diag::err_type_defined_in_result_type) |
| 1111 | << Context.getTypeDeclType(Tag); |
| 1112 | } |
| 1113 | |
Sebastian Redl | 3cc9726 | 2009-05-31 11:47:27 +0000 | [diff] [blame] | 1114 | // Exception specs are not allowed in typedefs. Complain, but add it |
| 1115 | // anyway. |
| 1116 | if (FTI.hasExceptionSpec && |
| 1117 | D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) |
| 1118 | Diag(FTI.getThrowLoc(), diag::err_exception_spec_in_typedef); |
| 1119 | |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 1120 | if (FTI.NumArgs == 0) { |
Argyrios Kyrtzidis | c6f7345 | 2008-10-16 17:31:08 +0000 | [diff] [blame] | 1121 | if (getLangOptions().CPlusPlus) { |
| 1122 | // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the |
| 1123 | // function takes no arguments. |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1124 | llvm::SmallVector<QualType, 4> Exceptions; |
| 1125 | Exceptions.reserve(FTI.NumExceptions); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1126 | for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) { |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 1127 | // FIXME: Preserve type source info. |
| 1128 | QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty); |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 1129 | // Check that the type is valid for an exception spec, and drop it |
| 1130 | // if not. |
| 1131 | if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range)) |
| 1132 | Exceptions.push_back(ET); |
| 1133 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1134 | T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, FTI.TypeQuals, |
| 1135 | FTI.hasExceptionSpec, |
| 1136 | FTI.hasAnyExceptionSpec, |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 1137 | Exceptions.size(), Exceptions.data(), |
| 1138 | false, CC_Default); |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 1139 | } else if (FTI.isVariadic) { |
| 1140 | // We allow a zero-parameter variadic function in C if the |
| 1141 | // function is marked with the "overloadable" |
| 1142 | // attribute. Scan for this attribute now. |
| 1143 | bool Overloadable = false; |
| 1144 | for (const AttributeList *Attrs = D.getAttributes(); |
| 1145 | Attrs; Attrs = Attrs->getNext()) { |
| 1146 | if (Attrs->getKind() == AttributeList::AT_overloadable) { |
| 1147 | Overloadable = true; |
| 1148 | break; |
| 1149 | } |
| 1150 | } |
| 1151 | |
| 1152 | if (!Overloadable) |
| 1153 | Diag(FTI.getEllipsisLoc(), diag::err_ellipsis_first_arg); |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 1154 | T = Context.getFunctionType(T, NULL, 0, FTI.isVariadic, 0, |
| 1155 | false, false, 0, 0, false, CC_Default); |
Argyrios Kyrtzidis | c6f7345 | 2008-10-16 17:31:08 +0000 | [diff] [blame] | 1156 | } else { |
| 1157 | // Simple void foo(), where the incoming T is the result type. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1158 | T = Context.getFunctionNoProtoType(T); |
Argyrios Kyrtzidis | c6f7345 | 2008-10-16 17:31:08 +0000 | [diff] [blame] | 1159 | } |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 1160 | } else if (FTI.ArgInfo[0].Param == 0) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1161 | // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1162 | Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1163 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1164 | } else { |
| 1165 | // Otherwise, we have a function with an argument list that is |
| 1166 | // potentially variadic. |
| 1167 | llvm::SmallVector<QualType, 16> ArgTys; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1168 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1169 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) { |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1170 | ParmVarDecl *Param = |
| 1171 | cast<ParmVarDecl>(FTI.ArgInfo[i].Param.getAs<Decl>()); |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1172 | QualType ArgTy = Param->getType(); |
Chris Lattner | 78c75fb | 2007-07-21 05:30:18 +0000 | [diff] [blame] | 1173 | assert(!ArgTy.isNull() && "Couldn't parse type?"); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1174 | |
| 1175 | // Adjust the parameter type. |
Douglas Gregor | beb58cb | 2009-03-23 23:17:00 +0000 | [diff] [blame] | 1176 | assert((ArgTy == adjustParameterType(ArgTy)) && "Unadjusted type?"); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1177 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1178 | // Look for 'void'. void is allowed only as a single argument to a |
| 1179 | // function with no other parameters (C99 6.7.5.3p10). We record |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1180 | // int(void) as a FunctionProtoType with an empty argument list. |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1181 | if (ArgTy->isVoidType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1182 | // If this is something like 'float(int, void)', reject it. 'void' |
| 1183 | // is an incomplete type (C99 6.2.5p19) and function decls cannot |
| 1184 | // have arguments of incomplete type. |
| 1185 | if (FTI.NumArgs != 1 || FTI.isVariadic) { |
| 1186 | Diag(DeclType.Loc, diag::err_void_only_param); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 1187 | ArgTy = Context.IntTy; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1188 | Param->setType(ArgTy); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 1189 | } else if (FTI.ArgInfo[i].Ident) { |
| 1190 | // Reject, but continue to parse 'int(void abc)'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1191 | Diag(FTI.ArgInfo[i].IdentLoc, |
Chris Lattner | 4565d4e | 2007-07-21 05:26:43 +0000 | [diff] [blame] | 1192 | diag::err_param_with_void_type); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 1193 | ArgTy = Context.IntTy; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1194 | Param->setType(ArgTy); |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 1195 | } else { |
| 1196 | // Reject, but continue to parse 'float(const void)'. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1197 | if (ArgTy.hasQualifiers()) |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 1198 | Diag(DeclType.Loc, diag::err_void_param_qualified); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1199 | |
Chris Lattner | 2ff5426 | 2007-07-21 05:18:12 +0000 | [diff] [blame] | 1200 | // Do not add 'void' to the ArgTys list. |
| 1201 | break; |
| 1202 | } |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 1203 | } else if (!FTI.hasPrototype) { |
| 1204 | if (ArgTy->isPromotableIntegerType()) { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 1205 | ArgTy = Context.getPromotedIntegerType(ArgTy); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1206 | } else if (const BuiltinType* BTy = ArgTy->getAs<BuiltinType>()) { |
Eli Friedman | eb4b705 | 2008-08-25 21:31:01 +0000 | [diff] [blame] | 1207 | if (BTy->getKind() == BuiltinType::Float) |
| 1208 | ArgTy = Context.DoubleTy; |
| 1209 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1210 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1211 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1212 | ArgTys.push_back(ArgTy); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1213 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1214 | |
| 1215 | llvm::SmallVector<QualType, 4> Exceptions; |
| 1216 | Exceptions.reserve(FTI.NumExceptions); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1217 | for (unsigned ei = 0, ee = FTI.NumExceptions; ei != ee; ++ei) { |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 1218 | // FIXME: Preserve type source info. |
| 1219 | QualType ET = GetTypeFromParser(FTI.Exceptions[ei].Ty); |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 1220 | // Check that the type is valid for an exception spec, and drop it if |
| 1221 | // not. |
| 1222 | if (!CheckSpecifiedExceptionType(ET, FTI.Exceptions[ei].Range)) |
| 1223 | Exceptions.push_back(ET); |
| 1224 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1225 | |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1226 | T = Context.getFunctionType(T, ArgTys.data(), ArgTys.size(), |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1227 | FTI.isVariadic, FTI.TypeQuals, |
| 1228 | FTI.hasExceptionSpec, |
| 1229 | FTI.hasAnyExceptionSpec, |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 1230 | Exceptions.size(), Exceptions.data(), |
| 1231 | false, CC_Default); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1232 | } |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1233 | |
| 1234 | // For GCC compatibility, we allow attributes that apply only to |
| 1235 | // function types to be placed on a function's return type |
| 1236 | // instead (as long as that type doesn't happen to be function |
| 1237 | // or function-pointer itself). |
| 1238 | ProcessDelayedFnAttrs(*this, T, FnAttrsFromPreviousChunk); |
| 1239 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1240 | break; |
| 1241 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1242 | case DeclaratorChunk::MemberPointer: |
Sebastian Redl | 4994d2d | 2009-07-04 11:39:00 +0000 | [diff] [blame] | 1243 | // Verify that we're not building a pointer to pointer to function with |
| 1244 | // exception specification. |
| 1245 | if (getLangOptions().CPlusPlus && CheckDistantExceptionSpec(T)) { |
| 1246 | Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec); |
| 1247 | D.setInvalidType(true); |
| 1248 | // Build the type anyway. |
| 1249 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1250 | // The scope spec must refer to a class, or be dependent. |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1251 | QualType ClsType; |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 1252 | if (isDependentScopeSpecifier(DeclType.Mem.Scope()) |
| 1253 | || dyn_cast_or_null<CXXRecordDecl>( |
| 1254 | computeDeclContext(DeclType.Mem.Scope()))) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1255 | NestedNameSpecifier *NNS |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 1256 | = (NestedNameSpecifier *)DeclType.Mem.Scope().getScopeRep(); |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 1257 | NestedNameSpecifier *NNSPrefix = NNS->getPrefix(); |
| 1258 | switch (NNS->getKind()) { |
| 1259 | case NestedNameSpecifier::Identifier: |
| 1260 | ClsType = Context.getTypenameType(NNSPrefix, NNS->getAsIdentifier()); |
| 1261 | break; |
| 1262 | |
| 1263 | case NestedNameSpecifier::Namespace: |
| 1264 | case NestedNameSpecifier::Global: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 1265 | llvm_unreachable("Nested-name-specifier must name a type"); |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 1266 | break; |
| 1267 | |
| 1268 | case NestedNameSpecifier::TypeSpec: |
| 1269 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 1270 | ClsType = QualType(NNS->getAsType(), 0); |
| 1271 | if (NNSPrefix) |
| 1272 | ClsType = Context.getQualifiedNameType(NNSPrefix, ClsType); |
| 1273 | break; |
| 1274 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1275 | } else { |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 1276 | Diag(DeclType.Mem.Scope().getBeginLoc(), |
| 1277 | diag::err_illegal_decl_mempointer_in_nonclass) |
| 1278 | << (D.getIdentifier() ? D.getIdentifier()->getName() : "type name") |
| 1279 | << DeclType.Mem.Scope().getRange(); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1280 | D.setInvalidType(true); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1281 | } |
| 1282 | |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 1283 | if (!ClsType.isNull()) |
| 1284 | T = BuildMemberPointerType(T, ClsType, DeclType.Mem.TypeQuals, |
| 1285 | DeclType.Loc, D.getIdentifier()); |
| 1286 | if (T.isNull()) { |
| 1287 | T = Context.IntTy; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1288 | D.setInvalidType(true); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1289 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1290 | break; |
| 1291 | } |
| 1292 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 1293 | if (T.isNull()) { |
| 1294 | D.setInvalidType(true); |
| 1295 | T = Context.IntTy; |
| 1296 | } |
| 1297 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1298 | DiagnoseDelayedFnAttrs(*this, FnAttrsFromPreviousChunk); |
| 1299 | |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1300 | // See if there are any attributes on this declarator chunk. |
| 1301 | if (const AttributeList *AL = DeclType.getAttrs()) |
Charles Davis | 328ce34 | 2010-02-24 02:27:18 +0000 | [diff] [blame] | 1302 | ProcessTypeAttributeList(*this, T, false, AL, FnAttrsFromPreviousChunk); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1303 | } |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1304 | |
| 1305 | if (getLangOptions().CPlusPlus && T->isFunctionType()) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1306 | const FunctionProtoType *FnTy = T->getAs<FunctionProtoType>(); |
Chris Lattner | 778ed74 | 2009-10-25 17:36:50 +0000 | [diff] [blame] | 1307 | assert(FnTy && "Why oh why is there not a FunctionProtoType here?"); |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1308 | |
| 1309 | // C++ 8.3.5p4: A cv-qualifier-seq shall only be part of the function type |
| 1310 | // for a nonstatic member function, the function type to which a pointer |
| 1311 | // to member refers, or the top-level function type of a function typedef |
| 1312 | // declaration. |
| 1313 | if (FnTy->getTypeQuals() != 0 && |
| 1314 | D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
Douglas Gregor | 584049d | 2008-12-15 23:53:10 +0000 | [diff] [blame] | 1315 | ((D.getContext() != Declarator::MemberContext && |
| 1316 | (!D.getCXXScopeSpec().isSet() || |
Douglas Gregor | f59a56e | 2009-07-21 23:53:31 +0000 | [diff] [blame] | 1317 | !computeDeclContext(D.getCXXScopeSpec(), /*FIXME:*/true) |
| 1318 | ->isRecord())) || |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1319 | D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static)) { |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1320 | if (D.isFunctionDeclarator()) |
| 1321 | Diag(D.getIdentifierLoc(), diag::err_invalid_qualified_function_type); |
| 1322 | else |
| 1323 | Diag(D.getIdentifierLoc(), |
| 1324 | diag::err_invalid_qualified_typedef_function_type_use); |
| 1325 | |
| 1326 | // Strip the cv-quals from the type. |
| 1327 | T = Context.getFunctionType(FnTy->getResultType(), FnTy->arg_type_begin(), |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 1328 | FnTy->getNumArgs(), FnTy->isVariadic(), 0, |
| 1329 | false, false, 0, 0, false, CC_Default); |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1330 | } |
| 1331 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1332 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1333 | // Process any function attributes we might have delayed from the |
| 1334 | // declaration-specifiers. |
| 1335 | ProcessDelayedFnAttrs(*this, T, FnAttrsFromDeclSpec); |
| 1336 | |
| 1337 | // If there were any type attributes applied to the decl itself, not |
| 1338 | // the type, apply them to the result type. But don't do this for |
| 1339 | // block-literal expressions, which are parsed wierdly. |
| 1340 | if (D.getContext() != Declarator::BlockLiteralContext) |
| 1341 | if (const AttributeList *Attrs = D.getAttributes()) |
Charles Davis | 328ce34 | 2010-02-24 02:27:18 +0000 | [diff] [blame] | 1342 | ProcessTypeAttributeList(*this, T, false, Attrs, |
| 1343 | FnAttrsFromPreviousChunk); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1344 | |
| 1345 | DiagnoseDelayedFnAttrs(*this, FnAttrsFromPreviousChunk); |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 1346 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1347 | if (TInfo) { |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1348 | if (D.isInvalidType()) |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1349 | *TInfo = 0; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1350 | else |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1351 | *TInfo = GetTypeSourceInfoForDeclarator(D, T); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1352 | } |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 1353 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1354 | return T; |
| 1355 | } |
| 1356 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 1357 | namespace { |
| 1358 | class TypeSpecLocFiller : public TypeLocVisitor<TypeSpecLocFiller> { |
| 1359 | const DeclSpec &DS; |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 1360 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 1361 | public: |
| 1362 | TypeSpecLocFiller(const DeclSpec &DS) : DS(DS) {} |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 1363 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 1364 | void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { |
| 1365 | Visit(TL.getUnqualifiedLoc()); |
| 1366 | } |
| 1367 | void VisitTypedefTypeLoc(TypedefTypeLoc TL) { |
| 1368 | TL.setNameLoc(DS.getTypeSpecTypeLoc()); |
| 1369 | } |
| 1370 | void VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { |
| 1371 | TL.setNameLoc(DS.getTypeSpecTypeLoc()); |
Argyrios Kyrtzidis | eb66759 | 2009-09-29 19:45:22 +0000 | [diff] [blame] | 1372 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1373 | if (DS.getProtocolQualifiers()) { |
| 1374 | assert(TL.getNumProtocols() > 0); |
| 1375 | assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers()); |
| 1376 | TL.setLAngleLoc(DS.getProtocolLAngleLoc()); |
| 1377 | TL.setRAngleLoc(DS.getSourceRange().getEnd()); |
| 1378 | for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i) |
| 1379 | TL.setProtocolLoc(i, DS.getProtocolLocs()[i]); |
| 1380 | } else { |
| 1381 | assert(TL.getNumProtocols() == 0); |
| 1382 | TL.setLAngleLoc(SourceLocation()); |
| 1383 | TL.setRAngleLoc(SourceLocation()); |
| 1384 | } |
| 1385 | } |
| 1386 | void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { |
| 1387 | assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers()); |
| 1388 | |
| 1389 | TL.setStarLoc(SourceLocation()); |
| 1390 | |
| 1391 | if (DS.getProtocolQualifiers()) { |
| 1392 | assert(TL.getNumProtocols() > 0); |
| 1393 | assert(TL.getNumProtocols() == DS.getNumProtocolQualifiers()); |
| 1394 | TL.setHasProtocolsAsWritten(true); |
| 1395 | TL.setLAngleLoc(DS.getProtocolLAngleLoc()); |
| 1396 | TL.setRAngleLoc(DS.getSourceRange().getEnd()); |
| 1397 | for (unsigned i = 0, e = DS.getNumProtocolQualifiers(); i != e; ++i) |
| 1398 | TL.setProtocolLoc(i, DS.getProtocolLocs()[i]); |
| 1399 | |
| 1400 | } else { |
| 1401 | assert(TL.getNumProtocols() == 0); |
| 1402 | TL.setHasProtocolsAsWritten(false); |
| 1403 | TL.setLAngleLoc(SourceLocation()); |
| 1404 | TL.setRAngleLoc(SourceLocation()); |
| 1405 | } |
| 1406 | |
| 1407 | // This might not have been written with an inner type. |
| 1408 | if (DS.getTypeSpecType() == DeclSpec::TST_unspecified) { |
| 1409 | TL.setHasBaseTypeAsWritten(false); |
| 1410 | TL.getBaseTypeLoc().initialize(SourceLocation()); |
| 1411 | } else { |
| 1412 | TL.setHasBaseTypeAsWritten(true); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 1413 | Visit(TL.getBaseTypeLoc()); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1414 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 1415 | } |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1416 | void VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc TL) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1417 | TypeSourceInfo *TInfo = 0; |
| 1418 | Sema::GetTypeFromParser(DS.getTypeRep(), &TInfo); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1419 | |
| 1420 | // If we got no declarator info from previous Sema routines, |
| 1421 | // just fill with the typespec loc. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1422 | if (!TInfo) { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1423 | TL.initialize(DS.getTypeSpecTypeLoc()); |
| 1424 | return; |
| 1425 | } |
| 1426 | |
| 1427 | TemplateSpecializationTypeLoc OldTL = |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1428 | cast<TemplateSpecializationTypeLoc>(TInfo->getTypeLoc()); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1429 | TL.copy(OldTL); |
| 1430 | } |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 1431 | void VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) { |
| 1432 | assert(DS.getTypeSpecType() == DeclSpec::TST_typeofExpr); |
| 1433 | TL.setTypeofLoc(DS.getTypeSpecTypeLoc()); |
| 1434 | TL.setParensRange(DS.getTypeofParensRange()); |
| 1435 | } |
| 1436 | void VisitTypeOfTypeLoc(TypeOfTypeLoc TL) { |
| 1437 | assert(DS.getTypeSpecType() == DeclSpec::TST_typeofType); |
| 1438 | TL.setTypeofLoc(DS.getTypeSpecTypeLoc()); |
| 1439 | TL.setParensRange(DS.getTypeofParensRange()); |
| 1440 | assert(DS.getTypeRep()); |
| 1441 | TypeSourceInfo *TInfo = 0; |
| 1442 | Sema::GetTypeFromParser(DS.getTypeRep(), &TInfo); |
| 1443 | TL.setUnderlyingTInfo(TInfo); |
| 1444 | } |
Douglas Gregor | ddf889a | 2010-01-18 18:04:31 +0000 | [diff] [blame] | 1445 | void VisitBuiltinTypeLoc(BuiltinTypeLoc TL) { |
| 1446 | // By default, use the source location of the type specifier. |
| 1447 | TL.setBuiltinLoc(DS.getTypeSpecTypeLoc()); |
| 1448 | if (TL.needsExtraLocalData()) { |
| 1449 | // Set info for the written builtin specifiers. |
| 1450 | TL.getWrittenBuiltinSpecs() = DS.getWrittenBuiltinSpecs(); |
| 1451 | // Try to have a meaningful source location. |
| 1452 | if (TL.getWrittenSignSpec() != TSS_unspecified) |
| 1453 | // Sign spec loc overrides the others (e.g., 'unsigned long'). |
| 1454 | TL.setBuiltinLoc(DS.getTypeSpecSignLoc()); |
| 1455 | else if (TL.getWrittenWidthSpec() != TSW_unspecified) |
| 1456 | // Width spec loc overrides type spec loc (e.g., 'short int'). |
| 1457 | TL.setBuiltinLoc(DS.getTypeSpecWidthLoc()); |
| 1458 | } |
| 1459 | } |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 1460 | void VisitTypeLoc(TypeLoc TL) { |
| 1461 | // FIXME: add other typespec types and change this to an assert. |
| 1462 | TL.initialize(DS.getTypeSpecTypeLoc()); |
| 1463 | } |
| 1464 | }; |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 1465 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 1466 | class DeclaratorLocFiller : public TypeLocVisitor<DeclaratorLocFiller> { |
| 1467 | const DeclaratorChunk &Chunk; |
| 1468 | |
| 1469 | public: |
| 1470 | DeclaratorLocFiller(const DeclaratorChunk &Chunk) : Chunk(Chunk) {} |
| 1471 | |
| 1472 | void VisitQualifiedTypeLoc(QualifiedTypeLoc TL) { |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 1473 | llvm_unreachable("qualified type locs not expected here!"); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
| 1476 | void VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) { |
| 1477 | assert(Chunk.Kind == DeclaratorChunk::BlockPointer); |
| 1478 | TL.setCaretLoc(Chunk.Loc); |
| 1479 | } |
| 1480 | void VisitPointerTypeLoc(PointerTypeLoc TL) { |
| 1481 | assert(Chunk.Kind == DeclaratorChunk::Pointer); |
| 1482 | TL.setStarLoc(Chunk.Loc); |
| 1483 | } |
| 1484 | void VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) { |
| 1485 | assert(Chunk.Kind == DeclaratorChunk::Pointer); |
| 1486 | TL.setStarLoc(Chunk.Loc); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1487 | TL.setHasBaseTypeAsWritten(true); |
| 1488 | TL.setHasProtocolsAsWritten(false); |
| 1489 | TL.setLAngleLoc(SourceLocation()); |
| 1490 | TL.setRAngleLoc(SourceLocation()); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 1491 | } |
| 1492 | void VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) { |
| 1493 | assert(Chunk.Kind == DeclaratorChunk::MemberPointer); |
| 1494 | TL.setStarLoc(Chunk.Loc); |
| 1495 | // FIXME: nested name specifier |
| 1496 | } |
| 1497 | void VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) { |
| 1498 | assert(Chunk.Kind == DeclaratorChunk::Reference); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1499 | // 'Amp' is misleading: this might have been originally |
| 1500 | /// spelled with AmpAmp. |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 1501 | TL.setAmpLoc(Chunk.Loc); |
| 1502 | } |
| 1503 | void VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) { |
| 1504 | assert(Chunk.Kind == DeclaratorChunk::Reference); |
| 1505 | assert(!Chunk.Ref.LValueRef); |
| 1506 | TL.setAmpAmpLoc(Chunk.Loc); |
| 1507 | } |
| 1508 | void VisitArrayTypeLoc(ArrayTypeLoc TL) { |
| 1509 | assert(Chunk.Kind == DeclaratorChunk::Array); |
| 1510 | TL.setLBracketLoc(Chunk.Loc); |
| 1511 | TL.setRBracketLoc(Chunk.EndLoc); |
| 1512 | TL.setSizeExpr(static_cast<Expr*>(Chunk.Arr.NumElts)); |
| 1513 | } |
| 1514 | void VisitFunctionTypeLoc(FunctionTypeLoc TL) { |
| 1515 | assert(Chunk.Kind == DeclaratorChunk::Function); |
| 1516 | TL.setLParenLoc(Chunk.Loc); |
| 1517 | TL.setRParenLoc(Chunk.EndLoc); |
| 1518 | |
| 1519 | const DeclaratorChunk::FunctionTypeInfo &FTI = Chunk.Fun; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1520 | for (unsigned i = 0, e = TL.getNumArgs(), tpi = 0; i != e; ++i) { |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 1521 | ParmVarDecl *Param = FTI.ArgInfo[i].Param.getAs<ParmVarDecl>(); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1522 | TL.setArg(tpi++, Param); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 1523 | } |
| 1524 | // FIXME: exception specs |
| 1525 | } |
| 1526 | |
| 1527 | void VisitTypeLoc(TypeLoc TL) { |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 1528 | llvm_unreachable("unsupported TypeLoc kind in declarator!"); |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 1529 | } |
| 1530 | }; |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1533 | /// \brief Create and instantiate a TypeSourceInfo with type source information. |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 1534 | /// |
| 1535 | /// \param T QualType referring to the type as written in source code. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1536 | TypeSourceInfo * |
| 1537 | Sema::GetTypeSourceInfoForDeclarator(Declarator &D, QualType T) { |
| 1538 | TypeSourceInfo *TInfo = Context.CreateTypeSourceInfo(T); |
| 1539 | UnqualTypeLoc CurrTL = TInfo->getTypeLoc().getUnqualifiedLoc(); |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 1540 | |
Sebastian Redl | 8ce35b0 | 2009-10-25 21:45:37 +0000 | [diff] [blame] | 1541 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) { |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 1542 | DeclaratorLocFiller(D.getTypeObject(i)).Visit(CurrTL); |
| 1543 | CurrTL = CurrTL.getNextTypeLoc().getUnqualifiedLoc(); |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 1544 | } |
Argyrios Kyrtzidis | f352bdd | 2009-09-29 19:43:35 +0000 | [diff] [blame] | 1545 | |
John McCall | 51bd803 | 2009-10-18 01:05:36 +0000 | [diff] [blame] | 1546 | TypeSpecLocFiller(D.getDeclSpec()).Visit(CurrTL); |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 1547 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1548 | return TInfo; |
Argyrios Kyrtzidis | 4adab7f | 2009-08-19 01:28:06 +0000 | [diff] [blame] | 1549 | } |
| 1550 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1551 | /// \brief Create a LocInfoType to hold the given QualType and TypeSourceInfo. |
| 1552 | QualType Sema::CreateLocInfoType(QualType T, TypeSourceInfo *TInfo) { |
Argyrios Kyrtzidis | 1bb8a45 | 2009-08-19 01:28:17 +0000 | [diff] [blame] | 1553 | // FIXME: LocInfoTypes are "transient", only needed for passing to/from Parser |
| 1554 | // and Sema during declaration parsing. Try deallocating/caching them when |
| 1555 | // it's appropriate, instead of allocating them and keeping them around. |
| 1556 | LocInfoType *LocT = (LocInfoType*)BumpAlloc.Allocate(sizeof(LocInfoType), 8); |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1557 | new (LocT) LocInfoType(T, TInfo); |
Argyrios Kyrtzidis | 1bb8a45 | 2009-08-19 01:28:17 +0000 | [diff] [blame] | 1558 | assert(LocT->getTypeClass() != T->getTypeClass() && |
| 1559 | "LocInfoType's TypeClass conflicts with an existing Type class"); |
| 1560 | return QualType(LocT, 0); |
| 1561 | } |
| 1562 | |
| 1563 | void LocInfoType::getAsStringInternal(std::string &Str, |
| 1564 | const PrintingPolicy &Policy) const { |
Argyrios Kyrtzidis | 35d44e5 | 2009-08-19 01:46:06 +0000 | [diff] [blame] | 1565 | assert(false && "LocInfoType leaked into the type system; an opaque TypeTy*" |
| 1566 | " was used directly instead of getting the QualType through" |
| 1567 | " GetTypeFromParser"); |
Argyrios Kyrtzidis | 1bb8a45 | 2009-08-19 01:28:17 +0000 | [diff] [blame] | 1568 | } |
| 1569 | |
Sebastian Redl | 9e5e4aa | 2009-01-26 19:54:48 +0000 | [diff] [blame] | 1570 | /// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that |
| 1571 | /// may be similar (C++ 4.4), replaces T1 and T2 with the type that |
| 1572 | /// they point to and return true. If T1 and T2 aren't pointer types |
| 1573 | /// or pointer-to-member types, or if they are not similar at this |
| 1574 | /// level, returns false and leaves T1 and T2 unchanged. Top-level |
| 1575 | /// qualifiers on T1 and T2 are ignored. This function will typically |
| 1576 | /// be called in a loop that successively "unwraps" pointer and |
| 1577 | /// pointer-to-member types to compare them at each level. |
Chris Lattner | ecb81f2 | 2009-02-16 21:43:00 +0000 | [diff] [blame] | 1578 | bool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1579 | const PointerType *T1PtrType = T1->getAs<PointerType>(), |
| 1580 | *T2PtrType = T2->getAs<PointerType>(); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1581 | if (T1PtrType && T2PtrType) { |
| 1582 | T1 = T1PtrType->getPointeeType(); |
| 1583 | T2 = T2PtrType->getPointeeType(); |
| 1584 | return true; |
| 1585 | } |
| 1586 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1587 | const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(), |
| 1588 | *T2MPType = T2->getAs<MemberPointerType>(); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1589 | if (T1MPType && T2MPType && |
| 1590 | Context.getCanonicalType(T1MPType->getClass()) == |
| 1591 | Context.getCanonicalType(T2MPType->getClass())) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1592 | T1 = T1MPType->getPointeeType(); |
| 1593 | T2 = T2MPType->getPointeeType(); |
| 1594 | return true; |
| 1595 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1596 | return false; |
| 1597 | } |
| 1598 | |
Sebastian Redl | cee63fb | 2008-12-02 14:43:59 +0000 | [diff] [blame] | 1599 | Sema::TypeResult Sema::ActOnTypeName(Scope *S, Declarator &D) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1600 | // C99 6.7.6: Type names have no identifier. This is already validated by |
| 1601 | // the parser. |
| 1602 | assert(D.getIdentifier() == 0 && "Type name should have no identifier!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1603 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1604 | TypeSourceInfo *TInfo = 0; |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 1605 | TagDecl *OwnedTag = 0; |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1606 | QualType T = GetTypeForDeclarator(D, S, &TInfo, &OwnedTag); |
Chris Lattner | 5153ee6 | 2009-04-25 08:47:54 +0000 | [diff] [blame] | 1607 | if (D.isInvalidType()) |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 1608 | return true; |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 1609 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 1610 | if (getLangOptions().CPlusPlus) { |
| 1611 | // Check that there are no default arguments (C++ only). |
Douglas Gregor | 6d6eb57 | 2008-05-07 04:49:29 +0000 | [diff] [blame] | 1612 | CheckExtraCXXDefaultArguments(D); |
| 1613 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 1614 | // C++0x [dcl.type]p3: |
| 1615 | // A type-specifier-seq shall not define a class or enumeration |
| 1616 | // unless it appears in the type-id of an alias-declaration |
| 1617 | // (7.1.3). |
| 1618 | if (OwnedTag && OwnedTag->isDefinition()) |
| 1619 | Diag(OwnedTag->getLocation(), diag::err_type_defined_in_type_specifier) |
| 1620 | << Context.getTypeDeclType(OwnedTag); |
| 1621 | } |
| 1622 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1623 | if (TInfo) |
| 1624 | T = CreateLocInfoType(T, TInfo); |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 1625 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1626 | return T.getAsOpaquePtr(); |
| 1627 | } |
| 1628 | |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1629 | |
| 1630 | |
| 1631 | //===----------------------------------------------------------------------===// |
| 1632 | // Type Attribute Processing |
| 1633 | //===----------------------------------------------------------------------===// |
| 1634 | |
| 1635 | /// HandleAddressSpaceTypeAttribute - Process an address_space attribute on the |
| 1636 | /// specified type. The attribute contains 1 argument, the id of the address |
| 1637 | /// space for the type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1638 | static void HandleAddressSpaceTypeAttribute(QualType &Type, |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1639 | const AttributeList &Attr, Sema &S){ |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1640 | |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1641 | // If this type is already address space qualified, reject it. |
| 1642 | // Clause 6.7.3 - Type qualifiers: "No type shall be qualified by qualifiers |
| 1643 | // for two or more different address spaces." |
| 1644 | if (Type.getAddressSpace()) { |
| 1645 | S.Diag(Attr.getLoc(), diag::err_attribute_address_multiple_qualifiers); |
| 1646 | return; |
| 1647 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1648 | |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1649 | // Check the attribute arguments. |
| 1650 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | f3a41af | 2008-11-20 06:38:18 +0000 | [diff] [blame] | 1651 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1652 | return; |
| 1653 | } |
| 1654 | Expr *ASArgExpr = static_cast<Expr *>(Attr.getArg(0)); |
| 1655 | llvm::APSInt addrSpace(32); |
| 1656 | if (!ASArgExpr->isIntegerConstantExpr(addrSpace, S.Context)) { |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1657 | S.Diag(Attr.getLoc(), diag::err_attribute_address_space_not_int) |
| 1658 | << ASArgExpr->getSourceRange(); |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1659 | return; |
| 1660 | } |
| 1661 | |
John McCall | efadb77 | 2009-07-28 06:52:18 +0000 | [diff] [blame] | 1662 | // Bounds checking. |
| 1663 | if (addrSpace.isSigned()) { |
| 1664 | if (addrSpace.isNegative()) { |
| 1665 | S.Diag(Attr.getLoc(), diag::err_attribute_address_space_negative) |
| 1666 | << ASArgExpr->getSourceRange(); |
| 1667 | return; |
| 1668 | } |
| 1669 | addrSpace.setIsSigned(false); |
| 1670 | } |
| 1671 | llvm::APSInt max(addrSpace.getBitWidth()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1672 | max = Qualifiers::MaxAddressSpace; |
John McCall | efadb77 | 2009-07-28 06:52:18 +0000 | [diff] [blame] | 1673 | if (addrSpace > max) { |
| 1674 | S.Diag(Attr.getLoc(), diag::err_attribute_address_space_too_high) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1675 | << Qualifiers::MaxAddressSpace << ASArgExpr->getSourceRange(); |
John McCall | efadb77 | 2009-07-28 06:52:18 +0000 | [diff] [blame] | 1676 | return; |
| 1677 | } |
| 1678 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1679 | unsigned ASIdx = static_cast<unsigned>(addrSpace.getZExtValue()); |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 1680 | Type = S.Context.getAddrSpaceQualType(Type, ASIdx); |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1681 | } |
| 1682 | |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1683 | /// HandleObjCGCTypeAttribute - Process an objc's gc attribute on the |
| 1684 | /// specified type. The attribute contains 1 argument, weak or strong. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1685 | static void HandleObjCGCTypeAttribute(QualType &Type, |
Chris Lattner | 3b6b83b | 2009-02-18 22:58:38 +0000 | [diff] [blame] | 1686 | const AttributeList &Attr, Sema &S) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1687 | if (Type.getObjCGCAttr() != Qualifiers::GCNone) { |
Fariborz Jahanian | 5934e75 | 2009-02-18 18:52:41 +0000 | [diff] [blame] | 1688 | S.Diag(Attr.getLoc(), diag::err_attribute_multiple_objc_gc); |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1689 | return; |
| 1690 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1691 | |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1692 | // Check the attribute arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1693 | if (!Attr.getParameterName()) { |
Fariborz Jahanian | ba372b8 | 2009-02-18 17:52:36 +0000 | [diff] [blame] | 1694 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 1695 | << "objc_gc" << 1; |
| 1696 | return; |
| 1697 | } |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1698 | Qualifiers::GC GCAttr; |
Fariborz Jahanian | ba372b8 | 2009-02-18 17:52:36 +0000 | [diff] [blame] | 1699 | if (Attr.getNumArgs() != 0) { |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1700 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1701 | return; |
| 1702 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1703 | if (Attr.getParameterName()->isStr("weak")) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1704 | GCAttr = Qualifiers::Weak; |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1705 | else if (Attr.getParameterName()->isStr("strong")) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1706 | GCAttr = Qualifiers::Strong; |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1707 | else { |
| 1708 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 1709 | << "objc_gc" << Attr.getParameterName(); |
| 1710 | return; |
| 1711 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1712 | |
Chris Lattner | 3b6b83b | 2009-02-18 22:58:38 +0000 | [diff] [blame] | 1713 | Type = S.Context.getObjCGCQualType(Type, GCAttr); |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1716 | /// Process an individual function attribute. Returns true if the |
| 1717 | /// attribute does not make sense to apply to this type. |
| 1718 | bool ProcessFnAttr(Sema &S, QualType &Type, const AttributeList &Attr) { |
| 1719 | if (Attr.getKind() == AttributeList::AT_noreturn) { |
| 1720 | // Complain immediately if the arg count is wrong. |
| 1721 | if (Attr.getNumArgs() != 0) { |
| 1722 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1723 | return false; |
| 1724 | } |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1725 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1726 | // Delay if this is not a function or pointer to block. |
| 1727 | if (!Type->isFunctionPointerType() |
| 1728 | && !Type->isBlockPointerType() |
| 1729 | && !Type->isFunctionType()) |
| 1730 | return true; |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1731 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1732 | // Otherwise we can process right away. |
| 1733 | Type = S.Context.getNoReturnType(Type); |
| 1734 | return false; |
| 1735 | } |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1736 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1737 | // Otherwise, a calling convention. |
| 1738 | if (Attr.getNumArgs() != 0) { |
| 1739 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1740 | return false; |
| 1741 | } |
John McCall | f82b4e8 | 2010-02-04 05:44:44 +0000 | [diff] [blame] | 1742 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1743 | QualType T = Type; |
| 1744 | if (const PointerType *PT = Type->getAs<PointerType>()) |
| 1745 | T = PT->getPointeeType(); |
| 1746 | const FunctionType *Fn = T->getAs<FunctionType>(); |
John McCall | f82b4e8 | 2010-02-04 05:44:44 +0000 | [diff] [blame] | 1747 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1748 | // Delay if the type didn't work out to a function. |
| 1749 | if (!Fn) return true; |
| 1750 | |
| 1751 | // TODO: diagnose uses of these conventions on the wrong target. |
| 1752 | CallingConv CC; |
| 1753 | switch (Attr.getKind()) { |
| 1754 | case AttributeList::AT_cdecl: CC = CC_C; break; |
| 1755 | case AttributeList::AT_fastcall: CC = CC_X86FastCall; break; |
| 1756 | case AttributeList::AT_stdcall: CC = CC_X86StdCall; break; |
| 1757 | default: llvm_unreachable("unexpected attribute kind"); return false; |
| 1758 | } |
| 1759 | |
| 1760 | CallingConv CCOld = Fn->getCallConv(); |
Charles Davis | 064f7db | 2010-02-23 06:13:55 +0000 | [diff] [blame] | 1761 | if (S.Context.getCanonicalCallConv(CC) == |
| 1762 | S.Context.getCanonicalCallConv(CCOld)) return false; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1763 | |
| 1764 | if (CCOld != CC_Default) { |
| 1765 | // Should we diagnose reapplications of the same convention? |
| 1766 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 1767 | << FunctionType::getNameForCallConv(CC) |
| 1768 | << FunctionType::getNameForCallConv(CCOld); |
| 1769 | return false; |
| 1770 | } |
| 1771 | |
| 1772 | // Diagnose the use of X86 fastcall on varargs or unprototyped functions. |
| 1773 | if (CC == CC_X86FastCall) { |
| 1774 | if (isa<FunctionNoProtoType>(Fn)) { |
| 1775 | S.Diag(Attr.getLoc(), diag::err_cconv_knr) |
| 1776 | << FunctionType::getNameForCallConv(CC); |
| 1777 | return false; |
| 1778 | } |
| 1779 | |
| 1780 | const FunctionProtoType *FnP = cast<FunctionProtoType>(Fn); |
| 1781 | if (FnP->isVariadic()) { |
| 1782 | S.Diag(Attr.getLoc(), diag::err_cconv_varargs) |
| 1783 | << FunctionType::getNameForCallConv(CC); |
| 1784 | return false; |
| 1785 | } |
| 1786 | } |
| 1787 | |
| 1788 | Type = S.Context.getCallConvType(Type, CC); |
| 1789 | return false; |
John McCall | f82b4e8 | 2010-02-04 05:44:44 +0000 | [diff] [blame] | 1790 | } |
| 1791 | |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 1792 | /// HandleVectorSizeAttribute - this attribute is only applicable to integral |
| 1793 | /// and float scalars, although arrays, pointers, and function return values are |
| 1794 | /// allowed in conjunction with this construct. Aggregates with this attribute |
| 1795 | /// are invalid, even if they are of the same size as a corresponding scalar. |
| 1796 | /// The raw attribute should contain precisely 1 argument, the vector size for |
| 1797 | /// the variable, measured in bytes. If curType and rawAttr are well formed, |
| 1798 | /// this routine will return a new vector type. |
| 1799 | static void HandleVectorSizeAttr(QualType& CurType, const AttributeList &Attr, Sema &S) { |
| 1800 | // Check the attribute arugments. |
| 1801 | if (Attr.getNumArgs() != 1) { |
| 1802 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1803 | return; |
| 1804 | } |
| 1805 | Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0)); |
| 1806 | llvm::APSInt vecSize(32); |
| 1807 | if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) { |
| 1808 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 1809 | << "vector_size" << sizeExpr->getSourceRange(); |
| 1810 | return; |
| 1811 | } |
| 1812 | // the base type must be integer or float, and can't already be a vector. |
| 1813 | if (CurType->isVectorType() || |
| 1814 | (!CurType->isIntegerType() && !CurType->isRealFloatingType())) { |
| 1815 | S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType; |
| 1816 | return; |
| 1817 | } |
| 1818 | unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType)); |
| 1819 | // vecSize is specified in bytes - convert to bits. |
| 1820 | unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8); |
| 1821 | |
| 1822 | // the vector size needs to be an integral multiple of the type size. |
| 1823 | if (vectorSize % typeSize) { |
| 1824 | S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size) |
| 1825 | << sizeExpr->getSourceRange(); |
| 1826 | return; |
| 1827 | } |
| 1828 | if (vectorSize == 0) { |
| 1829 | S.Diag(Attr.getLoc(), diag::err_attribute_zero_size) |
| 1830 | << sizeExpr->getSourceRange(); |
| 1831 | return; |
| 1832 | } |
| 1833 | |
| 1834 | // Success! Instantiate the vector type, the number of elements is > 0, and |
| 1835 | // not required to be a power of 2, unlike GCC. |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 1836 | CurType = S.Context.getVectorType(CurType, vectorSize/typeSize, false, false); |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 1837 | } |
| 1838 | |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1839 | void ProcessTypeAttributeList(Sema &S, QualType &Result, |
Charles Davis | 328ce34 | 2010-02-24 02:27:18 +0000 | [diff] [blame] | 1840 | bool IsDeclSpec, const AttributeList *AL, |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1841 | DelayedAttributeSet &FnAttrs) { |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 1842 | // Scan through and apply attributes to this type where it makes sense. Some |
| 1843 | // attributes (such as __address_space__, __vector_size__, etc) apply to the |
| 1844 | // type, but others can be present in the type specifiers even though they |
Chris Lattner | fca0ddd | 2008-06-26 06:27:57 +0000 | [diff] [blame] | 1845 | // apply to the decl. Here we apply type attributes and ignore the rest. |
| 1846 | for (; AL; AL = AL->getNext()) { |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 1847 | // If this is an attribute we can handle, do so now, otherwise, add it to |
| 1848 | // the LeftOverAttrs list for rechaining. |
Chris Lattner | fca0ddd | 2008-06-26 06:27:57 +0000 | [diff] [blame] | 1849 | switch (AL->getKind()) { |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 1850 | default: break; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1851 | |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 1852 | case AttributeList::AT_address_space: |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1853 | HandleAddressSpaceTypeAttribute(Result, *AL, S); |
Chris Lattner | c9b346d | 2008-06-29 00:50:08 +0000 | [diff] [blame] | 1854 | break; |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1855 | case AttributeList::AT_objc_gc: |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1856 | HandleObjCGCTypeAttribute(Result, *AL, S); |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1857 | break; |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 1858 | case AttributeList::AT_vector_size: |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1859 | HandleVectorSizeAttr(Result, *AL, S); |
| 1860 | break; |
| 1861 | |
| 1862 | case AttributeList::AT_noreturn: |
| 1863 | case AttributeList::AT_cdecl: |
| 1864 | case AttributeList::AT_fastcall: |
| 1865 | case AttributeList::AT_stdcall: |
Charles Davis | 328ce34 | 2010-02-24 02:27:18 +0000 | [diff] [blame] | 1866 | // Don't process these on the DeclSpec. |
| 1867 | if (IsDeclSpec || |
| 1868 | ProcessFnAttr(S, Result, *AL)) |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1869 | FnAttrs.push_back(DelayedAttribute(AL, Result)); |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 1870 | break; |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 1871 | } |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 1872 | } |
Chris Lattner | 232e882 | 2008-02-21 01:08:11 +0000 | [diff] [blame] | 1873 | } |
| 1874 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1875 | /// @brief Ensure that the type T is a complete type. |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1876 | /// |
| 1877 | /// This routine checks whether the type @p T is complete in any |
| 1878 | /// context where a complete type is required. If @p T is a complete |
Douglas Gregor | 86447ec | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 1879 | /// type, returns false. If @p T is a class template specialization, |
| 1880 | /// this routine then attempts to perform class template |
| 1881 | /// instantiation. If instantiation fails, or if @p T is incomplete |
| 1882 | /// and cannot be completed, issues the diagnostic @p diag (giving it |
| 1883 | /// the type @p T) and returns true. |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1884 | /// |
| 1885 | /// @param Loc The location in the source that the incomplete type |
| 1886 | /// diagnostic should refer to. |
| 1887 | /// |
| 1888 | /// @param T The type that this routine is examining for completeness. |
| 1889 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1890 | /// @param PD The partial diagnostic that will be printed out if T is not a |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 1891 | /// complete type. |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1892 | /// |
| 1893 | /// @returns @c true if @p T is incomplete and a diagnostic was emitted, |
| 1894 | /// @c false otherwise. |
Anders Carlsson | 91a0cc9 | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 1895 | bool Sema::RequireCompleteType(SourceLocation Loc, QualType T, |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 1896 | const PartialDiagnostic &PD, |
| 1897 | std::pair<SourceLocation, |
| 1898 | PartialDiagnostic> Note) { |
Anders Carlsson | 91a0cc9 | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 1899 | unsigned diag = PD.getDiagID(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1900 | |
Douglas Gregor | 573d9c3 | 2009-10-21 23:19:44 +0000 | [diff] [blame] | 1901 | // FIXME: Add this assertion to make sure we always get instantiation points. |
| 1902 | // assert(!Loc.isInvalid() && "Invalid location in RequireCompleteType"); |
Douglas Gregor | 690dc7f | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 1903 | // FIXME: Add this assertion to help us flush out problems with |
| 1904 | // checking for dependent types and type-dependent expressions. |
| 1905 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1906 | // assert(!T->isDependentType() && |
Douglas Gregor | 690dc7f | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 1907 | // "Can't ask whether a dependent type is complete"); |
| 1908 | |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1909 | // If we have a complete type, we're done. |
| 1910 | if (!T->isIncompleteType()) |
| 1911 | return false; |
Eli Friedman | 3c0eb16 | 2008-05-27 03:33:27 +0000 | [diff] [blame] | 1912 | |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1913 | // If we have a class template specialization or a class member of a |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1914 | // class template specialization, or an array with known size of such, |
| 1915 | // try to instantiate it. |
| 1916 | QualType MaybeTemplate = T; |
Douglas Gregor | 89c49f0 | 2009-11-09 22:08:55 +0000 | [diff] [blame] | 1917 | if (const ConstantArrayType *Array = Context.getAsConstantArrayType(T)) |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1918 | MaybeTemplate = Array->getElementType(); |
| 1919 | if (const RecordType *Record = MaybeTemplate->getAs<RecordType>()) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1920 | if (ClassTemplateSpecializationDecl *ClassTemplateSpec |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1921 | = dyn_cast<ClassTemplateSpecializationDecl>(Record->getDecl())) { |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 1922 | if (ClassTemplateSpec->getSpecializationKind() == TSK_Undeclared) |
| 1923 | return InstantiateClassTemplateSpecialization(Loc, ClassTemplateSpec, |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1924 | TSK_ImplicitInstantiation, |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1925 | /*Complain=*/diag != 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1926 | } else if (CXXRecordDecl *Rec |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1927 | = dyn_cast<CXXRecordDecl>(Record->getDecl())) { |
| 1928 | if (CXXRecordDecl *Pattern = Rec->getInstantiatedFromMemberClass()) { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 1929 | MemberSpecializationInfo *MSInfo = Rec->getMemberSpecializationInfo(); |
| 1930 | assert(MSInfo && "Missing member specialization information?"); |
Douglas Gregor | 357bbd0 | 2009-08-28 20:50:45 +0000 | [diff] [blame] | 1931 | // This record was instantiated from a class within a template. |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 1932 | if (MSInfo->getTemplateSpecializationKind() |
Douglas Gregor | 972e6ce | 2009-10-27 06:26:26 +0000 | [diff] [blame] | 1933 | != TSK_ExplicitSpecialization) |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1934 | return InstantiateClass(Loc, Rec, Pattern, |
| 1935 | getTemplateInstantiationArgs(Rec), |
| 1936 | TSK_ImplicitInstantiation, |
| 1937 | /*Complain=*/diag != 0); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1938 | } |
| 1939 | } |
| 1940 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 1941 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1942 | if (diag == 0) |
| 1943 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1944 | |
Rafael Espindola | 0162070 | 2010-03-21 22:56:43 +0000 | [diff] [blame^] | 1945 | const TagType *Tag = 0; |
| 1946 | if (const RecordType *Record = T->getAs<RecordType>()) |
| 1947 | Tag = Record; |
| 1948 | else if (const EnumType *Enum = T->getAs<EnumType>()) |
| 1949 | Tag = Enum; |
| 1950 | |
| 1951 | // Avoid diagnosing invalid decls as incomplete. |
| 1952 | if (Tag && Tag->getDecl()->isInvalidDecl()) |
| 1953 | return true; |
| 1954 | |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1955 | // We have an incomplete type. Produce a diagnostic. |
Anders Carlsson | 91a0cc9 | 2009-08-26 22:33:56 +0000 | [diff] [blame] | 1956 | Diag(Loc, PD) << T; |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1957 | |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 1958 | // If we have a note, produce it. |
| 1959 | if (!Note.first.isInvalid()) |
| 1960 | Diag(Note.first, Note.second); |
| 1961 | |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1962 | // If the type was a forward declaration of a class/struct/union |
Rafael Espindola | 0162070 | 2010-03-21 22:56:43 +0000 | [diff] [blame^] | 1963 | // type, produce a note. |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1964 | if (Tag && !Tag->getDecl()->isInvalidDecl()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1965 | Diag(Tag->getDecl()->getLocation(), |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1966 | Tag->isBeingDefined() ? diag::note_type_being_defined |
| 1967 | : diag::note_forward_declaration) |
| 1968 | << QualType(Tag, 0); |
| 1969 | |
| 1970 | return true; |
| 1971 | } |
Douglas Gregor | e625893 | 2009-03-19 00:39:20 +0000 | [diff] [blame] | 1972 | |
| 1973 | /// \brief Retrieve a version of the type 'T' that is qualified by the |
| 1974 | /// nested-name-specifier contained in SS. |
| 1975 | QualType Sema::getQualifiedNameType(const CXXScopeSpec &SS, QualType T) { |
| 1976 | if (!SS.isSet() || SS.isInvalid() || T.isNull()) |
| 1977 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1978 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1979 | NestedNameSpecifier *NNS |
Douglas Gregor | 3507369 | 2009-03-26 23:56:24 +0000 | [diff] [blame] | 1980 | = static_cast<NestedNameSpecifier *>(SS.getScopeRep()); |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1981 | return Context.getQualifiedNameType(NNS, T); |
Douglas Gregor | e625893 | 2009-03-19 00:39:20 +0000 | [diff] [blame] | 1982 | } |
Anders Carlsson | af017e6 | 2009-06-29 22:58:55 +0000 | [diff] [blame] | 1983 | |
| 1984 | QualType Sema::BuildTypeofExprType(Expr *E) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 1985 | if (E->getType() == Context.OverloadTy) { |
| 1986 | // C++ [temp.arg.explicit]p3 allows us to resolve a template-id to a |
| 1987 | // function template specialization wherever deduction cannot occur. |
| 1988 | if (FunctionDecl *Specialization |
| 1989 | = ResolveSingleFunctionTemplateSpecialization(E)) { |
| 1990 | E = FixOverloadedFunctionReference(E, Specialization); |
| 1991 | if (!E) |
| 1992 | return QualType(); |
| 1993 | } else { |
| 1994 | Diag(E->getLocStart(), |
| 1995 | diag::err_cannot_determine_declared_type_of_overloaded_function) |
| 1996 | << false << E->getSourceRange(); |
| 1997 | return QualType(); |
| 1998 | } |
| 1999 | } |
| 2000 | |
Anders Carlsson | af017e6 | 2009-06-29 22:58:55 +0000 | [diff] [blame] | 2001 | return Context.getTypeOfExprType(E); |
| 2002 | } |
| 2003 | |
| 2004 | QualType Sema::BuildDecltypeType(Expr *E) { |
| 2005 | if (E->getType() == Context.OverloadTy) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 2006 | // C++ [temp.arg.explicit]p3 allows us to resolve a template-id to a |
| 2007 | // function template specialization wherever deduction cannot occur. |
| 2008 | if (FunctionDecl *Specialization |
| 2009 | = ResolveSingleFunctionTemplateSpecialization(E)) { |
| 2010 | E = FixOverloadedFunctionReference(E, Specialization); |
| 2011 | if (!E) |
| 2012 | return QualType(); |
| 2013 | } else { |
| 2014 | Diag(E->getLocStart(), |
| 2015 | diag::err_cannot_determine_declared_type_of_overloaded_function) |
| 2016 | << true << E->getSourceRange(); |
| 2017 | return QualType(); |
| 2018 | } |
Anders Carlsson | af017e6 | 2009-06-29 22:58:55 +0000 | [diff] [blame] | 2019 | } |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 2020 | |
Anders Carlsson | af017e6 | 2009-06-29 22:58:55 +0000 | [diff] [blame] | 2021 | return Context.getDecltypeType(E); |
| 2022 | } |