antlraux package
v0.2.1

antlraux.context
Class Declaration

java.lang.Object
  |
  +--antlraux.context.Declaration
All Implemented Interfaces:
LexInfo

public class Declaration
extends java.lang.Object
implements LexInfo

Declarations are the basic contextual information unit.

A declaration is a set of related fields, representing an bit of contextual information that has to be inserted in a Scope. Some of these are mandatory and others are optional (can be null). Mandatory fields are

Optional fields are: Optional fields can be null.

The aim of this class may be better understood with an example. Let's imagine a language whose variables would be declared like in C:

    int a,b,c;
 
Then somewhere in the semantic analyzer some code would have to make something equivalent to this:
    Scope currentScope = getCurrentScope();
    currentScope.insert( new Declaration(VARIABLE, intTag, "a"));
    currentScope.insert( new Declaration(VARIABLE, intTag, "b"));
    currentScope.insert( new Declaration(VARIABLE, intTag, "c"));
 
Note that in this example we are ignoring optional fields; if possible, you should not!

Declarations are also valid for declaring types; this:

 class A {...}
 struct s {...}
 
Would trigger the generation of a code similar to:
    Type AClassType = getTypeForClass("A");
    currentScope.insert(new Declaration(CLASS, AClassType, "A"));
    Type sStructType = getTypeForStruct("s");
    currentScope.insert(new Declaration(STRUCT, sStructType, "s"));
 
Depending on how you implement the scoping system (with or without Context- see the comments on Scope for further information ) this codes will be on a subclass of Scope or Context.

Author:
Enrique José García Cota
See Also:
Context, Scope, Type

Field Summary
protected  antlr.collections.AST ast
          The AST that originated the declaration.
protected  int column
          Lexical information.
protected  java.lang.String filename
          Lexical information.
protected  antlr.collections.AST initialValue
          An initial value for the declaration.
protected  int line
          Lexical information.
protected  java.lang.String name
          The declaration name
protected  int tag
          An integer used to store different "declaration types", such as PARAMETER, ATTRIBUTE or CLASS.
protected  Type type
          The declaration Type.
 
Fields inherited from interface antlraux.util.LexInfo
DEFAULT_COLUMN, DEFAULT_FILENAME, DEFAULT_LINE
 
Constructor Summary
Declaration(int tag, Type t, java.lang.String name)
           
Declaration(int tag, Type t, java.lang.String name, antlr.collections.AST ast, antlr.collections.AST initialValue, LexInfo lexInfo)
           
Declaration(int tag, Type t, java.lang.String name, antlr.collections.AST ast, antlr.collections.AST initialValue, java.lang.String fileName, int line, int column)
           
 
Method Summary
 void copyLexInfo(LexInfo from)
          Copies the lexical information from other LexInfo.
 antlr.collections.AST getAST()
           
 int getColumn()
          Get the column for this node
 java.lang.String getFilename()
          Get the file name for this node
 antlr.collections.AST getInitialValue()
           
 java.lang.String getLexInfoString()
          Returns true if all the information (Filename:line:column) is valid; false otherwise.
 int getLine()
          Get the line for this node
 java.lang.String getName()
           
 int getTag()
           
 Type getType()
           
 void initialize(Declaration other)
           
 void setAST(antlr.collections.AST ast)
           
 void setColumn(int column)
          Set the column for this node
 void setFilename(java.lang.String fn)
          Sets the file name for this node.
 void setInitialValue(antlr.collections.AST initialValue)
           
 void setLine(int line)
          Set the line for this node
 void setName(java.lang.String name)
          Accessor for name
 void setTag(int tag)
           
 void setType(Type t)
          Accessor for type
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

type

protected Type type
The declaration Type.


name

protected java.lang.String name
The declaration name


tag

protected int tag
An integer used to store different "declaration types", such as PARAMETER, ATTRIBUTE or CLASS.


ast

protected antlr.collections.AST ast
The AST that originated the declaration. Can be null


initialValue

protected antlr.collections.AST initialValue
An initial value for the declaration. Use it when modelling declarations that involve a certain initialization value (like variables initialized with an expression ):
       Declaration
      ______/\______
     /              \    
     int a  = 2 + 2 ;
             \__  __/
                \/
          Initial value
 
In declarations with no initializations, null or a type- dependent default value could be used.

I've left this member's type as an AST, so you can use your own; however, I strongly recomment using an instance of ExpressionAST for it.


filename

protected java.lang.String filename
Lexical information. Can be null


line

protected int line
Lexical information. Can be null


column

protected int column
Lexical information. Can be null

Constructor Detail

Declaration

public Declaration(int tag,
                   Type t,
                   java.lang.String name)

Declaration

public Declaration(int tag,
                   Type t,
                   java.lang.String name,
                   antlr.collections.AST ast,
                   antlr.collections.AST initialValue,
                   LexInfo lexInfo)

Declaration

public Declaration(int tag,
                   Type t,
                   java.lang.String name,
                   antlr.collections.AST ast,
                   antlr.collections.AST initialValue,
                   java.lang.String fileName,
                   int line,
                   int column)
Method Detail

setName

public void setName(java.lang.String name)
Accessor for name

Throws:
java.lang.IllegalArgumentException - if name==null

getName

public java.lang.String getName()

setType

public void setType(Type t)
Accessor for type

Throws:
java.lang.IllegalArgumentException - if null==t

setTag

public void setTag(int tag)

setAST

public void setAST(antlr.collections.AST ast)

setInitialValue

public void setInitialValue(antlr.collections.AST initialValue)

getAST

public antlr.collections.AST getAST()

getType

public Type getType()

getTag

public int getTag()

getInitialValue

public antlr.collections.AST getInitialValue()

getFilename

public java.lang.String getFilename()
Description copied from interface: LexInfo
Get the file name for this node

Specified by:
getFilename in interface LexInfo

getColumn

public int getColumn()
Description copied from interface: LexInfo
Get the column for this node

Specified by:
getColumn in interface LexInfo

getLine

public int getLine()
Description copied from interface: LexInfo
Get the line for this node

Specified by:
getLine in interface LexInfo

setFilename

public void setFilename(java.lang.String fn)
Description copied from interface: LexInfo
Sets the file name for this node.

Specified by:
setFilename in interface LexInfo

setColumn

public void setColumn(int column)
Description copied from interface: LexInfo
Set the column for this node

Specified by:
setColumn in interface LexInfo

setLine

public void setLine(int line)
Description copied from interface: LexInfo
Set the line for this node

Specified by:
setLine in interface LexInfo

copyLexInfo

public void copyLexInfo(LexInfo from)
Description copied from interface: LexInfo
Copies the lexical information from other LexInfo. WARNING: do not forget the case from==null!

Specified by:
copyLexInfo in interface LexInfo

getLexInfoString

public java.lang.String getLexInfoString()
Description copied from interface: LexInfo
Returns true if all the information (Filename:line:column) is valid; false otherwise. It will usually be implemented around a FileLineFormatter, which authomatically deals with incompleteness in the information.

Specified by:
getLexInfoString in interface LexInfo

initialize

public void initialize(Declaration other)

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

antlraux package
v0.2.1

Created by Enrique José García Cota