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 <astmodule.hxx>
21 #include <asttypedef.hxx>
22 #include <astservice.hxx>
23 #include <astconstant.hxx>
24 #include <astattribute.hxx>
25 #include <astinterfacemember.hxx>
26 #include <astservicemember.hxx>
27 #include <astobserves.hxx>
28 #include <astneeds.hxx>
29 #include <astsequence.hxx>
30 #include <astoperation.hxx>
31 
32 #include <osl/diagnose.h>
33 
34 #include <registry/version.h>
35 #include <registry/writer.hxx>
36 
dump(RegistryKey & rKey)37 bool AstModule::dump(RegistryKey& rKey)
38 {
39     RegistryKey localKey;
40     if ( getNodeType() == NT_root )
41     {
42         localKey = rKey;
43     }else
44     {
45         if (rKey.createKey( OStringToOUString(getFullName(), RTL_TEXTENCODING_UTF8 ), localKey) != RegError::NO_ERROR)
46         {
47             fprintf(stderr, "%s: warning, could not create key '%s' in '%s'\n",
48                     idlc()->getOptions()->getProgramName().getStr(),
49                     getFullName().getStr(), OUStringToOString(rKey.getRegistryName(), RTL_TEXTENCODING_UTF8).getStr());
50             return false;
51         }
52     }
53 
54     sal_uInt16          nConst = getNodeCount(NT_const);
55 
56     if ( nConst > 0 )
57     {
58         RTTypeClass typeClass = RT_TYPE_MODULE;
59         if ( getNodeType() == NT_constants )
60             typeClass = RT_TYPE_CONSTANTS;
61 
62         typereg::Writer aBlob(
63             m_bPublished ? TYPEREG_VERSION_1 : TYPEREG_VERSION_0,
64             getDocumentation(), "", typeClass,
65             m_bPublished,
66             OStringToOUString(getRelativName(), RTL_TEXTENCODING_UTF8), 0,
67             nConst, 0, 0);
68 
69         DeclList::const_iterator iter = getIteratorBegin();
70         DeclList::const_iterator end = getIteratorEnd();
71         sal_uInt16 index = 0;
72         while ( iter != end )
73         {
74             AstDeclaration* pDecl = *iter;
75             if ( pDecl->getNodeType() == NT_const &&
76                  pDecl->isInMainfile() )
77             {
78                 static_cast<AstConstant*>(pDecl)->dumpBlob(
79                     aBlob, index++,
80                     getNodeType() == NT_module && pDecl->isPublished());
81             }
82             ++iter;
83         }
84 
85         sal_uInt32 aBlobSize;
86         void const * pBlob = aBlob.getBlob(&aBlobSize);
87 
88         if (localKey.setValue("", RegValueType::BINARY,
89                                 const_cast<RegValue>(pBlob), aBlobSize) != RegError::NO_ERROR)
90         {
91             fprintf(stderr, "%s: warning, could not set value of key \"%s\" in %s\n",
92                     idlc()->getOptions()->getProgramName().getStr(),
93                     getFullName().getStr(), OUStringToOString(localKey.getRegistryName(), RTL_TEXTENCODING_UTF8).getStr());
94             return false;
95         }
96     } else
97     {
98         RTTypeClass typeClass = RT_TYPE_MODULE;
99         if ( getNodeType() == NT_constants )
100             typeClass = RT_TYPE_CONSTANTS;
101 
102         typereg::Writer aBlob(
103             m_bPublished ? TYPEREG_VERSION_1 : TYPEREG_VERSION_0,
104             getDocumentation(), "", typeClass, m_bPublished,
105             OStringToOUString(getRelativName(), RTL_TEXTENCODING_UTF8), 0, 0, 0,
106             0);
107 
108         sal_uInt32 aBlobSize;
109         void const * pBlob = aBlob.getBlob(&aBlobSize);
110 
111         if ( getNodeType() != NT_root )
112         {
113             if (localKey.setValue("", RegValueType::BINARY,
114                                     const_cast<RegValue>(pBlob), aBlobSize) != RegError::NO_ERROR)
115             {
116                 fprintf(stderr, "%s: warning, could not set value of key \"%s\" in %s\n",
117                         idlc()->getOptions()->getProgramName().getStr(),
118                         getFullName().getStr(), OUStringToOString(localKey.getRegistryName(), RTL_TEXTENCODING_UTF8).getStr());
119                 return false;
120             }
121         }
122     }
123     if ( getNodeType() == NT_root )
124     {
125         localKey.releaseKey();
126     }
127     return AstDeclaration::dump(rKey);
128 }
129 
dump(RegistryKey & rKey)130 bool AstTypeDef::dump(RegistryKey& rKey)
131 {
132     RegistryKey localKey;
133     if (rKey.createKey( OStringToOUString(getFullName(), RTL_TEXTENCODING_UTF8 ), localKey) != RegError::NO_ERROR)
134     {
135         fprintf(stderr, "%s: warning, could not create key '%s' in '%s'\n",
136                 idlc()->getOptions()->getProgramName().getStr(),
137                 getFullName().getStr(), OUStringToOString(rKey.getRegistryName(), RTL_TEXTENCODING_UTF8).getStr());
138         return false;
139     }
140 
141     typereg::Writer aBlob(
142         m_bPublished ? TYPEREG_VERSION_1 : TYPEREG_VERSION_0,
143         getDocumentation(), "", RT_TYPE_TYPEDEF, m_bPublished,
144         OStringToOUString(getRelativName(), RTL_TEXTENCODING_UTF8), 1, 0, 0, 0);
145     aBlob.setSuperTypeName(
146         0,
147         OStringToOUString(
148             getBaseType()->getRelativName(), RTL_TEXTENCODING_UTF8));
149 
150     sal_uInt32 aBlobSize;
151     void const * pBlob = aBlob.getBlob(&aBlobSize);
152 
153     if (localKey.setValue("", RegValueType::BINARY, const_cast<RegValue>(pBlob), aBlobSize) != RegError::NO_ERROR)
154     {
155         fprintf(stderr, "%s: warning, could not set value of key \"%s\" in %s\n",
156                 idlc()->getOptions()->getProgramName().getStr(),
157                 getFullName().getStr(), OUStringToOString(localKey.getRegistryName(), RTL_TEXTENCODING_UTF8).getStr());
158         return false;
159     }
160 
161     return true;
162 }
163 
dump(RegistryKey & rKey)164 bool AstService::dump(RegistryKey& rKey)
165 {
166     typereg_Version version = m_bPublished
167         ? TYPEREG_VERSION_1 : TYPEREG_VERSION_0;
168     OString superName;
169     sal_uInt16 constructors = 0;
170     sal_uInt16 properties = 0;
171     sal_uInt16 references = 0;
172     for (DeclList::const_iterator i(getIteratorBegin()); i != getIteratorEnd();
173          ++i)
174     {
175         switch ((*i)->getNodeType()) {
176         case NT_interface:
177         case NT_typedef:
178             version = TYPEREG_VERSION_1;
179             OSL_ASSERT(superName.isEmpty());
180             superName = (*i)->getRelativName();
181             break;
182 
183         case NT_operation:
184             OSL_ASSERT(getNodeType() == NT_service);
185             ++constructors;
186             break;
187 
188         case NT_property:
189             OSL_ASSERT(getNodeType() == NT_service);
190             ++properties;
191             break;
192 
193         case NT_service_member:
194             if (getNodeType() == NT_singleton) {
195                 OSL_ASSERT(superName.isEmpty());
196                 superName = static_cast<AstServiceMember *>(*i)->
197                     getRealService()->getRelativName();
198                 break;
199             }
200             [[fallthrough]];
201         case NT_interface_member:
202         case NT_observes:
203         case NT_needs:
204             OSL_ASSERT(getNodeType() == NT_service);
205             ++references;
206             break;
207 
208         default:
209             OSL_ASSERT(false);
210             break;
211         }
212     }
213     OSL_ASSERT(constructors == 0 || !m_defaultConstructor);
214     if (m_defaultConstructor) {
215         constructors = 1;
216     }
217     RegistryKey localKey;
218     if (rKey.createKey(
219             OStringToOUString(getFullName(), RTL_TEXTENCODING_UTF8),
220             localKey) != RegError::NO_ERROR) {
221         fprintf(
222             stderr, "%s: warning, could not create key '%s' in '%s'\n",
223             idlc()->getOptions()->getProgramName().getStr(),
224             getFullName().getStr(),
225             OUStringToOString(
226                 rKey.getRegistryName(), RTL_TEXTENCODING_UTF8).getStr());
227         return false;
228     }
229     typereg::Writer writer(
230         version, getDocumentation(), "",
231         getNodeType() == NT_singleton ? RT_TYPE_SINGLETON : RT_TYPE_SERVICE,
232         m_bPublished,
233         OStringToOUString(getRelativName(), RTL_TEXTENCODING_UTF8),
234         superName.isEmpty() ? 0 : 1, properties, constructors,
235         references);
236     if (!superName.isEmpty()) {
237         writer.setSuperTypeName(
238             0, OStringToOUString(superName, RTL_TEXTENCODING_UTF8));
239     }
240     sal_uInt16 constructorIndex = 0;
241     sal_uInt16 propertyIndex = 0;
242     sal_uInt16 referenceIndex = 0;
243     for (DeclList::const_iterator i(getIteratorBegin()); i != getIteratorEnd(); ++i)
244     {
245         switch ((*i)->getNodeType()) {
246         case NT_operation:
247             static_cast<AstOperation *>(*i)->dumpBlob(writer, constructorIndex++);
248             break;
249 
250         case NT_property:
251             static_cast<AstAttribute *>(*i)->dumpBlob(writer, propertyIndex++, nullptr);
252             break;
253 
254         case NT_interface_member:
255         {
256             AstInterfaceMember * decl = static_cast<AstInterfaceMember *>(*i);
257             writer.setReferenceData(
258                 referenceIndex++, decl->getDocumentation(), RTReferenceType::SUPPORTS,
259                 (decl->isOptional() ? RTFieldAccess::OPTIONAL : RTFieldAccess::INVALID),
260                 OStringToOUString( decl->getRealInterface()->getRelativName(),
261                                         RTL_TEXTENCODING_UTF8));
262             break;
263         }
264 
265         case NT_service_member:
266             if (getNodeType() == NT_service)
267             {
268                 AstServiceMember * decl = static_cast<AstServiceMember *>(*i);
269                 writer.setReferenceData(referenceIndex++, decl->getDocumentation(), RTReferenceType::EXPORTS,
270                     (decl->isOptional() ? RTFieldAccess::OPTIONAL : RTFieldAccess::INVALID),
271                     OStringToOUString(decl->getRealService()->getRelativName(),
272                                            RTL_TEXTENCODING_UTF8));
273             }
274             break;
275 
276         case NT_observes:
277             {
278                 AstObserves * decl = static_cast<AstObserves *>(*i);
279                 writer.setReferenceData(referenceIndex++, decl->getDocumentation(), RTReferenceType::OBSERVES,
280                     RTFieldAccess::INVALID,
281                     OStringToOUString( decl->getRealInterface()->getRelativName(),
282                                             RTL_TEXTENCODING_UTF8));
283                 break;
284             }
285 
286         case NT_needs:
287             {
288                 AstNeeds * decl = static_cast<AstNeeds *>(*i);
289                 writer.setReferenceData( referenceIndex++, decl->getDocumentation(), RTReferenceType::NEEDS,
290                     RTFieldAccess::INVALID,
291                     OStringToOUString( decl->getRealService()->getRelativName(),
292                                             RTL_TEXTENCODING_UTF8));
293                 break;
294             }
295 
296         default:
297             OSL_ASSERT( (*i)->getNodeType() == NT_interface || (*i)->getNodeType() == NT_typedef);
298             break;
299         }
300     }
301     if (m_defaultConstructor) {
302         writer.setMethodData(
303             constructorIndex++, "", RTMethodMode::TWOWAY,
304             "", "void",
305             0, 0);
306     }
307     sal_uInt32 size;
308     void const * blob = writer.getBlob(&size);
309     if (localKey.setValue(
310             "", RegValueType::BINARY, const_cast< void * >(blob),
311             size) != RegError::NO_ERROR)
312     {
313         fprintf(
314             stderr, "%s: warning, could not set value of key \"%s\" in %s\n",
315             idlc()->getOptions()->getProgramName().getStr(),
316             getFullName().getStr(),
317             OUStringToOString(
318                 localKey.getRegistryName(), RTL_TEXTENCODING_UTF8).getStr());
319         return false;
320     }
321     return true;
322 }
323 
dumpBlob(typereg::Writer & rBlob,sal_uInt16 index,sal_uInt16 * methodIndex) const324 void AstAttribute::dumpBlob(
325     typereg::Writer & rBlob, sal_uInt16 index, sal_uInt16 * methodIndex) const
326 {
327     RTFieldAccess accessMode = RTFieldAccess::INVALID;
328 
329     if (isReadonly())
330     {
331         accessMode |= RTFieldAccess::READONLY;
332     } else
333     {
334         accessMode |= RTFieldAccess::READWRITE;
335     }
336     if (isOptional())
337     {
338         accessMode |= RTFieldAccess::OPTIONAL;
339     }
340     if (isBound())
341     {
342         accessMode |= RTFieldAccess::BOUND;
343     }
344     if (isMayBeVoid())
345     {
346         accessMode |= RTFieldAccess::MAYBEVOID;
347     }
348     if (isConstrained())
349     {
350         accessMode |= RTFieldAccess::CONSTRAINED;
351     }
352     if (isTransient())
353     {
354         accessMode |= RTFieldAccess::TRANSIENT;
355     }
356     if (isMayBeAmbiguous())
357     {
358         accessMode |= RTFieldAccess::MAYBEAMBIGUOUS;
359     }
360     if (isMayBeDefault())
361     {
362         accessMode |= RTFieldAccess::MAYBEDEFAULT;
363     }
364     if (isRemoveable())
365     {
366         accessMode |= RTFieldAccess::REMOVABLE;
367     }
368 
369     OUString name(OStringToOUString(getLocalName(), RTL_TEXTENCODING_UTF8));
370     rBlob.setFieldData(
371         index, getDocumentation(), OUString(), accessMode, name,
372         OStringToOUString(m_pType->getRelativName(), RTL_TEXTENCODING_UTF8),
373         RTConstValue());
374     dumpExceptions(
375         rBlob, m_getDocumentation, m_getExceptions, RTMethodMode::ATTRIBUTE_GET,
376         methodIndex);
377     dumpExceptions(
378         rBlob, m_setDocumentation, m_setExceptions, RTMethodMode::ATTRIBUTE_SET,
379         methodIndex);
380 }
381 
dumpExceptions(typereg::Writer & writer,OUString const & documentation,DeclList const & exceptions,RTMethodMode flags,sal_uInt16 * methodIndex) const382 void AstAttribute::dumpExceptions(
383     typereg::Writer & writer, OUString const & documentation,
384     DeclList const & exceptions, RTMethodMode flags, sal_uInt16 * methodIndex) const
385 {
386     if (!exceptions.empty()) {
387         OSL_ASSERT(methodIndex != nullptr);
388         sal_uInt16 idx = (*methodIndex)++;
389         // exceptions.size() <= SAL_MAX_UINT16 already checked in
390         // AstInterface::dump:
391         writer.setMethodData(
392             idx, documentation, flags,
393             OStringToOUString(getLocalName(), RTL_TEXTENCODING_UTF8),
394             "void", 0,
395             static_cast< sal_uInt16 >(exceptions.size()));
396         sal_uInt16 exceptionIndex = 0;
397         for (auto const& elem : exceptions)
398         {
399             writer.setMethodExceptionTypeName(
400                 idx, exceptionIndex++,
401                 OStringToOUString(
402                     elem->getRelativName(), RTL_TEXTENCODING_UTF8));
403         }
404     }
405 }
406 
getRelativName() const407 const sal_Char* AstSequence::getRelativName() const
408 {
409     if ( !m_xRelativName )
410     {
411         m_xRelativName = OString("[]");
412         AstDeclaration const * pType = resolveTypedefs( m_pMemberType );
413         *m_xRelativName += pType->getRelativName();
414     }
415 
416     return m_xRelativName->getStr();
417 }
418 
419 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
420