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