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

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

[index] Changed all Functional indices to store a thunk to get the return types, since during type checking the return types might need to be lazily evaluated. Also changed Functional indices to yield an Option for return types.
[type checker] Started implementing extraction of bindings from nodes for type environments.

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