summaryrefslogtreecommitdiff
path: root/src/main/java/com/seaofnodes/simple/node/FRefNode.java
blob: 644f1434743ea9a7fb32557700b87347bd37835c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.seaofnodes.simple.node;

import com.seaofnodes.print.ExprPrinter;

import com.seaofnodes.simple.Parser;
import com.seaofnodes.simple.Var;
import com.seaofnodes.simple.type.Type;

/**
 *  A Forward Reference.  Its any final constant, including functions.  When
 *  the Def finally appears its plugged into the forward reference, which then
 *  peepholes to the Def.

 *  FRefs can be external references, to other Simple defined values in ELF
 *  files, or external FFI/C libs.  If external, a file system lookup needs to
 *  find it in another file, and the name (one time) change to match the found
 *  location.
 *
 *  Example, a ambiguous name "foo" is looked up externally; it might resolve
 *  to "A.B.foo" or "libc.foo" or the like; the name is changed to match.
 */
public class FRefNode extends ConstantNode {
    public static final Type FREF_TYPE = Type.BOTTOM;
    public String _name;
    public final Parser.Lexer _loc;    // Source location

    public FRefNode( String name, Parser.Lexer loc ) { this(name,loc,FREF_TYPE); }
    public FRefNode( String name, Parser.Lexer loc, Type type ) {
        super(type);
        _name = name;
        _loc = loc;
    }

    @Override public String label() { return "FRef"+_name; }


    @Override protected ExprPrinter<Node> _print1(ExprPrinter<Node> p) {
        return p.p("FRef_").p(_name);
    }

    @Override public Node idealize() {
        // When FRef finds its definition, idealize to it
        return nIns()==1 ? null : in(1);
    }

    public Parser.ParseException err() { return Parser.error("Undefined name '"+_name+"'",_loc); }

    @Override public boolean eq(Node n) { return this==n; }
    @Override int hash() { return _name.hashCode(); }
}