| 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 | |
|---|
| 18 | package com.sun.fortress.scala_src.typechecker.staticenv |
|---|
| 19 | |
|---|
| 20 | import com.sun.fortress.nodes._ |
|---|
| 21 | import com.sun.fortress.nodes_util.Modifiers |
|---|
| 22 | import scala.collection.immutable.EmptyMap |
|---|
| 23 | |
|---|
| 24 | /** |
|---|
| 25 | * Represents a list of variable name to type bindings for some context. All |
|---|
| 26 | * instances of this class should be created with `STypeEnv.make` or with the |
|---|
| 27 | * `extendWith` method. |
|---|
| 28 | */ |
|---|
| 29 | abstract sealed class STypeEnv extends StaticEnv[Type] { |
|---|
| 30 | |
|---|
| 31 | /** My type. */ |
|---|
| 32 | type Env = STypeEnv |
|---|
| 33 | |
|---|
| 34 | /** My binding type. */ |
|---|
| 35 | type EnvBinding = TypeBinding |
|---|
| 36 | |
|---|
| 37 | /** Extend me with the immediate bindings of the given node. */ |
|---|
| 38 | def extendWith(node: Any): STypeEnv = |
|---|
| 39 | new NestedSTypeEnv(this, STypeEnv.extractEnvBindings(node)) |
|---|
| 40 | |
|---|
| 41 | /** Same as `lookup`. */ |
|---|
| 42 | def getType(x: Name): Option[Type] = lookup(x).map(_.value) |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | /** The single empty type environment. */ |
|---|
| 46 | object EmptySTypeEnv extends STypeEnv with EmptyStaticEnv[Type] |
|---|
| 47 | |
|---|
| 48 | /** |
|---|
| 49 | * A type environment with a parent and some explicit bindings. |
|---|
| 50 | * |
|---|
| 51 | * @param parent A type environment that this one extends. |
|---|
| 52 | * @param _bindings A collection of all the bindings in this environment. |
|---|
| 53 | */ |
|---|
| 54 | class NestedSTypeEnv protected (protected val parent: STypeEnv, |
|---|
| 55 | _bindings: Collection[TypeBinding]) |
|---|
| 56 | extends STypeEnv with NestedStaticEnv[Type] { |
|---|
| 57 | |
|---|
| 58 | /** Internal representation of `bindings` is a map. */ |
|---|
| 59 | protected val bindings: Map[Name, TypeBinding] = |
|---|
| 60 | new EmptyMap ++ _bindings.map(b => b.name -> b) |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | /** Companion module for STypeEnv. */ |
|---|
| 64 | object STypeEnv extends StaticEnvCompanion[Type] with STypeEnvExtraction { |
|---|
| 65 | |
|---|
| 66 | /** My type. */ |
|---|
| 67 | type Env = STypeEnv |
|---|
| 68 | |
|---|
| 69 | /** My binding type. */ |
|---|
| 70 | type EnvBinding = TypeBinding |
|---|
| 71 | |
|---|
| 72 | /** New type environment with empty parent and the node's bindings. */ |
|---|
| 73 | def make(node: Any): STypeEnv = |
|---|
| 74 | new NestedSTypeEnv(EmptySTypeEnv, extractEnvBindings(node)) |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | /** |
|---|
| 79 | * A binding for a type environment contains name to type pairs, along with some |
|---|
| 80 | * modifiers for the binding and whether or not the binding is mutable. |
|---|
| 81 | * |
|---|
| 82 | * @param name The variable name for the binding. |
|---|
| 83 | * @param value The bound type for this variable name. |
|---|
| 84 | * @param mods Any modifiers for the binding. |
|---|
| 85 | * @param mutable Whether or not the binding is mutable. |
|---|
| 86 | */ |
|---|
| 87 | case class TypeBinding(override val name: Name, |
|---|
| 88 | typ: Type, |
|---|
| 89 | mods: Modifiers, |
|---|
| 90 | mutable: Boolean) extends StaticBinding[Type](name, typ) |
|---|