@@ -70,19 +70,6 @@ enum ASTNode {
7070 fatalError ( " Must be expr, stmt, or decl. " )
7171 }
7272 }
73-
74- var raw : UnsafeMutableRawPointer {
75- switch self {
76- case . expr( let e) :
77- return e. raw
78- case . stmt( let s) :
79- return s. raw
80- case . decl( let d) :
81- return d. raw
82- case . type( let t) :
83- return t. raw
84- }
85- }
8673}
8774
8875/// Little utility wrapper that lets us have some mutable state within
@@ -428,16 +415,16 @@ public func buildTopLevelASTNodes(
428415
429416/// Generate an AST node at the given source location. Returns the generated
430417/// ASTNode and mutate the pointee of `endLocPtr` to the end of the node.
431- private func _build< Node: SyntaxProtocol > (
432- kind : Node . Type ,
418+ private func _build< Node: SyntaxProtocol , Result > (
419+ generator : ( ASTGenVisitor ) -> ( Node ) -> Result ,
433420 diagEngine: BridgedDiagnosticEngine ,
434421 sourceFilePtr: UnsafeRawPointer ,
435422 sourceLoc: BridgedSourceLoc ,
436423 declContext: BridgedDeclContext ,
437424 astContext: BridgedASTContext ,
438425 legacyParser: BridgedLegacyParser ,
439426 endLocPtr: UnsafeMutablePointer < BridgedSourceLoc >
440- ) -> UnsafeMutableRawPointer ? {
427+ ) -> Result ? {
441428 let sourceFile = sourceFilePtr. assumingMemoryBound ( to: ExportedSourceFile . self)
442429
443430 // Find the type syntax node.
@@ -458,13 +445,13 @@ private func _build<Node: SyntaxProtocol>(
458445 endLocPtr. pointee = sourceLoc. advanced ( by: node. totalLength. utf8Length)
459446
460447 // Convert the syntax node.
461- return ASTGenVisitor (
448+ return generator ( ASTGenVisitor (
462449 diagnosticEngine: diagEngine,
463450 sourceBuffer: sourceFile. pointee. buffer,
464451 declContext: declContext,
465452 astContext: astContext,
466453 legacyParser: legacyParser
467- ) . generate ( Syntax ( node) ) . raw
454+ ) ) ( node)
468455}
469456
470457@_cdecl ( " swift_ASTGen_buildTypeRepr " )
@@ -479,15 +466,15 @@ func buildTypeRepr(
479466 endLocPtr: UnsafeMutablePointer < BridgedSourceLoc >
480467) -> UnsafeMutableRawPointer ? {
481468 return _build (
482- kind : TypeSyntax . self ,
469+ generator : ASTGenVisitor . generate ( type : ) ,
483470 diagEngine: diagEngine,
484471 sourceFilePtr: sourceFilePtr,
485472 sourceLoc: sourceLoc,
486473 declContext: declContext,
487474 astContext: astContext,
488475 legacyParser: legacyParser,
489476 endLocPtr: endLocPtr
490- )
477+ ) ? . raw
491478}
492479
493480@_cdecl ( " swift_ASTGen_buildDecl " )
@@ -502,15 +489,15 @@ func buildDecl(
502489 endLocPtr: UnsafeMutablePointer < BridgedSourceLoc >
503490) -> UnsafeMutableRawPointer ? {
504491 return _build (
505- kind : DeclSyntax . self ,
492+ generator : ASTGenVisitor . generate ( decl : ) ,
506493 diagEngine: diagEngine,
507494 sourceFilePtr: sourceFilePtr,
508495 sourceLoc: sourceLoc,
509496 declContext: declContext,
510497 astContext: astContext,
511498 legacyParser: legacyParser,
512499 endLocPtr: endLocPtr
513- )
500+ ) ? . raw
514501}
515502
516503@_cdecl ( " swift_ASTGen_buildExpr " )
@@ -525,15 +512,15 @@ func buildExpr(
525512 endLocPtr: UnsafeMutablePointer < BridgedSourceLoc >
526513) -> UnsafeMutableRawPointer ? {
527514 return _build (
528- kind : ExprSyntax . self ,
515+ generator : ASTGenVisitor . generate ( expr : ) ,
529516 diagEngine: diagEngine,
530517 sourceFilePtr: sourceFilePtr,
531518 sourceLoc: sourceLoc,
532519 declContext: declContext,
533520 astContext: astContext,
534521 legacyParser: legacyParser,
535522 endLocPtr: endLocPtr
536- )
523+ ) ? . raw
537524}
538525
539526@_cdecl ( " swift_ASTGen_buildStmt " )
@@ -548,13 +535,13 @@ func buildStmt(
548535 endLocPtr: UnsafeMutablePointer < BridgedSourceLoc >
549536) -> UnsafeMutableRawPointer ? {
550537 return _build (
551- kind : StmtSyntax . self ,
538+ generator : ASTGenVisitor . generate ( stmt : ) ,
552539 diagEngine: diagEngine,
553540 sourceFilePtr: sourceFilePtr,
554541 sourceLoc: sourceLoc,
555542 declContext: declContext,
556543 astContext: astContext,
557544 legacyParser: legacyParser,
558545 endLocPtr: endLocPtr
559- )
546+ ) ? . raw
560547}
0 commit comments