2 * Copyright Johannes Sixt
3 * This file is licensed under the GNU General Public License Version 2.
4 * See the file COPYING in the toplevel directory of the source directory.
11 #include <ksimpleconfig.h>
15 #include "typetable.h"
18 //! the TypeTables of all known libraries
19 static QList
<TypeTable
> typeTables
;
20 bool typeTablesInited
= false;
23 //! an indentifier for wchar_t
24 TypeInfo
TypeInfo::m_wchartType("");
26 TypeInfo
TypeInfo::m_unknownType("");
29 void TypeTable::initTypeLibraries()
31 if (!typeTablesInited
) {
32 TypeTable::loadTypeTables();
36 void TypeTable::loadTypeTables()
38 typeTablesInited
= true;
40 const QStringList files
= KGlobal::dirs()->findAllResources("types", "*.kdbgtt",
43 if (files
.isEmpty()) {
44 TRACE("no type tables found");
49 for (QValueListConstIterator
<QString
> p
= files
.begin(); p
!= files
.end(); ++p
) {
51 TypeTable
* newTable
= new TypeTable
;
52 newTable
->loadFromFile(fileName
);
53 typeTables
.append(newTable
);
58 TypeTable::TypeTable() :
59 m_printQStringDataCmd(0)
61 m_typeDict
.setAutoDelete(true);
62 // aliasDict keeps only pointers to items into typeDict
63 m_aliasDict
.setAutoDelete(false);
66 TypeTable::~TypeTable()
68 delete[] m_printQStringDataCmd
;
69 while (!m_templates
.empty()) {
70 delete m_templates
.begin()->second
;
71 m_templates
.erase(m_templates
.begin());
76 static const char TypeTableGroup
[] = "Type Table";
77 static const char LibDisplayName
[] = "LibDisplayName";
78 static const char ShlibRE
[] = "ShlibRE";
79 static const char EnableBuiltin
[] = "EnableBuiltin";
80 static const char PrintQStringCmd
[] = "PrintQStringCmd";
81 static const char TypesEntryFmt
[] = "Types%d";
82 static const char DisplayEntry
[] = "Display";
83 static const char AliasEntry
[] = "Alias";
84 static const char TemplateEntry
[] = "Template";
85 static const char ExprEntryFmt
[] = "Expr%d";
86 static const char FunctionGuardEntryFmt
[] = "FunctionGuard%d";
89 void TypeTable::loadFromFile(const QString
& fileName
)
91 TRACE("reading file " + fileName
);
92 KSimpleConfig
cf(fileName
, true); /* read-only */
95 * Read library name and properties.
97 cf
.setGroup(TypeTableGroup
);
98 m_displayName
= cf
.readEntry(LibDisplayName
);
99 if (m_displayName
.isEmpty()) {
100 // use file name instead
101 QFileInfo
fi(fileName
);
102 m_displayName
= fi
.baseName(true);
105 m_shlibNameRE
= QRegExp(cf
.readEntry(ShlibRE
));
106 m_enabledBuiltins
= cf
.readListEntry(EnableBuiltin
);
108 QString printQString
= cf
.readEntry(PrintQStringCmd
);
109 const char* ascii
= printQString
.ascii();
112 m_printQStringDataCmd
= new char[strlen(ascii
)+1];
113 strcpy(m_printQStringDataCmd
, ascii
);
116 * Get the types. We search for entries of kind Types1, Types2, etc.
117 * because a single entry Types could get rather long for large
121 for (int i
= 1; ; i
++) {
122 // next bunch of types
123 cf
.setGroup(TypeTableGroup
);
124 typesEntry
.sprintf(TypesEntryFmt
, i
);
125 if (!cf
.hasKey(typesEntry
))
128 QStringList typeNames
= cf
.readListEntry(typesEntry
, ',');
132 for (QStringList::iterator it
= typeNames
.begin(); it
!= typeNames
.end(); ++it
)
135 // check if this is an alias
136 alias
= cf
.readEntry(AliasEntry
);
137 if (alias
.isEmpty()) {
140 // look up the alias type and insert it
141 TypeInfo
* info
= m_typeDict
[alias
];
143 TRACE(*it
+ ": alias " + alias
+ " not found");
145 m_aliasDict
.insert(alias
, info
);
146 TRACE(*it
+ ": alias " + alias
);
153 void TypeTable::readType(KConfigBase
& cf
, const QString
& type
)
155 // the display string
156 QString expr
= cf
.readEntry(DisplayEntry
);
158 TypeInfo
* info
= new TypeInfo(expr
);
159 if (info
->m_numExprs
== 0) {
160 TRACE("bogus type " + type
+ ": no %% in Display: " + expr
);
165 info
->m_templatePattern
= cf
.readEntry(TemplateEntry
);
167 // Expr1, Expr2, etc...
169 QString funcGuardEntry
;
170 for (int j
= 0; j
< info
->m_numExprs
; j
++) {
171 exprEntry
.sprintf(ExprEntryFmt
, j
+1);
172 expr
= cf
.readEntry(exprEntry
);
173 info
->m_exprStrings
[j
] = expr
;
175 funcGuardEntry
.sprintf(FunctionGuardEntryFmt
, j
+1);
176 expr
= cf
.readEntry(funcGuardEntry
);
177 info
->m_guardStrings
[j
] = expr
;
181 if (info
->m_templatePattern
.find('<') < 0)
182 m_typeDict
.insert(type
, info
);
184 m_templates
[type
] = info
;
185 TRACE(type
+ QString().sprintf(": %d exprs", info
->m_numExprs
));
188 void TypeTable::copyTypes(QDict
<TypeInfo
>& dict
)
190 for (QDictIterator
<TypeInfo
> it
= m_typeDict
; it
!= 0; ++it
) {
191 dict
.insert(it
.currentKey(), it
);
193 for (QDictIterator
<TypeInfo
> it
= m_aliasDict
; it
!= 0; ++it
) {
194 dict
.insert(it
.currentKey(), it
);
198 bool TypeTable::isEnabledBuiltin(const QString
& feature
) const
200 return m_enabledBuiltins
.find(feature
) != m_enabledBuiltins
.end();
203 TypeInfo::TypeInfo(const QString
& displayString
)
205 // decompose the input into the parts
209 while (i
< typeInfoMaxExpr
&&
210 (idx
= displayString
.find('%', startIdx
)) >= 0)
212 m_displayString
[i
] = displayString
.mid(startIdx
, idx
-startIdx
);
218 * Remaining string; note that there's one more display string than
221 m_displayString
[i
] = displayString
.right(displayString
.length()-startIdx
);
224 TypeInfo::~TypeInfo()
229 ProgramTypeTable::ProgramTypeTable() :
230 m_parseQt2QStrings(false),
231 m_QCharIsShort(false),
232 m_printQStringDataCmd(0)
234 m_types
.setAutoDelete(false); /* paranoia */
235 m_aliasDict
.setAutoDelete(false); /* paranoia */
238 ProgramTypeTable::~ProgramTypeTable()
242 void ProgramTypeTable::loadTypeTable(TypeTable
* table
)
244 table
->copyTypes(m_types
);
247 const TypeTable::TypeMap
& t
= table
->templates();
248 std::transform(t
.begin(), t
.end(),
249 std::inserter(m_templates
, m_templates
.begin()),
250 std::ptr_fun(template2Info
));
252 // check whether to enable builtin QString support
253 if (!m_parseQt2QStrings
) {
254 m_parseQt2QStrings
= table
->isEnabledBuiltin("QString::Data");
256 if (!m_QCharIsShort
) {
257 m_QCharIsShort
= table
->isEnabledBuiltin("QCharIsShort");
259 if (!m_printQStringDataCmd
&& *table
->printQStringDataCmd()) {
260 m_printQStringDataCmd
= table
->printQStringDataCmd();
264 ProgramTypeTable::TemplateMap::value_type
265 ProgramTypeTable::template2Info(const TypeTable::TypeMap::value_type
& tt
)
267 QStringList args
= splitTemplateArgs(tt
.second
->m_templatePattern
);
269 TemplateMap::value_type
result(args
.front(), TemplateInfo());
270 result
.second
.type
= tt
.second
;
272 result
.second
.templateArgs
= args
;
277 * Splits the name \a t into the template name and its arguments.
278 * The first entry of the returned list is the template name, the remaining
279 * entries are the arguments.
281 QStringList
ProgramTypeTable::splitTemplateArgs(const QString
& t
)
290 // split off the template name
291 result
.front().truncate(i
);
294 // look for the next comma or the closing '>', skipping nested '<>'
297 for (; unsigned(i
) < t
.length() && nest
>= 0; i
++)
301 else if (t
[i
] == '>')
303 else if (nest
== 0 && t
[i
] == ',') {
304 // found end of argument
305 QString arg
= t
.mid(start
, i
-start
);
306 result
.push_back(arg
);
307 start
= i
+1; // skip ','
310 // last argument; could be incomplete
311 QString arg
= t
.mid(start
, i
-start
-(nest
<0));
312 result
.push_back(arg
);
316 TypeInfo
* ProgramTypeTable::lookup(QString type
)
319 * Registered aliases contain the complete template parameter list.
320 * Check for an alias first so that this case is out of the way.
322 if (TypeInfo
* result
= m_aliasDict
[type
])
326 * Check for a normal type. Even if type is a template instance,
327 * it could have been registered as a normal type instead of a pattern.
329 if (TypeInfo
* result
= m_types
[type
])
333 * The hard part: Look up a template.
335 QStringList parts
= splitTemplateArgs(type
);
336 if (parts
.size() == 1)
337 return 0; // not a template
339 // We can have several patterns for the same template name.
340 std::pair
<TemplateMap::const_iterator
, TemplateMap::const_iterator
> range
=
341 m_templates
.equal_range(parts
.front());
344 for (TemplateMap::const_iterator i
= range
.first
; i
!= range
.second
; ++i
)
346 const QStringList
& pat
= i
->second
.templateArgs
;
347 if (parts
.size() < pat
.size())
348 continue; // too few arguments
350 // a "*" in the last position of the pattern matches all arguments
351 // at the end of the template's arguments
352 if (parts
.size() > pat
.size() && pat
.back() != "*")
353 continue; // too many arguments and no wildcard
355 QStringList::const_iterator t
= parts
.begin();
356 QStringList::const_iterator p
= pat
.begin();
358 for (; equal
&& p
!= pat
.end(); ++p
, ++t
)
359 equal
= *p
== "*" || *p
== *t
;
361 return i
->second
.type
;
366 void ProgramTypeTable::registerAlias(const QString
& name
, TypeInfo
* type
)
368 ASSERT(lookup(name
) == 0 || lookup(name
) == type
);
369 m_aliasDict
.insert(name
, type
);
372 void ProgramTypeTable::loadLibTypes(const QStrList
& libs
)
374 QStrListIterator it
= libs
;
377 * We use a copy of the list of known libraries, from which we delete
378 * those libs that we already have added. This way we avoid to load a
381 QList
<TypeTable
> allTables
= typeTables
; /* shallow copy! */
382 allTables
.setAutoDelete(false); /* important! */
384 for (; it
&& allTables
.count() > 0; ++it
)
386 // look up the library
388 for (TypeTable
* t
= allTables
.first(); t
!= 0; t
= allTables
.next())
390 if (t
->matchFileName(it
))
392 TRACE("adding types for " + QString(it
));
397 * continue the search (due to remove's unpredictable
398 * behavior of setting the current item we simply go
399 * through the whole list again)