summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/typechecker/SyntheticMethods.scala
blob: caf0573f3d30e362c68656662990bebff6c0f346 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/* NSC -- new Scala compiler
 * Copyright 2005-2006 LAMP/EPFL
 * @author Martin Odersky
 */
// $Id$

package scala.tools.nsc.typechecker

import symtab.Flags._
import util.FreshNameCreator
import scala.collection.mutable.ListBuffer

/** <ul>
 *    <li>
 *      <code>caseArity</code>, <code>caseElement</code> implementations added
 *      to case classes
 *    </li>
 *    <li>
 *      <code>equals</code>, <code>hashCode</code> and </code>toString</code>
 *      methods are added to case classes, unless they are defined in the
 *      class or a baseclass different from <code>java.lang.Object</code>
 *    </li>
 *    <li>
 *      <code>toString</code> method is added to case objects, unless they
 *      are defined in the class or a baseclass different from
 *      <code>java.lang.Object</code>
 *    </li>
 *  </ul>
 */
trait SyntheticMethods requires Analyzer {
  import global._                  // the global environment
  import definitions._             // standard classes and methods
  import typer.{typed}             // methods to type trees

  /**
   *  @param templ ...
   *  @param clazz ...
   *  @param unit  ...
   *  @return      ...
   */
  def addSyntheticMethods(templ: Template, clazz: Symbol, unit: CompilationUnit): Template = {

    def hasImplementation(name: Name): Boolean = {
      val sym = clazz.info.nonPrivateMember(name)
      (sym.isTerm &&
       (sym.owner == clazz ||
        !(ObjectClass isNonBottomSubClass sym.owner) && !(sym hasFlag DEFERRED)))
    }

    def syntheticMethod(name: Name, flags: Int, tpe: Type) =
      newSyntheticMethod(name, flags | OVERRIDE, tpe)

    def newSyntheticMethod(name: Name, flags: Int, tpe: Type) = {
      val method = clazz.newMethod(clazz.pos, name) setFlag (flags) setInfo tpe
      clazz.info.decls.enter(method)
      method
    }

    def productSelectorMethod(n: int, accessor: Symbol): Tree = {
      val method = syntheticMethod(newTermName("_"+n), FINAL, accessor.tpe)
      typed(DefDef(method, vparamss => gen.mkAttributedRef(accessor)))
    }

    def caseElementMethod: Tree = {
      val method = syntheticMethod(
        nme.caseElement, FINAL, MethodType(List(IntClass.tpe), AnyClass.tpe))
      val caseFields = clazz.caseFieldAccessors map gen.mkAttributedRef
      typed(
        DefDef(method, {vparamss =>
            val doThrow:Tree = Throw(New(TypeTree(definitions.IndexOutOfBoundsExceptionClass.tpe),
                                         List(List(Select(Ident(vparamss.head.head), nme.toString_)))))
            if (caseFields.isEmpty) doThrow
            else {
              var i = caseFields.length
              var cases = List(CaseDef(Ident(nme.WILDCARD), EmptyTree, doThrow))
              for (val field <- caseFields.reverse) {
                i = i - 1; cases = CaseDef(Literal(Constant(i)), EmptyTree, field) :: cases
              }
              Match(Ident(vparamss.head.head), cases)
            }
        }))
    }
    def caseArityMethod: Tree = {
      val method = syntheticMethod(nme.caseArity, FINAL, PolyType(List(), IntClass.tpe))
      typed(DefDef(method, vparamss => Literal(Constant(clazz.caseFieldAccessors.length))))
    }

    def caseNameMethod: Tree = {
      val method = syntheticMethod(nme.caseName, FINAL, PolyType(List(), StringClass.tpe))
      typed(DefDef(method, vparamss => Literal(Constant(clazz.name.decode))))
    }

    def moduleToStringMethod: Tree = {
      val method = syntheticMethod(nme.toString_, FINAL, MethodType(List(), StringClass.tpe))
      typed(DefDef(method, vparamss => Literal(Constant(clazz.name.decode))))
    }

    def tagMethod: Tree = {
      val method = syntheticMethod(nme.tag, FINAL, MethodType(List(), IntClass.tpe))
      typed(DefDef(method, vparamss => Literal(Constant(clazz.tag))))
    }

    def forwardingMethod(name: Name): Tree = {
      val target = getMember(ScalaRunTimeModule, "_" + name)
      val paramtypes =
        if (target.tpe.paramTypes.isEmpty) List()
        else target.tpe.paramTypes.tail
      val method = syntheticMethod(
        name, 0, MethodType(paramtypes, target.tpe.resultType))
      typed(DefDef(method, vparamss =>
        Apply(gen.mkAttributedRef(target), This(clazz) :: (vparamss.head map Ident))))
    }

    def equalsMethod: Tree = {
      val target = getMember(ScalaRunTimeModule, nme._equals)
      val paramtypes =
        if (target.tpe.paramTypes.isEmpty) List()
        else target.tpe.paramTypes.tail
      val method = syntheticMethod(
       nme.equals_, 0, MethodType(paramtypes, target.tpe.resultType))
      typed(DefDef(method, vparamss =>
        Apply(
          Select(
            TypeApply(
              Select(Ident(vparamss.head.head), Any_isInstanceOf),
              List(TypeTree(clazz.tpe))),
            Boolean_and),
          List(
            Apply(gen.mkAttributedRef(target), This(clazz) :: (vparamss.head map Ident))))));
    }

    def isSerializable(clazz: Symbol): Boolean =
      !clazz.getAttributes(definitions.SerializableAttr).isEmpty

    def readResolveMethod: Tree = {
      // !!! the synthetic method "readResolve" should be private,
      // but then it is renamed !!!
      val method = newSyntheticMethod(nme.readResolve, PROTECTED,
                                      MethodType(List(), ObjectClass.tpe))
      typed(DefDef(method, vparamss => gen.mkAttributedRef(clazz.sourceModule)))
    }

    def newAccessorMethod(tree: Tree): Tree = tree match {
      case DefDef(_, _, _, _, _, rhs) =>
        val newAcc = tree.symbol.cloneSymbol
        newAcc.name = unit.fresh.newName("" + tree.symbol.name + "$")
        newAcc.setFlag(SYNTHETIC).resetFlag(ACCESSOR | PARAMACCESSOR)
        newAcc.owner.info.decls enter newAcc
        val result = typed(DefDef(newAcc, vparamss => rhs.duplicate))
        log("new accessor method " + result)
        result
    }

    def beanSetterOrGetter(sym: Symbol): Symbol =
      if (!sym.name(0).isLetter) {
        unit.error(sym.pos, "attribute `BeanProperty' can be applied only to fields that start with a letter")
        NoSymbol
      } else {
        var name0 = sym.name
        if (sym.isSetter) name0 = nme.setterToGetter(name0)
        val prefix = if (sym.isSetter) "set" else
          if (sym.tpe.resultType == BooleanClass.tpe) "is" else "get"
        val arity = if (sym.isSetter) 1 else 0
        val name1 = prefix + name0(0).toUpperCase + name0.subName(1, name0.length)
        val sym1 = clazz.info.decl(name1)
        if (sym1 != NoSymbol && sym1.tpe.paramTypes.length == arity) {
          unit.error(sym.pos, "a definition of `"+name1+"' already exists in " + clazz)
          NoSymbol
        } else {
          clazz.newMethod(sym.pos, name1)
            .setInfo(sym.info)
            .setFlag(sym.getFlag(DEFERRED | OVERRIDE | STATIC))
        }
      }

    val ts = new ListBuffer[Tree]

    def addBeanGetterMethod(sym: Symbol) = {
      val getter = beanSetterOrGetter(sym)
      if (getter != NoSymbol)
        ts += typed(DefDef(
          getter,
          vparamss => if (sym hasFlag DEFERRED) EmptyTree else gen.mkAttributedRef(sym)))
    }

    def addBeanSetterMethod(sym: Symbol) = {
      val setter = beanSetterOrGetter(sym)
      if (setter != NoSymbol)
        ts += typed(DefDef(
          setter,
          vparamss =>
            if (sym hasFlag DEFERRED) EmptyTree
            else Apply(gen.mkAttributedRef(sym), List(Ident(vparamss.head.head)))))
    }

    def isPublic(sym: Symbol) =
      !sym.hasFlag(PRIVATE | PROTECTED) && sym.privateWithin == NoSymbol

    if (!phase.erasedTypes) {

      if (clazz hasFlag CASE) {
        // case classes are implicitly declared serializable
        clazz.attributes = Triple(SerializableAttr.tpe, List(), List()) :: clazz.attributes

        for (val stat <- templ.body) {
          if (stat.isDef && stat.symbol.isMethod && stat.symbol.hasFlag(CASEACCESSOR) && !isPublic(stat.symbol)) {
            ts += newAccessorMethod(stat)
            stat.symbol.resetFlag(CASEACCESSOR)
          }
        }

        if (clazz.info.nonPrivateDecl(nme.tag) == NoSymbol) ts += tagMethod
        if (clazz.isModuleClass) {
          if (!hasImplementation(nme.toString_)) ts += moduleToStringMethod
        } else {
          if (!hasImplementation(nme.hashCode_)) ts += forwardingMethod(nme.hashCode_)
          if (!hasImplementation(nme.toString_)) ts += forwardingMethod(nme.toString_)
          if (!hasImplementation(nme.equals_)) ts += equalsMethod //forwardingMethod(nme.equals_)
        }
        if (!hasImplementation(nme.caseElement)) ts += caseElementMethod
        if (!hasImplementation(nme.caseArity)) ts += caseArityMethod
        if (!hasImplementation(nme.caseName)) ts += caseNameMethod

	val accessors = if(clazz hasFlag CASE) clazz.caseFieldAccessors else clazz.constrParamAccessors
        for (val i <- 0 until accessors.length) {
          val acc = accessors(i)
          if (acc.name.toString != "_"+(i+1)) ts += productSelectorMethod(i+1, acc)
        }
      }

      if (clazz.isModuleClass && isSerializable(clazz)) {
        // If you serialize a singleton and then deserialize it twice,
        // you will have two instances of your singleton, unless you implement
        // the readResolve() method (see http://www.javaworld.com/javaworld/
        // jw-04-2003/jw-0425-designpatterns_p.html)
        if (!hasImplementation(nme.readResolve)) ts += readResolveMethod
      }
      if (!forCLDC)
        for (val sym <- clazz.info.decls.toList)
          if (!sym.getAttributes(BeanPropertyAttr).isEmpty)
            if (sym.isGetter)
              addBeanGetterMethod(sym)
            else if (sym.isSetter)
              addBeanSetterMethod(sym)
            else if (sym.isMethod || sym.isType)
              unit.error(sym.pos, "attribute `BeanProperty' is not applicable to " + sym)
    }
    val synthetics = ts.toList
    copy.Template(
      templ, templ.parents, if (synthetics.isEmpty) templ.body else templ.body ::: synthetics)
  }
}