fusionlanguage / fut

Fusion programming language. Transpiling to C, C++, C#, D, Java, JavaScript, Python, Swift, TypeScript and OpenCL C.
https://fusion-lang.org
GNU General Public License v3.0
1.76k stars 55 forks source link

fut C++ build warnings #96

Closed ghost closed 1 year ago

ghost commented 1 year ago

This will get rid of these warnings:

libfut.cpp: In member function 'std::shared_ptr<FuExpr> FuSema::visitInterpolatedString(std::shared_ptr<FuInterpolatedString>)':
libfut.cpp:4055:45: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<FuInterpolatedPart>::size_type' {aka 'long long unsigned int'} [-Wsign-compare]
 4055 |         for (int partsIndex = 0; partsIndex < expr->parts.size(); partsIndex++) {
      |                                  ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
libfut.cpp: In member function 'std::shared_ptr<FuExpr> FuSema::resolveCallWithArguments(std::shared_ptr<FuCallExpr>, const std::vector<std::shared_ptr<FuExpr> >*)':
libfut.cpp:5282:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::shared_ptr<FuExpr> >::size_type' {aka 'long long unsigned int'} [-Wsign-compare]
 5282 |                 if (i >= arguments->size()) {
      |                     ~~^~~~~~~~~~~~~~~~~~~~
libfut.cpp:5299:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::shared_ptr<FuExpr> >::size_type' {aka 'long long unsigned int'} [-Wsign-compare]
 5299 |         if (i < arguments->size())
      |             ~~^~~~~~~~~~~~~~~~~~~
libfut.cpp:5313:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::shared_ptr<FuExpr> >::size_type' {aka 'long long unsigned int'} [-Wsign-compare]
 5313 |                         if (i < arguments->size())
      |                             ~~^~~~~~~~~~~~~~~~~~~
libfut.cpp: In member function 'std::shared_ptr<FuExpr> FuSema::visitCallExpr(std::shared_ptr<FuCallExpr>)':
libfut.cpp:5342:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::shared_ptr<FuExpr> >::size_type' {aka 'long long unsigned int'} [-Wsign-compare]
 5342 |                 for (int i = 0; i < arguments->size(); i++) {
      |                                 ~~^~~~~~~~~~~~~~~~~~~
libfut.cpp: In member function 'std::shared_ptr<FuExpr> FuSema::visitExpr(std::shared_ptr<FuExpr>)':
libfut.cpp:5400:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::shared_ptr<FuExpr> >::size_type' {aka 'long long unsigned int'} [-Wsign-compare]
 5400 |                 for (int i = 0; i < items->size(); i++)
      |                                 ~~^~~~~~~~~~~~~~~
libfut.cpp: In member function 'void FuSema::visitSwitch(FuSwitch*)':
libfut.cpp:5834:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::shared_ptr<FuExpr> >::size_type' {aka 'long long unsigned int'} [-Wsign-compare]
 5834 |                 for (int i = 0; i < kase.values.size(); i++) {
      |                                 ~~^~~~~~~~~~~~~~~~~~~~
libfut.cpp: In member function 'void GenBase::writeCoercedLiterals(const FuType*, const std::vector<std::shared_ptr<FuExpr> >*)':
libfut.cpp:6834:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::shared_ptr<FuExpr> >::size_type' {aka 'long long unsigned int'} [-Wsign-compare]
 6834 |         for (int i = 0; i < exprs->size(); i++) {
      |                         ~~^~~~~~~~~~~~~~~
libfut.cpp: In member function 'void GenC::writeArgsAndRightParenthesis(const FuMethod*, const std::vector<std::shared_ptr<FuExpr> >*)':
libfut.cpp:10158:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::shared_ptr<FuExpr> >::size_type' {aka 'long long unsigned int'} [-Wsign-compare]
10158 |                 if (i >= args->size())
      |                     ~~^~~~~~~~~~~~~~~
libfut.cpp: In member function 'void GenD::writeInsertedArg(const FuType*, const std::vector<std::shared_ptr<FuExpr> >*, int)':
libfut.cpp:15660:26: warning: comparison of integer expressions of different signedness: 'std::vector<std::shared_ptr<FuExpr> >::size_type' {aka 'long long unsigned int'} and 'int' [-Wsign-compare]
15660 |         if (args->size() <= index) {
      |             ~~~~~~~~~~~~~^~~~~~~~
libfut.cpp: In member function 'void GenJsNoModule::writeInterpolatedLiteral(std::string_view)':
libfut.cpp:17904:48: warning: comparison of integer expressions of different signedness: 'int' and 'std::basic_string_view<char>::size_type' {aka 'long long unsigned int'} [-Wsign-compare]
17904 |                 if (c == '`' || (c == '$' && i < s.length() && s[i] == '{'))
      |                                              ~~^~~~~~~~~~~~
libfut.cpp: In member function 'void GenSwift::initVarsAtIndent()':
libfut.cpp:20706:42: warning: comparison of integer expressions of different signedness: 'std::vector<std::unordered_set<std::basic_string_view<char> > >::size_type' {aka 'long long unsigned int'} and 'int' [-Wsign-compare]
20706 |         while (this->varsAtIndent.size() <= this->indent) {
      |                ~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
libfut.cpp: In member function 'virtual void GenSwift::visitSwitch(const FuSwitch*)':
libfut.cpp:20972:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::shared_ptr<FuExpr> >::size_type' {aka 'long long unsigned int'} [-Wsign-compare]
20972 |                 for (int i = 0; i < kase.values.size(); i++) {
      |                                 ~~^~~~~~~~~~~~~~~~~~~~
pfusik commented 1 year ago

What do you mean by "use size_t" ? Introduce the size_t type in Fusion? Or emit type casts?

ghost commented 1 year ago

What do you mean by "use size_t" ? Introduce the size_t type in Fusion? Or emit type casts?

Use size_t in the generated code instead of int. As you can see, you are currently use int.

pfusik commented 1 year ago

Transpiling all ints to size_t is not an option. size_t is an unsigned type.

ghost commented 1 year ago

Transpiling all ints to size_t is not an option. size_t is an unsigned type.

No. I didn't tell you to replace all instances of int with size_t. This is nonsense. I don't know where you got this idea because I have never said it. But size_t should be used for indexes (please read the warnings I posted on this thread) and something related to size like klass->typeParameterCount and arrayStg->length on #97.

ghost commented 1 year ago

Oh my bad. If the original Fusion code uses int then the transpiler just translated them as is. This is the expected behavior.

ghost commented 1 year ago

@pfusik Which language is Fusion written in? I'm confused. Is it written in itself or in C++?

pfusik commented 1 year ago

Fusion is self-hosted. It transpiles itself to C++, C# and JavaScript.

I'd like to avoid the C++ compilation warnings, but haven't decided yet how.

ghost commented 1 year ago

Fusion is self-hosted. It transpiles itself to C++, C# and JavaScript.

I'd like to avoid the C++ compilation warnings, but haven't decided yet how.

Done 😄

https://github.com/fusionlanguage/fut/pull/99

pfusik commented 1 year ago

I don't want to hide the warnings.

One option is emitting std::ssize, unfortunately that's C++20. Shall we cast to ptrdiff_t instead?

BenBE commented 1 year ago

ssize_t (in stddef.h) has been around for quite some time.

(I know that's not proper, canonical C++ in the purist sense, could use cstddef instead, but not sure re namespacing on those).

Edit: Ah, oops. Misread that. You could just implement ssize in the boilerplate used by Fusion in this case as seen in https://en.cppreference.com/w/cpp/iterator/size -> The implementation is not that difficult.

template <class C>
constexpr auto ssize(const C& c) -> std::common_type_t<std::ptrdiff_t, std::make_signed_t<decltype(c.size())>>
{
    using R = std::common_type_t<std::ptrdiff_t, std::make_signed_t<decltype(c.size())>>;
    return static_cast<R>(c.size());
}
pfusik commented 1 year ago

I chose std::ssize. Compilers have been supporting it for some time and fut requires a C++20 compiler anyway. I know many projects are still not yet on C++20, but that's only a matter of time.

What's left is string length:

libfut.cpp: In member function 'void GenJsNoModule::writeInterpolatedLiteral(std::string_view)':
libfut.cpp:17904:48: warning: comparison of integer expressions of different signedness: 'int' and 
 'std::basic_string_view<char>::size_type' {aka 'long long unsigned int'} [-Wsign-compare]
  17904 |                 if (c == '`' || (c == '$' && i < s.length() && s[i] == '{'))
        |                                              ~~^~~~~~~~~~~~