Implement type sugar "T?" for Optional<T>.

- New type representation OptionalTypeRepr.
- New sugared type OptionalType.
- New base type SyntaxSugarType, parent of ArraySliceType and OptionalType.
  These two are the same in a lot of ways.
- The form "T[]?" is forbidden, because it makes "Int[4][2]" oddly
  different from "Int[4]?[2]". The type can be spelled "(T[])?" or
  Optional<T[]>.
- Like Slice, "Optional" is just looked up in the current module. This may
  or may not be the desired behavior in the long run.

<rdar://problem/14666783>

Swift SVN r7100
This commit is contained in:
Jordan Rose
2013-08-09 21:33:36 +00:00
parent f736e1711c
commit 88cad52d72
17 changed files with 290 additions and 58 deletions

View File

@@ -455,6 +455,7 @@ void Serializer::writeBlockInfoBlock() {
RECORD(decls_block, ARRAY_TYPE);
RECORD(decls_block, REFERENCE_STORAGE_TYPE);
RECORD(decls_block, UNBOUND_GENERIC_TYPE);
RECORD(decls_block, OPTIONAL_TYPE);
RECORD(decls_block, TYPE_ALIAS_DECL);
RECORD(decls_block, STRUCT_DECL);
@@ -1613,6 +1614,18 @@ bool Serializer::writeType(Type ty) {
return true;
}
case TypeKind::Optional: {
auto sliceTy = cast<OptionalType>(ty.getPointer());
Type base = sliceTy->getBaseType();
Type impl = sliceTy->getImplementationType();
unsigned abbrCode = DeclTypeAbbrCodes[OptionalTypeLayout::Code];
OptionalTypeLayout::emitRecord(Out, ScratchRecord, abbrCode,
addTypeRef(base), addTypeRef(impl));
return true;
}
case TypeKind::ProtocolComposition: {
auto composition = cast<ProtocolCompositionType>(ty.getPointer());
@@ -1714,6 +1727,7 @@ void Serializer::writeAllDeclsAndTypes() {
registerDeclTypeAbbr<ArraySliceTypeLayout>();
registerDeclTypeAbbr<ArrayTypeLayout>();
registerDeclTypeAbbr<UnboundGenericTypeLayout>();
registerDeclTypeAbbr<OptionalTypeLayout>();
registerDeclTypeAbbr<TypeAliasLayout>();
registerDeclTypeAbbr<StructLayout>();