Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

slice2java: fix constructor parameter count check #1540

Merged
merged 7 commits into from
Oct 9, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
slic2java: fix constructor parameter count
Closes #1539
externl committed Oct 9, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit cbdda95909e02d7e1c453f4cd9d3074da00467dd
28 changes: 25 additions & 3 deletions cpp/src/slice2java/Gen.cpp
Original file line number Diff line number Diff line change
@@ -136,6 +136,28 @@ string ofFactory(const TypePtr& type)
return "java.util.Optional.ofNullable";
}

// Returns the "length" of the a constructor parameter list from the given data member list.
// All types are counted as 1, except for long and double which are counted as 2.
// See https://docs.oracle.com/javase/specs/jvms/se20/html/jvms-4.html#jvms-4.3.3
int
constructorParameterLength(const DataMemberList& members)
{
int length = 0;
for(DataMemberList::const_iterator i = members.begin(); i != members.end(); ++i)
{
BuiltinPtr builtin = BuiltinPtr::dynamicCast((*i)->type());
if(builtin && (builtin->kind() == Builtin::KindLong || builtin->kind() == Builtin::KindDouble))
{
length += 2;
}
else
{
length++;
}
}
return length;
}

}

Slice::JavaVisitor::JavaVisitor(const string& dir) :
@@ -2554,7 +2576,7 @@ Slice::Gen::TypesVisitor::visitClassDefStart(const ClassDefPtr& p)
//
// A method cannot have more than 255 parameters (including the implicit "this" argument).
//
if(allDataMembers.size() < 255)
if(constructorParameterLength(allDataMembers) < 255)
{
DataMemberList baseDataMembers;
if(baseClass)
@@ -2947,7 +2969,7 @@ Slice::Gen::TypesVisitor::visitExceptionStart(const ExceptionPtr& p)
//
// A method cannot have more than 255 parameters (including the implicit "this" argument).
//
if(allDataMembers.size() < 255)
if(constructorParameterLength(allDataMembers) < 255)
{
if(hasRequiredMembers && hasOptionalMembers)
{
@@ -3384,7 +3406,7 @@ Slice::Gen::TypesVisitor::visitStructEnd(const StructPtr& p)
//
// A method cannot have more than 255 parameters (including the implicit "this" argument).
//
if(members.size() < 255)
if(constructorParameterLength(members) < 255)
{
vector<string> paramDecl;
vector<string> paramNames;
28 changes: 25 additions & 3 deletions cpp/src/slice2java/GenCompat.cpp
Original file line number Diff line number Diff line change
@@ -210,6 +210,28 @@ writeParamList(Output& out, vector<string> params, bool end = true, bool newLine
}
}

// Returns the "length" of the a constructor parameter list from the given data member list.
// All types are counted as 1, except for long and double which are counted as 2.
// See https://docs.oracle.com/javase/specs/jvms/se20/html/jvms-4.html#jvms-4.3.3
int
constructorParameterLength(const DataMemberList& members)
{
int length = 0;
for(DataMemberList::const_iterator i = members.begin(); i != members.end(); ++i)
{
BuiltinPtr builtin = BuiltinPtr::dynamicCast((*i)->type());
if(builtin && (builtin->kind() == Builtin::KindLong || builtin->kind() == Builtin::KindDouble))
{
length += 2;
}
else
{
length++;
}
}
return length;
}

}

Slice::JavaCompatVisitor::JavaCompatVisitor(const string& dir) :
@@ -2947,7 +2969,7 @@ Slice::GenCompat::TypesVisitor::visitClassDefStart(const ClassDefPtr& p)
//
// A method cannot have more than 255 parameters (including the implicit "this" argument).
//
if(allDataMembers.size() < 255)
if(constructorParameterLength(allDataMembers) < 255)
{
DataMemberList baseDataMembers;
if(baseClass)
@@ -3257,7 +3279,7 @@ Slice::GenCompat::TypesVisitor::visitExceptionStart(const ExceptionPtr& p)
//
// A method cannot have more than 255 parameters (including the implicit "this" argument).
//
if(allDataMembers.size() < 255)
if(constructorParameterLength(allDataMembers) < 255)
{
if(hasRequiredMembers && hasOptionalMembers)
{
@@ -3672,7 +3694,7 @@ Slice::GenCompat::TypesVisitor::visitStructEnd(const StructPtr& p)
//
// A method cannot have more than 255 parameters (including the implicit "this" argument).
//
if(members.size() < 255)
if(constructorParameterLength(members) < 255)
{
vector<string> paramDecl;
vector<string> paramNames;