summaryrefslogtreecommitdiffstats
path: root/kode/printer.cpp
blob: 7808f2902f861b5fe49a98b5d6dbd5c047a7b1ce (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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
/*
    This file is part of tdepim.

    Copyright (c) 2004 Cornelius Schumacher <schumacher@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.
*/

#include "printer.h"

#include <kdebug.h>
#include <ksavefile.h>

#include <tqfile.h>
#include <tqtextstream.h>

using namespace KODE;

Printer::Printer()
  : mCreationWarning( false ), mGenerator( "libkode" )
{
}

Printer::Printer( const Style &style )
  : mStyle( style ), mCreationWarning( false ), mGenerator( "libkode" )
{
}

void Printer::setCreationWarning( bool v )
{
  mCreationWarning = v;
}

void Printer::setGenerator( const TQString &g )
{
  mGenerator = g;
}

void Printer::setOutputDirectory( const TQString &o )
{
  mOutputDirectory = o;
}

void Printer::setSourceFile( const TQString &s )
{
  mSourceFile = s;
}

TQString Printer::functionSignature( const Function &f,
                                    const TQString &className,
                                    bool includeClassQualifier )
{
  TQString s;
  
  if ( f.isStatic() && !includeClassQualifier ) {
    s += "static ";
  }

  TQString ret = f.returnType();
  if ( !ret.isEmpty() ) {
    s += ret;
    if ( ret.right( 1 ) != "*" && ret.right( 1 ) != "&" ) {
      s += " ";
    }
  }
  if ( includeClassQualifier ) {
    s += mStyle.className( className ) + "::";
  }
  if ( className == f.name() ) {
    // Constructor
    s += mStyle.className( f.name() );
  } else {
    s += f.name();
  }
  s += "(";
  if ( !f.arguments().isEmpty() ) {
    s += " " + f.arguments().join( ", " ) + " ";
  }
  s += ")";
  if ( f.isConst() ) s += " const";

  return s;
}

TQString Printer::creationWarning()
{
  // Create warning about generated file
  TQString str = "// This file is generated by " + mGenerator;
  if ( !mSourceFile.isEmpty() ) {
    str += " from " + mSourceFile;
  }
  str += ".\n";
  
  str += "// All changes you do to this file will be lost.";

  return str;
}

TQString Printer::licenseHeader( const File &file )
{
  Code code;
  code += "/*";
  code.setIndent( 4 );

  code += "This file is part of " + file.project() + ".";
  code.newLine();
  
  TQStringList copyrights = file.copyrightStrings();
  if ( !copyrights.isEmpty() ) {
    code.addBlock( copyrights.join( "\n" ) );
    code.newLine();
  }
  
  code.addBlock( file.license().text() );

  code.setIndent( 0 );
  code += "*/";
  
  return code.text();
}

Code Printer::functionHeaders( const Function::List &functions,
                               const TQString &className,
                               int access )
{
  bool needNewLine = false;
  bool hasAccess = false;

  Code code;

  Function::List::ConstIterator it;
  for( it = functions.begin(); it != functions.end(); ++it ) {
    Function f = *it;
    if ( f.access() == access ) {
      if ( !hasAccess ) {
        code += f.accessAsString() + ":";
        hasAccess = true;
      }
      code.indent();
      if ( !(*it).docs().isEmpty() ) {
        code += "/**";
        code.indent();
        code.addFormattedText( (*it).docs() );
        code.unindent();
        code += "*/";
      }
      code += functionSignature( *it, className ) + ";";
      code.unindent();
      needNewLine = true;
    }
  }
  if ( needNewLine ) code.newLine();

  return code;
}

TQString Printer::classHeader( const Class &c )
{
  Code code;

  if ( !c.docs().isEmpty() ) {
    code += "/**";
    code.indent();
    code.addFormattedText( c.docs() );
    code.unindent();
    code += "*/";
  }

  TQString txt = "class " + mStyle.className( c.name() );

  Class::List baseClasses = c.baseClasses();
  if ( !baseClasses.isEmpty() ) {
    txt += " : ";
    Class::List::ConstIterator it;
    for( it = baseClasses.begin(); it != baseClasses.end(); ++it ) {
      Class bc = *it;
      
      if ( it != baseClasses.begin() ) txt +=", ";
      txt += "public ";
      if ( !bc.nameSpace().isEmpty() ) txt += bc.nameSpace() + "::";
      txt += bc.name();
    }
  }
  code += txt;

  code += "{";
  code.indent();

  if ( c.isTQObject() ) {
    code += "Q_OBJECT";
    code.newLine();
    code += "";
    code.newLine();
  }

  Function::List functions = c.functions();

  Typedef::List typedefs = c.typedefs();
  if ( typedefs.count() > 0 ) {
    code += "public:";
    code.indent();
    Typedef::List::ConstIterator it;
    for( it = typedefs.begin(); it != typedefs.end(); ++it ) {
      code += (*it).declaration();
    }
    code.unindent();
    code.newLine();
  }

  Enum::List enums = c.enums();
  if ( enums.count() > 0 ) {
    code += "public:";
    code.indent();
    Enum::List::ConstIterator it;
    for( it = enums.begin(); it != enums.end(); ++it ) {
      code += (*it).declaration();
    }
    code.unindent();
    code.newLine();
  }

  code.addBlock( functionHeaders( functions, c.name(), Function::Public ) );
  code.addBlock( functionHeaders( functions, c.name(), Function::Public | Function::Slot ) );
  code.addBlock( functionHeaders( functions, c.name(), Function::Signal ) );
  code.addBlock( functionHeaders( functions, c.name(), Function::Protected ) );
  code.addBlock( functionHeaders( functions, c.name(), Function::Protected | Function::Slot ) );
  code.addBlock( functionHeaders( functions, c.name(), Function::Private ) );
  code.addBlock( functionHeaders( functions, c.name(), Function::Private | Function::Slot ) );

  if ( !c.memberVariables().isEmpty() ) {
    Function::List::ConstIterator it;
    for( it = functions.begin(); it != functions.end(); ++it ) {
      if ( (*it).access() == Function::Private ) break;
    }
    if ( it == functions.end() ) code += "private:";

    code.indent();

    MemberVariable::List variables = c.memberVariables();
    MemberVariable::List::ConstIterator it2;
    for( it2 = variables.begin(); it2 != variables.end(); ++it2 ) {
      MemberVariable v = *it2;

      TQString decl;
      if ( v.isStatic() ) decl += "static ";
      decl += v.type();
      if ( v.type().right( 1 ) != "*" && v.type().right( 1 ) != "&" ) {
        decl += " ";
      }
      decl += v.name() + ";";

      code += decl;
    }
  }

  code.setIndent( 0 );
  code += "};";

  return code.text();
}

TQString Printer::classImplementation( const Class &c )
{
  Code code;

  bool needNewLine = false;

  MemberVariable::List vars = c.memberVariables();
  MemberVariable::List::ConstIterator itV;
  for( itV = vars.begin(); itV != vars.end(); ++itV ) {
    MemberVariable v = *itV;
    if ( !v.isStatic() ) continue;
    code += v.type() + c.name() + "::" + v.name() + " = " + v.initializer() +
            ";";
    needNewLine = true;
  }
  if ( needNewLine ) code.newLine();
  
  Function::List functions = c.functions();
  Function::List::ConstIterator it;
  for( it = functions.begin(); it != functions.end(); ++it ) {
    Function f = *it;

    // Omit signals
    if ( f.access() == Function::Signal )
      continue;

    code += functionSignature( f, c.name(), true );

    if ( !f.initializers().isEmpty() ) {
      code += ": " + f.initializers().join( ", " );
    }

    code += "{";
    code.addBlock( f.body(), 2 );
    code += "}";
    code += "";
  }

  if ( c.isTQObject() ) {
    code.newLine();
    code += "#include \"" + c.name().lower() + ".moc\"";
  }

  return code.text();
}

void Printer::printHeader( const File &f )
{
  Code out;

  if ( mCreationWarning ) out += creationWarning();

  out.addBlock( licenseHeader( f ) );

  // Create include guard
  TQString className = f.filename();
  className.replace( "-", "_" );

  TQString includeGuard;
  if ( !f.nameSpace().isEmpty() ) includeGuard += f.nameSpace().upper() + "_";
  includeGuard += className.upper() + "_H";

  out += "#ifndef " + includeGuard;
  out += "#define " + includeGuard;
  
  out.newLine();


  // Create includes
  TQStringList processed;
  Class::List classes = f.classes();
  Class::List::ConstIterator it;
  for( it = classes.begin(); it != classes.end(); ++it ) {
    TQStringList includes = (*it).headerIncludes();
    TQStringList::ConstIterator it2;
    for( it2 = includes.begin(); it2 != includes.end(); ++it2 ) {
      if ( processed.find( *it2 ) == processed.end() ) {
        out += "#include <" + *it2 + ">";
        processed.append( *it2 );
      }
    }
  }
  if ( !processed.isEmpty() ) out.newLine();


  // Create forward declarations
  processed.clear();
  for( it = classes.begin(); it != classes.end(); ++it ) {
    TQStringList decls = (*it).forwardDeclarations();
    TQStringList::ConstIterator it2;
    for( it2 = decls.begin(); it2 != decls.end(); ++it2 ) {
      if ( processed.find( *it2 ) == processed.end() ) {
        out += "class " + *it2 + ";";
        processed.append( *it2 );
      }
    }
  }
  if ( !processed.isEmpty() ) out.newLine();


  if ( !f.nameSpace().isEmpty() ) {
    out += "namespace " + f.nameSpace() + " {";
    out.newLine();
  }

  // Create content
  for( it = classes.begin(); it != classes.end(); ++it ) {
    out.addBlock( classHeader( *it ) );
    out.newLine();
  }

  if ( !f.nameSpace().isEmpty() ) {
    out += "}";
    out.newLine();
  }

  // Finish file
  out += "#endif";


  // Print to file
  TQString filename = f.filename() + ".h";

  if ( !mOutputDirectory.isEmpty() ) filename.prepend( mOutputDirectory + "/" );

  KSaveFile::backupFile( filename, TQString(), ".backup" );

  TQFile header( filename );
  if ( !header.open( IO_WriteOnly ) ) {
    kdError() << "Can't open '" << filename << "' for writing." << endl;
    return;
  }

  TQTextStream h( &header );

  h << out.text();

  header.close();
}

void Printer::printImplementation( const File &f, bool createHeaderInclude )
{
  Code out;

  if ( mCreationWarning ) out += creationWarning();

  out.addBlock( licenseHeader( f ) );

  out.newLine();

  // Create includes
  if ( createHeaderInclude ) {
    out += "#include \"" + f.filename() + ".h\"";
    out.newLine();
  }

  TQStringList includes = f.includes();
  TQStringList::ConstIterator it2;
  for( it2 = includes.begin(); it2 != includes.end(); ++it2 ) {
    out += "#include <" + *it2 + ">";
  }
  if ( !includes.isEmpty() ) out.newLine();

  // Create class includes
  TQStringList processed;
  Class::List classes = f.classes();
  Class::List::ConstIterator it;
  for( it = classes.begin(); it != classes.end(); ++it ) {
    TQStringList includes = (*it).includes();
    TQStringList::ConstIterator it2;
    for( it2 = includes.begin(); it2 != includes.end(); ++it2 ) {
      if ( processed.find( *it2 ) == processed.end() ) {
        out += "#include <" + *it2 + ">";
        processed.append( *it2 );
      }
    }
  }
  if ( !processed.isEmpty() ) out.newLine();

  if ( !f.nameSpace().isEmpty() ) {
    out += "using namespace " + f.nameSpace() + ";";
    out.newLine();
  }

  // 'extern "C"' declarations
  TQStringList externCDeclarations = f.externCDeclarations();
  if ( !externCDeclarations.isEmpty() ) {
    out += "extern \"C\" {";
    TQStringList::ConstIterator it;
    for( it = externCDeclarations.begin(); it != externCDeclarations.end();
         ++it ) {
      out += *it + ";";
    }
    out += "}";
    out.newLine();
  }

  // File variables
  Variable::List vars = f.fileVariables();
  Variable::List::ConstIterator itV;
  for( itV = vars.begin(); itV != vars.end(); ++itV ) {
    Variable v = *itV;
    TQString str;
    if ( v.isStatic() ) str += "static ";
    str += v.type() + " " + v.name() + ";";
    out += str;
  }
  if ( !vars.isEmpty() ) out.newLine();

  // File code
  if ( !f.fileCode().isEmpty() ) {
    out += f.fileCode();
    out.newLine();
  }

  // File functions
  Function::List funcs = f.fileFunctions();
  Function::List::ConstIterator itF;
  for( itF = funcs.begin(); itF != funcs.end(); ++itF ) {
    Function f = *itF;
    out += functionSignature( f );
    out += "{";
    out.addBlock( f.body(), 2 );
    out += "}";
    out.newLine();
  }

  // Classes
  for( it = classes.begin(); it != classes.end(); ++it ) {
    TQString str = classImplementation( *it );
    if ( !str.isEmpty() ) out += classImplementation( *it );
  }

  // Print to file
  TQString filename = f.filename() + ".cpp";

  if ( !mOutputDirectory.isEmpty() ) filename.prepend( mOutputDirectory + "/" );

  KSaveFile::backupFile( filename, TQString(), ".backup" );

  TQFile implementation( filename );
  if ( !implementation.open( IO_WriteOnly ) ) {
    kdError() << "Can't open '" << filename << "' for writing." << endl;
    return;
  }

  TQTextStream h( &implementation );

  h << out.text();

  implementation.close();
}

void Printer::printAutoMakefile( const AutoMakefile &am )
{
  TQString filename = "Makefile.am";

  if ( !mOutputDirectory.isEmpty() ) filename.prepend( mOutputDirectory + "/" );

  KSaveFile::backupFile( filename, TQString(), ".backup" );

  TQFile file( filename );
  if ( !file.open( IO_WriteOnly ) ) {
    kdError() << "Can't open '" << filename << "' for writing." << endl;
    return;
  }

  TQTextStream ts( &file );

  ts << am.text();
}