lxdrgen-swift: fix warnings with exhaustive unions
This commit is contained in:
		@@ -20,4 +20,9 @@ struct Struct {
 | 
				
			|||||||
	case NOTHING:
 | 
						case NOTHING:
 | 
				
			||||||
		void;
 | 
							void;
 | 
				
			||||||
	} u<>;
 | 
						} u<>;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						union Onion switch (Enum tag) {
 | 
				
			||||||
 | 
						case NOTHING:
 | 
				
			||||||
 | 
							void;
 | 
				
			||||||
 | 
						} o;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -259,7 +259,7 @@ function codegen_union_struct( \
 | 
				
			|||||||
		"\t\tbreak;\n")
 | 
							"\t\tbreak;\n")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function codegen_union(name, cg,    f, ctype, funcname) {
 | 
					function codegen_union(name, cg, exhaustive,    f, ctype, funcname) {
 | 
				
			||||||
	ctype = "union " PrefixLower cameltosnake(name)
 | 
						ctype = "union " PrefixLower cameltosnake(name)
 | 
				
			||||||
	print ""
 | 
						print ""
 | 
				
			||||||
	print ctype " {"
 | 
						print ctype " {"
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -467,7 +467,7 @@ function codegen_union_struct(name, casename, cg, scg,     structname, init) {
 | 
				
			|||||||
		"\t\tu.Interface = &s\n")
 | 
							"\t\tu.Interface = &s\n")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function codegen_union(name, cg,    gotype, tagvar) {
 | 
					function codegen_union(name, cg, exhaustive,    gotype, tagvar) {
 | 
				
			||||||
	gotype = PrefixCamel name
 | 
						gotype = PrefixCamel name
 | 
				
			||||||
	print "type " gotype " struct {"
 | 
						print "type " gotype " struct {"
 | 
				
			||||||
	print "\tInterface any"
 | 
						print "\tInterface any"
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -208,7 +208,7 @@ function codegen_union_struct(name, casename, cg, scg,     structname) {
 | 
				
			|||||||
		"\t}\n")
 | 
							"\t}\n")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function codegen_union(name, cg,    tagvar) {
 | 
					function codegen_union(name, cg, exhaustive,    tagvar) {
 | 
				
			||||||
	tagvar = cg["tagname"]
 | 
						tagvar = cg["tagname"]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	print ""
 | 
						print ""
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -246,7 +246,7 @@ function codegen_union_struct(name, casename, cg, scg,     swifttype) {
 | 
				
			|||||||
		delete scg[i]
 | 
							delete scg[i]
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function codegen_union(name, cg,    swifttype, init) {
 | 
					function codegen_union(name, cg, exhaustive,    swifttype, init) {
 | 
				
			||||||
	# Classes don't have automatic member-wise initializers,
 | 
						# Classes don't have automatic member-wise initializers,
 | 
				
			||||||
	# thus using structs and protocols.
 | 
						# thus using structs and protocols.
 | 
				
			||||||
	swifttype = PrefixCamel name
 | 
						swifttype = PrefixCamel name
 | 
				
			||||||
@@ -255,6 +255,10 @@ function codegen_union(name, cg,    swifttype, init) {
 | 
				
			|||||||
	print "\tvar " cg["tagname"] ": " CodegenSwiftType[cg["tagtype"]] " { get }"
 | 
						print "\tvar " cg["tagname"] ": " CodegenSwiftType[cg["tagtype"]] " { get }"
 | 
				
			||||||
	print "}"
 | 
						print "}"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (!exhaustive)
 | 
				
			||||||
 | 
							append(cg, "cases", "\tdefault:\n" \
 | 
				
			||||||
 | 
								"\t\tthrow RelayReader.ReadError.unexpectedValue\n")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	init = decapitalize(swifttype)
 | 
						init = decapitalize(swifttype)
 | 
				
			||||||
	print ""
 | 
						print ""
 | 
				
			||||||
	print "public func " init \
 | 
						print "public func " init \
 | 
				
			||||||
@@ -262,11 +266,7 @@ function codegen_union(name, cg,    swifttype, init) {
 | 
				
			|||||||
	print "\tlet " cg["tagname"] ": " CodegenSwiftType[cg["tagtype"]] \
 | 
						print "\tlet " cg["tagname"] ": " CodegenSwiftType[cg["tagtype"]] \
 | 
				
			||||||
		" = try from.read()"
 | 
							" = try from.read()"
 | 
				
			||||||
	print "\tswitch " cg["tagname"] " {"
 | 
						print "\tswitch " cg["tagname"] " {"
 | 
				
			||||||
	# TODO: Only generate the default if there are remaining values,
 | 
						print cg["cases"] "\t}"
 | 
				
			||||||
	# so that swiftc doesn't produce warnings.
 | 
					 | 
				
			||||||
	print cg["cases"] "\tdefault:"
 | 
					 | 
				
			||||||
	print "\t\tthrow RelayReader.ReadError.unexpectedValue"
 | 
					 | 
				
			||||||
	print"\t}"
 | 
					 | 
				
			||||||
	print "}"
 | 
						print "}"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	CodegenSwiftType[name] = swifttype
 | 
						CodegenSwiftType[name] = swifttype
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -217,7 +217,8 @@ function defstruct(    name, d, cg) {
 | 
				
			|||||||
	return name
 | 
						return name
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function defunion(    name, tag, tagtype, tagvalue, cg, scg, d, a, i, unseen) {
 | 
					function defunion(    name, tag, tagtype, tagvalue, cg, scg, d, a, i,
 | 
				
			||||||
 | 
							unseen, exhaustive) {
 | 
				
			||||||
	delete cg[0]
 | 
						delete cg[0]
 | 
				
			||||||
	delete scg[0]
 | 
						delete scg[0]
 | 
				
			||||||
	delete d[0]
 | 
						delete d[0]
 | 
				
			||||||
@@ -258,9 +259,14 @@ function defunion(    name, tag, tagtype, tagvalue, cg, scg, d, a, i, unseen) {
 | 
				
			|||||||
	if (tagvalue)
 | 
						if (tagvalue)
 | 
				
			||||||
		codegen_union_struct(name, tagvalue, cg, scg)
 | 
							codegen_union_struct(name, tagvalue, cg, scg)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	# What remains non-zero in unseen[2..] is simply not recognized/allowed.
 | 
						# Unseen cases are simply not recognized/allowed.
 | 
				
			||||||
 | 
						exhaustive = 1
 | 
				
			||||||
 | 
						for (i in unseen)
 | 
				
			||||||
 | 
							if (i && unseen[i])
 | 
				
			||||||
 | 
								exhaustive = 0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	Types[name] = "union"
 | 
						Types[name] = "union"
 | 
				
			||||||
	codegen_union(name, cg)
 | 
						codegen_union(name, cg, exhaustive)
 | 
				
			||||||
	return name
 | 
						return name
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user