blob: 276e35a3497e6279b579d0cd476d656cf8edec2d [file] [log] [blame]
Eugene Zelenko1e95bc02018-04-05 22:15:42 +00001//===--- DeclSpec.cpp - Declaration Specifier Semantic Analysis -----------===//
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattnerb9093cd2006-08-04 04:39:53 +00006//
7//===----------------------------------------------------------------------===//
8//
Chris Lattner289ab7b2006-11-08 06:54:53 +00009// This file implements semantic analysis for declaration specifiers.
Chris Lattnerb9093cd2006-08-04 04:39:53 +000010//
11//===----------------------------------------------------------------------===//
12
John McCall8b0666c2010-08-20 18:27:03 +000013#include "clang/Sema/DeclSpec.h"
Douglas Gregor869ad452011-02-24 17:54:50 +000014#include "clang/AST/ASTContext.h"
Reid Kleckner9a7f3e62013-10-08 00:58:57 +000015#include "clang/AST/DeclCXX.h"
Douglas Gregorc15b0cf2011-06-25 00:56:27 +000016#include "clang/AST/Expr.h"
Benjamin Kramer31b382e2016-02-01 17:42:01 +000017#include "clang/AST/LocInfoType.h"
Douglas Gregor90c99722011-02-24 00:17:56 +000018#include "clang/AST/TypeLoc.h"
Chris Lattnerda48a8e2006-08-04 05:25:55 +000019#include "clang/Basic/LangOptions.h"
Reid Kleckner86565c12020-02-27 11:01:58 -080020#include "clang/Basic/SourceManager.h"
Bill Schmidt691e01d2014-10-31 19:19:24 +000021#include "clang/Basic/TargetInfo.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "clang/Sema/ParsedTemplate.h"
23#include "clang/Sema/Sema.h"
24#include "clang/Sema/SemaDiagnostic.h"
Chris Lattner1ce41ed2009-01-20 19:11:22 +000025#include "llvm/ADT/STLExtras.h"
Richard Smithb4a9e862013-04-12 22:46:28 +000026#include "llvm/ADT/SmallString.h"
Douglas Gregor52537682009-03-19 00:18:19 +000027#include <cstring>
Eugene Zelenkobc9d4f42018-04-05 21:09:03 +000028using namespace clang;
Chris Lattner3b0f3ef2008-11-22 08:32:36 +000029
Eugene Zelenko1e95bc02018-04-05 22:15:42 +000030
Douglas Gregorb53edfb2009-11-10 19:49:08 +000031void UnqualifiedId::setTemplateId(TemplateIdAnnotation *TemplateId) {
32 assert(TemplateId && "NULL template-id annotation?");
Richard Smithb3f6e3d2020-03-27 17:08:26 -070033 assert(!TemplateId->isInvalid() &&
34 "should not convert invalid template-ids to unqualified-ids");
35
Faisal Vali2ab8c152017-12-30 04:15:27 +000036 Kind = UnqualifiedIdKind::IK_TemplateId;
Douglas Gregorb53edfb2009-11-10 19:49:08 +000037 this->TemplateId = TemplateId;
38 StartLocation = TemplateId->TemplateNameLoc;
39 EndLocation = TemplateId->RAngleLoc;
40}
41
Douglas Gregor9de54ea2010-01-13 17:31:36 +000042void UnqualifiedId::setConstructorTemplateId(TemplateIdAnnotation *TemplateId) {
43 assert(TemplateId && "NULL template-id annotation?");
Richard Smithb3f6e3d2020-03-27 17:08:26 -070044 assert(!TemplateId->isInvalid() &&
45 "should not convert invalid template-ids to unqualified-ids");
46
Faisal Vali2ab8c152017-12-30 04:15:27 +000047 Kind = UnqualifiedIdKind::IK_ConstructorTemplateId;
Douglas Gregor9de54ea2010-01-13 17:31:36 +000048 this->TemplateId = TemplateId;
49 StartLocation = TemplateId->TemplateNameLoc;
50 EndLocation = TemplateId->RAngleLoc;
51}
52
Fangrui Song6907ce22018-07-30 19:24:48 +000053void CXXScopeSpec::Extend(ASTContext &Context, SourceLocation TemplateKWLoc,
Douglas Gregor90c99722011-02-24 00:17:56 +000054 TypeLoc TL, SourceLocation ColonColonLoc) {
Douglas Gregor9b272512011-02-28 23:58:31 +000055 Builder.Extend(Context, TemplateKWLoc, TL, ColonColonLoc);
Douglas Gregor90c99722011-02-24 00:17:56 +000056 if (Range.getBegin().isInvalid())
57 Range.setBegin(TL.getBeginLoc());
58 Range.setEnd(ColonColonLoc);
Douglas Gregor869ad452011-02-24 17:54:50 +000059
Douglas Gregor9b272512011-02-28 23:58:31 +000060 assert(Range == Builder.getSourceRange() &&
Douglas Gregor869ad452011-02-24 17:54:50 +000061 "NestedNameSpecifierLoc range computation incorrect");
Douglas Gregor90c99722011-02-24 00:17:56 +000062}
63
64void CXXScopeSpec::Extend(ASTContext &Context, IdentifierInfo *Identifier,
Fangrui Song6907ce22018-07-30 19:24:48 +000065 SourceLocation IdentifierLoc,
Douglas Gregor90c99722011-02-24 00:17:56 +000066 SourceLocation ColonColonLoc) {
Douglas Gregor9b272512011-02-28 23:58:31 +000067 Builder.Extend(Context, Identifier, IdentifierLoc, ColonColonLoc);
Fangrui Song6907ce22018-07-30 19:24:48 +000068
Douglas Gregor90c99722011-02-24 00:17:56 +000069 if (Range.getBegin().isInvalid())
70 Range.setBegin(IdentifierLoc);
71 Range.setEnd(ColonColonLoc);
Fangrui Song6907ce22018-07-30 19:24:48 +000072
Douglas Gregor9b272512011-02-28 23:58:31 +000073 assert(Range == Builder.getSourceRange() &&
Douglas Gregor869ad452011-02-24 17:54:50 +000074 "NestedNameSpecifierLoc range computation incorrect");
Douglas Gregor90c99722011-02-24 00:17:56 +000075}
76
77void CXXScopeSpec::Extend(ASTContext &Context, NamespaceDecl *Namespace,
Fangrui Song6907ce22018-07-30 19:24:48 +000078 SourceLocation NamespaceLoc,
Douglas Gregor90c99722011-02-24 00:17:56 +000079 SourceLocation ColonColonLoc) {
Douglas Gregor9b272512011-02-28 23:58:31 +000080 Builder.Extend(Context, Namespace, NamespaceLoc, ColonColonLoc);
Fangrui Song6907ce22018-07-30 19:24:48 +000081
Douglas Gregor90c99722011-02-24 00:17:56 +000082 if (Range.getBegin().isInvalid())
83 Range.setBegin(NamespaceLoc);
84 Range.setEnd(ColonColonLoc);
Douglas Gregor869ad452011-02-24 17:54:50 +000085
Douglas Gregor9b272512011-02-28 23:58:31 +000086 assert(Range == Builder.getSourceRange() &&
Douglas Gregor869ad452011-02-24 17:54:50 +000087 "NestedNameSpecifierLoc range computation incorrect");
Douglas Gregor90c99722011-02-24 00:17:56 +000088}
89
Douglas Gregor7b26ff92011-02-24 02:36:08 +000090void CXXScopeSpec::Extend(ASTContext &Context, NamespaceAliasDecl *Alias,
Fangrui Song6907ce22018-07-30 19:24:48 +000091 SourceLocation AliasLoc,
Douglas Gregor7b26ff92011-02-24 02:36:08 +000092 SourceLocation ColonColonLoc) {
Douglas Gregor9b272512011-02-28 23:58:31 +000093 Builder.Extend(Context, Alias, AliasLoc, ColonColonLoc);
Fangrui Song6907ce22018-07-30 19:24:48 +000094
Douglas Gregor7b26ff92011-02-24 02:36:08 +000095 if (Range.getBegin().isInvalid())
96 Range.setBegin(AliasLoc);
97 Range.setEnd(ColonColonLoc);
Douglas Gregor869ad452011-02-24 17:54:50 +000098
Douglas Gregor9b272512011-02-28 23:58:31 +000099 assert(Range == Builder.getSourceRange() &&
Douglas Gregor869ad452011-02-24 17:54:50 +0000100 "NestedNameSpecifierLoc range computation incorrect");
Douglas Gregor7b26ff92011-02-24 02:36:08 +0000101}
102
Fangrui Song6907ce22018-07-30 19:24:48 +0000103void CXXScopeSpec::MakeGlobal(ASTContext &Context,
Douglas Gregor90c99722011-02-24 00:17:56 +0000104 SourceLocation ColonColonLoc) {
Douglas Gregor9b272512011-02-28 23:58:31 +0000105 Builder.MakeGlobal(Context, ColonColonLoc);
Fangrui Song6907ce22018-07-30 19:24:48 +0000106
Douglas Gregor90c99722011-02-24 00:17:56 +0000107 Range = SourceRange(ColonColonLoc);
Fangrui Song6907ce22018-07-30 19:24:48 +0000108
Douglas Gregor9b272512011-02-28 23:58:31 +0000109 assert(Range == Builder.getSourceRange() &&
Douglas Gregor869ad452011-02-24 17:54:50 +0000110 "NestedNameSpecifierLoc range computation incorrect");
111}
112
Nikola Smiljanic67860242014-09-26 00:28:20 +0000113void CXXScopeSpec::MakeSuper(ASTContext &Context, CXXRecordDecl *RD,
114 SourceLocation SuperLoc,
115 SourceLocation ColonColonLoc) {
116 Builder.MakeSuper(Context, RD, SuperLoc, ColonColonLoc);
117
118 Range.setBegin(SuperLoc);
119 Range.setEnd(ColonColonLoc);
120
121 assert(Range == Builder.getSourceRange() &&
122 "NestedNameSpecifierLoc range computation incorrect");
123}
124
Fangrui Song6907ce22018-07-30 19:24:48 +0000125void CXXScopeSpec::MakeTrivial(ASTContext &Context,
Douglas Gregor869ad452011-02-24 17:54:50 +0000126 NestedNameSpecifier *Qualifier, SourceRange R) {
Douglas Gregor9b272512011-02-28 23:58:31 +0000127 Builder.MakeTrivial(Context, Qualifier, R);
Douglas Gregor869ad452011-02-24 17:54:50 +0000128 Range = R;
Douglas Gregor869ad452011-02-24 17:54:50 +0000129}
130
131void CXXScopeSpec::Adopt(NestedNameSpecifierLoc Other) {
132 if (!Other) {
133 Range = SourceRange();
Douglas Gregor9b272512011-02-28 23:58:31 +0000134 Builder.Clear();
Douglas Gregor869ad452011-02-24 17:54:50 +0000135 return;
136 }
Douglas Gregor9b272512011-02-28 23:58:31 +0000137
Douglas Gregor869ad452011-02-24 17:54:50 +0000138 Range = Other.getSourceRange();
Douglas Gregor9b272512011-02-28 23:58:31 +0000139 Builder.Adopt(Other);
Richard Smith0e3a4872020-02-07 16:34:08 -0800140 assert(Range == Builder.getSourceRange() &&
141 "NestedNameSpecifierLoc range computation incorrect");
Douglas Gregor869ad452011-02-24 17:54:50 +0000142}
143
John McCall21878762011-07-06 06:57:57 +0000144SourceLocation CXXScopeSpec::getLastQualifierNameLoc() const {
145 if (!Builder.getRepresentation())
Eugene Zelenko1e95bc02018-04-05 22:15:42 +0000146 return SourceLocation();
John McCall21878762011-07-06 06:57:57 +0000147 return Builder.getTemporary().getLocalBeginLoc();
148}
149
Fangrui Song6907ce22018-07-30 19:24:48 +0000150NestedNameSpecifierLoc
Douglas Gregor14454802011-02-25 02:25:35 +0000151CXXScopeSpec::getWithLocInContext(ASTContext &Context) const {
Douglas Gregor2b1ca9e2011-03-03 21:48:55 +0000152 if (!Builder.getRepresentation())
Eugene Zelenko1e95bc02018-04-05 22:15:42 +0000153 return NestedNameSpecifierLoc();
Fangrui Song6907ce22018-07-30 19:24:48 +0000154
Douglas Gregor9b272512011-02-28 23:58:31 +0000155 return Builder.getWithLocInContext(Context);
Douglas Gregor90c99722011-02-24 00:17:56 +0000156}
157
Chris Lattner1ce41ed2009-01-20 19:11:22 +0000158/// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function.
159/// "TheDeclarator" is the declarator that this will be added to.
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000160DeclaratorChunk DeclaratorChunk::getFunction(bool hasProto,
Richard Smith943c4402012-07-30 21:30:52 +0000161 bool isAmbiguous,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000162 SourceLocation LParenLoc,
Alp Tokerc5350722014-02-26 22:27:52 +0000163 ParamInfo *Params,
164 unsigned NumParams,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000165 SourceLocation EllipsisLoc,
166 SourceLocation RParenLoc,
Douglas Gregor54992352011-01-26 03:43:54 +0000167 bool RefQualifierIsLvalueRef,
168 SourceLocation RefQualifierLoc,
Douglas Gregorad69e652011-07-13 21:47:47 +0000169 SourceLocation MutableLoc,
Sebastian Redl802a4532011-03-05 22:42:13 +0000170 ExceptionSpecificationType
171 ESpecType,
Nathan Wilsone23a9a42015-08-26 04:19:36 +0000172 SourceRange ESpecRange,
John McCallba7bf592010-08-24 05:47:05 +0000173 ParsedType *Exceptions,
Sebastian Redld6434562009-05-29 18:02:33 +0000174 SourceRange *ExceptionRanges,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +0000175 unsigned NumExceptions,
Sebastian Redl802a4532011-03-05 22:42:13 +0000176 Expr *NoexceptExpr,
Richard Smith0b3a4622014-11-13 20:01:57 +0000177 CachedTokens *ExceptionSpecTokens,
Reid Kleckner078aea92016-12-09 17:14:05 +0000178 ArrayRef<NamedDecl*>
179 DeclsInPrototype,
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000180 SourceLocation LocalRangeBegin,
181 SourceLocation LocalRangeEnd,
Douglas Gregor7fb25412010-10-01 18:44:50 +0000182 Declarator &TheDeclarator,
Anastasia Stulovaa9bc4bd2019-01-09 11:25:09 +0000183 TypeResult TrailingReturnType,
184 DeclSpec *MethodQualifiers) {
185 assert(!(MethodQualifiers && MethodQualifiers->getTypeQualifiers() & DeclSpec::TQ_atomic) &&
Richard Smith8e1ac332013-03-28 01:55:44 +0000186 "function cannot have _Atomic qualifier");
187
Chris Lattner1ce41ed2009-01-20 19:11:22 +0000188 DeclaratorChunk I;
Sebastian Redl802a4532011-03-05 22:42:13 +0000189 I.Kind = Function;
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000190 I.Loc = LocalRangeBegin;
191 I.EndLoc = LocalRangeEnd;
Sebastian Redl802a4532011-03-05 22:42:13 +0000192 I.Fun.hasPrototype = hasProto;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000193 I.Fun.isVariadic = EllipsisLoc.isValid();
Richard Smith943c4402012-07-30 21:30:52 +0000194 I.Fun.isAmbiguous = isAmbiguous;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000195 I.Fun.LParenLoc = LParenLoc.getRawEncoding();
Sebastian Redl802a4532011-03-05 22:42:13 +0000196 I.Fun.EllipsisLoc = EllipsisLoc.getRawEncoding();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +0000197 I.Fun.RParenLoc = RParenLoc.getRawEncoding();
Alp Tokerc5350722014-02-26 22:27:52 +0000198 I.Fun.DeleteParams = false;
Alp Tokerc5350722014-02-26 22:27:52 +0000199 I.Fun.NumParams = NumParams;
Craig Topper4b566922014-06-09 02:04:02 +0000200 I.Fun.Params = nullptr;
Douglas Gregor54992352011-01-26 03:43:54 +0000201 I.Fun.RefQualifierIsLValueRef = RefQualifierIsLvalueRef;
Sebastian Redl802a4532011-03-05 22:42:13 +0000202 I.Fun.RefQualifierLoc = RefQualifierLoc.getRawEncoding();
Douglas Gregorad69e652011-07-13 21:47:47 +0000203 I.Fun.MutableLoc = MutableLoc.getRawEncoding();
Sebastian Redl802a4532011-03-05 22:42:13 +0000204 I.Fun.ExceptionSpecType = ESpecType;
Nathan Wilsone23a9a42015-08-26 04:19:36 +0000205 I.Fun.ExceptionSpecLocBeg = ESpecRange.getBegin().getRawEncoding();
206 I.Fun.ExceptionSpecLocEnd = ESpecRange.getEnd().getRawEncoding();
Reid Kleckner078aea92016-12-09 17:14:05 +0000207 I.Fun.NumExceptionsOrDecls = 0;
Craig Topper4b566922014-06-09 02:04:02 +0000208 I.Fun.Exceptions = nullptr;
209 I.Fun.NoexceptExpr = nullptr;
Richard Smith700537c2012-06-12 01:51:59 +0000210 I.Fun.HasTrailingReturnType = TrailingReturnType.isUsable() ||
211 TrailingReturnType.isInvalid();
212 I.Fun.TrailingReturnType = TrailingReturnType.get();
Anastasia Stulovaa9bc4bd2019-01-09 11:25:09 +0000213 I.Fun.MethodQualifiers = nullptr;
214 I.Fun.QualAttrFactory = nullptr;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +0000215
Anastasia Stulovaa9bc4bd2019-01-09 11:25:09 +0000216 if (MethodQualifiers && (MethodQualifiers->getTypeQualifiers() ||
217 MethodQualifiers->getAttributes().size())) {
218 auto &attrs = MethodQualifiers->getAttributes();
219 I.Fun.MethodQualifiers = new DeclSpec(attrs.getPool().getFactory());
220 MethodQualifiers->forEachCVRUQualifier(
221 [&](DeclSpec::TQ TypeQual, StringRef PrintName, SourceLocation SL) {
222 I.Fun.MethodQualifiers->SetTypeQual(TypeQual, SL);
223 });
224 I.Fun.MethodQualifiers->getAttributes().takeAllFrom(attrs);
225 I.Fun.MethodQualifiers->getAttributePool().takeAllFrom(attrs.getPool());
226 }
227
Richard Smith0b3a4622014-11-13 20:01:57 +0000228 assert(I.Fun.ExceptionSpecType == ESpecType && "bitfield overflow");
229
Alp Tokerc5350722014-02-26 22:27:52 +0000230 // new[] a parameter array if needed.
231 if (NumParams) {
Chris Lattner1ce41ed2009-01-20 19:11:22 +0000232 // If the 'InlineParams' in Declarator is unused and big enough, put our
233 // parameter list there (in an effort to avoid new/delete traffic). If it
234 // is already used (consider a function returning a function pointer) or too
Alp Tokerc5350722014-02-26 22:27:52 +0000235 // small (function with too many parameters), go to the heap.
Richard Smithbdb84f32016-07-22 23:36:59 +0000236 if (!TheDeclarator.InlineStorageUsed &&
Alp Tokerc5350722014-02-26 22:27:52 +0000237 NumParams <= llvm::array_lengthof(TheDeclarator.InlineParams)) {
238 I.Fun.Params = TheDeclarator.InlineParams;
Malcolm Parsonsca9d8342016-11-17 21:00:09 +0000239 new (I.Fun.Params) ParamInfo[NumParams];
Alp Tokerc5350722014-02-26 22:27:52 +0000240 I.Fun.DeleteParams = false;
Richard Smithbdb84f32016-07-22 23:36:59 +0000241 TheDeclarator.InlineStorageUsed = true;
Chris Lattner1ce41ed2009-01-20 19:11:22 +0000242 } else {
Malcolm Parsonsca9d8342016-11-17 21:00:09 +0000243 I.Fun.Params = new DeclaratorChunk::ParamInfo[NumParams];
Alp Tokerc5350722014-02-26 22:27:52 +0000244 I.Fun.DeleteParams = true;
Chris Lattner1ce41ed2009-01-20 19:11:22 +0000245 }
Malcolm Parsonsff0382c2016-11-17 17:52:58 +0000246 for (unsigned i = 0; i < NumParams; i++)
Fangrui Song6907ce22018-07-30 19:24:48 +0000247 I.Fun.Params[i] = std::move(Params[i]);
Chris Lattner1ce41ed2009-01-20 19:11:22 +0000248 }
Sebastian Redl802a4532011-03-05 22:42:13 +0000249
250 // Check what exception specification information we should actually store.
251 switch (ESpecType) {
252 default: break; // By default, save nothing.
253 case EST_Dynamic:
254 // new[] an exception array if needed
255 if (NumExceptions) {
Reid Kleckner078aea92016-12-09 17:14:05 +0000256 I.Fun.NumExceptionsOrDecls = NumExceptions;
Sebastian Redl802a4532011-03-05 22:42:13 +0000257 I.Fun.Exceptions = new DeclaratorChunk::TypeAndRange[NumExceptions];
258 for (unsigned i = 0; i != NumExceptions; ++i) {
259 I.Fun.Exceptions[i].Ty = Exceptions[i];
260 I.Fun.Exceptions[i].Range = ExceptionRanges[i];
261 }
Sebastian Redld6434562009-05-29 18:02:33 +0000262 }
Sebastian Redl802a4532011-03-05 22:42:13 +0000263 break;
264
Richard Smitheaf11ad2018-05-03 03:58:32 +0000265 case EST_DependentNoexcept:
266 case EST_NoexceptFalse:
267 case EST_NoexceptTrue:
Sebastian Redl802a4532011-03-05 22:42:13 +0000268 I.Fun.NoexceptExpr = NoexceptExpr;
269 break;
Richard Smith0b3a4622014-11-13 20:01:57 +0000270
271 case EST_Unparsed:
272 I.Fun.ExceptionSpecTokens = ExceptionSpecTokens;
273 break;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +0000274 }
Reid Kleckner078aea92016-12-09 17:14:05 +0000275
276 if (!DeclsInPrototype.empty()) {
277 assert(ESpecType == EST_None && NumExceptions == 0 &&
278 "cannot have exception specifiers and decls in prototype");
279 I.Fun.NumExceptionsOrDecls = DeclsInPrototype.size();
280 // Copy the array of decls into stable heap storage.
281 I.Fun.DeclsInPrototype = new NamedDecl *[DeclsInPrototype.size()];
282 for (size_t J = 0; J < DeclsInPrototype.size(); ++J)
283 I.Fun.DeclsInPrototype[J] = DeclsInPrototype[J];
284 }
285
Chris Lattner1ce41ed2009-01-20 19:11:22 +0000286 return I;
287}
Chris Lattner3b0f3ef2008-11-22 08:32:36 +0000288
Richard Smithbdb84f32016-07-22 23:36:59 +0000289void Declarator::setDecompositionBindings(
290 SourceLocation LSquareLoc,
291 ArrayRef<DecompositionDeclarator::Binding> Bindings,
292 SourceLocation RSquareLoc) {
293 assert(!hasName() && "declarator given multiple names!");
294
295 BindingGroup.LSquareLoc = LSquareLoc;
296 BindingGroup.RSquareLoc = RSquareLoc;
297 BindingGroup.NumBindings = Bindings.size();
298 Range.setEnd(RSquareLoc);
299
300 // We're now past the identifier.
301 SetIdentifier(nullptr, LSquareLoc);
302 Name.EndLocation = RSquareLoc;
303
304 // Allocate storage for bindings and stash them away.
Eugene Zelenko1e95bc02018-04-05 22:15:42 +0000305 if (Bindings.size()) {
Richard Smithbdb84f32016-07-22 23:36:59 +0000306 if (!InlineStorageUsed &&
307 Bindings.size() <= llvm::array_lengthof(InlineBindings)) {
308 BindingGroup.Bindings = InlineBindings;
309 BindingGroup.DeleteBindings = false;
310 InlineStorageUsed = true;
311 } else {
312 BindingGroup.Bindings =
313 new DecompositionDeclarator::Binding[Bindings.size()];
314 BindingGroup.DeleteBindings = true;
315 }
316 std::uninitialized_copy(Bindings.begin(), Bindings.end(),
317 BindingGroup.Bindings);
318 }
319}
320
Douglas Gregorc15b0cf2011-06-25 00:56:27 +0000321bool Declarator::isDeclarationOfFunction() const {
Eugene Zelenko1e95bc02018-04-05 22:15:42 +0000322 for (unsigned i = 0, i_end = DeclTypeInfo.size(); i < i_end; ++i) {
323 switch (DeclTypeInfo[i].Kind) {
Richard Smithcfcdf3a2011-06-25 02:28:38 +0000324 case DeclaratorChunk::Function:
325 return true;
326 case DeclaratorChunk::Paren:
327 continue;
328 case DeclaratorChunk::Pointer:
329 case DeclaratorChunk::Reference:
330 case DeclaratorChunk::Array:
331 case DeclaratorChunk::BlockPointer:
332 case DeclaratorChunk::MemberPointer:
Xiuli Pan9c14e282016-01-09 12:53:17 +0000333 case DeclaratorChunk::Pipe:
Richard Smithcfcdf3a2011-06-25 02:28:38 +0000334 return false;
335 }
336 llvm_unreachable("Invalid type chunk");
Richard Smithcfcdf3a2011-06-25 02:28:38 +0000337 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000338
Douglas Gregorc15b0cf2011-06-25 00:56:27 +0000339 switch (DS.getTypeSpecType()) {
Faisal Vali090da2d2018-01-01 18:23:28 +0000340 case TST_atomic:
341 case TST_auto:
342 case TST_auto_type:
343 case TST_bool:
344 case TST_char:
Richard Smith3a8244d2018-05-01 05:02:45 +0000345 case TST_char8:
Faisal Vali090da2d2018-01-01 18:23:28 +0000346 case TST_char16:
347 case TST_char32:
348 case TST_class:
349 case TST_decimal128:
350 case TST_decimal32:
351 case TST_decimal64:
352 case TST_double:
Leonard Chanf921d852018-06-04 16:07:52 +0000353 case TST_Accum:
Leonard Chanab80f3c2018-06-14 14:53:51 +0000354 case TST_Fract:
Faisal Vali090da2d2018-01-01 18:23:28 +0000355 case TST_Float16:
356 case TST_float128:
357 case TST_enum:
358 case TST_error:
359 case TST_float:
360 case TST_half:
361 case TST_int:
362 case TST_int128:
Erich Keane5f0903e2020-04-17 10:44:19 -0700363 case TST_extint:
Faisal Vali090da2d2018-01-01 18:23:28 +0000364 case TST_struct:
365 case TST_interface:
366 case TST_union:
367 case TST_unknown_anytype:
368 case TST_unspecified:
369 case TST_void:
370 case TST_wchar:
371#define GENERIC_IMAGE_TYPE(ImgType, Id) case TST_##ImgType##_t:
Alexey Baderb62f1442016-04-13 08:33:41 +0000372#include "clang/Basic/OpenCLImageTypes.def"
Douglas Gregorc15b0cf2011-06-25 00:56:27 +0000373 return false;
374
Faisal Vali090da2d2018-01-01 18:23:28 +0000375 case TST_decltype_auto:
Richard Smith74aeef52013-04-26 16:15:35 +0000376 // This must have an initializer, so can't be a function declaration,
377 // even if the initializer has function type.
378 return false;
379
Faisal Vali090da2d2018-01-01 18:23:28 +0000380 case TST_decltype:
381 case TST_typeofExpr:
Douglas Gregorc15b0cf2011-06-25 00:56:27 +0000382 if (Expr *E = DS.getRepAsExpr())
383 return E->getType()->isFunctionType();
384 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000385
Faisal Vali090da2d2018-01-01 18:23:28 +0000386 case TST_underlyingType:
387 case TST_typename:
388 case TST_typeofType: {
Douglas Gregorc15b0cf2011-06-25 00:56:27 +0000389 QualType QT = DS.getRepAsType().get();
390 if (QT.isNull())
391 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000392
Eugene Zelenko1e95bc02018-04-05 22:15:42 +0000393 if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT))
Douglas Gregorc15b0cf2011-06-25 00:56:27 +0000394 QT = LIT->getType();
395
396 if (QT.isNull())
397 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000398
Douglas Gregorc15b0cf2011-06-25 00:56:27 +0000399 return QT->isFunctionType();
400 }
401 }
David Blaikie8a40f702012-01-17 06:56:22 +0000402
403 llvm_unreachable("Invalid TypeSpecType!");
Douglas Gregorc15b0cf2011-06-25 00:56:27 +0000404}
405
Reid Kleckner9a7f3e62013-10-08 00:58:57 +0000406bool Declarator::isStaticMember() {
Faisal Vali421b2d12017-12-29 05:41:00 +0000407 assert(getContext() == DeclaratorContext::MemberContext);
Reid Kleckner9a7f3e62013-10-08 00:58:57 +0000408 return getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static ||
Faisal Vali2ab8c152017-12-30 04:15:27 +0000409 (getName().Kind == UnqualifiedIdKind::IK_OperatorFunctionId &&
Petar Jovanovice886cfb2015-03-30 00:43:56 +0000410 CXXMethodDecl::isStaticOverloadedOperator(
411 getName().OperatorFunctionId.Operator));
Reid Kleckner9a7f3e62013-10-08 00:58:57 +0000412}
413
Andrey Bokhankoddc04ef2015-09-14 21:29:57 +0000414bool Declarator::isCtorOrDtor() {
Faisal Vali2ab8c152017-12-30 04:15:27 +0000415 return (getName().getKind() == UnqualifiedIdKind::IK_ConstructorName) ||
416 (getName().getKind() == UnqualifiedIdKind::IK_DestructorName);
Andrey Bokhankoddc04ef2015-09-14 21:29:57 +0000417}
418
Anastasia Stulovaa9bc4bd2019-01-09 11:25:09 +0000419void DeclSpec::forEachCVRUQualifier(
420 llvm::function_ref<void(TQ, StringRef, SourceLocation)> Handle) {
421 if (TypeQualifiers & TQ_const)
422 Handle(TQ_const, "const", TQ_constLoc);
423 if (TypeQualifiers & TQ_volatile)
424 Handle(TQ_volatile, "volatile", TQ_volatileLoc);
425 if (TypeQualifiers & TQ_restrict)
426 Handle(TQ_restrict, "restrict", TQ_restrictLoc);
427 if (TypeQualifiers & TQ_unaligned)
428 Handle(TQ_unaligned, "unaligned", TQ_unalignedLoc);
429}
430
431void DeclSpec::forEachQualifier(
432 llvm::function_ref<void(TQ, StringRef, SourceLocation)> Handle) {
433 forEachCVRUQualifier(Handle);
434 // FIXME: Add code below to iterate through the attributes and call Handle.
435}
436
Richard Smith404dfb42013-11-19 22:47:36 +0000437bool DeclSpec::hasTagDefinition() const {
438 if (!TypeSpecOwned)
439 return false;
440 return cast<TagDecl>(getRepAsDecl())->isCompleteDefinition();
441}
442
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000443/// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
Chris Lattnere0c51162009-02-27 18:35:46 +0000444/// declaration specifier includes.
Eugene Zelenko1e95bc02018-04-05 22:15:42 +0000445///
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000446unsigned DeclSpec::getParsedSpecifiers() const {
447 unsigned Res = 0;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000448 if (StorageClassSpec != SCS_unspecified ||
Richard Smithb4a9e862013-04-12 22:46:28 +0000449 ThreadStorageClassSpec != TSCS_unspecified)
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000450 Res |= PQ_StorageClassSpecifier;
Mike Stump65643c62008-06-19 19:52:46 +0000451
Chris Lattner4d8f8732006-11-28 05:05:08 +0000452 if (TypeQualifiers != TQ_unspecified)
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000453 Res |= PQ_TypeQualifier;
Mike Stump11289f42009-09-09 15:08:12 +0000454
Chris Lattnerf055d432006-11-28 04:28:12 +0000455 if (hasTypeSpecifier())
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000456 Res |= PQ_TypeSpecifier;
Mike Stump11289f42009-09-09 15:08:12 +0000457
Richard Smith76b90272019-05-09 03:59:21 +0000458 if (FS_inline_specified || FS_virtual_specified || hasExplicitSpecifier() ||
Serge Pavlov750db652013-11-13 06:57:53 +0000459 FS_noreturn_specified || FS_forceinline_specified)
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000460 Res |= PQ_FunctionSpecifier;
461 return Res;
462}
463
John McCall49bfce42009-08-03 20:12:06 +0000464template <class T> static bool BadSpecifier(T TNew, T TPrev,
465 const char *&PrevSpec,
Aaron Ballman3731b332012-08-28 20:55:40 +0000466 unsigned &DiagID,
467 bool IsExtension = true) {
John McCall898cd0f2009-08-03 18:47:27 +0000468 PrevSpec = DeclSpec::getSpecifierName(TPrev);
Aaron Ballman3731b332012-08-28 20:55:40 +0000469 if (TNew != TPrev)
470 DiagID = diag::err_invalid_decl_spec_combination;
471 else
Nick Desaulniers150ca532018-10-03 23:09:29 +0000472 DiagID = IsExtension ? diag::ext_warn_duplicate_declspec :
Fangrui Song6907ce22018-07-30 19:24:48 +0000473 diag::warn_duplicate_declspec;
John McCall898cd0f2009-08-03 18:47:27 +0000474 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000475}
John McCall898cd0f2009-08-03 18:47:27 +0000476
Chris Lattner7bd11fe2007-01-23 04:35:33 +0000477const char *DeclSpec::getSpecifierName(DeclSpec::SCS S) {
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000478 switch (S) {
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000479 case DeclSpec::SCS_unspecified: return "unspecified";
480 case DeclSpec::SCS_typedef: return "typedef";
481 case DeclSpec::SCS_extern: return "extern";
482 case DeclSpec::SCS_static: return "static";
483 case DeclSpec::SCS_auto: return "auto";
484 case DeclSpec::SCS_register: return "register";
Eli Friedmand5c0eed2009-04-19 20:27:55 +0000485 case DeclSpec::SCS_private_extern: return "__private_extern__";
Sebastian Redlccdfaba2008-11-14 23:42:31 +0000486 case DeclSpec::SCS_mutable: return "mutable";
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000487 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000488 llvm_unreachable("Unknown typespec!");
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000489}
490
Richard Smithb4a9e862013-04-12 22:46:28 +0000491const char *DeclSpec::getSpecifierName(DeclSpec::TSCS S) {
492 switch (S) {
493 case DeclSpec::TSCS_unspecified: return "unspecified";
494 case DeclSpec::TSCS___thread: return "__thread";
495 case DeclSpec::TSCS_thread_local: return "thread_local";
496 case DeclSpec::TSCS__Thread_local: return "_Thread_local";
497 }
498 llvm_unreachable("Unknown typespec!");
499}
500
John McCall898cd0f2009-08-03 18:47:27 +0000501const char *DeclSpec::getSpecifierName(TSW W) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000502 switch (W) {
John McCall898cd0f2009-08-03 18:47:27 +0000503 case TSW_unspecified: return "unspecified";
504 case TSW_short: return "short";
505 case TSW_long: return "long";
506 case TSW_longlong: return "long long";
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000507 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000508 llvm_unreachable("Unknown typespec!");
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000509}
510
John McCall898cd0f2009-08-03 18:47:27 +0000511const char *DeclSpec::getSpecifierName(TSC C) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000512 switch (C) {
John McCall898cd0f2009-08-03 18:47:27 +0000513 case TSC_unspecified: return "unspecified";
514 case TSC_imaginary: return "imaginary";
515 case TSC_complex: return "complex";
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000516 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000517 llvm_unreachable("Unknown typespec!");
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000518}
519
Eugene Zelenko1e95bc02018-04-05 22:15:42 +0000520
John McCall898cd0f2009-08-03 18:47:27 +0000521const char *DeclSpec::getSpecifierName(TSS S) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000522 switch (S) {
John McCall898cd0f2009-08-03 18:47:27 +0000523 case TSS_unspecified: return "unspecified";
524 case TSS_signed: return "signed";
525 case TSS_unsigned: return "unsigned";
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000526 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000527 llvm_unreachable("Unknown typespec!");
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000528}
Faisal Valia534f072018-04-26 00:42:40 +0000529
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000530const char *DeclSpec::getSpecifierName(DeclSpec::TST T,
531 const PrintingPolicy &Policy) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000532 switch (T) {
Chris Lattner839713c2006-08-04 06:15:52 +0000533 case DeclSpec::TST_unspecified: return "unspecified";
534 case DeclSpec::TST_void: return "void";
535 case DeclSpec::TST_char: return "char";
Faisal Vali090da2d2018-01-01 18:23:28 +0000536 case DeclSpec::TST_wchar: return Policy.MSWChar ? "__wchar_t" : "wchar_t";
Richard Smith3a8244d2018-05-01 05:02:45 +0000537 case DeclSpec::TST_char8: return "char8_t";
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000538 case DeclSpec::TST_char16: return "char16_t";
539 case DeclSpec::TST_char32: return "char32_t";
Chris Lattner839713c2006-08-04 06:15:52 +0000540 case DeclSpec::TST_int: return "int";
Richard Smithf016bbc2012-04-04 06:24:32 +0000541 case DeclSpec::TST_int128: return "__int128";
Erich Keane5f0903e2020-04-17 10:44:19 -0700542 case DeclSpec::TST_extint: return "_ExtInt";
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +0000543 case DeclSpec::TST_half: return "half";
Chris Lattner839713c2006-08-04 06:15:52 +0000544 case DeclSpec::TST_float: return "float";
545 case DeclSpec::TST_double: return "double";
Leonard Chanf921d852018-06-04 16:07:52 +0000546 case DeclSpec::TST_accum: return "_Accum";
Leonard Chanab80f3c2018-06-14 14:53:51 +0000547 case DeclSpec::TST_fract: return "_Fract";
Sjoerd Meijercc623ad2017-09-08 15:15:00 +0000548 case DeclSpec::TST_float16: return "_Float16";
Nemanja Ivanovicbb1ea2d2016-05-09 08:52:33 +0000549 case DeclSpec::TST_float128: return "__float128";
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000550 case DeclSpec::TST_bool: return Policy.Bool ? "bool" : "_Bool";
Chris Lattner839713c2006-08-04 06:15:52 +0000551 case DeclSpec::TST_decimal32: return "_Decimal32";
552 case DeclSpec::TST_decimal64: return "_Decimal64";
553 case DeclSpec::TST_decimal128: return "_Decimal128";
Chris Lattnerda72c822006-08-13 22:16:42 +0000554 case DeclSpec::TST_enum: return "enum";
Chris Lattner861a2262008-04-13 18:59:07 +0000555 case DeclSpec::TST_class: return "class";
Chris Lattnerda72c822006-08-13 22:16:42 +0000556 case DeclSpec::TST_union: return "union";
557 case DeclSpec::TST_struct: return "struct";
Joao Matosdc86f942012-08-31 18:45:21 +0000558 case DeclSpec::TST_interface: return "__interface";
Douglas Gregor9817f4a2009-02-09 15:09:02 +0000559 case DeclSpec::TST_typename: return "type-name";
Steve Naroffad373bd2007-07-31 12:34:36 +0000560 case DeclSpec::TST_typeofType:
561 case DeclSpec::TST_typeofExpr: return "typeof";
John McCall898cd0f2009-08-03 18:47:27 +0000562 case DeclSpec::TST_auto: return "auto";
Richard Smithe301ba22015-11-11 02:02:15 +0000563 case DeclSpec::TST_auto_type: return "__auto_type";
John McCall898cd0f2009-08-03 18:47:27 +0000564 case DeclSpec::TST_decltype: return "(decltype)";
Richard Smith74aeef52013-04-26 16:15:35 +0000565 case DeclSpec::TST_decltype_auto: return "decltype(auto)";
Alexis Hunte852b102011-05-24 22:41:36 +0000566 case DeclSpec::TST_underlyingType: return "__underlying_type";
John McCall39439732011-04-09 22:50:59 +0000567 case DeclSpec::TST_unknown_anytype: return "__unknown_anytype";
Eli Friedman0dfb8892011-10-06 23:00:33 +0000568 case DeclSpec::TST_atomic: return "_Atomic";
Alexey Bader954ba212016-04-08 13:40:33 +0000569#define GENERIC_IMAGE_TYPE(ImgType, Id) \
570 case DeclSpec::TST_##ImgType##_t: \
571 return #ImgType "_t";
Alexey Baderb62f1442016-04-13 08:33:41 +0000572#include "clang/Basic/OpenCLImageTypes.def"
John McCall898cd0f2009-08-03 18:47:27 +0000573 case DeclSpec::TST_error: return "(error)";
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000574 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000575 llvm_unreachable("Unknown typespec!");
Chris Lattner839713c2006-08-04 06:15:52 +0000576}
577
Gauthier Harnisch796ed032019-06-14 08:56:20 +0000578const char *DeclSpec::getSpecifierName(ConstexprSpecKind C) {
579 switch (C) {
580 case CSK_unspecified: return "unspecified";
581 case CSK_constexpr: return "constexpr";
582 case CSK_consteval: return "consteval";
Richard Smitha6e8b682019-09-04 20:30:37 +0000583 case CSK_constinit: return "constinit";
Gauthier Harnisch796ed032019-06-14 08:56:20 +0000584 }
585 llvm_unreachable("Unknown ConstexprSpecKind");
586}
587
John McCall898cd0f2009-08-03 18:47:27 +0000588const char *DeclSpec::getSpecifierName(TQ T) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000589 switch (T) {
John McCall898cd0f2009-08-03 18:47:27 +0000590 case DeclSpec::TQ_unspecified: return "unspecified";
591 case DeclSpec::TQ_const: return "const";
592 case DeclSpec::TQ_restrict: return "restrict";
593 case DeclSpec::TQ_volatile: return "volatile";
Richard Smith8e1ac332013-03-28 01:55:44 +0000594 case DeclSpec::TQ_atomic: return "_Atomic";
Andrey Bokhanko45d41322016-05-11 18:38:21 +0000595 case DeclSpec::TQ_unaligned: return "__unaligned";
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000596 }
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000597 llvm_unreachable("Unknown typespec!");
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000598}
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000599
Peter Collingbourne485b80f2011-10-06 03:01:00 +0000600bool DeclSpec::SetStorageClassSpec(Sema &S, SCS SC, SourceLocation Loc,
John McCall49bfce42009-08-03 20:12:06 +0000601 const char *&PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000602 unsigned &DiagID,
603 const PrintingPolicy &Policy) {
Tanya Lattner4fdce3f2012-06-19 23:09:52 +0000604 // OpenCL v1.1 s6.8g: "The extern, static, auto and register storage-class
605 // specifiers are not supported.
Peter Collingbournede32b202011-02-11 19:59:54 +0000606 // It seems sensible to prohibit private_extern too
Peter Collingbourne485b80f2011-10-06 03:01:00 +0000607 // The cl_clang_storage_class_specifiers extension enables support for
608 // these storage-class specifiers.
Tanya Lattner4fdce3f2012-06-19 23:09:52 +0000609 // OpenCL v1.2 s6.8 changes this to "The auto and register storage-class
610 // specifiers are not supported."
David Blaikiebbafb8a2012-03-11 07:00:24 +0000611 if (S.getLangOpts().OpenCL &&
Yaxun Liu5b746652016-12-18 05:18:55 +0000612 !S.getOpenCLOptions().isEnabled("cl_clang_storage_class_specifiers")) {
Peter Collingbourne485b80f2011-10-06 03:01:00 +0000613 switch (SC) {
Peter Collingbournede32b202011-02-11 19:59:54 +0000614 case SCS_extern:
615 case SCS_private_extern:
Tanya Lattner4fdce3f2012-06-19 23:09:52 +0000616 case SCS_static:
Anastasia Stulova04307942018-11-16 16:22:56 +0000617 if (S.getLangOpts().OpenCLVersion < 120 &&
618 !S.getLangOpts().OpenCLCPlusPlus) {
619 DiagID = diag::err_opencl_unknown_type_specifier;
Craig Topper7d2f9432015-11-14 19:31:52 +0000620 PrevSpec = getSpecifierName(SC);
621 return true;
622 }
623 break;
Peter Collingbournede32b202011-02-11 19:59:54 +0000624 case SCS_auto:
625 case SCS_register:
Anastasia Stulova2c8dcfb2014-11-26 14:10:06 +0000626 DiagID = diag::err_opencl_unknown_type_specifier;
Peter Collingbourne485b80f2011-10-06 03:01:00 +0000627 PrevSpec = getSpecifierName(SC);
Peter Collingbournede32b202011-02-11 19:59:54 +0000628 return true;
629 default:
630 break;
631 }
632 }
633
Abramo Bagnaraed5b6892010-07-30 16:47:02 +0000634 if (StorageClassSpec != SCS_unspecified) {
Richard Smith74aeef52013-04-26 16:15:35 +0000635 // Maybe this is an attempt to use C++11 'auto' outside of C++11 mode.
Richard Smith58c74332011-09-04 19:54:14 +0000636 bool isInvalid = true;
David Blaikiebbafb8a2012-03-11 07:00:24 +0000637 if (TypeSpecType == TST_unspecified && S.getLangOpts().CPlusPlus) {
Peter Collingbourne485b80f2011-10-06 03:01:00 +0000638 if (SC == SCS_auto)
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000639 return SetTypeSpecType(TST_auto, Loc, PrevSpec, DiagID, Policy);
Richard Smith58c74332011-09-04 19:54:14 +0000640 if (StorageClassSpec == SCS_auto) {
641 isInvalid = SetTypeSpecType(TST_auto, StorageClassSpecLoc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000642 PrevSpec, DiagID, Policy);
Richard Smith58c74332011-09-04 19:54:14 +0000643 assert(!isInvalid && "auto SCS -> TST recovery failed");
644 }
645 }
646
Abramo Bagnaraed5b6892010-07-30 16:47:02 +0000647 // Changing storage class is allowed only if the previous one
648 // was the 'extern' that is part of a linkage specification and
649 // the new storage class is 'typedef'.
Richard Smith58c74332011-09-04 19:54:14 +0000650 if (isInvalid &&
651 !(SCS_extern_in_linkage_spec &&
Abramo Bagnaraed5b6892010-07-30 16:47:02 +0000652 StorageClassSpec == SCS_extern &&
Peter Collingbourne485b80f2011-10-06 03:01:00 +0000653 SC == SCS_typedef))
654 return BadSpecifier(SC, (SCS)StorageClassSpec, PrevSpec, DiagID);
Abramo Bagnaraed5b6892010-07-30 16:47:02 +0000655 }
Peter Collingbourne485b80f2011-10-06 03:01:00 +0000656 StorageClassSpec = SC;
Chris Lattner4d8f8732006-11-28 05:05:08 +0000657 StorageClassSpecLoc = Loc;
Peter Collingbourne485b80f2011-10-06 03:01:00 +0000658 assert((unsigned)SC == StorageClassSpec && "SCS constants overflow bitfield");
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000659 return false;
660}
661
Richard Smithb4a9e862013-04-12 22:46:28 +0000662bool DeclSpec::SetStorageClassSpecThread(TSCS TSC, SourceLocation Loc,
John McCall49bfce42009-08-03 20:12:06 +0000663 const char *&PrevSpec,
664 unsigned &DiagID) {
Richard Smithb4a9e862013-04-12 22:46:28 +0000665 if (ThreadStorageClassSpec != TSCS_unspecified)
666 return BadSpecifier(TSC, (TSCS)ThreadStorageClassSpec, PrevSpec, DiagID);
667
668 ThreadStorageClassSpec = TSC;
669 ThreadStorageClassSpecLoc = Loc;
Chris Lattner353f5742006-11-28 04:50:12 +0000670 return false;
671}
672
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000673/// These methods set the specified attribute of the DeclSpec, but return true
674/// and ignore the request if invalid (e.g. "extern" then "auto" is
675/// specified).
Chris Lattnerb20e8942006-11-28 05:30:29 +0000676bool DeclSpec::SetTypeSpecWidth(TSW W, SourceLocation Loc,
John McCall49bfce42009-08-03 20:12:06 +0000677 const char *&PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000678 unsigned &DiagID,
679 const PrintingPolicy &Policy) {
Malcolm Parsons57c09c82016-10-21 21:13:56 +0000680 // Overwrite TSWRange.Begin only if TypeSpecWidth was unspecified, so that
Abramo Bagnaraead67d92011-03-06 22:21:56 +0000681 // for 'long long' we will keep the source location of the first 'long'.
682 if (TypeSpecWidth == TSW_unspecified)
Malcolm Parsons57c09c82016-10-21 21:13:56 +0000683 TSWRange.setBegin(Loc);
Abramo Bagnaraead67d92011-03-06 22:21:56 +0000684 // Allow turning long -> long long.
685 else if (W != TSW_longlong || TypeSpecWidth != TSW_long)
John McCall49bfce42009-08-03 20:12:06 +0000686 return BadSpecifier(W, (TSW)TypeSpecWidth, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000687 TypeSpecWidth = W;
Malcolm Parsons57c09c82016-10-21 21:13:56 +0000688 // Remember location of the last 'long'
689 TSWRange.setEnd(Loc);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000690 return false;
691}
692
Mike Stump11289f42009-09-09 15:08:12 +0000693bool DeclSpec::SetTypeSpecComplex(TSC C, SourceLocation Loc,
John McCall49bfce42009-08-03 20:12:06 +0000694 const char *&PrevSpec,
695 unsigned &DiagID) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000696 if (TypeSpecComplex != TSC_unspecified)
John McCall49bfce42009-08-03 20:12:06 +0000697 return BadSpecifier(C, (TSC)TypeSpecComplex, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000698 TypeSpecComplex = C;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000699 TSCLoc = Loc;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000700 return false;
701}
702
Mike Stump11289f42009-09-09 15:08:12 +0000703bool DeclSpec::SetTypeSpecSign(TSS S, SourceLocation Loc,
John McCall49bfce42009-08-03 20:12:06 +0000704 const char *&PrevSpec,
705 unsigned &DiagID) {
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000706 if (TypeSpecSign != TSS_unspecified)
John McCall49bfce42009-08-03 20:12:06 +0000707 return BadSpecifier(S, (TSS)TypeSpecSign, PrevSpec, DiagID);
Chris Lattnerdeb42f52006-08-04 05:26:52 +0000708 TypeSpecSign = S;
Chris Lattnerb20e8942006-11-28 05:30:29 +0000709 TSSLoc = Loc;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000710 return false;
711}
712
Chris Lattnerb20e8942006-11-28 05:30:29 +0000713bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
John McCall49bfce42009-08-03 20:12:06 +0000714 const char *&PrevSpec,
715 unsigned &DiagID,
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000716 ParsedType Rep,
717 const PrintingPolicy &Policy) {
718 return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep, Policy);
Abramo Bagnara9875a3c2011-03-16 20:16:18 +0000719}
720
721bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
722 SourceLocation TagNameLoc,
723 const char *&PrevSpec,
724 unsigned &DiagID,
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000725 ParsedType Rep,
726 const PrintingPolicy &Policy) {
John McCallba7bf592010-08-24 05:47:05 +0000727 assert(isTypeRep(T) && "T does not store a type");
728 assert(Rep && "no type provided!");
Richard Smithb3065792019-05-07 07:36:07 +0000729 if (TypeSpecType == TST_error)
730 return false;
John McCallba7bf592010-08-24 05:47:05 +0000731 if (TypeSpecType != TST_unspecified) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000732 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
John McCallba7bf592010-08-24 05:47:05 +0000733 DiagID = diag::err_invalid_decl_spec_combination;
734 return true;
735 }
736 TypeSpecType = T;
737 TypeRep = Rep;
Abramo Bagnara9875a3c2011-03-16 20:16:18 +0000738 TSTLoc = TagKwLoc;
739 TSTNameLoc = TagNameLoc;
John McCallba7bf592010-08-24 05:47:05 +0000740 TypeSpecOwned = false;
741 return false;
742}
743
744bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
745 const char *&PrevSpec,
746 unsigned &DiagID,
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000747 Expr *Rep,
748 const PrintingPolicy &Policy) {
John McCallba7bf592010-08-24 05:47:05 +0000749 assert(isExprRep(T) && "T does not store an expr");
750 assert(Rep && "no expression provided!");
Richard Smithb3065792019-05-07 07:36:07 +0000751 if (TypeSpecType == TST_error)
752 return false;
John McCallba7bf592010-08-24 05:47:05 +0000753 if (TypeSpecType != TST_unspecified) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000754 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
John McCallba7bf592010-08-24 05:47:05 +0000755 DiagID = diag::err_invalid_decl_spec_combination;
756 return true;
757 }
758 TypeSpecType = T;
759 ExprRep = Rep;
760 TSTLoc = Loc;
Abramo Bagnara9875a3c2011-03-16 20:16:18 +0000761 TSTNameLoc = Loc;
John McCallba7bf592010-08-24 05:47:05 +0000762 TypeSpecOwned = false;
763 return false;
764}
765
766bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
767 const char *&PrevSpec,
768 unsigned &DiagID,
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000769 Decl *Rep, bool Owned,
770 const PrintingPolicy &Policy) {
771 return SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, Rep, Owned, Policy);
Abramo Bagnara9875a3c2011-03-16 20:16:18 +0000772}
773
774bool DeclSpec::SetTypeSpecType(TST T, SourceLocation TagKwLoc,
775 SourceLocation TagNameLoc,
776 const char *&PrevSpec,
777 unsigned &DiagID,
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000778 Decl *Rep, bool Owned,
779 const PrintingPolicy &Policy) {
John McCallba7bf592010-08-24 05:47:05 +0000780 assert(isDeclRep(T) && "T does not store a decl");
781 // Unlike the other cases, we don't assert that we actually get a decl.
782
Richard Smithb3065792019-05-07 07:36:07 +0000783 if (TypeSpecType == TST_error)
784 return false;
John McCallba7bf592010-08-24 05:47:05 +0000785 if (TypeSpecType != TST_unspecified) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000786 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
John McCallba7bf592010-08-24 05:47:05 +0000787 DiagID = diag::err_invalid_decl_spec_combination;
788 return true;
789 }
790 TypeSpecType = T;
791 DeclRep = Rep;
Abramo Bagnara9875a3c2011-03-16 20:16:18 +0000792 TSTLoc = TagKwLoc;
793 TSTNameLoc = TagNameLoc;
Craig Topper4b566922014-06-09 02:04:02 +0000794 TypeSpecOwned = Owned && Rep != nullptr;
John McCallba7bf592010-08-24 05:47:05 +0000795 return false;
796}
797
Saar Razb481f022020-01-22 02:03:05 +0200798bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec,
799 unsigned &DiagID, TemplateIdAnnotation *Rep,
800 const PrintingPolicy &Policy) {
801 assert(T == TST_auto || T == TST_decltype_auto);
802 ConstrainedAuto = true;
803 TemplateIdRep = Rep;
804 return SetTypeSpecType(T, Loc, PrevSpec, DiagID, Policy);
805}
806
John McCallba7bf592010-08-24 05:47:05 +0000807bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
808 const char *&PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000809 unsigned &DiagID,
810 const PrintingPolicy &Policy) {
John McCallba7bf592010-08-24 05:47:05 +0000811 assert(!isDeclRep(T) && !isTypeRep(T) && !isExprRep(T) &&
812 "rep required for these type-spec kinds!");
Richard Smithb3065792019-05-07 07:36:07 +0000813 if (TypeSpecType == TST_error)
814 return false;
John McCall49bfce42009-08-03 20:12:06 +0000815 if (TypeSpecType != TST_unspecified) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000816 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
John McCall49bfce42009-08-03 20:12:06 +0000817 DiagID = diag::err_invalid_decl_spec_combination;
818 return true;
819 }
Abramo Bagnara9875a3c2011-03-16 20:16:18 +0000820 TSTLoc = Loc;
821 TSTNameLoc = Loc;
Chris Lattner37141f42010-06-23 06:00:24 +0000822 if (TypeAltiVecVector && (T == TST_bool) && !TypeAltiVecBool) {
823 TypeAltiVecBool = true;
Chris Lattner37141f42010-06-23 06:00:24 +0000824 return false;
825 }
Chris Lattnerdeb42f52006-08-04 05:26:52 +0000826 TypeSpecType = T;
John McCallba7bf592010-08-24 05:47:05 +0000827 TypeSpecOwned = false;
John Thompson22334602010-02-05 00:12:22 +0000828 return false;
829}
830
Leonard Chanab80f3c2018-06-14 14:53:51 +0000831bool DeclSpec::SetTypeSpecSat(SourceLocation Loc, const char *&PrevSpec,
832 unsigned &DiagID) {
833 // Cannot set twice
834 if (TypeSpecSat) {
835 DiagID = diag::warn_duplicate_declspec;
836 PrevSpec = "_Sat";
837 return true;
838 }
839 TypeSpecSat = true;
840 TSSatLoc = Loc;
841 return false;
842}
843
John Thompson22334602010-02-05 00:12:22 +0000844bool DeclSpec::SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000845 const char *&PrevSpec, unsigned &DiagID,
846 const PrintingPolicy &Policy) {
Richard Smithb3065792019-05-07 07:36:07 +0000847 if (TypeSpecType == TST_error)
848 return false;
John Thompson22334602010-02-05 00:12:22 +0000849 if (TypeSpecType != TST_unspecified) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000850 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
John Thompson22334602010-02-05 00:12:22 +0000851 DiagID = diag::err_invalid_vector_decl_spec_combination;
852 return true;
853 }
854 TypeAltiVecVector = isAltiVecVector;
855 AltiVecLoc = Loc;
856 return false;
857}
858
Xiuli Pan9c14e282016-01-09 12:53:17 +0000859bool DeclSpec::SetTypePipe(bool isPipe, SourceLocation Loc,
860 const char *&PrevSpec, unsigned &DiagID,
861 const PrintingPolicy &Policy) {
Richard Smithb3065792019-05-07 07:36:07 +0000862 if (TypeSpecType == TST_error)
863 return false;
Xiuli Pan9c14e282016-01-09 12:53:17 +0000864 if (TypeSpecType != TST_unspecified) {
865 PrevSpec = DeclSpec::getSpecifierName((TST)TypeSpecType, Policy);
866 DiagID = diag::err_invalid_decl_spec_combination;
867 return true;
868 }
869
Eugene Zelenko1e95bc02018-04-05 22:15:42 +0000870 if (isPipe) {
Xiuli Pan9c14e282016-01-09 12:53:17 +0000871 TypeSpecPipe = TSP_pipe;
Eugene Zelenko1e95bc02018-04-05 22:15:42 +0000872 }
Xiuli Pan9c14e282016-01-09 12:53:17 +0000873 return false;
874}
875
John Thompson22334602010-02-05 00:12:22 +0000876bool DeclSpec::SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000877 const char *&PrevSpec, unsigned &DiagID,
878 const PrintingPolicy &Policy) {
Richard Smithb3065792019-05-07 07:36:07 +0000879 if (TypeSpecType == TST_error)
880 return false;
Chris Lattner37141f42010-06-23 06:00:24 +0000881 if (!TypeAltiVecVector || TypeAltiVecPixel ||
882 (TypeSpecType != TST_unspecified)) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000883 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
John Thompson22334602010-02-05 00:12:22 +0000884 DiagID = diag::err_invalid_pixel_decl_spec_combination;
885 return true;
886 }
John Thompson22334602010-02-05 00:12:22 +0000887 TypeAltiVecPixel = isAltiVecPixel;
888 TSTLoc = Loc;
Abramo Bagnara9875a3c2011-03-16 20:16:18 +0000889 TSTNameLoc = Loc;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000890 return false;
891}
892
Bill Schmidt99a084b2013-07-03 20:54:09 +0000893bool DeclSpec::SetTypeAltiVecBool(bool isAltiVecBool, SourceLocation Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000894 const char *&PrevSpec, unsigned &DiagID,
895 const PrintingPolicy &Policy) {
Richard Smithb3065792019-05-07 07:36:07 +0000896 if (TypeSpecType == TST_error)
897 return false;
Bill Schmidt99a084b2013-07-03 20:54:09 +0000898 if (!TypeAltiVecVector || TypeAltiVecBool ||
899 (TypeSpecType != TST_unspecified)) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +0000900 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
Bill Schmidt99a084b2013-07-03 20:54:09 +0000901 DiagID = diag::err_invalid_vector_bool_decl_spec;
902 return true;
903 }
904 TypeAltiVecBool = isAltiVecBool;
905 TSTLoc = Loc;
906 TSTNameLoc = Loc;
907 return false;
908}
909
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000910bool DeclSpec::SetTypeSpecError() {
911 TypeSpecType = TST_error;
John McCalla3707cc2010-08-26 17:22:34 +0000912 TypeSpecOwned = false;
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000913 TSTLoc = SourceLocation();
Abramo Bagnara9875a3c2011-03-16 20:16:18 +0000914 TSTNameLoc = SourceLocation();
Douglas Gregorcd72ba92009-02-06 22:42:48 +0000915 return false;
916}
917
Erich Keane5f0903e2020-04-17 10:44:19 -0700918bool DeclSpec::SetExtIntType(SourceLocation KWLoc, Expr *BitsExpr,
919 const char *&PrevSpec, unsigned &DiagID,
920 const PrintingPolicy &Policy) {
921 assert(BitsExpr && "no expression provided!");
922 if (TypeSpecType == TST_error)
923 return false;
924
925 if (TypeSpecType != TST_unspecified) {
926 PrevSpec = DeclSpec::getSpecifierName((TST) TypeSpecType, Policy);
927 DiagID = diag::err_invalid_decl_spec_combination;
928 return true;
929 }
930
931 TypeSpecType = TST_extint;
932 ExprRep = BitsExpr;
933 TSTLoc = KWLoc;
934 TSTNameLoc = KWLoc;
935 TypeSpecOwned = false;
936 return false;
937}
938
Chris Lattner60809f52006-11-28 05:18:46 +0000939bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
Richard Smith87e79512012-10-17 23:31:46 +0000940 unsigned &DiagID, const LangOptions &Lang) {
Serge Pavlov750db652013-11-13 06:57:53 +0000941 // Duplicates are permitted in C99 onwards, but are not permitted in C89 or
942 // C++. However, since this is likely not what the user intended, we will
943 // always warn. We do not need to set the qualifier's location since we
944 // already have it.
Aaron Ballman3731b332012-08-28 20:55:40 +0000945 if (TypeQualifiers & T) {
Aaron Ballmand5a176d2012-08-29 12:35:14 +0000946 bool IsExtension = true;
Richard Smith87e79512012-10-17 23:31:46 +0000947 if (Lang.C99)
Aaron Ballmand5a176d2012-08-29 12:35:14 +0000948 IsExtension = false;
Aaron Ballman3731b332012-08-28 20:55:40 +0000949 return BadSpecifier(T, T, PrevSpec, DiagID, IsExtension);
950 }
Anastasia Stulovaa9bc4bd2019-01-09 11:25:09 +0000951
952 return SetTypeQual(T, Loc);
953}
954
955bool DeclSpec::SetTypeQual(TQ T, SourceLocation Loc) {
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000956 TypeQualifiers |= T;
Mike Stump11289f42009-09-09 15:08:12 +0000957
Chris Lattner60809f52006-11-28 05:18:46 +0000958 switch (T) {
Richard Smith8e1ac332013-03-28 01:55:44 +0000959 case TQ_unspecified: break;
960 case TQ_const: TQ_constLoc = Loc; return false;
961 case TQ_restrict: TQ_restrictLoc = Loc; return false;
962 case TQ_volatile: TQ_volatileLoc = Loc; return false;
Andrey Bokhanko45d41322016-05-11 18:38:21 +0000963 case TQ_unaligned: TQ_unalignedLoc = Loc; return false;
Andrey Bokhanko67a41862016-05-26 10:06:01 +0000964 case TQ_atomic: TQ_atomicLoc = Loc; return false;
Chris Lattner60809f52006-11-28 05:18:46 +0000965 }
Richard Smith8e1ac332013-03-28 01:55:44 +0000966
967 llvm_unreachable("Unknown type qualifier!");
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000968}
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000969
Serge Pavlov750db652013-11-13 06:57:53 +0000970bool DeclSpec::setFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
971 unsigned &DiagID) {
972 // 'inline inline' is ok. However, since this is likely not what the user
973 // intended, we will always warn, similar to duplicates of type qualifiers.
974 if (FS_inline_specified) {
975 DiagID = diag::warn_duplicate_declspec;
976 PrevSpec = "inline";
977 return true;
978 }
Chris Lattnera925dc62006-11-28 04:33:46 +0000979 FS_inline_specified = true;
Chris Lattner1b22eed2006-11-28 05:12:07 +0000980 FS_inlineLoc = Loc;
Chris Lattnera925dc62006-11-28 04:33:46 +0000981 return false;
982}
983
Serge Pavlov750db652013-11-13 06:57:53 +0000984bool DeclSpec::setFunctionSpecForceInline(SourceLocation Loc, const char *&PrevSpec,
985 unsigned &DiagID) {
986 if (FS_forceinline_specified) {
987 DiagID = diag::warn_duplicate_declspec;
988 PrevSpec = "__forceinline";
989 return true;
990 }
991 FS_forceinline_specified = true;
992 FS_forceinlineLoc = Loc;
993 return false;
994}
995
996bool DeclSpec::setFunctionSpecVirtual(SourceLocation Loc,
997 const char *&PrevSpec,
998 unsigned &DiagID) {
999 // 'virtual virtual' is ok, but warn as this is likely not what the user
1000 // intended.
1001 if (FS_virtual_specified) {
1002 DiagID = diag::warn_duplicate_declspec;
1003 PrevSpec = "virtual";
1004 return true;
1005 }
Douglas Gregor61956c42008-10-31 09:07:45 +00001006 FS_virtual_specified = true;
1007 FS_virtualLoc = Loc;
1008 return false;
1009}
1010
Serge Pavlov750db652013-11-13 06:57:53 +00001011bool DeclSpec::setFunctionSpecExplicit(SourceLocation Loc,
Richard Smith76b90272019-05-09 03:59:21 +00001012 const char *&PrevSpec, unsigned &DiagID,
1013 ExplicitSpecifier ExplicitSpec,
1014 SourceLocation CloseParenLoc) {
1015 assert((ExplicitSpec.getKind() == ExplicitSpecKind::ResolvedTrue ||
1016 ExplicitSpec.getExpr()) &&
1017 "invalid ExplicitSpecifier");
Serge Pavlov750db652013-11-13 06:57:53 +00001018 // 'explicit explicit' is ok, but warn as this is likely not what the user
1019 // intended.
Richard Smith76b90272019-05-09 03:59:21 +00001020 if (hasExplicitSpecifier()) {
1021 DiagID = (ExplicitSpec.getExpr() || FS_explicit_specifier.getExpr())
1022 ? diag::err_duplicate_declspec
1023 : diag::ext_warn_duplicate_declspec;
Serge Pavlov750db652013-11-13 06:57:53 +00001024 PrevSpec = "explicit";
1025 return true;
1026 }
Richard Smith76b90272019-05-09 03:59:21 +00001027 FS_explicit_specifier = ExplicitSpec;
Douglas Gregor61956c42008-10-31 09:07:45 +00001028 FS_explicitLoc = Loc;
Richard Smith76b90272019-05-09 03:59:21 +00001029 FS_explicitCloseParenLoc = CloseParenLoc;
Douglas Gregor61956c42008-10-31 09:07:45 +00001030 return false;
1031}
1032
Serge Pavlov750db652013-11-13 06:57:53 +00001033bool DeclSpec::setFunctionSpecNoreturn(SourceLocation Loc,
1034 const char *&PrevSpec,
1035 unsigned &DiagID) {
1036 // '_Noreturn _Noreturn' is ok, but warn as this is likely not what the user
1037 // intended.
1038 if (FS_noreturn_specified) {
1039 DiagID = diag::warn_duplicate_declspec;
1040 PrevSpec = "_Noreturn";
1041 return true;
1042 }
Richard Smith0015f092013-01-17 22:16:11 +00001043 FS_noreturn_specified = true;
1044 FS_noreturnLoc = Loc;
1045 return false;
1046}
1047
John McCall49bfce42009-08-03 20:12:06 +00001048bool DeclSpec::SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
1049 unsigned &DiagID) {
Anders Carlssoncd8db412009-05-06 04:46:28 +00001050 if (Friend_specified) {
1051 PrevSpec = "friend";
Richard Smithc0a5d5b2014-01-10 21:27:55 +00001052 // Keep the later location, so that we can later diagnose ill-formed
1053 // declarations like 'friend class X friend;'. Per [class.friend]p3,
1054 // 'friend' must be the first token in a friend declaration that is
1055 // not a function declaration.
1056 FriendLoc = Loc;
1057 DiagID = diag::warn_duplicate_declspec;
Anders Carlssoncd8db412009-05-06 04:46:28 +00001058 return true;
1059 }
John McCall49bfce42009-08-03 20:12:06 +00001060
Anders Carlssoncd8db412009-05-06 04:46:28 +00001061 Friend_specified = true;
1062 FriendLoc = Loc;
1063 return false;
1064}
Chris Lattnera925dc62006-11-28 04:33:46 +00001065
Douglas Gregor26701a42011-09-09 02:06:17 +00001066bool DeclSpec::setModulePrivateSpec(SourceLocation Loc, const char *&PrevSpec,
1067 unsigned &DiagID) {
1068 if (isModulePrivateSpecified()) {
1069 PrevSpec = "__module_private__";
Nick Desaulniers150ca532018-10-03 23:09:29 +00001070 DiagID = diag::ext_warn_duplicate_declspec;
Douglas Gregor26701a42011-09-09 02:06:17 +00001071 return true;
1072 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001073
Douglas Gregor26701a42011-09-09 02:06:17 +00001074 ModulePrivateLoc = Loc;
1075 return false;
1076}
1077
Gauthier Harnisch796ed032019-06-14 08:56:20 +00001078bool DeclSpec::SetConstexprSpec(ConstexprSpecKind ConstexprKind,
1079 SourceLocation Loc, const char *&PrevSpec,
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001080 unsigned &DiagID) {
Richard Smitha6e8b682019-09-04 20:30:37 +00001081 if (getConstexprSpecifier() != CSK_unspecified)
1082 return BadSpecifier(ConstexprKind, getConstexprSpecifier(), PrevSpec,
1083 DiagID);
Gauthier Harnisch796ed032019-06-14 08:56:20 +00001084 ConstexprSpecifier = ConstexprKind;
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001085 ConstexprLoc = Loc;
1086 return false;
1087}
1088
Douglas Gregorc9b7a592010-01-18 18:04:31 +00001089void DeclSpec::SaveWrittenBuiltinSpecs() {
Faisal Vali090da2d2018-01-01 18:23:28 +00001090 writtenBS.Sign = getTypeSpecSign();
1091 writtenBS.Width = getTypeSpecWidth();
1092 writtenBS.Type = getTypeSpecType();
Douglas Gregorc9b7a592010-01-18 18:04:31 +00001093 // Search the list of attributes for the presence of a mode attribute.
Erich Keanee891aa92018-07-13 15:07:47 +00001094 writtenBS.ModeAttr = getAttributes().hasAttribute(ParsedAttr::AT_Mode);
Douglas Gregorc9b7a592010-01-18 18:04:31 +00001095}
1096
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001097/// Finish - This does final analysis of the declspec, rejecting things like
1098/// "_Imaginary" (lacking an FP type). This returns a diagnostic to issue or
1099/// diag::NUM_DIAGNOSTICS if there is no error. After calling this method,
1100/// DeclSpec is guaranteed self-consistent, even if an error occurred.
Craig Topper25122412015-11-15 03:32:11 +00001101void DeclSpec::Finish(Sema &S, const PrintingPolicy &Policy) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +00001102 // Before possibly changing their values, save specs as written.
1103 SaveWrittenBuiltinSpecs();
1104
Richard Smithb3065792019-05-07 07:36:07 +00001105 // Check the type specifier components first. No checking for an invalid
1106 // type.
1107 if (TypeSpecType == TST_error)
1108 return;
Chris Lattnerfef9d2b2006-08-04 06:36:52 +00001109
Richard Smith74aeef52013-04-26 16:15:35 +00001110 // If decltype(auto) is used, no other type specifiers are permitted.
1111 if (TypeSpecType == TST_decltype_auto &&
1112 (TypeSpecWidth != TSW_unspecified ||
1113 TypeSpecComplex != TSC_unspecified ||
1114 TypeSpecSign != TSS_unspecified ||
1115 TypeAltiVecVector || TypeAltiVecPixel || TypeAltiVecBool ||
1116 TypeQualifiers)) {
Andrey Bokhanko45d41322016-05-11 18:38:21 +00001117 const unsigned NumLocs = 9;
Richard Smith74aeef52013-04-26 16:15:35 +00001118 SourceLocation ExtraLocs[NumLocs] = {
Malcolm Parsons57c09c82016-10-21 21:13:56 +00001119 TSWRange.getBegin(), TSCLoc, TSSLoc,
1120 AltiVecLoc, TQ_constLoc, TQ_restrictLoc,
1121 TQ_volatileLoc, TQ_atomicLoc, TQ_unalignedLoc};
Richard Smith74aeef52013-04-26 16:15:35 +00001122 FixItHint Hints[NumLocs];
1123 SourceLocation FirstLoc;
1124 for (unsigned I = 0; I != NumLocs; ++I) {
Yaron Kerened1fe5d2015-10-03 05:15:57 +00001125 if (ExtraLocs[I].isValid()) {
Richard Smith74aeef52013-04-26 16:15:35 +00001126 if (FirstLoc.isInvalid() ||
Craig Topper25122412015-11-15 03:32:11 +00001127 S.getSourceManager().isBeforeInTranslationUnit(ExtraLocs[I],
1128 FirstLoc))
Richard Smith74aeef52013-04-26 16:15:35 +00001129 FirstLoc = ExtraLocs[I];
1130 Hints[I] = FixItHint::CreateRemoval(ExtraLocs[I]);
1131 }
1132 }
1133 TypeSpecWidth = TSW_unspecified;
1134 TypeSpecComplex = TSC_unspecified;
1135 TypeSpecSign = TSS_unspecified;
1136 TypeAltiVecVector = TypeAltiVecPixel = TypeAltiVecBool = false;
1137 TypeQualifiers = 0;
Craig Topper25122412015-11-15 03:32:11 +00001138 S.Diag(TSTLoc, diag::err_decltype_auto_cannot_be_combined)
Richard Smith74aeef52013-04-26 16:15:35 +00001139 << Hints[0] << Hints[1] << Hints[2] << Hints[3]
1140 << Hints[4] << Hints[5] << Hints[6] << Hints[7];
1141 }
1142
Chris Lattner37141f42010-06-23 06:00:24 +00001143 // Validate and finalize AltiVec vector declspec.
1144 if (TypeAltiVecVector) {
1145 if (TypeAltiVecBool) {
1146 // Sign specifiers are not allowed with vector bool. (PIM 2.1)
1147 if (TypeSpecSign != TSS_unspecified) {
Craig Topper25122412015-11-15 03:32:11 +00001148 S.Diag(TSSLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner37141f42010-06-23 06:00:24 +00001149 << getSpecifierName((TSS)TypeSpecSign);
1150 }
1151
1152 // Only char/int are valid with vector bool. (PIM 2.1)
Duncan Sandsd3e231e2010-06-23 19:34:52 +00001153 if (((TypeSpecType != TST_unspecified) && (TypeSpecType != TST_char) &&
1154 (TypeSpecType != TST_int)) || TypeAltiVecPixel) {
Craig Topper25122412015-11-15 03:32:11 +00001155 S.Diag(TSTLoc, diag::err_invalid_vector_bool_decl_spec)
Chris Lattner37141f42010-06-23 06:00:24 +00001156 << (TypeAltiVecPixel ? "__pixel" :
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001157 getSpecifierName((TST)TypeSpecType, Policy));
Chris Lattner37141f42010-06-23 06:00:24 +00001158 }
1159
Bill Seurer2351bec2015-03-03 20:08:43 +00001160 // Only 'short' and 'long long' are valid with vector bool. (PIM 2.1)
1161 if ((TypeSpecWidth != TSW_unspecified) && (TypeSpecWidth != TSW_short) &&
1162 (TypeSpecWidth != TSW_longlong))
Malcolm Parsons57c09c82016-10-21 21:13:56 +00001163 S.Diag(TSWRange.getBegin(), diag::err_invalid_vector_bool_decl_spec)
1164 << getSpecifierName((TSW)TypeSpecWidth);
Chris Lattner37141f42010-06-23 06:00:24 +00001165
Ulrich Weigand3c5038a2015-07-30 14:08:36 +00001166 // vector bool long long requires VSX support or ZVector.
Kit Barton8553bec2015-03-11 15:57:19 +00001167 if ((TypeSpecWidth == TSW_longlong) &&
Craig Topper25122412015-11-15 03:32:11 +00001168 (!S.Context.getTargetInfo().hasFeature("vsx")) &&
1169 (!S.Context.getTargetInfo().hasFeature("power8-vector")) &&
1170 !S.getLangOpts().ZVector)
1171 S.Diag(TSTLoc, diag::err_invalid_vector_long_long_decl_spec);
Bill Seurer2351bec2015-03-03 20:08:43 +00001172
Chris Lattner37141f42010-06-23 06:00:24 +00001173 // Elements of vector bool are interpreted as unsigned. (PIM 2.1)
1174 if ((TypeSpecType == TST_char) || (TypeSpecType == TST_int) ||
1175 (TypeSpecWidth != TSW_unspecified))
1176 TypeSpecSign = TSS_unsigned;
Bill Schmidt691e01d2014-10-31 19:19:24 +00001177 } else if (TypeSpecType == TST_double) {
1178 // vector long double and vector long long double are never allowed.
Ulrich Weigand3c5038a2015-07-30 14:08:36 +00001179 // vector double is OK for Power7 and later, and ZVector.
Bill Schmidt691e01d2014-10-31 19:19:24 +00001180 if (TypeSpecWidth == TSW_long || TypeSpecWidth == TSW_longlong)
Malcolm Parsons57c09c82016-10-21 21:13:56 +00001181 S.Diag(TSWRange.getBegin(),
1182 diag::err_invalid_vector_long_double_decl_spec);
Craig Topper25122412015-11-15 03:32:11 +00001183 else if (!S.Context.getTargetInfo().hasFeature("vsx") &&
1184 !S.getLangOpts().ZVector)
1185 S.Diag(TSTLoc, diag::err_invalid_vector_double_decl_spec);
Ulrich Weigand3c5038a2015-07-30 14:08:36 +00001186 } else if (TypeSpecType == TST_float) {
Ulrich Weigand76976a72017-07-17 17:46:47 +00001187 // vector float is unsupported for ZVector unless we have the
1188 // vector-enhancements facility 1 (ISA revision 12).
1189 if (S.getLangOpts().ZVector &&
1190 !S.Context.getTargetInfo().hasFeature("arch12"))
Craig Topper25122412015-11-15 03:32:11 +00001191 S.Diag(TSTLoc, diag::err_invalid_vector_float_decl_spec);
Hal Finkel3a1f4c72014-08-24 04:50:19 +00001192 } else if (TypeSpecWidth == TSW_long) {
Ulrich Weigand3c5038a2015-07-30 14:08:36 +00001193 // vector long is unsupported for ZVector and deprecated for AltiVec.
Craig Topper25122412015-11-15 03:32:11 +00001194 if (S.getLangOpts().ZVector)
Malcolm Parsons57c09c82016-10-21 21:13:56 +00001195 S.Diag(TSWRange.getBegin(), diag::err_invalid_vector_long_decl_spec);
Ulrich Weigand3c5038a2015-07-30 14:08:36 +00001196 else
Malcolm Parsons57c09c82016-10-21 21:13:56 +00001197 S.Diag(TSWRange.getBegin(),
1198 diag::warn_vector_long_decl_spec_combination)
1199 << getSpecifierName((TST)TypeSpecType, Policy);
Chris Lattner37141f42010-06-23 06:00:24 +00001200 }
1201
1202 if (TypeAltiVecPixel) {
1203 //TODO: perform validation
1204 TypeSpecType = TST_int;
1205 TypeSpecSign = TSS_unsigned;
1206 TypeSpecWidth = TSW_short;
John McCalla3707cc2010-08-26 17:22:34 +00001207 TypeSpecOwned = false;
Chris Lattner37141f42010-06-23 06:00:24 +00001208 }
1209 }
1210
Leonard Chanab80f3c2018-06-14 14:53:51 +00001211 bool IsFixedPointType =
1212 TypeSpecType == TST_accum || TypeSpecType == TST_fract;
1213
Leonard Chanf921d852018-06-04 16:07:52 +00001214 // signed/unsigned are only valid with int/char/wchar_t/_Accum.
Chris Lattner839713c2006-08-04 06:15:52 +00001215 if (TypeSpecSign != TSS_unspecified) {
Chris Lattnerfef9d2b2006-08-04 06:36:52 +00001216 if (TypeSpecType == TST_unspecified)
1217 TypeSpecType = TST_int; // unsigned -> unsigned int, signed -> signed int.
Leonard Chanf921d852018-06-04 16:07:52 +00001218 else if (TypeSpecType != TST_int && TypeSpecType != TST_int128 &&
1219 TypeSpecType != TST_char && TypeSpecType != TST_wchar &&
Erich Keane5f0903e2020-04-17 10:44:19 -07001220 !IsFixedPointType && TypeSpecType != TST_extint) {
Craig Topper25122412015-11-15 03:32:11 +00001221 S.Diag(TSSLoc, diag::err_invalid_sign_spec)
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001222 << getSpecifierName((TST)TypeSpecType, Policy);
Chris Lattnerfef9d2b2006-08-04 06:36:52 +00001223 // signed double -> double.
Chris Lattner839713c2006-08-04 06:15:52 +00001224 TypeSpecSign = TSS_unspecified;
1225 }
1226 }
Chris Lattnerfef9d2b2006-08-04 06:36:52 +00001227
Chris Lattner839713c2006-08-04 06:15:52 +00001228 // Validate the width of the type.
Faisal Vali090da2d2018-01-01 18:23:28 +00001229 switch (TypeSpecWidth) {
Chris Lattner839713c2006-08-04 06:15:52 +00001230 case TSW_unspecified: break;
1231 case TSW_short: // short int
1232 case TSW_longlong: // long long int
Chris Lattnerfef9d2b2006-08-04 06:36:52 +00001233 if (TypeSpecType == TST_unspecified)
1234 TypeSpecType = TST_int; // short -> short int, long long -> long long int.
Leonard Chanf921d852018-06-04 16:07:52 +00001235 else if (!(TypeSpecType == TST_int ||
Leonard Chanab80f3c2018-06-14 14:53:51 +00001236 (IsFixedPointType && TypeSpecWidth != TSW_longlong))) {
Malcolm Parsons57c09c82016-10-21 21:13:56 +00001237 S.Diag(TSWRange.getBegin(), diag::err_invalid_width_spec)
1238 << (int)TypeSpecWidth << getSpecifierName((TST)TypeSpecType, Policy);
Chris Lattner839713c2006-08-04 06:15:52 +00001239 TypeSpecType = TST_int;
Leonard Chanab80f3c2018-06-14 14:53:51 +00001240 TypeSpecSat = false;
John McCalla3707cc2010-08-26 17:22:34 +00001241 TypeSpecOwned = false;
Chris Lattner839713c2006-08-04 06:15:52 +00001242 }
1243 break;
1244 case TSW_long: // long double, long int
Chris Lattnerfef9d2b2006-08-04 06:36:52 +00001245 if (TypeSpecType == TST_unspecified)
1246 TypeSpecType = TST_int; // long -> long int.
Leonard Chanf921d852018-06-04 16:07:52 +00001247 else if (TypeSpecType != TST_int && TypeSpecType != TST_double &&
Leonard Chanab80f3c2018-06-14 14:53:51 +00001248 !IsFixedPointType) {
Malcolm Parsons57c09c82016-10-21 21:13:56 +00001249 S.Diag(TSWRange.getBegin(), diag::err_invalid_width_spec)
1250 << (int)TypeSpecWidth << getSpecifierName((TST)TypeSpecType, Policy);
Chris Lattner839713c2006-08-04 06:15:52 +00001251 TypeSpecType = TST_int;
Leonard Chanab80f3c2018-06-14 14:53:51 +00001252 TypeSpecSat = false;
John McCalla3707cc2010-08-26 17:22:34 +00001253 TypeSpecOwned = false;
Chris Lattner839713c2006-08-04 06:15:52 +00001254 }
Chris Lattnerfef9d2b2006-08-04 06:36:52 +00001255 break;
Chris Lattner839713c2006-08-04 06:15:52 +00001256 }
Mike Stump11289f42009-09-09 15:08:12 +00001257
Chris Lattner0e894622006-08-13 19:58:17 +00001258 // TODO: if the implementation does not implement _Complex or _Imaginary,
Chris Lattnerfef9d2b2006-08-04 06:36:52 +00001259 // disallow their use. Need information about the backend.
1260 if (TypeSpecComplex != TSC_unspecified) {
1261 if (TypeSpecType == TST_unspecified) {
Craig Topper25122412015-11-15 03:32:11 +00001262 S.Diag(TSCLoc, diag::ext_plain_complex)
Douglas Gregora771f462010-03-31 17:46:05 +00001263 << FixItHint::CreateInsertion(
Craig Topper25122412015-11-15 03:32:11 +00001264 S.getLocForEndOfToken(getTypeSpecComplexLoc()),
Douglas Gregore3e01a22009-04-01 22:41:11 +00001265 " double");
Chris Lattnerfef9d2b2006-08-04 06:36:52 +00001266 TypeSpecType = TST_double; // _Complex -> _Complex double.
Erich Keane5f0903e2020-04-17 10:44:19 -07001267 } else if (TypeSpecType == TST_int || TypeSpecType == TST_char ||
1268 TypeSpecType == TST_extint) {
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001269 // Note that this intentionally doesn't include _Complex _Bool.
Craig Topper25122412015-11-15 03:32:11 +00001270 if (!S.getLangOpts().CPlusPlus)
1271 S.Diag(TSTLoc, diag::ext_integer_complex);
Chris Lattnerfef9d2b2006-08-04 06:36:52 +00001272 } else if (TypeSpecType != TST_float && TypeSpecType != TST_double) {
Craig Topper25122412015-11-15 03:32:11 +00001273 S.Diag(TSCLoc, diag::err_invalid_complex_spec)
Erik Verbruggen888d52a2014-01-15 09:15:43 +00001274 << getSpecifierName((TST)TypeSpecType, Policy);
Chris Lattnerfef9d2b2006-08-04 06:36:52 +00001275 TypeSpecComplex = TSC_unspecified;
1276 }
1277 }
Chris Lattner8e90ef62006-08-05 03:30:45 +00001278
Richard Smithb4a9e862013-04-12 22:46:28 +00001279 // C11 6.7.1/3, C++11 [dcl.stc]p1, GNU TLS: __thread, thread_local and
1280 // _Thread_local can only appear with the 'static' and 'extern' storage class
1281 // specifiers. We also allow __private_extern__ as an extension.
1282 if (ThreadStorageClassSpec != TSCS_unspecified) {
1283 switch (StorageClassSpec) {
1284 case SCS_unspecified:
1285 case SCS_extern:
1286 case SCS_private_extern:
1287 case SCS_static:
1288 break;
1289 default:
Craig Topper25122412015-11-15 03:32:11 +00001290 if (S.getSourceManager().isBeforeInTranslationUnit(
Richard Smithb4a9e862013-04-12 22:46:28 +00001291 getThreadStorageClassSpecLoc(), getStorageClassSpecLoc()))
Craig Topper25122412015-11-15 03:32:11 +00001292 S.Diag(getStorageClassSpecLoc(),
Richard Smithb4a9e862013-04-12 22:46:28 +00001293 diag::err_invalid_decl_spec_combination)
1294 << DeclSpec::getSpecifierName(getThreadStorageClassSpec())
1295 << SourceRange(getThreadStorageClassSpecLoc());
1296 else
Craig Topper25122412015-11-15 03:32:11 +00001297 S.Diag(getThreadStorageClassSpecLoc(),
Richard Smithb4a9e862013-04-12 22:46:28 +00001298 diag::err_invalid_decl_spec_combination)
1299 << DeclSpec::getSpecifierName(getStorageClassSpec())
1300 << SourceRange(getStorageClassSpecLoc());
1301 // Discard the thread storage class specifier to recover.
1302 ThreadStorageClassSpec = TSCS_unspecified;
1303 ThreadStorageClassSpecLoc = SourceLocation();
1304 }
1305 }
1306
Richard Smith58c74332011-09-04 19:54:14 +00001307 // If no type specifier was provided and we're parsing a language where
1308 // the type specifier is not optional, but we got 'auto' as a storage
1309 // class specifier, then assume this is an attempt to use C++0x's 'auto'
1310 // type specifier.
Craig Topper25122412015-11-15 03:32:11 +00001311 if (S.getLangOpts().CPlusPlus &&
Richard Smith58c74332011-09-04 19:54:14 +00001312 TypeSpecType == TST_unspecified && StorageClassSpec == SCS_auto) {
1313 TypeSpecType = TST_auto;
Rafael Espindola6ae7e502013-04-03 19:27:57 +00001314 StorageClassSpec = SCS_unspecified;
Richard Smith58c74332011-09-04 19:54:14 +00001315 TSTLoc = TSTNameLoc = StorageClassSpecLoc;
1316 StorageClassSpecLoc = SourceLocation();
1317 }
1318 // Diagnose if we've recovered from an ill-formed 'auto' storage class
Richard Smith74aeef52013-04-26 16:15:35 +00001319 // specifier in a pre-C++11 dialect of C++.
Craig Topper25122412015-11-15 03:32:11 +00001320 if (!S.getLangOpts().CPlusPlus11 && TypeSpecType == TST_auto)
1321 S.Diag(TSTLoc, diag::ext_auto_type_specifier);
1322 if (S.getLangOpts().CPlusPlus && !S.getLangOpts().CPlusPlus11 &&
Richard Smith58c74332011-09-04 19:54:14 +00001323 StorageClassSpec == SCS_auto)
Craig Topper25122412015-11-15 03:32:11 +00001324 S.Diag(StorageClassSpecLoc, diag::warn_auto_storage_class)
Richard Smith58c74332011-09-04 19:54:14 +00001325 << FixItHint::CreateRemoval(StorageClassSpecLoc);
Richard Smith3a8244d2018-05-01 05:02:45 +00001326 if (TypeSpecType == TST_char8)
1327 S.Diag(TSTLoc, diag::warn_cxx17_compat_unicode_type);
1328 else if (TypeSpecType == TST_char16 || TypeSpecType == TST_char32)
Craig Topper25122412015-11-15 03:32:11 +00001329 S.Diag(TSTLoc, diag::warn_cxx98_compat_unicode_type)
Richard Smithb15c11c2011-10-17 23:06:20 +00001330 << (TypeSpecType == TST_char16 ? "char16_t" : "char32_t");
Gauthier Harnisch796ed032019-06-14 08:56:20 +00001331 if (getConstexprSpecifier() == CSK_constexpr)
Craig Topper25122412015-11-15 03:32:11 +00001332 S.Diag(ConstexprLoc, diag::warn_cxx98_compat_constexpr);
Richard Smitha6e8b682019-09-04 20:30:37 +00001333 else if (getConstexprSpecifier() == CSK_consteval)
Gauthier Harnisch796ed032019-06-14 08:56:20 +00001334 S.Diag(ConstexprLoc, diag::warn_cxx20_compat_consteval);
Richard Smitha6e8b682019-09-04 20:30:37 +00001335 else if (getConstexprSpecifier() == CSK_constinit)
1336 S.Diag(ConstexprLoc, diag::warn_cxx20_compat_constinit);
John McCall07e91c02009-08-06 02:15:43 +00001337 // C++ [class.friend]p6:
1338 // No storage-class-specifier shall appear in the decl-specifier-seq
1339 // of a friend declaration.
Richard Smithb4a9e862013-04-12 22:46:28 +00001340 if (isFriendSpecified() &&
1341 (getStorageClassSpec() || getThreadStorageClassSpec())) {
1342 SmallString<32> SpecName;
1343 SourceLocation SCLoc;
1344 FixItHint StorageHint, ThreadHint;
John McCall07e91c02009-08-06 02:15:43 +00001345
Richard Smithb4a9e862013-04-12 22:46:28 +00001346 if (DeclSpec::SCS SC = getStorageClassSpec()) {
1347 SpecName = getSpecifierName(SC);
1348 SCLoc = getStorageClassSpecLoc();
1349 StorageHint = FixItHint::CreateRemoval(SCLoc);
1350 }
1351
1352 if (DeclSpec::TSCS TSC = getThreadStorageClassSpec()) {
1353 if (!SpecName.empty()) SpecName += " ";
1354 SpecName += getSpecifierName(TSC);
1355 SCLoc = getThreadStorageClassSpecLoc();
1356 ThreadHint = FixItHint::CreateRemoval(SCLoc);
1357 }
John McCall07e91c02009-08-06 02:15:43 +00001358
Craig Topper25122412015-11-15 03:32:11 +00001359 S.Diag(SCLoc, diag::err_friend_decl_spec)
Richard Smithb4a9e862013-04-12 22:46:28 +00001360 << SpecName << StorageHint << ThreadHint;
John McCall07e91c02009-08-06 02:15:43 +00001361
1362 ClearStorageClassSpecs();
1363 }
1364
David Majnemerdeca7052014-04-30 18:24:01 +00001365 // C++11 [dcl.fct.spec]p5:
1366 // The virtual specifier shall be used only in the initial
1367 // declaration of a non-static class member function;
1368 // C++11 [dcl.fct.spec]p6:
1369 // The explicit specifier shall be used only in the declaration of
1370 // a constructor or conversion function within its class
1371 // definition;
Richard Smith76b90272019-05-09 03:59:21 +00001372 if (isFriendSpecified() && (isVirtualSpecified() || hasExplicitSpecifier())) {
David Majnemerdeca7052014-04-30 18:24:01 +00001373 StringRef Keyword;
Richard Smith76b90272019-05-09 03:59:21 +00001374 FixItHint Hint;
David Majnemerdeca7052014-04-30 18:24:01 +00001375 SourceLocation SCLoc;
1376
1377 if (isVirtualSpecified()) {
1378 Keyword = "virtual";
1379 SCLoc = getVirtualSpecLoc();
Richard Smith76b90272019-05-09 03:59:21 +00001380 Hint = FixItHint::CreateRemoval(SCLoc);
David Majnemerdeca7052014-04-30 18:24:01 +00001381 } else {
1382 Keyword = "explicit";
1383 SCLoc = getExplicitSpecLoc();
Richard Smith76b90272019-05-09 03:59:21 +00001384 Hint = FixItHint::CreateRemoval(getExplicitSpecRange());
David Majnemerdeca7052014-04-30 18:24:01 +00001385 }
1386
Craig Topper25122412015-11-15 03:32:11 +00001387 S.Diag(SCLoc, diag::err_friend_decl_spec)
David Majnemerdeca7052014-04-30 18:24:01 +00001388 << Keyword << Hint;
1389
Richard Smith76b90272019-05-09 03:59:21 +00001390 FS_virtual_specified = false;
1391 FS_explicit_specifier = ExplicitSpecifier();
David Majnemerdeca7052014-04-30 18:24:01 +00001392 FS_virtualLoc = FS_explicitLoc = SourceLocation();
1393 }
1394
Douglas Gregor64226422011-09-09 21:14:29 +00001395 assert(!TypeSpecOwned || isDeclRep((TST) TypeSpecType));
David Majnemerdeca7052014-04-30 18:24:01 +00001396
Chris Lattner8e90ef62006-08-05 03:30:45 +00001397 // Okay, now we can infer the real type.
Mike Stump11289f42009-09-09 15:08:12 +00001398
Chris Lattner0e894622006-08-13 19:58:17 +00001399 // TODO: return "auto function" and other bad things based on the real type.
Mike Stump11289f42009-09-09 15:08:12 +00001400
Chris Lattner1ae23292006-08-04 06:42:31 +00001401 // 'data definition has no type or storage class'?
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001402}
Daniel Dunbarc74b5cc2008-08-11 03:45:03 +00001403
Sebastian Redla2b5e312008-12-28 15:28:59 +00001404bool DeclSpec::isMissingDeclaratorOk() {
1405 TST tst = getTypeSpecType();
Craig Topper4b566922014-06-09 02:04:02 +00001406 return isDeclRep(tst) && getRepAsDecl() != nullptr &&
John McCallba7bf592010-08-24 05:47:05 +00001407 StorageClassSpec != DeclSpec::SCS_typedef;
Sebastian Redla2b5e312008-12-28 15:28:59 +00001408}
Douglas Gregor7861a802009-11-03 01:35:08 +00001409
Fangrui Song6907ce22018-07-30 19:24:48 +00001410void UnqualifiedId::setOperatorFunctionId(SourceLocation OperatorLoc,
Douglas Gregor7861a802009-11-03 01:35:08 +00001411 OverloadedOperatorKind Op,
1412 SourceLocation SymbolLocations[3]) {
Faisal Vali2ab8c152017-12-30 04:15:27 +00001413 Kind = UnqualifiedIdKind::IK_OperatorFunctionId;
Douglas Gregor7861a802009-11-03 01:35:08 +00001414 StartLocation = OperatorLoc;
1415 EndLocation = OperatorLoc;
1416 OperatorFunctionId.Operator = Op;
1417 for (unsigned I = 0; I != 3; ++I) {
1418 OperatorFunctionId.SymbolLocations[I] = SymbolLocations[I].getRawEncoding();
Fangrui Song6907ce22018-07-30 19:24:48 +00001419
Douglas Gregor7861a802009-11-03 01:35:08 +00001420 if (SymbolLocations[I].isValid())
1421 EndLocation = SymbolLocations[I];
1422 }
1423}
Anders Carlsson56104902011-01-17 03:05:47 +00001424
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001425bool VirtSpecifiers::SetSpecifier(Specifier VS, SourceLocation Loc,
Anders Carlssonf2ca3892011-01-22 15:58:16 +00001426 const char *&PrevSpec) {
Ehsan Akhgari93ed5cf2015-03-25 00:53:27 +00001427 if (!FirstLocation.isValid())
1428 FirstLocation = Loc;
Douglas Gregorf2f08062011-03-08 17:10:18 +00001429 LastLocation = Loc;
Ehsan Akhgari93ed5cf2015-03-25 00:53:27 +00001430 LastSpecifier = VS;
Fangrui Song6907ce22018-07-30 19:24:48 +00001431
Anders Carlsson56104902011-01-17 03:05:47 +00001432 if (Specifiers & VS) {
1433 PrevSpec = getSpecifierName(VS);
1434 return true;
1435 }
1436
1437 Specifiers |= VS;
1438
1439 switch (VS) {
David Blaikie83d382b2011-09-23 05:06:16 +00001440 default: llvm_unreachable("Unknown specifier!");
Anders Carlsson56104902011-01-17 03:05:47 +00001441 case VS_Override: VS_overrideLoc = Loc; break;
Andrey Bokhanko276055b2016-07-29 10:42:48 +00001442 case VS_GNU_Final:
David Majnemera5433082013-10-18 00:33:31 +00001443 case VS_Sealed:
Anders Carlsson56104902011-01-17 03:05:47 +00001444 case VS_Final: VS_finalLoc = Loc; break;
Anders Carlsson56104902011-01-17 03:05:47 +00001445 }
Anders Carlssonf2ca3892011-01-22 15:58:16 +00001446
Anders Carlsson56104902011-01-17 03:05:47 +00001447 return false;
1448}
1449
Anders Carlsson4b63d0e2011-01-22 16:56:46 +00001450const char *VirtSpecifiers::getSpecifierName(Specifier VS) {
Anders Carlssona6d35012011-01-22 15:11:37 +00001451 switch (VS) {
David Blaikie83d382b2011-09-23 05:06:16 +00001452 default: llvm_unreachable("Unknown specifier");
Anders Carlssona6d35012011-01-22 15:11:37 +00001453 case VS_Override: return "override";
1454 case VS_Final: return "final";
Andrey Bokhanko276055b2016-07-29 10:42:48 +00001455 case VS_GNU_Final: return "__final";
David Majnemera5433082013-10-18 00:33:31 +00001456 case VS_Sealed: return "sealed";
Anders Carlssona6d35012011-01-22 15:11:37 +00001457 }
1458}