#ifndef LLVM_CLANG_LIB_FORMAT_FORMATTOKEN_H
#define LLVM_CLANG_LIB_FORMAT_FORMATTOKEN_H
#include "clang/Basic/IdentifierTable.h"
#include "clang/Basic/OperatorPrecedence.h"
#include "clang/Format/Format.h"
#include "clang/Lex/Lexer.h"
#include <memory>
namespace clang {
namespace format {
#define LIST_TOKEN_TYPES \
TYPE(ArrayInitializerLSquare) \
TYPE(ArraySubscriptLSquare) \
TYPE(AttributeParen) \
TYPE(BinaryOperator) \
TYPE(BitFieldColon) \
TYPE(BlockComment) \
TYPE(CastRParen) \
TYPE(ConditionalExpr) \
TYPE(ConflictAlternative) \
TYPE(ConflictEnd) \
TYPE(ConflictStart) \
TYPE(CtorInitializerColon) \
TYPE(CtorInitializerComma) \
TYPE(DesignatedInitializerPeriod) \
TYPE(DictLiteral) \
TYPE(ForEachMacro) \
TYPE(FunctionAnnotationRParen) \
TYPE(FunctionDeclarationName) \
TYPE(FunctionLBrace) \
TYPE(FunctionTypeLParen) \
TYPE(ImplicitStringLiteral) \
TYPE(InheritanceColon) \
TYPE(InlineASMBrace) \
TYPE(InlineASMColon) \
TYPE(JavaAnnotation) \
TYPE(JsComputedPropertyName) \
TYPE(JsFatArrow) \
TYPE(JsTypeColon) \
TYPE(JsTypeOptionalQuestion) \
TYPE(LambdaArrow) \
TYPE(LambdaLSquare) \
TYPE(LeadingJavaAnnotation) \
TYPE(LineComment) \
TYPE(MacroBlockBegin) \
TYPE(MacroBlockEnd) \
TYPE(ObjCBlockLBrace) \
TYPE(ObjCBlockLParen) \
TYPE(ObjCDecl) \
TYPE(ObjCForIn) \
TYPE(ObjCMethodExpr) \
TYPE(ObjCMethodSpecifier) \
TYPE(ObjCProperty) \
TYPE(ObjCStringLiteral) \
TYPE(OverloadedOperator) \
TYPE(OverloadedOperatorLParen) \
TYPE(PointerOrReference) \
TYPE(PureVirtualSpecifier) \
TYPE(RangeBasedForLoopColon) \
TYPE(RegexLiteral) \
TYPE(SelectorName) \
TYPE(StartOfName) \
TYPE(TemplateCloser) \
TYPE(TemplateOpener) \
TYPE(TemplateString) \
TYPE(TrailingAnnotation) \
TYPE(TrailingReturnArrow) \
TYPE(TrailingUnaryOperator) \
TYPE(UnaryOperator) \
TYPE(Unknown)
enum TokenType {
#define TYPE(X) TT_##X,
LIST_TOKEN_TYPES
#undef TYPE
NUM_TOKEN_TYPES
};
const char *getTokenTypeName(TokenType Type);
enum BraceBlockKind { BK_Unknown, BK_Block, BK_BracedInit };
enum ParameterPackingKind { PPK_BinPacked, PPK_OnePerLine, PPK_Inconclusive };
enum FormatDecision { FD_Unformatted, FD_Continue, FD_Break };
class TokenRole;
class AnnotatedLine;
struct FormatToken {
FormatToken() {}
Token Tok;
unsigned NewlinesBefore = 0;
bool HasUnescapedNewline = false;
SourceRange WhitespaceRange;
unsigned LastNewlineOffset = 0;
unsigned ColumnWidth = 0;
unsigned LastLineColumnWidth = 0;
bool IsMultiline = false;
bool IsFirst = false;
bool MustBreakBefore = false;
StringRef TokenText;
bool IsUnterminatedLiteral = 0;
BraceBlockKind BlockKind = BK_Unknown;
TokenType Type = TT_Unknown;
unsigned SpacesRequiredBefore = 0;
bool CanBreakBefore = false;
bool ClosesTemplateDeclaration = false;
unsigned ParameterCount = 0;
unsigned BlockParameterCount = 0;
tok::TokenKind ParentBracket = tok::unknown;
std::unique_ptr<TokenRole> Role;
ParameterPackingKind PackingKind = PPK_Inconclusive;
unsigned TotalLength = 0;
unsigned OriginalColumn = 0;
unsigned UnbreakableTailLength = 0;
unsigned BindingStrength = 0;
unsigned NestingLevel = 0;
unsigned SplitPenalty = 0;
unsigned LongestObjCSelectorName = 0;
SmallVector<prec::Level, 4> FakeLParens;
unsigned FakeRParens = 0;
bool StartsBinaryExpression = false;
bool EndsBinaryExpression = false;
unsigned OperatorIndex = 0;
FormatToken *NextOperator = nullptr;
bool PartOfMultiVariableDeclStmt = false;
FormatToken *MatchingParen = nullptr;
FormatToken *Previous = nullptr;
FormatToken *Next = nullptr;
SmallVector<AnnotatedLine *, 1> Children;
FormatDecision Decision = FD_Unformatted;
bool Finalized = false;
bool is(tok::TokenKind Kind) const { return Tok.is(Kind); }
bool is(TokenType TT) const { return Type == TT; }
bool is(const IdentifierInfo *II) const {
return II && II == Tok.getIdentifierInfo();
}
bool is(tok::PPKeywordKind Kind) const {
return Tok.getIdentifierInfo() &&
Tok.getIdentifierInfo()->getPPKeywordID() == Kind;
}
template <typename A, typename B> bool isOneOf(A K1, B K2) const {
return is(K1) || is(K2);
}
template <typename A, typename B, typename... Ts>
bool isOneOf(A K1, B K2, Ts... Ks) const {
return is(K1) || isOneOf(K2, Ks...);
}
template <typename T> bool isNot(T Kind) const { return !is(Kind); }
bool isStringLiteral() const { return tok::isStringLiteral(Tok.getKind()); }
bool isObjCAtKeyword(tok::ObjCKeywordKind Kind) const {
return Tok.isObjCAtKeyword(Kind);
}
bool isAccessSpecifier(bool ColonRequired = true) const {
return isOneOf(tok::kw_public, tok::kw_protected, tok::kw_private) &&
(!ColonRequired || (Next && Next->is(tok::colon)));
}
bool isSimpleTypeSpecifier() const;
bool isObjCAccessSpecifier() const {
return is(tok::at) && Next && (Next->isObjCAtKeyword(tok::objc_public) ||
Next->isObjCAtKeyword(tok::objc_protected) ||
Next->isObjCAtKeyword(tok::objc_package) ||
Next->isObjCAtKeyword(tok::objc_private));
}
bool opensScope() const {
return isOneOf(tok::l_paren, tok::l_brace, tok::l_square,
TT_TemplateOpener);
}
bool closesScope() const {
return isOneOf(tok::r_paren, tok::r_brace, tok::r_square,
TT_TemplateCloser);
}
bool isMemberAccess() const {
return isOneOf(tok::arrow, tok::period, tok::arrowstar) &&
!isOneOf(TT_DesignatedInitializerPeriod, TT_TrailingReturnArrow,
TT_LambdaArrow);
}
bool isUnaryOperator() const {
switch (Tok.getKind()) {
case tok::plus:
case tok::plusplus:
case tok::minus:
case tok::minusminus:
case tok::exclaim:
case tok::tilde:
case tok::kw_sizeof:
case tok::kw_alignof:
return true;
default:
return false;
}
}
bool isBinaryOperator() const {
return getPrecedence() > prec::Comma;
}
bool isTrailingComment() const {
return is(tok::comment) &&
(is(TT_LineComment) || !Next || Next->NewlinesBefore > 0);
}
bool isFunctionLikeKeyword() const {
switch (Tok.getKind()) {
case tok::kw_throw:
case tok::kw_typeid:
case tok::kw_return:
case tok::kw_sizeof:
case tok::kw_alignof:
case tok::kw_alignas:
case tok::kw_decltype:
case tok::kw_noexcept:
case tok::kw_static_assert:
case tok::kw___attribute:
return true;
default:
return false;
}
}
SourceLocation getStartOfNonWhitespace() const {
return WhitespaceRange.getEnd();
}
prec::Level getPrecedence() const {
return getBinOpPrecedence(Tok.getKind(), true, true);
}
FormatToken *getPreviousNonComment() const {
FormatToken *Tok = Previous;
while (Tok && Tok->is(tok::comment))
Tok = Tok->Previous;
return Tok;
}
const FormatToken *getNextNonComment() const {
const FormatToken *Tok = Next;
while (Tok && Tok->is(tok::comment))
Tok = Tok->Next;
return Tok;
}
bool opensBlockOrBlockTypeList(const FormatStyle &Style) const {
return is(TT_ArrayInitializerLSquare) ||
(is(tok::l_brace) &&
(BlockKind == BK_Block || is(TT_DictLiteral) ||
(!Style.Cpp11BracedListStyle && NestingLevel == 0)));
}
bool closesBlockOrBlockTypeList(const FormatStyle &Style) const {
return MatchingParen && MatchingParen->opensBlockOrBlockTypeList(Style);
}
private:
FormatToken(const FormatToken &) = delete;
void operator=(const FormatToken &) = delete;
};
class ContinuationIndenter;
struct LineState;
class TokenRole {
public:
TokenRole(const FormatStyle &Style) : Style(Style) {}
virtual ~TokenRole();
virtual void precomputeFormattingInfos(const FormatToken *Token);
virtual unsigned formatFromToken(LineState &State,
ContinuationIndenter *Indenter,
bool DryRun) {
return 0;
}
virtual unsigned formatAfterToken(LineState &State,
ContinuationIndenter *Indenter,
bool DryRun) {
return 0;
}
virtual void CommaFound(const FormatToken *Token) {}
protected:
const FormatStyle &Style;
};
class CommaSeparatedList : public TokenRole {
public:
CommaSeparatedList(const FormatStyle &Style)
: TokenRole(Style), HasNestedBracedList(false) {}
void precomputeFormattingInfos(const FormatToken *Token) override;
unsigned formatAfterToken(LineState &State, ContinuationIndenter *Indenter,
bool DryRun) override;
unsigned formatFromToken(LineState &State, ContinuationIndenter *Indenter,
bool DryRun) override;
void CommaFound(const FormatToken *Token) override {
Commas.push_back(Token);
}
private:
struct ColumnFormat {
unsigned Columns;
unsigned TotalWidth;
unsigned LineCount;
SmallVector<unsigned, 8> ColumnSizes;
};
const ColumnFormat *getColumnFormat(unsigned RemainingCharacters) const;
SmallVector<const FormatToken *, 8> Commas;
SmallVector<unsigned, 8> ItemLengths;
SmallVector<ColumnFormat, 4> Formats;
bool HasNestedBracedList;
};
struct AdditionalKeywords {
AdditionalKeywords(IdentifierTable &IdentTable) {
kw_final = &IdentTable.get("final");
kw_override = &IdentTable.get("override");
kw_in = &IdentTable.get("in");
kw_CF_ENUM = &IdentTable.get("CF_ENUM");
kw_CF_OPTIONS = &IdentTable.get("CF_OPTIONS");
kw_NS_ENUM = &IdentTable.get("NS_ENUM");
kw_NS_OPTIONS = &IdentTable.get("NS_OPTIONS");
kw_finally = &IdentTable.get("finally");
kw_function = &IdentTable.get("function");
kw_import = &IdentTable.get("import");
kw_is = &IdentTable.get("is");
kw_let = &IdentTable.get("let");
kw_var = &IdentTable.get("var");
kw_abstract = &IdentTable.get("abstract");
kw_assert = &IdentTable.get("assert");
kw_extends = &IdentTable.get("extends");
kw_implements = &IdentTable.get("implements");
kw_instanceof = &IdentTable.get("instanceof");
kw_interface = &IdentTable.get("interface");
kw_native = &IdentTable.get("native");
kw_package = &IdentTable.get("package");
kw_synchronized = &IdentTable.get("synchronized");
kw_throws = &IdentTable.get("throws");
kw___except = &IdentTable.get("__except");
kw_mark = &IdentTable.get("mark");
kw_extend = &IdentTable.get("extend");
kw_option = &IdentTable.get("option");
kw_optional = &IdentTable.get("optional");
kw_repeated = &IdentTable.get("repeated");
kw_required = &IdentTable.get("required");
kw_returns = &IdentTable.get("returns");
kw_signals = &IdentTable.get("signals");
kw_qsignals = &IdentTable.get("Q_SIGNALS");
kw_slots = &IdentTable.get("slots");
kw_qslots = &IdentTable.get("Q_SLOTS");
}
IdentifierInfo *kw_final;
IdentifierInfo *kw_override;
IdentifierInfo *kw_in;
IdentifierInfo *kw_CF_ENUM;
IdentifierInfo *kw_CF_OPTIONS;
IdentifierInfo *kw_NS_ENUM;
IdentifierInfo *kw_NS_OPTIONS;
IdentifierInfo *kw___except;
IdentifierInfo *kw_finally;
IdentifierInfo *kw_function;
IdentifierInfo *kw_import;
IdentifierInfo *kw_is;
IdentifierInfo *kw_let;
IdentifierInfo *kw_var;
IdentifierInfo *kw_abstract;
IdentifierInfo *kw_assert;
IdentifierInfo *kw_extends;
IdentifierInfo *kw_implements;
IdentifierInfo *kw_instanceof;
IdentifierInfo *kw_interface;
IdentifierInfo *kw_native;
IdentifierInfo *kw_package;
IdentifierInfo *kw_synchronized;
IdentifierInfo *kw_throws;
IdentifierInfo *kw_mark;
IdentifierInfo *kw_extend;
IdentifierInfo *kw_option;
IdentifierInfo *kw_optional;
IdentifierInfo *kw_repeated;
IdentifierInfo *kw_required;
IdentifierInfo *kw_returns;
IdentifierInfo *kw_signals;
IdentifierInfo *kw_qsignals;
IdentifierInfo *kw_slots;
IdentifierInfo *kw_qslots;
};
} }
#endif