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