LibreOffice Module codemaker (master) 1
classfile.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#pragma once
21
22#include <codemaker/unotype.hxx>
23#include <sal/types.h>
24
25#include <map>
26#include <memory>
27#include <utility>
28#include <vector>
29
30class FileStream;
31
33
34class ClassFile {
35public:
37 ACC_PUBLIC = 0x0001,
38 ACC_PRIVATE = 0x0002,
39 ACC_STATIC = 0x0008,
40 ACC_FINAL = 0x0010,
41 ACC_SUPER = 0x0020,
42 ACC_VARARGS = 0x0080,
43 ACC_INTERFACE = 0x0200,
44 ACC_ABSTRACT = 0x0400,
45 ACC_SYNTHETIC = 0x1000
46 };
47
48 class Code {
49 public:
50 typedef std::vector< unsigned char >::size_type Branch;
51 typedef std::vector< unsigned char >::size_type Position;
52
53 ~Code();
54
55 void instrAastore();
56 void instrAconstNull();
57 void instrAnewarray(rtl::OString const & type);
58 void instrAreturn();
59 void instrAthrow();
60 void instrCheckcast(rtl::OString const & type);
61 void instrDup();
62
63 void instrGetstatic(
64 rtl::OString const & type, rtl::OString const & name,
65 rtl::OString const & descriptor);
66
70
71 void instrInstanceof(rtl::OString const & type);
72
74 rtl::OString const & type, rtl::OString const & name,
75 rtl::OString const & descriptor, sal_uInt8 args);
76
78 rtl::OString const & type, rtl::OString const & name,
79 rtl::OString const & descriptor);
80
82 rtl::OString const & type, rtl::OString const & name,
83 rtl::OString const & descriptor);
84
86 rtl::OString const & type, rtl::OString const & name,
87 rtl::OString const & descriptor);
88
90 Code const * defaultBlock,
91 std::vector< std::pair< sal_Int32, Code * > > const & blocks);
92
93 void instrNew(rtl::OString const & type);
95 void instrPop();
96
97 void instrPutfield(
98 rtl::OString const & type, rtl::OString const & name,
99 rtl::OString const & descriptor);
100
101 void instrPutstatic(
102 rtl::OString const & type, rtl::OString const & name,
103 rtl::OString const & descriptor);
104
105 void instrReturn();
106 void instrSwap();
107
108 void instrTableswitch(
109 Code const * defaultBlock, sal_Int32 low,
110 std::vector< std::unique_ptr<Code> > const & blocks);
111
112 void loadIntegerConstant(sal_Int32 value);
113 void loadStringConstant(rtl::OString const & value);
114 void loadLocalInteger(sal_uInt16 index);
115 void loadLocalLong(sal_uInt16 index);
116 void loadLocalFloat(sal_uInt16 index);
117 void loadLocalDouble(sal_uInt16 index);
118 void loadLocalReference(sal_uInt16 index);
119 void storeLocalReference(sal_uInt16 index);
120 void branchHere(Branch branch);
121
122 void addException(
123 Position start, Position end, Position handler,
124 rtl::OString const & type);
125
126 void setMaxStackAndLocals(sal_uInt16 maxStack, sal_uInt16 maxLocals)
127 { m_maxStack = maxStack; m_maxLocals = maxLocals; }
128
129 Position getPosition() const;
130
131 private:
132 Code(Code const &) = delete;
133 Code& operator =(const Code&) = delete;
134
135 explicit Code(ClassFile & classFile);
136
137 void ldc(sal_uInt16 index);
138
139 void accessLocal(
140 sal_uInt16 index, sal_uInt8 fastOp, sal_uInt8 normalOp);
141
143 sal_uInt16 m_maxStack;
144 sal_uInt16 m_maxLocals;
145 std::vector< unsigned char > m_code;
147 std::vector< unsigned char > m_exceptionTable;
148
149 friend class ClassFile;
150 };
151
153 AccessFlags accessFlags, rtl::OString const & thisClass,
154 rtl::OString const & superClass, rtl::OString const & signature);
155
156 ~ClassFile();
157
158 std::unique_ptr<Code> newCode();
159
160 sal_uInt16 addIntegerInfo(sal_Int32 value);
161 sal_uInt16 addFloatInfo(float value);
162 sal_uInt16 addLongInfo(sal_Int64 value);
163 sal_uInt16 addDoubleInfo(double value);
164
165 void addInterface(rtl::OString const & interface);
166
167 void addField(
168 AccessFlags accessFlags, rtl::OString const & name,
169 rtl::OString const & descriptor, sal_uInt16 constantValueIndex,
170 rtl::OString const & signature);
171
172 void addMethod(
173 AccessFlags accessFlags, rtl::OString const & name,
174 rtl::OString const & descriptor, Code const * code,
175 std::vector< rtl::OString > const & exceptions,
176 rtl::OString const & signature);
177
178 void write(FileStream & file) const; //TODO
179
180private:
181 typedef std::map< rtl::OString, sal_uInt16 > Map;
182
183 ClassFile(ClassFile const &) = delete;
184 ClassFile& operator =(const ClassFile&) = delete;
185
186 sal_uInt16 nextConstantPoolIndex(sal_uInt16 width);
187 sal_uInt16 addUtf8Info(rtl::OString const & value);
188 sal_uInt16 addClassInfo(rtl::OString const & type);
189 sal_uInt16 addStringInfo(rtl::OString const & value);
190
191 sal_uInt16 addFieldrefInfo(
192 rtl::OString const & type, rtl::OString const & name,
193 rtl::OString const & descriptor);
194
195 sal_uInt16 addMethodrefInfo(
196 rtl::OString const & type, rtl::OString const & name,
197 rtl::OString const & descriptor);
198
199 sal_uInt16 addInterfaceMethodrefInfo(
200 rtl::OString const & type, rtl::OString const & name,
201 rtl::OString const & descriptor);
202
203 sal_uInt16 addNameAndTypeInfo(
204 rtl::OString const & name, rtl::OString const & descriptor);
205
207 std::vector< unsigned char > & stream, rtl::OString const & signature);
208
210 std::vector< unsigned char > m_constantPool;
211 std::map< rtl::OString, sal_uInt16 > m_utf8Infos;
212 std::map< sal_Int32, sal_uInt16 > m_integerInfos;
213 std::map< sal_Int64, sal_uInt16 > m_longInfos;
214 std::map< float, sal_uInt16 > m_floatInfos;
215 std::map< double, sal_uInt16 > m_doubleInfos;
216 std::map< sal_uInt16, sal_uInt16 > m_classInfos;
217 std::map< sal_uInt16, sal_uInt16 > m_stringInfos;
218 std::map< sal_uInt32, sal_uInt16 > m_fieldrefInfos;
219 std::map< sal_uInt32, sal_uInt16 > m_methodrefInfos;
220 std::map< sal_uInt32, sal_uInt16 > m_interfaceMethodrefInfos;
221 std::map< sal_uInt32, sal_uInt16 > m_nameAndTypeInfos;
223 sal_uInt16 m_thisClass;
224 sal_uInt16 m_superClass;
226 std::vector< unsigned char > m_interfaces;
227 sal_uInt16 m_fieldsCount;
228 std::vector< unsigned char > m_fields;
229 sal_uInt16 m_methodsCount;
230 std::vector< unsigned char > m_methods;
232 std::vector< unsigned char > m_attributes;
233
234 friend class Code;
235};
236
237}
238
239/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void accessLocal(sal_uInt16 index, sal_uInt8 fastOp, sal_uInt8 normalOp)
Definition: classfile.cxx:446
void loadLocalReference(sal_uInt16 index)
Definition: classfile.cxx:390
void instrNew(rtl::OString const &type)
Definition: classfile.cxx:263
void instrPutstatic(rtl::OString const &type, rtl::OString const &name, rtl::OString const &descriptor)
Definition: classfile.cxx:294
void instrInvokespecial(rtl::OString const &type, rtl::OString const &name, rtl::OString const &descriptor)
Definition: classfile.cxx:201
void instrInvokeinterface(rtl::OString const &type, rtl::OString const &name, rtl::OString const &descriptor, sal_uInt8 args)
Definition: classfile.cxx:189
void instrTableswitch(Code const *defaultBlock, sal_Int32 low, std::vector< std::unique_ptr< Code > > const &blocks)
Definition: classfile.cxx:313
void loadLocalFloat(sal_uInt16 index)
Definition: classfile.cxx:382
void instrGetstatic(rtl::OString const &type, rtl::OString const &name, rtl::OString const &descriptor)
Definition: classfile.cxx:150
void instrAnewarray(rtl::OString const &type)
Definition: classfile.cxx:123
Code & operator=(const Code &)=delete
void instrInvokestatic(rtl::OString const &type, rtl::OString const &name, rtl::OString const &descriptor)
Definition: classfile.cxx:210
void setMaxStackAndLocals(sal_uInt16 maxStack, sal_uInt16 maxLocals)
Definition: classfile.hxx:126
void instrInstanceof(rtl::OString const &type)
Definition: classfile.cxx:183
void instrLookupswitch(Code const *defaultBlock, std::vector< std::pair< sal_Int32, Code * > > const &blocks)
Definition: classfile.cxx:228
void loadLocalInteger(sal_uInt16 index)
Definition: classfile.cxx:374
void instrNewarray(codemaker::UnoType::Sort sort)
Definition: classfile.cxx:269
void instrCheckcast(rtl::OString const &type)
Definition: classfile.cxx:139
void loadLocalDouble(sal_uInt16 index)
Definition: classfile.cxx:386
void instrPutfield(rtl::OString const &type, rtl::OString const &name, rtl::OString const &descriptor)
Definition: classfile.cxx:285
void loadLocalLong(sal_uInt16 index)
Definition: classfile.cxx:378
std::vector< unsigned char > m_exceptionTable
Definition: classfile.hxx:147
void storeLocalReference(sal_uInt16 index)
Definition: classfile.cxx:394
void loadStringConstant(rtl::OString const &value)
Definition: classfile.cxx:370
std::vector< unsignedchar >::size_type Position
Definition: classfile.hxx:51
void addException(Position start, Position end, Position handler, rtl::OString const &type)
Definition: classfile.cxx:406
std::vector< unsigned char > m_code
Definition: classfile.hxx:145
void instrInvokevirtual(rtl::OString const &type, rtl::OString const &name, rtl::OString const &descriptor)
Definition: classfile.cxx:219
void loadIntegerConstant(sal_Int32 value)
Definition: classfile.cxx:353
std::vector< unsignedchar >::size_type Branch
Definition: classfile.hxx:50
std::map< double, sal_uInt16 > m_doubleInfos
Definition: classfile.hxx:215
std::map< sal_Int64, sal_uInt16 > m_longInfos
Definition: classfile.hxx:213
ClassFile(ClassFile const &)=delete
std::vector< unsigned char > m_constantPool
Definition: classfile.hxx:210
std::vector< unsigned char > m_attributes
Definition: classfile.hxx:232
void addField(AccessFlags accessFlags, rtl::OString const &name, rtl::OString const &descriptor, sal_uInt16 constantValueIndex, rtl::OString const &signature)
Definition: classfile.cxx:558
std::vector< unsigned char > m_methods
Definition: classfile.hxx:230
std::map< sal_uInt16, sal_uInt16 > m_classInfos
Definition: classfile.hxx:216
std::map< rtl::OString, sal_uInt16 > m_utf8Infos
Definition: classfile.hxx:211
std::map< sal_uInt32, sal_uInt16 > m_methodrefInfos
Definition: classfile.hxx:219
ClassFile & operator=(const ClassFile &)=delete
void addMethod(AccessFlags accessFlags, rtl::OString const &name, rtl::OString const &descriptor, Code const *code, std::vector< rtl::OString > const &exceptions, rtl::OString const &signature)
Definition: classfile.cxx:582
sal_uInt16 addClassInfo(rtl::OString const &type)
Definition: classfile.cxx:691
sal_uInt16 addMethodrefInfo(rtl::OString const &type, rtl::OString const &name, rtl::OString const &descriptor)
Definition: classfile.cxx:748
std::map< sal_uInt32, sal_uInt16 > m_interfaceMethodrefInfos
Definition: classfile.hxx:220
sal_uInt16 addInterfaceMethodrefInfo(rtl::OString const &type, rtl::OString const &name, rtl::OString const &descriptor)
Definition: classfile.cxx:771
std::map< sal_uInt32, sal_uInt16 > m_nameAndTypeInfos
Definition: classfile.hxx:221
std::map< sal_uInt32, sal_uInt16 > m_fieldrefInfos
Definition: classfile.hxx:218
sal_uInt16 addUtf8Info(rtl::OString const &value)
Definition: classfile.cxx:670
sal_uInt16 addDoubleInfo(double value)
Definition: classfile.cxx:533
void appendSignatureAttribute(std::vector< unsigned char > &stream, rtl::OString const &signature)
Definition: classfile.cxx:818
sal_uInt16 addFieldrefInfo(rtl::OString const &type, rtl::OString const &name, rtl::OString const &descriptor)
Definition: classfile.cxx:725
sal_uInt16 addFloatInfo(float value)
Definition: classfile.cxx:501
std::map< rtl::OString, sal_uInt16 > Map
Definition: classfile.hxx:181
std::vector< unsigned char > m_fields
Definition: classfile.hxx:228
sal_uInt16 addLongInfo(sal_Int64 value)
Definition: classfile.cxx:518
sal_uInt16 addNameAndTypeInfo(rtl::OString const &name, rtl::OString const &descriptor)
Definition: classfile.cxx:795
std::map< float, sal_uInt16 > m_floatInfos
Definition: classfile.hxx:214
std::vector< unsigned char > m_interfaces
Definition: classfile.hxx:226
sal_uInt16 nextConstantPoolIndex(sal_uInt16 width)
Definition: classfile.cxx:660
sal_uInt16 addStringInfo(rtl::OString const &value)
Definition: classfile.cxx:708
void write(FileStream &file) const
Definition: classfile.cxx:641
ClassFile(AccessFlags accessFlags, rtl::OString const &thisClass, rtl::OString const &superClass, rtl::OString const &signature)
std::unique_ptr< Code > newCode()
Definition: classfile.cxx:482
std::map< sal_Int32, sal_uInt16 > m_integerInfos
Definition: classfile.hxx:212
sal_uInt16 addIntegerInfo(sal_Int32 value)
Definition: classfile.cxx:486
void addInterface(rtl::OString const &interface)
Definition: classfile.cxx:550
std::map< sal_uInt16, sal_uInt16 > m_stringInfos
Definition: classfile.hxx:217
Sort
An enumeration of all the sorts of relevant UNOIDL entities.
Definition: unotype.hxx:33
unsigned char sal_uInt8