Implement support for constant expressions
@cirras ci sta già lavorando.
Dal 28/5/2025.
Valutazione
Questa issue non è ancora stata valutata.
Descrizione
Prerequisites
- This improvement has not already been suggested.
- This improvement would be generally useful, not specific to my code or setup.
Engine area
Delphi language support
Improvement description
Background
The Delphi compiler implements the following optimizations around constant expressions:
- constant folding
- constant propagation
This has significant implications for type resolution - see example.
Example
// overload for each integer type:
procedure X(B: Byte); overload; begin WriteLn('Byte'); end;
procedure X(S: ShortInt); overload; begin WriteLn('ShortInt'); end;
procedure X(S: SmallInt); overload; begin WriteLn('SmallInt'); end;
procedure X(W: Word); overload; begin WriteLn('Word'); end;
procedure X(I: Integer); overload; begin WriteLn('Integer'); end;
procedure X(C: Cardinal); overload; begin WriteLn('Cardinal'); end;
procedure X(I: Int64); overload; begin WriteLn('Int64'); end;
procedure X(U: UInt64); overload; begin WriteLn('UInt64'); end;
const
CFive = 5; // technically ShortInt, but the value "5" is propagated to constant expressions
CHundred = 25 * 4; // technically ShortInt, but the value "100" is propagated to constant expressions
procedure Test;
var
I8: ShortInt;
I16: SmallInt;
I32: Integer;
I64: Int64;
U8: Byte;
U16: Word;
U32: Cardinal;
U64: UInt64;
begin
// For variables, all we need to do to lose the original type is tack on a unary `+`.
// This is because the expression is evaluated at runtime, and only the following options are available at runtime:
//
// Positive(Integer): Integer
// Positive(Cardinal): Cardinal
// Positive(Int64): Int64
// Positive(UInt64): UInt64
X(+I8); // Integer
X(+I16); // Integer
X(+I32); // Integer
X(+I64); // Int64
X(+U8); // Integer
X(+U16); // Integer
X(+U32); // Cardinal
X(+U64); // UInt64
// But for constant expressions, that unary `+` (and indeed, all other arithmetic operators) are done at compile-time.
// The type is resolved *after* the expression is evaluated, based on the value.
X(+(0)); // ShortInt
X(+(128)); // Byte
X(+(256)); // SmallInt
X(+(32768)); // Word
X(+(65536)); // Integer
X(+(2147483648)); // Cardinal
X(+(4294967296)); // Int64
X(+(9223372036854775808)); // UInt64
// The following constant expressions feature constant propagation.
X(+CFive); // ShortInt
X(+CHundred); // ShortInt
X(+(CHundred + CHundred)); // Byte
X(+(256 - CFive)); // Byte
end;
See Also
Rationale
This is required for fully accurate type resolution.
Known affected areas
- Type conversions from array constructor -> set
- Expression type resolution (especially unary
-) - Subrange type modeling (low and high are constant expressions)
Other benefits
- Having access to evaluated constant expressions would be extremely useful for some rules.
- Really, any rule where we might want to raise an issue based on the contents of a string
- see: https://github.com/integrated-application-development/sonar-delphi/issues/94#issuecomment-1825031266
- Lingua principale
- Java
- Stelle
- 160
- Fork
- 36
- Merge medio
- 22g 7h
- PR unite (30g)
- 2
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Nessun modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di integrated-application-development/sonar-delphi
-
bug triage
Difficoltà 3/5 1-2 giorni Idoneità per principianti 66/100
integrated-application-development/sonar-delphi#473 · 3 commenti ·
-
VariableInitialization false positive for custom managed record with Initialize operatorForse già presa @Bazooper-blip l’ha presa 36 giorni fa. Apertaenhancement rule
Difficoltà 3/5 1-2 giorni Idoneità per principianti 65/100
integrated-application-development/sonar-delphi#468 · 1 commento ·
-
engine enhancement
Difficoltà 3/5 1-2 giorni Idoneità per principianti 68/100
-
feature rule
Difficoltà 5/5 Più di una settimana Idoneità per principianti 35/100
-
feature rule
Difficoltà 5/5 Più di una settimana Idoneità per principianti 35/100
integrated-application-development/sonar-delphi#423 · 1 commento ·
Tutte le issue di integrated-application-development/sonar-delphi
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
aws/context-ontology-accelerator#325 ·
I maintainer di solito rispondono entro 1 giorno
-
The README tells to run `mvn jeo:disassemble`, which fails with `No plugin found for prefix 'jeo'`Aperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 82/100
objectionary/jeo-maven-plugin#1938 ·
I maintainer di solito rispondono entro 2 giorni
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
oVirt/ovirt-engine#1219 ·
I maintainer di solito rispondono entro 1 giorno
-
pending:code-area pending:issue-category to-triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
I maintainer di solito rispondono entro 1 giorno
-
area:core bug priority:p2
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
aomarai/minecraft-mcp#247 ·
I maintainer di solito rispondono entro 1 giorno