56
56
import au .com .integradev .delphi .symbol .scope .DelphiScopeImpl ;
57
57
import au .com .integradev .delphi .type .factory .TypeFactoryImpl ;
58
58
import java .util .ArrayList ;
59
- import java .util .Arrays ;
60
59
import java .util .List ;
61
60
import org .sonar .plugins .communitydelphi .api .symbol .declaration .RoutineNameDeclaration ;
62
61
import org .sonar .plugins .communitydelphi .api .symbol .declaration .TypeNameDeclaration ;
62
+ import org .sonar .plugins .communitydelphi .api .symbol .declaration .VariableNameDeclaration ;
63
63
import org .sonar .plugins .communitydelphi .api .symbol .scope .DelphiScope ;
64
64
import org .sonar .plugins .communitydelphi .api .type .IntrinsicType ;
65
65
import org .sonar .plugins .communitydelphi .api .type .Type ;
66
66
import org .sonar .plugins .communitydelphi .api .type .TypeFactory ;
67
67
68
68
public final class IntrinsicsInjector {
69
69
private final TypeFactory typeFactory ;
70
+ private final List <IntrinsicConstant > constants ;
70
71
private final List <IntrinsicRoutine .Builder > routines ;
71
- private DelphiScopeImpl scope ;
72
72
73
73
public IntrinsicsInjector (TypeFactory typeFactory ) {
74
74
this .typeFactory = typeFactory ;
75
+ this .constants = new ArrayList <>();
75
76
this .routines = new ArrayList <>();
76
77
78
+ buildConstants ();
77
79
buildRoutines ();
78
80
}
79
81
80
- public void inject (DelphiScope scope ) {
81
- this .scope = (DelphiScopeImpl ) scope ;
82
- injectTypes ();
83
- injectRoutines ();
84
- injectConstants ();
82
+ public void injectTypes (DelphiScope scope ) {
83
+ for (IntrinsicType type : IntrinsicType .values ()) {
84
+ injectType (type , (DelphiScopeImpl ) scope );
85
+ }
86
+ }
87
+
88
+ public void injectConstants (DelphiScope scope ) {
89
+ for (IntrinsicConstant constant : constants ) {
90
+ injectConstant (constant , (DelphiScopeImpl ) scope );
91
+ }
92
+ }
93
+
94
+ public void injectRoutines (DelphiScope scope ) {
95
+ for (IntrinsicRoutine .Builder routine : routines ) {
96
+ injectRoutine (routine , (DelphiScopeImpl ) scope );
97
+ }
85
98
}
86
99
87
100
private Type type (IntrinsicType type ) {
@@ -96,6 +109,16 @@ private Type dynamicArraySizeType() {
96
109
return ((TypeFactoryImpl ) typeFactory ).dynamicArraySizeType ();
97
110
}
98
111
112
+ private void buildConstants () {
113
+ constant ("CompilerVersion" , EXTENDED );
114
+ constant ("MaxInt" , INTEGER );
115
+ constant ("MaxLongInt" , LONGINT );
116
+ constant ("True" , BOOLEAN );
117
+ constant ("False" , BOOLEAN );
118
+ constant ("ReturnAddress" , POINTER );
119
+ constant ("AddressOfReturnAddress" , POINTER );
120
+ }
121
+
99
122
private void buildRoutines () {
100
123
routine ("Abs" ).param (type (REAL )).returns (type (REAL ));
101
124
routine ("Abs" ).param (type (INTEGER )).returns (type (INTEGER ));
@@ -344,17 +367,17 @@ private void buildRoutines() {
344
367
routine ("WriteLn" ).variadic (TypeFactory .untypedType ());
345
368
}
346
369
370
+ private void constant (String name , IntrinsicType type ) {
371
+ constants .add (new IntrinsicConstant (name , type ));
372
+ }
373
+
347
374
private IntrinsicRoutine .Builder routine (String name ) {
348
375
IntrinsicRoutine .Builder builder = IntrinsicRoutine .builder (name );
349
376
routines .add (builder );
350
377
return builder ;
351
378
}
352
379
353
- private void injectTypes () {
354
- Arrays .stream (IntrinsicType .values ()).forEach (this ::injectType );
355
- }
356
-
357
- private void injectType (IntrinsicType intrinsic ) {
380
+ private void injectType (IntrinsicType intrinsic , DelphiScopeImpl scope ) {
358
381
SymbolicNode node = SymbolicNode .imaginary (intrinsic .simpleName (), scope );
359
382
Type type = typeFactory .getIntrinsic (intrinsic );
360
383
TypeNameDeclaration declaration =
@@ -363,11 +386,7 @@ private void injectType(IntrinsicType intrinsic) {
363
386
scope .addDeclaration (declaration );
364
387
}
365
388
366
- private void injectRoutines () {
367
- routines .forEach (this ::injectRoutine );
368
- }
369
-
370
- private void injectRoutine (IntrinsicRoutine .Builder builder ) {
389
+ private void injectRoutine (IntrinsicRoutine .Builder builder , DelphiScopeImpl scope ) {
371
390
IntrinsicRoutine routine = builder .build ();
372
391
SymbolicNode node = SymbolicNode .imaginary (routine .simpleName (), scope );
373
392
RoutineNameDeclaration declaration =
@@ -376,18 +395,11 @@ private void injectRoutine(IntrinsicRoutine.Builder builder) {
376
395
scope .addDeclaration (declaration );
377
396
}
378
397
379
- private void injectConstants () {
380
- injectConstant ("CompilerVersion" , EXTENDED );
381
- injectConstant ("MaxInt" , INTEGER );
382
- injectConstant ("MaxLongInt" , LONGINT );
383
- injectConstant ("True" , BOOLEAN );
384
- injectConstant ("False" , BOOLEAN );
385
- injectConstant ("ReturnAddress" , POINTER );
386
- injectConstant ("AddressOfReturnAddress" , POINTER );
387
- }
398
+ private void injectConstant (IntrinsicConstant constant , DelphiScopeImpl scope ) {
399
+ String name = constant .getName ();
400
+ Type type = type (constant .getType ());
401
+ VariableNameDeclaration declaration = VariableNameDeclarationImpl .constant (name , type , scope );
388
402
389
- private void injectConstant (String image , IntrinsicType intrinsic ) {
390
- var declaration = VariableNameDeclarationImpl .constant (image , type (intrinsic ), scope );
391
403
scope .addDeclaration (declaration );
392
404
}
393
405
}
0 commit comments