Skip to content

Commit 4654561

Browse files
[swiftsrc2cpg] Fix remaining validator error (#5850)
1 parent a198426 commit 4654561

File tree

1 file changed

+61
-56
lines changed

1 file changed

+61
-56
lines changed

joern-cli/frontends/swiftsrc2cpg/src/main/scala/io/joern/swiftsrc2cpg/astcreation/AstForDeclSyntaxCreator.scala

Lines changed: 61 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ trait AstForDeclSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
1616
this: AstCreator =>
1717

1818
protected type TypeDeclLike = ClassDeclSyntax | ProtocolDeclSyntax | StructDeclSyntax | EnumDeclSyntax |
19-
ActorDeclSyntax | TypeAliasDeclSyntax | AssociatedTypeDeclSyntax
19+
ActorDeclSyntax
2020

2121
protected type FunctionDeclLike = FunctionDeclSyntax | AccessorDeclSyntax | InitializerDeclSyntax |
2222
DeinitializerDeclSyntax | ClosureExprSyntax | SubscriptDeclSyntax
@@ -50,7 +50,10 @@ trait AstForDeclSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
5050
case _ => false
5151
}
5252

53-
private def declMembers(decl: TypeDeclLike, withConstructor: Boolean = true): Seq[DeclSyntax] = {
53+
private def declMembers(
54+
decl: TypeDeclLike | TypeAliasDeclSyntax | AssociatedTypeDeclSyntax,
55+
withConstructor: Boolean = true
56+
): Seq[DeclSyntax] = {
5457
val memberBlock = decl match {
5558
case c: ClassDeclSyntax => Option(c.memberBlock)
5659
case p: ProtocolDeclSyntax => Option(p.memberBlock)
@@ -125,7 +128,7 @@ trait AstForDeclSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
125128
}
126129

127130
private def createFakeConstructor(
128-
node: TypeDeclLike,
131+
node: TypeDeclLike | TypeAliasDeclSyntax | AssociatedTypeDeclSyntax,
129132
typeDeclNode: NewTypeDecl,
130133
methodBlockContent: List[DeclSyntax]
131134
): Unit = {
@@ -198,36 +201,33 @@ trait AstForDeclSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
198201

199202
private def astForDeclMember(node: DeclSyntax, typeDeclNode: NewTypeDecl): Ast = {
200203
node match {
201-
case d: FunctionDeclLike =>
202-
val ast = astForFunctionLike(d, List.empty, None)
203-
Ast.storeInDiffGraph(ast, diffGraph)
204-
ast.root.foreach(r => diffGraph.addEdge(typeDeclNode, r, EdgeTypes.AST))
205-
Ast()
206-
case ifConf: IfConfigDeclSyntax =>
207-
val declElements = declSyntaxFromIfConfigDeclSyntax(ifConf)
204+
case funcDecl: FunctionDeclLike =>
205+
astForFunctionLike(funcDecl, List.empty, Some(typeDeclNode))
206+
case ifConfigDecl: IfConfigDeclSyntax =>
207+
val declElements = declSyntaxFromIfConfigDeclSyntax(ifConfigDecl)
208208
declElements.foldLeft(Ast()) { (ast, decl) => ast.merge(astForDeclMember(decl, typeDeclNode)) }
209-
case _: (ActorDeclSyntax | AssociatedTypeDeclSyntax | ClassDeclSyntax | EnumDeclSyntax | ExtensionDeclSyntax |
210-
ImportDeclSyntax | ProtocolDeclSyntax | StructDeclSyntax | MacroDeclSyntax | MacroExpansionDeclSyntax |
211-
OperatorDeclSyntax | PoundSourceLocationSyntax | PrecedenceGroupDeclSyntax | SubscriptDeclSyntax |
212-
TypeAliasDeclSyntax) =>
213-
val ast = astForNode(node)
214-
Ast.storeInDiffGraph(ast, diffGraph)
215-
ast.root.foreach(r => diffGraph.addEdge(typeDeclNode, r, EdgeTypes.AST))
216-
Ast()
217-
case d: EnumCaseDeclSyntax =>
218-
val ast = astForNode(d)
219-
d.elements.children.foreach { c =>
209+
case typeDeclLike: TypeDeclLike =>
210+
Ast(refForTypeDeclSyntax(typeDeclLike))
211+
case extensionDecl: ExtensionDeclSyntax =>
212+
Ast(refForExtensionDeclSyntax(extensionDecl))
213+
case _: (ImportDeclSyntax | MacroDeclSyntax | MacroExpansionDeclSyntax | OperatorDeclSyntax |
214+
PoundSourceLocationSyntax | PrecedenceGroupDeclSyntax | SubscriptDeclSyntax | TypeAliasDeclSyntax |
215+
AssociatedTypeDeclSyntax) =>
216+
astForNode(node)
217+
case enumCaseDecl: EnumCaseDeclSyntax =>
218+
val ast = astForNode(enumCaseDecl)
219+
enumCaseDecl.elements.children.foreach { c =>
220220
val cCode = code(c.name)
221221
val tpeFromTypeMap = fullnameProvider.typeFullname(c)
222-
val typeFullName = tpeFromTypeMap.getOrElse(typeNameForDeclSyntax(d))
222+
val typeFullName = tpeFromTypeMap.getOrElse(typeNameForDeclSyntax(enumCaseDecl))
223223
val memberNode_ = memberNode(c, cCode, cCode, typeFullName)
224224
registerType(typeFullName)
225225
scope.addVariable(cCode, memberNode_, typeFullName, VariableScopeManager.ScopeType.TypeDeclScope)
226226
diffGraph.addEdge(typeDeclNode, memberNode_, EdgeTypes.AST)
227227
}
228228
ast
229-
case d: VariableDeclSyntax =>
230-
d.bindings.children.foreach { c =>
229+
case variableDecl: VariableDeclSyntax =>
230+
variableDecl.bindings.children.foreach { c =>
231231
val cCode = code(c.pattern)
232232
val tpeFromTypeMap = fullnameProvider.typeFullname(c)
233233
val typeFullName = tpeFromTypeMap.getOrElse(
@@ -238,24 +238,24 @@ trait AstForDeclSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
238238
scope.addVariable(cCode, memberNode_, typeFullName, VariableScopeManager.ScopeType.TypeDeclScope)
239239
diffGraph.addEdge(typeDeclNode, memberNode_, EdgeTypes.AST)
240240
}
241-
astForVariableDeclSyntax(d, true)
241+
astForVariableDeclSyntax(variableDecl, true)
242242
case other => notHandledYet(other)
243243
}
244244
}
245245

246-
private def findDeclConstructor(decl: TypeDeclLike): Option[DeclSyntax] =
246+
private def findDeclConstructor(
247+
decl: TypeDeclLike | TypeAliasDeclSyntax | AssociatedTypeDeclSyntax
248+
): Option[DeclSyntax] =
247249
declMembers(decl).find(isConstructor)
248250

249251
private def createDeclConstructor(
250-
node: TypeDeclLike,
252+
node: TypeDeclLike | TypeAliasDeclSyntax | AssociatedTypeDeclSyntax,
251253
typeDeclNode: NewTypeDecl,
252254
constructorContent: List[DeclSyntax]
253255
): Unit =
254256
findDeclConstructor(node) match {
255257
case Some(constructor: InitializerDeclSyntax) =>
256-
val ast = astForFunctionLike(constructor, methodBlockContent = constructorContent, Some(typeDeclNode))
257-
Ast.storeInDiffGraph(ast, diffGraph)
258-
ast.root.foreach(r => diffGraph.addEdge(typeDeclNode, r, EdgeTypes.AST))
258+
astForFunctionLike(constructor, methodBlockContent = constructorContent, Some(typeDeclNode))
259259
case _ =>
260260
createFakeConstructor(node, typeDeclNode, methodBlockContent = constructorContent)
261261
}
@@ -271,7 +271,9 @@ trait AstForDeclSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
271271
}
272272
}
273273

274-
private def astForDeclAttributes(node: TypeDeclLike | VariableDeclSyntax): Seq[Ast] = {
274+
private def astForDeclAttributes(
275+
node: TypeDeclLike | VariableDeclSyntax | AssociatedTypeDeclSyntax | TypeAliasDeclSyntax
276+
): Seq[Ast] = {
275277
node match {
276278
case c: ClassDeclSyntax => c.attributes.children.map(astForNode)
277279
case p: ProtocolDeclSyntax => p.attributes.children.map(astForNode)
@@ -314,7 +316,7 @@ trait AstForDeclSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
314316
diffGraph.addEdge(typeDeclNode, methodNode_, EdgeTypes.AST)
315317
}
316318

317-
private def astForTypeDeclSyntax(node: TypeDeclLike): Ast = {
319+
private def refForTypeDeclSyntax(node: TypeDeclLike): NewTypeRef = {
318320
// TODO:
319321
// - handle genericParameterClause
320322
// - handle genericWhereClause
@@ -371,7 +373,7 @@ trait AstForDeclSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
371373
Ast.storeInDiffGraph(Ast(typeDeclNode_), diffGraph)
372374
diffGraph.addEdge(methodAstParentStack.head, typeDeclNode_, EdgeTypes.AST)
373375

374-
Ast(typeRefNode_)
376+
typeRefNode_
375377
}
376378

377379
private def astForDeinitializerDeclSyntax(node: DeinitializerDeclSyntax): Ast = {
@@ -419,7 +421,9 @@ trait AstForDeclSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
419421
}
420422
}
421423

422-
private def inheritsFrom(node: TypeDeclLike | ExtensionDeclSyntax): Seq[String] = {
424+
private def inheritsFrom(
425+
node: TypeDeclLike | ExtensionDeclSyntax | TypeAliasDeclSyntax | AssociatedTypeDeclSyntax
426+
): Seq[String] = {
423427
val inheritFullNames = fullnameProvider.inheritsFor(node) match {
424428
case fullNames if fullNames.nonEmpty => fullNames
425429
case _ =>
@@ -443,7 +447,7 @@ trait AstForDeclSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
443447
inheritFullNames
444448
}
445449

446-
private def astForExtensionDeclSyntax(node: ExtensionDeclSyntax): Ast = {
450+
private def refForExtensionDeclSyntax(node: ExtensionDeclSyntax): NewTypeRef = {
447451
val TypeInfo(typeName, typeFullName) = typeNameInfoForDeclSyntax(node)
448452
val (extendedTypeName, extendedTypeFullName) = fullnameProvider.typeFullname(node) match {
449453
case Some(tpe) =>
@@ -502,10 +506,12 @@ trait AstForDeclSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
502506
ast.root.foreach(r => diffGraph.addEdge(methodAstParentStack.head, r, EdgeTypes.AST))
503507
}
504508

505-
Ast(typeRefNode_)
509+
typeRefNode_
506510
}
507511

508-
private def modifiersForDecl(node: TypeDeclLike | EnumCaseDeclSyntax): Seq[NewModifier] = {
512+
private def modifiersForDecl(
513+
node: TypeDeclLike | EnumCaseDeclSyntax | AssociatedTypeDeclSyntax | TypeAliasDeclSyntax
514+
): Seq[NewModifier] = {
509515
val modifierList = node match {
510516
case c: ClassDeclSyntax => c.modifiers.children
511517
case p: ProtocolDeclSyntax => p.modifiers.children
@@ -612,10 +618,10 @@ trait AstForDeclSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
612618
else in
613619
}
614620

615-
private def astForAccessorInSubscript(
621+
private def handleForAccessorInSubscript(
616622
node: SubscriptDeclSyntax,
617623
handleAccessor: (AccessorDeclSyntax, String, String, String, Seq[SwiftNode]) => Unit
618-
): Ast = {
624+
): Unit = {
619625
val methodInfo = methodInfoForFunctionDeclLike(node)
620626
val MethodInfo(methodName, _, signature, returnType) = methodInfo
621627
val paramClause = replaceSuffixIfEndsWith(signature, s"->$returnType", "")
@@ -627,15 +633,14 @@ trait AstForDeclSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
627633
case _ => Seq.empty
628634
}
629635
accessors.foreach(handleAccessor(_, "", returnType, subscriptSignaturePrefix, parameters))
630-
Ast()
631636
}
632637

633-
private def astForAccessorInSubscript(node: SubscriptDeclSyntax): Ast = {
634-
astForAccessorInSubscript(node, astForAccessor)
638+
private def handleAccessorInSubscript(node: SubscriptDeclSyntax): Unit = {
639+
handleForAccessorInSubscript(node, astForAccessor)
635640
}
636641

637-
private def astForAccessorInSubscriptInExtension(node: SubscriptDeclSyntax): Ast = {
638-
astForAccessorInSubscript(node, astForAccessorInExtension)
642+
private def handleAccessorInSubscriptInExtension(node: SubscriptDeclSyntax): Unit = {
643+
handleForAccessorInSubscript(node, astForAccessorInExtension)
639644
}
640645

641646
protected def astForFunctionLike(
@@ -644,7 +649,8 @@ trait AstForDeclSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
644649
typeDecl: Option[NewTypeDecl]
645650
): Ast = {
646651
if (hasNestedAccessor(node)) {
647-
return astForAccessorInSubscript(node.asInstanceOf[SubscriptDeclSyntax])
652+
handleAccessorInSubscript(node.asInstanceOf[SubscriptDeclSyntax])
653+
return Ast()
648654
}
649655

650656
val methodInfo = methodInfoForFunctionDeclLike(node)
@@ -691,11 +697,13 @@ trait AstForDeclSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
691697
case Some(ref) =>
692698
createFunctionTypeAndTypeDecl(node, methodNode_)
693699
Ast.storeInDiffGraph(astForMethod, diffGraph)
694-
diffGraph.addEdge(methodAstParentStack.head, methodNode_, EdgeTypes.AST)
700+
diffGraph.addEdge(typeDecl.getOrElse(methodAstParentStack.head), methodNode_, EdgeTypes.AST)
695701
Ast(ref)
696-
case None =>
697-
val functionBindingAst = createFunctionBinding(methodNode_)
698-
astForMethod.merge(functionBindingAst)
702+
case _ =>
703+
Ast.storeInDiffGraph(astForMethod, diffGraph)
704+
Ast.storeInDiffGraph(createFunctionBinding(methodNode_), diffGraph)
705+
diffGraph.addEdge(typeDecl.getOrElse(methodAstParentStack.head), methodNode_, EdgeTypes.AST)
706+
Ast()
699707
}
700708
}
701709

@@ -784,7 +792,8 @@ trait AstForDeclSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
784792

785793
private def astForFunctionInExtension(node: FunctionDeclLike): Ast = {
786794
if (hasNestedAccessor(node)) {
787-
return astForAccessorInSubscriptInExtension(node.asInstanceOf[SubscriptDeclSyntax])
795+
handleAccessorInSubscriptInExtension(node.asInstanceOf[SubscriptDeclSyntax])
796+
return Ast()
788797
}
789798

790799
val methodInfo = methodInfoForFunctionDeclLike(node)
@@ -1330,14 +1339,12 @@ trait AstForDeclSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
13301339
private def astForMissingDeclSyntax(@unused node: MissingDeclSyntax): Ast = Ast()
13311340

13321341
protected def astForDeclSyntax(declSyntax: DeclSyntax): Ast = declSyntax match {
1333-
case node: ActorDeclSyntax => astForTypeDeclSyntax(node)
1342+
case node: TypeDeclLike => Ast(refForTypeDeclSyntax(node))
13341343
case node: AssociatedTypeDeclSyntax => astForAssociatedTypeDeclSyntax(node)
1335-
case node: ClassDeclSyntax => astForTypeDeclSyntax(node)
13361344
case node: DeinitializerDeclSyntax => astForDeinitializerDeclSyntax(node)
13371345
case node: EditorPlaceholderDeclSyntax => astForEditorPlaceholderDeclSyntax(node)
13381346
case node: EnumCaseDeclSyntax => astForEnumCaseDeclSyntax(node)
1339-
case node: EnumDeclSyntax => astForTypeDeclSyntax(node)
1340-
case node: ExtensionDeclSyntax => astForExtensionDeclSyntax(node)
1347+
case node: ExtensionDeclSyntax => Ast(refForExtensionDeclSyntax(node))
13411348
case node: FunctionDeclSyntax => astForFunctionDeclSyntax(node)
13421349
case node: IfConfigDeclSyntax => astForIfConfigDeclSyntax(node)
13431350
case node: ImportDeclSyntax => astForImportDeclSyntax(node)
@@ -1348,8 +1355,6 @@ trait AstForDeclSyntaxCreator(implicit withSchemaValidation: ValidationMode) {
13481355
case node: OperatorDeclSyntax => astForOperatorDeclSyntax(node)
13491356
case node: PoundSourceLocationSyntax => astForPoundSourceLocationSyntax(node)
13501357
case node: PrecedenceGroupDeclSyntax => astForPrecedenceGroupDeclSyntax(node)
1351-
case node: ProtocolDeclSyntax => astForTypeDeclSyntax(node)
1352-
case node: StructDeclSyntax => astForTypeDeclSyntax(node)
13531358
case node: SubscriptDeclSyntax => astForSubscriptDeclSyntax(node)
13541359
case node: TypeAliasDeclSyntax => astForTypeAliasDeclSyntax(node)
13551360
case node: VariableDeclSyntax => astForVariableDeclSyntax(node)

0 commit comments

Comments
 (0)