1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_codemaker.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "includes.hxx"
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include "dumputils.hxx"
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include "codemaker/dependencies.hxx"
36*cdf0e10cSrcweir #include "codemaker/global.hxx"
37*cdf0e10cSrcweir #include "codemaker/typemanager.hxx"
38*cdf0e10cSrcweir #include "codemaker/unotype.hxx"
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir #include "osl/diagnose.h"
41*cdf0e10cSrcweir #include "rtl/string.hxx"
42*cdf0e10cSrcweir #include "rtl/ustring.hxx"
43*cdf0e10cSrcweir #include "sal/types.h"
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir #include <vector>
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir using codemaker::cppumaker::Includes;
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir Includes::Includes(
50*cdf0e10cSrcweir     TypeManager const & manager, codemaker::Dependencies const & dependencies,
51*cdf0e10cSrcweir     bool hpp):
52*cdf0e10cSrcweir     m_manager(manager), m_map(dependencies.getMap()), m_hpp(hpp),
53*cdf0e10cSrcweir     m_includeAny(dependencies.hasAnyDependency()), m_includeReference(false),
54*cdf0e10cSrcweir     m_includeSequence(dependencies.hasSequenceDependency()),
55*cdf0e10cSrcweir     m_includeType(dependencies.hasTypeDependency()),
56*cdf0e10cSrcweir     m_includeCppuMacrosHxx(false), m_includeCppuUnotypeHxx(false),
57*cdf0e10cSrcweir     m_includeOslDoublecheckedlockingH(false), m_includeOslMutexHxx(false),
58*cdf0e10cSrcweir     m_includeRtlStrbufHxx(false), m_includeRtlStringH(false),
59*cdf0e10cSrcweir     m_includeRtlTextencH(false), m_includeRtlUstrbufHxx(false),
60*cdf0e10cSrcweir     m_includeRtlUstringH(false),
61*cdf0e10cSrcweir     m_includeRtlUstringHxx(dependencies.hasStringDependency()),
62*cdf0e10cSrcweir     m_includeSalTypesH(
63*cdf0e10cSrcweir         dependencies.hasBooleanDependency() || dependencies.hasByteDependency()
64*cdf0e10cSrcweir         || dependencies.hasShortDependency()
65*cdf0e10cSrcweir         || dependencies.hasUnsignedShortDependency()
66*cdf0e10cSrcweir         || dependencies.hasLongDependency()
67*cdf0e10cSrcweir         || dependencies.hasUnsignedShortDependency()
68*cdf0e10cSrcweir         || dependencies.hasHyperDependency()
69*cdf0e10cSrcweir         || dependencies.hasUnsignedHyperDependency()
70*cdf0e10cSrcweir         || dependencies.hasCharDependency()),
71*cdf0e10cSrcweir     m_includeTypelibTypeclassH(false),
72*cdf0e10cSrcweir     m_includeTypelibTypedescriptionH(false)
73*cdf0e10cSrcweir {}
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir Includes::~Includes()
76*cdf0e10cSrcweir {}
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir void Includes::add(rtl::OString const & registryType) {
79*cdf0e10cSrcweir     sal_Int32 rank;
80*cdf0e10cSrcweir     std::vector< rtl::OString > args;
81*cdf0e10cSrcweir     rtl::OString type(
82*cdf0e10cSrcweir         codemaker::UnoType::decompose(registryType, &rank, &args));
83*cdf0e10cSrcweir     if (rank > 0) {
84*cdf0e10cSrcweir         m_includeSequence = true;
85*cdf0e10cSrcweir     }
86*cdf0e10cSrcweir     switch (codemaker::UnoType::getSort(type)) {
87*cdf0e10cSrcweir     case codemaker::UnoType::SORT_VOID:
88*cdf0e10cSrcweir         OSL_ASSERT(args.empty());
89*cdf0e10cSrcweir         OSL_ASSERT(false);
90*cdf0e10cSrcweir         break;
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir     case codemaker::UnoType::SORT_BOOLEAN:
93*cdf0e10cSrcweir     case codemaker::UnoType::SORT_BYTE:
94*cdf0e10cSrcweir     case codemaker::UnoType::SORT_SHORT:
95*cdf0e10cSrcweir     case codemaker::UnoType::SORT_UNSIGNED_SHORT:
96*cdf0e10cSrcweir     case codemaker::UnoType::SORT_LONG:
97*cdf0e10cSrcweir     case codemaker::UnoType::SORT_UNSIGNED_LONG:
98*cdf0e10cSrcweir     case codemaker::UnoType::SORT_HYPER:
99*cdf0e10cSrcweir     case codemaker::UnoType::SORT_UNSIGNED_HYPER:
100*cdf0e10cSrcweir     case codemaker::UnoType::SORT_CHAR:
101*cdf0e10cSrcweir         OSL_ASSERT(args.empty());
102*cdf0e10cSrcweir         m_includeSalTypesH = true;
103*cdf0e10cSrcweir         break;
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir     case codemaker::UnoType::SORT_FLOAT:
106*cdf0e10cSrcweir     case codemaker::UnoType::SORT_DOUBLE:
107*cdf0e10cSrcweir         OSL_ASSERT(args.empty());
108*cdf0e10cSrcweir         break;
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir     case codemaker::UnoType::SORT_STRING:
111*cdf0e10cSrcweir         OSL_ASSERT(args.empty());
112*cdf0e10cSrcweir         m_includeRtlUstringHxx = true;
113*cdf0e10cSrcweir         break;
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir     case codemaker::UnoType::SORT_TYPE:
116*cdf0e10cSrcweir         OSL_ASSERT(args.empty());
117*cdf0e10cSrcweir         m_includeType = true;
118*cdf0e10cSrcweir         break;
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir     case codemaker::UnoType::SORT_ANY:
121*cdf0e10cSrcweir         OSL_ASSERT(args.empty());
122*cdf0e10cSrcweir         m_includeAny = true;
123*cdf0e10cSrcweir         break;
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir     case codemaker::UnoType::SORT_COMPLEX:
126*cdf0e10cSrcweir         m_map.insert(
127*cdf0e10cSrcweir             codemaker::Dependencies::Map::value_type(
128*cdf0e10cSrcweir                 type, codemaker::Dependencies::KIND_NO_BASE));
129*cdf0e10cSrcweir         {for (std::vector< rtl::OString >::iterator i(args.begin());
130*cdf0e10cSrcweir               i != args.end(); ++i)
131*cdf0e10cSrcweir         {
132*cdf0e10cSrcweir             add(*i);
133*cdf0e10cSrcweir         }}
134*cdf0e10cSrcweir         break;
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir     default:
137*cdf0e10cSrcweir         OSL_ASSERT(false);
138*cdf0e10cSrcweir         break;
139*cdf0e10cSrcweir     }
140*cdf0e10cSrcweir }
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir namespace {
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir void dumpEmptyLineBeforeFirst(FileStream & out, bool * first) {
145*cdf0e10cSrcweir     OSL_ASSERT(first != 0);
146*cdf0e10cSrcweir     if (*first) {
147*cdf0e10cSrcweir         out << "\n";
148*cdf0e10cSrcweir         *first = false;
149*cdf0e10cSrcweir     }
150*cdf0e10cSrcweir }
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir }
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir void Includes::dump(FileStream & out, rtl::OString const * companionHdl) {
155*cdf0e10cSrcweir     OSL_ASSERT(companionHdl == 0 || m_hpp);
156*cdf0e10cSrcweir     if (!m_includeReference) {
157*cdf0e10cSrcweir         for (codemaker::Dependencies::Map::iterator i(m_map.begin());
158*cdf0e10cSrcweir              i != m_map.end(); ++i)
159*cdf0e10cSrcweir         {
160*cdf0e10cSrcweir             if (isInterfaceType(i->first)) {
161*cdf0e10cSrcweir                 m_includeReference = true;
162*cdf0e10cSrcweir                 break;
163*cdf0e10cSrcweir             }
164*cdf0e10cSrcweir         }
165*cdf0e10cSrcweir     }
166*cdf0e10cSrcweir     out << "#include \"sal/config.h\"\n";
167*cdf0e10cSrcweir     if (companionHdl) {
168*cdf0e10cSrcweir         out << "\n";
169*cdf0e10cSrcweir         dumpInclude(out, *companionHdl, false);
170*cdf0e10cSrcweir     }
171*cdf0e10cSrcweir     bool first = true;
172*cdf0e10cSrcweir     for (codemaker::Dependencies::Map::iterator i(m_map.begin());
173*cdf0e10cSrcweir          i != m_map.end(); ++i)
174*cdf0e10cSrcweir     {
175*cdf0e10cSrcweir         dumpEmptyLineBeforeFirst(out, &first);
176*cdf0e10cSrcweir         if (m_hpp || i->second == codemaker::Dependencies::KIND_BASE
177*cdf0e10cSrcweir             || !isInterfaceType(i->first))
178*cdf0e10cSrcweir         {
179*cdf0e10cSrcweir             dumpInclude(out, i->first, m_hpp);
180*cdf0e10cSrcweir         } else {
181*cdf0e10cSrcweir             bool ns = dumpNamespaceOpen(out, i->first, false);
182*cdf0e10cSrcweir             if (ns) {
183*cdf0e10cSrcweir                 out << " ";
184*cdf0e10cSrcweir             }
185*cdf0e10cSrcweir             out << "class ";
186*cdf0e10cSrcweir             dumpTypeIdentifier(out, i->first);
187*cdf0e10cSrcweir             out << ";";
188*cdf0e10cSrcweir             if (ns) {
189*cdf0e10cSrcweir                 out << " ";
190*cdf0e10cSrcweir             }
191*cdf0e10cSrcweir             dumpNamespaceClose(out, i->first, false);
192*cdf0e10cSrcweir             out << "\n";
193*cdf0e10cSrcweir         }
194*cdf0e10cSrcweir     }
195*cdf0e10cSrcweir     static char const * hxxExtension[2] = { "h", "hxx" };
196*cdf0e10cSrcweir     if (m_includeAny) {
197*cdf0e10cSrcweir         dumpEmptyLineBeforeFirst(out, &first);
198*cdf0e10cSrcweir         out << "#include \"com/sun/star/uno/Any." << hxxExtension[m_hpp] << "\"\n";
199*cdf0e10cSrcweir     }
200*cdf0e10cSrcweir     if (m_includeReference) {
201*cdf0e10cSrcweir         dumpEmptyLineBeforeFirst(out, &first);
202*cdf0e10cSrcweir         out << "#include \"com/sun/star/uno/Reference." << hxxExtension[m_hpp] << "\"\n";
203*cdf0e10cSrcweir     }
204*cdf0e10cSrcweir     if (m_includeSequence) {
205*cdf0e10cSrcweir         dumpEmptyLineBeforeFirst(out, &first);
206*cdf0e10cSrcweir         out << "#include \"com/sun/star/uno/Sequence." << hxxExtension[m_hpp] << "\"\n";
207*cdf0e10cSrcweir     }
208*cdf0e10cSrcweir     if (m_includeType) {
209*cdf0e10cSrcweir         dumpEmptyLineBeforeFirst(out, &first);
210*cdf0e10cSrcweir         out << "#include \"com/sun/star/uno/Type." << hxxExtension[m_hpp] << "\"\n";
211*cdf0e10cSrcweir     }
212*cdf0e10cSrcweir     if (m_includeCppuMacrosHxx) {
213*cdf0e10cSrcweir         dumpEmptyLineBeforeFirst(out, &first);
214*cdf0e10cSrcweir         out << ("#include \"cppu/macros.hxx\"\n");
215*cdf0e10cSrcweir     }
216*cdf0e10cSrcweir     if (m_includeCppuUnotypeHxx) {
217*cdf0e10cSrcweir         dumpEmptyLineBeforeFirst(out, &first);
218*cdf0e10cSrcweir         out << ("#include \"cppu/unotype.hxx\"\n");
219*cdf0e10cSrcweir     }
220*cdf0e10cSrcweir     if (m_includeOslDoublecheckedlockingH) {
221*cdf0e10cSrcweir         dumpEmptyLineBeforeFirst(out, &first);
222*cdf0e10cSrcweir         out << ("#include \"osl/doublecheckedlocking.h\"\n");
223*cdf0e10cSrcweir     }
224*cdf0e10cSrcweir     if (m_includeOslMutexHxx) {
225*cdf0e10cSrcweir         dumpEmptyLineBeforeFirst(out, &first);
226*cdf0e10cSrcweir         out << "#include \"osl/mutex.hxx\"\n";
227*cdf0e10cSrcweir     }
228*cdf0e10cSrcweir     if (m_includeRtlStrbufHxx) {
229*cdf0e10cSrcweir         dumpEmptyLineBeforeFirst(out, &first);
230*cdf0e10cSrcweir         out << ("#include \"rtl/strbuf.hxx\"\n");
231*cdf0e10cSrcweir     }
232*cdf0e10cSrcweir     if (m_includeRtlStringH) {
233*cdf0e10cSrcweir         dumpEmptyLineBeforeFirst(out, &first);
234*cdf0e10cSrcweir         out << "#include \"rtl/string.h\"\n";
235*cdf0e10cSrcweir     }
236*cdf0e10cSrcweir     if (m_includeRtlTextencH) {
237*cdf0e10cSrcweir         dumpEmptyLineBeforeFirst(out, &first);
238*cdf0e10cSrcweir         out << "#include \"rtl/textenc.h\"\n";
239*cdf0e10cSrcweir     }
240*cdf0e10cSrcweir     if (m_includeRtlUstrbufHxx) {
241*cdf0e10cSrcweir         dumpEmptyLineBeforeFirst(out, &first);
242*cdf0e10cSrcweir         out << ("#include \"rtl/ustrbuf.hxx\"\n");
243*cdf0e10cSrcweir     }
244*cdf0e10cSrcweir     if (m_includeRtlUstringH) {
245*cdf0e10cSrcweir         dumpEmptyLineBeforeFirst(out, &first);
246*cdf0e10cSrcweir         out << "#include \"rtl/ustring.h\"\n";
247*cdf0e10cSrcweir     }
248*cdf0e10cSrcweir     if (m_includeRtlUstringHxx) {
249*cdf0e10cSrcweir         dumpEmptyLineBeforeFirst(out, &first);
250*cdf0e10cSrcweir         out << ("#include \"rtl/ustring.hxx\"\n");
251*cdf0e10cSrcweir     }
252*cdf0e10cSrcweir     if (m_includeSalTypesH) {
253*cdf0e10cSrcweir         dumpEmptyLineBeforeFirst(out, &first);
254*cdf0e10cSrcweir         out << "#include \"sal/types.h\"\n";
255*cdf0e10cSrcweir     }
256*cdf0e10cSrcweir     if (m_includeTypelibTypeclassH) {
257*cdf0e10cSrcweir         dumpEmptyLineBeforeFirst(out, &first);
258*cdf0e10cSrcweir         out << ("#include \"typelib/typeclass.h\"\n");
259*cdf0e10cSrcweir     }
260*cdf0e10cSrcweir     if (m_includeTypelibTypedescriptionH) {
261*cdf0e10cSrcweir         dumpEmptyLineBeforeFirst(out, &first);
262*cdf0e10cSrcweir         out << ("#include \"typelib/typedescription.h\"\n");
263*cdf0e10cSrcweir     }
264*cdf0e10cSrcweir }
265*cdf0e10cSrcweir 
266*cdf0e10cSrcweir void Includes::dumpInclude(
267*cdf0e10cSrcweir     FileStream & out, rtl::OString const & registryType, bool hpp,
268*cdf0e10cSrcweir     rtl::OString const & suffix)
269*cdf0e10cSrcweir {
270*cdf0e10cSrcweir     static char const * extension[2] = { "hdl", "hpp" };
271*cdf0e10cSrcweir     out << "#include \"" << registryType;
272*cdf0e10cSrcweir     if (suffix.getLength() > 0) {
273*cdf0e10cSrcweir         out << "/" << suffix;
274*cdf0e10cSrcweir     }
275*cdf0e10cSrcweir     out << "." << extension[hpp] << "\"\n";
276*cdf0e10cSrcweir }
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir bool Includes::isInterfaceType(rtl::OString const & registryType) const {
279*cdf0e10cSrcweir     return m_manager.getTypeClass(registryType) == RT_TYPE_INTERFACE;
280*cdf0e10cSrcweir }
281