Skip to content

Commit

Permalink
[c] Include <stddef.h> in more scenarios.
Browse files Browse the repository at this point in the history
  • Loading branch information
pfusik committed Aug 25, 2024
1 parent 5e07120 commit b973a3a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
8 changes: 6 additions & 2 deletions GenC.fu
Original file line number Diff line number Diff line change
Expand Up @@ -1314,6 +1314,7 @@ public class GenC : GenCCpp
FuType type = symbol.Type;
int nesting = 0;
while (type is FuArrayStorageType array) {
IncludeStdDef();
Write("for (ptrdiff_t _i");
VisitLiteralLong(nesting);
Write(" = ");
Expand Down Expand Up @@ -1427,7 +1428,7 @@ public class GenC : GenCCpp
FuType type = def.Type;
int nesting = 0;
while (type is FuArrayStorageType array) {
OpenLoop("ptrdiff_t", nesting++, array.Length);
OpenLoop("size_t", nesting++, array.Length);
type = array.GetElementType();
}
if (type is FuStorageType lok && lok.Class.Id == FuId.LockClass) {
Expand Down Expand Up @@ -1631,6 +1632,7 @@ public class GenC : GenCCpp

protected override void WriteStringLength!(FuExpr expr)
{
IncludeStdDef();
Include("string.h");
WriteCall("(ptrdiff_t) strlen", expr);
}
Expand All @@ -1655,7 +1657,7 @@ public class GenC : GenCCpp

protected void WriteArrayFill!(FuExpr obj, List<FuExpr#> args)
{
Write("for (ptrdiff_t _i = 0; _i < ");
Write("for (size_t _i = 0; _i < ");
if (args.Count == 1)
WriteArrayStorageLength(obj);
else
Expand Down Expand Up @@ -2033,10 +2035,12 @@ public class GenC : GenCCpp
break;
case FuId.StringIndexOf:
this.StringIndexOf = true;
IncludeStdDef();
WriteStringMethod("IndexOf", obj, args);
break;
case FuId.StringLastIndexOf:
this.StringLastIndexOf = true;
IncludeStdDef();
WriteStringMethod("LastIndexOf", obj, args);
break;
case FuId.StringReplace:
Expand Down
8 changes: 6 additions & 2 deletions libfut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10738,6 +10738,7 @@ void GenC::writeDestruct(const FuSymbol * symbol)
const FuType * type = symbol->type.get();
int nesting = 0;
while (const FuArrayStorageType *array = dynamic_cast<const FuArrayStorageType *>(type)) {
includeStdDef();
write("for (ptrdiff_t _i");
visitLiteralLong(nesting);
write(" = ");
Expand Down Expand Up @@ -10851,7 +10852,7 @@ void GenC::writeInitCode(const FuNamedValue * def)
const FuType * type = def->type.get();
int nesting = 0;
while (const FuArrayStorageType *array = dynamic_cast<const FuArrayStorageType *>(type)) {
openLoop("ptrdiff_t", nesting++, array->length);
openLoop("size_t", nesting++, array->length);
type = array->getElementType().get();
}
const FuStorageType * lok;
Expand Down Expand Up @@ -11061,6 +11062,7 @@ void GenC::writeEqual(const FuExpr * left, const FuExpr * right, FuPriority pare

void GenC::writeStringLength(const FuExpr * expr)
{
includeStdDef();
include("string.h");
writeCall("(ptrdiff_t) strlen", expr);
}
Expand All @@ -11085,7 +11087,7 @@ void GenC::writeSizeofCompare(const FuType * elementType)

void GenC::writeArrayFill(const FuExpr * obj, const std::vector<std::shared_ptr<FuExpr>> * args)
{
write("for (ptrdiff_t _i = 0; _i < ");
write("for (size_t _i = 0; _i < ");
if (std::ssize(*args) == 1)
writeArrayStorageLength(obj);
else
Expand Down Expand Up @@ -11462,10 +11464,12 @@ void GenC::writeCallExpr(const FuExpr * obj, const FuMethod * method, const std:
break;
case FuId::stringIndexOf:
this->stringIndexOf = true;
includeStdDef();
writeStringMethod("IndexOf", obj, args);
break;
case FuId::stringLastIndexOf:
this->stringLastIndexOf = true;
includeStdDef();
writeStringMethod("LastIndexOf", obj, args);
break;
case FuId::stringReplace:
Expand Down
8 changes: 6 additions & 2 deletions libfut.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11001,6 +11001,7 @@ void WriteDestruct(FuSymbol symbol)
FuType type = symbol.Type;
int nesting = 0;
while (type is FuArrayStorageType array) {
IncludeStdDef();
Write("for (ptrdiff_t _i");
VisitLiteralLong(nesting);
Write(" = ");
Expand Down Expand Up @@ -11114,7 +11115,7 @@ protected override void WriteInitCode(FuNamedValue def)
FuType type = def.Type;
int nesting = 0;
while (type is FuArrayStorageType array) {
OpenLoop("ptrdiff_t", nesting++, array.Length);
OpenLoop("size_t", nesting++, array.Length);
type = array.GetElementType();
}
if (type is FuStorageType lok && lok.Class.Id == FuId.LockClass) {
Expand Down Expand Up @@ -11318,6 +11319,7 @@ protected override void WriteEqual(FuExpr left, FuExpr right, FuPriority parent,

protected override void WriteStringLength(FuExpr expr)
{
IncludeStdDef();
Include("string.h");
WriteCall("(ptrdiff_t) strlen", expr);
}
Expand All @@ -11342,7 +11344,7 @@ void WriteSizeofCompare(FuType elementType)

protected void WriteArrayFill(FuExpr obj, List<FuExpr> args)
{
Write("for (ptrdiff_t _i = 0; _i < ");
Write("for (size_t _i = 0; _i < ");
if (args.Count == 1)
WriteArrayStorageLength(obj);
else
Expand Down Expand Up @@ -11719,10 +11721,12 @@ protected override void WriteCallExpr(FuExpr obj, FuMethod method, List<FuExpr>
break;
case FuId.StringIndexOf:
this.StringIndexOf = true;
IncludeStdDef();
WriteStringMethod("IndexOf", obj, args);
break;
case FuId.StringLastIndexOf:
this.StringLastIndexOf = true;
IncludeStdDef();
WriteStringMethod("LastIndexOf", obj, args);
break;
case FuId.StringReplace:
Expand Down
8 changes: 6 additions & 2 deletions libfut.js
Original file line number Diff line number Diff line change
Expand Up @@ -11428,6 +11428,7 @@ export class GenC extends GenCCpp
let nesting = 0;
let array;
while ((array = type) instanceof FuArrayStorageType) {
this.includeStdDef();
this.write("for (ptrdiff_t _i");
this.visitLiteralLong(BigInt(nesting));
this.write(" = ");
Expand Down Expand Up @@ -11544,7 +11545,7 @@ export class GenC extends GenCCpp
let nesting = 0;
let array;
while ((array = type) instanceof FuArrayStorageType) {
this.openLoop("ptrdiff_t", nesting++, array.length);
this.openLoop("size_t", nesting++, array.length);
type = array.getElementType();
}
let lok;
Expand Down Expand Up @@ -11756,6 +11757,7 @@ export class GenC extends GenCCpp

writeStringLength(expr)
{
this.includeStdDef();
this.include("string.h");
this.writeCall("(ptrdiff_t) strlen", expr);
}
Expand All @@ -11780,7 +11782,7 @@ export class GenC extends GenCCpp

writeArrayFill(obj, args)
{
this.write("for (ptrdiff_t _i = 0; _i < ");
this.write("for (size_t _i = 0; _i < ");
if (args.length == 1)
this.writeArrayStorageLength(obj);
else
Expand Down Expand Up @@ -12165,10 +12167,12 @@ export class GenC extends GenCCpp
break;
case FuId.STRING_INDEX_OF:
this.#stringIndexOf = true;
this.includeStdDef();
this.#writeStringMethod("IndexOf", obj, args);
break;
case FuId.STRING_LAST_INDEX_OF:
this.#stringLastIndexOf = true;
this.includeStdDef();
this.#writeStringMethod("LastIndexOf", obj, args);
break;
case FuId.STRING_REPLACE:
Expand Down

0 comments on commit b973a3a

Please sign in to comment.