Skip to content

Commit

Permalink
Initializer lists now somewhat work
Browse files Browse the repository at this point in the history
  • Loading branch information
leventeBajczi committed Oct 18, 2024
1 parent b0ca294 commit e3e4e16
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -432,14 +432,19 @@ public CStatement visitBodyDeclaration(CParser.BodyDeclarationContext ctx) {
createVars(declaration);
if (declaration.getType() instanceof Struct) {
checkState(declaration.getInitExpr() instanceof CInitializerList, "Struct can only be initialized via an initializer list!");
final var initializerList = (CInitializerList) declaration.getInitExpr();
List<VarDecl<?>> varDecls = declaration.getVarDecls();
for (int i = 0; i < varDecls.size(); i++) {
VarDecl<?> varDecl = varDecls.get(i);
Tuple2<Optional<CStatement>, CStatement> initializer = ((CInitializerList) declaration.getInitExpr()).getStatements().get(i);

CAssignment cAssignment = new CAssignment(varDecl.getRef(), initializer.get2(), "=", parseContext);
VarDecl<?> varDecl = varDecls.get(0);
final var ptrType = CComplexType.getUnsignedLong(parseContext);
LitExpr<?> currentValue = ptrType.getNullValue();
LitExpr<?> unitValue = ptrType.getUnitValue();
for (Tuple2<Optional<CStatement>, CStatement> statement : initializerList.getStatements()) {
final var expr = statement.get2().getExpression();
final var deref = Exprs.Dereference(cast(varDecl.getRef(), currentValue.getType()), cast(currentValue, currentValue.getType()), expr.getType());
CAssignment cAssignment = new CAssignment(deref, statement.get2(), "=", parseContext);
recordMetadata(ctx, cAssignment);
compound.getcStatementList().add(cAssignment);
currentValue = Add(currentValue, unitValue).eval(ImmutableValuation.empty());
}
} else {
checkState(declaration.getVarDecls().size() == 1, "non-struct declarations shall only have one variable!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ class FrontendXcfaBuilder(val parseContext: ParseContext, val checkOverflow: Boo
for (flatVariable in flatVariables) {
builder.addVar(flatVariable)
val type = CComplexType.getType(flatVariable.ref, parseContext)
if (type is CArray && builder.getParams().none { it.first == flatVariable }) {
if ((type is CArray || type is CStruct) && builder.getParams().none { it.first == flatVariable }) {
initStmtList.add(StmtLabel(
Stmts.Assign(cast(flatVariable, flatVariable.type),
cast(type.getValue("$ptrCnt"), flatVariable.type))
Expand Down

0 comments on commit e3e4e16

Please sign in to comment.