Skip to content

Commit 5ec251c

Browse files
committed
WIP: attempt to extract RawRepresentable
1 parent b01dbc5 commit 5ec251c

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

include/swift/AST/ConstTypeInfo.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ class CompileTimeValue {
3737
Tuple,
3838
Enum,
3939
Type,
40-
Runtime
40+
Runtime,
41+
RawRepresentable
4142
};
4243

4344
ValueKind getKind() const { return Kind; }
@@ -66,6 +67,25 @@ class RawLiteralValue : public CompileTimeValue {
6667
std::string Value;
6768
};
6869

70+
class RawRepresentableValue : public CompileTimeValue {
71+
public:
72+
RawRepresentableValue(std::string Name,
73+
std::shared_ptr<CompileTimeValue> Value)
74+
: CompileTimeValue(ValueKind::RawRepresentable), Name(Name),
75+
Value(Value) {}
76+
77+
std::string getName() const { return Name; }
78+
std::shared_ptr<CompileTimeValue> getValue() const { return Value; }
79+
80+
static bool classof(const CompileTimeValue *T) {
81+
return T->getKind() == ValueKind::RawRepresentable;
82+
}
83+
84+
private:
85+
std::string Name;
86+
std::shared_ptr<CompileTimeValue> Value;
87+
};
88+
6989
struct FunctionParameter {
7090
std::string Label;
7191
swift::Type Type;

lib/ConstExtract/ConstExtract.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,31 @@ static std::shared_ptr<CompileTimeValue> extractCompileTimeValue(Expr *expr) {
381381
return extractCompileTimeValue(injectIntoOptionalExpr->getSubExpr());
382382
}
383383

384+
case ExprKind::MemberRef: {
385+
auto memberRefExpr = cast<MemberRefExpr>(expr);
386+
// somehow check raw representable
387+
auto base = memberRefExpr->getBase();
388+
if (base->getKind() == ExprKind::Type) {
389+
auto type = cast<TypeExpr>(base)->getInstanceType();
390+
// FIXME: ApolloZhu check type == expr->getType()
391+
// FIXME: ApolloZhu need a better way of getDecl for all types
392+
auto typeDecl = type->getAs<NominalType>()->getDecl();
393+
for (auto &protocol : typeDecl->getAllProtocols()) {
394+
if (protocol->getKnownProtocolKind() &&
395+
*protocol->getKnownProtocolKind() ==
396+
KnownProtocolKind::RawRepresentable) {
397+
auto decl = memberRefExpr->getMember().getDecl();
398+
// FIXME: ApolloZhu check it's actually VarDecl
399+
auto caseName = decl->getName().getBaseIdentifier().str().str();
400+
auto init = cast<VarDecl>(decl)->getParentInitializer();
401+
return std::make_shared<RawRepresentableValue>(
402+
caseName, extractCompileTimeValue(init));
403+
}
404+
}
405+
}
406+
break;
407+
}
408+
384409
default: {
385410
break;
386411
}
@@ -587,6 +612,16 @@ void writeValue(llvm::json::OStream &JSON,
587612
break;
588613
}
589614

615+
case CompileTimeValue::ValueKind::RawRepresentable: {
616+
auto rawRepresentable = cast<RawRepresentableValue>(value);
617+
JSON.attribute("valueKind", "RawRepresentable");
618+
JSON.attributeObject("value", [&]() {
619+
JSON.attribute("name", rawRepresentable->getName());
620+
writeValue(JSON, rawRepresentable->getValue());
621+
});
622+
break;
623+
}
624+
590625
case CompileTimeValue::ValueKind::InitCall: {
591626
auto initCallValue = cast<InitCallValue>(value);
592627

0 commit comments

Comments
 (0)