github.com/nevalang/neva@v0.23.1-0.20240507185603-7696a9bb8dda/internal/compiler/irgen/runtime_func.go (about)

     1  package irgen
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  
     7  	"github.com/nevalang/neva/internal/compiler"
     8  	src "github.com/nevalang/neva/internal/compiler/sourcecode"
     9  	ts "github.com/nevalang/neva/internal/compiler/sourcecode/typesystem"
    10  	"github.com/nevalang/neva/internal/runtime/ir"
    11  )
    12  
    13  func getRuntimeFuncRef(component src.Component, nodeTypeArgs []ts.Expr) (string, error) {
    14  	args, ok := component.Directives[compiler.ExternDirective]
    15  	if !ok {
    16  		return "", nil
    17  	}
    18  
    19  	if len(args) == 1 {
    20  		return args[0], nil
    21  	}
    22  
    23  	if len(nodeTypeArgs) == 0 || nodeTypeArgs[0].Inst == nil {
    24  		// FIXME sometimes we have union here
    25  		// we must use node argument instead of component type param
    26  		return "", nil
    27  	}
    28  
    29  	firstTypeArg := nodeTypeArgs[0].Inst.Ref.String()
    30  	for _, arg := range args {
    31  		parts := strings.Split(arg, " ")
    32  		if firstTypeArg == parts[0] {
    33  			return parts[1], nil
    34  		}
    35  	}
    36  
    37  	return "", errors.New("type argument mismatches runtime func directive")
    38  }
    39  
    40  func getRuntimeFuncMsg(node src.Node, scope src.Scope) (*ir.Msg, *compiler.Error) {
    41  	args, ok := node.Directives[compiler.BindDirective]
    42  	if !ok {
    43  		return nil, nil
    44  	}
    45  
    46  	entity, location, err := scope.Entity(compiler.ParseEntityRef(args[0]))
    47  	if err != nil {
    48  		return nil, &compiler.Error{
    49  			Err:      err,
    50  			Location: &scope.Location,
    51  		}
    52  	}
    53  
    54  	return getIRMsgBySrcRef(entity.Const, scope.WithLocation(location))
    55  }