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