1 //
2 // Copyright (c) 2006, Brian Frank and Andy Frank
3 // Licensed under the Academic Free License version 3.0
4 //
5 // History:
6 // 15 Sep 05 Brian Frank Creation
7 // 21 Jul 06 Brian Frank Ported from Java to Fan
8 //
9
10 **
11 ** FieldDef models a field definition
12 **
13 public class FieldDef : SlotDef, CField
14 {
15
16 //////////////////////////////////////////////////////////////////////////
17 // Construction
18 //////////////////////////////////////////////////////////////////////////
19
20 new make(Location location, TypeDef parent)
21 : super(location, parent)
22 {
23 }
24
25 //////////////////////////////////////////////////////////////////////////
26 // Access
27 //////////////////////////////////////////////////////////////////////////
28
29 Bool hasGet() { return get != null && !get.isSynthetic }
30 Bool hasSet() { return set != null && !set.isSynthetic }
31
32 FieldExpr makeAccessorExpr(Location loc, Bool useAccessor)
33 {
34 Expr target
35 if (isStatic)
36 target = StaticTargetExpr.make(loc, parent)
37 else
38 target = ThisExpr.make(loc)
39
40 return FieldExpr.make(loc, target, this, useAccessor)
41 }
42
43 //////////////////////////////////////////////////////////////////////////
44 // CField
45 //////////////////////////////////////////////////////////////////////////
46
47 override Str signature() { return qname }
48 override CMethod getter() { return get }
49 override CMethod setter() { return set }
50
51 override CType inheritedReturnType()
52 {
53 if (inheritedRet != null)
54 return inheritedRet
55 else
56 return fieldType
57 }
58
59 //////////////////////////////////////////////////////////////////////////
60 // Tree
61 //////////////////////////////////////////////////////////////////////////
62
63 override Void walk(Visitor v, VisitDepth depth)
64 {
65 v.enterFieldDef(this)
66 walkFacets(v, depth)
67 if (depth >= VisitDepth.expr && init != null)
68 init = init.walk(v)
69 v.visitFieldDef(this)
70 v.exitFieldDef(this)
71 }
72
73 //////////////////////////////////////////////////////////////////////////
74 // Debug
75 //////////////////////////////////////////////////////////////////////////
76
77 override Str toStr()
78 {
79 return "$fieldType $name"
80 }
81
82 override Void print(AstWriter out)
83 {
84 printFacets(out)
85 out.flags(flags)
86 if (fieldType != null) out.w(fieldType).w(" ")
87 out.w(name)
88 if (init != null) { out.w(" := "); init.print(out) }
89 out.nl.nl
90 }
91
92 //////////////////////////////////////////////////////////////////////////
93 // Fields
94 //////////////////////////////////////////////////////////////////////////
95
96 override CType fieldType // field type (null if inferred from init)
97 Field field // resolved finalized field
98 Expr init // init expression or null
99 MethodDef get // getter MethodDef
100 MethodDef set // setter MethodDef
101 CField concreteBase // if I override a concrete virtual field
102 CType inheritedRet // if covariant override of method
103
104 }