aboutsummaryrefslogtreecommitdiff
path: root/src/kconfig_compiler/KConfigCodeGeneratorBase.h
blob: fdf3b7e392d2ab169539a508aad2d8f0d5696fb1 (plain)
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
/*
    This file is part of KDE.

    Copyright (C) 2020 Tomaz Cananbrava (tcanabrava@kde.org)
    Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org>
    Copyright (c) 2003 Waldo Bastian <bastian@kde.org>
    Copyright (c) 2003 Zack Rusin <zack@kde.org>
    Copyright (c) 2006 Michaƫl Larouche <michael.larouche@kdemail.net>
    Copyright (c) 2008 Allen Winter <winter@kde.org>
    Copyright (C) 2020 Tomaz Cananbrava (tcanabrava@kde.org)

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#ifndef KCONFIGCODEGENERATORBASE_H
#define KCONFIGCODEGENERATORBASE_H

#include <QString>
#include <QTextStream>
#include <QFile>
#include <QVector>

#include "KConfigXTParameters.h"

class CfgEntry;
struct ParseResult;

/* This class manages the base of writing a C - Based code */
class KConfigCodeGeneratorBase {
public:
    enum ScopeFinalizer {None, Semicolon};

    KConfigCodeGeneratorBase(
        const QString& inputFileName,  // The kcfg file 
        const QString& baseDir,        // where we should store the generated file
        const QString& fileName,       // the name of the generated file
        const KConfigXTParameters &parameters, // parameters passed to the generator 
        ParseResult &parseResult // The pre processed configuration entries
    );
    virtual ~KConfigCodeGeneratorBase();

    // iterates over the header list adding an #include directive.
    void addHeaders(const QStringList& header);

    // Create all the namespace indentation levels based on the parsed result and parameters */
    void beginNamespaces();

    // Closes all the namespaces adding lines with single '}'
    void endNamespaces();

    // Add the correct amount of whitespace in the code.
    QString whitespace();

    // start a block scope `{` and increase indentation level.
    void endScope(ScopeFinalizer finalizer = None);
    
    // end a block scope `}` and decrease indentation level.
    void startScope();

    // start writing to the output file
    virtual void start();

    // save the result on the disk
    void save();

    // Code Implementations
    // Implements the `Get` methods for the CfgEntry
    // TODO: write to the stream directly without returning a QString.
    QString memberAccessorBody(const CfgEntry *e, bool globalEnums) const;

    // Implements the `Set` methods for the CfgEntry
    void memberMutatorBody(const CfgEntry *e);

    // This is the code that creates the logic for the Setter / Mutator.
    // It *just* creates the if test, no body. The reason is that just
    // the if test was more than 20 lines of code and hard to understand
    // what was happening in a bigger function.
    void createIfSetLogic(const CfgEntry *e, const QString &varExpression);

protected:
    /* advance the number of spaces for the indentation level */
    void indent();

    /* reduce the number of spaces for the indentation level */
    void unindent();

    QString inputFile; // the base file name, input file is based on this.

    QString baseDir;   // Where we are going to save the file
    QString fileName;  // The file name

    const KConfigXTParameters &cfg; // The parameters passed via the kcfgc file
    ParseResult &parseResult;       // the result of the parsed kcfg file
    QTextStream stream;             // the stream that operates in the file to write data.
    QFile file;                     // The file handler.

    // Special access to `this->` and `const` thru the code.
    QString This;
    QString Const;

private:
    int indentLevel = 0;
};

#endif