1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef INCLUDED_CODEMAKER_SOURCE_JAVAMAKER_CLASSFILE_HXX
25 #define INCLUDED_CODEMAKER_SOURCE_JAVAMAKER_CLASSFILE_HXX
26 
27 #include "codemaker/unotype.hxx"
28 #include "sal/types.h"
29 
30 #include <list>
31 #include <map>
32 #include <utility>
33 #include <vector>
34 
35 class FileStream;
36 namespace rtl { class OString; }
37 
38 namespace codemaker { namespace javamaker {
39 
40 class ClassFile {
41 public:
42     enum AccessFlags {
43         ACC_PUBLIC = 0x0001,
44         ACC_PRIVATE = 0x0002,
45         ACC_STATIC = 0x0008,
46         ACC_FINAL = 0x0010,
47         ACC_SUPER = 0x0020,
48         ACC_VARARGS = 0x0080,
49         ACC_INTERFACE = 0x0200,
50         ACC_ABSTRACT = 0x0400,
51         ACC_SYNTHETIC = 0x1000
52     };
53 
54     class Code {
55     public:
56         typedef std::vector< unsigned char >::size_type Branch;
57         typedef std::vector< unsigned char >::size_type Position;
58 
59         ~Code();
60 
61         void instrAastore();
62 
63         void instrAconstNull();
64 
65         void instrAnewarray(rtl::OString const & type);
66 
67         void instrAreturn();
68 
69         void instrAthrow();
70 
71         void instrCheckcast(rtl::OString const & type);
72 
73         void instrDup();
74 
75         void instrGetstatic(
76             rtl::OString const & type, rtl::OString const & name,
77             rtl::OString const & descriptor);
78 
79         Branch instrIfAcmpne();
80 
81         Branch instrIfeq();
82 
83         Branch instrIfnull();
84 
85         void instrInstanceof(rtl::OString const & type);
86 
87         void instrInvokeinterface(
88             rtl::OString const & type, rtl::OString const & name,
89             rtl::OString const & descriptor, sal_uInt8 args);
90 
91         void instrInvokespecial(
92             rtl::OString const & type, rtl::OString const & name,
93             rtl::OString const & descriptor);
94 
95         void instrInvokestatic(
96             rtl::OString const & type, rtl::OString const & name,
97             rtl::OString const & descriptor);
98 
99         void instrInvokevirtual(
100             rtl::OString const & type, rtl::OString const & name,
101             rtl::OString const & descriptor);
102 
103         void instrLookupswitch(
104             Code const * defaultBlock,
105             std::list< std::pair< sal_Int32, Code * > > const & blocks);
106 
107         void instrNew(rtl::OString const & type);
108 
109         void instrNewarray(codemaker::UnoType::Sort sort);
110 
111         void instrPop();
112 
113         void instrPutfield(
114             rtl::OString const & type, rtl::OString const & name,
115             rtl::OString const & descriptor);
116 
117         void instrPutstatic(
118             rtl::OString const & type, rtl::OString const & name,
119             rtl::OString const & descriptor);
120 
121         void instrReturn();
122 
123         void instrSwap();
124 
125         void instrTableswitch(
126             Code const * defaultBlock, sal_Int32 low,
127             std::list< Code * > const & blocks);
128 
129         void loadIntegerConstant(sal_Int32 value);
130 
131         void loadStringConstant(rtl::OString const & value);
132 
133         void loadLocalInteger(sal_uInt16 index);
134 
135         void loadLocalLong(sal_uInt16 index);
136 
137         void loadLocalFloat(sal_uInt16 index);
138 
139         void loadLocalDouble(sal_uInt16 index);
140 
141         void loadLocalReference(sal_uInt16 index);
142 
143         void storeLocalReference(sal_uInt16 index);
144 
145         void branchHere(Branch branch);
146 
147         void addException(
148             Position start, Position end, Position handler,
149             rtl::OString const & type);
150 
151         void setMaxStackAndLocals(sal_uInt16 maxStack, sal_uInt16 maxLocals)
152         { m_maxStack = maxStack; m_maxLocals = maxLocals; }
153 
154         Position getPosition() const;
155 
156     private:
157         Code(Code &); // not implemented
158         void operator =(Code); // not implemented
159 
160         Code(ClassFile & classFile);
161 
162         void ldc(sal_uInt16 index);
163 
164         void accessLocal(
165             sal_uInt16 index, sal_uInt8 fastOp, sal_uInt8 normalOp);
166 
167         ClassFile & m_classFile;
168         sal_uInt16 m_maxStack;
169         sal_uInt16 m_maxLocals;
170         std::vector< unsigned char > m_code;
171         sal_uInt16 m_exceptionTableLength;
172         std::vector< unsigned char > m_exceptionTable;
173 
174         friend class ClassFile;
175     };
176 
177     ClassFile(
178         AccessFlags accessFlags, rtl::OString const & thisClass,
179         rtl::OString const & superClass, rtl::OString const & signature);
180 
181     ~ClassFile();
182 
183     Code * newCode();
184 
185     sal_uInt16 addIntegerInfo(sal_Int32 value);
186 
187     sal_uInt16 addFloatInfo(float value);
188 
189     sal_uInt16 addLongInfo(sal_Int64 value);
190 
191     sal_uInt16 addDoubleInfo(double value);
192 
193     void addInterface(rtl::OString const & interface);
194 
195     void addField(
196         AccessFlags accessFlags, rtl::OString const & name,
197         rtl::OString const & descriptor, sal_uInt16 constantValueIndex,
198         rtl::OString const & signature);
199 
200     void addMethod(
201         AccessFlags accessFlags, rtl::OString const & name,
202         rtl::OString const & descriptor, Code const * code,
203         std::vector< rtl::OString > const & exceptions,
204         rtl::OString const & signature);
205 
206     void write(FileStream & file) const; //TODO
207 
208 private:
209     typedef std::map< rtl::OString, sal_uInt16 > Map;
210 
211     ClassFile(ClassFile &); // not implemented
212     void operator =(ClassFile); // not implemented
213 
214     sal_uInt16 nextConstantPoolIndex(sal_uInt16 width);
215 
216     sal_uInt16 addUtf8Info(rtl::OString const & value);
217 
218     sal_uInt16 addClassInfo(rtl::OString const & type);
219 
220     sal_uInt16 addStringInfo(rtl::OString const & value);
221 
222     sal_uInt16 addFieldrefInfo(
223         rtl::OString const & type, rtl::OString const & name,
224         rtl::OString const & descriptor);
225 
226     sal_uInt16 addMethodrefInfo(
227         rtl::OString const & type, rtl::OString const & name,
228         rtl::OString const & descriptor);
229 
230     sal_uInt16 addInterfaceMethodrefInfo(
231         rtl::OString const & type, rtl::OString const & name,
232         rtl::OString const & descriptor);
233 
234     sal_uInt16 addNameAndTypeInfo(
235         rtl::OString const & name, rtl::OString const & descriptor);
236 
237     void appendSignatureAttribute(
238         std::vector< unsigned char > & stream, rtl::OString const & signature);
239 
240     sal_uInt16 m_constantPoolCount;
241     std::vector< unsigned char > m_constantPool;
242     std::map< rtl::OString, sal_uInt16 > m_utf8Infos;
243     std::map< sal_Int32, sal_uInt16 > m_integerInfos;
244     std::map< sal_Int64, sal_uInt16 > m_longInfos;
245     std::map< float, sal_uInt16 > m_floatInfos;
246     std::map< double, sal_uInt16 > m_doubleInfos;
247     std::map< sal_uInt16, sal_uInt16 > m_classInfos;
248     std::map< sal_uInt16, sal_uInt16 > m_stringInfos;
249     std::map< sal_uInt32, sal_uInt16 > m_fieldrefInfos;
250     std::map< sal_uInt32, sal_uInt16 > m_methodrefInfos;
251     std::map< sal_uInt32, sal_uInt16 > m_interfaceMethodrefInfos;
252     std::map< sal_uInt32, sal_uInt16 > m_nameAndTypeInfos;
253     AccessFlags m_accessFlags;
254     sal_uInt16 m_thisClass;
255     sal_uInt16 m_superClass;
256     sal_uInt16 m_interfacesCount;
257     std::vector< unsigned char > m_interfaces;
258     sal_uInt16 m_fieldsCount;
259     std::vector< unsigned char > m_fields;
260     sal_uInt16 m_methodsCount;
261     std::vector< unsigned char > m_methods;
262     sal_uInt16 m_attributesCount;
263     std::vector< unsigned char > m_attributes;
264 
265     friend class Code;
266 };
267 
268 } }
269 
270 #endif // INCLUDED_CODEMAKER_SOURCE_JAVAMAKER_CLASSFILE_HXX
271