Skip to content

Commit

Permalink
[clang][NFC] Refactor CharacterLiteral::CharacterKind
Browse files Browse the repository at this point in the history
This patch converts `CharacterLiteral::CharacterKind` to scoped enum in namespace scope. This enables forward declaration of this enum, which is useful in case like annotating bit-fields with `preferred_type`.
  • Loading branch information
Endilll committed Nov 5, 2023
1 parent 3e6ce58 commit c23aaa4
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 55 deletions.
26 changes: 10 additions & 16 deletions clang/include/clang/AST/Expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -1620,35 +1620,27 @@ class FixedPointLiteral : public Expr, public APIntStorage {
}
};

class CharacterLiteral : public Expr {
public:
enum CharacterKind {
Ascii,
Wide,
UTF8,
UTF16,
UTF32
};
enum class CharacterLiteralKind { Ascii, Wide, UTF8, UTF16, UTF32 };

private:
class CharacterLiteral : public Expr {
unsigned Value;
SourceLocation Loc;
public:
// type should be IntTy
CharacterLiteral(unsigned value, CharacterKind kind, QualType type,
CharacterLiteral(unsigned value, CharacterLiteralKind kind, QualType type,
SourceLocation l)
: Expr(CharacterLiteralClass, type, VK_PRValue, OK_Ordinary),
Value(value), Loc(l) {
CharacterLiteralBits.Kind = kind;
CharacterLiteralBits.Kind = llvm::to_underlying(kind);
setDependence(ExprDependence::None);
}

/// Construct an empty character literal.
CharacterLiteral(EmptyShell Empty) : Expr(CharacterLiteralClass, Empty) { }

SourceLocation getLocation() const { return Loc; }
CharacterKind getKind() const {
return static_cast<CharacterKind>(CharacterLiteralBits.Kind);
CharacterLiteralKind getKind() const {
return static_cast<CharacterLiteralKind>(CharacterLiteralBits.Kind);
}

SourceLocation getBeginLoc() const LLVM_READONLY { return Loc; }
Expand All @@ -1657,14 +1649,16 @@ class CharacterLiteral : public Expr {
unsigned getValue() const { return Value; }

void setLocation(SourceLocation Location) { Loc = Location; }
void setKind(CharacterKind kind) { CharacterLiteralBits.Kind = kind; }
void setKind(CharacterLiteralKind kind) {
CharacterLiteralBits.Kind = llvm::to_underlying(kind);
}
void setValue(unsigned Val) { Value = Val; }

static bool classof(const Stmt *T) {
return T->getStmtClass() == CharacterLiteralClass;
}

static void print(unsigned val, CharacterKind Kind, raw_ostream &OS);
static void print(unsigned val, CharacterLiteralKind Kind, raw_ostream &OS);

// Iterators
child_range children() {
Expand Down
14 changes: 7 additions & 7 deletions clang/lib/AST/Expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -982,21 +982,21 @@ std::string FixedPointLiteral::getValueAsString(unsigned Radix) const {
return std::string(S.str());
}

void CharacterLiteral::print(unsigned Val, CharacterKind Kind,
void CharacterLiteral::print(unsigned Val, CharacterLiteralKind Kind,
raw_ostream &OS) {
switch (Kind) {
case CharacterLiteral::Ascii:
case CharacterLiteralKind::Ascii:
break; // no prefix.
case CharacterLiteral::Wide:
case CharacterLiteralKind::Wide:
OS << 'L';
break;
case CharacterLiteral::UTF8:
case CharacterLiteralKind::UTF8:
OS << "u8";
break;
case CharacterLiteral::UTF16:
case CharacterLiteralKind::UTF16:
OS << 'u';
break;
case CharacterLiteral::UTF32:
case CharacterLiteralKind::UTF32:
OS << 'U';
break;
}
Expand All @@ -1009,7 +1009,7 @@ void CharacterLiteral::print(unsigned Val, CharacterKind Kind,
// would result in an invalid \U escape sequence.
// FIXME: multicharacter literals such as '\xFF\xFF\xFF\xFF'
// are not correctly handled.
if ((Val & ~0xFFu) == ~0xFFu && Kind == CharacterLiteral::Ascii)
if ((Val & ~0xFFu) == ~0xFFu && Kind == CharacterLiteralKind::Ascii)
Val &= 0xFFu;
if (Val < 256 && isPrintable((unsigned char)Val))
OS << "'" << (char)Val << "'";
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/AST/StmtProfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1353,7 +1353,7 @@ void StmtProfiler::VisitFixedPointLiteral(const FixedPointLiteral *S) {

void StmtProfiler::VisitCharacterLiteral(const CharacterLiteral *S) {
VisitExpr(S);
ID.AddInteger(S->getKind());
ID.AddInteger(llvm::to_underlying(S->getKind()));
ID.AddInteger(S->getValue());
}

Expand Down
15 changes: 8 additions & 7 deletions clang/lib/AST/TemplateBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,20 @@ static void printIntegral(const TemplateArgument &TemplArg, raw_ostream &Out,
else if (T->isSpecificBuiltinType(BuiltinType::UChar))
Out << "(unsigned char)";
}
CharacterLiteral::print(Val.getZExtValue(), CharacterLiteral::Ascii, Out);
CharacterLiteral::print(Val.getZExtValue(), CharacterLiteralKind::Ascii,
Out);
} else if (T->isAnyCharacterType() && !Policy.MSVCFormatting) {
CharacterLiteral::CharacterKind Kind;
CharacterLiteralKind Kind;
if (T->isWideCharType())
Kind = CharacterLiteral::Wide;
Kind = CharacterLiteralKind::Wide;
else if (T->isChar8Type())
Kind = CharacterLiteral::UTF8;
Kind = CharacterLiteralKind::UTF8;
else if (T->isChar16Type())
Kind = CharacterLiteral::UTF16;
Kind = CharacterLiteralKind::UTF16;
else if (T->isChar32Type())
Kind = CharacterLiteral::UTF32;
Kind = CharacterLiteralKind::UTF32;
else
Kind = CharacterLiteral::Ascii;
Kind = CharacterLiteralKind::Ascii;
CharacterLiteral::print(Val.getExtValue(), Kind, Out);
} else if (IncludeType) {
if (const auto *BT = T->getAs<BuiltinType>()) {
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Edit/RewriteObjCFoundationAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ static bool shouldNotRewriteImmediateMessageArgs(const ObjCMessageExpr *Msg,
static bool rewriteToCharLiteral(const ObjCMessageExpr *Msg,
const CharacterLiteral *Arg,
const NSAPI &NS, Commit &commit) {
if (Arg->getKind() != CharacterLiteral::Ascii)
if (Arg->getKind() != CharacterLiteralKind::Ascii)
return false;
if (NS.isNSNumberLiteralSelector(NSAPI::NSNumberWithChar,
Msg->getSelector())) {
Expand Down
10 changes: 5 additions & 5 deletions clang/lib/Sema/SemaExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3814,15 +3814,15 @@ ExprResult Sema::ActOnCharacterConstant(const Token &Tok, Scope *UDLScope) {
Ty = Context.CharTy; // 'x' -> char in C++;
// u8'x' -> char in C11-C17 and in C++ without char8_t.

CharacterLiteral::CharacterKind Kind = CharacterLiteral::Ascii;
CharacterLiteralKind Kind = CharacterLiteralKind::Ascii;
if (Literal.isWide())
Kind = CharacterLiteral::Wide;
Kind = CharacterLiteralKind::Wide;
else if (Literal.isUTF16())
Kind = CharacterLiteral::UTF16;
Kind = CharacterLiteralKind::UTF16;
else if (Literal.isUTF32())
Kind = CharacterLiteral::UTF32;
Kind = CharacterLiteralKind::UTF32;
else if (Literal.isUTF8())
Kind = CharacterLiteral::UTF8;
Kind = CharacterLiteralKind::UTF8;

Expr *Lit = new (Context) CharacterLiteral(Literal.getValue(), Kind, Ty,
Tok.getLocation());
Expand Down
20 changes: 10 additions & 10 deletions clang/lib/Sema/SemaExprObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,20 +321,20 @@ ExprResult Sema::BuildObjCNumericLiteral(SourceLocation AtLoc, Expr *Number) {
// In C, character literals have type 'int'. That's not the type we want
// to use to determine the Objective-c literal kind.
switch (Char->getKind()) {
case CharacterLiteral::Ascii:
case CharacterLiteral::UTF8:
case CharacterLiteralKind::Ascii:
case CharacterLiteralKind::UTF8:
NumberType = Context.CharTy;
break;

case CharacterLiteral::Wide:
case CharacterLiteralKind::Wide:
NumberType = Context.getWideCharType();
break;

case CharacterLiteral::UTF16:
case CharacterLiteralKind::UTF16:
NumberType = Context.Char16Ty;
break;

case CharacterLiteral::UTF32:
case CharacterLiteralKind::UTF32:
NumberType = Context.Char32Ty;
break;
}
Expand Down Expand Up @@ -611,20 +611,20 @@ ExprResult Sema::BuildObjCBoxedExpr(SourceRange SR, Expr *ValueExpr) {
// In C, character literals have type 'int'. That's not the type we want
// to use to determine the Objective-c literal kind.
switch (Char->getKind()) {
case CharacterLiteral::Ascii:
case CharacterLiteral::UTF8:
case CharacterLiteralKind::Ascii:
case CharacterLiteralKind::UTF8:
ValueType = Context.CharTy;
break;

case CharacterLiteral::Wide:
case CharacterLiteralKind::Wide:
ValueType = Context.getWideCharType();
break;

case CharacterLiteral::UTF16:
case CharacterLiteralKind::UTF16:
ValueType = Context.Char16Ty;
break;

case CharacterLiteral::UTF32:
case CharacterLiteralKind::UTF32:
ValueType = Context.Char32Ty;
break;
}
Expand Down
12 changes: 6 additions & 6 deletions clang/lib/Sema/SemaTemplate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7985,17 +7985,17 @@ Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg,

Expr *E;
if (T->isAnyCharacterType()) {
CharacterLiteral::CharacterKind Kind;
CharacterLiteralKind Kind;
if (T->isWideCharType())
Kind = CharacterLiteral::Wide;
Kind = CharacterLiteralKind::Wide;
else if (T->isChar8Type() && getLangOpts().Char8)
Kind = CharacterLiteral::UTF8;
Kind = CharacterLiteralKind::UTF8;
else if (T->isChar16Type())
Kind = CharacterLiteral::UTF16;
Kind = CharacterLiteralKind::UTF16;
else if (T->isChar32Type())
Kind = CharacterLiteral::UTF32;
Kind = CharacterLiteralKind::UTF32;
else
Kind = CharacterLiteral::Ascii;
Kind = CharacterLiteralKind::Ascii;

E = new (Context) CharacterLiteral(Arg.getAsIntegral().getZExtValue(),
Kind, T, Loc);
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Serialization/ASTReaderStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ void ASTStmtReader::VisitCharacterLiteral(CharacterLiteral *E) {
VisitExpr(E);
E->setValue(Record.readInt());
E->setLocation(readSourceLocation());
E->setKind(static_cast<CharacterLiteral::CharacterKind>(Record.readInt()));
E->setKind(static_cast<CharacterLiteralKind>(Record.readInt()));
}

void ASTStmtReader::VisitParenExpr(ParenExpr *E) {
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Serialization/ASTWriterStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ void ASTStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
VisitExpr(E);
Record.push_back(E->getValue());
Record.AddSourceLocation(E->getLocation());
Record.push_back(E->getKind());
Record.push_back(llvm::to_underlying(E->getKind()));

AbbrevToUse = Writer.getCharacterLiteralAbbrev();

Expand Down

0 comments on commit c23aaa4

Please sign in to comment.