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