Skip to content

Commit

Permalink
Generic setCodeAndRegion
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Jul 19, 2023
1 parent 6ca5511 commit 81ec46b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ abstract class Handler<ResultNode : Node, HandlerNode, L : LanguageFrontend<Hand
protected val configConstructor: Supplier<ResultNode>,
/** Returns the frontend which used this handler. */
var frontend: L
) : LanguageProvider, CodeAndLocationProvider, ScopeProvider, NamespaceProvider, ContextProvider {
) :
LanguageProvider,
CodeAndLocationProvider<HandlerNode>,
ScopeProvider,
NamespaceProvider,
ContextProvider {
protected val map = HashMap<Class<out HandlerNode>, HandlerInterface<ResultNode, HandlerNode>>()
private val typeOfT: Class<*>?

Expand Down Expand Up @@ -159,7 +164,7 @@ abstract class Handler<ResultNode : Node, HandlerNode, L : LanguageFrontend<Hand
override val language: Language<L>
get() = frontend.language as Language<L>

override fun setCodeAndLocation(cpgNode: Node?, astNode: Any?) {
override fun setCodeAndLocation(cpgNode: Node, astNode: HandlerNode) {
frontend.setCodeAndLocation(cpgNode, astNode)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ abstract class LanguageFrontend<in AstNode, TypeNode>(
final override var ctx: TranslationContext,
) :
ProcessedListener(),
CodeAndLocationProvider,
CodeAndLocationProvider<AstNode>,
LanguageProvider,
ScopeProvider,
NamespaceProvider,
Expand Down Expand Up @@ -127,19 +127,17 @@ abstract class LanguageFrontend<in AstNode, TypeNode>(
*/
abstract fun locationOf(astNode: AstNode): PhysicalLocation?

override fun setCodeAndLocation(cpgNode: Node?, astNode: Any?) {
if (cpgNode != null && astNode != null) {
if (config.codeInNodes) {
// only set code, if it's not already set or empty
val code = codeOf(astNode as AstNode)
if (code != null) {
cpgNode.code = code
} else {
log.warn("Unexpected: No code for node {}", astNode)
}
override fun setCodeAndLocation(cpgNode: Node, astNode: AstNode) {
if (config.codeInNodes) {
// only set code, if it's not already set or empty
val code = codeOf(astNode)
if (code != null) {
cpgNode.code = code
} else {
log.warn("Unexpected: No code for node {}", astNode)
}
cpgNode.location = locationOf(astNode as AstNode)
}
cpgNode.location = locationOf(astNode)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ interface LanguageProvider : MetadataProvider {
* This interface denotes that the class is able to provide source code and location information for
* a specific node and set it using the [setCodeAndLocation] function.
*/
interface CodeAndLocationProvider : MetadataProvider {
fun setCodeAndLocation(cpgNode: Node?, astNode: Any?)
interface CodeAndLocationProvider<in AstNode> : MetadataProvider {
fun setCodeAndLocation(cpgNode: Node, astNode: AstNode)
}

/**
Expand Down Expand Up @@ -103,8 +103,8 @@ fun Node.applyMetadata(
localNameOnly: Boolean = false,
defaultNamespace: Name? = null,
) {
if (provider is CodeAndLocationProvider) {
provider.setCodeAndLocation(this, rawNode)
if (provider is CodeAndLocationProvider<*> && rawNode != null) {
(provider as CodeAndLocationProvider<Any>).setCodeAndLocation(this, rawNode)
}

if (provider is LanguageProvider) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ class StatementHandler(lang: JavaLanguageFrontend?) :
val expression = frontend.expressionHandler.handle(stmt.asExpressionStmt().expression)

// update expression's code and location to match the statement
frontend.setCodeAndLocation(expression, stmt)
if (expression != null) {
frontend.setCodeAndLocation(expression, stmt)
}

return expression
}

Expand Down Expand Up @@ -188,7 +191,9 @@ class StatementHandler(lang: JavaLanguageFrontend?) :
val s = frontend.expressionHandler.handle(initExpr)

// make sure location is set
frontend.setCodeAndLocation(s, initExpr)
if (s != null) {
frontend.setCodeAndLocation(s, initExpr)
}
s?.let { initExprList.addExpression(it) }

// can not update location
Expand Down Expand Up @@ -241,7 +246,9 @@ class StatementHandler(lang: JavaLanguageFrontend?) :
val s = frontend.expressionHandler.handle(updateExpr)

// make sure location is set
frontend.setCodeAndLocation(s, updateExpr)
if (s != null) {
frontend.setCodeAndLocation(s, updateExpr)
}
s?.let { iterationExprList.addExpression(it) }

// can not update location
Expand Down

0 comments on commit 81ec46b

Please sign in to comment.