Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TY&RES: implement hygiene & infer types in macros
- Loading branch information
Showing
17 changed files
with
490 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...main/kotlin/org/rust/lang/core/psi/RsWithMacroExpansionsRecursiveElementWalkingVisitor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Use of this source code is governed by the MIT license that can be | ||
* found in the LICENSE file. | ||
*/ | ||
|
||
package org.rust.lang.core.psi | ||
|
||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.PsiRecursiveElementWalkingVisitor | ||
import org.rust.lang.core.psi.ext.expansion | ||
|
||
abstract class RsWithMacroExpansionsRecursiveElementWalkingVisitor : PsiRecursiveElementWalkingVisitor() { | ||
override fun visitElement(element: PsiElement) { | ||
if (element is RsMacroCall && element.macroArgument != null) { | ||
val expansion = element.expansion ?: return | ||
for (expandedElement in expansion.elements) { | ||
visitElement(expandedElement) | ||
} | ||
} else { | ||
super.visitElement(element) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Use of this source code is governed by the MIT license that can be | ||
* found in the LICENSE file. | ||
*/ | ||
|
||
package org.rust.lang.core.psi.ext | ||
|
||
import org.rust.lang.core.macros.RsExpandedElement | ||
import org.rust.lang.core.psi.RsBlock | ||
import org.rust.lang.core.psi.RsExpr | ||
import org.rust.lang.core.psi.RsMacroCall | ||
import org.rust.lang.core.psi.RsStmt | ||
|
||
/** | ||
* Can contain [RsStmt]s and [RsExpr]s (which are equivalent to RsExprStmt(RsExpr)) | ||
*/ | ||
val RsBlock.expandedStmts: List<RsExpandedElement> | ||
get() { | ||
val stmts = mutableListOf<RsExpandedElement>() | ||
processExpandedStmtsInternal { stmt -> | ||
stmts.add(stmt) | ||
false | ||
} | ||
return stmts | ||
} | ||
|
||
private val RsBlock.stmtsAndMacros: Sequence<RsElement> | ||
get() { | ||
val stub = greenStub | ||
return if (stub != null) { | ||
stub.childrenStubs.asSequence().map { it.psi } | ||
} else { | ||
childrenWithLeaves | ||
}.filterIsInstance<RsElement>() | ||
} | ||
|
||
private fun RsBlock.processExpandedStmtsInternal(processor: (RsExpandedElement) -> Boolean): Boolean { | ||
return stmtsAndMacros.any { it.processStmt(processor) } | ||
} | ||
|
||
private fun RsElement.processStmt(processor: (RsExpandedElement) -> Boolean) = when (this) { | ||
is RsMacroCall -> processExpansionRecursively(processor) | ||
is RsExpandedElement -> processor(this) | ||
else -> false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
* Use of this source code is governed by the MIT license that can be | ||
* found in the LICENSE file. | ||
*/ | ||
|
||
package org.rust.lang.core.psi.ext | ||
|
||
import com.intellij.lang.ASTNode | ||
import com.intellij.psi.PsiElement | ||
import org.rust.lang.core.macros.RsExpandedElement | ||
import org.rust.lang.core.psi.RsStmt | ||
|
||
abstract class RsStmtMixin(node: ASTNode) : RsElementImpl(node), RsStmt { | ||
override fun getContext(): PsiElement? = RsExpandedElement.getContextImpl(this) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.