update dev300-m58
[ooovba.git] / codemaker / source / commoncpp / commoncpp.cxx
blob49ebb652ae3306d556308e3b80afc70b100afb0d
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: commoncpp.cxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_codemaker.hxx"
33 #include "sal/config.h"
35 #include "codemaker/commoncpp.hxx"
37 #include "codemaker/options.hxx"
38 #include "codemaker/typemanager.hxx"
39 #include "codemaker/unotype.hxx"
41 #include "osl/diagnose.h"
42 #include "registry/reader.hxx"
43 #include "registry/types.h"
44 #include "rtl/strbuf.hxx"
45 #include "rtl/string.hxx"
46 #include "rtl/ustring.hxx"
47 #include "sal/types.h"
49 #include <vector>
51 namespace codemaker { namespace cpp {
53 rtl::OString typeToPrefix(TypeManager const & manager, rtl::OString const & type)
55 RTTypeClass typeclass = manager.getTypeClass(type);
56 if (typeclass == RT_TYPE_INVALID ||
57 typeclass == RT_TYPE_PUBLISHED)
58 return rtl::OString("_");
60 static char const * const typeclassPrefix[RT_TYPE_UNION + 1] = {
61 "invalid", /* RT_TYPE_INVALID, is here only as placeholder */
62 "interface", /* RT_TYPE_INTERFACE */
63 "module", /* RT_TYPE_MODULE */
64 "struct", /* RT_TYPE_STRUCT */
65 "enum", /* RT_TYPE_ENUM */
66 "exception", /* RT_TYPE_EXCEPTION */
67 "typedef", /* RT_TYPE_TYPEDEF */
68 "service", /* RT_TYPE_SERVICE */
69 "singleton", /* RT_TYPE_SINGLETON */
70 "object", /* RT_TYPE_OBJECT */
71 "constants", /* RT_TYPE_CONSTANTS */
72 "union" /* RT_TYPE_UNION */
75 return rtl::OString(typeclassPrefix[typeclass]);
78 rtl::OString scopedCppName(rtl::OString const & type, bool bNoNameSpace,
79 bool shortname)
81 char c('/');
82 sal_Int32 nPos = type.lastIndexOf( c );
83 if (nPos == -1) {
84 nPos = type.lastIndexOf( '.' );
85 if (nPos == -1)
86 return type;
88 c = '.';
90 if (bNoNameSpace)
91 return type.copy(nPos+1);
93 rtl::OStringBuffer tmpBuf(type.getLength()*2);
94 nPos = 0;
97 tmpBuf.append("::");
98 tmpBuf.append(type.getToken(0, c, nPos));
99 } while( nPos != -1 );
101 if (shortname) {
102 rtl::OString s(tmpBuf.makeStringAndClear());
103 if (s.indexOf("::com::sun::star") == 0)
104 return s.replaceAt(0, 16, "css");
105 else
106 return s;
109 return tmpBuf.makeStringAndClear();
113 rtl::OString translateUnoToCppType(
114 codemaker::UnoType::Sort sort, RTTypeClass typeClass,
115 rtl::OString const & nucleus, bool shortname)
117 rtl::OStringBuffer buf;
118 if (sort == codemaker::UnoType::SORT_COMPLEX) {
119 if (typeClass == RT_TYPE_INTERFACE
120 && nucleus == rtl::OString("com/sun/star/uno/XInterface"))
122 buf.append(RTL_CONSTASCII_STRINGPARAM("::com::sun::star::uno::XInterface"));
123 } else {
124 //TODO: check that nucleus is a valid (UTF-8) identifier
125 buf.append(nucleus);
127 } else {
128 static char const * const cppTypes[codemaker::UnoType::SORT_ANY + 1] = {
129 "void", "::sal_Bool", "::sal_Int8", "::sal_Int16", "::sal_uInt16",
130 "::sal_Int32", "::sal_uInt32", "::sal_Int64", "::sal_uInt64",
131 "float", "double", "::sal_Unicode", "::rtl::OUString",
132 "::com::sun::star::uno::Type", "::com::sun::star::uno::Any" };
133 buf.append(cppTypes[sort]);
136 if (shortname) {
137 rtl::OString s(buf.makeStringAndClear());
138 if (s.indexOf("::com::sun::star") == 0)
139 return s.replaceAt(0, 16, "css");
140 else
141 return s;
144 return buf.makeStringAndClear();
147 rtl::OString translateUnoToCppIdentifier(
148 rtl::OString const & unoIdentifier, rtl::OString const & prefix,
149 IdentifierTranslationMode transmode, rtl::OString const * forbidden)
151 if (// Keywords:
152 unoIdentifier == "asm"
153 || unoIdentifier == "auto"
154 || unoIdentifier == "bool"
155 || unoIdentifier == "break"
156 || unoIdentifier == "case"
157 || unoIdentifier == "catch"
158 || unoIdentifier == "char"
159 || unoIdentifier == "class"
160 || unoIdentifier == "const"
161 /* unoIdentifier == "const_cast" */
162 || unoIdentifier == "continue"
163 || unoIdentifier == "default"
164 || unoIdentifier == "delete"
165 || unoIdentifier == "do"
166 || unoIdentifier == "double"
167 /* unoIdentifier == "dynamic_cast" */
168 || unoIdentifier == "else"
169 || unoIdentifier == "enum"
170 || unoIdentifier == "explicit"
171 || unoIdentifier == "export"
172 || unoIdentifier == "extern"
173 || unoIdentifier == "false"
174 || unoIdentifier == "float"
175 || unoIdentifier == "for"
176 || unoIdentifier == "friend"
177 || unoIdentifier == "goto"
178 || unoIdentifier == "if"
179 || unoIdentifier == "inline"
180 || unoIdentifier == "int"
181 || unoIdentifier == "long"
182 || unoIdentifier == "mutable"
183 || unoIdentifier == "namespace"
184 || unoIdentifier == "new"
185 || unoIdentifier == "operator"
186 || unoIdentifier == "private"
187 || unoIdentifier == "protected"
188 || unoIdentifier == "public"
189 || unoIdentifier == "register"
190 /* unoIdentifier == "reinterpret_cast" */
191 || unoIdentifier == "return"
192 || unoIdentifier == "short"
193 || unoIdentifier == "signed"
194 || unoIdentifier == "sizeof"
195 || unoIdentifier == "static"
196 /* unoIdentifier == "static_cast" */
197 || unoIdentifier == "struct"
198 || unoIdentifier == "switch"
199 || unoIdentifier == "template"
200 || unoIdentifier == "this"
201 || unoIdentifier == "throw"
202 || unoIdentifier == "true"
203 || unoIdentifier == "try"
204 || unoIdentifier == "typedef"
205 || unoIdentifier == "typeid"
206 || unoIdentifier == "typename"
207 || unoIdentifier == "union"
208 || unoIdentifier == "unsigned"
209 || unoIdentifier == "using"
210 || unoIdentifier == "virtual"
211 || unoIdentifier == "void"
212 || unoIdentifier == "volatile"
213 /* unoIdentifier == "wchar_t" */
214 || unoIdentifier == "while"
215 // Alternative representations:
216 || unoIdentifier == "and"
217 /* unoIdentifier == "and_eq" */
218 || unoIdentifier == "bitand"
219 || unoIdentifier == "bitor"
220 || unoIdentifier == "compl"
221 || unoIdentifier == "not"
222 /* unoIdentifier == "not_eq" */
223 || unoIdentifier == "or"
224 /* unoIdentifier == "or_eq" */
225 || unoIdentifier == "xor"
226 /* unoIdentifier == "xor_eq" */
227 // Standard macros:
228 || (transmode != ITM_KEYWORDSONLY
229 && (unoIdentifier == "BUFSIZ"
230 || unoIdentifier == "CLOCKS_PER_SEC"
231 || unoIdentifier == "EDOM"
232 || unoIdentifier == "EOF"
233 || unoIdentifier == "ERANGE"
234 || unoIdentifier == "EXIT_FAILURE"
235 || unoIdentifier == "EXIT_SUCCESS"
236 || unoIdentifier == "FILENAME_MAX"
237 || unoIdentifier == "FOPEN_MAX"
238 || unoIdentifier == "HUGE_VAL"
239 || unoIdentifier == "LC_ALL"
240 || unoIdentifier == "LC_COLLATE"
241 || unoIdentifier == "LC_CTYPE"
242 || unoIdentifier == "LC_MONETARY"
243 || unoIdentifier == "LC_NUMERIC"
244 || unoIdentifier == "LC_TIME"
245 || unoIdentifier == "L_tmpnam"
246 || unoIdentifier == "MB_CUR_MAX"
247 || unoIdentifier == "NULL"
248 || unoIdentifier == "RAND_MAX"
249 || unoIdentifier == "SEEK_CUR"
250 || unoIdentifier == "SEEK_END"
251 || unoIdentifier == "SEEK_SET"
252 || unoIdentifier == "SIGABRT"
253 || unoIdentifier == "SIGFPE"
254 || unoIdentifier == "SIGILL"
255 || unoIdentifier == "SIGINT"
256 || unoIdentifier == "SIGSEGV"
257 || unoIdentifier == "SIGTERM"
258 || unoIdentifier == "SIG_DFL"
259 || unoIdentifier == "SIG_ERR"
260 || unoIdentifier == "SIG_IGN"
261 || unoIdentifier == "TMP_MAX"
262 || unoIdentifier == "WCHAR_MAX"
263 || unoIdentifier == "WCHAR_MIN"
264 || unoIdentifier == "WEOF"
265 /* unoIdentifier == "_IOFBF" */
266 /* unoIdentifier == "_IOLBF" */
267 /* unoIdentifier == "_IONBF" */
268 || unoIdentifier == "assert"
269 || unoIdentifier == "errno"
270 || unoIdentifier == "offsetof"
271 || unoIdentifier == "setjmp"
272 || unoIdentifier == "stderr"
273 || unoIdentifier == "stdin"
274 || unoIdentifier == "stdout"
275 /* unoIdentifier == "va_arg" */
276 /* unoIdentifier == "va_end" */
277 /* unoIdentifier == "va_start" */
278 // Standard values:
279 || unoIdentifier == "CHAR_BIT"
280 || unoIdentifier == "CHAR_MAX"
281 || unoIdentifier == "CHAR_MIN"
282 || unoIdentifier == "DBL_DIG"
283 || unoIdentifier == "DBL_EPSILON"
284 || unoIdentifier == "DBL_MANT_DIG"
285 || unoIdentifier == "DBL_MAX"
286 || unoIdentifier == "DBL_MAX_10_EXP"
287 || unoIdentifier == "DBL_MAX_EXP"
288 || unoIdentifier == "DBL_MIN"
289 || unoIdentifier == "DBL_MIN_10_EXP"
290 || unoIdentifier == "DBL_MIN_EXP"
291 || unoIdentifier == "FLT_DIG"
292 || unoIdentifier == "FLT_EPSILON"
293 || unoIdentifier == "FLT_MANT_DIG"
294 || unoIdentifier == "FLT_MAX"
295 || unoIdentifier == "FLT_MAX_10_EXP"
296 || unoIdentifier == "FLT_MAX_EXP"
297 || unoIdentifier == "FLT_MIN"
298 || unoIdentifier == "FLT_MIN_10_EXP"
299 || unoIdentifier == "FLT_MIN_EXP"
300 || unoIdentifier == "FLT_RADIX"
301 || unoIdentifier == "FLT_ROUNDS"
302 || unoIdentifier == "INT_MAX"
303 || unoIdentifier == "INT_MIN"
304 || unoIdentifier == "LDBL_DIG"
305 || unoIdentifier == "LDBL_EPSILON"
306 || unoIdentifier == "LDBL_MANT_DIG"
307 || unoIdentifier == "LDBL_MAX"
308 || unoIdentifier == "LDBL_MAX_10_EXP"
309 || unoIdentifier == "LDBL_MAX_EXP"
310 || unoIdentifier == "LDBL_MIN"
311 || unoIdentifier == "LDBL_MIN_10_EXP"
312 || unoIdentifier == "LDBL_MIN_EXP"
313 || unoIdentifier == "LONG_MAX"
314 || unoIdentifier == "LONG_MIN"
315 || unoIdentifier == "MB_LEN_MAX"
316 || unoIdentifier == "SCHAR_MAX"
317 || unoIdentifier == "SCHAR_MIN"
318 || unoIdentifier == "SHRT_MAX"
319 || unoIdentifier == "SHRT_MIN"
320 || unoIdentifier == "UCHAR_MAX"
321 || unoIdentifier == "UINT_MAX"
322 || unoIdentifier == "ULONG_MAX"
323 || unoIdentifier == "USHRT_MAX"))
324 || (transmode == ITM_GLOBAL
325 && (// Standard types:
326 /* unoIdentifier == "clock_t" */
327 /* unoIdentifier == "div_t" */
328 unoIdentifier == "FILE"
329 /* unoIdentifier == "fpos_t" */
330 /* unoIdentifier == "jmp_buf" */
331 || unoIdentifier == "lconv"
332 /* unoIdentifier == "ldiv_t" */
333 /* unoIdentifier == "mbstate_t" */
334 /* unoIdentifier == "ptrdiff_t" */
335 /* unoIdentifier == "sig_atomic_t" */
336 /* unoIdentifier == "size_t" */
337 /* unoIdentifier == "time_t" */
338 || unoIdentifier == "tm"
339 /* unoIdentifier == "va_list" */
340 /* unoIdentifier == "wctrans_t" */
341 /* unoIdentifier == "wctype_t" */
342 /* unoIdentifier == "wint_t" */
343 // Standard namespaces:
344 || unoIdentifier == "std"))
345 // Others:
346 || unoIdentifier == "NDEBUG"
347 || (forbidden != 0 && unoIdentifier == *forbidden) )
349 rtl::OStringBuffer buf(prefix);
350 buf.append('_');
351 buf.append(unoIdentifier);
352 return buf.makeStringAndClear();
353 } else {
354 return unoIdentifier;