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