Add {-# OPTIONS_GHC -w #-} and some blurb to all compiler modules
[ghc-hetmet.git] / compiler / javaGen / Java.lhs
1 Anbstract syntax for Java subset that is the target of Mondrian.
2 The syntax has been taken from "The Java Language Specification".
3
4 (c) Erik Meijer & Arjan van IJzendoorn
5
6 November 1999
7
8 Major reworking to be usable for the intermeduate (GOO) language
9 for the backend of GHC and to target languauges like Java sucessfully.
10 -- Andy Gill
11
12 \begin{code}
13 {-# OPTIONS_GHC -w #-}
14 -- The above warning supression flag is a temporary kludge.
15 -- While working on this module you are encouraged to remove it and fix
16 -- any warnings in the module. See
17 --     http://hackage.haskell.org/trac/ghc/wiki/WorkingConventions#Warnings
18 -- for details
19
20 module Java where
21
22 \end{code}
23
24 %************************************************************************
25 %*                                                                      *
26 \subsection{Java type declararations}
27 %*                                                                      *
28 %************************************************************************
29
30 \begin{code}
31 data CompilationUnit
32   = Package PackageName [Decl]
33     deriving (Show)
34     
35 data Decl
36  = Import PackageName
37  | Field [Modifier] Name (Maybe Expr)
38  | Constructor [Modifier] TypeName [Parameter] [Statement]
39  | Method [Modifier] Name [Parameter] [Exception] [Statement]
40  | Comment [String]
41  | Interface [Modifier] TypeName [TypeName] [Decl]
42  | Class [Modifier] TypeName [TypeName] [TypeName] [Decl]
43    deriving (Show)
44
45 data Parameter
46  = Parameter [Modifier] Name
47    deriving (Show)
48    
49 data Statement
50   = Skip
51   | Return Expr         -- This always comes last in a list
52                         -- of statements, and it is understood
53                         -- you might change this to something
54                         -- else (like a variable assignment)
55                         -- if this is not top level statements.
56   | Block [Statement]
57   | ExprStatement Expr  -- You are never interested in the result
58                         -- of an ExprStatement
59   | Declaration Decl -- variable = inner Field, Class = innerclass
60   | IfThenElse [(Expr,Statement)] (Maybe Statement)
61   | Switch Expr [(Expr, [Statement])] (Maybe [Statement])
62     deriving (Show)
63
64 data Expr 
65   = Var Name
66   | Literal Lit
67   | Cast Type Expr
68   | Access Expr Name
69   | Assign Expr Expr
70   | InstanceOf Expr Type
71   | Call Expr Name [Expr]
72   | Op Expr String Expr
73   | Raise TypeName [Expr]
74   | New Type [Expr] (Maybe [Decl]) -- anonymous innerclass
75     deriving (Show)
76     
77 data Modifier 
78   = Public | Protected | Private
79   | Static
80   | Abstract | Final | Native | Synchronized | Transient | Volatile
81   deriving (Show, Eq, Ord)
82
83 -- A type is used to refer in general to the shape of things,
84 -- or a specific class. Never use a name to refer to a class,
85 -- always use a type.
86
87 data Type 
88   = PrimType  PrimType
89   | ArrayType Type
90   | Type      TypeName
91     deriving (Show, Eq)
92
93 data PrimType 
94   = PrimInt 
95   | PrimBoolean
96   | PrimChar
97   | PrimLong
98   | PrimFloat
99   | PrimDouble
100   | PrimByte
101   | PrimVoid
102     deriving (Show, Eq)
103
104 type PackageName = String       -- A package name
105                                 -- like "java.awt.Button"
106
107 type Exception   = TypeName     -- A class name that must be an exception.
108
109 type TypeName    = String       -- a fully qualified type name
110                                 -- like "java.lang.Object".
111                                 -- has type "Type <the name>"
112
113 data Name        = Name String Type
114         deriving Show           -- A class name or method etc, 
115                                 -- at defintion time,
116                                 -- this generally not a qualified name.
117
118                                 -- The type is shape of the box require
119                                 -- to store an access to this thing.
120                                 -- So variables might be Int or Object.
121
122                                 --  ** method calls store the returned
123                                 --  ** type, not a complete arg x result type.
124                                 --
125                                 -- Thinking:
126                                 -- ... foo1.foo2(...).foo3 ...
127                                 -- here you want to know the *result*
128                                 -- after calling foo1, then foo2,
129                                 -- then foo3.
130
131 instance Eq Name where
132    (Name nm _) == (Name nm' _) = nm == nm'
133
134
135 instance Ord Name where
136    (Name nm _) `compare` (Name nm' _) = nm `compare` nm'
137
138
139 data Lit
140   = IntLit Integer      -- unboxed
141   | CharLit Int         -- unboxed
142   | StringLit String    -- java string
143   deriving Show
144
145 addModifier :: Modifier -> Decl -> Decl
146 addModifier = \m -> \d ->
147  case d of
148    { Import n -> Import n
149    ; Field ms n e -> Field (m:ms) n e  
150    ; Constructor ms n as ss -> Constructor (m:ms) n as ss
151    ; Method ms n as ts ss -> Method (m:ms) n as ts ss
152    ; Comment ss -> Comment ss
153    ; Interface ms n xs ds -> Interface (m:ms) n xs ds
154    ; Class ms n xs is ds -> Class (m:ms) n xs is ds
155    }
156
157 changeNameType :: Type -> Name -> Name
158 changeNameType ty (Name n _) = Name n ty
159    
160 areSimple :: [Expr] -> Bool
161 areSimple = \es -> all isSimple es
162
163 isSimple :: Expr -> Bool
164 isSimple = \e ->
165   case e of
166    { Cast t e -> isSimple e
167    ; Access e n -> isSimple e
168    ; Assign l r -> isSimple l && isSimple r
169    ; InstanceOf e t -> isSimple e
170    ; Call e n es -> isSimple e && areSimple es
171    ; Op e1 o e2 -> False
172    ; New n es Nothing -> areSimple es
173    ; New n es (Just ds) -> False
174    ; otherwise -> True
175    }
176 \end{code}