view ast/Decl.d @ 201:4f94b5adbc8a

Added ast/Expr.d to contain all expressions.
author Anders Johnsen <skabet@gmail.com>
date Mon, 11 Aug 2008 18:41:45 +0200
parents 5e05c03d1558
children
line wrap: on
line source

module ast.Decl;

import ast.Expr;

/**
  The base class for all Declarations. 
  
  Declarations comes in two forms:

    $(OL
    $(LI members, such as variables and functions.)
    $(LI types, such as classes, structs.))

  */
class Decl
{

    /// Method for doing cast(FunctionDecl)
    FunctionDecl asFunctionDecl() { return null;}

    /// Returns true if it is a FunctionDecl
    bool isFunctionDecl() { return false;}

    /// Method for doing cast(VarDecl)
    VarDecl asVarDecl() { return null;}

    /// Returns true if it is a VarDecl
    bool isVarDecl() { return false;}

    /// Method for doing cast(StructDecl)
    StructDecl asStructDecl() { return null;}

    /// Returns true if it is a StructDecl
    bool isStructDecl() { return false;}

    /// Method for doing cast(ClassDecl)
    ClassDecl asClassDecl() { return null;}

    /// Returns true if it is a ClassDecl
    bool isClassDecl() { return false;}

    /// Method for doing cast(InterfaceDecl)
    InterfaceDecl asInterfaceDecl() { return null;}

    /// Returns true if it is a InterfaceDecl
    bool isInterfaceDecl() { return false;}

    /// Method for doing cast(TypedefDecl)
    TypedefDecl asTypedefDecl() { return null;}

    /// Returns true if it is a TypedefDecl
    bool isTypedefDecl() { return false;}
}

/**
  The FunctionDecl contains a set of VarDecls, being the parameters of the
  method. It also contains a potentiel list of statements.
  */
class FunctionDecl : Decl
{
    /// Returns the parameters of the method.
    VarDecl[] getParams() { return params;}
    /// Return the parameter on a given index.
    VarDecl getParam(int index) { return params[index];}
    /// Returns the number of parameters.
    int getNumberOfParamst() { return params.length;}
    /**
      Returns the number of required arguments, that should be given to
      call this method.
      */
    int getMinimumNumberOfParams() 
    { 
        assert(0, "Unimplemented");
        return 0;
    }

    override FunctionDecl asFunctionDecl() { return this;}
    override bool isFunctionDecl() { return true;}

private:
    VarDecl[] params;
}

/**
  The VarDecl contains as a minimum a Type. As an addition, i will also
  in most cases contain an Identifier that it'll be reconized by and in
  some cases it will also contain an expression that'll be it initializer.

  Some cases to check:

  $(UL
      $(LI
          If the VarDecl is a param in a FunctionDecl the initializer, if
          present, should be a constant expression(Enum, Number, String 
          or the like).
        )
    )
  */
class VarDecl : Decl
{
    /// Return true if the VarDecl has an identifier/name.
    bool hasIdentifier() { return !(identifier is null);}
    /// Return true if the VarDecl has en initializer.
    bool hasInitializer() { return !(initializer is null);}

    override VarDecl asVarDecl() { return this;}
    override bool isVarDecl() { return true;}
    
private:
    Identifier identifier;
    Expr initializer;
}

class StructDecl : Decl
{
    override StructDecl asStructDecl() { return this;}
    override bool isStructDecl() { return true;}
}

class ClassDecl : Decl
{
    override ClassDecl asClassDecl() { return this;}
    override bool isClassDecl() { return true;}
}

class InterfaceDecl : Decl
{
    override InterfaceDecl asInterfaceDecl() { return this;}
    override bool isInterfaceDecl() { return true;}
}

class TypedefDecl : Decl
{
    override TypedefDecl asTypedefDecl() { return this;}
    override bool isTypedefDecl() { return true;}
}