Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- SemaDeclSpec.cpp - Declaration Specifier Semantic Analysis -------===// |
| 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 semantic analysis for declaration specifiers. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 1951085 | 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 | 555f57e | 2011-06-25 00:56:27 +0000 | [diff] [blame] | 16 | #include "clang/Sema/LocInfoType.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 17 | #include "clang/Sema/ParsedTemplate.h" |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 18 | #include "clang/Sema/Sema.h" |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 555f57e | 2011-06-25 00:56:27 +0000 | [diff] [blame] | 20 | #include "clang/AST/Expr.h" |
Douglas Gregor | 2e4c34a | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 21 | #include "clang/AST/NestedNameSpecifier.h" |
| 22 | #include "clang/AST/TypeLoc.h" |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 23 | #include "clang/Lex/Preprocessor.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 24 | #include "clang/Basic/LangOptions.h" |
Chris Lattner | 5af2f35 | 2009-01-20 19:11:22 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/STLExtras.h" |
John McCall | 32d335e | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ErrorHandling.h" |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 27 | #include <cstring> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 28 | using namespace clang; |
| 29 | |
Chris Lattner | 254be6a | 2008-11-22 08:32:36 +0000 | [diff] [blame] | 30 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 31 | static DiagnosticBuilder Diag(DiagnosticsEngine &D, SourceLocation Loc, |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 32 | unsigned DiagID) { |
| 33 | return D.Report(Loc, DiagID); |
Chris Lattner | 254be6a | 2008-11-22 08:32:36 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Douglas Gregor | 314b97f | 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 | 0efc2c1 | 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 | 2e4c34a | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 53 | void CXXScopeSpec::Extend(ASTContext &Context, SourceLocation TemplateKWLoc, |
| 54 | TypeLoc TL, SourceLocation ColonColonLoc) { |
Douglas Gregor | 5f791bb | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 55 | Builder.Extend(Context, TemplateKWLoc, TL, ColonColonLoc); |
Douglas Gregor | 2e4c34a | 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 | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 59 | |
Douglas Gregor | 5f791bb | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 60 | assert(Range == Builder.getSourceRange() && |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 61 | "NestedNameSpecifierLoc range computation incorrect"); |
Douglas Gregor | 2e4c34a | 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 | 5f791bb | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 67 | Builder.Extend(Context, Identifier, IdentifierLoc, ColonColonLoc); |
| 68 | |
Douglas Gregor | 2e4c34a | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 69 | if (Range.getBegin().isInvalid()) |
| 70 | Range.setBegin(IdentifierLoc); |
| 71 | Range.setEnd(ColonColonLoc); |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 72 | |
Douglas Gregor | 5f791bb | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 73 | assert(Range == Builder.getSourceRange() && |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 74 | "NestedNameSpecifierLoc range computation incorrect"); |
Douglas Gregor | 2e4c34a | 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 | 5f791bb | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 80 | Builder.Extend(Context, Namespace, NamespaceLoc, ColonColonLoc); |
| 81 | |
Douglas Gregor | 2e4c34a | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 82 | if (Range.getBegin().isInvalid()) |
| 83 | Range.setBegin(NamespaceLoc); |
| 84 | Range.setEnd(ColonColonLoc); |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 85 | |
Douglas Gregor | 5f791bb | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 86 | assert(Range == Builder.getSourceRange() && |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 87 | "NestedNameSpecifierLoc range computation incorrect"); |
Douglas Gregor | 2e4c34a | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Douglas Gregor | 14aba76 | 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 | 5f791bb | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 93 | Builder.Extend(Context, Alias, AliasLoc, ColonColonLoc); |
| 94 | |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 95 | if (Range.getBegin().isInvalid()) |
| 96 | Range.setBegin(AliasLoc); |
| 97 | Range.setEnd(ColonColonLoc); |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 98 | |
Douglas Gregor | 5f791bb | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 99 | assert(Range == Builder.getSourceRange() && |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 100 | "NestedNameSpecifierLoc range computation incorrect"); |
Douglas Gregor | 14aba76 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Douglas Gregor | 2e4c34a | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 103 | void CXXScopeSpec::MakeGlobal(ASTContext &Context, |
| 104 | SourceLocation ColonColonLoc) { |
Douglas Gregor | 5f791bb | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 105 | Builder.MakeGlobal(Context, ColonColonLoc); |
| 106 | |
Douglas Gregor | 2e4c34a | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 107 | Range = SourceRange(ColonColonLoc); |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 108 | |
Douglas Gregor | 5f791bb | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 109 | assert(Range == Builder.getSourceRange() && |
Douglas Gregor | c34348a | 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 | 5f791bb | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 115 | Builder.MakeTrivial(Context, Qualifier, R); |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 116 | Range = R; |
Douglas Gregor | c34348a | 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 | 5f791bb | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 122 | Builder.Clear(); |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 123 | return; |
| 124 | } |
Douglas Gregor | 5f791bb | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 125 | |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 126 | Range = Other.getSourceRange(); |
Douglas Gregor | 5f791bb | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 127 | Builder.Adopt(Other); |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 128 | } |
| 129 | |
John McCall | 9dc71d2 | 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 | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 136 | NestedNameSpecifierLoc |
| 137 | CXXScopeSpec::getWithLocInContext(ASTContext &Context) const { |
Douglas Gregor | b46ae39 | 2011-03-03 21:48:55 +0000 | [diff] [blame] | 138 | if (!Builder.getRepresentation()) |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 139 | return NestedNameSpecifierLoc(); |
| 140 | |
Douglas Gregor | 5f791bb | 2011-02-28 23:58:31 +0000 | [diff] [blame] | 141 | return Builder.getWithLocInContext(Context); |
Douglas Gregor | 2e4c34a | 2011-02-24 00:17:56 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Chris Lattner | 5af2f35 | 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 | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 146 | DeclaratorChunk DeclaratorChunk::getFunction(bool hasProto, bool isVariadic, |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 147 | SourceLocation EllipsisLoc, |
Chris Lattner | 5af2f35 | 2009-01-20 19:11:22 +0000 | [diff] [blame] | 148 | ParamInfo *ArgInfo, |
| 149 | unsigned NumArgs, |
| 150 | unsigned TypeQuals, |
Douglas Gregor | 83f5172 | 2011-01-26 03:43:54 +0000 | [diff] [blame] | 151 | bool RefQualifierIsLvalueRef, |
| 152 | SourceLocation RefQualifierLoc, |
Douglas Gregor | 90ebed0 | 2011-07-13 21:47:47 +0000 | [diff] [blame] | 153 | SourceLocation MutableLoc, |
Sebastian Redl | 6e5d319 | 2011-03-05 22:42:13 +0000 | [diff] [blame] | 154 | ExceptionSpecificationType |
| 155 | ESpecType, |
| 156 | SourceLocation ESpecLoc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 157 | ParsedType *Exceptions, |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 158 | SourceRange *ExceptionRanges, |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 159 | unsigned NumExceptions, |
Sebastian Redl | 6e5d319 | 2011-03-05 22:42:13 +0000 | [diff] [blame] | 160 | Expr *NoexceptExpr, |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 161 | SourceLocation LocalRangeBegin, |
| 162 | SourceLocation LocalRangeEnd, |
Douglas Gregor | dab60ad | 2010-10-01 18:44:50 +0000 | [diff] [blame] | 163 | Declarator &TheDeclarator, |
| 164 | ParsedType TrailingReturnType) { |
Chris Lattner | 5af2f35 | 2009-01-20 19:11:22 +0000 | [diff] [blame] | 165 | DeclaratorChunk I; |
Sebastian Redl | 6e5d319 | 2011-03-05 22:42:13 +0000 | [diff] [blame] | 166 | I.Kind = Function; |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 167 | I.Loc = LocalRangeBegin; |
| 168 | I.EndLoc = LocalRangeEnd; |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 169 | I.Fun.AttrList = 0; |
Sebastian Redl | 6e5d319 | 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 | 83f5172 | 2011-01-26 03:43:54 +0000 | [diff] [blame] | 177 | I.Fun.RefQualifierIsLValueRef = RefQualifierIsLvalueRef; |
Sebastian Redl | 6e5d319 | 2011-03-05 22:42:13 +0000 | [diff] [blame] | 178 | I.Fun.RefQualifierLoc = RefQualifierLoc.getRawEncoding(); |
Douglas Gregor | 90ebed0 | 2011-07-13 21:47:47 +0000 | [diff] [blame] | 179 | I.Fun.MutableLoc = MutableLoc.getRawEncoding(); |
Sebastian Redl | 6e5d319 | 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 | dab60ad | 2010-10-01 18:44:50 +0000 | [diff] [blame] | 185 | I.Fun.TrailingReturnType = TrailingReturnType.getAsOpaquePtr(); |
Sebastian Redl | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 186 | |
Chris Lattner | 5af2f35 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 193 | if (!TheDeclarator.InlineParamsUsed && |
Chris Lattner | 5af2f35 | 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 | 6e5d319 | 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 | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 217 | } |
Sebastian Redl | 6e5d319 | 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 | 7dc8134 | 2009-04-29 17:30:04 +0000 | [diff] [blame] | 223 | } |
Chris Lattner | 5af2f35 | 2009-01-20 19:11:22 +0000 | [diff] [blame] | 224 | return I; |
| 225 | } |
Chris Lattner | 254be6a | 2008-11-22 08:32:36 +0000 | [diff] [blame] | 226 | |
Douglas Gregor | 555f57e | 2011-06-25 00:56:27 +0000 | [diff] [blame] | 227 | bool Declarator::isDeclarationOfFunction() const { |
Richard Smith | 1ab0d90 | 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 | 555f57e | 2011-06-25 00:56:27 +0000 | [diff] [blame] | 244 | |
| 245 | switch (DS.getTypeSpecType()) { |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 246 | case TST_atomic: |
Douglas Gregor | 555f57e | 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: |
| 260 | case TST_int: |
| 261 | case TST_struct: |
| 262 | case TST_union: |
| 263 | case TST_unknown_anytype: |
| 264 | case TST_unspecified: |
| 265 | case TST_void: |
| 266 | case TST_wchar: |
| 267 | return false; |
| 268 | |
| 269 | case TST_decltype: |
| 270 | case TST_typeofExpr: |
| 271 | if (Expr *E = DS.getRepAsExpr()) |
| 272 | return E->getType()->isFunctionType(); |
| 273 | return false; |
| 274 | |
| 275 | case TST_underlyingType: |
| 276 | case TST_typename: |
| 277 | case TST_typeofType: { |
| 278 | QualType QT = DS.getRepAsType().get(); |
| 279 | if (QT.isNull()) |
| 280 | return false; |
| 281 | |
| 282 | if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT)) |
| 283 | QT = LIT->getType(); |
| 284 | |
| 285 | if (QT.isNull()) |
| 286 | return false; |
| 287 | |
| 288 | return QT->isFunctionType(); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | return false; |
| 293 | } |
| 294 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 295 | /// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this |
Chris Lattner | 2a327d1 | 2009-02-27 18:35:46 +0000 | [diff] [blame] | 296 | /// declaration specifier includes. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 297 | /// |
| 298 | unsigned DeclSpec::getParsedSpecifiers() const { |
| 299 | unsigned Res = 0; |
| 300 | if (StorageClassSpec != SCS_unspecified || |
| 301 | SCS_thread_specified) |
| 302 | Res |= PQ_StorageClassSpecifier; |
Mike Stump | d420433 | 2008-06-19 19:52:46 +0000 | [diff] [blame] | 303 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 304 | if (TypeQualifiers != TQ_unspecified) |
| 305 | Res |= PQ_TypeQualifier; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 306 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 307 | if (hasTypeSpecifier()) |
| 308 | Res |= PQ_TypeSpecifier; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 309 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 310 | if (FS_inline_specified || FS_virtual_specified || FS_explicit_specified) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 311 | Res |= PQ_FunctionSpecifier; |
| 312 | return Res; |
| 313 | } |
| 314 | |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 315 | template <class T> static bool BadSpecifier(T TNew, T TPrev, |
| 316 | const char *&PrevSpec, |
| 317 | unsigned &DiagID) { |
John McCall | 32d335e | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 318 | PrevSpec = DeclSpec::getSpecifierName(TPrev); |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 319 | DiagID = (TNew == TPrev ? diag::ext_duplicate_declspec |
| 320 | : diag::err_invalid_decl_spec_combination); |
John McCall | 32d335e | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 321 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 322 | } |
John McCall | 32d335e | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 323 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 324 | const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) { |
| 325 | switch (S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 326 | case DeclSpec::SCS_unspecified: return "unspecified"; |
| 327 | case DeclSpec::SCS_typedef: return "typedef"; |
| 328 | case DeclSpec::SCS_extern: return "extern"; |
| 329 | case DeclSpec::SCS_static: return "static"; |
| 330 | case DeclSpec::SCS_auto: return "auto"; |
| 331 | case DeclSpec::SCS_register: return "register"; |
Eli Friedman | 63054b3 | 2009-04-19 20:27:55 +0000 | [diff] [blame] | 332 | case DeclSpec::SCS_private_extern: return "__private_extern__"; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 333 | case DeclSpec::SCS_mutable: return "mutable"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 334 | } |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 335 | llvm_unreachable("Unknown typespec!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 336 | } |
| 337 | |
John McCall | 32d335e | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 338 | const char *DeclSpec::getSpecifierName(TSW W) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 339 | switch (W) { |
John McCall | 32d335e | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 340 | case TSW_unspecified: return "unspecified"; |
| 341 | case TSW_short: return "short"; |
| 342 | case TSW_long: return "long"; |
| 343 | case TSW_longlong: return "long long"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 344 | } |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 345 | llvm_unreachable("Unknown typespec!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 346 | } |
| 347 | |
John McCall | 32d335e | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 348 | const char *DeclSpec::getSpecifierName(TSC C) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 349 | switch (C) { |
John McCall | 32d335e | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 350 | case TSC_unspecified: return "unspecified"; |
| 351 | case TSC_imaginary: return "imaginary"; |
| 352 | case TSC_complex: return "complex"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 353 | } |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 354 | llvm_unreachable("Unknown typespec!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | |
John McCall | 32d335e | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 358 | const char *DeclSpec::getSpecifierName(TSS S) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 359 | switch (S) { |
John McCall | 32d335e | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 360 | case TSS_unspecified: return "unspecified"; |
| 361 | case TSS_signed: return "signed"; |
| 362 | case TSS_unsigned: return "unsigned"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 363 | } |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 364 | llvm_unreachable("Unknown typespec!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | const char *DeclSpec::getSpecifierName(DeclSpec::TST T) { |
| 368 | switch (T) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 369 | case DeclSpec::TST_unspecified: return "unspecified"; |
| 370 | case DeclSpec::TST_void: return "void"; |
| 371 | case DeclSpec::TST_char: return "char"; |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 372 | case DeclSpec::TST_wchar: return "wchar_t"; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 373 | case DeclSpec::TST_char16: return "char16_t"; |
| 374 | case DeclSpec::TST_char32: return "char32_t"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 375 | case DeclSpec::TST_int: return "int"; |
| 376 | case DeclSpec::TST_float: return "float"; |
| 377 | case DeclSpec::TST_double: return "double"; |
| 378 | case DeclSpec::TST_bool: return "_Bool"; |
| 379 | case DeclSpec::TST_decimal32: return "_Decimal32"; |
| 380 | case DeclSpec::TST_decimal64: return "_Decimal64"; |
| 381 | case DeclSpec::TST_decimal128: return "_Decimal128"; |
| 382 | case DeclSpec::TST_enum: return "enum"; |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 383 | case DeclSpec::TST_class: return "class"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 384 | case DeclSpec::TST_union: return "union"; |
| 385 | case DeclSpec::TST_struct: return "struct"; |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 386 | case DeclSpec::TST_typename: return "type-name"; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 387 | case DeclSpec::TST_typeofType: |
| 388 | case DeclSpec::TST_typeofExpr: return "typeof"; |
John McCall | 32d335e | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 389 | case DeclSpec::TST_auto: return "auto"; |
| 390 | case DeclSpec::TST_decltype: return "(decltype)"; |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 391 | case DeclSpec::TST_underlyingType: return "__underlying_type"; |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 392 | case DeclSpec::TST_unknown_anytype: return "__unknown_anytype"; |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 393 | case DeclSpec::TST_atomic: return "_Atomic"; |
John McCall | 32d335e | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 394 | case DeclSpec::TST_error: return "(error)"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 395 | } |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 396 | llvm_unreachable("Unknown typespec!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 397 | } |
| 398 | |
John McCall | 32d335e | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 399 | const char *DeclSpec::getSpecifierName(TQ T) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 400 | switch (T) { |
John McCall | 32d335e | 2009-08-03 18:47:27 +0000 | [diff] [blame] | 401 | case DeclSpec::TQ_unspecified: return "unspecified"; |
| 402 | case DeclSpec::TQ_const: return "const"; |
| 403 | case DeclSpec::TQ_restrict: return "restrict"; |
| 404 | case DeclSpec::TQ_volatile: return "volatile"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 405 | } |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 406 | llvm_unreachable("Unknown typespec!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 409 | bool DeclSpec::SetStorageClassSpec(Sema &S, SCS SC, SourceLocation Loc, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 410 | const char *&PrevSpec, |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 411 | unsigned &DiagID) { |
| 412 | // OpenCL 1.1 6.8g: "The extern, static, auto and register storage-class |
| 413 | // specifiers are not supported." |
Peter Collingbourne | e2f82f7 | 2011-02-11 19:59:54 +0000 | [diff] [blame] | 414 | // It seems sensible to prohibit private_extern too |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 415 | // The cl_clang_storage_class_specifiers extension enables support for |
| 416 | // these storage-class specifiers. |
| 417 | if (S.getLangOptions().OpenCL && |
| 418 | !S.getOpenCLOptions().cl_clang_storage_class_specifiers) { |
| 419 | switch (SC) { |
Peter Collingbourne | e2f82f7 | 2011-02-11 19:59:54 +0000 | [diff] [blame] | 420 | case SCS_extern: |
| 421 | case SCS_private_extern: |
| 422 | case SCS_auto: |
| 423 | case SCS_register: |
| 424 | case SCS_static: |
| 425 | DiagID = diag::err_not_opencl_storage_class_specifier; |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 426 | PrevSpec = getSpecifierName(SC); |
Peter Collingbourne | e2f82f7 | 2011-02-11 19:59:54 +0000 | [diff] [blame] | 427 | return true; |
| 428 | default: |
| 429 | break; |
| 430 | } |
| 431 | } |
| 432 | |
Abramo Bagnara | 35f9a19 | 2010-07-30 16:47:02 +0000 | [diff] [blame] | 433 | if (StorageClassSpec != SCS_unspecified) { |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 434 | // Maybe this is an attempt to use C++0x 'auto' outside of C++0x mode. |
| 435 | bool isInvalid = true; |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 436 | if (TypeSpecType == TST_unspecified && S.getLangOptions().CPlusPlus) { |
| 437 | if (SC == SCS_auto) |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 438 | return SetTypeSpecType(TST_auto, Loc, PrevSpec, DiagID); |
| 439 | if (StorageClassSpec == SCS_auto) { |
| 440 | isInvalid = SetTypeSpecType(TST_auto, StorageClassSpecLoc, |
| 441 | PrevSpec, DiagID); |
| 442 | assert(!isInvalid && "auto SCS -> TST recovery failed"); |
| 443 | } |
| 444 | } |
| 445 | |
Abramo Bagnara | 35f9a19 | 2010-07-30 16:47:02 +0000 | [diff] [blame] | 446 | // Changing storage class is allowed only if the previous one |
| 447 | // was the 'extern' that is part of a linkage specification and |
| 448 | // the new storage class is 'typedef'. |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 449 | if (isInvalid && |
| 450 | !(SCS_extern_in_linkage_spec && |
Abramo Bagnara | 35f9a19 | 2010-07-30 16:47:02 +0000 | [diff] [blame] | 451 | StorageClassSpec == SCS_extern && |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 452 | SC == SCS_typedef)) |
| 453 | return BadSpecifier(SC, (SCS)StorageClassSpec, PrevSpec, DiagID); |
Abramo Bagnara | 35f9a19 | 2010-07-30 16:47:02 +0000 | [diff] [blame] | 454 | } |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 455 | StorageClassSpec = SC; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 456 | StorageClassSpecLoc = Loc; |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 457 | assert((unsigned)SC == StorageClassSpec && "SCS constants overflow bitfield"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 458 | return false; |
| 459 | } |
| 460 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 461 | bool DeclSpec::SetStorageClassSpecThread(SourceLocation Loc, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 462 | const char *&PrevSpec, |
| 463 | unsigned &DiagID) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 464 | if (SCS_thread_specified) { |
| 465 | PrevSpec = "__thread"; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 466 | DiagID = diag::ext_duplicate_declspec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 467 | return true; |
| 468 | } |
| 469 | SCS_thread_specified = true; |
| 470 | SCS_threadLoc = Loc; |
| 471 | return false; |
| 472 | } |
| 473 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 474 | /// These methods set the specified attribute of the DeclSpec, but return true |
| 475 | /// and ignore the request if invalid (e.g. "extern" then "auto" is |
| 476 | /// specified). |
| 477 | bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 478 | const char *&PrevSpec, |
| 479 | unsigned &DiagID) { |
Abramo Bagnara | 2553eaf | 2011-03-06 22:21:56 +0000 | [diff] [blame] | 480 | // Overwrite TSWLoc only if TypeSpecWidth was unspecified, so that |
| 481 | // for 'long long' we will keep the source location of the first 'long'. |
| 482 | if (TypeSpecWidth == TSW_unspecified) |
| 483 | TSWLoc = Loc; |
| 484 | // Allow turning long -> long long. |
| 485 | else if (W != TSW_longlong || TypeSpecWidth != TSW_long) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 486 | return BadSpecifier(W, (TSW)TypeSpecWidth, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 487 | TypeSpecWidth = W; |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 488 | if (TypeAltiVecVector && !TypeAltiVecBool && |
| 489 | ((TypeSpecWidth == TSW_long) || (TypeSpecWidth == TSW_longlong))) { |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 490 | PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType); |
| 491 | DiagID = diag::warn_vector_long_decl_spec_combination; |
| 492 | return true; |
| 493 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 494 | return false; |
| 495 | } |
| 496 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 497 | bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 498 | const char *&PrevSpec, |
| 499 | unsigned &DiagID) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 500 | if (TypeSpecComplex != TSC_unspecified) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 501 | return BadSpecifier(C, (TSC)TypeSpecComplex, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 502 | TypeSpecComplex = C; |
| 503 | TSCLoc = Loc; |
| 504 | return false; |
| 505 | } |
| 506 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 507 | bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 508 | const char *&PrevSpec, |
| 509 | unsigned &DiagID) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 510 | if (TypeSpecSign != TSS_unspecified) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 511 | return BadSpecifier(S, (TSS)TypeSpecSign, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 512 | TypeSpecSign = S; |
| 513 | TSSLoc = Loc; |
| 514 | return false; |
| 515 | } |
| 516 | |
| 517 | bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 518 | const char *&PrevSpec, |
| 519 | unsigned &DiagID, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 520 | ParsedType Rep) { |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 521 | return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep); |
| 522 | } |
| 523 | |
| 524 | bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc, |
| 525 | SourceLocation TagNameLoc, |
| 526 | const char *&PrevSpec, |
| 527 | unsigned &DiagID, |
| 528 | ParsedType Rep) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 529 | assert(isTypeRep(T) && "T does not store a type"); |
| 530 | assert(Rep && "no type provided!"); |
| 531 | if (TypeSpecType != TST_unspecified) { |
| 532 | PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType); |
| 533 | DiagID = diag::err_invalid_decl_spec_combination; |
| 534 | return true; |
| 535 | } |
| 536 | TypeSpecType = T; |
| 537 | TypeRep = Rep; |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 538 | TSTLoc = TagKwLoc; |
| 539 | TSTNameLoc = TagNameLoc; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 540 | TypeSpecOwned = false; |
| 541 | return false; |
| 542 | } |
| 543 | |
| 544 | bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc, |
| 545 | const char *&PrevSpec, |
| 546 | unsigned &DiagID, |
| 547 | Expr *Rep) { |
| 548 | assert(isExprRep(T) && "T does not store an expr"); |
| 549 | assert(Rep && "no expression provided!"); |
| 550 | if (TypeSpecType != TST_unspecified) { |
| 551 | PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType); |
| 552 | DiagID = diag::err_invalid_decl_spec_combination; |
| 553 | return true; |
| 554 | } |
| 555 | TypeSpecType = T; |
| 556 | ExprRep = Rep; |
| 557 | TSTLoc = Loc; |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 558 | TSTNameLoc = Loc; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 559 | TypeSpecOwned = false; |
| 560 | return false; |
| 561 | } |
| 562 | |
| 563 | bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc, |
| 564 | const char *&PrevSpec, |
| 565 | unsigned &DiagID, |
| 566 | Decl *Rep, bool Owned) { |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 567 | return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep, Owned); |
| 568 | } |
| 569 | |
| 570 | bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc, |
| 571 | SourceLocation TagNameLoc, |
| 572 | const char *&PrevSpec, |
| 573 | unsigned &DiagID, |
| 574 | Decl *Rep, bool Owned) { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 575 | assert(isDeclRep(T) && "T does not store a decl"); |
| 576 | // Unlike the other cases, we don't assert that we actually get a decl. |
| 577 | |
| 578 | if (TypeSpecType != TST_unspecified) { |
| 579 | PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType); |
| 580 | DiagID = diag::err_invalid_decl_spec_combination; |
| 581 | return true; |
| 582 | } |
| 583 | TypeSpecType = T; |
| 584 | DeclRep = Rep; |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 585 | TSTLoc = TagKwLoc; |
| 586 | TSTNameLoc = TagNameLoc; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 587 | TypeSpecOwned = Owned; |
| 588 | return false; |
| 589 | } |
| 590 | |
| 591 | bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc, |
| 592 | const char *&PrevSpec, |
| 593 | unsigned &DiagID) { |
| 594 | assert(!isDeclRep(T) && !isTypeRep(T) && !isExprRep(T) && |
| 595 | "rep required for these type-spec kinds!"); |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 596 | if (TypeSpecType != TST_unspecified) { |
| 597 | PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType); |
| 598 | DiagID = diag::err_invalid_decl_spec_combination; |
| 599 | return true; |
| 600 | } |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 601 | TSTLoc = Loc; |
| 602 | TSTNameLoc = Loc; |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 603 | if (TypeAltiVecVector && (T == TST_bool) && !TypeAltiVecBool) { |
| 604 | TypeAltiVecBool = true; |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 605 | return false; |
| 606 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 607 | TypeSpecType = T; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 608 | TypeSpecOwned = false; |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 609 | if (TypeAltiVecVector && !TypeAltiVecBool && (TypeSpecType == TST_double)) { |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 610 | PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType); |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 611 | DiagID = diag::err_invalid_vector_decl_spec; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 612 | return true; |
| 613 | } |
| 614 | return false; |
| 615 | } |
| 616 | |
| 617 | bool DeclSpec::SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc, |
| 618 | const char *&PrevSpec, unsigned &DiagID) { |
| 619 | if (TypeSpecType != TST_unspecified) { |
| 620 | PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType); |
| 621 | DiagID = diag::err_invalid_vector_decl_spec_combination; |
| 622 | return true; |
| 623 | } |
| 624 | TypeAltiVecVector = isAltiVecVector; |
| 625 | AltiVecLoc = Loc; |
| 626 | return false; |
| 627 | } |
| 628 | |
| 629 | bool DeclSpec::SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc, |
| 630 | const char *&PrevSpec, unsigned &DiagID) { |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 631 | if (!TypeAltiVecVector || TypeAltiVecPixel || |
| 632 | (TypeSpecType != TST_unspecified)) { |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 633 | PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType); |
| 634 | DiagID = diag::err_invalid_pixel_decl_spec_combination; |
| 635 | return true; |
| 636 | } |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 637 | TypeAltiVecPixel = isAltiVecPixel; |
| 638 | TSTLoc = Loc; |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 639 | TSTNameLoc = Loc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 640 | return false; |
| 641 | } |
| 642 | |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 643 | bool DeclSpec::SetTypeSpecError() { |
| 644 | TypeSpecType = TST_error; |
John McCall | 9e46b8c | 2010-08-26 17:22:34 +0000 | [diff] [blame] | 645 | TypeSpecOwned = false; |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 646 | TSTLoc = SourceLocation(); |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 647 | TSTNameLoc = SourceLocation(); |
Douglas Gregor | ddc29e1 | 2009-02-06 22:42:48 +0000 | [diff] [blame] | 648 | return false; |
| 649 | } |
| 650 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 651 | bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 652 | unsigned &DiagID, const LangOptions &Lang) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 653 | // Duplicates turn into warnings pre-C99. |
| 654 | if ((TypeQualifiers & T) && !Lang.C99) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 655 | return BadSpecifier(T, T, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 656 | TypeQualifiers |= T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 657 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 658 | switch (T) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 659 | default: llvm_unreachable("Unknown type qualifier!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 660 | case TQ_const: TQ_constLoc = Loc; break; |
| 661 | case TQ_restrict: TQ_restrictLoc = Loc; break; |
| 662 | case TQ_volatile: TQ_volatileLoc = Loc; break; |
| 663 | } |
| 664 | return false; |
| 665 | } |
| 666 | |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 667 | bool DeclSpec::SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec, |
| 668 | unsigned &DiagID) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 669 | // 'inline inline' is ok. |
| 670 | FS_inline_specified = true; |
| 671 | FS_inlineLoc = Loc; |
| 672 | return false; |
| 673 | } |
| 674 | |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 675 | bool DeclSpec::SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec, |
| 676 | unsigned &DiagID) { |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 677 | // 'virtual virtual' is ok. |
| 678 | FS_virtual_specified = true; |
| 679 | FS_virtualLoc = Loc; |
| 680 | return false; |
| 681 | } |
| 682 | |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 683 | bool DeclSpec::SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec, |
| 684 | unsigned &DiagID) { |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 685 | // 'explicit explicit' is ok. |
| 686 | FS_explicit_specified = true; |
| 687 | FS_explicitLoc = Loc; |
| 688 | return false; |
| 689 | } |
| 690 | |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 691 | bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec, |
| 692 | unsigned &DiagID) { |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 693 | if (Friend_specified) { |
| 694 | PrevSpec = "friend"; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 695 | DiagID = diag::ext_duplicate_declspec; |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 696 | return true; |
| 697 | } |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 698 | |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 699 | Friend_specified = true; |
| 700 | FriendLoc = Loc; |
| 701 | return false; |
| 702 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 703 | |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 704 | bool DeclSpec::setModulePrivateSpec(SourceLocation Loc, const char *&PrevSpec, |
| 705 | unsigned &DiagID) { |
| 706 | if (isModulePrivateSpecified()) { |
| 707 | PrevSpec = "__module_private__"; |
| 708 | DiagID = diag::ext_duplicate_declspec; |
| 709 | return true; |
| 710 | } |
| 711 | |
| 712 | ModulePrivateLoc = Loc; |
| 713 | return false; |
| 714 | } |
| 715 | |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 716 | bool DeclSpec::SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec, |
| 717 | unsigned &DiagID) { |
| 718 | // 'constexpr constexpr' is ok. |
| 719 | Constexpr_specified = true; |
| 720 | ConstexprLoc = Loc; |
| 721 | return false; |
| 722 | } |
| 723 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 724 | void DeclSpec::setProtocolQualifiers(Decl * const *Protos, |
Argyrios Kyrtzidis | e3a535b | 2009-09-29 19:42:11 +0000 | [diff] [blame] | 725 | unsigned NP, |
| 726 | SourceLocation *ProtoLocs, |
| 727 | SourceLocation LAngleLoc) { |
| 728 | if (NP == 0) return; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 729 | ProtocolQualifiers = new Decl*[NP]; |
Argyrios Kyrtzidis | e3a535b | 2009-09-29 19:42:11 +0000 | [diff] [blame] | 730 | ProtocolLocs = new SourceLocation[NP]; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 731 | memcpy((void*)ProtocolQualifiers, Protos, sizeof(Decl*)*NP); |
Argyrios Kyrtzidis | e3a535b | 2009-09-29 19:42:11 +0000 | [diff] [blame] | 732 | memcpy(ProtocolLocs, ProtoLocs, sizeof(SourceLocation)*NP); |
| 733 | NumProtocolQualifiers = NP; |
| 734 | ProtocolLAngleLoc = LAngleLoc; |
| 735 | } |
| 736 | |
Douglas Gregor | ddf889a | 2010-01-18 18:04:31 +0000 | [diff] [blame] | 737 | void DeclSpec::SaveWrittenBuiltinSpecs() { |
| 738 | writtenBS.Sign = getTypeSpecSign(); |
| 739 | writtenBS.Width = getTypeSpecWidth(); |
| 740 | writtenBS.Type = getTypeSpecType(); |
| 741 | // Search the list of attributes for the presence of a mode attribute. |
| 742 | writtenBS.ModeAttr = false; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 743 | AttributeList* attrs = getAttributes().getList(); |
Douglas Gregor | ddf889a | 2010-01-18 18:04:31 +0000 | [diff] [blame] | 744 | while (attrs) { |
| 745 | if (attrs->getKind() == AttributeList::AT_mode) { |
| 746 | writtenBS.ModeAttr = true; |
| 747 | break; |
| 748 | } |
| 749 | attrs = attrs->getNext(); |
| 750 | } |
| 751 | } |
| 752 | |
Abramo Bagnara | 35f9a19 | 2010-07-30 16:47:02 +0000 | [diff] [blame] | 753 | void DeclSpec::SaveStorageSpecifierAsWritten() { |
| 754 | if (SCS_extern_in_linkage_spec && StorageClassSpec == SCS_extern) |
| 755 | // If 'extern' is part of a linkage specification, |
| 756 | // then it is not a storage class "as written". |
| 757 | StorageClassSpecAsWritten = SCS_unspecified; |
| 758 | else |
| 759 | StorageClassSpecAsWritten = StorageClassSpec; |
| 760 | } |
| 761 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 762 | /// Finish - This does final analysis of the declspec, rejecting things like |
| 763 | /// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or |
| 764 | /// diag::NUM_DIAGNOSTICS if there is no error. After calling this method, |
| 765 | /// DeclSpec is guaranteed self-consistent, even if an error occurred. |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 766 | void DeclSpec::Finish(DiagnosticsEngine &D, Preprocessor &PP) { |
Douglas Gregor | ddf889a | 2010-01-18 18:04:31 +0000 | [diff] [blame] | 767 | // Before possibly changing their values, save specs as written. |
| 768 | SaveWrittenBuiltinSpecs(); |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 769 | SaveStorageSpecifierAsWritten(); |
Douglas Gregor | ddf889a | 2010-01-18 18:04:31 +0000 | [diff] [blame] | 770 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 771 | // Check the type specifier components first. |
| 772 | |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 773 | // Validate and finalize AltiVec vector declspec. |
| 774 | if (TypeAltiVecVector) { |
| 775 | if (TypeAltiVecBool) { |
| 776 | // Sign specifiers are not allowed with vector bool. (PIM 2.1) |
| 777 | if (TypeSpecSign != TSS_unspecified) { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 778 | Diag(D, TSSLoc, diag::err_invalid_vector_bool_decl_spec) |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 779 | << getSpecifierName((TSS)TypeSpecSign); |
| 780 | } |
| 781 | |
| 782 | // Only char/int are valid with vector bool. (PIM 2.1) |
Duncan Sands | 2e964a92 | 2010-06-23 19:34:52 +0000 | [diff] [blame] | 783 | if (((TypeSpecType != TST_unspecified) && (TypeSpecType != TST_char) && |
| 784 | (TypeSpecType != TST_int)) || TypeAltiVecPixel) { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 785 | Diag(D, TSTLoc, diag::err_invalid_vector_bool_decl_spec) |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 786 | << (TypeAltiVecPixel ? "__pixel" : |
| 787 | getSpecifierName((TST)TypeSpecType)); |
| 788 | } |
| 789 | |
| 790 | // Only 'short' is valid with vector bool. (PIM 2.1) |
| 791 | if ((TypeSpecWidth != TSW_unspecified) && (TypeSpecWidth != TSW_short)) |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 792 | Diag(D, TSWLoc, diag::err_invalid_vector_bool_decl_spec) |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 793 | << getSpecifierName((TSW)TypeSpecWidth); |
| 794 | |
| 795 | // Elements of vector bool are interpreted as unsigned. (PIM 2.1) |
| 796 | if ((TypeSpecType == TST_char) || (TypeSpecType == TST_int) || |
| 797 | (TypeSpecWidth != TSW_unspecified)) |
| 798 | TypeSpecSign = TSS_unsigned; |
| 799 | } |
| 800 | |
| 801 | if (TypeAltiVecPixel) { |
| 802 | //TODO: perform validation |
| 803 | TypeSpecType = TST_int; |
| 804 | TypeSpecSign = TSS_unsigned; |
| 805 | TypeSpecWidth = TSW_short; |
John McCall | 9e46b8c | 2010-08-26 17:22:34 +0000 | [diff] [blame] | 806 | TypeSpecOwned = false; |
Chris Lattner | 788b0fd | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 807 | } |
| 808 | } |
| 809 | |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 810 | // signed/unsigned are only valid with int/char/wchar_t. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 811 | if (TypeSpecSign != TSS_unspecified) { |
| 812 | if (TypeSpecType == TST_unspecified) |
| 813 | TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int. |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 814 | else if (TypeSpecType != TST_int && |
| 815 | TypeSpecType != TST_char && TypeSpecType != TST_wchar) { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 816 | Diag(D, TSSLoc, diag::err_invalid_sign_spec) |
Chris Lattner | 254be6a | 2008-11-22 08:32:36 +0000 | [diff] [blame] | 817 | << getSpecifierName((TST)TypeSpecType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 818 | // signed double -> double. |
| 819 | TypeSpecSign = TSS_unspecified; |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | // Validate the width of the type. |
| 824 | switch (TypeSpecWidth) { |
| 825 | case TSW_unspecified: break; |
| 826 | case TSW_short: // short int |
| 827 | case TSW_longlong: // long long int |
| 828 | if (TypeSpecType == TST_unspecified) |
| 829 | TypeSpecType = TST_int; // short -> short int, long long -> long long int. |
| 830 | else if (TypeSpecType != TST_int) { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 831 | Diag(D, TSWLoc, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 832 | TypeSpecWidth == TSW_short ? diag::err_invalid_short_spec |
Chris Lattner | 254be6a | 2008-11-22 08:32:36 +0000 | [diff] [blame] | 833 | : diag::err_invalid_longlong_spec) |
| 834 | << getSpecifierName((TST)TypeSpecType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 835 | TypeSpecType = TST_int; |
John McCall | 9e46b8c | 2010-08-26 17:22:34 +0000 | [diff] [blame] | 836 | TypeSpecOwned = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 837 | } |
| 838 | break; |
| 839 | case TSW_long: // long double, long int |
| 840 | if (TypeSpecType == TST_unspecified) |
| 841 | TypeSpecType = TST_int; // long -> long int. |
| 842 | else if (TypeSpecType != TST_int && TypeSpecType != TST_double) { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 843 | Diag(D, TSWLoc, diag::err_invalid_long_spec) |
Chris Lattner | 254be6a | 2008-11-22 08:32:36 +0000 | [diff] [blame] | 844 | << getSpecifierName((TST)TypeSpecType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 845 | TypeSpecType = TST_int; |
John McCall | 9e46b8c | 2010-08-26 17:22:34 +0000 | [diff] [blame] | 846 | TypeSpecOwned = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 847 | } |
| 848 | break; |
| 849 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 850 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 851 | // TODO: if the implementation does not implement _Complex or _Imaginary, |
| 852 | // disallow their use. Need information about the backend. |
| 853 | if (TypeSpecComplex != TSC_unspecified) { |
| 854 | if (TypeSpecType == TST_unspecified) { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 855 | Diag(D, TSCLoc, diag::ext_plain_complex) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 856 | << FixItHint::CreateInsertion( |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 857 | PP.getLocForEndOfToken(getTypeSpecComplexLoc()), |
| 858 | " double"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 859 | TypeSpecType = TST_double; // _Complex -> _Complex double. |
| 860 | } else if (TypeSpecType == TST_int || TypeSpecType == TST_char) { |
| 861 | // Note that this intentionally doesn't include _Complex _Bool. |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 862 | Diag(D, TSTLoc, diag::ext_integer_complex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 863 | } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 864 | Diag(D, TSCLoc, diag::err_invalid_complex_spec) |
Chris Lattner | 254be6a | 2008-11-22 08:32:36 +0000 | [diff] [blame] | 865 | << getSpecifierName((TST)TypeSpecType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 866 | TypeSpecComplex = TSC_unspecified; |
| 867 | } |
| 868 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 869 | |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 870 | // If no type specifier was provided and we're parsing a language where |
| 871 | // the type specifier is not optional, but we got 'auto' as a storage |
| 872 | // class specifier, then assume this is an attempt to use C++0x's 'auto' |
| 873 | // type specifier. |
| 874 | // FIXME: Does Microsoft really support implicit int in C++? |
Francois Pichet | 62ec1f2 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 875 | if (PP.getLangOptions().CPlusPlus && !PP.getLangOptions().MicrosoftExt && |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 876 | TypeSpecType == TST_unspecified && StorageClassSpec == SCS_auto) { |
| 877 | TypeSpecType = TST_auto; |
| 878 | StorageClassSpec = StorageClassSpecAsWritten = SCS_unspecified; |
| 879 | TSTLoc = TSTNameLoc = StorageClassSpecLoc; |
| 880 | StorageClassSpecLoc = SourceLocation(); |
| 881 | } |
| 882 | // Diagnose if we've recovered from an ill-formed 'auto' storage class |
| 883 | // specifier in a pre-C++0x dialect of C++. |
| 884 | if (!PP.getLangOptions().CPlusPlus0x && TypeSpecType == TST_auto) |
| 885 | Diag(D, TSTLoc, diag::ext_auto_type_specifier); |
| 886 | if (PP.getLangOptions().CPlusPlus && !PP.getLangOptions().CPlusPlus0x && |
| 887 | StorageClassSpec == SCS_auto) |
| 888 | Diag(D, StorageClassSpecLoc, diag::warn_auto_storage_class) |
| 889 | << FixItHint::CreateRemoval(StorageClassSpecLoc); |
| 890 | |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 891 | // C++ [class.friend]p6: |
| 892 | // No storage-class-specifier shall appear in the decl-specifier-seq |
| 893 | // of a friend declaration. |
| 894 | if (isFriendSpecified() && getStorageClassSpec()) { |
| 895 | DeclSpec::SCS SC = getStorageClassSpec(); |
| 896 | const char *SpecName = getSpecifierName(SC); |
| 897 | |
| 898 | SourceLocation SCLoc = getStorageClassSpecLoc(); |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 899 | SourceLocation SCEndLoc = SCLoc.getLocWithOffset(strlen(SpecName)); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 900 | |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 901 | Diag(D, SCLoc, diag::err_friend_storage_spec) |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 902 | << SpecName |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 903 | << FixItHint::CreateRemoval(SourceRange(SCLoc, SCEndLoc)); |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 904 | |
| 905 | ClearStorageClassSpecs(); |
| 906 | } |
| 907 | |
Douglas Gregor | 6274d30 | 2011-09-09 21:14:29 +0000 | [diff] [blame] | 908 | assert(!TypeSpecOwned || isDeclRep((TST) TypeSpecType)); |
| 909 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 910 | // Okay, now we can infer the real type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 911 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 912 | // TODO: return "auto function" and other bad things based on the real type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 913 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 914 | // 'data definition has no type or storage class'? |
| 915 | } |
Daniel Dunbar | e4858a6 | 2008-08-11 03:45:03 +0000 | [diff] [blame] | 916 | |
Sebastian Redl | a4ed0d8 | 2008-12-28 15:28:59 +0000 | [diff] [blame] | 917 | bool DeclSpec::isMissingDeclaratorOk() { |
| 918 | TST tst = getTypeSpecType(); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 919 | return isDeclRep(tst) && getRepAsDecl() != 0 && |
| 920 | StorageClassSpec != DeclSpec::SCS_typedef; |
Sebastian Redl | a4ed0d8 | 2008-12-28 15:28:59 +0000 | [diff] [blame] | 921 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 922 | |
| 923 | void UnqualifiedId::clear() { |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 924 | Kind = IK_Identifier; |
| 925 | Identifier = 0; |
| 926 | StartLocation = SourceLocation(); |
| 927 | EndLocation = SourceLocation(); |
| 928 | } |
| 929 | |
| 930 | void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc, |
| 931 | OverloadedOperatorKind Op, |
| 932 | SourceLocation SymbolLocations[3]) { |
| 933 | Kind = IK_OperatorFunctionId; |
| 934 | StartLocation = OperatorLoc; |
| 935 | EndLocation = OperatorLoc; |
| 936 | OperatorFunctionId.Operator = Op; |
| 937 | for (unsigned I = 0; I != 3; ++I) { |
| 938 | OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I].getRawEncoding(); |
| 939 | |
| 940 | if (SymbolLocations[I].isValid()) |
| 941 | EndLocation = SymbolLocations[I]; |
| 942 | } |
| 943 | } |
Anders Carlsson | b971dbd | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 944 | |
Anders Carlsson | cc54d59 | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 945 | bool VirtSpecifiers::SetSpecifier(Specifier VS, SourceLocation Loc, |
Anders Carlsson | 46127a9 | 2011-01-22 15:58:16 +0000 | [diff] [blame] | 946 | const char *&PrevSpec) { |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 947 | LastLocation = Loc; |
| 948 | |
Anders Carlsson | b971dbd | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 949 | if (Specifiers & VS) { |
| 950 | PrevSpec = getSpecifierName(VS); |
| 951 | return true; |
| 952 | } |
| 953 | |
| 954 | Specifiers |= VS; |
| 955 | |
| 956 | switch (VS) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 957 | default: llvm_unreachable("Unknown specifier!"); |
Anders Carlsson | b971dbd | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 958 | case VS_Override: VS_overrideLoc = Loc; break; |
| 959 | case VS_Final: VS_finalLoc = Loc; break; |
Anders Carlsson | b971dbd | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 960 | } |
Anders Carlsson | 46127a9 | 2011-01-22 15:58:16 +0000 | [diff] [blame] | 961 | |
Anders Carlsson | b971dbd | 2011-01-17 03:05:47 +0000 | [diff] [blame] | 962 | return false; |
| 963 | } |
| 964 | |
Anders Carlsson | cc54d59 | 2011-01-22 16:56:46 +0000 | [diff] [blame] | 965 | const char *VirtSpecifiers::getSpecifierName(Specifier VS) { |
Anders Carlsson | c46bb7d | 2011-01-22 15:11:37 +0000 | [diff] [blame] | 966 | switch (VS) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 967 | default: llvm_unreachable("Unknown specifier"); |
Anders Carlsson | c46bb7d | 2011-01-22 15:11:37 +0000 | [diff] [blame] | 968 | case VS_Override: return "override"; |
| 969 | case VS_Final: return "final"; |
Anders Carlsson | c46bb7d | 2011-01-22 15:11:37 +0000 | [diff] [blame] | 970 | } |
| 971 | } |