Warning, /kdevelop/kdevelop/kdevplatform/language/codegen/Mainpage.dox is written in an unsupported language. File is not indexed.

0001 /*!
0002  * @mainpage Code Generation and Refactoring library
0003  *
0004  * Overview | \ref Refactoring
0005  *
0006  * The code generation api provides a system to simplify the creation of refactoring and
0007  * code generating tools.  The definition-use chain (duchain) and the language's specific
0008  * abstract syntax tree (AST) provide the basis of a two-tiered, high-level and low-level
0009  * api respectively.  This gives you the simplicity and reusability of working with a
0010  * language-independent api (the duchain) while still having the full power to manipulate
0011  * language specifics (the AST).
0012  *
0013  * To perform code generation, you should create a new duchain (eg. starting with a
0014  * KDevelop::DUContext or KDevelop::Declaration) and then if desired attach AST branches
0015  * where required.
0016  *
0017  * To perform refactoring, a change set needs to be created against pre-existing duchains
0018  * and/or ASTs; the chains/ASTs are not modified directly.  Create a KDevelop::DUChangeSet
0019  * and/or an KDevelop::AstChangeSet.  Once created, this api will use the
0020  * KDevelop::EditorIntegrator to modify the editor text, or directly edit on-disk files.
0021  *
0022  * Refactoring which includes some code generation can combine the two different
0023  * approaches (direct creation of objects and change sets).
0024  *
0025  * @licenses
0026  * @lgpl
0027  *
0028  * For questions and discussions about editor either contact the author
0029  * or the <a href="mailto:kdevelop-devel@kde.org">kdevelop-devel@kde.org</a>
0030  * mailing list.
0031  */
0032 
0033 /*!
0034  * \page Refactoring Refactoring and Code Generation
0035  *
0036  * \ref index "Overview" | Refactoring and Code Generation
0037  *
0038  * The process of refactoring involves several logical steps - initial condition checking,
0039  * user input gathering, final condition checking, generation of change sets, user review
0040  * of changes, and change committing.  Tools may need to repeat steps as required, eg.
0041  * if further information is needed, or if there would be a semantic problem created by the
0042  * refactoring, the user may get a chance to resolve the problem.
0043  *
0044  * \section precondition Precondition checking
0045  *
0046  * Preconditions are basic conditions which must be met for the refactoring to be possible.
0047  * They usually include that the project does not have any parsing errors, and that the
0048  * location where it is to be initiated is a location which can accomodate the refactoring
0049  * (eg. extract method should have a portion of a method selected in the editor).
0050  *
0051  * These conditions may be run often to present the state to the user, so they should be
0052  * efficient; save any complex analysis for later.
0053  *
0054  * \section ui User input gathering
0055  *
0056  * Here the user is presented with a user interface to enable the efficient gathering of information
0057  * required to undertake the refactoring.  This may take the form of a wizard for complex
0058  * refactorings, or a popup notification for simpler refactorings such as an object
0059  * renaming.
0060  *
0061  * \section final Final condition checking and change set generation
0062  *
0063  * Once the user input has been collected, the tool should be in a better position to evaluate
0064  * whether the refactoring is possible and will generate semantically correct changes.
0065  * If there are no issues, the tool should proceed to generate change set(s).
0066  *
0067  * Ideally, no more than one AST should be stored in memory at once in order that refactoring
0068  * of complex projects proceeds without requiring excessive system resources.  Once the
0069  * change set is finalised, request for it to be translated into a KDevelop::EditorChangeSet.
0070  *
0071  * \section review User review of changes
0072  *
0073  * The editor change sets will be optionally presented to the user for review before applying
0074  * to the code base.
0075  */
0076 
0077 /*!
0078  * \page ChangeSets Change Sets
0079  *
0080  * \ref index "Overview" | \ref Refactoring | Change Sets
0081  *
0082  * Change sets are generated via two main methods - altering duchains, and/or (when
0083  * the duchain is not expressive enough) altering ASTs.  Code which is able to express everything
0084  * using only the duchain may be instantly applicable to many languages, such as class generation.
0085  * However code which accesses the AST is automatically language-specific, and will require
0086  * additional effort to port to other languages.  In general, use the duchain when you can,
0087  * and the AST when you must.
0088  *
0089  * Create a new duchain change set as follows:
0090  * \code
0091  *   KDevelop::ReferencedTopDUContext top = KDevelop::DUChainUtils::standardContextForUrl( myFile );
0092  *
0093  *   KDevelop::DUChainChangeSet* change = new KDevelop::DUChainChangeSet( top );
0094  * \endcode
0095  *
0096  * From here, you can alter declarations, contexts, and types.  Some examples:
0097  * \code
0098  *   // Create a new class definition
0099  *   KDevelop::Declaration* dec = new KDevelop::Declaration();
0100  *   dec->setIdentifier("MyNewClass");
0101  *   dec->setDeclarationIsDefinition();
0102  *
0103  *   KDevelop::StructureType::Ptr classType(new KDevelop::StructureType);
0104  *   classType->setClassType(KDevelop::StructureType::Class);
0105  *
0106  *   dec->setType(classType);
0107  * \endcode
0108  *
0109  * Make a member variable private and add accessor methods:
0110  * \code
0111  *   const QList<KDevelop::Declaration*> declarations = topContext->findDeclarations("m_myVariable");
0112  *
0113  *   for ( KDevelop::Declaration* d : declarations ) {
0114  *     if ( KDevelop::ClassMemberDeclaration* cd = dynamic_cast<KDevelop::ClassMemberDeclaration*>( d ) ) {
0115  *       if ( cd->accessPolicy() == KDevelop::Declaration::Public ) {
0116  *         KDevelop::DUChainRef* ref = change->modifyObject( cd );
0117  *
0118  *         // Make the variable private
0119  *         ref->setAccessPolicy( KDevelop::Declaration::Private );
0120  *
0121  *         // Create an accessor function
0122  *         KDevelop::Declaration* accessor = new KDevelop::Declaration();
0123  *         FunctionType::Ptr accessorType( new KDevelop::FunctionType() );
0124  *         accessorType->setReturnType( cd->type() );
0125  *         accessorType->setModifiers( KDevelop::AbstractType::ConstModifier );
0126  *         declaration->setType( accessorType );
0127  *         declaration->setIdentifier("myVariable");
0128  *
0129  *         // Provide an implementation for the accessor function
0130  *         // Would usually be done in a language-specific component of the code generator
0131  *         // Details to be worked out
0132  *         CompoundStatementAST* ast = ref->createAst<CompoundStatementAST>();
0133  *
0134  *         // blah
0135  *       }
0136  *     }
0137  *   }
0138  * \endcode
0139  */
0140 
0141 // DOXYGEN_REFERENCES = language/editor language/duchain
0142 // DOXYGEN_SET_WARN_LOGFILE=language/codegen/doxygen.log
0143 // DOXYGEN_SET_RECURSIVE=yes