5656import au .com .integradev .delphi .symbol .scope .DelphiScopeImpl ;
5757import au .com .integradev .delphi .type .factory .TypeFactoryImpl ;
5858import java .util .ArrayList ;
59- import java .util .Arrays ;
6059import java .util .List ;
6160import org .sonar .plugins .communitydelphi .api .symbol .declaration .RoutineNameDeclaration ;
6261import org .sonar .plugins .communitydelphi .api .symbol .declaration .TypeNameDeclaration ;
62+ import org .sonar .plugins .communitydelphi .api .symbol .declaration .VariableNameDeclaration ;
6363import org .sonar .plugins .communitydelphi .api .symbol .scope .DelphiScope ;
6464import org .sonar .plugins .communitydelphi .api .type .IntrinsicType ;
6565import org .sonar .plugins .communitydelphi .api .type .Type ;
6666import org .sonar .plugins .communitydelphi .api .type .TypeFactory ;
6767
6868public final class IntrinsicsInjector {
6969 private final TypeFactory typeFactory ;
70+ private final List <IntrinsicConstant > constants ;
7071 private final List <IntrinsicRoutine .Builder > routines ;
71- private DelphiScopeImpl scope ;
7272
7373 public IntrinsicsInjector (TypeFactory typeFactory ) {
7474 this .typeFactory = typeFactory ;
75+ this .constants = new ArrayList <>();
7576 this .routines = new ArrayList <>();
7677
78+ buildConstants ();
7779 buildRoutines ();
7880 }
7981
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+ }
8598 }
8699
87100 private Type type (IntrinsicType type ) {
@@ -96,6 +109,16 @@ private Type dynamicArraySizeType() {
96109 return ((TypeFactoryImpl ) typeFactory ).dynamicArraySizeType ();
97110 }
98111
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+
99122 private void buildRoutines () {
100123 routine ("Abs" ).param (type (REAL )).returns (type (REAL ));
101124 routine ("Abs" ).param (type (INTEGER )).returns (type (INTEGER ));
@@ -344,17 +367,17 @@ private void buildRoutines() {
344367 routine ("WriteLn" ).variadic (TypeFactory .untypedType ());
345368 }
346369
370+ private void constant (String name , IntrinsicType type ) {
371+ constants .add (new IntrinsicConstant (name , type ));
372+ }
373+
347374 private IntrinsicRoutine .Builder routine (String name ) {
348375 IntrinsicRoutine .Builder builder = IntrinsicRoutine .builder (name );
349376 routines .add (builder );
350377 return builder ;
351378 }
352379
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 ) {
358381 SymbolicNode node = SymbolicNode .imaginary (intrinsic .simpleName (), scope );
359382 Type type = typeFactory .getIntrinsic (intrinsic );
360383 TypeNameDeclaration declaration =
@@ -363,11 +386,7 @@ private void injectType(IntrinsicType intrinsic) {
363386 scope .addDeclaration (declaration );
364387 }
365388
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 ) {
371390 IntrinsicRoutine routine = builder .build ();
372391 SymbolicNode node = SymbolicNode .imaginary (routine .simpleName (), scope );
373392 RoutineNameDeclaration declaration =
@@ -376,18 +395,11 @@ private void injectRoutine(IntrinsicRoutine.Builder builder) {
376395 scope .addDeclaration (declaration );
377396 }
378397
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 );
388402
389- private void injectConstant (String image , IntrinsicType intrinsic ) {
390- var declaration = VariableNameDeclarationImpl .constant (image , type (intrinsic ), scope );
391403 scope .addDeclaration (declaration );
392404 }
393405}
0 commit comments