forked from rochus-keller/GnTools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGnCodeModel.cpp
1220 lines (1118 loc) · 41 KB
/
GnCodeModel.cpp
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2019 Rochus Keller <mailto:[email protected]>
*
* This file is part of the GN parser library.
*
* The following is the license that applies to this copy of the
* library. For a license to use the library under conditions
* other than those described here, please email to [email protected].
*
* GNU General Public License Usage
* This file may be used under the terms of the GNU General Public
* License (GPL) versions 2.0 or 3.0 as published by the Free Software
* Foundation and appearing in the file LICENSE.GPL included in
* the packaging of this file. Please review the following information
* to ensure GNU General Public Licensing requirements will be met:
* http://www.fsf.org/licensing/licenses/info/GPLv2.html and
* http://www.gnu.org/copyleft/gpl.html.
*/
#include "GnCodeModel.h"
#include "GnErrors.h"
#include "GnLexer.h"
#include "GnParser.h"
#include <QBuffer>
#include <QtDebug>
using namespace Gn;
static const char* s_buildGn = "BUILD.gn";
static const char* s_dotFile = ".gn";
// Werte aus GN master 152c5144ceed9592c20f0 12.7.2019
static const char* s_knownVars[] =
{
// dotfile
"arg_file_template",
"buildconfig",
"check_targets",
"exec_script_whitelist",
"root",
"script_executable",
"secondary_source",
"default_args",
// Built-in predefined variables (type "gn help <variable>" for more help):
"current_cpu", //: [string] The processor architecture of the current toolchain.
"current_os", //: [string] The operating system of the current toolchain.
"current_toolchain", //: [string] Label of the current toolchain.
"default_toolchain", //: [string] Label of the default toolchain.
"host_cpu", // : [string] The processor architecture that GN is running on.
"host_os", //: [string] The operating system that GN is running on.
"invoker", // : [string] The invoking scope inside a template.
"python_path", //: [string] Absolute path of Python.
"root_build_dir", //: [string] Directory where build commands are run.
"root_gen_dir", //: [string] Directory for the toolchain's generated files.
"root_out_dir", //: [string] Root directory for toolchain output files.
"target_cpu", //: [string] The desired cpu architecture for the build.
"target_gen_dir", //: [string] Directory for a target's generated files.
"target_name", //: [string] The name of the current target.
"target_os", //: [string] The desired operating system for the build.
"target_out_dir", //: [string] Directory for target output files.
// Variables you set in targets (type "gn help <variable>" for more help):
"aliased_deps", //: [scope] Set of crate-dependency pairs.
"all_dependent_configs", //: [label list] Configs to be forced on dependents.
"allow_circular_includes_from", //: [label list] Permit includes from deps.
"arflags", //: [string list] Arguments passed to static_library archiver.
"args", //: [string list] Arguments passed to an action.
"asmflags", //: [string list] Flags passed to the assembler.
"assert_no_deps", //: [label pattern list] Ensure no deps on these targets.
"bundle_contents_dir", //: Expansion of {{bundle_contents_dir}} in create_bundle.
"bundle_deps_filter", //: [label list] A list of labels that are filtered out.
"bundle_executable_dir", //: Expansion of {{bundle_executable_dir}} in create_bundle
"bundle_resources_dir", //: Expansion of {{bundle_resources_dir}} in create_bundle.
"bundle_root_dir", //: Expansion of {{bundle_root_dir}} in create_bundle.
"cflags", //: [string list] Flags passed to all C compiler variants.
"cflags_c", //: [string list] Flags passed to the C compiler.
"cflags_cc", //: [string list] Flags passed to the C++ compiler.
"cflags_objc", //: [string list] Flags passed to the Objective C compiler.
"cflags_objcc", //: [string list] Flags passed to the Objective C++ compiler.
"check_includes", //: [boolean] Controls whether a target's files are checked.
"code_signing_args", //:[string list] Arguments passed to code signing script.
"code_signing_outputs", //:[file list] Output files for code signing step.
"code_signing_script", //:[file name] Script for code signing.
"code_signing_sources", //:[file list] Sources for code signing step.
"complete_static_lib", //:[boolean] Links all deps into a static library.
"configs", //:[label list] Configs applying to this target or config.
"contents", //:Contents to write to file.
"crate_name", //:[string] The name for the compiled crate.
"crate_root", //:[string] The root source file for a binary or library.
"crate_type", //:[string] The type of linkage to use on a shared_library.
"data", //:[file list] Runtime data file dependencies.
"data_deps", //:[label list] Non-linked dependencies.
"data_keys", //:[string list] Keys from which to collect metadata.
"defines", //:[string list] C preprocessor defines.
"depfile", //:[string] File name for input dependencies for actions.
"deps", //:[label list] Private linked dependencies.
"edition", //:[string] The rustc edition to use in compiliation.
"friend", //:[label pattern list] Allow targets to include private headers.
"include_dirs", //:[directory list] Additional include directories.
"inputs", //:[file list] Additional compile-time dependencies.
"ldflags", //:[string list] Flags passed to the linker.
"lib_dirs", //:[directory list] Additional library directories.
"libs", //:[string list] Additional libraries to link.
"metadata", //:[scope] Metadata of this target.
"output_conversion", //:Data format for generated_file targets.
"output_dir", //:[directory] Directory to put output file in.
"output_extension", //:[string] Value to use for the output's file extension.
"output_name", //:[string] Name for the output file other than the default.
"output_prefix_override", //:[boolean] Don't use prefix for output name.
"outputs", //:[file list] Output files for actions and copy targets.
"partial_info_plist", //:[filename] Path plist from asset catalog compiler.
"pool", //:[string] Label of the pool used by the action.
"precompiled_header", //:[string] Header file to precompile.
"precompiled_header_type", //:[string] "gcc" or "msvc".
"precompiled_source", //:[file name] Source file to precompile.
"product_type", //:[string] Product type for Xcode projects.
"public", //:[file list] Declare public header files for a target.
"public_configs", //:[label list] Configs applied to dependents.
"public_deps", //:[label list] Declare public dependencies.
"rebase", //:[boolean] Rebase collected metadata as files.
"response_file_contents", //:[string list] Contents of .rsp file for actions.
"script", //:[file name] Script file for actions.
"sources", //:[file list] Source files for a target.
"testonly", //:[boolean] Declares a target must only be used for testing.
"visibility", //:[label list] A list of labels that can depend on a target.
"walk_keys", //:[string list] Key(s) for managing the metadata collection walk.
"write_runtime_deps", //:Writes the target's runtime_deps to the given path.
"xcode_extra_attributes", //:[scope] Extra attributes for Xcode projects.
"xcode_test_application_name", //:[string] Name for Xcode test target.
// weitere empirisch gefundene
"toolchain_args",
0
};
static const char* s_knownFuncs[] =
{
// Buildfile functions (type "gn help <function>" for more help):
"assert", //:Assert an expression is true at generation time.
"declare_args", //:Declare build arguments.
"defined", //:Returns whether an identifier is defined.
"exec_script", //:Synchronously run a script and return the output.
"foreach", //:Iterate over a list.
"forward_variables_from", //:Copies variables from a different scope.
"get_label_info", //:Get an attribute from a target's label.
"get_path_info", //:Extract parts of a file or directory name.
"get_target_outputs", //:[file list] Get the list of outputs from a target.
"getenv", //:Get an environment variable.
"import", //:Import a file into the current scope.
"not_needed", //:Mark variables from scope as not needed.
"print", //:Prints to the console.
"process_file_template", //:Do template expansion over a list of files.
"read_file", //:Read a file into a variable.
"rebase_path", //:Rebase a file or directory to another location.
"set_default_toolchain", //:Sets the default toolchain name.
"set_defaults", //:Set default values for a target type.
"set_sources_assignment_filter", //:Set a pattern to filter source files.
"split_list", //:Splits a list into N different sub-lists.
"string_replace", //:Replaces substring in the given string.
"tool", //:Specify arguments to a toolchain tool.
"write_file", //:Write a file to disk.
0
};
static const char* s_namedObjs[] =
{
// Buildfile functions (type "gn help <function>" for more help):
"config", //:Defines a configuration object.
"pool", //:Defines a pool object.
"template", //:Define a template rule.
"toolchain", //:Defines a toolchain.
// Target declarations (type "gn help <function>" for more help):
"action", //:Declare a target that runs a script a single time.
"action_foreach", //:Declare a target that runs a script over a set of files.
"bundle_data", //:[iOS/macOS] Declare a target without output.
"copy", //:Declare a target that copies files.
"create_bundle", //:[iOS/macOS] Build an iOS or macOS bundle.
"executable", //:Declare an executable target.
"generated_file", //:Declare a generated_file target.
"group", //:Declare a named group of targets.
"loadable_module", //:Declare a loadable module target.
"rust_library", //:Declare a Rust library target.
"shared_library", //:Declare a shared library target.
"source_set", //:Declare a source set target.
"static_library", //:Declare a static library target.
"target", //:Declare an target with the given programmatic type.
0
};
static QStringList collectBuildFiles( const QDir& dir )
{
QStringList res;
QStringList files = dir.entryList( QStringList() << QString("*.gn") << QString("*.gni"), QDir::Files, QDir::Name );
foreach( const QString& f, files )
{
res.append( dir.absoluteFilePath(f) );
}
files = dir.entryList( QDir::Dirs | QDir::NoDotAndDotDot | QDir::NoSymLinks, QDir::Name );
foreach( const QString& f, files )
res += collectBuildFiles( QDir( dir.absoluteFilePath(f) ) );
return res;
}
CodeModel::CodeModel(QObject *parent) : QObject(parent)
{
d_errs = new Errors(this);
d_errs->setReportToConsole(true);
}
CodeModel::~CodeModel()
{
clear();
}
bool CodeModel::parseDir(const QDir& dir)
{
clear();
const QString dotfile = findDotFile( dir );
if( dotfile.isEmpty() )
{
d_errs->error(Errors::Semantics, dir.absoluteFilePath(s_dotFile), 0, 0,
tr("could not find any dotfile in current or super directories"));
return false;
}
d_sourceRoot = QFileInfo(dotfile).absoluteDir();
d_errs->setRoot(d_sourceRoot);
QStringList files;
files += dotfile;
files += collectBuildFiles(d_sourceRoot);
// qDebug() << "####" << files.size() << "files to parse in" << d_sourceRoot.absolutePath();
foreach( const QString& f, files )
{
parseFile(f);
}
return d_errs->getErrCount() == 0;
}
QString CodeModel::calcPath(SynTree* ref) const
{
Q_ASSERT( ref != 0 && ref->d_tok.d_type == Tok_string );
return calcPath(ref->d_tok.getEscapedVal(), ref->d_tok.d_sourcePath);
}
QString CodeModel::calcPath(const QByteArray& path, const QByteArray& ref ) const
{
QString tmp = QString::fromUtf8(path);
if( tmp.startsWith("//") )
tmp = d_sourceRoot.absoluteFilePath(tmp.mid(2));
else if( tmp.startsWith("/"))
tmp = tmp; // NOP, absolute path
else if( !tmp.isEmpty() && !ref.isEmpty() )
tmp = QFileInfo(QString::fromUtf8(ref)).absoluteDir().absoluteFilePath(tmp);
else if( !tmp.isEmpty() )
tmp = d_sourceRoot.absoluteFilePath(tmp);
return QFileInfo(tmp).canonicalFilePath();
}
QString CodeModel::calcPath(QByteArray path, const QByteArray& ref, bool addBUILDgn) const
{
if( addBUILDgn )
path += QByteArray("/") + s_buildGn;
return calcPath(path,ref);
}
QString CodeModel::relativePath(const QByteArray& sourcePath) const
{
return d_sourceRoot.relativeFilePath(QString::fromUtf8(sourcePath));
}
SynTree*CodeModel::findSymbolBySourcePos(const QByteArray& sourcePath, quint32 line, quint16 col) const
{
Files::const_iterator i = d_files.find(sourcePath.constData());
if( i == d_files.end() )
i = d_files.find(Lexer::getSymbol(sourcePath).constData());
if( i == d_files.end() || i.value().d_st == 0 )
return 0;
return findSymbolBySourcePos( i.value().d_st, line, col );
}
SynTree*CodeModel::findFromPath(const QByteArray& path, const QByteArray& callerPath)
{
if( path.isEmpty() )
return 0;
PathIdentPair pip = extractPathIdentFromString(path);
if( pip.first.isEmpty() && pip.second.isEmpty() )
return 0;
QByteArray file = callerPath;
if( !pip.first.isEmpty() )
{
file = calcPath( pip.first, callerPath, !pip.second.isEmpty() ).toUtf8();
if( file.isEmpty() )
return 0;
}
const Scope* s = getScope( file );
if( s == 0 )
return 0;
if( !pip.second.isEmpty() )
{
const QByteArray name = Lexer::getSymbol(pip.second);
Scope* o = s->findObject(name);
if( o )
return o->d_st;
}else
return s->d_st;
return 0;
}
SynTree*CodeModel::findDefinition(const SynTree* st)
{
if( st == 0 )
return 0;
if( st->d_tok.d_type == Tok_string )
return findFromPath( st->d_tok.getEscapedVal(), st->d_tok.d_sourcePath );
else if( st->d_tok.d_type == Tok_identifier )
{
ObjRefs::const_iterator i = d_allObjDefs.find( st->d_tok.d_val.constData() );
if( i != d_allObjDefs.end() )
{
if( i.value().size() == 1 )
return i.value().first()->d_st;
}
}
return 0;
}
QByteArrayList CodeModel::getFileList() const
{
QByteArrayList res;
Files::const_iterator i;
for( i = d_files.begin(); i != d_files.end(); ++i )
res.append( i.value().d_name );
std::sort( res.begin(), res.end() );
return res;
}
CodeModel::Scope*CodeModel::getScope(const QByteArray& sourcePath) const
{
Files::const_iterator i = d_files.find( sourcePath.constData() );
if( i == d_files.end() )
i = d_files.find(Lexer::getSymbol(sourcePath).constData());
if( i == d_files.end() || i.value().d_st == 0 )
return 0;
else
return const_cast<Scope*>(&i.value());
}
bool CodeModel::isKnownVar(const char* id) const
{
return d_knownVars.contains( id );
}
bool CodeModel::isKnownObj(const QByteArray& id) const
{
return d_knownFuncs.contains( id.constData() ) ||
d_namedObjs.contains( id.constData() );
}
bool CodeModel::isKnownId(const QByteArray& id) const
{
return d_knownVars.contains( id.constData() ) || d_knownFuncs.contains( id.constData() ) ||
d_namedObjs.contains( id.constData() );
}
QString CodeModel::findDotFile(const QDir& dir)
{
const QFileInfo path = dir.absoluteFilePath(s_dotFile);
if( path.exists() )
return path.absoluteFilePath();
else
{
QDir up = path.absoluteDir();
if( !up.cdUp() )
return QString();
else
return findDotFile( up );
}
}
void CodeModel::clear()
{
d_errs->clear();
for( Files::iterator i = d_files.begin(); i != d_files.end(); ++i )
{
delete i.value().d_st;
}
d_files.clear();
Lexer::clearSymbols();
d_knownVars.clear();
d_knownFuncs.clear();
d_namedObjs.clear();
const char** s = 0;
s = s_knownVars;
while( *s )
{
d_knownVars.insert( Lexer::getSymbol(*s).constData() );
s++;
}
s = s_knownFuncs;
while( *s )
{
d_knownFuncs.insert( Lexer::getSymbol(*s).constData() );
s++;
}
s = s_namedObjs;
while( *s )
{
d_namedObjs.insert( Lexer::getSymbol(*s).constData() );
s++;
}
d_foreach = Lexer::getSymbol("foreach").constData();
d_import = Lexer::getSymbol("import").constData();
d_fileKind = Lexer::getSymbol("file");
d_declare_args = Lexer::getSymbol("declare_args");
d_allRhs.clear();
d_allLhs.clear();
d_allObjDefs.clear();
d_allFuncRefs.clear();
d_allImports.clear();
d_allUnresolvedImports.clear();
d_allUnnamedObjs.clear();
d_unresolvedRefs.clear();
d_declaredArgs.clear();
}
CodeModel::Scope* CodeModel::parseFile(const QString& path, bool viaImport)
{
const QByteArray pathSym = Lexer::getSymbol(path.toUtf8());
Files::iterator i = d_files.find(pathSym.constData());
if( i != d_files.end() )
return &i.value();
QFile in( path );
if( !in.open(QIODevice::ReadOnly) )
{
d_errs->warning( Errors::Lexer, path, 0, 0, tr("cannot open file for reading") );
return 0;
}
// qDebug() << "*** parsing file" << ( d_files.size() + 1 ) << d_sourceRoot.relativeFilePath(path) <<
// ( viaImport ? "via import" : "" );
Gn::Lexer lex;
lex.setStream( &in, path );
lex.setErrors(d_errs);
lex.setIgnoreComments(false);
lex.setPackComments(true);
Gn::Parser p(&lex,d_errs);
p.RunParser();
Q_ASSERT( p.d_root.d_children.isEmpty() ||
( p.d_root.d_children.size() == 1 &&
p.d_root.d_children.first()->d_tok.d_type == SynTree::R_StatementList ) );
Scope* scope = 0;
if( !p.d_root.d_children.isEmpty() )
{
SynTree* st = p.d_root.d_children.first();
scope = &d_files[pathSym.constData()];
scope->d_kind = d_fileKind;
scope->d_name = pathSym;
p.d_root.d_children.clear();
// Analyze file
Q_ASSERT( scope != 0 && st != 0 );
statementList(st,scope);
scope->d_st = st; // wird erst gesetzt wenn Analyse fertig
}
return scope;
}
void CodeModel::statementList(SynTree* st, Scope* sc)
{
Q_ASSERT( st != 0 && st->d_tok.d_type == SynTree::R_StatementList );
foreach( SynTree* s, st->d_children )
{
statement(s,sc);
}
}
void CodeModel::statement(SynTree* st, Scope* sc)
{
Q_ASSERT( st != 0 && st->d_tok.d_type == SynTree::R_Statement && st->d_children.size() == 1 );
switch( st->d_children.first()->d_tok.d_type )
{
case SynTree::R_Assignment:
assignment_( st->d_children.first(), sc );
break;
case SynTree::R_Call:
call_( st->d_children.first(), sc );
break;
case SynTree::R_Condition:
condition_( st->d_children.first(), sc );
break;
default:
break;
}
}
void CodeModel::call_(SynTree* st, Scope* sc)
{
Q_ASSERT( st->d_children.size() >= 3 && st->d_children[0]->d_tok.d_type == Tok_identifier );
const char* kind = st->d_children[0]->d_tok.d_val.constData();
d_allFuncRefs[kind].append(st->d_children[0]);
if( kind == d_foreach )
{
// no new scope
loop_(st,sc);
}else if( kind == d_import )
{
import_(st,sc);
}else if( d_namedObjs.contains(kind)
|| !d_knownFuncs.contains(kind) ) // template instances
{
// new scope
namedObj_( st, sc, st->d_children[0]->d_tok.d_val );
}else
{
// new scope
function_( st, sc, st->d_children[0]->d_tok.d_val );
}
}
void CodeModel::loop_(SynTree* st, CodeModel::Scope* scope)
{
if( st->d_children[2]->d_tok.d_type == Tok_Rpar || st->d_children.last()->d_tok.d_type != SynTree::R_Block )
{
d_errs->error(Errors::Syntax,st,tr("invalid foreach statement") );
return;
}
Q_ASSERT( st->d_children[2]->d_tok.d_type == SynTree::R_ExprList && st->d_children.size() == 5
&& st->d_children.last()->d_tok.d_type == SynTree::R_Block );
if( st->d_children[2]->d_children.size() != 2 )
{
d_errs->error(Errors::Syntax,st,tr("invalid expression list in foreach statement") );
return;
}
SynTree* var = flatten(st->d_children[2]->d_children.first() );
if( var->d_tok.d_type != Tok_identifier )
{
d_errs->error(Errors::Syntax,st,tr("invalid loop variable in foreach statement") );
}else
d_allLhs[var->d_tok.d_val.constData()].append(var);
expr( st->d_children[2]->d_children.last(), scope );
// TODO: muss man var irgendwie in den scope bringen?
block( st->d_children.last(), scope );
}
void CodeModel::import_(SynTree* st, CodeModel::Scope* sc)
{
// Import
// The imported file's scope will be merged with the scope at the point import
// was called. If there is a conflict (both the current scope and the imported
// file define some variable or rule with the same name but different value), a
// runtime error will be thrown. Therefore, it's good practice to minimize the
// stuff that an imported file defines.
if( st->d_children[2]->d_tok.d_type == Tok_Rpar )
{
d_errs->error(Errors::Syntax,st,tr("invalid import statement") );
return;
}
Q_ASSERT( st->d_children[2]->d_tok.d_type == SynTree::R_ExprList && !st->d_children[2]->d_children.isEmpty() );
SynTree* ref = flatten(st->d_children[2]);
bool resolved = false;
if( ref->d_tok.d_type == Tok_string )
{
string(ref,sc);
if( ref->d_children.isEmpty() )
{
// Name known
const QString path = calcPath(ref);
if( !path.isEmpty() )
{
const QByteArray pathSym = Lexer::getSymbol(path.toUtf8());
d_allImports[pathSym.constData()].append(ref);
if( QFileInfo(path).exists() )
{
// TODO: ev. verhindern dass direkt selber importiert
Scope* res = parseFile( path, true );
if( res )
{
resolved = true;
sc->d_relovedImports.insert(res->d_name.constData(),res);
}
}else
d_errs->warning(Errors::Semantics, ref, tr("import file doesn't exist: %1").arg(path));
}
}
}else
{
expr(st->d_children[2]->d_children.first(),sc);
}
if( !resolved )
{
sc->d_unresolvedImports.append(st->d_children[2]->d_children.first() );
d_allUnresolvedImports.append(st->d_children[2]->d_children.first());
}
}
void CodeModel::expr(SynTree* st, CodeModel::Scope* sc)
{
Q_ASSERT( st != 0 && st->d_tok.d_type == SynTree::R_Expr && !st->d_children.isEmpty() );
unaryExpr( st->d_children.first(), sc );
if( st->d_children.size() > 1 )
{
if( st->d_children.size() == 2 )
{
exprNlr( st->d_children.last(), sc );
}else
Q_ASSERT( false );
}
}
void CodeModel::exprList(SynTree* st, CodeModel::Scope* sc)
{
Q_ASSERT( st != 0 && st->d_tok.d_type == SynTree::R_ExprList );
foreach( SynTree* e, st->d_children )
expr(e,sc);
}
void CodeModel::block(SynTree* st, CodeModel::Scope* sc)
{
Q_ASSERT( st != 0 && st->d_tok.d_type == SynTree::R_Block && st->d_children.size() == 3
&& st->d_children.first()->d_tok.d_type == Tok_Lbrace
&& st->d_children.last()->d_tok.d_type == Tok_Rbrace );
statementList( st->d_children[1], sc );
}
SynTree* CodeModel::flatten(SynTree* st, int stopAt)
{
if( st == 0 )
return 0;
while( st->d_children.size() == 1 && ( stopAt == 0 || st->d_tok.d_type != stopAt ) )
st = st->d_children.first();
return st;
}
SynTree*CodeModel::firstToken(SynTree* st)
{
if( st == 0 )
return 0;
if( st->d_tok.d_type < TT_MaxToken )
return st;
foreach( SynTree* sub, st->d_children )
{
SynTree* res = firstToken(sub);
if( res )
return res;
}
return 0;
}
static int findNonEscapedDollar( const QByteArray& str, int start = 0 )
{
int pos = start;
while( pos < str.size() )
{
pos = str.indexOf( '$', pos );
if( pos != -1 )
{
if( pos > 0 && str[pos-1] == '\\' )
{
pos++;
continue;
}else
return pos;
}else
return pos;
}
return -1;
}
CodeModel::Dollars CodeModel::findDollars(const QByteArray& str)
{
// str ist begrenzt durch "" und kann die Escapes \\, \$ und \" enthalten
int pos = findNonEscapedDollar( str );
Dollars res;
while( pos != -1 )
{
int len = 0;
if( pos + 1 < str.size() )
{
if( str[pos+1] == '{' )
{
const int pos2 = str.indexOf('}', pos+1 );
if( pos2 == -1 )
{
return res;
}else
{
len = pos2 - pos + 1;
res << Dollar( pos, len );
}
}else if( str[pos+1] == '0' )
{
len = 5; // hex char $0xff
res << Dollar(pos,len);
}else
{
int i = pos + 1;
while( i < str.size() )
{
if( !::isalnum( str[i] ) && str[i] != '_' )
break;
i++;
}
len = i - pos;
res << Dollar(pos,len);
}
}
pos = findNonEscapedDollar( str, pos + len );
}
return res;
}
static inline char get( const QByteArray& str, int i )
{
if( i < str.size() )
return str[i];
else
return 0;
}
static inline bool looksLikeImplicitNamePath( const QByteArray& str )
{
Q_ASSERT( !str.contains(':') );
if( str.isEmpty() )
return false;
int i = 0;
if( str[i] == '"' )
i++;
int lastSlash = -1;
if( get(str,i) == '/' ) // starts with "/"
{
lastSlash = i;
i++;
if( get(str,i) == '/' ) // starts with "//"
{
lastSlash = i;
i++;
}
}
while( i < str.size() )
{
const char c = get(str,i);
if( c == '\\' )
return false;
else if( c == '/' )
{
if( lastSlash != -1 && i - lastSlash == 0 ) // embedded "//"
return false;
lastSlash = i;
}
i++;
}
i = str.size() - 1;
if( get(str,i) == '"' )
i--;
if( lastSlash == i )
return false; // ends with "/"
// if( lastSlash == -1 )
// return false; // no "/" at all
// now check whether the last part has a suffix
return str.indexOf( '.', lastSlash + 1 ) == -1; // true wenn kein '.' enthalten
}
bool CodeModel::looksLikeFilePath( const QByteArray& str )
{
if( str.contains('/') )
return true;
if( str.contains('\\') )
return true;
if( str.contains('.') && !str.endsWith('.') )
return true;
return false;
}
CodeModel::PathIdentPair CodeModel::extractPathIdentFromString(QByteArray str)
{
if( str.isEmpty() )
return PathIdentPair();
const int pos1 = str.indexOf(':');
if( pos1 == -1 )
{
// allow for implicit names
if( looksLikeImplicitNamePath(str) )
{
QByteArray name;
const int pos = str.lastIndexOf('/');
if( pos != -1 )
name = str.mid(pos+1);
//else
// name = str; // TODO: we require at least one "/" which might be too strict
return PathIdentPair(str,name);
}
return PathIdentPair(str,QByteArray()); // path only
#if 0 // no, if ':' is missing and no implicit path/name we always conclude path here
if( looksLikeFilePath(str) )
return PathIdentPair(str,QByteArray()); // path only
else if( str.contains(' ') )
return PathIdentPair(); // neither path nor name
else
return PathIdentPair(QByteArray(),str); // name only
#endif
}
if( str.indexOf(':', pos1+1 ) != -1 )
return PathIdentPair(); // invalid format
PathIdentPair res;
const int pos2 = str.indexOf( '(', pos1+1 );
if( pos2 != -1 )
{
if( pos2 - pos1 == 1 )
return PathIdentPair(); // invalid ident
else
res.second = str.mid(pos1+1, pos2-pos1-1 );
}else
res.second = str.mid(pos1+1);
res.first = str.left(pos1);
return res;
}
void CodeModel::condition_(SynTree* st, CodeModel::Scope* scope)
{
Q_ASSERT( st->d_tok.d_type == SynTree::R_Condition && st->d_children.size() >= 5 );
expr( st->d_children[2], scope );
block( st->d_children[4], scope );
if( st->d_children.size() > 5 )
{
Q_ASSERT( st->d_children.size() == 7 );
if( st->d_children[6]->d_tok.d_type == SynTree::R_Condition )
condition_( st->d_children[6], scope );
else
block( st->d_children[6], scope );
}
}
void CodeModel::assignment_(SynTree* st, CodeModel::Scope* sc)
{
Q_ASSERT( st != 0 && st->d_children.size() == 3 && st->d_children.first()->d_tok.d_type == SynTree::R_LValue &&
st->d_children[1]->d_tok.d_type == SynTree::R_AssignOp &&
st->d_children.last()->d_tok.d_type == SynTree::R_Expr );
lvalue( st->d_children.first(), sc );
expr( st->d_children.last(), sc );
}
void CodeModel::lvalue(SynTree* st, CodeModel::Scope* sc)
{
Q_ASSERT( st != 0 && st->d_tok.d_type == SynTree::R_LValue && !st->d_children.isEmpty()
&& st->d_children.first()->d_tok.d_type == Tok_identifier );
SynTree* id = st->d_children.first();
if( st->d_children.size() > 1 )
{
if( st->d_children[1]->d_tok.d_type == Tok_Lbrack ) // ArrayAccess
{
Q_ASSERT( st->d_children.size() == 4 && st->d_children[3]->d_tok.d_type == Tok_Rbrack );
varLhs(id,sc);
expr( st->d_children[2], sc );
}else if( st->d_children[1]->d_tok.d_type == Tok_Dot ) // ScopeAccess
{
Q_ASSERT( st->d_children.size() == 3 && st->d_children[2]->d_tok.d_type == Tok_identifier );
varRhs(id,sc); // TODO ist das richtig?
varLhs(st->d_children[2],sc);
}else
Q_ASSERT( false );
}else // plain ident
varLhs(id,sc);
}
void CodeModel::primaryExpr(SynTree* st, CodeModel::Scope* sc)
{
Q_ASSERT( st != 0 && !st->d_children.isEmpty() );
switch( st->d_children.first()->d_tok.d_type )
{
case SynTree::R_Call:
call_( st->d_children.first(), sc );
break;
case Tok_string:
string( st->d_children.first(), sc ); // parses Tok_string if $ included
break;
case Tok_Lpar:
Q_ASSERT( st->d_children.size() == 3 );
expr( st->d_children[1], sc );
break;
case SynTree::R_Scope_:
Q_ASSERT( !st->d_children.first()->d_children.isEmpty() );
block( st->d_children.first()->d_children.first(), sc ); // TODO: neuer scope?
break;
case Tok_identifier:
varRhs(st->d_children.first(), sc);
break;
case SynTree::R_ArrayAccess:
arrayAccess(st->d_children.first(), sc);
break;
case SynTree::R_ScopeAccess:
scopeAccess(st->d_children.first(), sc);
break;
case SynTree::R_List_:
list(st->d_children.first(), sc);
break;
}
}
void CodeModel::unaryExpr(SynTree* st, CodeModel::Scope* sc)
{
Q_ASSERT( st != 0 && st->d_tok.d_type == SynTree::R_UnaryExpr );
if( st->d_children.size() == 1 )
{
primaryExpr( st->d_children.first(), sc );
}else if( st->d_children.size() == 2 )
{
Q_ASSERT( st->d_children.first()->d_tok.d_type == SynTree::R_UnaryOp );
unaryExpr( st->d_children.last(), sc );
}else
Q_ASSERT( false );
}
void CodeModel::exprNlr(SynTree* st, CodeModel::Scope* sc)
{
Q_ASSERT( st != 0 && st->d_tok.d_type == SynTree::R_Expr_nlr_ && st->d_children.size() >= 2 );
Q_ASSERT( st->d_children.first()->d_tok.d_type == SynTree::R_BinaryOp );
expr( st->d_children[1], sc );
if( st->d_children.size() > 2 )
{
if( st->d_children.size() == 3 )
{
exprNlr( st->d_children.last(), sc );
}else
Q_ASSERT(false);
}
}
void CodeModel::string(SynTree* st, CodeModel::Scope* sc)
{
// ev. findDollars verwenden
Q_ASSERT( st != 0 && st->d_tok.d_type == Tok_string );
const QByteArray str = st->d_tok.d_val;
int pos = findNonEscapedDollar( str );
while( pos != -1 )
{
int len = 0;
if( pos + 1 < str.size() )
{
if( str[pos+1] == '{' )
{
const int pos2 = str.indexOf('}', pos+1 );
if( pos2 == -1 )
{
d_errs->error(Errors::Syntax, st, tr("'${' without terminating '}'") );
return;
}else
{
len = pos2 - pos;
stringVar_( st, sc, pos+2, pos2 - pos - 2 );
}
}else if( str[pos+1] == '0' )
len = 5; // hex char $0xff
else
{
int i = pos + 1;
while( i < str.size() )
{
if( !::isalnum( str[i] ) && str[i] != '_' )
break;
i++;
}
len = i - pos;
stringVar_( st, sc, pos+1, i - pos - 1 );
}
}
pos = findNonEscapedDollar( str, pos + len );
}
PathIdentPair pip = extractPathIdentFromString(str);
if( !pip.second.isEmpty() && !pip.second.startsWith('\\') ) // avoid Windows paths
{