File: type_analyzer.cxx

package info (click to toggle)
libreoffice 4%3A26.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,833,120 kB
  • sloc: cpp: 4,395,780; xml: 499,109; java: 254,438; python: 81,820; ansic: 33,823; perl: 30,297; javascript: 19,722; sh: 12,050; makefile: 10,854; cs: 8,865; yacc: 8,549; objc: 2,131; lex: 1,385; asm: 1,231; awk: 996; pascal: 914; csh: 20; sed: 5
file content (188 lines) | stat: -rw-r--r-- 6,885 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include "type_analyzer.hxx"
#include <codemaker/unotype.hxx>
#include <rtl/ustring.hxx>
#include <sal/log.hxx>
#include <unoidl/unoidl.hxx>
#include <vector>

TypeAnalyzer::TypeAnalyzer(const rtl::Reference<TypeManager>& typeManager)
    : m_typeManager(typeManager)
{
}

TypeAnalyzer::TypeInfo
TypeAnalyzer::analyzeInterface(const rtl::Reference<unoidl::InterfaceTypeEntity>& entity)
{
    TypeInfo typeInfo;

    // Always need XInterface for base type operations
    typeInfo.needsUnoInterface = true;
    typeInfo.namespaces.insert("com::sun::star::uno"_ostr);

    // Analyze all methods in the interface
    for (const auto& method : entity->getDirectMethods())
    {
        // Analyze return type
        if (method.returnType != u"void")
        {
            analyzeType(typeInfo, method.returnType);
        }

        // Analyze parameter types
        for (const auto& param : method.parameters)
        {
            analyzeType(typeInfo, param.type);
        }
    }

    return typeInfo;
}

void TypeAnalyzer::analyzeType(TypeInfo& typeInfo, std::u16string_view unoType)
{
    // Use cppumaker's approach: recursively insert dependencies
    insertDependency(typeInfo, unoType);
}

void TypeAnalyzer::insertDependency(TypeInfo& typeInfo, std::u16string_view unoType)
{
    // Decompose sequence/template types like cppumaker does
    sal_Int32 sequenceDepth;
    std::vector<OString> templateArgs;
    OString baseType = codemaker::UnoType::decompose(u2b(unoType), &sequenceDepth, &templateArgs);
    OUString baseTypeU = b2u(baseType);

    if (sequenceDepth != 0)
    {
        typeInfo.needsSequence = true;
    }

    // Get the type sort to determine how to handle it
    rtl::Reference<unoidl::Entity> entity;
    rtl::Reference<unoidl::MapCursor> cursor;
    codemaker::UnoType::Sort sort = m_typeManager->getSort(baseTypeU, &entity, &cursor);

    switch (sort)
    {
        case codemaker::UnoType::Sort::Void:
            // No special handling needed
            break;
        case codemaker::UnoType::Sort::Boolean:
        case codemaker::UnoType::Sort::Byte:
        case codemaker::UnoType::Sort::Short:
        case codemaker::UnoType::Sort::UnsignedShort:
        case codemaker::UnoType::Sort::Long:
        case codemaker::UnoType::Sort::UnsignedLong:
        case codemaker::UnoType::Sort::Hyper:
        case codemaker::UnoType::Sort::UnsignedHyper:
        case codemaker::UnoType::Sort::Float:
        case codemaker::UnoType::Sort::Double:
        case codemaker::UnoType::Sort::Char:
            typeInfo.needsSalTypes = true;
            break;
        case codemaker::UnoType::Sort::String:
            typeInfo.needsString = true;
            typeInfo.needsRtlUstring = true;
            break;
        case codemaker::UnoType::Sort::Type:
            typeInfo.needsUnoInterface = true; // Type needs XInterface includes
            break;
        case codemaker::UnoType::Sort::Any:
            typeInfo.needsAny = true;
            break;
        case codemaker::UnoType::Sort::PlainStruct:
        {
            // Add this struct to dependencies and recursively analyze its members
            typeInfo.structTypes.insert(baseType);

            rtl::Reference<unoidl::PlainStructTypeEntity> structEntity(
                static_cast<unoidl::PlainStructTypeEntity*>(entity.get()));
            if (structEntity.is())
            {
                // Recursively analyze base struct
                if (!structEntity->getDirectBase().isEmpty())
                {
                    insertDependency(typeInfo, structEntity->getDirectBase());
                }

                // Recursively analyze member types
                for (const auto& member : structEntity->getDirectMembers())
                {
                    insertDependency(typeInfo, member.type);
                }
            }
            break;
        }
        case codemaker::UnoType::Sort::Enum:
            typeInfo.enumTypes.insert(baseType);
            typeInfo.needsEnum = true;
            typeInfo.needsSalTypes = true;
            break;
        case codemaker::UnoType::Sort::Interface:
            typeInfo.interfaceTypes.insert(baseType);
            typeInfo.needsUnoInterface = true;
            typeInfo.namespaces.insert("com::sun::star::uno"_ostr);
            break;
        case codemaker::UnoType::Sort::ConstantGroup:
            typeInfo.constantGroupTypes.insert(baseType);
            typeInfo.needsConstantGroup = true;
            break;
        case codemaker::UnoType::Sort::PolymorphicStructTemplate:
        {
            // Handle template arguments recursively
            for (const OString& arg : templateArgs)
            {
                insertDependency(typeInfo, b2u(arg));
            }
            // Note: Don't add polymorphic struct templates to structTypes
            // since they don't have concrete header files to include
            break;
        }
        default:
            // For unknown types, ensure basic includes
            typeInfo.needsUnoInterface = true;
            break;
    }

    // Extract namespace from UNO type name (for any valid UNO type)
    if (sort != codemaker::UnoType::Sort::Void && sort != codemaker::UnoType::Sort::Boolean
        && sort != codemaker::UnoType::Sort::Byte && sort != codemaker::UnoType::Sort::Short
        && sort != codemaker::UnoType::Sort::UnsignedShort && sort != codemaker::UnoType::Sort::Long
        && sort != codemaker::UnoType::Sort::UnsignedLong && sort != codemaker::UnoType::Sort::Hyper
        && sort != codemaker::UnoType::Sort::UnsignedHyper
        && sort != codemaker::UnoType::Sort::Float && sort != codemaker::UnoType::Sort::Double
        && sort != codemaker::UnoType::Sort::Char && sort != codemaker::UnoType::Sort::String
        && sort != codemaker::UnoType::Sort::Type && sort != codemaker::UnoType::Sort::Any
        && baseType.indexOf('.') >= 0)
    {
        typeInfo.namespaces.insert(extractNamespace(baseTypeU));
    }
}

OString TypeAnalyzer::extractNamespace(std::u16string_view unoType)
{
    OString typeStr = u2b(unoType);

    // Convert dots to double colons for C++ namespace
    OString cppNamespace = typeStr.replaceAll("."_ostr, "::"_ostr);

    // Remove the last component (the actual type name) to get just the namespace
    sal_Int32 lastColon = cppNamespace.lastIndexOf("::");
    if (lastColon != -1)
    {
        return cppNamespace.copy(0, lastColon);
    }

    return cppNamespace;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */