Introduction | Projects and Files | Statements | Expressions | Symbols | Types |
Attributes | Labels | Non-Member Functions | Constructing Declarations | Example Programs | Index |
All Languages:
C:
Fortran:
Fortran 90:
SgSymbol
is the base class for the classes representing
symbols. As with the other classes there
is a variant()
which identifies the subclass and an integer identifier
id()
that is unique for each symbol. The symbols in each file are
organized as a list which is accessible from the file object as
SgFile::firstSymbol()
and the following symbol to the current symbol
is located with next()
. Every symbol has a character string
identifier()
,
a type()
, which is described in detail below, and a statement called
scope()
that is the statement in which declaration is scoped. The
scope is the control parent of the declaration statement.
In addition, there are functions that can be used to generate a copy of the symbol. There are three forms of the copy. The basic copy make a simple copy of the symbol table entry. The level one copy also generates new type information. The level two copy also copies the declaration body.
class SgSymbol { public: // basic class contains PTR_SYMB thesymb; SgSymbol(int variant, char *identifier, SgType &t, SgStatement &scope); SgSymbol(int variant, char *identifier, SgType *t, SgStatement *scope); SgSymbol(int variant, char *identifier, SgStatement &scope); SgSymbol(int variant, char *identifier, SgStatement *scope); SgSymbol(int variant, char *identifier, SgType *type, SgStatement *scope, SgSymbol *structsymb, SgSymbol *nextfield );SgSymbol(int variant, char *name); SgSymbol(int variant); SgSymbol(PTR_SYMB symb); inline SgSymbol(SgSymbol &); ~SgSymbol(); inline int variant(); inline int id(); // unique identifier inline char *identifier(); // the text name for the symbol. inline SgType *type(); // the type of the symbol inline void setType(SgType &t); // the type of the symbol inline void setType(SgType *t); // the type of the symbol inline SgStatement *scope(); // the SgControlStatement where defined. inline SgSymbol *next(); // next symbol reference. SgStatement *declaredInStmt(); // the declaration statement inline SgSymbol ©(); inline SgSymbol ©Level1(); // copy also parameters inline SgSymbol ©Level2(); // copy parameters, body also inline SgSymbol ©AcrossFiles(SgStatement &where); // special copy to // move things from a file. int attributes(); // the Fortran 90 attributes void setAttribute(int attribute); void removeAttribute(int attribute); void declareTheSymbol(SgStatement &st); inline void declareTheSymbolWithParamList (SgStatement &st, SgExpression &parlist); SgExpression *makeDeclExpr(); inline SgExpression *makeDeclExprWithParamList (SgExpression &parlist); SgVarDeclStmt *makeVarDeclStmt(); SgVarDeclStmt *makeVarDeclStmtWithParamList (SgExpression &parlist); SgStatement *body(); // the body of the symbol if has one (like, function // call, class,...)
/////////////// FOR ATTRIBUTES //////////////////////////
int numberOfAttributes(); int numberOfAttributes(int type); // of a specified type; void *attributeValue(int i); int attributeType(int i); void *attributeValue(int i,int type); // only considering one type attribute void *deleteAttribute(int i); void addAttribute(int type, void *a, int size); // void * can be NULL; void addAttribute(int type); //void * is NULL; void addAttribute(void *a, int size); //no type specifed; void addAttribute(SgAttribute *att); SgAttribute *getAttribute(int i); SgAttribute *getAttribute(int i,int type); }
SgSymbol
(int variant, char *identifier, SgType &t, SgStatement &scope)
SgSymbol
(int variant, char *identifier, SgStatement &scope)
SgSymbol
(int variant, char *name)
void setAttribute
(int attribute)
void removeAttribute
(int attribute)
void declareTheSymbol
(SgStatement &st)
st
. Please
see section The SgMakeDeclExp and Related Methods for more information
about this method and other methods for constructing declarations.
void declareTheSymbolWithParamList
(SgStatement &st)
declareTheSymbol()
but takes a second argument that is
a parameter list (for function prototypes).
SgExpression *makeDeclExprWithParamList
(SgExpression &parlist)
makeDeclExpr()
but takes a second argument that is
a parameter list (for function prototypes).
SgVarDeclStmt *makeVarDeclStmt
()
SgVarDeclStmt *makeVarDeclStmtWithParamList
(SgExpression &parlist)
makeVarDeclStmt()
but takes a second argument that is
a parameter list (for function prototypes)
As an example of how to traverse the symbol table, consider the problem of looking for a symbol table entry for a given member function in a given class. There are several ways to do this. The scheme shown below will search the table for the class by name. Then it searches the field list for the member function and returns the pointer to the symbol object if it is found.
SgSymbol *memberFunction(char *className, char *functionName){ SgSymbol *s, *fld; SgClassSymb *cl; SgMemberFuncSymb *f; int i;for (s=file.firstSymbol(); s ; s = s->next()) if (!strcmp(s->identifier(), className)) break;
if (s == NULL) return NULL;
if (cl = isSgClassSymb(s)){ i = 1; while(fld = cl->field(i++)){ if ((f = isSgMemberFuncSymb(fld)) && !strcmp(f->identifier(), functionName)) return f; } } return NULL; }
As another example, consider the task of generating temporary variables that match the type and initial constructor parameters of a given variable symbol. More specifically, consider the declarations
int *x[100], y[34]; myClass object(parm1, parm2);
Each of these statements is of class SgVarDeclStmt
which has
a type and an expression list. In the first case the expression
list has two items (called values in SgExprListExp
):
x[100], y[34]
,
and the second declaration has one item object(parm1, parm2)
.
The following function creates a new temporary symbol with name
_Ti
where i
is an integer counter and given a symbol table
entry, finds the declaration of the symbol, and creates an identical
declaration for the temporary.
char name_counter = '0'; SgSymbol * DeclareTemporaryLike(SgSymbol * x){ SgStatement *old_decl_stmt; SgSymbol *tmp; SgVarDeclStmt *decl_stmt; SgExpression *exp; char *name; SgExprListExp *elist;// now find the declaration statement for x and get // the correct initializing expression. old_decl_stmt = x.declaredInStmt() // get the expression list of variables delcared exp = old_decl_stmt->expr(0);
// look for the reference to our symbol x. while(elist = isSgExprListExp(exp)){ if((elist->value())->isSymbolInExpression(*x) break; exp = elist->next(); } if(exp == NULL) return NULL;
// now create a new expression list and // and a new declaration statement elist = new SgExprListExp(*(exp->copy()); decl_stmt = new SgVarDeclStmt(*elist,old_decl_stmt->type());
// create a new variable of the same class // generate the name as "_Ti" where i is counter. name = new char(10); sprintf(name, "_T%c", name_counter++); tmp = new SgVariableSymb(name, (x->type())->copy(), *scope);
// replace x with the new variable and insert // the new decl statement after the old one. decl_stmt->replaceSymbBySymb(*x, *tmp); old_decl_stmt->insertStmtAfter(*decl_stmt); return tmp; }
SgSymbol
is used in the following example programs:
Represents variable symbols, for all languages.
class SgVariableSymb: public SgSymbol { // a variable // variant = VARIABLE_NAME public: inline SgVariableSymb(char *identifier, SgType &t, SgStatement &scope); inline SgVariableSymb(char *identifier, SgType *t, SgStatement *scope); inline SgVariableSymb(char *identifier, SgType &t); inline SgVariableSymb(char *identifier, SgStatement &scope); inline SgVariableSymb(char *identifier, SgStatement *scope); inline SgVariableSymb(char *identifier); inline ~SgVariableSymb();/* This function allocates and returns a new variable reference expression to this symbol. (ajm) */ inline SgVarRefExp *varRef (void); };
SgVariableSymb
(char *identifier, SgType &t, SgStatement &scope)
SgVariableSymb
(char *identifier, SgType &t)
SgVariableSymb
(char *identifier, SgStatement &scope)
SgVariableSymb
(char *identifier)
SgVariableSymb
is used in the following example programs:
Represents symbols for constants, for all languages.
class SgConstantSymb: public SgSymbol { // a symbol for a constant object // variant == CONST_NAME public: inline SgConstantSymb(char *identifier, SgStatement &scope, SgExpression &value); inline ~SgConstantSymb(); inline SgExpression *constantValue(); };
SgConstantSymb
(char *identifier, SgStatement &scope, SgExpression &value)
SgConstantSymb
is used in the following example programs:
Represents symbols for subroutine
, function
, and
main program
, for all languages.
class SgFunctionSymb: public SgSymbol { // a subroutine, function or main program // variant == PROGRAM_NAME, PROCEDURE_NAME, or FUNCTION_NAME public: SgFunctionSymb(int variant); SgFunctionSymb(int variant, char *identifier, SgType &t, SgStatement &scope); ~SgFunctionSymb(); inline void addParameter(SgSymbol ¶meters); void insertParameter(int position, SgSymbol &symb); int numberOfParameters(); SgSymbol *parameter(int i); SgSymbol *result(); void setResult(SgSymbol &symbol); };
SgFunctionSymb
(int variant, char *identifier, SgType &t, SgStatement &scope)
void addParameter
(int n, SgSymbol ¶meters)
void insertParameter
(int position, SgSymbol &symb)
i
th parameter in the formal parameter list
(of a function).
void setResult
(SgSymbol &symbol)
SgFunctionSymb
is used in the following example programs:
class SgLabelSymb: public SgSymbol { // a C label name // variant == LABEL_NAME public: inline SgLabelSymb(char *name); inline SgLabelSymb(char *name, SgStatement &scope); inline ~SgLabelSymb(); };
Represents the C typedef
symbols.
class SgTypeSymb: public SgSymbol { // a C typedef. the type() function returns the base type. // variant == TYPE_NAME public: SgTypeSymb(char *name, SgType &baseType); SgType &baseType(); ~SgTypeSymb(); };
SgTypeSymb
(char *name, SgType &baseType)
Represents the C symbols for class
, union
, struct
,
and collection
.
class SgClassSymb: public SgSymbol { // the class, union, struct and collection type. // variant == CLASS_NAME, UNION_NAME, STRUCT_NAME or COLLECTION_NAME public: inline SgClassSymb(int variant, char *name, SgStatement &scope); inline ~SgClassSymb(); inline int numberOfFields(); inline SgSymbol *field(int i); //i=0,1,... };
SgClassSymb
(int variant, char *name, SgStatement &scope)
SgSymbol *field(int i)
method for useful information.
i
th member. Starting with version 1.9, the
parameter i
starts with zero (it started with one in versions 1.7
and earlier). The definition of what constitutes a field for the purpose
of the numberOfFields
and field
methods is somewhat unusual.
Consider the following example:
class foo; class X{ public: void xmember();};class test{ public: int i, j, k; // data members static istat; friend test operator+(const test&, const test&); friend class foo; friend class foo; friend void X::xmember(); class I1; //forward decl. of nested class int fint(int); //member function typedef int Myint; //nested type class inner{}; //nested type enum dir{left='l',right='r'}; //left and right are member constants };
In this case, Sage++ reports the following fields:
number of fields 11 field no. 0 symbol i field no. 1 symbol j field no. 2 symbol k field no. 3 symbol istat field no. 4 symbol + field no. 5 symbol fint field no. 6 symbol Myint field no. 7 symbol inner field no. 8 symbol dir field no. 9 symbol left field no. 10 symbol right
For a SgClassSymb that represents a class that has already been defined,
type()
returns a pointer to SgClassType (i.e.,
isSgClassType(symb->type())
should return a non-NULL
SgClassType pointer).
The class SgClassType
contains the method
SgStatement *structureDecl()
that can be used to access the statement that contains the definition
of the class.
Represents C symbols for fields in enums, symbols for fields in structs, and symbols for fields in classes.
class SgFieldSymb: public SgSymbol { // a field in an enum or in a struct. // variant == ENUM_NAME or FIELD_NAME public: // no check is made to see if the field "identifier" // already exists in the structure. inline SgFieldSymb(char *identifier, SgType &t, SgSymbol &structureName); inline ~SgFieldSymb(); inline int offset(); // position in the structure inline SgSymbol *structureName(); // parent structure inline SgSymbol *nextField(); inline int isMethodOfElement(); };
SgFieldSymb
(char *identifier, SgType &t, SgSymbol &structureName)
SgFieldSymb
is used in the following example programs:
Represents C symbols for members of structs and classes.
class SgMemberFuncSymb: public SgFunctionSymb { // a member function for a class or struct or collection // variant = MEMBER_FUNC // may be either MEMB_PRIVATE, MEMB_PUBLIC, // MEMP_METHOELEM or MEMB_PROTECTED public: inline SgMemberFuncSymb(char *identifier, SgType &t, SgStatement &cla, int status); inline ~SgMemberFuncSymb(); inline int isMethodOfElement(); inline SgSymbol *className(); inline void setClassName(SgSymbol &symb); };
SgMemberFuncSymb
(char *identifier, SgType &t, SgStatement &cla, int status)
void setClassName
(SgSymbol &symb)
SgMemberFuncSymb
is used in the following example programs:
Represents Fortran symbols for label variables for assigned goto statements.
class SgLabelVarSymb: public SgSymbol { // a Fortran label variable for an assigned goto stmt // variant == LABEL_NAME public: inline SgLabelVarSymb(char *name, SgStatement &scope); inline ~SgLabelVarSymb(); };
SgLabelVarSymb
(char *name, SgStatement &scope)
SgLabelVarSymb
is used in the following example programs:
Represents Fortran symbols for external functions.
class SgExternalSymb: public SgSymbol { // for fortran external statement // variant == ROUTINE_NAME public: inline SgExternalSymb(char *name, SgStatement &scope); inline ~SgExternalSymb(); };
SgExternalSymb
(char *name, SgStatement &scope)
SgExternalSymb
is used in the following example programs:
Represents Fortran symbols for construct names.
class SgConstructSymb: public SgSymbol { // for fortran statement with construct names // variant == CONSTRUCT_NAME public: inline SgConstructSymb(char *name, SgStatement &scope); inline ~SgConstructSymb(); };
SgConstructSymb
(char *name, SgStatement &scope)
SgConstructSymb
is used in the following example programs:
Represents Fortran symbols for module statements.
class SgModuleSymb: public SgSymbol { // for fortran module statement // variant == MODULE_NAME public: inline SgModuleSymb(char *name); inline ~SgModuleSymb(); };
SgModuleSymb
is used in the following example programs:
Represents Fortran 90 symbols for module interface statements.
class SgInterfaceSymb: public SgSymbol { // for fortran interface statement // variant == INTERFACE_NAME public: inline SgInterfaceSymb(char *name, SgStatement &scope); inline ~SgInterfaceSymb(); };
SgInterfaceSymb
(char *name, SgStatement &scope)
SgInterfaceSymb
is used in the following example programs: