c3lang / c3c

Compiler for the C3 language
https://c3-lang.org
GNU Lesser General Public License v3.0
2.88k stars 176 forks source link

C headers created by c3c is not compatible for C compilers. #1113

Open kcvinker opened 9 months ago

kcvinker commented 9 months ago

When creating dlls with c3c, it is a nice feature to create C headers automatically. But this header file is not working in c compilers like MSVC. Please improve this feature so that we use this header files straight in MSVC. Now type names are showing with module name like abc.type.

lerno commented 8 months ago

Can you check if this looks better with 0.5.4?

kcvinker commented 8 months ago

@lerno , Sure.

Edit: What happened ? I couldn't find the download files for 0.5.4.

kcvinker commented 8 months ago

@lerno, This is the error I see.

1: module std::math::random;
2:
3: interface Random
             ^^^^^^
(C:/Compilers/C3/lib/std/math/math_random.c3:3:11) Error: 'Random' would shadow a previous declaration.

1: module std::math::random;
2:
3: interface Random
             ^^^^^^
(C:/Compilers/C3/lib/std/math/math.random.c3:3:11) Note: The previous use of 'Random' was here.
lerno commented 8 months ago

What did you do to get that error?

kcvinker commented 8 months ago

@lerno, The one and only use for math library in my code is this.

import std::math;
macro clip(x) {
    return math::clamp(x, 0, 255);
}
lerno commented 8 months ago

If you just compile a file, do you get the same error?

lerno commented 8 months ago

Because somehow you've managed to insert the same symbol twice, and I just can't see how that could happen. But apparently it does, so I'm trying to find out how...

kcvinker commented 8 months ago

If you just compile a file, do you get the same error?

Let me try that.

kcvinker commented 8 months ago

@lerno I just tested with sample dll code. This time, I didn't used any math lib code. See this is my source code.

module dlltest;
fn void sample(int x) @export("MySampleFunc")
{
    int b = x + 2;
}

fn int testFunc(int y) @export("My_test_func")
{
    return y + 8;
}

And this is the start position of the error message.

 c3c build dlltest
1: module std::math::random;
2:
3: interface Random
             ^^^^^^
(C:/Compilers/C3/lib/std/math/math_random.c3:3:11) Error: 'Random' would shadow a previous declaration.

1: module std::math::random;
2:
3: interface Random
             ^^^^^^
(C:/Compilers/C3/lib/std/math/math.random.c3:3:11) Note: The previous use of 'Random' was here.
lerno commented 8 months ago

Can you show me the project file?

kcvinker commented 8 months ago

@lerno Here is my project.json

{
  // language version of C3
  "langrev": "1",
  // warnings used for all targets
  "warnings": [ "no-unused" ],
  // directories where C3 library files may be found
  "dependency-search-paths": [],
  // libraries to use for all targets
  "dependencies": [ ],

  "linker-search-paths": [],

  "linked-libraries": [],
  // authors, optionally with email
  "authors": [ "kcvinker" ],
  // Version using semantic versioning
  "version": "0.1.0",
  // sources compiled for all targets
  "sources": [ "./**"],
  // Targets
  "targets": {
    "dlltest": {
      // executable or library
      "type": "dynamic-lib"
      // additional libraries, sources
      // and overrides of global settings here
    },
  },

  // Debug information, may be 'none', 'full' and 'line-tables'
  "debug-info": "full",
  // Architecture and OS target:
  "target": "windows-x64",
  // The size of the symtab, which limits the amount
  // of symbols that can be used. Should usually not
  // be changed.
  //"symtab": 4194304,
  // "none", "pic", "PIC", "pie", "PIE"
  "reloc": "none",
  // Trap on signed and unsigned integer wrapping
  // for testing
  "trap-on-wrap": false,
  // Use / don't use soft float, value is otherwise target default
  "soft-float": false,
  // Vector settings on x86: none/native/mmx/sse/avx/avx512
  "x86vec": "sse",
  // CPU name, used for optimizations in the LLVM backend
  "cpu": "generic",
  // Output location, relative to project file
  "output": "./build",
  // C compiler if the project also compiles c sources
  // defaults to 'cc'
  "cc": "cc",
  // c sources if the project also compiles c sources
  // relative to the project file
  "c-sources": [
    "csource/**"
  ]
}
lerno commented 8 months ago

Hmm.. that one looks normal. What if you just compile a single file with c3c.exe compile foo.c3?

kcvinker commented 8 months ago

Let me check that.

kcvinker commented 8 months ago

@lerno Same result. This is source.

import std::io;
fn void main()
{
    io::println("testing version 0.5.4");
}

And this is result

λ c3c compile foo.c3
1: module std::math::random;
2:
3: interface Random
             ^^^^^^
(C:/Compilers/C3/lib/std/math/math_random.c3:3:11) Error: 'Random' would shadow a previous declaration.

1: module std::math::random;
2:
3: interface Random
             ^^^^^^
(C:/Compilers/C3/lib/std/math/math.random.c3:3:11) Note: The previous use of 'Random' was here.

11:     fn void next_bytes(char[] buffer);
12: }
13:
14: macro bool is_random(random) => $assignable(random, Random*);
               ^^^^^^^^^
(C:/Compilers/C3/lib/std/math/math_random.c3:14:12) Error: 'is_random' would shadow a previous declaration.
kcvinker commented 8 months ago

@lerno I think this issue is not caused by c3. I think I can do something. Let me try.

kcvinker commented 8 months ago

@lerno, Yeah, it was a mistake from my side and I solved it. What caused the issue ---> I am using a python script to update the compilers I use in my projects. This script will download the latest binaries from github and extract them. Then it will remove the old files and paste the new files in respective directories. Last day, when you told me to check the version 0.5.4, I ran this script and at that time newest binaries where not uploaded to the repo's asset section. So my script ran and showed me an error message that there is no new files in the repo. After 2 hours, you uploaded the files and I ran this script again. This time everything worked smoothly and I got the version 0.5.4 installed. But something strange were happened when last time the script ran. I don't know what was it. But now, I copy pasted the new binaries with my hand, everything works smoothly.

lerno commented 8 months ago

I wonder how it can be reproduced. Because that should never really be possible, incorrect libraries or not....

kcvinker commented 8 months ago

I think we can reproduce. Steps

  1. run updater script when same version is available on github.
  2. run updater again when the new version available on github later.
  3. run c3 file now. It might bring the same error report.
lerno commented 8 months ago

Ok, I know why now. Look at the file names:

C:/Compilers/C3/lib/std/math/math_random.c3:3:11) Error:
C:/Compilers/C3/lib/std/math/math.random.c3:3:11) Note 

Do you see the subtle difference? math_random.c3 and math.random.c3. Both ended up in lib/std/math because your just copied updated files in your script, you didn't delete the old stdlib files. So when names change, this happens.

kcvinker commented 8 months ago

That's a clever observation. Your attention to detail and problem-solving skills are really impressive.

lerno commented 8 months ago

Just ordinary debugging.

goyalyashpal commented 8 months ago

can this issue be closed then?

kcvinker commented 8 months ago

@goyalyashpal, Actually, the header file generation is still the same. It is not fixed yet.

lerno commented 8 months ago

@kcvinker this is odd, can you show me some sample of the output. Because this was changed.

kcvinker commented 8 months ago

@lerno This is my c3c version

C3 Compiler Version (alpha):       0.5.4
Installed directory:               C:/Compilers/C3/
LLVM version:                      16.0.2
LLVM default target:               x86_64-pc-windows-msvc

And this is the types generated by c3c in the header file.

typedef struct pyc3.Button__ pyc3.Button;
typedef struct pyc3.Control__ pyc3.Control;
typedef enum pyc3.ControlType__ pyc3.ControlType;
enum pyc3.ControlType__
{
     pyc3.ControlType_NONE,
     pyc3.ControlType_BUTTON,
     pyc3.ControlType_CALENDAR,
     pyc3.ControlType_CHECK_BOX,
     pyc3.ControlType_COMBO_BOX,
     pyc3.ControlType_DATE_TIME_PICKER,
     pyc3.ControlType_GROUP_BOX,
     pyc3.ControlType_LABEL,
     pyc3.ControlType_LIST_BOX,
     pyc3.ControlType_LIST_VIEW,
     pyc3.ControlType_NUM_PICKER,
     pyc3.ControlType_PROGRESS_BAR,
     pyc3.ControlType_RADIO_BUTTON,
     pyc3.ControlType_TEXT_BOX,
     pyc3.ControlType_TRACK_BAR,
     pyc3.ControlType_TREE_VIEW,
};
cbuttner commented 8 months ago

@kcvinker You can use @export on your types to name them correctly.

kcvinker commented 8 months ago

@cbuttner Let me try that. Thanks.