xref: /aosp_15_r20/external/apache-commons-bcel/src/examples/Mini/Function.java (revision 0c56280ab0842982c46a149f7b9eaa497e31e292)
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  */
18 package Mini;
19 
20 /**
21  * Represents a function declaration and its arguments. Type information is contained
22  * in the ASTIdent fields.
23  *
24  * @version $Id$
25  */
26 public class Function implements org.apache.bcel.Constants, EnvEntry {
27   private ASTIdent   name;         // Reference to the original declaration
28   private ASTIdent[] args;         // Reference to argument identifiers
29 //  private ASTExpr    body;         // Reference to function expression body
30   private boolean    reserved;     // Is a key word?
31   private int        line, column; // Short for name.getToken()
32   private String     fun_name;     // Short for name.getName()
33   private int        no_args;
34 
Function(ASTIdent name, ASTIdent[] args)35   public Function(ASTIdent name, ASTIdent[] args) {
36     this(name, args, false);
37   }
38 
Function(ASTIdent name, ASTIdent[] args, boolean reserved)39   public Function(ASTIdent name, ASTIdent[] args, boolean reserved) {
40     this.name     = name;
41     this.args     = args;
42     this.reserved = reserved;
43 
44     fun_name = name.getName();
45     line     = name.getLine();
46     column   = name.getColumn();
47     setArgs(args);
48   }
49 
50   @Override
toString()51   public String toString() {
52     StringBuffer buf = new StringBuffer();
53 
54     for(int i=0; i < no_args; i++) {
55       buf.append(args[i].getName());
56 
57       if(i < no_args - 1) {
58         buf.append(", ");
59     }
60     }
61 
62     String prefix = "Function " + fun_name + "(" + buf.toString() + ")";
63 
64     if(!reserved) {
65         return prefix + " declared at line " + line + ", column " + column;
66     } else {
67         return prefix + " <predefined function>";
68     }
69   }
70 
getNoArgs()71   public int        getNoArgs()       { return no_args; }
getName()72   public ASTIdent   getName()         { return name; }
getHashKey()73   public String     getHashKey()      { return fun_name; }
getLine()74   public int        getLine()         { return line; }
getColumn()75   public int        getColumn()       { return column; }
getArg(int i)76   public ASTIdent   getArg(int i)     { return args[i]; }
getArgs()77   public ASTIdent[] getArgs()         { return args; }
setArgs(ASTIdent[] args)78   public void       setArgs(ASTIdent[] args) {
79     this.args = args;
80     no_args   = (args == null)? 0 : args.length;
81   }
82 }
83