| 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.compiler.Types |
|---|
| 21 | import com.sun.fortress.nodes._ |
|---|
| 22 | import scala.collection.immutable.EmptyMap |
|---|
| 23 | |
|---|
| 24 | /** |
|---|
| 25 | * Represents a list of variable name to static parameter bindings for some |
|---|
| 26 | * context. All instances of this class should be created with `KindEnv.make` |
|---|
| 27 | * or with the `extendWith` method. |
|---|
| 28 | */ |
|---|
| 29 | abstract sealed class KindEnv extends StaticEnv[StaticParam] { |
|---|
| 30 | |
|---|
| 31 | /** My type. */ |
|---|
| 32 | type Env = KindEnv |
|---|
| 33 | |
|---|
| 34 | /** My binding type. */ |
|---|
| 35 | type EnvBinding = KindBinding |
|---|
| 36 | |
|---|
| 37 | /** Extend me with the immediate bindings of the given node. */ |
|---|
| 38 | def extendWith(node: Any): KindEnv = |
|---|
| 39 | new NestedKindEnv(this, KindEnv.extractEnvBindings(node)) |
|---|
| 40 | |
|---|
| 41 | /** |
|---|
| 42 | * Get the type of the given variable name, if it is bound. The resulting |
|---|
| 43 | * type will only be defined if this name maps to a bool, int, or nat static |
|---|
| 44 | * parameter. |
|---|
| 45 | */ |
|---|
| 46 | def getType(x: Name): Option[Type] = lookup(x).flatMap(b => |
|---|
| 47 | b.value.getKind match { |
|---|
| 48 | case _:KindBool => Some(Types.BOOLEAN) |
|---|
| 49 | case _:KindInt => Some(Types.INT_LITERAL) |
|---|
| 50 | case _:KindNat => Some(Types.INT_LITERAL) |
|---|
| 51 | case _ => None |
|---|
| 52 | }) |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | /** The single empty kind environment. */ |
|---|
| 56 | object EmptyKindEnv extends KindEnv with EmptyStaticEnv[StaticParam] |
|---|
| 57 | |
|---|
| 58 | /** |
|---|
| 59 | * A kind environment with a parent and some explicit bindings. |
|---|
| 60 | * |
|---|
| 61 | * @param parent A kind environment that this one extends. |
|---|
| 62 | * @param _bindings A collection of all the bindings in this environment. |
|---|
| 63 | */ |
|---|
| 64 | class NestedKindEnv protected (protected val parent: KindEnv, |
|---|
| 65 | _bindings: Collection[KindBinding]) |
|---|
| 66 | extends KindEnv with NestedStaticEnv[StaticParam] { |
|---|
| 67 | |
|---|
| 68 | /** Internal representation of `bindings` is a map. */ |
|---|
| 69 | protected val bindings: Map[Name, KindBinding] = |
|---|
| 70 | new EmptyMap ++ _bindings.map(b => b.name -> b) |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | /** Companion module for KindEnv; contains "static" members. */ |
|---|
| 74 | object KindEnv extends StaticEnvCompanion[StaticParam] { |
|---|
| 75 | |
|---|
| 76 | /** My type. */ |
|---|
| 77 | type Env = KindEnv |
|---|
| 78 | |
|---|
| 79 | /** My binding type. */ |
|---|
| 80 | type EnvBinding = KindBinding |
|---|
| 81 | |
|---|
| 82 | /** New kind environment with empty parent and the node's bindings. */ |
|---|
| 83 | def make(node: Any): KindEnv = |
|---|
| 84 | new NestedKindEnv(EmptyKindEnv, extractEnvBindings(node)) |
|---|
| 85 | |
|---|
| 86 | /** Extract out the bindings in node. */ |
|---|
| 87 | protected def extractEnvBindings(node: Any) : Collection[KindBinding] = null |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | /** |
|---|
| 91 | * A binding for a kind environment contains name to static parameter pairs. |
|---|
| 92 | * |
|---|
| 93 | * @param name The variable name for the binding. |
|---|
| 94 | * @param sparam The static parameter that this name is binding. |
|---|
| 95 | */ |
|---|
| 96 | case class KindBinding(override val name: Name, |
|---|
| 97 | sparam: StaticParam) |
|---|
| 98 | extends StaticBinding[StaticParam](name, sparam) |
|---|