root/trunk/ProjectFortress/src/com/sun/fortress/scala_src/typechecker/StaticEnv.scala @ 3905

Revision 3905, 5.1 KB (checked in by jrhil47, 5 months ago)

[static env] Added some more code to new static environments regarding the binding objects. Still not integrated.

Line 
1/*******************************************************************************
2    Copyright 2009 Sun Microsystems, Inc.,
3    4150 Network Circle, Santa Clara, California 95054, U.S.A.
4    All rights reserved.
5
6    U.S. Government Rights - Commercial software.
7    Government users are subject to the Sun Microsystems, Inc. standard
8    license agreement and applicable provisions of the FAR and its supplements.
9
10    Use is subject to license terms.
11
12    This distribution may include materials developed by third parties.
13
14    Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered
15    trademarks of Sun Microsystems, Inc. in the U.S. and other countries.
16 ******************************************************************************/
17
18package com.sun.fortress.scala_src.typechecker
19
20import com.sun.fortress.nodes.Name
21import com.sun.fortress.nodes.Type
22import com.sun.fortress.nodes_util.Modifiers
23
24/**
25 * Represents an environment that exists during static checking, mapping
26 * variable names to some value. Each static environment contains
27 */
28trait StaticEnv[T] extends Collection[StaticBinding[T]] {
29 
30  /** Define Env as the type of the implementing class. */
31  type Env <: StaticEnv[T]
32 
33  /** Define EnvBinding as the type of the bindings. */
34  type EnvBinding <: StaticBinding[T]
35 
36  /**
37   * Extend this environment with the bindings immediately contained in the
38   * given node.
39   *
40   * @param node A node or collection of nodes in which to find bindings.
41   * @return A new environment with the bindings of this one and those found in
42   *         `node` combined.
43   */
44  def extendWith(node: Any): Env
45 
46  /**
47   * Gets the value stored for the given variable name, if that binding exists.
48   *
49   * @param name The name to lookup.
50   * @return Some(T) if name:T is a binding. None otherwise.
51   */
52  def lookup(x: Name): Option[EnvBinding]
53 
54  /** Same as lookup when treating implementing object as a function. */
55  def apply(x: Name): Option[EnvBinding] = lookup(x)
56 
57  /**
58   * Gets the type stored for the given variable name, if that binding exists.
59   *
60   * @param name The name to lookup.
61   * @return Some(T) if name:T is a binding. None otherwise.
62   */
63  def getType(x: Name): Option[Type]
64 
65  /** Make the type on `Collection.elements` more specific. */
66  def elements: Iterator[EnvBinding]
67}
68
69/**
70 * A single empty static environment. There are no bindings at all in this
71 * environemnt.
72 */
73trait EmptyStaticEnv[T] extends StaticEnv[T] {
74     
75  /** Every call to `lookup` fails. */
76  override def lookup(x: Name): Option[EnvBinding] = None
77     
78  /** Every call to `getType` fails. */
79  override def getType(x: Name): Option[Type] = None
80 
81  // Collection implementation
82  override def elements: Iterator[EnvBinding] = Iterator.empty
83  override def size: Int = 0
84}
85
86/**
87 * A nested static environment that contains all the bindings of some parent
88 * environment and additionally all the explicitly supplied bindings.
89 */
90trait NestedStaticEnv[T] extends StaticEnv[T] {
91   
92  /** A static environment that this one extends. */
93  protected val parent: Env {
94    // Require that the parent has the same binding type.
95    type EnvBinding = NestedStaticEnv.this.EnvBinding
96  }
97   
98  /** The bindings explicitly declared in this environment. */
99  protected val bindings: Map[Name, EnvBinding]
100 
101  /** Find it among `bindings` or else in `parent`. */
102  def lookup(x: Name): Option[EnvBinding] = bindings.get(x) match {
103    case Some(v) => Some(v)
104    case None => parent.lookup(x)
105  }
106 
107  // Collection implementation
108  def elements: Iterator[EnvBinding] = parent.elements ++ bindings.values
109  def size: Int = parent.size + bindings.size
110}
111
112/**
113 * Provides functionality for finding bindings in nodes and creating new
114 * instances of the environment.
115 */
116trait StaticEnvCompanion[T] {
117 
118  /** Define Env as the type of the implementing object's companion class. */
119  type Env <: StaticEnv[T]
120 
121  /** Define EnvBinding as the type of the bindings. */
122  type EnvBinding <: StaticBinding[T]
123 
124  /**
125   * Extracts all the _immediate_ bindings for this kind of environment from the
126   * given node. If `node` is a collection of nodes, then this method returns
127   * the concatenation of all bindings found therein. Any bindings located
128   * further inside the node will not be extracted.
129   *
130   * @param node A node or collection of nodes in which to extract bindings.
131   * @return A collection of all the bindings extracted in the given node.
132   */
133  protected def extractEnvBindings(node: Any): Collection[EnvBinding]
134 
135  /**
136   * Creates a new instance of the environment containing all the bindings
137   * found in the given node.
138   *
139   * @param node A node or collection of nodes in which to find bindings.
140   * @return A new instance of Env containing these bindings.
141   */
142  def make(node: Any): Env
143}
144
145
146/**
147 * A static environment binding of a variable name to some kind of value.
148 *
149 * @param name The variable name for the binding.
150 * @param value The bound value for this binding.
151 */
152abstract class StaticBinding[T](val name: Name, val value: T)
153
154object StaticBinding {
155  def unapply[T](b: StaticBinding[T]): Option[(Name, T)] = Some(b.name, b.value)
156}
157
Note: See TracBrowser for help on using the browser.