Introduce isDecl and getDeclType

fix enum logic issue

fix tests

guard against null types
This commit is contained in:
Azoy
2021-04-18 01:27:46 -04:00
parent f75f5fe78d
commit 9ed732f0ab
62 changed files with 391 additions and 569 deletions

View File

@@ -78,7 +78,7 @@ deriveRawValueInit(AbstractFunctionDecl *initDecl, void *) {
auto *rawValueDecl = new (C) ParamDecl(
SourceLoc(), SourceLoc(), C.Id_rawValue,
SourceLoc(), C.Id_rawValue, parentDC);
rawValueDecl->setInterfaceType(C.getIntDecl()->getDeclaredInterfaceType());
rawValueDecl->setInterfaceType(C.getIntType());
rawValueDecl->setSpecifier(ParamSpecifier::Default);
rawValueDecl->setImplicit();
auto *paramList = ParameterList::createWithoutLoc(rawValueDecl);
@@ -332,10 +332,9 @@ static bool canSynthesizeCodingKey(DerivedConformance &derived) {
auto *parentDC = derived.getConformanceContext();
rawType = parentDC->mapTypeIntoContext(rawType);
auto &C = derived.Context;
auto *nominal = rawType->getCanonicalType()->getAnyNominal();
if (nominal != C.getStringDecl() && nominal != C.getIntDecl())
if (!rawType->isString() && !rawType->isInt()) {
return false;
}
}
auto inherited = enumDecl->getInherited();
@@ -363,9 +362,9 @@ ValueDecl *DerivedConformance::deriveCodingKey(ValueDecl *requirement) {
auto name = requirement->getBaseName();
if (name == Context.Id_stringValue) {
// Synthesize `var stringValue: String { get }`
auto stringType = Context.getStringDecl()->getDeclaredInterfaceType();
auto synth = [rawType, stringType](AbstractFunctionDecl *getterDecl) {
if (rawType && rawType->isEqual(stringType)) {
auto stringType = Context.getStringType();
auto synth = [rawType](AbstractFunctionDecl *getterDecl) {
if (rawType && rawType->isString()) {
// enum SomeStringEnum : String {
// case A, B, C
// @derived var stringValue: String {
@@ -394,11 +393,11 @@ ValueDecl *DerivedConformance::deriveCodingKey(ValueDecl *requirement) {
} else if (name == Context.Id_intValue) {
// Synthesize `var intValue: Int? { get }`
auto intType = Context.getIntDecl()->getDeclaredInterfaceType();
auto intType = Context.getIntType();
auto optionalIntType = OptionalType::get(intType);
auto synth = [rawType, intType](AbstractFunctionDecl *getterDecl) {
if (rawType && rawType->isEqual(intType)) {
auto synth = [rawType](AbstractFunctionDecl *getterDecl) {
if (rawType && rawType->isInt()) {
// enum SomeIntEnum : Int {
// case A = 1, B = 2, C = 3
// @derived var intValue: Int? {
@@ -423,9 +422,9 @@ ValueDecl *DerivedConformance::deriveCodingKey(ValueDecl *requirement) {
if (argumentNames.size() == 1) {
if (argumentNames[0] == Context.Id_stringValue) {
// Derive `init?(stringValue:)`
auto stringType = Context.getStringDecl()->getDeclaredInterfaceType();
auto synth = [rawType, stringType](AbstractFunctionDecl *initDecl) {
if (rawType && rawType->isEqual(stringType)) {
auto stringType = Context.getStringType();
auto synth = [rawType](AbstractFunctionDecl *initDecl) {
if (rawType && rawType->isString()) {
// enum SomeStringEnum : String {
// case A = "a", B = "b", C = "c"
// @derived init?(stringValue: String) {
@@ -456,9 +455,9 @@ ValueDecl *DerivedConformance::deriveCodingKey(ValueDecl *requirement) {
return deriveInitDecl(*this, stringType, Context.Id_stringValue, synth);
} else if (argumentNames[0] == Context.Id_intValue) {
// Synthesize `init?(intValue:)`
auto intType = Context.getIntDecl()->getDeclaredInterfaceType();
auto synthesizer = [rawType, intType](AbstractFunctionDecl *initDecl) {
if (rawType && rawType->isEqual(intType)) {
auto intType = Context.getIntType();
auto synthesizer = [rawType](AbstractFunctionDecl *initDecl) {
if (rawType && rawType->isInt()) {
// enum SomeIntEnum : Int {
// case A = 1, B = 2, C = 3
// @derived init?(intValue: Int) {