Skip to content

Commit 7b31d2b

Browse files
committed
[SyntaxParse] Finish type parsing
- Type attributes - SIL types
1 parent 5726179 commit 7b31d2b

17 files changed

+826
-605
lines changed

include/swift/Parse/ASTGen.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ class ASTGen {
103103
//===--------------------------------------------------------------------===//
104104
// Types.
105105

106-
TypeRepr *generate(const syntax::TypeSyntax &Type, const SourceLoc Loc);
106+
TypeRepr *generate(const syntax::TypeSyntax &Type, const SourceLoc Loc,
107+
bool IsSILFuncDecl = false);
107108
TypeRepr *generate(const syntax::SomeTypeSyntax &Type, const SourceLoc Loc);
108109
TypeRepr *generate(const syntax::CompositionTypeSyntax &Type,
109110
const SourceLoc Loc);
@@ -127,11 +128,19 @@ class ASTGen {
127128
const SourceLoc Loc);
128129
TypeRepr *generate(const syntax::ClassRestrictionTypeSyntax &Type,
129130
const SourceLoc Loc);
131+
TypeRepr *generate(const syntax::SILBoxTypeSyntax &Type, const SourceLoc Loc,
132+
bool IsSILFuncDecl);
133+
TypeRepr *generate(const syntax::SILFunctionTypeSyntax &Type,
134+
const SourceLoc Loc, bool IsSILFuncDecl);
130135
TypeRepr *generate(const syntax::CodeCompletionTypeSyntax &Type,
131136
const SourceLoc Loc);
132137
TypeRepr *generate(const syntax::UnknownTypeSyntax &Type,
133138
const SourceLoc Loc);
134139

140+
TypeAttributes
141+
generateTypeAttributes(const syntax::AttributeListSyntax &syntax,
142+
const SourceLoc Loc);
143+
135144
private:
136145
TupleTypeRepr *
137146
generateTuple(const syntax::TokenSyntax &LParen,

include/swift/Parse/Parser.h

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,24 +1082,13 @@ class Parser {
10821082
/// an error parsing.
10831083
bool parseVersionTuple(llvm::VersionTuple &Version, SourceRange &Range,
10841084
const Diagnostic &D);
1085-
10861085
bool parseTypeAttributeList(ParamDecl::Specifier &Specifier,
10871086
SourceLoc &SpecifierLoc,
1088-
TypeAttributes &Attributes) {
1089-
if (Tok.isAny(tok::at_sign, tok::kw_inout) ||
1090-
(Tok.is(tok::identifier) &&
1091-
(Tok.getRawText().equals("__shared") ||
1092-
Tok.getRawText().equals("__owned"))))
1093-
return parseTypeAttributeListPresent(Specifier, SpecifierLoc, Attributes);
1094-
return false;
1095-
}
1096-
bool parseTypeAttributeListPresent(ParamDecl::Specifier &Specifier,
1097-
SourceLoc &SpecifierLoc,
1098-
TypeAttributes &Attributes);
1099-
bool parseTypeAttribute(TypeAttributes &Attributes, SourceLoc AtLoc,
1100-
bool justChecking = false);
1101-
1102-
1087+
TypeAttributes &Attributes);
1088+
ParserStatus parseTypeAttributeListSyntax(Optional<ParsedTokenSyntax> &specifier,
1089+
Optional<ParsedAttributeListSyntax> &attrs);
1090+
ParsedSyntaxResult<ParsedAttributeSyntax> parseTypeAttributeSyntax();
1091+
11031092
ParserResult<ImportDecl> parseDeclImport(ParseDeclOptions Flags,
11041093
DeclAttributes &Attributes);
11051094
ParserStatus parseInheritance(MutableArrayRef<TypeLoc> &Inherited,
@@ -1206,11 +1195,8 @@ class Parser {
12061195
ParserStatus
12071196
parseGenericArgumentsAST(llvm::SmallVectorImpl<TypeRepr *> &ArgsAST,
12081197
SourceLoc &LAngleLoc, SourceLoc &RAngleLoc);
1209-
TypeASTResult parseSILBoxType(GenericParamList *generics,
1210-
const TypeAttributes &attrs,
1211-
Optional<Scope> &GenericsScope);
1212-
TypeASTResult parseTypeSimpleOrCompositionAST(Diag<> MessageID,
1213-
bool HandleCodeCompletion);
1198+
TypeResult parseSILBoxTypeSyntax(
1199+
Optional<ParsedGenericParameterClauseListSyntax> genericParams);
12141200
TypeASTResult parseAnyTypeAST();
12151201

12161202
ParsedSyntaxResult<ParsedGenericArgumentClauseSyntax>
@@ -1240,7 +1226,10 @@ class Parser {
12401226
TypeRepr *applyAttributeToType(TypeRepr *Ty, const TypeAttributes &Attr,
12411227
ParamDecl::Specifier Specifier,
12421228
SourceLoc SpecifierLoc);
1243-
1229+
ParsedSyntaxResult<ParsedTypeSyntax>
1230+
applyAttributeToTypeSyntax(ParsedSyntaxResult<ParsedTypeSyntax> &&ty,
1231+
Optional<ParsedTokenSyntax> specifier,
1232+
Optional<ParsedAttributeListSyntax> attrs);
12441233
//===--------------------------------------------------------------------===//
12451234
// Pattern Parsing
12461235

lib/Parse/ASTGen.cpp

Lines changed: 204 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,8 @@ Expr *ASTGen::generate(const UnknownExprSyntax &Expr, const SourceLoc Loc) {
226226
return nullptr;
227227
}
228228

229-
TypeRepr *ASTGen::generate(const TypeSyntax &Type, const SourceLoc Loc) {
229+
TypeRepr *ASTGen::generate(const TypeSyntax &Type, const SourceLoc Loc,
230+
bool IsSILFuncDecl) {
230231
TypeRepr *TypeAST = nullptr;
231232

232233
if (auto SimpleIdentifier = Type.getAs<SimpleTypeIdentifierSyntax>())
@@ -255,6 +256,10 @@ TypeRepr *ASTGen::generate(const TypeSyntax &Type, const SourceLoc Loc) {
255256
TypeAST = generate(*Attributed, Loc);
256257
else if (auto ClassRestriction = Type.getAs<ClassRestrictionTypeSyntax>())
257258
TypeAST = generate(*ClassRestriction, Loc);
259+
else if (auto SILBoxType = Type.getAs<SILBoxTypeSyntax>())
260+
TypeAST = generate(*SILBoxType, Loc, IsSILFuncDecl);
261+
else if (auto SILFunctionType = Type.getAs<SILFunctionTypeSyntax>())
262+
TypeAST = generate(*SILFunctionType, Loc, IsSILFuncDecl);
258263
else if (auto CompletionTy = Type.getAs<CodeCompletionTypeSyntax>())
259264
TypeAST = generate(*CompletionTy, Loc);
260265
else if (auto Unknown = Type.getAs<UnknownTypeSyntax>())
@@ -270,16 +275,37 @@ TypeRepr *ASTGen::generate(const TypeSyntax &Type, const SourceLoc Loc) {
270275

271276
TypeRepr *ASTGen::generate(const FunctionTypeSyntax &Type,
272277
const SourceLoc Loc) {
273-
auto ArgumentTypes = generateTuple(Type.getLeftParen(), Type.getArguments(),
274-
Type.getRightParen(), Loc,
275-
/*IsFunction=*/true);
278+
TupleTypeRepr *ArgumentTypes = nullptr;
279+
280+
SourceLoc VoidLoc;
281+
if (Type.getLeftParen().isMissing() && Type.getArguments().size() == 1) {
282+
if (auto ident = Type.getArguments()[0]
283+
.getType()
284+
.getAs<SimpleTypeIdentifierSyntax>()) {
285+
if (!ident->getGenericArgumentClause().hasValue() &&
286+
ident->getName().getText() == "Void")
287+
VoidLoc = advanceLocBegin(Loc, ident->getName());
288+
}
289+
}
290+
291+
if (VoidLoc.isValid())
292+
ArgumentTypes = TupleTypeRepr::createEmpty(Context, VoidLoc);
293+
else {
294+
ArgumentTypes = generateTuple(Type.getLeftParen(), Type.getArguments(),
295+
Type.getRightParen(), Loc,
296+
/*IsFunction=*/true);
297+
}
298+
if (!ArgumentTypes)
299+
return nullptr;
276300

277301
auto ThrowsLoc = Type.getThrowsOrRethrowsKeyword()
278302
? generate(*Type.getThrowsOrRethrowsKeyword(), Loc)
279303
: SourceLoc();
280304

281305
auto ArrowLoc = generate(Type.getArrow(), Loc);
282306
auto ReturnType = generate(Type.getReturnType(), Loc);
307+
if (!ReturnType)
308+
return nullptr;
283309

284310
return new (Context)
285311
FunctionTypeRepr(nullptr, ArgumentTypes, ThrowsLoc, ArrowLoc, ReturnType);
@@ -345,42 +371,109 @@ TupleTypeRepr *ASTGen::generateTuple(const TokenSyntax &LParen,
345371
EllipsisLoc, EllipsisIdx);
346372
}
347373

348-
TypeRepr *ASTGen::generate(const AttributedTypeSyntax &Type,
349-
const SourceLoc Loc) {
350-
// todo [gsoc]: improve this after refactoring attribute parsing
374+
TypeAttributes ASTGen::generateTypeAttributes(const AttributeListSyntax &syntax,
375+
const SourceLoc Loc) {
376+
TypeAttributes attrs;
351377

352-
auto TypeAST = generate(Type.getBaseType(), Loc);
378+
for (auto elem : syntax) {
379+
auto attrSyntax = elem.castTo<AttributeSyntax>();
380+
if (attrSyntax.getAttributeName().isMissing())
381+
continue;
353382

354-
if (auto Attributes = Type.getAttributes()) {
355-
TypeAttributes TypeAttrs;
383+
auto attrName = attrSyntax.getAttributeName().getText();
356384

357-
for (auto Attribute : *Attributes) {
358-
auto Attr = Attribute.castTo<AttributeSyntax>();
359-
auto AttrNameStr = Attr.getAttributeName().getText();
385+
auto atLoc = advanceLocBegin(Loc, attrSyntax.getAtSignToken());
386+
if (attrs.AtLoc.isInvalid())
387+
attrs.AtLoc = atLoc;
360388

361-
auto AtLoc = advanceLocBegin(Loc, Attr.getAtSignToken());
362-
auto AttrKind = TypeAttributes::getAttrKindFromString(AttrNameStr);
389+
auto attr = TypeAttributes::getAttrKindFromString(attrName);
390+
if (attr == TAK_Count)
391+
continue;
392+
393+
if (attrs.has(attr)) {
394+
P.diagnose(atLoc, diag::duplicate_attribute, /*isModifier=*/false);
395+
continue;
396+
}
363397

364-
TypeAttrs.setAttr(AttrKind, AtLoc);
398+
auto arg = attrSyntax.getArgument();
365399

366-
if (AttrKind == TAK_convention) {
367-
auto Argument = Attr.getArgument()->castTo<TokenSyntax>();
368-
auto Convention = Context.getIdentifier(Argument.getIdentifierText());
369-
TypeAttrs.convention = Convention.str();
400+
if (attr == TAK_sil_weak || attr == TAK_sil_unowned) {
401+
if (attrs.hasOwnership()) {
402+
P.diagnose(atLoc, diag::duplicate_attribute, /*isModifier*/false);
370403
}
404+
} else if (attr == TAK_convention) {
405+
// @convention(block)
406+
// @convention(witness_method: ProtocolName)
407+
if (!arg)
408+
continue;
371409

372-
if (AttrKind == TAK_opened) {
373-
auto AttrText = Attr.getArgument()->castTo<TokenSyntax>().getText();
374-
auto LiteralText = AttrText.slice(1, AttrText.size() - 1);
375-
TypeAttrs.OpenedID = UUID::fromString(LiteralText.str().c_str());
410+
if (auto conventionNameTok = arg->getAs<TokenSyntax>()) {
411+
assert(conventionNameTok->getTokenKind() == tok::identifier);
412+
auto convention =
413+
Context.getIdentifier(conventionNameTok->getIdentifierText());
414+
attrs.convention = convention.str();
415+
} else if (auto witness =
416+
arg->getAs<NamedAttributeStringArgumentSyntax>()) {
417+
assert(witness->getNameTok().getIdentifierText() == "witness_method");
418+
if (witness->getStringOrDeclname().isMissing())
419+
continue;
420+
auto protocolName =
421+
witness->getStringOrDeclname().castTo<DeclNameSyntax>();
422+
auto protocol = Context.getIdentifier(
423+
protocolName.getDeclBaseName().getIdentifierText());
424+
attrs.convention = "witness_method";
425+
attrs.conventionWitnessMethodProtocol = protocol.str();
426+
} else {
427+
continue;
376428
}
429+
} else if (attr == TAK_opened) {
430+
// @opened("01234567-89ab-cdef-0123-111111111111")
431+
if (!arg)
432+
continue;
377433

378-
if (TypeAttrs.AtLoc.isInvalid())
379-
TypeAttrs.AtLoc = AtLoc;
434+
assert(arg->castTo<TokenSyntax>().getTokenKind() ==
435+
tok::string_literal);
436+
auto tokText = arg->castTo<TokenSyntax>().getText();
437+
auto literalText = tokText.slice(1, tokText.size() - 1);
438+
attrs.OpenedID = UUID::fromString(literalText.str().c_str());
439+
} else if (attr == TAK__opaqueReturnTypeOf) {
440+
// @_opaqueReturnTypeOf("$sMangledName", 0)
441+
if (!arg)
442+
continue;
443+
444+
auto opaqueArg =
445+
arg->castTo<OpaqueReturnTypeOfAttributeArgumentsSyntax>();
446+
447+
auto manglingTok = opaqueArg.getMangledName();
448+
auto indexTok = opaqueArg.getIndex();
449+
if (manglingTok.isMissing() || indexTok.isMissing())
450+
continue;
451+
452+
auto tokText = manglingTok.getText();
453+
auto mangling =
454+
Context.getIdentifier(tokText.slice(1, tokText.size() - 1));
455+
unsigned index;
456+
if (indexTok.getText().getAsInteger(10, index))
457+
continue;
458+
attrs.setOpaqueReturnTypeOf(mangling.str(), index);
380459
}
381460

382-
if (!TypeAttrs.empty())
383-
TypeAST = new (Context) AttributedTypeRepr(TypeAttrs, TypeAST);
461+
attrs.setAttr(attr, atLoc);
462+
}
463+
464+
return attrs;
465+
}
466+
467+
TypeRepr *ASTGen::generate(const AttributedTypeSyntax &Type,
468+
const SourceLoc Loc) {
469+
auto TypeAST = generate(Type.getBaseType(), Loc);
470+
if (!TypeAST)
471+
return nullptr;
472+
473+
if (auto Attributes = Type.getAttributes()) {
474+
TypeAttributes attrs = generateTypeAttributes(*Attributes, Loc);
475+
if (!attrs.empty())
476+
TypeAST = new (Context) AttributedTypeRepr(attrs, TypeAST);
384477
}
385478

386479
if (auto Specifier = Type.getSpecifier()) {
@@ -581,18 +674,92 @@ ASTGen::generate(const ClassRestrictionTypeSyntax &Type, const SourceLoc Loc) {
581674
SimpleIdentTypeRepr(classLoc, Context.getIdentifier("AnyObject"));
582675
}
583676

677+
TypeRepr *ASTGen::generate(const SILBoxTypeSyntax &Type, const SourceLoc Loc,
678+
bool IsSILFuncDecl) {
679+
if (Type.getRightBrace().isMissing())
680+
return nullptr;
681+
682+
// If this is part of a sil function decl, generic parameters are visible in
683+
// the function body; otherwise, they are visible when parsing the type.
684+
Optional<Scope> GenericsScope;
685+
if (!IsSILFuncDecl)
686+
GenericsScope.emplace(&P, ScopeKind::Generics);
687+
688+
GenericParamList *generics = nullptr;
689+
if (auto genericParamsSyntax = Type.getGenericParameterClauses())
690+
generics = generate(*genericParamsSyntax, Loc);
691+
692+
SmallVector<SILBoxTypeRepr::Field, 4> Fields;
693+
for (auto field : Type.getFields()) {
694+
auto specifier = field.getSpecifier();
695+
if (specifier.isMissing())
696+
return nullptr;
697+
bool Mutable;
698+
if (specifier.getTokenKind() == tok::kw_let)
699+
Mutable = false;
700+
else if (specifier.getTokenKind() == tok::kw_var)
701+
Mutable = true;
702+
else
703+
return nullptr;
704+
SourceLoc VarOrLetLoc = advanceLocBegin(Loc, specifier);;
705+
auto fieldTy = generate(field.getType(), Loc);
706+
if (!fieldTy)
707+
return nullptr;
708+
709+
Fields.emplace_back(VarOrLetLoc, Mutable, fieldTy);
710+
}
711+
GenericsScope.reset();
712+
713+
auto LBraceLoc = advanceLocBegin(Loc, Type.getLeftBrace());
714+
auto RBraceLoc = advanceLocBegin(Loc, Type.getRightBrace());
715+
716+
SourceLoc LAngleLoc, RAngleLoc;
717+
SmallVector<TypeRepr*, 4> Args;
718+
if (auto genericArgs = Type.getGenericArgumentClause()) {
719+
if (genericArgs->getRightAngleBracket().isMissing())
720+
return nullptr;
721+
LAngleLoc = advanceLocBegin(Loc, genericArgs->getLeftAngleBracket());
722+
RAngleLoc = advanceLocBegin(Loc, genericArgs->getRightAngleBracket());
723+
Args = generate(genericArgs->getArguments(), Loc);
724+
}
725+
726+
auto SILType = SILBoxTypeRepr::create(Context, generics, LBraceLoc, Fields,
727+
RBraceLoc, LAngleLoc, Args, RAngleLoc);
728+
return SILType;
729+
}
730+
731+
TypeRepr *ASTGen::generate(const SILFunctionTypeSyntax &Type,
732+
const SourceLoc Loc, bool IsSILFuncDecl) {
733+
// If this is part of a sil function decl, generic parameters are visible in
734+
// the function body; otherwise, they are visible when parsing the type.
735+
Optional<Scope> GenericsScope;
736+
if (!IsSILFuncDecl)
737+
GenericsScope.emplace(&P, ScopeKind::Generics);
738+
739+
GenericParamList *generics = nullptr;
740+
if (auto genericParamsSyntax = Type.getGenericParameterClauses())
741+
generics = generate(*genericParamsSyntax, Loc);
742+
743+
auto tyR = cast<FunctionTypeRepr>(generate(Type.getFunction(), Loc));
744+
return new (Context)
745+
FunctionTypeRepr(generics, tyR->getArgsTypeRepr(), tyR->getThrowsLoc(),
746+
tyR->getArrowLoc(), tyR->getResultTypeRepr());
747+
}
748+
584749
TypeRepr *ASTGen::generate(const CodeCompletionTypeSyntax &Type,
585750
const SourceLoc Loc) {
586751
auto base = Type.getBase();
587752
if (!base)
588753
return nullptr;
589754

590-
TypeRepr *parsedTyR = generate(*base, Loc);
591-
if (parsedTyR) {
592-
if (P.CodeCompletion)
755+
if (P.CodeCompletion) {
756+
TypeRepr *parsedTyR = generate(*base, Loc);
757+
if (parsedTyR)
593758
P.CodeCompletion->setParsedTypeLoc(parsedTyR);
594759
}
595-
return parsedTyR;
760+
761+
// Return nullptr to typecheck this TypeRepr independently in code completion.
762+
return nullptr;
596763
}
597764

598765
TypeRepr *ASTGen::generate(const UnknownTypeSyntax &Type, const SourceLoc Loc) {
@@ -650,12 +817,11 @@ TypeRepr *ASTGen::generate(const UnknownTypeSyntax &Type, const SourceLoc Loc) {
650817
SmallVector<TypeRepr *, 4>
651818
ASTGen::generate(const GenericArgumentListSyntax &Args, const SourceLoc Loc) {
652819
SmallVector<TypeRepr *, 4> Types;
653-
Types.resize(Args.size());
654-
655-
for (int i = Args.size() - 1; i >= 0; i--) {
656-
auto Arg = Args.getChild(i).getValue().castTo<GenericArgumentSyntax>();
657-
auto Type = generate(Arg, Loc);
658-
Types[i] = Type;
820+
for (auto Arg : Args) {
821+
auto tyR = generate(Arg, Loc);
822+
if (!tyR)
823+
tyR = new (Context) ErrorTypeRepr(advanceLocBegin(Loc, Arg));
824+
Types.push_back(tyR);
659825
}
660826

661827
return Types;

0 commit comments

Comments
 (0)