Chris Lattner | 289ab7b | 2006-11-08 06:54:53 +0000 | [diff] [blame] | 1 | //===--- SemaDeclSpec.cpp - Declaration Specifier Semantic Analysis -------===// |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | 289ab7b | 2006-11-08 06:54:53 +0000 | [diff] [blame] | 10 | // This file implements semantic analysis for declaration specifiers. |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 14 | #include "clang/Parse/ParseDiagnostic.h" // FIXME: remove this back-dependency! |
| 15 | #include "clang/Sema/DeclSpec.h" |
Douglas Gregor | c15b0cf | 2011-06-25 00:56:27 +0000 | [diff] [blame] | 16 | #include "clang/Sema/LocInfoType.h" |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 17 | #include "clang/Sema/ParsedTemplate.h" |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 18 | #include "clang/Sema/Sema.h" |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | c15b0cf | 2011-06-25 00:56:27 +0000 | [diff] [blame] | 20 | #include "clang/AST/Expr.h" |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 21 | #include "clang/AST/NestedNameSpecifier.h" |
| 22 | #include "clang/AST/TypeLoc.h" |
Douglas Gregor | e3e01a2 | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 23 | #include "clang/Lex/Preprocessor.h" |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 24 | #include "clang/Basic/LangOptions.h" |
Chris Lattner | 1ce41ed | 2009-01-20 19:11:22 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/STLExtras.h" |
John McCall | 898cd0f | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ErrorHandling.h" |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 27 | #include <cstring> |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 28 | using namespace clang; |
| 29 | |
Chris Lattner | 3b0f3ef | 2008-11-22 08:32:36 +0000 | [diff] [blame] | 30 | |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 31 | static DiagnosticBuilder Diag(DiagnosticsEngine &D, SourceLocation Loc, |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 32 | unsigned DiagID) { |
| 33 | return D.Report(Loc, DiagID); |
Chris Lattner | 3b0f3ef | 2008-11-22 08:32:36 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 36 | |
| 37 | void UnqualifiedId::setTemplateId(TemplateIdAnnotation *TemplateId) { |
| 38 | assert(TemplateId && "NULL template-id annotation?"); |
| 39 | Kind = IK_TemplateId; |
| 40 | this->TemplateId = TemplateId; |
| 41 | StartLocation = TemplateId->TemplateNameLoc; |
| 42 | EndLocation = TemplateId->RAngleLoc; |
| 43 | } |
| 44 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 45 | void UnqualifiedId::setConstructorTemplateId(TemplateIdAnnotation *TemplateId) { |
| 46 | assert(TemplateId && "NULL template-id annotation?"); |
| 47 | Kind = IK_ConstructorTemplateId; |
| 48 | this->TemplateId = TemplateId; |
| 49 | StartLocation = TemplateId->TemplateNameLoc; |
| 50 | EndLocation = TemplateId->RAngleLoc; |
| 51 | } |
| 52 | |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 53 | void CXXScopeSpec::Extend(ASTContext &Context, SourceLocation TemplateKWLoc, |
| 54 | TypeLoc TL, SourceLocation ColonColonLoc) { |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 55 | Builder.Extend(Context, TemplateKWLoc, TL, ColonColonLoc); |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 56 | if (Range.getBegin().isInvalid()) |
| 57 | Range.setBegin(TL.getBeginLoc()); |
| 58 | Range.setEnd(ColonColonLoc); |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 59 | |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 60 | assert(Range == Builder.getSourceRange() && |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 61 | "NestedNameSpecifierLoc range computation incorrect"); |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void CXXScopeSpec::Extend(ASTContext &Context, IdentifierInfo *Identifier, |
| 65 | SourceLocation IdentifierLoc, |
| 66 | SourceLocation ColonColonLoc) { |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 67 | Builder.Extend(Context, Identifier, IdentifierLoc, ColonColonLoc); |
| 68 | |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 69 | if (Range.getBegin().isInvalid()) |
| 70 | Range.setBegin(IdentifierLoc); |
| 71 | Range.setEnd(ColonColonLoc); |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 72 | |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 73 | assert(Range == Builder.getSourceRange() && |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 74 | "NestedNameSpecifierLoc range computation incorrect"); |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void CXXScopeSpec::Extend(ASTContext &Context, NamespaceDecl *Namespace, |
| 78 | SourceLocation NamespaceLoc, |
| 79 | SourceLocation ColonColonLoc) { |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 80 | Builder.Extend(Context, Namespace, NamespaceLoc, ColonColonLoc); |
| 81 | |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 82 | if (Range.getBegin().isInvalid()) |
| 83 | Range.setBegin(NamespaceLoc); |
| 84 | Range.setEnd(ColonColonLoc); |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 85 | |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 86 | assert(Range == Builder.getSourceRange() && |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 87 | "NestedNameSpecifierLoc range computation incorrect"); |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 90 | void CXXScopeSpec::Extend(ASTContext &Context, NamespaceAliasDecl *Alias, |
| 91 | SourceLocation AliasLoc, |
| 92 | SourceLocation ColonColonLoc) { |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 93 | Builder.Extend(Context, Alias, AliasLoc, ColonColonLoc); |
| 94 | |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 95 | if (Range.getBegin().isInvalid()) |
| 96 | Range.setBegin(AliasLoc); |
| 97 | Range.setEnd(ColonColonLoc); |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 98 | |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 99 | assert(Range == Builder.getSourceRange() && |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 100 | "NestedNameSpecifierLoc range computation incorrect"); |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 103 | void CXXScopeSpec::MakeGlobal(ASTContext &Context, |
| 104 | SourceLocation ColonColonLoc) { |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 105 | Builder.MakeGlobal(Context, ColonColonLoc); |
| 106 | |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 107 | Range = SourceRange(ColonColonLoc); |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 108 | |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 109 | assert(Range == Builder.getSourceRange() && |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 110 | "NestedNameSpecifierLoc range computation incorrect"); |
| 111 | } |
| 112 | |
| 113 | void CXXScopeSpec::MakeTrivial(ASTContext &Context, |
| 114 | NestedNameSpecifier *Qualifier, SourceRange R) { |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 115 | Builder.MakeTrivial(Context, Qualifier, R); |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 116 | Range = R; |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | void CXXScopeSpec::Adopt(NestedNameSpecifierLoc Other) { |
| 120 | if (!Other) { |
| 121 | Range = SourceRange(); |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 122 | Builder.Clear(); |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 123 | return; |
| 124 | } |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 125 | |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 126 | Range = Other.getSourceRange(); |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 127 | Builder.Adopt(Other); |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 128 | } |
| 129 | |
John McCall | 2187876 | 2011-07-06 06:57:57 +0000 | [diff] [blame] | 130 | SourceLocation CXXScopeSpec::getLastQualifierNameLoc() const { |
| 131 | if (!Builder.getRepresentation()) |
| 132 | return SourceLocation(); |
| 133 | return Builder.getTemporary().getLocalBeginLoc(); |
| 134 | } |
| 135 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 136 | NestedNameSpecifierLoc |
| 137 | CXXScopeSpec::getWithLocInContext(ASTContext &Context) const { |
Douglas Gregor | 2b1ca9e | 2011-03-03 21:48:55 +0000 | [diff] [blame] | 138 | if (!Builder.getRepresentation()) |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 139 | return NestedNameSpecifierLoc(); |
| 140 | |
Douglas Gregor | 9b27251 | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 141 | return Builder.getWithLocInContext(Context); |
Douglas Gregor | 90c9972 | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Chris Lattner | 1ce41ed | 2009-01-20 19:11:22 +0000 | [diff] [blame] | 144 | /// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function. |
| 145 | /// "TheDeclarator" is the declarator that this will be added to. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 146 | DeclaratorChunk DeclaratorChunk::getFunction(bool hasProto, bool isVariadic, |
Douglas Gregor | 94349fd | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 147 | SourceLocation EllipsisLoc, |
Chris Lattner | 1ce41ed | 2009-01-20 19:11:22 +0000 | [diff] [blame] | 148 | ParamInfo *ArgInfo, |
| 149 | unsigned NumArgs, |
| 150 | unsigned TypeQuals, |
Douglas Gregor | 5499235 | 2011-01-26 03:43:54 +0000 | [diff] [blame] | 151 | bool RefQualifierIsLvalueRef, |
| 152 | SourceLocation RefQualifierLoc, |
Douglas Gregor | ad69e65 | 2011-07-13 21:47:47 +0000 | [diff] [blame] | 153 | SourceLocation MutableLoc, |
Sebastian Redl | 802a453 | 2011-03-05 22:42:13 +0000 | [diff] [blame] | 154 | ExceptionSpecificationType |
| 155 | ESpecType, |
| 156 | SourceLocation ESpecLoc, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 157 | ParsedType *Exceptions, |
Sebastian Redl | d643456 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 158 | SourceRange *ExceptionRanges, |
Sebastian Redl | 2b9cacb | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 159 | unsigned NumExceptions, |
Sebastian Redl | 802a453 | 2011-03-05 22:42:13 +0000 | [diff] [blame] | 160 | Expr *NoexceptExpr, |
Abramo Bagnara | f2a79d9 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 161 | SourceLocation LocalRangeBegin, |
| 162 | SourceLocation LocalRangeEnd, |
Douglas Gregor | 7fb2541 | 2010-10-01 18:44:50 +0000 | [diff] [blame] | 163 | Declarator &TheDeclarator, |
| 164 | ParsedType TrailingReturnType) { |
Chris Lattner | 1ce41ed | 2009-01-20 19:11:22 +0000 | [diff] [blame] | 165 | DeclaratorChunk I; |
Sebastian Redl | 802a453 | 2011-03-05 22:42:13 +0000 | [diff] [blame] | 166 | I.Kind = Function; |
Abramo Bagnara | f2a79d9 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 167 | I.Loc = LocalRangeBegin; |
| 168 | I.EndLoc = LocalRangeEnd; |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 169 | I.Fun.AttrList = 0; |
Sebastian Redl | 802a453 | 2011-03-05 22:42:13 +0000 | [diff] [blame] | 170 | I.Fun.hasPrototype = hasProto; |
| 171 | I.Fun.isVariadic = isVariadic; |
| 172 | I.Fun.EllipsisLoc = EllipsisLoc.getRawEncoding(); |
| 173 | I.Fun.DeleteArgInfo = false; |
| 174 | I.Fun.TypeQuals = TypeQuals; |
| 175 | I.Fun.NumArgs = NumArgs; |
| 176 | I.Fun.ArgInfo = 0; |
Douglas Gregor | 5499235 | 2011-01-26 03:43:54 +0000 | [diff] [blame] | 177 | I.Fun.RefQualifierIsLValueRef = RefQualifierIsLvalueRef; |
Sebastian Redl | 802a453 | 2011-03-05 22:42:13 +0000 | [diff] [blame] | 178 | I.Fun.RefQualifierLoc = RefQualifierLoc.getRawEncoding(); |
Douglas Gregor | ad69e65 | 2011-07-13 21:47:47 +0000 | [diff] [blame] | 179 | I.Fun.MutableLoc = MutableLoc.getRawEncoding(); |
Sebastian Redl | 802a453 | 2011-03-05 22:42:13 +0000 | [diff] [blame] | 180 | I.Fun.ExceptionSpecType = ESpecType; |
| 181 | I.Fun.ExceptionSpecLoc = ESpecLoc.getRawEncoding(); |
| 182 | I.Fun.NumExceptions = 0; |
| 183 | I.Fun.Exceptions = 0; |
| 184 | I.Fun.NoexceptExpr = 0; |
Douglas Gregor | 7fb2541 | 2010-10-01 18:44:50 +0000 | [diff] [blame] | 185 | I.Fun.TrailingReturnType = TrailingReturnType.getAsOpaquePtr(); |
Sebastian Redl | 2b9cacb | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 186 | |
Chris Lattner | 1ce41ed | 2009-01-20 19:11:22 +0000 | [diff] [blame] | 187 | // new[] an argument array if needed. |
| 188 | if (NumArgs) { |
| 189 | // If the 'InlineParams' in Declarator is unused and big enough, put our |
| 190 | // parameter list there (in an effort to avoid new/delete traffic). If it |
| 191 | // is already used (consider a function returning a function pointer) or too |
| 192 | // small (function taking too many arguments), go to the heap. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 193 | if (!TheDeclarator.InlineParamsUsed && |
Chris Lattner | 1ce41ed | 2009-01-20 19:11:22 +0000 | [diff] [blame] | 194 | NumArgs <= llvm::array_lengthof(TheDeclarator.InlineParams)) { |
| 195 | I.Fun.ArgInfo = TheDeclarator.InlineParams; |
| 196 | I.Fun.DeleteArgInfo = false; |
| 197 | TheDeclarator.InlineParamsUsed = true; |
| 198 | } else { |
| 199 | I.Fun.ArgInfo = new DeclaratorChunk::ParamInfo[NumArgs]; |
| 200 | I.Fun.DeleteArgInfo = true; |
| 201 | } |
| 202 | memcpy(I.Fun.ArgInfo, ArgInfo, sizeof(ArgInfo[0])*NumArgs); |
| 203 | } |
Sebastian Redl | 802a453 | 2011-03-05 22:42:13 +0000 | [diff] [blame] | 204 | |
| 205 | // Check what exception specification information we should actually store. |
| 206 | switch (ESpecType) { |
| 207 | default: break; // By default, save nothing. |
| 208 | case EST_Dynamic: |
| 209 | // new[] an exception array if needed |
| 210 | if (NumExceptions) { |
| 211 | I.Fun.NumExceptions = NumExceptions; |
| 212 | I.Fun.Exceptions = new DeclaratorChunk::TypeAndRange[NumExceptions]; |
| 213 | for (unsigned i = 0; i != NumExceptions; ++i) { |
| 214 | I.Fun.Exceptions[i].Ty = Exceptions[i]; |
| 215 | I.Fun.Exceptions[i].Range = ExceptionRanges[i]; |
| 216 | } |
Sebastian Redl | d643456 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 217 | } |
Sebastian Redl | 802a453 | 2011-03-05 22:42:13 +0000 | [diff] [blame] | 218 | break; |
| 219 | |
| 220 | case EST_ComputedNoexcept: |
| 221 | I.Fun.NoexceptExpr = NoexceptExpr; |
| 222 | break; |
Sebastian Redl | 2b9cacb | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 223 | } |
Chris Lattner | 1ce41ed | 2009-01-20 19:11:22 +0000 | [diff] [blame] | 224 | return I; |
| 225 | } |
Chris Lattner | 3b0f3ef | 2008-11-22 08:32:36 +0000 | [diff] [blame] | 226 | |
Douglas Gregor | c15b0cf | 2011-06-25 00:56:27 +0000 | [diff] [blame] | 227 | bool Declarator::isDeclarationOfFunction() const { |
Richard Smith | cfcdf3a | 2011-06-25 02:28:38 +0000 | [diff] [blame] | 228 | for (unsigned i = 0, i_end = DeclTypeInfo.size(); i < i_end; ++i) { |
| 229 | switch (DeclTypeInfo[i].Kind) { |
| 230 | case DeclaratorChunk::Function: |
| 231 | return true; |
| 232 | case DeclaratorChunk::Paren: |
| 233 | continue; |
| 234 | case DeclaratorChunk::Pointer: |
| 235 | case DeclaratorChunk::Reference: |
| 236 | case DeclaratorChunk::Array: |
| 237 | case DeclaratorChunk::BlockPointer: |
| 238 | case DeclaratorChunk::MemberPointer: |
| 239 | return false; |
| 240 | } |
| 241 | llvm_unreachable("Invalid type chunk"); |
| 242 | return false; |
| 243 | } |
Douglas Gregor | c15b0cf | 2011-06-25 00:56:27 +0000 | [diff] [blame] | 244 | |
| 245 | switch (DS.getTypeSpecType()) { |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 246 | case TST_atomic: |
Douglas Gregor | c15b0cf | 2011-06-25 00:56:27 +0000 | [diff] [blame] | 247 | case TST_auto: |
| 248 | case TST_bool: |
| 249 | case TST_char: |
| 250 | case TST_char16: |
| 251 | case TST_char32: |
| 252 | case TST_class: |
| 253 | case TST_decimal128: |
| 254 | case TST_decimal32: |
| 255 | case TST_decimal64: |
| 256 | case TST_double: |
| 257 | case TST_enum: |
| 258 | case TST_error: |
| 259 | case TST_float: |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 260 | case TST_half: |
Douglas Gregor | c15b0cf | 2011-06-25 00:56:27 +0000 | [diff] [blame] | 261 | case TST_int: |
| 262 | case TST_struct: |
| 263 | case TST_union: |
| 264 | case TST_unknown_anytype: |
| 265 | case TST_unspecified: |
| 266 | case TST_void: |
| 267 | case TST_wchar: |
| 268 | return false; |
| 269 | |
| 270 | case TST_decltype: |
| 271 | case TST_typeofExpr: |
| 272 | if (Expr *E = DS.getRepAsExpr()) |
| 273 | return E->getType()->isFunctionType(); |
| 274 | return false; |
| 275 | |
| 276 | case TST_underlyingType: |
| 277 | case TST_typename: |
| 278 | case TST_typeofType: { |
| 279 | QualType QT = DS.getRepAsType().get(); |
| 280 | if (QT.isNull()) |
| 281 | return false; |
| 282 | |
| 283 | if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) |
| 284 | QT = LIT->getType(); |
| 285 | |
| 286 | if (QT.isNull()) |
| 287 | return false; |
| 288 | |
| 289 | return QT->isFunctionType(); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | return false; |
| 294 | } |
| 295 | |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 296 | /// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this |
Chris Lattner | e0c5116 | 2009-02-27 18:35:46 +0000 | [diff] [blame] | 297 | /// declaration specifier includes. |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 298 | /// |
| 299 | unsigned DeclSpec::getParsedSpecifiers() const { |
| 300 | unsigned Res = 0; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 301 | if (StorageClassSpec != SCS_unspecified || |
| 302 | SCS_thread_specified) |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 303 | Res |= PQ_StorageClassSpecifier; |
Mike Stump | 65643c6 | 2008-06-19 19:52:46 +0000 | [diff] [blame] | 304 | |
Chris Lattner | 4d8f873 | 2006-11-28 05:05:08 +0000 | [diff] [blame] | 305 | if (TypeQualifiers != TQ_unspecified) |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 306 | Res |= PQ_TypeQualifier; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 307 | |
Chris Lattner | f055d43 | 2006-11-28 04:28:12 +0000 | [diff] [blame] | 308 | if (hasTypeSpecifier()) |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 309 | Res |= PQ_TypeSpecifier; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 310 | |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 311 | if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified) |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 312 | Res |= PQ_FunctionSpecifier; |
| 313 | return Res; |
| 314 | } |
| 315 | |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 316 | template <class T> static bool BadSpecifier(T TNew, T TPrev, |
| 317 | const char *&PrevSpec, |
| 318 | unsigned &DiagID) { |
John McCall | 898cd0f | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 319 | PrevSpec = DeclSpec::getSpecifierName(TPrev); |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 320 | DiagID = (TNew == TPrev ? diag::ext_duplicate_declspec |
| 321 | : diag::err_invalid_decl_spec_combination); |
John McCall | 898cd0f | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 322 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 323 | } |
John McCall | 898cd0f | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 324 | |
Chris Lattner | 7bd11fe | 2007-01-23 04:35:33 +0000 | [diff] [blame] | 325 | const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) { |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 326 | switch (S) { |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 327 | case DeclSpec::SCS_unspecified: return "unspecified"; |
| 328 | case DeclSpec::SCS_typedef: return "typedef"; |
| 329 | case DeclSpec::SCS_extern: return "extern"; |
| 330 | case DeclSpec::SCS_static: return "static"; |
| 331 | case DeclSpec::SCS_auto: return "auto"; |
| 332 | case DeclSpec::SCS_register: return "register"; |
Eli Friedman | d5c0eed | 2009-04-19 20:27:55 +0000 | [diff] [blame] | 333 | case DeclSpec::SCS_private_extern: return "__private_extern__"; |
Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 334 | case DeclSpec::SCS_mutable: return "mutable"; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 335 | } |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 336 | llvm_unreachable("Unknown typespec!"); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 337 | } |
| 338 | |
John McCall | 898cd0f | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 339 | const char *DeclSpec::getSpecifierName(TSW W) { |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 340 | switch (W) { |
John McCall | 898cd0f | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 341 | case TSW_unspecified: return "unspecified"; |
| 342 | case TSW_short: return "short"; |
| 343 | case TSW_long: return "long"; |
| 344 | case TSW_longlong: return "long long"; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 345 | } |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 346 | llvm_unreachable("Unknown typespec!"); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 347 | } |
| 348 | |
John McCall | 898cd0f | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 349 | const char *DeclSpec::getSpecifierName(TSC C) { |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 350 | switch (C) { |
John McCall | 898cd0f | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 351 | case TSC_unspecified: return "unspecified"; |
| 352 | case TSC_imaginary: return "imaginary"; |
| 353 | case TSC_complex: return "complex"; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 354 | } |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 355 | llvm_unreachable("Unknown typespec!"); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | |
John McCall | 898cd0f | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 359 | const char *DeclSpec::getSpecifierName(TSS S) { |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 360 | switch (S) { |
John McCall | 898cd0f | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 361 | case TSS_unspecified: return "unspecified"; |
| 362 | case TSS_signed: return "signed"; |
| 363 | case TSS_unsigned: return "unsigned"; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 364 | } |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 365 | llvm_unreachable("Unknown typespec!"); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Chris Lattner | 69680ea | 2007-01-23 04:34:43 +0000 | [diff] [blame] | 368 | const char *DeclSpec::getSpecifierName(DeclSpec::TST T) { |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 369 | switch (T) { |
Chris Lattner | 839713c | 2006-08-04 06:15:52 +0000 | [diff] [blame] | 370 | case DeclSpec::TST_unspecified: return "unspecified"; |
| 371 | case DeclSpec::TST_void: return "void"; |
| 372 | case DeclSpec::TST_char: return "char"; |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 373 | case DeclSpec::TST_wchar: return "wchar_t"; |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 374 | case DeclSpec::TST_char16: return "char16_t"; |
| 375 | case DeclSpec::TST_char32: return "char32_t"; |
Chris Lattner | 839713c | 2006-08-04 06:15:52 +0000 | [diff] [blame] | 376 | case DeclSpec::TST_int: return "int"; |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 377 | case DeclSpec::TST_half: return "half"; |
Chris Lattner | 839713c | 2006-08-04 06:15:52 +0000 | [diff] [blame] | 378 | case DeclSpec::TST_float: return "float"; |
| 379 | case DeclSpec::TST_double: return "double"; |
| 380 | case DeclSpec::TST_bool: return "_Bool"; |
| 381 | case DeclSpec::TST_decimal32: return "_Decimal32"; |
| 382 | case DeclSpec::TST_decimal64: return "_Decimal64"; |
| 383 | case DeclSpec::TST_decimal128: return "_Decimal128"; |
Chris Lattner | da72c82 | 2006-08-13 22:16:42 +0000 | [diff] [blame] | 384 | case DeclSpec::TST_enum: return "enum"; |
Chris Lattner | 861a226 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 385 | case DeclSpec::TST_class: return "class"; |
Chris Lattner | da72c82 | 2006-08-13 22:16:42 +0000 | [diff] [blame] | 386 | case DeclSpec::TST_union: return "union"; |
| 387 | case DeclSpec::TST_struct: return "struct"; |
Douglas Gregor | 9817f4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 388 | case DeclSpec::TST_typename: return "type-name"; |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 389 | case DeclSpec::TST_typeofType: |
| 390 | case DeclSpec::TST_typeofExpr: return "typeof"; |
John McCall | 898cd0f | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 391 | case DeclSpec::TST_auto: return "auto"; |
| 392 | case DeclSpec::TST_decltype: return "(decltype)"; |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 393 | case DeclSpec::TST_underlyingType: return "__underlying_type"; |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 394 | case DeclSpec::TST_unknown_anytype: return "__unknown_anytype"; |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 395 | case DeclSpec::TST_atomic: return "_Atomic"; |
John McCall | 898cd0f | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 396 | case DeclSpec::TST_error: return "(error)"; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 397 | } |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 398 | llvm_unreachable("Unknown typespec!"); |
Chris Lattner | 839713c | 2006-08-04 06:15:52 +0000 | [diff] [blame] | 399 | } |
| 400 | |
John McCall | 898cd0f | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 401 | const char *DeclSpec::getSpecifierName(TQ T) { |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 402 | switch (T) { |
John McCall | 898cd0f | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 403 | case DeclSpec::TQ_unspecified: return "unspecified"; |
| 404 | case DeclSpec::TQ_const: return "const"; |
| 405 | case DeclSpec::TQ_restrict: return "restrict"; |
| 406 | case DeclSpec::TQ_volatile: return "volatile"; |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 407 | } |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 408 | llvm_unreachable("Unknown typespec!"); |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 409 | } |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 410 | |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 411 | bool DeclSpec::SetStorageClassSpec(Sema &S, SCS SC, SourceLocation Loc, |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 412 | const char *&PrevSpec, |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 413 | unsigned &DiagID) { |
| 414 | // OpenCL 1.1 6.8g: "The extern, static, auto and register storage-class |
| 415 | // specifiers are not supported." |
Peter Collingbourne | de32b20 | 2011-02-11 19:59:54 +0000 | [diff] [blame] | 416 | // It seems sensible to prohibit private_extern too |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 417 | // The cl_clang_storage_class_specifiers extension enables support for |
| 418 | // these storage-class specifiers. |
| 419 | if (S.getLangOptions().OpenCL && |
| 420 | !S.getOpenCLOptions().cl_clang_storage_class_specifiers) { |
| 421 | switch (SC) { |
Peter Collingbourne | de32b20 | 2011-02-11 19:59:54 +0000 | [diff] [blame] | 422 | case SCS_extern: |
| 423 | case SCS_private_extern: |
| 424 | case SCS_auto: |
| 425 | case SCS_register: |
| 426 | case SCS_static: |
| 427 | DiagID = diag::err_not_opencl_storage_class_specifier; |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 428 | PrevSpec = getSpecifierName(SC); |
Peter Collingbourne | de32b20 | 2011-02-11 19:59:54 +0000 | [diff] [blame] | 429 | return true; |
| 430 | default: |
| 431 | break; |
| 432 | } |
| 433 | } |
| 434 | |
Abramo Bagnara | ed5b689 | 2010-07-30 16:47:02 +0000 | [diff] [blame] | 435 | if (StorageClassSpec != SCS_unspecified) { |
Richard Smith | 58c7433 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 436 | // Maybe this is an attempt to use C++0x 'auto' outside of C++0x mode. |
| 437 | bool isInvalid = true; |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 438 | if (TypeSpecType == TST_unspecified && S.getLangOptions().CPlusPlus) { |
| 439 | if (SC == SCS_auto) |
Richard Smith | 58c7433 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 440 | return SetTypeSpecType(TST_auto, Loc, PrevSpec, DiagID); |
| 441 | if (StorageClassSpec == SCS_auto) { |
| 442 | isInvalid = SetTypeSpecType(TST_auto, StorageClassSpecLoc, |
| 443 | PrevSpec, DiagID); |
| 444 | assert(!isInvalid && "auto SCS -> TST recovery failed"); |
| 445 | } |
| 446 | } |
| 447 | |
Abramo Bagnara | ed5b689 | 2010-07-30 16:47:02 +0000 | [diff] [blame] | 448 | // Changing storage class is allowed only if the previous one |
| 449 | // was the 'extern' that is part of a linkage specification and |
| 450 | // the new storage class is 'typedef'. |
Richard Smith | 58c7433 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 451 | if (isInvalid && |
| 452 | !(SCS_extern_in_linkage_spec && |
Abramo Bagnara | ed5b689 | 2010-07-30 16:47:02 +0000 | [diff] [blame] | 453 | StorageClassSpec == SCS_extern && |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 454 | SC == SCS_typedef)) |
| 455 | return BadSpecifier(SC, (SCS)StorageClassSpec, PrevSpec, DiagID); |
Abramo Bagnara | ed5b689 | 2010-07-30 16:47:02 +0000 | [diff] [blame] | 456 | } |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 457 | StorageClassSpec = SC; |
Chris Lattner | 4d8f873 | 2006-11-28 05:05:08 +0000 | [diff] [blame] | 458 | StorageClassSpecLoc = Loc; |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 459 | assert((unsigned)SC == StorageClassSpec && "SCS constants overflow bitfield"); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 460 | return false; |
| 461 | } |
| 462 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 463 | bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc, |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 464 | const char *&PrevSpec, |
| 465 | unsigned &DiagID) { |
Chris Lattner | 353f574 | 2006-11-28 04:50:12 +0000 | [diff] [blame] | 466 | if (SCS_thread_specified) { |
| 467 | PrevSpec = "__thread"; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 468 | DiagID = diag::ext_duplicate_declspec; |
Chris Lattner | 353f574 | 2006-11-28 04:50:12 +0000 | [diff] [blame] | 469 | return true; |
| 470 | } |
| 471 | SCS_thread_specified = true; |
Chris Lattner | 4d8f873 | 2006-11-28 05:05:08 +0000 | [diff] [blame] | 472 | SCS_threadLoc = Loc; |
Chris Lattner | 353f574 | 2006-11-28 04:50:12 +0000 | [diff] [blame] | 473 | return false; |
| 474 | } |
| 475 | |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 476 | /// These methods set the specified attribute of the DeclSpec, but return true |
| 477 | /// and ignore the request if invalid (e.g. "extern" then "auto" is |
| 478 | /// specified). |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 479 | bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc, |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 480 | const char *&PrevSpec, |
| 481 | unsigned &DiagID) { |
Abramo Bagnara | ead67d9 | 2011-03-06 22:21:56 +0000 | [diff] [blame] | 482 | // Overwrite TSWLoc only if TypeSpecWidth was unspecified, so that |
| 483 | // for 'long long' we will keep the source location of the first 'long'. |
| 484 | if (TypeSpecWidth == TSW_unspecified) |
| 485 | TSWLoc = Loc; |
| 486 | // Allow turning long -> long long. |
| 487 | else if (W != TSW_longlong || TypeSpecWidth != TSW_long) |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 488 | return BadSpecifier(W, (TSW)TypeSpecWidth, PrevSpec, DiagID); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 489 | TypeSpecWidth = W; |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 490 | if (TypeAltiVecVector && !TypeAltiVecBool && |
| 491 | ((TypeSpecWidth == TSW_long) || (TypeSpecWidth == TSW_longlong))) { |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 492 | PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType); |
| 493 | DiagID = diag::warn_vector_long_decl_spec_combination; |
| 494 | return true; |
| 495 | } |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 496 | return false; |
| 497 | } |
| 498 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 499 | bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc, |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 500 | const char *&PrevSpec, |
| 501 | unsigned &DiagID) { |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 502 | if (TypeSpecComplex != TSC_unspecified) |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 503 | return BadSpecifier(C, (TSC)TypeSpecComplex, PrevSpec, DiagID); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 504 | TypeSpecComplex = C; |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 505 | TSCLoc = Loc; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 506 | return false; |
| 507 | } |
| 508 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 509 | bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc, |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 510 | const char *&PrevSpec, |
| 511 | unsigned &DiagID) { |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 512 | if (TypeSpecSign != TSS_unspecified) |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 513 | return BadSpecifier(S, (TSS)TypeSpecSign, PrevSpec, DiagID); |
Chris Lattner | deb42f5 | 2006-08-04 05:26:52 +0000 | [diff] [blame] | 514 | TypeSpecSign = S; |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 515 | TSSLoc = Loc; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 516 | return false; |
| 517 | } |
| 518 | |
Chris Lattner | b20e894 | 2006-11-28 05:30:29 +0000 | [diff] [blame] | 519 | bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc, |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 520 | const char *&PrevSpec, |
| 521 | unsigned &DiagID, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 522 | ParsedType Rep) { |
Abramo Bagnara | 9875a3c | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 523 | return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep); |
| 524 | } |
| 525 | |
| 526 | bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc, |
| 527 | SourceLocation TagNameLoc, |
| 528 | const char *&PrevSpec, |
| 529 | unsigned &DiagID, |
| 530 | ParsedType Rep) { |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 531 | assert(isTypeRep(T) && "T does not store a type"); |
| 532 | assert(Rep && "no type provided!"); |
| 533 | if (TypeSpecType != TST_unspecified) { |
| 534 | PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType); |
| 535 | DiagID = diag::err_invalid_decl_spec_combination; |
| 536 | return true; |
| 537 | } |
| 538 | TypeSpecType = T; |
| 539 | TypeRep = Rep; |
Abramo Bagnara | 9875a3c | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 540 | TSTLoc = TagKwLoc; |
| 541 | TSTNameLoc = TagNameLoc; |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 542 | TypeSpecOwned = false; |
| 543 | return false; |
| 544 | } |
| 545 | |
| 546 | bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc, |
| 547 | const char *&PrevSpec, |
| 548 | unsigned &DiagID, |
| 549 | Expr *Rep) { |
| 550 | assert(isExprRep(T) && "T does not store an expr"); |
| 551 | assert(Rep && "no expression provided!"); |
| 552 | if (TypeSpecType != TST_unspecified) { |
| 553 | PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType); |
| 554 | DiagID = diag::err_invalid_decl_spec_combination; |
| 555 | return true; |
| 556 | } |
| 557 | TypeSpecType = T; |
| 558 | ExprRep = Rep; |
| 559 | TSTLoc = Loc; |
Abramo Bagnara | 9875a3c | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 560 | TSTNameLoc = Loc; |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 561 | TypeSpecOwned = false; |
| 562 | return false; |
| 563 | } |
| 564 | |
| 565 | bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc, |
| 566 | const char *&PrevSpec, |
| 567 | unsigned &DiagID, |
| 568 | Decl *Rep, bool Owned) { |
Abramo Bagnara | 9875a3c | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 569 | return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep, Owned); |
| 570 | } |
| 571 | |
| 572 | bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc, |
| 573 | SourceLocation TagNameLoc, |
| 574 | const char *&PrevSpec, |
| 575 | unsigned &DiagID, |
| 576 | Decl *Rep, bool Owned) { |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 577 | assert(isDeclRep(T) && "T does not store a decl"); |
| 578 | // Unlike the other cases, we don't assert that we actually get a decl. |
| 579 | |
| 580 | if (TypeSpecType != TST_unspecified) { |
| 581 | PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType); |
| 582 | DiagID = diag::err_invalid_decl_spec_combination; |
| 583 | return true; |
| 584 | } |
| 585 | TypeSpecType = T; |
| 586 | DeclRep = Rep; |
Abramo Bagnara | 9875a3c | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 587 | TSTLoc = TagKwLoc; |
| 588 | TSTNameLoc = TagNameLoc; |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 589 | TypeSpecOwned = Owned; |
| 590 | return false; |
| 591 | } |
| 592 | |
| 593 | bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc, |
| 594 | const char *&PrevSpec, |
| 595 | unsigned &DiagID) { |
| 596 | assert(!isDeclRep(T) && !isTypeRep(T) && !isExprRep(T) && |
| 597 | "rep required for these type-spec kinds!"); |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 598 | if (TypeSpecType != TST_unspecified) { |
| 599 | PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType); |
| 600 | DiagID = diag::err_invalid_decl_spec_combination; |
| 601 | return true; |
| 602 | } |
Abramo Bagnara | 9875a3c | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 603 | TSTLoc = Loc; |
| 604 | TSTNameLoc = Loc; |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 605 | if (TypeAltiVecVector && (T == TST_bool) && !TypeAltiVecBool) { |
| 606 | TypeAltiVecBool = true; |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 607 | return false; |
| 608 | } |
Chris Lattner | deb42f5 | 2006-08-04 05:26:52 +0000 | [diff] [blame] | 609 | TypeSpecType = T; |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 610 | TypeSpecOwned = false; |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 611 | if (TypeAltiVecVector && !TypeAltiVecBool && (TypeSpecType == TST_double)) { |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 612 | PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType); |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 613 | DiagID = diag::err_invalid_vector_decl_spec; |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 614 | return true; |
| 615 | } |
| 616 | return false; |
| 617 | } |
| 618 | |
| 619 | bool DeclSpec::SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc, |
| 620 | const char *&PrevSpec, unsigned &DiagID) { |
| 621 | if (TypeSpecType != TST_unspecified) { |
| 622 | PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType); |
| 623 | DiagID = diag::err_invalid_vector_decl_spec_combination; |
| 624 | return true; |
| 625 | } |
| 626 | TypeAltiVecVector = isAltiVecVector; |
| 627 | AltiVecLoc = Loc; |
| 628 | return false; |
| 629 | } |
| 630 | |
| 631 | bool DeclSpec::SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc, |
| 632 | const char *&PrevSpec, unsigned &DiagID) { |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 633 | if (!TypeAltiVecVector || TypeAltiVecPixel || |
| 634 | (TypeSpecType != TST_unspecified)) { |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 635 | PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType); |
| 636 | DiagID = diag::err_invalid_pixel_decl_spec_combination; |
| 637 | return true; |
| 638 | } |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 639 | TypeAltiVecPixel = isAltiVecPixel; |
| 640 | TSTLoc = Loc; |
Abramo Bagnara | 9875a3c | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 641 | TSTNameLoc = Loc; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 642 | return false; |
| 643 | } |
| 644 | |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 645 | bool DeclSpec::SetTypeSpecError() { |
| 646 | TypeSpecType = TST_error; |
John McCall | a3707cc | 2010-08-26 17:22:34 +0000 | [diff] [blame] | 647 | TypeSpecOwned = false; |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 648 | TSTLoc = SourceLocation(); |
Abramo Bagnara | 9875a3c | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 649 | TSTNameLoc = SourceLocation(); |
Douglas Gregor | cd72ba9 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 650 | return false; |
| 651 | } |
| 652 | |
Chris Lattner | 60809f5 | 2006-11-28 05:18:46 +0000 | [diff] [blame] | 653 | bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec, |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 654 | unsigned &DiagID, const LangOptions &Lang) { |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 655 | // Duplicates turn into warnings pre-C99. |
| 656 | if ((TypeQualifiers & T) && !Lang.C99) |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 657 | return BadSpecifier(T, T, PrevSpec, DiagID); |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 658 | TypeQualifiers |= T; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 659 | |
Chris Lattner | 60809f5 | 2006-11-28 05:18:46 +0000 | [diff] [blame] | 660 | switch (T) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 661 | default: llvm_unreachable("Unknown type qualifier!"); |
Chris Lattner | 60809f5 | 2006-11-28 05:18:46 +0000 | [diff] [blame] | 662 | case TQ_const: TQ_constLoc = Loc; break; |
| 663 | case TQ_restrict: TQ_restrictLoc = Loc; break; |
| 664 | case TQ_volatile: TQ_volatileLoc = Loc; break; |
| 665 | } |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 666 | return false; |
| 667 | } |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 668 | |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 669 | bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec, |
| 670 | unsigned &DiagID) { |
Chris Lattner | a925dc6 | 2006-11-28 04:33:46 +0000 | [diff] [blame] | 671 | // 'inline inline' is ok. |
| 672 | FS_inline_specified = true; |
Chris Lattner | 1b22eed | 2006-11-28 05:12:07 +0000 | [diff] [blame] | 673 | FS_inlineLoc = Loc; |
Chris Lattner | a925dc6 | 2006-11-28 04:33:46 +0000 | [diff] [blame] | 674 | return false; |
| 675 | } |
| 676 | |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 677 | bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec, |
| 678 | unsigned &DiagID) { |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 679 | // 'virtual virtual' is ok. |
| 680 | FS_virtual_specified = true; |
| 681 | FS_virtualLoc = Loc; |
| 682 | return false; |
| 683 | } |
| 684 | |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 685 | bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec, |
| 686 | unsigned &DiagID) { |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 687 | // 'explicit explicit' is ok. |
| 688 | FS_explicit_specified = true; |
| 689 | FS_explicitLoc = Loc; |
| 690 | return false; |
| 691 | } |
| 692 | |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 693 | bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec, |
| 694 | unsigned &DiagID) { |
Anders Carlsson | cd8db41 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 695 | if (Friend_specified) { |
| 696 | PrevSpec = "friend"; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 697 | DiagID = diag::ext_duplicate_declspec; |
Anders Carlsson | cd8db41 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 698 | return true; |
| 699 | } |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 700 | |
Anders Carlsson | cd8db41 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 701 | Friend_specified = true; |
| 702 | FriendLoc = Loc; |
| 703 | return false; |
| 704 | } |
Chris Lattner | a925dc6 | 2006-11-28 04:33:46 +0000 | [diff] [blame] | 705 | |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 706 | bool DeclSpec::setModulePrivateSpec(SourceLocation Loc, const char *&PrevSpec, |
| 707 | unsigned &DiagID) { |
| 708 | if (isModulePrivateSpecified()) { |
| 709 | PrevSpec = "__module_private__"; |
| 710 | DiagID = diag::ext_duplicate_declspec; |
| 711 | return true; |
| 712 | } |
| 713 | |
| 714 | ModulePrivateLoc = Loc; |
| 715 | return false; |
| 716 | } |
| 717 | |
Sebastian Redl | 39c2a8b | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 718 | bool DeclSpec::SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec, |
| 719 | unsigned &DiagID) { |
| 720 | // 'constexpr constexpr' is ok. |
| 721 | Constexpr_specified = true; |
| 722 | ConstexprLoc = Loc; |
| 723 | return false; |
| 724 | } |
| 725 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 726 | void DeclSpec::setProtocolQualifiers(Decl * const *Protos, |
Argyrios Kyrtzidis | 5ec645b | 2009-09-29 19:42:11 +0000 | [diff] [blame] | 727 | unsigned NP, |
| 728 | SourceLocation *ProtoLocs, |
| 729 | SourceLocation LAngleLoc) { |
| 730 | if (NP == 0) return; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 731 | ProtocolQualifiers = new Decl*[NP]; |
Argyrios Kyrtzidis | 5ec645b | 2009-09-29 19:42:11 +0000 | [diff] [blame] | 732 | ProtocolLocs = new SourceLocation[NP]; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 733 | memcpy((void*)ProtocolQualifiers, Protos, sizeof(Decl*)*NP); |
Argyrios Kyrtzidis | 5ec645b | 2009-09-29 19:42:11 +0000 | [diff] [blame] | 734 | memcpy(ProtocolLocs, ProtoLocs, sizeof(SourceLocation)*NP); |
| 735 | NumProtocolQualifiers = NP; |
| 736 | ProtocolLAngleLoc = LAngleLoc; |
| 737 | } |
| 738 | |
Douglas Gregor | c9b7a59 | 2010-01-18 18:04:31 +0000 | [diff] [blame] | 739 | void DeclSpec::SaveWrittenBuiltinSpecs() { |
| 740 | writtenBS.Sign = getTypeSpecSign(); |
| 741 | writtenBS.Width = getTypeSpecWidth(); |
| 742 | writtenBS.Type = getTypeSpecType(); |
| 743 | // Search the list of attributes for the presence of a mode attribute. |
| 744 | writtenBS.ModeAttr = false; |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 745 | AttributeList* attrs = getAttributes().getList(); |
Douglas Gregor | c9b7a59 | 2010-01-18 18:04:31 +0000 | [diff] [blame] | 746 | while (attrs) { |
| 747 | if (attrs->getKind() == AttributeList::AT_mode) { |
| 748 | writtenBS.ModeAttr = true; |
| 749 | break; |
| 750 | } |
| 751 | attrs = attrs->getNext(); |
| 752 | } |
| 753 | } |
| 754 | |
Abramo Bagnara | ed5b689 | 2010-07-30 16:47:02 +0000 | [diff] [blame] | 755 | void DeclSpec::SaveStorageSpecifierAsWritten() { |
| 756 | if (SCS_extern_in_linkage_spec && StorageClassSpec == SCS_extern) |
| 757 | // If 'extern' is part of a linkage specification, |
| 758 | // then it is not a storage class "as written". |
| 759 | StorageClassSpecAsWritten = SCS_unspecified; |
| 760 | else |
| 761 | StorageClassSpecAsWritten = StorageClassSpec; |
| 762 | } |
| 763 | |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 764 | /// Finish - This does final analysis of the declspec, rejecting things like |
| 765 | /// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or |
| 766 | /// diag::NUM_DIAGNOSTICS if there is no error. After calling this method, |
| 767 | /// DeclSpec is guaranteed self-consistent, even if an error occurred. |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 768 | void DeclSpec::Finish(DiagnosticsEngine &D, Preprocessor &PP) { |
Douglas Gregor | c9b7a59 | 2010-01-18 18:04:31 +0000 | [diff] [blame] | 769 | // Before possibly changing their values, save specs as written. |
| 770 | SaveWrittenBuiltinSpecs(); |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 771 | SaveStorageSpecifierAsWritten(); |
Douglas Gregor | c9b7a59 | 2010-01-18 18:04:31 +0000 | [diff] [blame] | 772 | |
Chris Lattner | fef9d2b | 2006-08-04 06:36:52 +0000 | [diff] [blame] | 773 | // Check the type specifier components first. |
| 774 | |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 775 | // Validate and finalize AltiVec vector declspec. |
| 776 | if (TypeAltiVecVector) { |
| 777 | if (TypeAltiVecBool) { |
| 778 | // Sign specifiers are not allowed with vector bool. (PIM 2.1) |
| 779 | if (TypeSpecSign != TSS_unspecified) { |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 780 | Diag(D, TSSLoc, diag::err_invalid_vector_bool_decl_spec) |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 781 | << getSpecifierName((TSS)TypeSpecSign); |
| 782 | } |
| 783 | |
| 784 | // Only char/int are valid with vector bool. (PIM 2.1) |
Duncan Sands | d3e231e | 2010-06-23 19:34:52 +0000 | [diff] [blame] | 785 | if (((TypeSpecType != TST_unspecified) && (TypeSpecType != TST_char) && |
| 786 | (TypeSpecType != TST_int)) || TypeAltiVecPixel) { |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 787 | Diag(D, TSTLoc, diag::err_invalid_vector_bool_decl_spec) |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 788 | << (TypeAltiVecPixel ? "__pixel" : |
| 789 | getSpecifierName((TST)TypeSpecType)); |
| 790 | } |
| 791 | |
| 792 | // Only 'short' is valid with vector bool. (PIM 2.1) |
| 793 | if ((TypeSpecWidth != TSW_unspecified) && (TypeSpecWidth != TSW_short)) |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 794 | Diag(D, TSWLoc, diag::err_invalid_vector_bool_decl_spec) |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 795 | << getSpecifierName((TSW)TypeSpecWidth); |
| 796 | |
| 797 | // Elements of vector bool are interpreted as unsigned. (PIM 2.1) |
| 798 | if ((TypeSpecType == TST_char) || (TypeSpecType == TST_int) || |
| 799 | (TypeSpecWidth != TSW_unspecified)) |
| 800 | TypeSpecSign = TSS_unsigned; |
| 801 | } |
| 802 | |
| 803 | if (TypeAltiVecPixel) { |
| 804 | //TODO: perform validation |
| 805 | TypeSpecType = TST_int; |
| 806 | TypeSpecSign = TSS_unsigned; |
| 807 | TypeSpecWidth = TSW_short; |
John McCall | a3707cc | 2010-08-26 17:22:34 +0000 | [diff] [blame] | 808 | TypeSpecOwned = false; |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 809 | } |
| 810 | } |
| 811 | |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 812 | // signed/unsigned are only valid with int/char/wchar_t. |
Chris Lattner | 839713c | 2006-08-04 06:15:52 +0000 | [diff] [blame] | 813 | if (TypeSpecSign != TSS_unspecified) { |
Chris Lattner | fef9d2b | 2006-08-04 06:36:52 +0000 | [diff] [blame] | 814 | if (TypeSpecType == TST_unspecified) |
| 815 | TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int. |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 816 | else if (TypeSpecType != TST_int && |
| 817 | TypeSpecType != TST_char && TypeSpecType != TST_wchar) { |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 818 | Diag(D, TSSLoc, diag::err_invalid_sign_spec) |
Chris Lattner | 3b0f3ef | 2008-11-22 08:32:36 +0000 | [diff] [blame] | 819 | << getSpecifierName((TST)TypeSpecType); |
Chris Lattner | fef9d2b | 2006-08-04 06:36:52 +0000 | [diff] [blame] | 820 | // signed double -> double. |
Chris Lattner | 839713c | 2006-08-04 06:15:52 +0000 | [diff] [blame] | 821 | TypeSpecSign = TSS_unspecified; |
| 822 | } |
| 823 | } |
Chris Lattner | fef9d2b | 2006-08-04 06:36:52 +0000 | [diff] [blame] | 824 | |
Chris Lattner | 839713c | 2006-08-04 06:15:52 +0000 | [diff] [blame] | 825 | // Validate the width of the type. |
| 826 | switch (TypeSpecWidth) { |
| 827 | case TSW_unspecified: break; |
| 828 | case TSW_short: // short int |
| 829 | case TSW_longlong: // long long int |
Chris Lattner | fef9d2b | 2006-08-04 06:36:52 +0000 | [diff] [blame] | 830 | if (TypeSpecType == TST_unspecified) |
| 831 | TypeSpecType = TST_int; // short -> short int, long long -> long long int. |
| 832 | else if (TypeSpecType != TST_int) { |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 833 | Diag(D, TSWLoc, |
Chris Lattner | 36982e4 | 2007-05-16 17:49:37 +0000 | [diff] [blame] | 834 | TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec |
Chris Lattner | 3b0f3ef | 2008-11-22 08:32:36 +0000 | [diff] [blame] | 835 | : diag::err_invalid_longlong_spec) |
| 836 | << getSpecifierName((TST)TypeSpecType); |
Chris Lattner | 839713c | 2006-08-04 06:15:52 +0000 | [diff] [blame] | 837 | TypeSpecType = TST_int; |
John McCall | a3707cc | 2010-08-26 17:22:34 +0000 | [diff] [blame] | 838 | TypeSpecOwned = false; |
Chris Lattner | 839713c | 2006-08-04 06:15:52 +0000 | [diff] [blame] | 839 | } |
| 840 | break; |
| 841 | case TSW_long: // long double, long int |
Chris Lattner | fef9d2b | 2006-08-04 06:36:52 +0000 | [diff] [blame] | 842 | if (TypeSpecType == TST_unspecified) |
| 843 | TypeSpecType = TST_int; // long -> long int. |
| 844 | else if (TypeSpecType != TST_int && TypeSpecType != TST_double) { |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 845 | Diag(D, TSWLoc, diag::err_invalid_long_spec) |
Chris Lattner | 3b0f3ef | 2008-11-22 08:32:36 +0000 | [diff] [blame] | 846 | << getSpecifierName((TST)TypeSpecType); |
Chris Lattner | 839713c | 2006-08-04 06:15:52 +0000 | [diff] [blame] | 847 | TypeSpecType = TST_int; |
John McCall | a3707cc | 2010-08-26 17:22:34 +0000 | [diff] [blame] | 848 | TypeSpecOwned = false; |
Chris Lattner | 839713c | 2006-08-04 06:15:52 +0000 | [diff] [blame] | 849 | } |
Chris Lattner | fef9d2b | 2006-08-04 06:36:52 +0000 | [diff] [blame] | 850 | break; |
Chris Lattner | 839713c | 2006-08-04 06:15:52 +0000 | [diff] [blame] | 851 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 852 | |
Chris Lattner | 0e89462 | 2006-08-13 19:58:17 +0000 | [diff] [blame] | 853 | // TODO: if the implementation does not implement _Complex or _Imaginary, |
Chris Lattner | fef9d2b | 2006-08-04 06:36:52 +0000 | [diff] [blame] | 854 | // disallow their use. Need information about the backend. |
| 855 | if (TypeSpecComplex != TSC_unspecified) { |
| 856 | if (TypeSpecType == TST_unspecified) { |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 857 | Diag(D, TSCLoc, diag::ext_plain_complex) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 858 | << FixItHint::CreateInsertion( |
Douglas Gregor | e3e01a2 | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 859 | PP.getLocForEndOfToken(getTypeSpecComplexLoc()), |
| 860 | " double"); |
Chris Lattner | fef9d2b | 2006-08-04 06:36:52 +0000 | [diff] [blame] | 861 | TypeSpecType = TST_double; // _Complex -> _Complex double. |
| 862 | } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) { |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 863 | // Note that this intentionally doesn't include _Complex _Bool. |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 864 | Diag(D, TSTLoc, diag::ext_integer_complex); |
Chris Lattner | fef9d2b | 2006-08-04 06:36:52 +0000 | [diff] [blame] | 865 | } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) { |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 866 | Diag(D, TSCLoc, diag::err_invalid_complex_spec) |
Chris Lattner | 3b0f3ef | 2008-11-22 08:32:36 +0000 | [diff] [blame] | 867 | << getSpecifierName((TST)TypeSpecType); |
Chris Lattner | fef9d2b | 2006-08-04 06:36:52 +0000 | [diff] [blame] | 868 | TypeSpecComplex = TSC_unspecified; |
| 869 | } |
| 870 | } |
Chris Lattner | 8e90ef6 | 2006-08-05 03:30:45 +0000 | [diff] [blame] | 871 | |
Richard Smith | 58c7433 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 872 | // If no type specifier was provided and we're parsing a language where |
| 873 | // the type specifier is not optional, but we got 'auto' as a storage |
| 874 | // class specifier, then assume this is an attempt to use C++0x's 'auto' |
| 875 | // type specifier. |
| 876 | // FIXME: Does Microsoft really support implicit int in C++? |
Francois Pichet | 0706d20 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 877 | if (PP.getLangOptions().CPlusPlus && !PP.getLangOptions().MicrosoftExt && |
Richard Smith | 58c7433 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 878 | TypeSpecType == TST_unspecified && StorageClassSpec == SCS_auto) { |
| 879 | TypeSpecType = TST_auto; |
| 880 | StorageClassSpec = StorageClassSpecAsWritten = SCS_unspecified; |
| 881 | TSTLoc = TSTNameLoc = StorageClassSpecLoc; |
| 882 | StorageClassSpecLoc = SourceLocation(); |
| 883 | } |
| 884 | // Diagnose if we've recovered from an ill-formed 'auto' storage class |
| 885 | // specifier in a pre-C++0x dialect of C++. |
| 886 | if (!PP.getLangOptions().CPlusPlus0x && TypeSpecType == TST_auto) |
| 887 | Diag(D, TSTLoc, diag::ext_auto_type_specifier); |
| 888 | if (PP.getLangOptions().CPlusPlus && !PP.getLangOptions().CPlusPlus0x && |
| 889 | StorageClassSpec == SCS_auto) |
| 890 | Diag(D, StorageClassSpecLoc, diag::warn_auto_storage_class) |
| 891 | << FixItHint::CreateRemoval(StorageClassSpecLoc); |
| 892 | |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 893 | // C++ [class.friend]p6: |
| 894 | // No storage-class-specifier shall appear in the decl-specifier-seq |
| 895 | // of a friend declaration. |
| 896 | if (isFriendSpecified() && getStorageClassSpec()) { |
| 897 | DeclSpec::SCS SC = getStorageClassSpec(); |
| 898 | const char *SpecName = getSpecifierName(SC); |
| 899 | |
| 900 | SourceLocation SCLoc = getStorageClassSpecLoc(); |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 901 | SourceLocation SCEndLoc = SCLoc.getLocWithOffset(strlen(SpecName)); |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 902 | |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 903 | Diag(D, SCLoc, diag::err_friend_storage_spec) |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 904 | << SpecName |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 905 | << FixItHint::CreateRemoval(SourceRange(SCLoc, SCEndLoc)); |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 906 | |
| 907 | ClearStorageClassSpecs(); |
| 908 | } |
| 909 | |
Douglas Gregor | 6422642 | 2011-09-09 21:14:29 +0000 | [diff] [blame] | 910 | assert(!TypeSpecOwned || isDeclRep((TST) TypeSpecType)); |
| 911 | |
Chris Lattner | 8e90ef6 | 2006-08-05 03:30:45 +0000 | [diff] [blame] | 912 | // Okay, now we can infer the real type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 913 | |
Chris Lattner | 0e89462 | 2006-08-13 19:58:17 +0000 | [diff] [blame] | 914 | // TODO: return "auto function" and other bad things based on the real type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 915 | |
Chris Lattner | 1ae2329 | 2006-08-04 06:42:31 +0000 | [diff] [blame] | 916 | // 'data definition has no type or storage class'? |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 917 | } |
Daniel Dunbar | c74b5cc | 2008-08-11 03:45:03 +0000 | [diff] [blame] | 918 | |
Sebastian Redl | a2b5e31 | 2008-12-28 15:28:59 +0000 | [diff] [blame] | 919 | bool DeclSpec::isMissingDeclaratorOk() { |
| 920 | TST tst = getTypeSpecType(); |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 921 | return isDeclRep(tst) && getRepAsDecl() != 0 && |
| 922 | StorageClassSpec != DeclSpec::SCS_typedef; |
Sebastian Redl | a2b5e31 | 2008-12-28 15:28:59 +0000 | [diff] [blame] | 923 | } |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 924 | |
| 925 | void UnqualifiedId::clear() { |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 926 | Kind = IK_Identifier; |
| 927 | Identifier = 0; |
| 928 | StartLocation = SourceLocation(); |
| 929 | EndLocation = SourceLocation(); |
| 930 | } |
| 931 | |
| 932 | void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc, |
| 933 | OverloadedOperatorKind Op, |
| 934 | SourceLocation SymbolLocations[3]) { |
| 935 | Kind = IK_OperatorFunctionId; |
| 936 | StartLocation = OperatorLoc; |
| 937 | EndLocation = OperatorLoc; |
| 938 | OperatorFunctionId.Operator = Op; |
| 939 | for (unsigned I = 0; I != 3; ++I) { |
| 940 | OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I].getRawEncoding(); |
| 941 | |
| 942 | if (SymbolLocations[I].isValid()) |
| 943 | EndLocation = SymbolLocations[I]; |
| 944 | } |
| 945 | } |
Anders Carlsson | 5610490 | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 946 | |
Anders Carlsson | 4b63d0e | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 947 | bool VirtSpecifiers::SetSpecifier(Specifier VS, SourceLocation Loc, |
Anders Carlsson | f2ca389 | 2011-01-22 15:58:16 +0000 | [diff] [blame] | 948 | const char *&PrevSpec) { |
Douglas Gregor | f2f0806 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 949 | LastLocation = Loc; |
| 950 | |
Anders Carlsson | 5610490 | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 951 | if (Specifiers & VS) { |
| 952 | PrevSpec = getSpecifierName(VS); |
| 953 | return true; |
| 954 | } |
| 955 | |
| 956 | Specifiers |= VS; |
| 957 | |
| 958 | switch (VS) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 959 | default: llvm_unreachable("Unknown specifier!"); |
Anders Carlsson | 5610490 | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 960 | case VS_Override: VS_overrideLoc = Loc; break; |
| 961 | case VS_Final: VS_finalLoc = Loc; break; |
Anders Carlsson | 5610490 | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 962 | } |
Anders Carlsson | f2ca389 | 2011-01-22 15:58:16 +0000 | [diff] [blame] | 963 | |
Anders Carlsson | 5610490 | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 964 | return false; |
| 965 | } |
| 966 | |
Anders Carlsson | 4b63d0e | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 967 | const char *VirtSpecifiers::getSpecifierName(Specifier VS) { |
Anders Carlsson | a6d3501 | 2011-01-22 15:11:37 +0000 | [diff] [blame] | 968 | switch (VS) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 969 | default: llvm_unreachable("Unknown specifier"); |
Anders Carlsson | a6d3501 | 2011-01-22 15:11:37 +0000 | [diff] [blame] | 970 | case VS_Override: return "override"; |
| 971 | case VS_Final: return "final"; |
Anders Carlsson | a6d3501 | 2011-01-22 15:11:37 +0000 | [diff] [blame] | 972 | } |
| 973 | } |