Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ class SwiftSrc2Cpg extends X2CpgFrontend {
val astCreationPass = new AstCreationPass(cpg, astGenResult, config, report)(config.schemaValidation)
astCreationPass.createAndApply()

SwiftTypeNodePass.withRegisteredTypes(astCreationPass.typesSeen(), cpg).createAndApply()
new BuiltinTypesPass(cpg).createAndApply()
new ExtensionCallPass(cpg, astCreationPass.extensionMethodFullNameMapping()).createAndApply()
new ExtensionInheritancePass(cpg, astCreationPass.extensionInherits()).createAndApply()

new ExtensionInheritancePass(cpg).createAndApply()
SwiftTypeNodePass.withRegisteredTypes(astCreationPass.typesSeen(), cpg).createAndApply()
new MetaDataPass(cpg, hash, config.inputPath).createAndApply()
new ConfigFileCreationPass(cpg, config).createAndApply()
new DependenciesPass(cpg).createAndApply()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import io.joern.swiftsrc2cpg.parser.SwiftJsonParser.ParseResult
import io.joern.swiftsrc2cpg.parser.SwiftNodeSyntax.*
import io.joern.swiftsrc2cpg.utils.FullnameProvider
import io.joern.swiftsrc2cpg.utils.SwiftTypesProvider.SwiftFileLocalTypeMapping
import io.joern.x2cpg.datastructures.Global
import io.joern.x2cpg.datastructures.Stack.*
import io.joern.x2cpg.frontendspecific.swiftsrc2cpg.Defines
import io.joern.x2cpg.{Ast, AstCreatorBase, ValidationMode}
Expand All @@ -18,7 +17,7 @@ import scala.collection.mutable

class AstCreator(
val config: Config,
val global: Global,
val global: SwiftSrcGlobal,
val parserResult: ParseResult,
val typeMap: SwiftFileLocalTypeMapping
)(implicit withSchemaValidation: ValidationMode)
Expand Down Expand Up @@ -73,7 +72,7 @@ class AstCreator(
val fakeGlobalMethod =
methodNode(ast, name, name, fullName, None, path, Option(NodeTypes.TYPE_DECL), Option(fullName))
methodAstParentStack.push(fakeGlobalMethod)
scope.pushNewTypeDeclScope(fullName)
scope.pushNewTypeDeclScope(name, fullName)
scope.pushNewMethodScope(fullName, name, fakeGlobalMethod, None)
val sourceFileAst = astForNode(ast)
val methodReturn = methodReturnNode(ast, Defines.Any)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ object AstCreatorHelper {
def cleanType(rawType: String): String = {
if (rawType == Defines.Any) return rawType
val normalizedTpe = StringUtils.normalizeSpace(rawType.stripSuffix(" ()"))
stripGenerics(normalizedTpe) match {
val tpe = stripGenerics(normalizedTpe) match {
// Empty or problematic types
case "" => Defines.Any
case t if t.contains("?") => Defines.Any
Expand Down Expand Up @@ -99,6 +99,11 @@ object AstCreatorHelper {
// Default case
case typeStr => typeStr
}
if (tpe.startsWith("@")) {
tpe.substring(tpe.indexOf(" ") + 1)
} else {
tpe
}
}

}
Expand Down Expand Up @@ -167,20 +172,25 @@ trait AstCreatorHelper(implicit withSchemaValidation: ValidationMode) { this: As
if (scope.variableIsInTypeDeclScope(identifierName)) {
// we found it as member of the surrounding type decl
// (Swift does not allow to access any member / function of the outer class instance)
val tpe = scope.typeDeclFullNameForMember(identifierName).getOrElse(typeForSelfExpression())
val tpe = scope.typeDeclFullNameForMember(identifierName).getOrElse(fullNameOfEnclosingTypeDecl())
val selfNode = identifierNode(node, "self", "self", tpe)
scope.addVariableReference("self", selfNode, selfNode.typeFullName, EvaluationStrategies.BY_REFERENCE)

val variableOption = scope.lookupVariable(identifierName)
val callTpe = variableOption.map(_._2).getOrElse(Defines.Any)
fieldAccessAst(node, node, Ast(selfNode), s"self.$identifierName", identifierName, callTpe)
} else {
if (config.swiftBuild && scope.lookupVariable(identifierName).isEmpty) {
val tpe = typeForSelfExpression()
if (
config.swiftBuild &&
scope.lookupVariable(identifierName).isEmpty &&
fullnameProvider.declFullname(node).nonEmpty
) {
val tpe = fullNameOfEnclosingTypeDecl()
val selfNode = identifierNode(node, "self", "self", tpe)
scope.addVariableReference("self", selfNode, selfNode.typeFullName, EvaluationStrategies.BY_REFERENCE)

val callTpe = fullnameProvider.typeFullname(node).getOrElse(Defines.Any)
registerType(callTpe)
fieldAccessAst(node, node, Ast(selfNode), s"self.$identifierName", identifierName, callTpe)
} else {
// otherwise it must come from a variable (potentially captured from an outer scope)
Expand Down Expand Up @@ -231,7 +241,7 @@ trait AstCreatorHelper(implicit withSchemaValidation: ValidationMode) { this: As
case _: DeinitializerDeclSyntax =>
Defines.Void
case _: InitializerDeclSyntax =>
ReturnTypeMatcher.findFirstMatchIn(signature).map(_.group(2)).getOrElse(astParentInfo()._2)
ReturnTypeMatcher.findFirstMatchIn(signature).map(_.group(2)).getOrElse(fullNameOfEnclosingTypeDecl())
case _ =>
ReturnTypeMatcher.findFirstMatchIn(signature).map(_.group(2)).getOrElse(Defines.Any)
}
Expand All @@ -246,7 +256,7 @@ trait AstCreatorHelper(implicit withSchemaValidation: ValidationMode) { this: As
case a: AccessorDeclSyntax =>
(Defines.Any, Defines.Any)
case i: InitializerDeclSyntax =>
val (_, returnType) = astParentInfo()
val returnType = fullNameOfEnclosingTypeDecl()
(s"${paramSignature(i.signature.parameterClause)}->$returnType", returnType)
case _: DeinitializerDeclSyntax =>
val returnType = Defines.Any
Expand All @@ -272,7 +282,8 @@ trait AstCreatorHelper(implicit withSchemaValidation: ValidationMode) { this: As
}

case class MethodInfo(name: String, fullName: String, signature: String, returnType: String) {
val fullNameAndSignature: String = s"$fullName:$signature"
val fullNameAndSignature: String = s"$fullName:$signature"
val fullNameAndSignatureExt: String = fullNameAndSignature.replaceFirst(s"\\.$name:", s"<extension>.$name:")
}

case class TypeInfo(name: String, fullName: String)
Expand Down Expand Up @@ -365,7 +376,9 @@ trait AstCreatorHelper(implicit withSchemaValidation: ValidationMode) { this: As
val fullName = declFullname.substring(0, declFullname.indexOf("("))
fullName.substring(0, fullName.lastIndexOf("."))
} else declFullname
registerType(cleanedFullName)
if (!node.isInstanceOf[ExtensionDeclSyntax]) {
registerType(cleanedFullName)
}
TypeInfo(name, cleanedFullName)
case None =>
val (_, declFullname) = calcNameAndFullName(name)
Expand Down
Loading