Browse Source

`@(default_calling_convention = ...)` for `foreign` blocks

gingerBill 7 years ago
parent
commit
3e05be8eb8
7 changed files with 298 additions and 174 deletions
  1. 155 149
      core/sys/windows.odin
  2. 11 4
      src/check_decl.cpp
  3. 3 0
      src/check_stmt.cpp
  4. 13 4
      src/check_type.cpp
  5. 82 0
      src/checker.cpp
  6. 1 0
      src/common.cpp
  7. 33 17
      src/parser.cpp

+ 155 - 149
core/sys/windows.odin

@@ -431,222 +431,228 @@ GET_FILEEX_INFO_LEVELS :: i32;
 GetFileExInfoStandard: GET_FILEEX_INFO_LEVELS : 0;
 GetFileExInfoStandard: GET_FILEEX_INFO_LEVELS : 0;
 GetFileExMaxInfoLevel: GET_FILEEX_INFO_LEVELS : 1;
 GetFileExMaxInfoLevel: GET_FILEEX_INFO_LEVELS : 1;
 
 
-
+@(default_calling_convention = "std")
 foreign kernel32 {
 foreign kernel32 {
-	@(link_name="GetLastError")              get_last_error              :: proc "std" () -> i32 ---;
-	@(link_name="ExitProcess")               exit_process                :: proc "std" (exit_code: u32) ---;
-	@(link_name="GetModuleHandleA")          get_module_handle_a         :: proc "std" (module_name: ^u8) -> Hinstance ---;
-	@(link_name="GetModuleHandleW")          get_module_handle_w         :: proc "std" (module_name: ^u16) -> Hinstance ---;
-	@(link_name="Sleep")                     sleep                       :: proc "std" (ms: i32) -> i32 ---;
-	@(link_name="QueryPerformanceFrequency") query_performance_frequency :: proc "std" (result: ^i64) -> i32 ---;
-	@(link_name="QueryPerformanceCounter")   query_performance_counter   :: proc "std" (result: ^i64) -> i32 ---;
-	@(link_name="OutputDebugStringA")        output_debug_string_a       :: proc "std" (c_str: ^u8) ---;
-
-	@(link_name="GetCommandLineA")    get_command_line_a    :: proc "std" () -> ^u8 ---;
-	@(link_name="GetCommandLineW")    get_command_line_w    :: proc "std" () -> ^u16 ---;
-	@(link_name="GetSystemMetrics")   get_system_metrics    :: proc "std" (index: i32) -> i32 ---;
-	@(link_name="GetCurrentThreadId") get_current_thread_id :: proc "std" () -> u32 ---;
-
-	@(link_name="GetSystemTimeAsFileTime") get_system_time_as_file_time :: proc "std" (system_time_as_file_time: ^Filetime) ---;
-	@(link_name="FileTimeToLocalFileTime") file_time_to_local_file_time :: proc "std" (file_time: ^Filetime, local_file_time: ^Filetime) -> Bool ---;
-	@(link_name="FileTimeToSystemTime")    file_time_to_system_time     :: proc "std" (file_time: ^Filetime, system_time: ^Systemtime) -> Bool ---;
-	@(link_name="SystemTimeToFileTime")    system_time_to_file_time     :: proc "std" (system_time: ^Systemtime, file_time: ^Filetime) -> Bool ---;
-
-	@(link_name="CloseHandle")  close_handle   :: proc "std" (h: Handle) -> i32 ---;
-	@(link_name="GetStdHandle") get_std_handle :: proc "std" (h: i32) -> Handle ---;
+	@(link_name="GetLastError")              get_last_error              :: proc() -> i32 ---;
+	@(link_name="ExitProcess")               exit_process                :: proc(exit_code: u32) ---;
+	@(link_name="GetModuleHandleA")          get_module_handle_a         :: proc(module_name: ^u8) -> Hinstance ---;
+	@(link_name="GetModuleHandleW")          get_module_handle_w         :: proc(module_name: ^u16) -> Hinstance ---;
+	@(link_name="Sleep")                     sleep                       :: proc(ms: i32) -> i32 ---;
+	@(link_name="QueryPerformanceFrequency") query_performance_frequency :: proc(result: ^i64) -> i32 ---;
+	@(link_name="QueryPerformanceCounter")   query_performance_counter   :: proc(result: ^i64) -> i32 ---;
+	@(link_name="OutputDebugStringA")        output_debug_string_a       :: proc(c_str: ^u8) ---;
+
+	@(link_name="GetCommandLineA")    get_command_line_a    :: proc() -> ^u8 ---;
+	@(link_name="GetCommandLineW")    get_command_line_w    :: proc() -> ^u16 ---;
+	@(link_name="GetSystemMetrics")   get_system_metrics    :: proc(index: i32) -> i32 ---;
+	@(link_name="GetCurrentThreadId") get_current_thread_id :: proc() -> u32 ---;
+
+	@(link_name="GetSystemTimeAsFileTime") get_system_time_as_file_time :: proc(system_time_as_file_time: ^Filetime) ---;
+	@(link_name="FileTimeToLocalFileTime") file_time_to_local_file_time :: proc(file_time: ^Filetime, local_file_time: ^Filetime) -> Bool ---;
+	@(link_name="FileTimeToSystemTime")    file_time_to_system_time     :: proc(file_time: ^Filetime, system_time: ^Systemtime) -> Bool ---;
+	@(link_name="SystemTimeToFileTime")    system_time_to_file_time     :: proc(system_time: ^Systemtime, file_time: ^Filetime) -> Bool ---;
+
+	@(link_name="CloseHandle")  close_handle   :: proc(h: Handle) -> i32 ---;
+	@(link_name="GetStdHandle") get_std_handle :: proc(h: i32) -> Handle ---;
 
 
 	@(link_name="CreateFileA")
 	@(link_name="CreateFileA")
-	create_file_a :: proc "std" (filename: ^u8, desired_access, share_module: u32,
-	                             security: rawptr,
-	                             creation, flags_and_attribs: u32, template_file: Handle) -> Handle ---;
+	create_file_a :: proc(filename: ^u8, desired_access, share_module: u32,
+	                      security: rawptr,
+	                      creation, flags_and_attribs: u32, template_file: Handle) -> Handle ---;
 
 
-	@(link_name="ReadFile")  read_file  :: proc "std" (h: Handle, buf: rawptr, to_read: u32, bytes_read: ^i32, overlapped: rawptr) -> Bool ---;
-	@(link_name="WriteFile") write_file :: proc "std" (h: Handle, buf: rawptr, len: i32, written_result: ^i32, overlapped: rawptr) -> Bool ---;
+	@(link_name="ReadFile")  read_file  :: proc(h: Handle, buf: rawptr, to_read: u32, bytes_read: ^i32, overlapped: rawptr) -> Bool ---;
+	@(link_name="WriteFile") write_file :: proc(h: Handle, buf: rawptr, len: i32, written_result: ^i32, overlapped: rawptr) -> Bool ---;
 
 
-	@(link_name="GetFileSizeEx")              get_file_size_ex               :: proc "std" (file_handle: Handle, file_size: ^i64) -> Bool ---;
-	@(link_name="GetFileAttributesA")         get_file_attributes_a          :: proc "std" (filename: ^u8) -> u32 ---;
-	@(link_name="GetFileAttributesExA")       get_file_attributes_ex_a       :: proc "std" (filename: ^u8, info_level_id: GET_FILEEX_INFO_LEVELS, file_info: rawptr) -> Bool ---;
-	@(link_name="GetFileInformationByHandle") get_file_information_by_handle :: proc "std" (file_handle: Handle, file_info: ^By_Handle_File_Information) -> Bool ---;
+	@(link_name="GetFileSizeEx")              get_file_size_ex               :: proc(file_handle: Handle, file_size: ^i64) -> Bool ---;
+	@(link_name="GetFileAttributesA")         get_file_attributes_a          :: proc(filename: ^u8) -> u32 ---;
+	@(link_name="GetFileAttributesExA")       get_file_attributes_ex_a       :: proc(filename: ^u8, info_level_id: GET_FILEEX_INFO_LEVELS, file_info: rawptr) -> Bool ---;
+	@(link_name="GetFileInformationByHandle") get_file_information_by_handle :: proc(file_handle: Handle, file_info: ^By_Handle_File_Information) -> Bool ---;
 
 
-	@(link_name="GetFileType")    get_file_type    :: proc "std" (file_handle: Handle) -> u32 ---;
-	@(link_name="SetFilePointer") set_file_pointer :: proc "std" (file_handle: Handle, distance_to_move: i32, distance_to_move_high: ^i32, move_method: u32) -> u32 ---;
+	@(link_name="GetFileType")    get_file_type    :: proc(file_handle: Handle) -> u32 ---;
+	@(link_name="SetFilePointer") set_file_pointer :: proc(file_handle: Handle, distance_to_move: i32, distance_to_move_high: ^i32, move_method: u32) -> u32 ---;
 
 
-	@(link_name="SetHandleInformation") set_handle_information :: proc "std" (obj: Handle, mask, flags: u32) -> Bool ---;
+	@(link_name="SetHandleInformation") set_handle_information :: proc(obj: Handle, mask, flags: u32) -> Bool ---;
 
 
-	@(link_name="FindFirstFileA") find_first_file_a :: proc "std" (file_name : ^u8, data : ^Find_Data) -> Handle ---;
-	@(link_name="FindNextFileA")  find_next_file_a  :: proc "std" (file : Handle, data : ^Find_Data) -> Bool ---;
-	@(link_name="FindClose")      find_close        :: proc "std" (file : Handle) -> Bool ---;
+	@(link_name="FindFirstFileA") find_first_file_a :: proc(file_name : ^u8, data : ^Find_Data) -> Handle ---;
+	@(link_name="FindNextFileA")  find_next_file_a  :: proc(file : Handle, data : ^Find_Data) -> Bool ---;
+	@(link_name="FindClose")      find_close        :: proc(file : Handle) -> Bool ---;
 
 
 
 
-	@(link_name="HeapAlloc")      heap_alloc       :: proc "std" (h: Handle, flags: u32, bytes: int) -> rawptr ---;
-	@(link_name="HeapReAlloc")    heap_realloc     :: proc "std" (h: Handle, flags: u32, memory: rawptr, bytes: int) -> rawptr ---;
-	@(link_name="HeapFree")       heap_free        :: proc "std" (h: Handle, flags: u32, memory: rawptr) -> Bool ---;
-	@(link_name="GetProcessHeap") get_process_heap :: proc "std" () -> Handle ---;
+	@(link_name="HeapAlloc")      heap_alloc       :: proc(h: Handle, flags: u32, bytes: int) -> rawptr ---;
+	@(link_name="HeapReAlloc")    heap_realloc     :: proc(h: Handle, flags: u32, memory: rawptr, bytes: int) -> rawptr ---;
+	@(link_name="HeapFree")       heap_free        :: proc(h: Handle, flags: u32, memory: rawptr) -> Bool ---;
+	@(link_name="GetProcessHeap") get_process_heap :: proc() -> Handle ---;
 
 
 
 
-	@(link_name="CreateSemaphoreA")    create_semaphore_a     :: proc "std" (attributes: ^Security_Attributes, initial_count, maximum_count: i32, name: ^u8) -> Handle ---;
-	@(link_name="ReleaseSemaphore")    release_semaphore      :: proc "std" (semaphore: Handle, release_count: i32, previous_count: ^i32) -> Bool ---;
-	@(link_name="WaitForSingleObject") wait_for_single_object :: proc "std" (handle: Handle, milliseconds: u32) -> u32 ---;
+	@(link_name="CreateSemaphoreA")    create_semaphore_a     :: proc(attributes: ^Security_Attributes, initial_count, maximum_count: i32, name: ^u8) -> Handle ---;
+	@(link_name="ReleaseSemaphore")    release_semaphore      :: proc(semaphore: Handle, release_count: i32, previous_count: ^i32) -> Bool ---;
+	@(link_name="WaitForSingleObject") wait_for_single_object :: proc(handle: Handle, milliseconds: u32) -> u32 ---;
 }
 }
 
 
+@(default_calling_convention = "c")
 foreign kernel32 {
 foreign kernel32 {
-	@(link_name="InterlockedCompareExchange") interlocked_compare_exchange :: proc "c" (dst: ^i32, exchange, comparand: i32) -> i32 ---;
-	@(link_name="InterlockedExchange")        interlocked_exchange         :: proc "c" (dst: ^i32, desired: i32) -> i32 ---;
-	@(link_name="InterlockedExchangeAdd")     interlocked_exchange_add     :: proc "c" (dst: ^i32, desired: i32) -> i32 ---;
-	@(link_name="InterlockedAnd")             interlocked_and              :: proc "c" (dst: ^i32, desired: i32) -> i32 ---;
-	@(link_name="InterlockedOr")              interlocked_or               :: proc "c" (dst: ^i32, desired: i32) -> i32 ---;
+	@(link_name="InterlockedCompareExchange") interlocked_compare_exchange :: proc(dst: ^i32, exchange, comparand: i32) -> i32 ---;
+	@(link_name="InterlockedExchange")        interlocked_exchange         :: proc(dst: ^i32, desired: i32) -> i32 ---;
+	@(link_name="InterlockedExchangeAdd")     interlocked_exchange_add     :: proc(dst: ^i32, desired: i32) -> i32 ---;
+	@(link_name="InterlockedAnd")             interlocked_and              :: proc(dst: ^i32, desired: i32) -> i32 ---;
+	@(link_name="InterlockedOr")              interlocked_or               :: proc(dst: ^i32, desired: i32) -> i32 ---;
 
 
-	@(link_name="InterlockedCompareExchange64") interlocked_compare_exchange64 :: proc "c" (dst: ^i64, exchange, comparand: i64) -> i64 ---;
-	@(link_name="InterlockedExchange64")        interlocked_exchange64         :: proc "c" (dst: ^i64, desired: i64) -> i64 ---;
-	@(link_name="InterlockedExchangeAdd64")     interlocked_exchange_add64     :: proc "c" (dst: ^i64, desired: i64) -> i64 ---;
-	@(link_name="InterlockedAnd64")             interlocked_and64              :: proc "c" (dst: ^i64, desired: i64) -> i64 ---;
-	@(link_name="InterlockedOr64")              interlocked_or64               :: proc "c" (dst: ^i64, desired: i64) -> i64 ---;
+	@(link_name="InterlockedCompareExchange64") interlocked_compare_exchange64 :: proc(dst: ^i64, exchange, comparand: i64) -> i64 ---;
+	@(link_name="InterlockedExchange64")        interlocked_exchange64         :: proc(dst: ^i64, desired: i64) -> i64 ---;
+	@(link_name="InterlockedExchangeAdd64")     interlocked_exchange_add64     :: proc(dst: ^i64, desired: i64) -> i64 ---;
+	@(link_name="InterlockedAnd64")             interlocked_and64              :: proc(dst: ^i64, desired: i64) -> i64 ---;
+	@(link_name="InterlockedOr64")              interlocked_or64               :: proc(dst: ^i64, desired: i64) -> i64 ---;
 }
 }
 
 
+@(default_calling_convention = "std")
 foreign kernel32 {
 foreign kernel32 {
-	@(link_name="_mm_pause")        mm_pause           :: proc "std" () ---;
-	@(link_name="ReadWriteBarrier") read_write_barrier :: proc "std" () ---;
-	@(link_name="WriteBarrier")     write_barrier      :: proc "std" () ---;
-	@(link_name="ReadBarrier")      read_barrier       :: proc "std" () ---;
+	@(link_name="_mm_pause")        mm_pause           :: proc() ---;
+	@(link_name="ReadWriteBarrier") read_write_barrier :: proc() ---;
+	@(link_name="WriteBarrier")     write_barrier      :: proc() ---;
+	@(link_name="ReadBarrier")      read_barrier       :: proc() ---;
 
 
 	@(link_name="CreateThread")
 	@(link_name="CreateThread")
-	create_thread :: proc "std" (thread_attributes: ^Security_Attributes, stack_size: int, start_routine: rawptr,
-	                             parameter: rawptr, creation_flags: u32, thread_id: ^u32) -> Handle ---;
-	@(link_name="ResumeThread")      resume_thread        :: proc "std" (thread: Handle) -> u32 ---;
-	@(link_name="GetThreadPriority") get_thread_priority  :: proc "std" (thread: Handle) -> i32 ---;
-	@(link_name="SetThreadPriority") set_thread_priority  :: proc "std" (thread: Handle, priority: i32) -> Bool ---;
-	@(link_name="GetExitCodeThread") get_exit_code_thread :: proc "std" (thread: Handle, exit_code: ^u32) -> Bool ---;
+	create_thread :: proc(thread_attributes: ^Security_Attributes, stack_size: int, start_routine: rawptr,
+	                      parameter: rawptr, creation_flags: u32, thread_id: ^u32) -> Handle ---;
+	@(link_name="ResumeThread")      resume_thread        :: proc(thread: Handle) -> u32 ---;
+	@(link_name="GetThreadPriority") get_thread_priority  :: proc(thread: Handle) -> i32 ---;
+	@(link_name="SetThreadPriority") set_thread_priority  :: proc(thread: Handle, priority: i32) -> Bool ---;
+	@(link_name="GetExitCodeThread") get_exit_code_thread :: proc(thread: Handle, exit_code: ^u32) -> Bool ---;
 
 
-	@(link_name="InitializeCriticalSection")             initialize_critical_section                :: proc "std" (critical_section: ^Critical_Section) ---;
-	@(link_name="InitializeCriticalSectionAndSpinCount") initialize_critical_section_and_spin_count :: proc "std" (critical_section: ^Critical_Section, spin_count: u32) ---;
-	@(link_name="DeleteCriticalSection")                 delete_critical_section                    :: proc "std" (critical_section: ^Critical_Section) ---;
-	@(link_name="SetCriticalSectionSpinCount")           set_critical_section_spin_count            :: proc "std" (critical_section: ^Critical_Section, spin_count: u32) -> u32 ---;
-	@(link_name="TryEnterCriticalSection")               try_enter_critical_section                 :: proc "std" (critical_section: ^Critical_Section) -> Bool ---;
-	@(link_name="EnterCriticalSection")                  enter_critical_section                     :: proc "std" (critical_section: ^Critical_Section) ---;
-	@(link_name="LeaveCriticalSection")                  leave_critical_section                     :: proc "std" (critical_section: ^Critical_Section) ---;
+	@(link_name="InitializeCriticalSection")             initialize_critical_section                :: proc(critical_section: ^Critical_Section) ---;
+	@(link_name="InitializeCriticalSectionAndSpinCount") initialize_critical_section_and_spin_count :: proc(critical_section: ^Critical_Section, spin_count: u32) ---;
+	@(link_name="DeleteCriticalSection")                 delete_critical_section                    :: proc(critical_section: ^Critical_Section) ---;
+	@(link_name="SetCriticalSectionSpinCount")           set_critical_section_spin_count            :: proc(critical_section: ^Critical_Section, spin_count: u32) -> u32 ---;
+	@(link_name="TryEnterCriticalSection")               try_enter_critical_section                 :: proc(critical_section: ^Critical_Section) -> Bool ---;
+	@(link_name="EnterCriticalSection")                  enter_critical_section                     :: proc(critical_section: ^Critical_Section) ---;
+	@(link_name="LeaveCriticalSection")                  leave_critical_section                     :: proc(critical_section: ^Critical_Section) ---;
 
 
-	@(link_name="CreateEventA") create_event_a :: proc "std" (event_attributes: ^Security_Attributes, manual_reset, initial_state: Bool, name: ^u8) -> Handle ---;
+	@(link_name="CreateEventA") create_event_a :: proc(event_attributes: ^Security_Attributes, manual_reset, initial_state: Bool, name: ^u8) -> Handle ---;
 
 
-	@(link_name="LoadLibraryA")   load_library_a   :: proc "std" (c_str: ^u8)  -> Hmodule ---;
-	@(link_name="LoadLibraryW")   load_library_a   :: proc "std" (c_str: ^u16) -> Hmodule ---;
-	@(link_name="FreeLibrary")    free_library     :: proc "std" (h: Hmodule) ---;
-	@(link_name="GetProcAddress") get_proc_address :: proc "std" (h: Hmodule, c_str: ^u8) -> rawptr ---;
+	@(link_name="LoadLibraryA")   load_library_a   :: proc(c_str: ^u8)  -> Hmodule ---;
+	@(link_name="LoadLibraryW")   load_library_a   :: proc(c_str: ^u16) -> Hmodule ---;
+	@(link_name="FreeLibrary")    free_library     :: proc(h: Hmodule) ---;
+	@(link_name="GetProcAddress") get_proc_address :: proc(h: Hmodule, c_str: ^u8) -> rawptr ---;
 
 
 }
 }
 
 
+@(default_calling_convention = "std")
 foreign user32 {
 foreign user32 {
-	@(link_name="GetDesktopWindow") get_desktop_window  :: proc "std" () -> Hwnd ---;
-	@(link_name="ShowCursor")       show_cursor         :: proc "std" (show : Bool) ---;
-	@(link_name="GetCursorPos")     get_cursor_pos      :: proc "std" (p: ^Point) -> Bool ---;
-	@(link_name="SetCursorPos")     set_cursor_pos      :: proc "std" (x, y: i32) -> Bool ---;
-	@(link_name="ScreenToClient")   screen_to_client    :: proc "std" (h: Hwnd, p: ^Point) -> Bool ---;
-	@(link_name="ClientToScreen")   client_to_screen    :: proc "std" (h: Hwnd, p: ^Point) -> Bool ---;
-	@(link_name="PostQuitMessage")  post_quit_message   :: proc "std" (exit_code: i32) ---;
-	@(link_name="SetWindowTextA")   set_window_text_a   :: proc "std" (hwnd: Hwnd, c_string: ^u8) -> Bool ---;
-	@(link_name="RegisterClassExA") register_class_ex_a :: proc "std" (wc: ^Wnd_Class_Ex_A) -> i16 ---;
-	@(link_name="RegisterClassExW") register_class_ex_w :: proc "std" (wc: ^Wnd_Class_Ex_W) -> i16 ---;
+	@(link_name="GetDesktopWindow") get_desktop_window  :: proc() -> Hwnd ---;
+	@(link_name="ShowCursor")       show_cursor         :: proc(show : Bool) ---;
+	@(link_name="GetCursorPos")     get_cursor_pos      :: proc(p: ^Point) -> Bool ---;
+	@(link_name="SetCursorPos")     set_cursor_pos      :: proc(x, y: i32) -> Bool ---;
+	@(link_name="ScreenToClient")   screen_to_client    :: proc(h: Hwnd, p: ^Point) -> Bool ---;
+	@(link_name="ClientToScreen")   client_to_screen    :: proc(h: Hwnd, p: ^Point) -> Bool ---;
+	@(link_name="PostQuitMessage")  post_quit_message   :: proc(exit_code: i32) ---;
+	@(link_name="SetWindowTextA")   set_window_text_a   :: proc(hwnd: Hwnd, c_string: ^u8) -> Bool ---;
+	@(link_name="RegisterClassExA") register_class_ex_a :: proc(wc: ^Wnd_Class_Ex_A) -> i16 ---;
+	@(link_name="RegisterClassExW") register_class_ex_w :: proc(wc: ^Wnd_Class_Ex_W) -> i16 ---;
 
 
 	@(link_name="CreateWindowExA")
 	@(link_name="CreateWindowExA")
-	create_window_ex_a :: proc "std" (ex_style: u32,
-	                                  class_name, title: ^u8,
-	                                  style: u32,
-	                                  x, y, w, h: i32,
-	                                  parent: Hwnd, menu: Hmenu, instance: Hinstance,
-	                                  param: rawptr) -> Hwnd ---;
+	create_window_ex_a :: proc(ex_style: u32,
+	                           class_name, title: ^u8,
+	                           style: u32,
+	                           x, y, w, h: i32,
+	                           parent: Hwnd, menu: Hmenu, instance: Hinstance,
+	                           param: rawptr) -> Hwnd ---;
 
 
 	@(link_name="CreateWindowExW")
 	@(link_name="CreateWindowExW")
-	create_window_ex_w :: proc "std" (ex_style: u32,
-	                                  class_name, title: ^u16,
-	                                  style: u32,
-	                                  x, y, w, h: i32,
-	                                  parent: Hwnd, menu: Hmenu, instance: Hinstance,
-	                                  param: rawptr) -> Hwnd ---;
+	create_window_ex_w :: proc(ex_style: u32,
+	                           class_name, title: ^u16,
+	                           style: u32,
+	                           x, y, w, h: i32,
+	                           parent: Hwnd, menu: Hmenu, instance: Hinstance,
+	                           param: rawptr) -> Hwnd ---;
 
 
-	@(link_name="ShowWindow")       show_window        :: proc "std" (hwnd: Hwnd, cmd_show: i32) -> Bool ---;
-	@(link_name="TranslateMessage") translate_message  :: proc "std" (msg: ^Msg) -> Bool ---;
-	@(link_name="DispatchMessageA") dispatch_message_a :: proc "std" (msg: ^Msg) -> Lresult ---;
-	@(link_name="DispatchMessageW") dispatch_message_w :: proc "std" (msg: ^Msg) -> Lresult ---;
-	@(link_name="UpdateWindow")     update_window      :: proc "std" (hwnd: Hwnd) -> Bool ---;
-	@(link_name="GetMessageA")      get_message_a      :: proc "std" (msg: ^Msg, hwnd: Hwnd, msg_filter_min, msg_filter_max : u32) -> Bool ---;
-	@(link_name="GetMessageW")      get_message_w      :: proc "std" (msg: ^Msg, hwnd: Hwnd, msg_filter_min, msg_filter_max : u32) -> Bool ---;
+	@(link_name="ShowWindow")       show_window        :: proc(hwnd: Hwnd, cmd_show: i32) -> Bool ---;
+	@(link_name="TranslateMessage") translate_message  :: proc(msg: ^Msg) -> Bool ---;
+	@(link_name="DispatchMessageA") dispatch_message_a :: proc(msg: ^Msg) -> Lresult ---;
+	@(link_name="DispatchMessageW") dispatch_message_w :: proc(msg: ^Msg) -> Lresult ---;
+	@(link_name="UpdateWindow")     update_window      :: proc(hwnd: Hwnd) -> Bool ---;
+	@(link_name="GetMessageA")      get_message_a      :: proc(msg: ^Msg, hwnd: Hwnd, msg_filter_min, msg_filter_max : u32) -> Bool ---;
+	@(link_name="GetMessageW")      get_message_w      :: proc(msg: ^Msg, hwnd: Hwnd, msg_filter_min, msg_filter_max : u32) -> Bool ---;
 
 
-	@(link_name="PeekMessageA") peek_message_a :: proc "std" (msg: ^Msg, hwnd: Hwnd, msg_filter_min, msg_filter_max, remove_msg: u32) -> Bool ---;
-	@(link_name="PeekMessageW") peek_message_w :: proc "std" (msg: ^Msg, hwnd: Hwnd, msg_filter_min, msg_filter_max, remove_msg: u32) -> Bool ---;
+	@(link_name="PeekMessageA") peek_message_a :: proc(msg: ^Msg, hwnd: Hwnd, msg_filter_min, msg_filter_max, remove_msg: u32) -> Bool ---;
+	@(link_name="PeekMessageW") peek_message_w :: proc(msg: ^Msg, hwnd: Hwnd, msg_filter_min, msg_filter_max, remove_msg: u32) -> Bool ---;
 
 
 
 
-	@(link_name="PostMessageA") post_message :: proc "std" (hwnd: Hwnd, msg, wparam, lparam : u32) -> Bool ---;
+	@(link_name="PostMessageA") post_message :: proc(hwnd: Hwnd, msg, wparam, lparam : u32) -> Bool ---;
 
 
-	@(link_name="DefWindowProcA") def_window_proc_a :: proc "std" (hwnd: Hwnd, msg: u32, wparam: Wparam, lparam: Lparam) -> Lresult ---;
+	@(link_name="DefWindowProcA") def_window_proc_a :: proc(hwnd: Hwnd, msg: u32, wparam: Wparam, lparam: Lparam) -> Lresult ---;
 
 
-	@(link_name="AdjustWindowRect") adjust_window_rect :: proc "std" (rect: ^Rect, style: u32, menu: Bool) -> Bool ---;
-	@(link_name="GetActiveWindow")  get_active_window  :: proc "std" () -> Hwnd ---;
+	@(link_name="AdjustWindowRect") adjust_window_rect :: proc(rect: ^Rect, style: u32, menu: Bool) -> Bool ---;
+	@(link_name="GetActiveWindow")  get_active_window  :: proc() -> Hwnd ---;
 
 
-	@(link_name="DestroyWindow")       destroy_window        :: proc "std" (wnd: Hwnd) -> Bool ---;
-	@(link_name="DescribePixelFormat") describe_pixel_format :: proc "std" (dc: Hdc, pixel_format: i32, bytes: u32, pfd: ^Pixel_Format_Descriptor) -> i32 ---;
+	@(link_name="DestroyWindow")       destroy_window        :: proc(wnd: Hwnd) -> Bool ---;
+	@(link_name="DescribePixelFormat") describe_pixel_format :: proc(dc: Hdc, pixel_format: i32, bytes: u32, pfd: ^Pixel_Format_Descriptor) -> i32 ---;
 
 
-	@(link_name="GetMonitor_InfoA")  get_monitor_info_a  :: proc "std" (monitor: Hmonitor, mi: ^Monitor_Info) -> Bool ---;
-	@(link_name="MonitorFromWindow") monitor_from_window :: proc "std" (wnd: Hwnd, flags : u32) -> Hmonitor ---;
+	@(link_name="GetMonitor_InfoA")  get_monitor_info_a  :: proc(monitor: Hmonitor, mi: ^Monitor_Info) -> Bool ---;
+	@(link_name="MonitorFromWindow") monitor_from_window :: proc(wnd: Hwnd, flags : u32) -> Hmonitor ---;
 
 
-	@(link_name="SetWindowPos") set_window_pos        :: proc "std" (wnd: Hwnd, wndInsertAfter: Hwnd, x, y, width, height: i32, flags: u32) ---;
+	@(link_name="SetWindowPos") set_window_pos :: proc(wnd: Hwnd, wndInsertAfter: Hwnd, x, y, width, height: i32, flags: u32) ---;
 
 
-	@(link_name="GetWindowPlacement") get_window_placement  :: proc "std" (wnd: Hwnd, wndpl: ^Window_Placement) -> Bool ---;
-	@(link_name="SetWindowPlacement") set_window_placement  :: proc "std" (wnd: Hwnd, wndpl: ^Window_Placement) -> Bool ---;
-	@(link_name="GetWindowRect")      get_window_rect       :: proc "std" (wnd: Hwnd, rect: ^Rect) -> Bool ---;
+	@(link_name="GetWindowPlacement") get_window_placement  :: proc(wnd: Hwnd, wndpl: ^Window_Placement) -> Bool ---;
+	@(link_name="SetWindowPlacement") set_window_placement  :: proc(wnd: Hwnd, wndpl: ^Window_Placement) -> Bool ---;
+	@(link_name="GetWindowRect")      get_window_rect       :: proc(wnd: Hwnd, rect: ^Rect) -> Bool ---;
 
 
-	@(link_name="GetWindowLongPtrA") get_window_long_ptr_a :: proc "std" (wnd: Hwnd, index: i32) -> Long_Ptr ---;
-	@(link_name="SetWindowLongPtrA") set_window_long_ptr_a :: proc "std" (wnd: Hwnd, index: i32, new: Long_Ptr) -> Long_Ptr ---;
-	@(link_name="GetWindowLongPtrW") get_window_long_ptr_w :: proc "std" (wnd: Hwnd, index: i32) -> Long_Ptr ---;
-	@(link_name="SetWindowLongPtrW") set_window_long_ptr_w :: proc "std" (wnd: Hwnd, index: i32, new: Long_Ptr) -> Long_Ptr ---;
+	@(link_name="GetWindowLongPtrA") get_window_long_ptr_a :: proc(wnd: Hwnd, index: i32) -> Long_Ptr ---;
+	@(link_name="SetWindowLongPtrA") set_window_long_ptr_a :: proc(wnd: Hwnd, index: i32, new: Long_Ptr) -> Long_Ptr ---;
+	@(link_name="GetWindowLongPtrW") get_window_long_ptr_w :: proc(wnd: Hwnd, index: i32) -> Long_Ptr ---;
+	@(link_name="SetWindowLongPtrW") set_window_long_ptr_w :: proc(wnd: Hwnd, index: i32, new: Long_Ptr) -> Long_Ptr ---;
 
 
-	@(link_name="GetWindowText") get_window_text :: proc "std" (wnd: Hwnd, str: ^u8, maxCount: i32) -> i32 ---;
+	@(link_name="GetWindowText") get_window_text :: proc(wnd: Hwnd, str: ^u8, maxCount: i32) -> i32 ---;
 
 
-	@(link_name="GetClientRect") get_client_rect :: proc "std" (hwnd: Hwnd, rect: ^Rect) -> Bool ---;
+	@(link_name="GetClientRect") get_client_rect :: proc(hwnd: Hwnd, rect: ^Rect) -> Bool ---;
 
 
-	@(link_name="GetDC")     get_dc     :: proc "std" (h: Hwnd) -> Hdc ---;
-	@(link_name="ReleaseDC") release_dc :: proc "std" (wnd: Hwnd, hdc: Hdc) -> i32 ---;
+	@(link_name="GetDC")     get_dc     :: proc(h: Hwnd) -> Hdc ---;
+	@(link_name="ReleaseDC") release_dc :: proc(wnd: Hwnd, hdc: Hdc) -> i32 ---;
 
 
-	@(link_name="MapVirtualKeyA") map_virtual_key_a :: proc "std" (scancode : u32, map_type : u32) -> u32 ---;
-	@(link_name="MapVirtualKeyW") map_virtual_key_w :: proc "std" (scancode : u32, map_type : u32) -> u32 ---;
+	@(link_name="MapVirtualKeyA") map_virtual_key_a :: proc(scancode : u32, map_type : u32) -> u32 ---;
+	@(link_name="MapVirtualKeyW") map_virtual_key_w :: proc(scancode : u32, map_type : u32) -> u32 ---;
 
 
-	@(link_name="GetKeyState")      get_key_state       :: proc "std" (v_key: i32) -> i16 ---;
-	@(link_name="GetAsyncKeyState") get_async_key_state :: proc "std" (v_key: i32) -> i16 ---;
+	@(link_name="GetKeyState")      get_key_state       :: proc(v_key: i32) -> i16 ---;
+	@(link_name="GetAsyncKeyState") get_async_key_state :: proc(v_key: i32) -> i16 ---;
 
 
-	@(link_name="SetForegroundWindow") set_foreground_window :: proc "std" (h: Hwnd) -> Bool ---;
-	@(link_name="SetFocus")            set_focus             :: proc "std" (h: Hwnd) -> Hwnd ---;
+	@(link_name="SetForegroundWindow") set_foreground_window :: proc(h: Hwnd) -> Bool ---;
+	@(link_name="SetFocus")            set_focus             :: proc(h: Hwnd) -> Hwnd ---;
 
 
 
 
 
 
-	@(link_name="RegisterRawInputDevices") register_raw_input_devices :: proc "std" (raw_input_device: ^Raw_Input_Device, num_devices, size: u32) -> Bool ---;
+	@(link_name="RegisterRawInputDevices") register_raw_input_devices :: proc(raw_input_device: ^Raw_Input_Device, num_devices, size: u32) -> Bool ---;
 
 
-	@(link_name="GetRawInputData") get_raw_input_data         :: proc "std" (raw_input: Hrawinput, command: u32, data: rawptr, size: ^u32, size_header: u32) -> u32 ---;
+	@(link_name="GetRawInputData") get_raw_input_data :: proc(raw_input: Hrawinput, command: u32, data: rawptr, size: ^u32, size_header: u32) -> u32 ---;
 
 
-	@(link_name="MapVirtualKeyExW") map_virtual_key_ex_w       :: proc "std" (code, map_type: u32, hkl: HKL) ---;
-	@(link_name="MapVirtualKeyExA") map_virtual_key_ex_a       :: proc "std" (code, map_type: u32, hkl: HKL) ---;
+	@(link_name="MapVirtualKeyExW") map_virtual_key_ex_w :: proc(code, map_type: u32, hkl: HKL) ---;
+	@(link_name="MapVirtualKeyExA") map_virtual_key_ex_a :: proc(code, map_type: u32, hkl: HKL) ---;
 }
 }
 
 
+@(default_calling_convention = "std")
 foreign gdi32 {
 foreign gdi32 {
-	@(link_name="GetStockObject") get_stock_object :: proc "std" (fn_object: i32) -> Hgdiobj ---;
+	@(link_name="GetStockObject") get_stock_object :: proc(fn_object: i32) -> Hgdiobj ---;
 
 
 	@(link_name="StretchDIBits")
 	@(link_name="StretchDIBits")
-	stretch_dibits :: proc "std" (hdc: Hdc,
-	                              x_dst, y_dst, width_dst, height_dst: i32,
-	                              x_src, y_src, width_src, header_src: i32,
-	                              bits: rawptr, bits_info: ^Bitmap_Info,
-	                              usage: u32,
-	                              rop: u32) -> i32 ---;
+	stretch_dibits :: proc(hdc: Hdc,
+	                       x_dst, y_dst, width_dst, height_dst: i32,
+	                       x_src, y_src, width_src, header_src: i32,
+	                       bits: rawptr, bits_info: ^Bitmap_Info,
+	                       usage: u32,
+	                       rop: u32) -> i32 ---;
 
 
-	@(link_name="SetPixelFormat")    set_pixel_format    :: proc "std" (hdc: Hdc, pixel_format: i32, pfd: ^Pixel_Format_Descriptor) -> Bool ---;
-	@(link_name="ChoosePixelFormat") choose_pixel_format :: proc "std" (hdc: Hdc, pfd: ^Pixel_Format_Descriptor) -> i32 ---;
-	@(link_name="SwapBuffers")       swap_buffers        :: proc "std" (hdc: Hdc) -> Bool ---;
+	@(link_name="SetPixelFormat")    set_pixel_format    :: proc(hdc: Hdc, pixel_format: i32, pfd: ^Pixel_Format_Descriptor) -> Bool ---;
+	@(link_name="ChoosePixelFormat") choose_pixel_format :: proc(hdc: Hdc, pfd: ^Pixel_Format_Descriptor) -> i32 ---;
+	@(link_name="SwapBuffers")       swap_buffers        :: proc(hdc: Hdc) -> Bool ---;
 
 
 }
 }
 
 
+@(default_calling_convention = "std")
 foreign shell32 {
 foreign shell32 {
-	@(link_name="CommandLineToArgvW") command_line_to_argv_w :: proc "std" (cmd_list: ^u16, num_args: ^i32) -> ^^u16 ---;
+	@(link_name="CommandLineToArgvW") command_line_to_argv_w :: proc(cmd_list: ^u16, num_args: ^i32) -> ^^u16 ---;
 }
 }
 
 
+@(default_calling_convention = "std")
 foreign winmm {
 foreign winmm {
-	@(link_name="timeGetTime") time_get_time :: proc "std" () -> u32 ---;
+	@(link_name="timeGetTime") time_get_time :: proc() -> u32 ---;
 }
 }
 
 
 
 

+ 11 - 4
src/check_decl.cpp

@@ -467,6 +467,10 @@ void check_proc_decl(Checker *c, Entity *e, DeclInfo *d) {
 
 
 
 
 	if (d != nullptr && d->attributes.count > 0) {
 	if (d != nullptr && d->attributes.count > 0) {
+		StringSet set = {};
+		string_set_init(&set, heap_allocator());
+		defer (string_set_destroy(&set));
+
 		for_array(i, d->attributes) {
 		for_array(i, d->attributes) {
 			AstNode *attr = d->attributes[i];
 			AstNode *attr = d->attributes[i];
 			if (attr->kind != AstNode_Attribute) continue;
 			if (attr->kind != AstNode_Attribute) continue;
@@ -500,14 +504,17 @@ void check_proc_decl(Checker *c, Entity *e, DeclInfo *d) {
 					}
 					}
 				}
 				}
 
 
+				if (string_set_exists(&set, name)) {
+					error(elem, "Previous declaration of `%.*s`", LIT(name));
+				} else {
+					string_set_add(&set, name);
+				}
+
 				if (name == "link_name") {
 				if (name == "link_name") {
-					if (link_name.len > 0) {
-						error(elem, "Previous declaration of `link_name`");
-					}
 					if (ev.kind == ExactValue_String) {
 					if (ev.kind == ExactValue_String) {
 						link_name = ev.value_string;
 						link_name = ev.value_string;
 					} else {
 					} else {
-						error(elem, "Expected a string value for `link_name`");
+						error(elem, "Expected a string value for `%.*s`", LIT(name));
 					}
 					}
 				} else {
 				} else {
 					error(elem, "Unknown attribute element name `%.*s`", LIT(name));
 					error(elem, "Unknown attribute element name `%.*s`", LIT(name));

+ 3 - 0
src/check_stmt.cpp

@@ -1670,8 +1670,11 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) {
 		CheckerContext prev_context = c->context;
 		CheckerContext prev_context = c->context;
 		if (ok) {
 		if (ok) {
 			c->context.curr_foreign_library = foreign_library;
 			c->context.curr_foreign_library = foreign_library;
+			c->context.default_foreign_cc = ProcCC_C;
 		}
 		}
 
 
+		check_foreign_block_decl_attributes(c, fb);
+
 		for_array(i, fb->decls) {
 		for_array(i, fb->decls) {
 			AstNode *decl = fb->decls[i];
 			AstNode *decl = fb->decls[i];
 			if (decl->kind == AstNode_ValueDecl && decl->ValueDecl.is_mutable) {
 			if (decl->kind == AstNode_ValueDecl && decl->ValueDecl.is_mutable) {

+ 13 - 4
src/check_type.cpp

@@ -1888,6 +1888,15 @@ bool check_procedure_type(Checker *c, Type *type, AstNode *proc_type_node, Array
 		}
 		}
 	}
 	}
 
 
+	ProcCallingConvention cc = pt->calling_convention;
+	if (cc == ProcCC_ForeignBlockDefault) {
+		cc = ProcCC_C;
+		if (c->context.default_foreign_cc > 0) {
+			cc = c->context.default_foreign_cc;
+		}
+	}
+	GB_ASSERT(cc > 0);
+
 	type->Proc.node                 = proc_type_node;
 	type->Proc.node                 = proc_type_node;
 	type->Proc.scope                = c->context.scope;
 	type->Proc.scope                = c->context.scope;
 	type->Proc.params               = params;
 	type->Proc.params               = params;
@@ -1895,18 +1904,18 @@ bool check_procedure_type(Checker *c, Type *type, AstNode *proc_type_node, Array
 	type->Proc.results              = results;
 	type->Proc.results              = results;
 	type->Proc.result_count         = cast(i32)result_count;
 	type->Proc.result_count         = cast(i32)result_count;
 	type->Proc.variadic             = variadic;
 	type->Proc.variadic             = variadic;
-	type->Proc.calling_convention   = pt->calling_convention;
+	type->Proc.calling_convention   = cc;
 	type->Proc.is_polymorphic       = pt->generic;
 	type->Proc.is_polymorphic       = pt->generic;
 	type->Proc.specialization_count = specialization_count;
 	type->Proc.specialization_count = specialization_count;
 
 
 	if (param_count > 0) {
 	if (param_count > 0) {
 		Entity *end = params->Tuple.variables[param_count-1];
 		Entity *end = params->Tuple.variables[param_count-1];
 		if (end->flags&EntityFlag_CVarArg) {
 		if (end->flags&EntityFlag_CVarArg) {
-			if (pt->calling_convention == ProcCC_Odin) {
+			if (cc == ProcCC_Odin) {
 				error(end->token, "Odin calling convention does not support #c_vararg");
 				error(end->token, "Odin calling convention does not support #c_vararg");
-			} else if (pt->calling_convention == ProcCC_Contextless) {
+			} else if (cc == ProcCC_Contextless) {
 				error(end->token, "Odin's contextless calling convention does not support #c_vararg");
 				error(end->token, "Odin's contextless calling convention does not support #c_vararg");
-			} else if (pt->calling_convention == ProcCC_Fast) {
+			} else if (cc == ProcCC_Fast) {
 				error(end->token, "Fast calling convention does not support #c_vararg");
 				error(end->token, "Fast calling convention does not support #c_vararg");
 			} else {
 			} else {
 				type->Proc.c_vararg = true;
 				type->Proc.c_vararg = true;

+ 82 - 0
src/checker.cpp

@@ -423,6 +423,7 @@ struct CheckerContext {
 	Type *     type_hint;
 	Type *     type_hint;
 	DeclInfo * curr_proc_decl;
 	DeclInfo * curr_proc_decl;
 	AstNode *  curr_foreign_library;
 	AstNode *  curr_foreign_library;
+	ProcCallingConvention default_foreign_cc;
 
 
 	bool       in_foreign_export;
 	bool       in_foreign_export;
 	bool       collect_delayed_decls;
 	bool       collect_delayed_decls;
@@ -1854,6 +1855,7 @@ void check_procedure_overloading(Checker *c, Entity *e) {
 	gb_temp_arena_memory_end(tmp);
 	gb_temp_arena_memory_end(tmp);
 }
 }
 
 
+void check_foreign_block_decl_attributes(Checker *c, AstNodeForeignBlockDecl *fb);
 
 
 #include "check_expr.cpp"
 #include "check_expr.cpp"
 #include "check_type.cpp"
 #include "check_type.cpp"
@@ -1861,6 +1863,71 @@ void check_procedure_overloading(Checker *c, Entity *e) {
 #include "check_stmt.cpp"
 #include "check_stmt.cpp"
 
 
 
 
+void check_foreign_block_decl_attributes(Checker *c, AstNodeForeignBlockDecl *fb) {
+	if (fb->attributes.count == 0) return;
+	StringSet set = {};
+	string_set_init(&set, heap_allocator());
+	defer (string_set_destroy(&set));
+
+	for_array(i, fb->attributes) {
+		AstNode *attr = fb->attributes[i];
+		if (attr->kind != AstNode_Attribute) continue;
+		for_array(j, attr->Attribute.elems) {
+			AstNode *elem = attr->Attribute.elems[j];
+			String name = {};
+			AstNode *value = nullptr;
+
+			switch (elem->kind) {
+			case_ast_node(i, Ident, elem);
+				name = i->token.string;
+			case_end;
+			case_ast_node(fv, FieldValue, elem);
+				GB_ASSERT(fv->field->kind == AstNode_Ident);
+				name = fv->field->Ident.token.string;
+				value = fv->value;
+			case_end;
+			default:
+				error(elem, "Invalid attribute element");
+				continue;
+			}
+
+			ExactValue ev = {};
+			if (value != nullptr) {
+				Operand op = {};
+				check_expr(c, &op, value);
+				if (op.mode != Addressing_Constant) {
+					error(value, "An attribute element must be constant");
+				} else {
+					ev = op.value;
+				}
+			}
+
+			if (string_set_exists(&set, name)) {
+				error(elem, "Previous declaration of `%.*s`", LIT(name));
+				continue;
+			} else {
+				string_set_add(&set, name);
+			}
+
+			if (name == "default_calling_convention") {
+				if (ev.kind == ExactValue_String) {
+					auto cc = string_to_calling_convention(ev.value_string);
+					if (cc == ProcCC_Invalid) {
+						error(elem, "Unknown procedure calling convention: `%.*s`\n", LIT(ev.value_string));
+					} else {
+						c->context.default_foreign_cc = cc;
+					}
+				} else {
+					error(elem, "Expected a string value for `%.*s`", LIT(name));
+				}
+			} else {
+				error(elem, "Unknown attribute element name `%.*s`", LIT(name));
+			}
+		}
+	}
+}
+
+
 
 
 bool check_arity_match(Checker *c, AstNodeValueDecl *vd, bool is_global) {
 bool check_arity_match(Checker *c, AstNodeValueDecl *vd, bool is_global) {
 	isize lhs = vd->names.count;
 	isize lhs = vd->names.count;
@@ -1990,6 +2057,7 @@ void check_collect_value_decl(Checker *c, AstNode *decl) {
 				GB_ASSERT(fl->kind == AstNode_Ident);
 				GB_ASSERT(fl->kind == AstNode_Ident);
 				e->Variable.is_foreign = true;
 				e->Variable.is_foreign = true;
 				e->Variable.foreign_library_ident = fl;
 				e->Variable.foreign_library_ident = fl;
+
 			} else if (c->context.in_foreign_export) {
 			} else if (c->context.in_foreign_export) {
 				e->Variable.is_export = true;
 				e->Variable.is_export = true;
 			}
 			}
@@ -2054,6 +2122,18 @@ void check_collect_value_decl(Checker *c, AstNode *decl) {
 					GB_ASSERT(fl->kind == AstNode_Ident);
 					GB_ASSERT(fl->kind == AstNode_Ident);
 					e->Procedure.foreign_library_ident = fl;
 					e->Procedure.foreign_library_ident = fl;
 					e->Procedure.is_foreign = true;
 					e->Procedure.is_foreign = true;
+
+					GB_ASSERT(pl->type->kind == AstNode_ProcType);
+					auto cc = pl->type->ProcType.calling_convention;
+					if (cc == ProcCC_ForeignBlockDefault) {
+						cc = ProcCC_C;
+						if (c->context.default_foreign_cc > 0) {
+							cc = c->context.default_foreign_cc;
+						}
+					}
+					GB_ASSERT(cc != ProcCC_Invalid);
+					pl->type->ProcType.calling_convention = cc;
+
 				} else if (c->context.in_foreign_export) {
 				} else if (c->context.in_foreign_export) {
 					e->Procedure.is_export = true;
 					e->Procedure.is_export = true;
 				}
 				}
@@ -2103,6 +2183,8 @@ void check_add_foreign_block_decl(Checker *c, AstNode *decl) {
 		c->context.curr_foreign_library = nullptr;
 		c->context.curr_foreign_library = nullptr;
 	}
 	}
 
 
+	check_foreign_block_decl_attributes(c, fb);
+
 	c->context.collect_delayed_decls = true;
 	c->context.collect_delayed_decls = true;
 	check_collect_entities(c, fb->decls);
 	check_collect_entities(c, fb->decls);
 	c->context = prev_context;
 	c->context = prev_context;

+ 1 - 0
src/common.cpp

@@ -114,6 +114,7 @@ u128 fnv128a(void const *data, isize len) {
 
 
 #include "map.cpp"
 #include "map.cpp"
 #include "ptr_set.cpp"
 #include "ptr_set.cpp"
+#include "string_set.cpp"
 #include "priority_queue.cpp"
 #include "priority_queue.cpp"
 
 
 
 

+ 33 - 17
src/parser.cpp

@@ -110,6 +110,8 @@ enum ProcCallingConvention {
 	ProcCC_C           = 3,
 	ProcCC_C           = 3,
 	ProcCC_Std         = 4,
 	ProcCC_Std         = 4,
 	ProcCC_Fast        = 5,
 	ProcCC_Fast        = 5,
+
+	ProcCC_ForeignBlockDefault = -1,
 };
 };
 
 
 enum VarDeclFlag {
 enum VarDeclFlag {
@@ -332,6 +334,7 @@ AST_NODE_KIND(_DeclBegin,      "", i32) \
 		AstNode *        foreign_library; \
 		AstNode *        foreign_library; \
 		Token            open, close;     \
 		Token            open, close;     \
 		Array<AstNode *> decls;           \
 		Array<AstNode *> decls;           \
+		Array<AstNode *> attributes;      \
 		bool             been_handled;    \
 		bool             been_handled;    \
 		CommentGroup     docs;            \
 		CommentGroup     docs;            \
 	}) \
 	}) \
@@ -842,6 +845,7 @@ AstNode *clone_ast_node(gbAllocator a, AstNode *node) {
 	case AstNode_ForeignBlockDecl:
 	case AstNode_ForeignBlockDecl:
 		n->ForeignBlockDecl.foreign_library = clone_ast_node(a, n->ForeignBlockDecl.foreign_library);
 		n->ForeignBlockDecl.foreign_library = clone_ast_node(a, n->ForeignBlockDecl.foreign_library);
 		n->ForeignBlockDecl.decls           = clone_ast_node_array(a, n->ForeignBlockDecl.decls);
 		n->ForeignBlockDecl.decls           = clone_ast_node_array(a, n->ForeignBlockDecl.decls);
+		n->ForeignBlockDecl.attributes      = clone_ast_node_array(a, n->ForeignBlockDecl.attributes);
 		break;
 		break;
 	case AstNode_Label:
 	case AstNode_Label:
 		n->Label.name = clone_ast_node(a, n->Label.name);
 		n->Label.name = clone_ast_node(a, n->Label.name);
@@ -1548,6 +1552,8 @@ AstNode *ast_foreign_block_decl(AstFile *f, Token token, AstNode *foreign_librar
 	result->ForeignBlockDecl.close           = close;
 	result->ForeignBlockDecl.close           = close;
 	result->ForeignBlockDecl.decls           = decls;
 	result->ForeignBlockDecl.decls           = decls;
 	result->ForeignBlockDecl.docs            = docs;
 	result->ForeignBlockDecl.docs            = docs;
+
+	result->ForeignBlockDecl.attributes.allocator = heap_allocator();
 	return result;
 	return result;
 }
 }
 
 
@@ -3247,6 +3253,22 @@ AstNode *parse_results(AstFile *f) {
 	return list;
 	return list;
 }
 }
 
 
+
+ProcCallingConvention string_to_calling_convention(String s) {
+	if (s == "odin") {
+		return ProcCC_Odin;
+	} else if (s == "contextless") {
+		return ProcCC_Contextless;
+	} else if (s == "cdecl" || s == "c") {
+		return ProcCC_C;
+	} else if (s == "stdcall" || s == "std") {
+		return ProcCC_Std;
+	} else if (s == "fastcall" || s == "fast") {
+		return ProcCC_Fast;
+	}
+	return ProcCC_Invalid;
+}
+
 AstNode *parse_proc_type(AstFile *f, Token proc_token) {
 AstNode *parse_proc_type(AstFile *f, Token proc_token) {
 	AstNode *params = nullptr;
 	AstNode *params = nullptr;
 	AstNode *results = nullptr;
 	AstNode *results = nullptr;
@@ -3254,24 +3276,16 @@ AstNode *parse_proc_type(AstFile *f, Token proc_token) {
 	ProcCallingConvention cc = ProcCC_Invalid;
 	ProcCallingConvention cc = ProcCC_Invalid;
 	if (f->curr_token.kind == Token_String) {
 	if (f->curr_token.kind == Token_String) {
 		Token token = expect_token(f, Token_String);
 		Token token = expect_token(f, Token_String);
-		String conv = token.string;
-		if (conv == "odin") {
-			cc = ProcCC_Odin;
-		} else if (conv == "contextless") {
-			cc = ProcCC_Contextless;
-		} else if (conv == "cdecl" || conv == "c") {
-			cc = ProcCC_C;
-		} else if (conv == "stdcall" || conv == "std") {
-			cc = ProcCC_Std;
-		} else if (conv == "fastcall" || conv == "fast") {
-			cc = ProcCC_Fast;
+		auto c = string_to_calling_convention(token.string);
+		if (c == ProcCC_Invalid) {
+			syntax_error(token, "Unknown procedure calling convention: `%.*s`\n", LIT(token.string));
 		} else {
 		} else {
-			syntax_error(token, "Unknown procedure tag #%.*s\n", LIT(conv));
+			cc = c;
 		}
 		}
 	}
 	}
 	if (cc == ProcCC_Invalid) {
 	if (cc == ProcCC_Invalid) {
 		if (f->in_foreign_block) {
 		if (f->in_foreign_block) {
-			cc = ProcCC_C;
+			cc = ProcCC_ForeignBlockDefault;
 		} else {
 		} else {
 			cc = ProcCC_Odin;
 			cc = ProcCC_Odin;
 		}
 		}
@@ -4578,7 +4592,6 @@ AstNode *parse_stmt(AstFile *f) {
 		f->expr_level++;
 		f->expr_level++;
 		if (f->curr_token.kind != Token_CloseParen) {
 		if (f->curr_token.kind != Token_CloseParen) {
 			elems = make_ast_node_array(f);
 			elems = make_ast_node_array(f);
-
 			while (f->curr_token.kind != Token_CloseParen &&
 			while (f->curr_token.kind != Token_CloseParen &&
 			       f->curr_token.kind != Token_EOF) {
 			       f->curr_token.kind != Token_EOF) {
 				AstNode *elem = parse_ident(f);
 				AstNode *elem = parse_ident(f);
@@ -4601,12 +4614,15 @@ AstNode *parse_stmt(AstFile *f) {
 		AstNode *attribute = ast_attribute(f, token, open, close, elems);
 		AstNode *attribute = ast_attribute(f, token, open, close, elems);
 
 
 		AstNode *decl = parse_stmt(f);
 		AstNode *decl = parse_stmt(f);
-		if (decl->kind != AstNode_ValueDecl) {
-			syntax_error(decl, "Expected a value declaration after an attribute, got %.*s", LIT(ast_node_strings[decl->kind]));
+		if (decl->kind == AstNode_ValueDecl) {
+			array_add(&decl->ValueDecl.attributes, attribute);
+		} else if (decl->kind == AstNode_ForeignBlockDecl) {
+			array_add(&decl->ForeignBlockDecl.attributes, attribute);
+		} else {
+			syntax_error(decl, "Expected a value or foreign declaration after an attribute, got %.*s", LIT(ast_node_strings[decl->kind]));
 			return ast_bad_stmt(f, token, f->curr_token);
 			return ast_bad_stmt(f, token, f->curr_token);
 		}
 		}
 
 
-		array_add(&decl->ValueDecl.attributes, attribute);
 		return decl;
 		return decl;
 	}
 	}