Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Tobias Hunger
qt-creator
Commits
e4f07e9f
Commit
e4f07e9f
authored
Mar 18, 2010
by
Erik Verbruggen
Browse files
Added AST node constructor generation.
parent
33d59283
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/tools/cplusplus/generate-ast.cpp
View file @
e4f07e9f
...
...
@@ -769,6 +769,72 @@ private:
Overview
oo
;
};
static
QList
<
QTextCursor
>
removeConstructors
(
ClassSpecifierAST
*
classAST
,
TranslationUnit
*
translationUnit
,
QTextDocument
*
document
)
{
Overview
oo
;
QList
<
QTextCursor
>
cursors
;
const
QString
className
=
oo
(
classAST
->
symbol
->
name
());
for
(
DeclarationListAST
*
iter
=
classAST
->
member_specifier_list
;
iter
;
iter
=
iter
->
next
)
{
if
(
FunctionDefinitionAST
*
funDef
=
iter
->
value
->
asFunctionDefinition
())
{
if
(
oo
(
funDef
->
symbol
->
name
())
==
className
)
{
// found it:
QTextCursor
tc
=
createCursor
(
translationUnit
,
funDef
,
document
);
tc
.
movePosition
(
QTextCursor
::
EndOfBlock
,
QTextCursor
::
KeepAnchor
);
tc
.
setPosition
(
tc
.
position
()
+
1
,
QTextCursor
::
KeepAnchor
);
cursors
.
append
(
tc
);
}
}
}
return
cursors
;
}
static
QStringList
collectFields
(
ClassSpecifierAST
*
classAST
)
{
QStringList
fields
;
Overview
oo
;
Class
*
clazz
=
classAST
->
symbol
;
for
(
unsigned
i
=
0
;
i
<
clazz
->
memberCount
();
++
i
)
{
Symbol
*
s
=
clazz
->
memberAt
(
i
);
if
(
Declaration
*
decl
=
s
->
asDeclaration
())
{
FullySpecifiedType
ty
=
decl
->
type
();
if
(
ty
->
isPointerType
())
fields
.
append
(
oo
(
decl
->
name
()));
else
if
(
ty
.
isUnsigned
())
fields
.
append
(
oo
(
decl
->
name
()));
}
}
return
fields
;
}
static
QString
createConstructor
(
ClassSpecifierAST
*
classAST
)
{
Overview
oo
;
Class
*
clazz
=
classAST
->
symbol
;
QString
result
(
QLatin1String
(
" "
));
result
.
append
(
oo
(
clazz
->
name
()));
result
.
append
(
QLatin1String
(
"()
\n
"
));
QStringList
classFields
=
collectFields
(
classAST
);
for
(
int
i
=
0
;
i
<
classFields
.
size
();
++
i
)
{
if
(
i
==
0
)
{
result
.
append
(
QLatin1String
(
" : "
));
result
.
append
(
classFields
.
at
(
i
));
result
.
append
(
QLatin1String
(
"(0)
\n
"
));
}
else
{
result
.
append
(
QLatin1String
(
" , "
));
result
.
append
(
classFields
.
at
(
i
));
result
.
append
(
QLatin1String
(
"(0)
\n
"
));
}
}
result
.
append
(
QLatin1String
(
" {}
\n
"
));
return
result
;
}
QStringList
generateAST_H
(
const
Snapshot
&
snapshot
,
const
QDir
&
cplusplusDir
)
{
...
...
@@ -802,6 +868,8 @@ QStringList generateAST_H(const Snapshot &snapshot, const QDir &cplusplusDir)
QList
<
QTextCursor
>
baseCastMethodCursors
=
removeCastMethods
(
astNodes
.
base
);
QMap
<
ClassSpecifierAST
*
,
QList
<
QTextCursor
>
>
cursors
;
QMap
<
ClassSpecifierAST
*
,
QString
>
replacementCastMethods
;
QMap
<
ClassSpecifierAST
*
,
QList
<
QTextCursor
>
>
constructors
;
QMap
<
ClassSpecifierAST
*
,
QString
>
replacementConstructors
;
Overview
oo
;
...
...
@@ -812,8 +880,10 @@ QStringList generateAST_H(const Snapshot &snapshot, const QDir &cplusplusDir)
const
QString
methodName
=
QLatin1String
(
"as"
)
+
className
.
mid
(
0
,
className
.
length
()
-
3
);
replacementCastMethods
[
classAST
]
=
QString
(
" virtual %1 *%2() { return this; }
\n
"
).
arg
(
className
,
methodName
);
castMethods
.
append
(
QString
(
" virtual %1 *%2() { return 0; }
\n
"
).
arg
(
className
,
methodName
));
astDerivedClasses
.
append
(
className
);
constructors
[
classAST
]
=
removeConstructors
(
classAST
,
AST_h_document
->
translationUnit
(),
&
document
);
replacementConstructors
[
classAST
]
=
createConstructor
(
classAST
);
}
if
(
!
baseCastMethodCursors
.
isEmpty
())
{
...
...
@@ -828,13 +898,23 @@ QStringList generateAST_H(const Snapshot &snapshot, const QDir &cplusplusDir)
for
(
int
classIndex
=
0
;
classIndex
<
astNodes
.
deriveds
.
size
();
++
classIndex
)
{
ClassSpecifierAST
*
classAST
=
astNodes
.
deriveds
.
at
(
classIndex
);
// remove the cast methods.
QList
<
QTextCursor
>
c
=
cursors
.
value
(
classAST
);
for
(
int
i
=
0
;
i
<
c
.
length
();
++
i
)
{
c
[
i
].
removeSelectedText
();
{
// remove the cast methods.
QList
<
QTextCursor
>
c
=
cursors
.
value
(
classAST
);
for
(
int
i
=
0
;
i
<
c
.
length
();
++
i
)
{
c
[
i
].
removeSelectedText
();
}
}
{
// remove the constructors.
QList
<
QTextCursor
>
c
=
constructors
.
value
(
classAST
);
for
(
int
i
=
0
;
i
<
c
.
length
();
++
i
)
{
c
[
i
].
removeSelectedText
();
}
}
astNodes
.
endOfPublicClassSpecifiers
[
classIndex
].
insertText
(
replacementCastMethods
.
value
(
classAST
));
astNodes
.
endOfPublicClassSpecifiers
[
classIndex
].
insertText
(
replacementConstructors
.
value
(
classAST
)
+
QLatin1String
(
"
\n
"
)
+
replacementCastMethods
.
value
(
classAST
));
}
if
(
file
.
open
(
QFile
::
WriteOnly
))
{
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment