@@ -215,9 +215,13 @@ ParserResult<TypeRepr> Parser::parseTypeSimple(Diag<> MessageID,
215
215
// Eat the code completion token because we handled it.
216
216
consumeToken (tok::code_complete);
217
217
return makeParserCodeCompletionResult<TypeRepr>();
218
- case tok::l_square:
219
- ty = parseTypeCollection ();
218
+ case tok::l_square: {
219
+ auto Result = parseTypeCollection ();
220
+ if (Result.hasSyntax ())
221
+ SyntaxContext->addSyntax (Result.getSyntax ());
222
+ ty = Result.getASTResult ();
220
223
break ;
224
+ }
221
225
case tok::kw_protocol:
222
226
if (startsWithLess (peekToken ())) {
223
227
ty = parseOldStyleProtocolComposition ();
@@ -240,6 +244,17 @@ ParserResult<TypeRepr> Parser::parseTypeSimple(Diag<> MessageID,
240
244
checkForInputIncomplete ();
241
245
return nullptr ;
242
246
}
247
+
248
+ auto makeMetatypeTypeSyntax = [&]() {
249
+ if (!SyntaxContext->isEnabled ())
250
+ return ;
251
+ MetatypeTypeSyntaxBuilder Builder;
252
+ Builder
253
+ .useTypeOrProtocol (SyntaxContext->popToken ())
254
+ .usePeriod (SyntaxContext->popToken ())
255
+ .useBaseType (SyntaxContext->popIf <TypeSyntax>().getValue ());
256
+ SyntaxContext->addSyntax (Builder.build ());
257
+ };
243
258
244
259
// '.Type', '.Protocol', '?', '!', and '[]' still leave us with type-simple.
245
260
while (ty.isNonNull ()) {
@@ -249,24 +264,32 @@ ParserResult<TypeRepr> Parser::parseTypeSimple(Diag<> MessageID,
249
264
SourceLoc metatypeLoc = consumeToken (tok::identifier);
250
265
ty = makeParserResult (ty,
251
266
new (Context) MetatypeTypeRepr (ty.get (), metatypeLoc));
267
+ makeMetatypeTypeSyntax ();
252
268
continue ;
253
269
}
254
270
if (peekToken ().isContextualKeyword (" Protocol" )) {
255
271
consumeToken ();
256
272
SourceLoc protocolLoc = consumeToken (tok::identifier);
257
273
ty = makeParserResult (ty,
258
274
new (Context) ProtocolTypeRepr (ty.get (), protocolLoc));
275
+ makeMetatypeTypeSyntax ();
259
276
continue ;
260
277
}
261
278
}
262
279
263
280
if (!Tok.isAtStartOfLine ()) {
264
281
if (isOptionalToken (Tok)) {
265
- ty = parseTypeOptional (ty.get ());
282
+ auto Result = parseTypeOptional (ty.get ());
283
+ if (Result.hasSyntax ())
284
+ SyntaxContext->addSyntax (Result.getSyntax ());
285
+ ty = Result.getASTResult ();
266
286
continue ;
267
287
}
268
288
if (isImplicitlyUnwrappedOptionalToken (Tok)) {
269
- ty = parseTypeImplicitlyUnwrappedOptional (ty.get ());
289
+ auto Result = parseTypeImplicitlyUnwrappedOptional (ty.get ());
290
+ if (Result.hasSyntax ())
291
+ SyntaxContext->addSyntax (Result.getSyntax ());
292
+ ty = Result.getASTResult ();
270
293
continue ;
271
294
}
272
295
// Parse legacy array types for migration.
@@ -730,6 +753,7 @@ Parser::parseAnyType() {
730
753
// / type-identifier
731
754
// / type-composition-list-deprecated ',' type-identifier
732
755
ParserResult<TypeRepr> Parser::parseOldStyleProtocolComposition () {
756
+ SyntaxParsingContext TypeContext (SyntaxContext, SyntaxContextKind::Type);
733
757
assert (Tok.is (tok::kw_protocol) && startsWithLess (peekToken ()));
734
758
735
759
// Start a context for creating type syntax.
@@ -839,6 +863,7 @@ ParserResult<TypeRepr> Parser::parseOldStyleProtocolComposition() {
839
863
// / identifier ':' type
840
864
// / type
841
865
ParserResult<TupleTypeRepr> Parser::parseTypeTupleBody () {
866
+ SyntaxParsingContext TypeContext (SyntaxContext, SyntaxContextKind::Type);
842
867
Parser::StructureMarkerRAII ParsingTypeTuple (*this , Tok);
843
868
SourceLoc RPLoc, LPLoc = consumeToken (tok::l_paren);
844
869
SourceLoc EllipsisLoc;
@@ -1051,14 +1076,16 @@ ParserResult<TypeRepr> Parser::parseTypeArray(TypeRepr *Base) {
1051
1076
return makeParserResult (ATR);
1052
1077
}
1053
1078
1054
- ParserResult<TypeRepr> Parser::parseTypeCollection () {
1079
+ SyntaxParserResult<TypeSyntax, TypeRepr> Parser::parseTypeCollection () {
1080
+ ParserStatus Status;
1055
1081
// Parse the leading '['.
1056
1082
assert (Tok.is (tok::l_square));
1057
1083
Parser::StructureMarkerRAII parsingCollection (*this , Tok);
1058
1084
SourceLoc lsquareLoc = consumeToken ();
1059
1085
1060
1086
// Parse the element type.
1061
1087
ParserResult<TypeRepr> firstTy = parseType (diag::expected_element_type);
1088
+ Status |= firstTy;
1062
1089
1063
1090
// If there is a ':', this is a dictionary type.
1064
1091
SourceLoc colonLoc;
@@ -1068,36 +1095,57 @@ ParserResult<TypeRepr> Parser::parseTypeCollection() {
1068
1095
1069
1096
// Parse the second type.
1070
1097
secondTy = parseType (diag::expected_dictionary_value_type);
1098
+ Status |= secondTy;
1071
1099
}
1072
1100
1073
1101
// Parse the closing ']'.
1074
1102
SourceLoc rsquareLoc;
1075
- parseMatchingToken (tok::r_square, rsquareLoc,
1076
- colonLoc.isValid ()
1077
- ? diag::expected_rbracket_dictionary_type
1078
- : diag::expected_rbracket_array_type,
1079
- lsquareLoc);
1103
+ if (parseMatchingToken (tok::r_square, rsquareLoc,
1104
+ colonLoc.isValid ()
1105
+ ? diag::expected_rbracket_dictionary_type
1106
+ : diag::expected_rbracket_array_type,
1107
+ lsquareLoc))
1108
+ Status.setIsParseError ();
1080
1109
1081
- if (firstTy. hasCodeCompletion () || secondTy .hasCodeCompletion ())
1082
- return makeParserCodeCompletionStatus () ;
1110
+ if (Status .hasCodeCompletion ())
1111
+ return Status ;
1083
1112
1084
1113
// If we couldn't parse anything for one of the types, propagate the error.
1085
- if (firstTy. isNull () || (colonLoc. isValid () && secondTy. isNull () ))
1114
+ if (Status. isError ( ))
1086
1115
return makeParserError ();
1087
1116
1088
- // Form the dictionary type.
1117
+ TypeRepr *TyR;
1118
+ llvm::Optional<TypeSyntax> SyntaxNode;
1119
+
1089
1120
SourceRange brackets (lsquareLoc, rsquareLoc);
1090
- if (colonLoc.isValid ())
1091
- return makeParserResult (ParserStatus (firstTy) | ParserStatus (secondTy),
1092
- new (Context) DictionaryTypeRepr (firstTy.get (),
1093
- secondTy.get (),
1094
- colonLoc,
1095
- brackets));
1121
+ if (colonLoc.isValid ()) {
1122
+ // Form the dictionary type.
1123
+ TyR = new (Context)
1124
+ DictionaryTypeRepr (firstTy.get (), secondTy.get (), colonLoc, brackets);
1125
+ if (SyntaxContext->isEnabled ()) {
1126
+ DictionaryTypeSyntaxBuilder Builder;
1127
+ Builder
1128
+ .useRightSquareBracket (SyntaxContext->popToken ())
1129
+ .useValueType (SyntaxContext->popIf <TypeSyntax>().getValue ())
1130
+ .useColon (SyntaxContext->popToken ())
1131
+ .useKeyType (SyntaxContext->popIf <TypeSyntax>().getValue ())
1132
+ .useLeftSquareBracket (SyntaxContext->popToken ());
1133
+ SyntaxNode.emplace (Builder.build ());
1134
+ }
1135
+ } else {
1136
+ // Form the array type.
1137
+ TyR = new (Context) ArrayTypeRepr (firstTy.get (), brackets);
1138
+ if (SyntaxContext->isEnabled ()) {
1139
+ ArrayTypeSyntaxBuilder Builder;
1140
+ Builder
1141
+ .useRightSquareBracket (SyntaxContext->popToken ())
1142
+ .useElementType (SyntaxContext->popIf <TypeSyntax>().getValue ())
1143
+ .useLeftSquareBracket (SyntaxContext->popToken ());
1144
+ SyntaxNode.emplace (Builder.build ());
1145
+ }
1146
+ }
1096
1147
1097
- // Form the array type.
1098
- return makeParserResult (firstTy,
1099
- new (Context) ArrayTypeRepr (firstTy.get (),
1100
- brackets));
1148
+ return makeSyntaxResult (Status, SyntaxNode, TyR);
1101
1149
}
1102
1150
1103
1151
bool Parser::isOptionalToken (const Token &T) const {
@@ -1142,19 +1190,37 @@ SourceLoc Parser::consumeImplicitlyUnwrappedOptionalToken() {
1142
1190
1143
1191
// / Parse a single optional suffix, given that we are looking at the
1144
1192
// / question mark.
1145
- ParserResult<OptionalTypeRepr> Parser::parseTypeOptional (TypeRepr *base) {
1193
+ SyntaxParserResult<TypeSyntax, OptionalTypeRepr>
1194
+ Parser::parseTypeOptional (TypeRepr *base) {
1146
1195
SourceLoc questionLoc = consumeOptionalToken ();
1147
- return makeParserResult (new (Context) OptionalTypeRepr (base, questionLoc));
1196
+ auto TyR = new (Context) OptionalTypeRepr (base, questionLoc);
1197
+ llvm::Optional<TypeSyntax> SyntaxNode;
1198
+ if (SyntaxContext->isEnabled ()) {
1199
+ OptionalTypeSyntaxBuilder Builder;
1200
+ Builder
1201
+ .useQuestionMark (SyntaxContext->popToken ())
1202
+ .useWrappedType (SyntaxContext->popIf <TypeSyntax>().getValue ());
1203
+ SyntaxNode.emplace (Builder.build ());
1204
+ }
1205
+ return makeSyntaxResult (SyntaxNode, TyR);
1148
1206
}
1149
1207
1150
1208
// / Parse a single implicitly unwrapped optional suffix, given that we
1151
1209
// / are looking at the exclamation mark.
1152
- ParserResult< ImplicitlyUnwrappedOptionalTypeRepr>
1210
+ SyntaxParserResult<TypeSyntax, ImplicitlyUnwrappedOptionalTypeRepr>
1153
1211
Parser::parseTypeImplicitlyUnwrappedOptional (TypeRepr *base) {
1154
1212
SourceLoc exclamationLoc = consumeImplicitlyUnwrappedOptionalToken ();
1155
- return makeParserResult (
1156
- new (Context) ImplicitlyUnwrappedOptionalTypeRepr (
1157
- base, exclamationLoc));
1213
+ auto TyR =
1214
+ new (Context) ImplicitlyUnwrappedOptionalTypeRepr (base, exclamationLoc);
1215
+ llvm::Optional<TypeSyntax> SyntaxNode;
1216
+ if (SyntaxContext->isEnabled ()) {
1217
+ ImplicitlyUnwrappedOptionalTypeSyntaxBuilder Builder;
1218
+ Builder
1219
+ .useExclamationMark (SyntaxContext->popToken ())
1220
+ .useWrappedType (SyntaxContext->popIf <TypeSyntax>().getValue ());
1221
+ SyntaxNode.emplace (Builder.build ());
1222
+ }
1223
+ return makeSyntaxResult (SyntaxNode, TyR);
1158
1224
}
1159
1225
1160
1226
// ===----------------------------------------------------------------------===//
0 commit comments