logo

class

compiler::ParamDef

sys::Obj
  compiler::Node
    compiler::ParamDef : compiler::CParam
//
// Copyright (c) 2006, Brian Frank and Andy Frank
// Licensed under the Academic Free License version 3.0
//
// History:
//   19 Jul 06  Brian Frank  Creation
//

**
** ParamDef models the definition of a method parameter.
**
class ParamDef : Node, CParam
{

//////////////////////////////////////////////////////////////////////////
// Construction
//////////////////////////////////////////////////////////////////////////

  new make(Location location, CType paramType := null, Str name := null, Expr def := null)
    : super(location)
  {
    this.paramType = paramType
    this.name = name
    this.def  = def
  }

//////////////////////////////////////////////////////////////////////////
// CParam
//////////////////////////////////////////////////////////////////////////

  override Bool hasDefault() { return def != null }

//////////////////////////////////////////////////////////////////////////
// Doc
//////////////////////////////////////////////////////////////////////////

  Str defDoc()
  {
    if (def == null) return null
    // not perfect, but better than what we had previously which
    // was nothing; we might want to grab the actual text from the
    // actual source file - but with the current design we've freed
    // the buffer by the time the tokens are passed to the parser
    s := def.toStr
    if (s[0] == '(' && s[-1] == ')') s = s[1..-2]
    if (s.contains("=")) s = s[s.index("=")+1..-1].trim
    if (s[0] == '(' && s[-1] == ')') s = s[1..-2]
    return s
  }

//////////////////////////////////////////////////////////////////////////
// Debug
//////////////////////////////////////////////////////////////////////////

  override Str toStr()
  {
    return "$paramType $name"
  }

  override Void print(AstWriter out)
  {
    out.w(paramType).w(" ").w(name)
    if (def != null) { out.w(" := "); def.print(out) }
  }

//////////////////////////////////////////////////////////////////////////
// Fields
//////////////////////////////////////////////////////////////////////////

  override CType paramType   // type of parameter
  override Str name          // local variable name
  Expr def                   // default expression

}