Skip to content

Commit 293690e

Browse files
committed
Add util::capitalized() and Type::categoryName()
1 parent 7b63415 commit 293690e

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

libsolidity/ast/Types.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,35 @@ Type const* Type::commonType(Type const* _a, Type const* _b)
294294
return nullptr;
295295
}
296296

297+
char const* Type::categoryName(Type::Category _category)
298+
{
299+
switch (_category)
300+
{
301+
case Category::Address: return "address";
302+
case Category::Integer: return "integer";
303+
case Category::RationalNumber: return "rational number literal";
304+
case Category::StringLiteral: return "string literal";
305+
case Category::Bool: return "boolean";
306+
case Category::FixedPoint: return "fixed-point number";
307+
case Category::Array: return "array";
308+
case Category::ArraySlice: return "array slice";
309+
case Category::FixedBytes: return "fixed-size byte array";
310+
case Category::Contract: return "contract";
311+
case Category::Struct: return "struct";
312+
case Category::Function: return "function";
313+
case Category::Enum: return "enum";
314+
case Category::UserDefinedValueType: return "user-defined value type";
315+
case Category::Tuple: return "tuple";
316+
case Category::Mapping: return "mapping";
317+
case Category::TypeType: return "type of a type";
318+
case Category::Modifier: return "modifier";
319+
case Category::Magic: return "magic variable";
320+
case Category::Module: return "module";
321+
case Category::InaccessibleDynamic: return "inaccessible dynamic value";
322+
}
323+
util::unreachable();
324+
}
325+
297326
MemberList const& Type::members(ASTNode const* _currentScope) const
298327
{
299328
if (!m_members[_currentScope])

libsolidity/ast/Types.h

+22-3
Original file line numberDiff line numberDiff line change
@@ -174,16 +174,35 @@ class Type
174174

175175
enum class Category
176176
{
177-
Address, Integer, RationalNumber, StringLiteral, Bool, FixedPoint, Array, ArraySlice,
178-
FixedBytes, Contract, Struct, Function, Enum, UserDefinedValueType, Tuple,
179-
Mapping, TypeType, Modifier, Magic, Module,
177+
Address,
178+
Integer,
179+
RationalNumber,
180+
StringLiteral,
181+
Bool,
182+
FixedPoint,
183+
Array,
184+
ArraySlice,
185+
FixedBytes,
186+
Contract,
187+
Struct,
188+
Function,
189+
Enum,
190+
UserDefinedValueType,
191+
Tuple,
192+
Mapping,
193+
TypeType,
194+
Modifier,
195+
Magic,
196+
Module,
180197
InaccessibleDynamic
181198
};
182199

183200
/// @returns a pointer to _a or _b if the other is implicitly convertible to it or nullptr otherwise
184201
static Type const* commonType(Type const* _a, Type const* _b);
185202

186203
virtual Category category() const = 0;
204+
static char const* categoryName(Type::Category _category);
205+
187206
/// @returns a valid solidity identifier such that two types should compare equal if and
188207
/// only if they have the same identifier.
189208
/// The identifier should start with "t_".

libsolutil/StringUtils.h

+9
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,15 @@ inline std::string toLower(std::string _s)
160160
return _s;
161161
}
162162

163+
/// Returns a copy of the string with the first character converted to its uppercase equivalent.
164+
/// Uses the classic "C" locale semantics.
165+
inline std::string capitalized(std::string _s)
166+
{
167+
if (_s.size() > 0)
168+
_s[0] = toUpper(_s[0]);
169+
return _s;
170+
}
171+
163172
/// Checks whether _c is a decimal digit character. It uses the classic "C" locale semantics.
164173
/// @param _c character to be checked
165174
/// @return true if _c is a decimal digit character, false otherwise

0 commit comments

Comments
 (0)