php-parser/pkg/visitor/nsresolver/namespace_resolver.go

442 lines
10 KiB
Go
Raw Normal View History

2020-05-13 18:05:15 +00:00
// Package visitor contains walker.visitor implementations
2020-12-28 19:13:08 +00:00
package nsresolver
2020-05-13 18:05:15 +00:00
import (
"errors"
"strings"
2023-12-09 21:36:19 +00:00
"git.maride.cc/maride/php-parser/pkg/ast"
"git.maride.cc/maride/php-parser/pkg/visitor"
2020-05-13 18:05:15 +00:00
)
// NamespaceResolver visitor
type NamespaceResolver struct {
2020-12-28 19:13:08 +00:00
visitor.Null
2020-05-13 18:05:15 +00:00
Namespace *Namespace
ResolvedNames map[ast.Vertex]string
goDeep bool
2020-05-13 18:05:15 +00:00
}
// NewNamespaceResolver NamespaceResolver type constructor
func NewNamespaceResolver() *NamespaceResolver {
return &NamespaceResolver{
Namespace: NewNamespace(""),
ResolvedNames: map[ast.Vertex]string{},
goDeep: true,
}
}
func (nsr *NamespaceResolver) EnterNode(n ast.Vertex) bool {
n.Accept(nsr)
2020-05-17 19:56:32 +00:00
2020-05-13 18:05:15 +00:00
if !nsr.goDeep {
nsr.goDeep = true
return false
}
return true
}
func (nsr *NamespaceResolver) StmtNamespace(n *ast.StmtNamespace) {
if n.Name == nil {
2020-05-13 18:05:15 +00:00
nsr.Namespace = NewNamespace("")
} else {
2020-12-28 21:01:02 +00:00
NSParts := n.Name.(*ast.Name).Parts
2020-05-13 18:05:15 +00:00
nsr.Namespace = NewNamespace(concatNameParts(NSParts))
}
}
2020-12-28 21:01:02 +00:00
func (nsr *NamespaceResolver) StmtUse(n *ast.StmtUseList) {
useType := ""
if n.Type != nil {
useType = string(n.Type.(*ast.Identifier).Value)
2020-05-13 18:05:15 +00:00
}
2020-12-28 21:01:02 +00:00
for _, nn := range n.Uses {
nsr.AddAlias(useType, nn, nil)
2020-05-13 18:05:15 +00:00
}
nsr.goDeep = false
2020-05-13 18:05:15 +00:00
}
2020-12-28 21:01:02 +00:00
func (nsr *NamespaceResolver) StmtGroupUse(n *ast.StmtGroupUseList) {
useType := ""
if n.Type != nil {
useType = string(n.Type.(*ast.Identifier).Value)
2020-05-13 18:05:15 +00:00
}
2020-12-28 21:01:02 +00:00
for _, nn := range n.Uses {
nsr.AddAlias(useType, nn, n.Prefix.(*ast.Name).Parts)
}
2020-05-13 18:05:15 +00:00
nsr.goDeep = false
}
func (nsr *NamespaceResolver) StmtClass(n *ast.StmtClass) {
if n.Extends != nil {
nsr.ResolveName(n.Extends, "")
2020-05-13 18:05:15 +00:00
}
if n.Implements != nil {
for _, interfaceName := range n.Implements {
2020-05-13 18:05:15 +00:00
nsr.ResolveName(interfaceName, "")
}
}
2020-12-28 21:01:02 +00:00
if n.Name != nil {
nsr.AddNamespacedName(n, string(n.Name.(*ast.Identifier).Value))
2020-05-13 18:05:15 +00:00
}
}
func (nsr *NamespaceResolver) StmtEnum(n *ast.StmtEnum) {
if n.Type != nil {
nsr.ResolveName(n.Type, "")
}
if n.Implements != nil {
for _, interfaceName := range n.Implements {
nsr.ResolveName(interfaceName, "")
}
}
if n.Name != nil {
nsr.AddNamespacedName(n, string(n.Name.(*ast.Identifier).Value))
}
}
2020-05-13 18:05:15 +00:00
func (nsr *NamespaceResolver) StmtInterface(n *ast.StmtInterface) {
if n.Extends != nil {
for _, interfaceName := range n.Extends {
2020-05-13 18:05:15 +00:00
nsr.ResolveName(interfaceName, "")
}
}
2020-12-28 21:01:02 +00:00
nsr.AddNamespacedName(n, string(n.Name.(*ast.Identifier).Value))
2020-05-13 18:05:15 +00:00
}
func (nsr *NamespaceResolver) StmtTrait(n *ast.StmtTrait) {
2020-12-28 21:01:02 +00:00
nsr.AddNamespacedName(n, string(n.Name.(*ast.Identifier).Value))
2020-05-13 18:05:15 +00:00
}
func (nsr *NamespaceResolver) StmtFunction(n *ast.StmtFunction) {
2020-12-28 21:01:02 +00:00
nsr.AddNamespacedName(n, string(n.Name.(*ast.Identifier).Value))
2020-05-13 18:05:15 +00:00
for _, parameter := range n.Params {
nsr.ResolveType(parameter.(*ast.Parameter).Type)
}
if n.ReturnType != nil {
nsr.ResolveType(n.ReturnType)
}
}
func (nsr *NamespaceResolver) StmtClassMethod(n *ast.StmtClassMethod) {
for _, parameter := range n.Params {
nsr.ResolveType(parameter.(*ast.Parameter).Type)
}
if n.ReturnType != nil {
nsr.ResolveType(n.ReturnType)
}
}
func (nsr *NamespaceResolver) ExprClosure(n *ast.ExprClosure) {
for _, parameter := range n.Params {
nsr.ResolveType(parameter.(*ast.Parameter).Type)
}
if n.ReturnType != nil {
nsr.ResolveType(n.ReturnType)
}
}
2020-07-29 20:23:44 +00:00
func (nsr *NamespaceResolver) StmtPropertyList(n *ast.StmtPropertyList) {
if n.Type != nil {
nsr.ResolveType(n.Type)
}
}
2020-05-13 18:05:15 +00:00
func (nsr *NamespaceResolver) StmtConstList(n *ast.StmtConstList) {
for _, constant := range n.Consts {
nsr.AddNamespacedName(constant, string(constant.(*ast.StmtConstant).Name.(*ast.Identifier).Value))
2020-05-13 18:05:15 +00:00
}
}
func (nsr *NamespaceResolver) ExprStaticCall(n *ast.ExprStaticCall) {
nsr.ResolveName(n.Class, "")
}
func (nsr *NamespaceResolver) ExprStaticPropertyFetch(n *ast.ExprStaticPropertyFetch) {
nsr.ResolveName(n.Class, "")
}
func (nsr *NamespaceResolver) ExprClassConstFetch(n *ast.ExprClassConstFetch) {
nsr.ResolveName(n.Class, "")
}
func (nsr *NamespaceResolver) ExprNew(n *ast.ExprNew) {
nsr.ResolveName(n.Class, "")
}
func (nsr *NamespaceResolver) ExprInstanceOf(n *ast.ExprInstanceOf) {
nsr.ResolveName(n.Class, "")
}
func (nsr *NamespaceResolver) StmtCatch(n *ast.StmtCatch) {
for _, t := range n.Types {
nsr.ResolveName(t, "")
}
}
func (nsr *NamespaceResolver) ExprFunctionCall(n *ast.ExprFunctionCall) {
nsr.ResolveName(n.Function, "function")
}
func (nsr *NamespaceResolver) ExprConstFetch(n *ast.ExprConstFetch) {
nsr.ResolveName(n.Const, "const")
}
func (nsr *NamespaceResolver) StmtTraitUse(n *ast.StmtTraitUse) {
for _, t := range n.Traits {
nsr.ResolveName(t, "")
}
for _, a := range n.Adaptations {
switch aa := a.(type) {
case *ast.StmtTraitUsePrecedence:
refTrait := aa.Trait
if refTrait != nil {
nsr.ResolveName(refTrait, "")
}
for _, insteadOf := range aa.Insteadof {
nsr.ResolveName(insteadOf, "")
}
case *ast.StmtTraitUseAlias:
refTrait := aa.Trait
if refTrait != nil {
nsr.ResolveName(refTrait, "")
2020-05-13 18:05:15 +00:00
}
}
}
}
func (nsr *NamespaceResolver) Attribute(n *ast.Attribute) {
nsr.ResolveName(n.Name, "")
}
2020-05-13 18:05:15 +00:00
// LeaveNode is invoked after node process
func (nsr *NamespaceResolver) LeaveNode(n ast.Vertex) {
switch nn := n.(type) {
case *ast.StmtNamespace:
if nn.Stmts != nil {
nsr.Namespace = NewNamespace("")
}
}
}
// AddAlias adds a new alias
func (nsr *NamespaceResolver) AddAlias(useType string, nn ast.Vertex, prefix []ast.Vertex) {
switch use := nn.(type) {
2020-12-28 21:01:02 +00:00
case *ast.StmtUse:
if use.Type != nil {
useType = string(use.Type.(*ast.Identifier).Value)
}
2020-12-28 21:01:02 +00:00
useNameParts := use.Use.(*ast.Name).Parts
var alias string
if use.Alias == nil {
2020-12-28 21:01:02 +00:00
alias = string(useNameParts[len(useNameParts)-1].(*ast.NamePart).Value)
} else {
alias = string(use.Alias.(*ast.Identifier).Value)
}
nsr.Namespace.AddAlias(useType, concatNameParts(prefix, useNameParts), alias)
2020-05-13 18:05:15 +00:00
}
}
// AddNamespacedName adds namespaced name by node
func (nsr *NamespaceResolver) AddNamespacedName(nn ast.Vertex, nodeName string) {
if nsr.Namespace.Namespace == "" {
nsr.ResolvedNames[nn] = nodeName
} else {
nsr.ResolvedNames[nn] = nsr.Namespace.Namespace + "\\" + nodeName
}
}
// ResolveName adds a resolved fully qualified name by node
func (nsr *NamespaceResolver) ResolveName(nameNode ast.Vertex, aliasType string) {
resolved, err := nsr.Namespace.ResolveName(nameNode, aliasType)
if err == nil {
nsr.ResolvedNames[nameNode] = resolved
}
}
// ResolveType adds a resolved fully qualified type name
func (nsr *NamespaceResolver) ResolveType(n ast.Vertex) {
switch nn := n.(type) {
case *ast.Nullable:
nsr.ResolveType(nn.Expr)
case *ast.Union:
for _, nnn := range nn.Types {
nsr.ResolveType(nnn)
}
case *ast.Intersection:
for _, nnn := range nn.Types {
nsr.ResolveType(nnn)
}
2020-12-28 21:01:02 +00:00
case *ast.Name:
2020-05-13 18:05:15 +00:00
nsr.ResolveName(n, "")
case *ast.NameRelative:
nsr.ResolveName(n, "")
case *ast.NameFullyQualified:
nsr.ResolveName(n, "")
}
}
// Namespace context
type Namespace struct {
Namespace string
Aliases map[string]map[string]string
}
// NewNamespace constructor
func NewNamespace(NSName string) *Namespace {
return &Namespace{
Namespace: NSName,
Aliases: map[string]map[string]string{
"": {},
"const": {},
"function": {},
},
}
}
// AddAlias adds a new alias
func (ns *Namespace) AddAlias(aliasType string, aliasName string, alias string) {
aliasType = strings.ToLower(aliasType)
if aliasType == "const" {
ns.Aliases[aliasType][alias] = aliasName
} else {
ns.Aliases[aliasType][strings.ToLower(alias)] = aliasName
}
}
// ResolveName returns a resolved fully qualified name
func (ns *Namespace) ResolveName(nameNode ast.Vertex, aliasType string) (string, error) {
switch n := nameNode.(type) {
case *ast.NameFullyQualified:
// Fully qualifid name is already resolved
return concatNameParts(n.Parts), nil
case *ast.NameRelative:
if ns.Namespace == "" {
return concatNameParts(n.Parts), nil
}
return ns.Namespace + "\\" + concatNameParts(n.Parts), nil
2020-12-28 21:01:02 +00:00
case *ast.Name:
2020-05-13 18:05:15 +00:00
if aliasType == "const" && len(n.Parts) == 1 {
2020-12-28 21:01:02 +00:00
part := strings.ToLower(string(n.Parts[0].(*ast.NamePart).Value))
2020-05-13 18:05:15 +00:00
if part == "true" || part == "false" || part == "null" {
return part, nil
}
}
if aliasType == "" && len(n.Parts) == 1 {
2020-12-28 21:01:02 +00:00
part := strings.ToLower(string(n.Parts[0].(*ast.NamePart).Value))
2020-05-13 18:05:15 +00:00
switch part {
case "self":
fallthrough
case "static":
fallthrough
case "parent":
fallthrough
case "int":
fallthrough
case "float":
fallthrough
case "bool":
fallthrough
case "string":
fallthrough
case "void":
fallthrough
case "iterable":
fallthrough
case "object":
fallthrough
case "mixed": // 8.0
fallthrough
case "never": // 8.1
fallthrough
case "true", "false", "null": // 8.2
2020-05-13 18:05:15 +00:00
return part, nil
}
}
aliasName, err := ns.ResolveAlias(nameNode, aliasType)
if err != nil {
// resolve as relative name if alias not found
if ns.Namespace == "" {
return concatNameParts(n.Parts), nil
}
return ns.Namespace + "\\" + concatNameParts(n.Parts), nil
}
if len(n.Parts) > 1 {
// if name qualified, replace first part by alias
return aliasName + "\\" + concatNameParts(n.Parts[1:]), nil
}
return aliasName, nil
}
return "", errors.New("must be instance of name.Names")
}
// ResolveAlias returns alias or error if not found
func (ns *Namespace) ResolveAlias(nameNode ast.Vertex, aliasType string) (string, error) {
aliasType = strings.ToLower(aliasType)
2020-12-28 21:01:02 +00:00
nameParts := nameNode.(*ast.Name).Parts
2020-05-13 18:05:15 +00:00
2020-12-28 21:01:02 +00:00
firstPartStr := string(nameParts[0].(*ast.NamePart).Value)
2020-05-13 18:05:15 +00:00
if len(nameParts) > 1 { // resolve aliases for qualified names, always against class alias type
firstPartStr = strings.ToLower(firstPartStr)
aliasType = ""
} else {
if aliasType != "const" { // constants are case-sensitive
firstPartStr = strings.ToLower(firstPartStr)
}
}
aliasName, ok := ns.Aliases[aliasType][firstPartStr]
if !ok {
return "", errors.New("Not found")
}
return aliasName, nil
}
func concatNameParts(parts ...[]ast.Vertex) string {
str := ""
for _, p := range parts {
for _, n := range p {
if str == "" {
2020-12-28 21:01:02 +00:00
str = string(n.(*ast.NamePart).Value)
2020-05-13 18:05:15 +00:00
} else {
2020-12-28 21:01:02 +00:00
str = str + "\\" + string(n.(*ast.NamePart).Value)
2020-05-13 18:05:15 +00:00
}
}
}
return str
}