From 58f9c50eb2b0dbea7f8ec211a0d223af20acf187 Mon Sep 17 00:00:00 2001 From: Tetralux Date: Sun, 8 Dec 2024 05:38:50 +0000 Subject: [PATCH] Clarify nature of calling convention --- content/docs/overview.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/content/docs/overview.md b/content/docs/overview.md index d5164b33..271019a6 100644 --- a/content/docs/overview.md +++ b/content/docs/overview.md @@ -698,9 +698,13 @@ multiply :: proc(x, y: int) -> int { fmt.println(multiply(137, 432)) ``` -Continuing the C family tradition, everything in Odin is passed by value (rather than by reference, e.g. FORTRAN, Java, etc). However, Odin differs from the C/C++ tradition in that all procedure parameters in Odin are immutable values. This allows for numerous optimizations with the Odin [calling conventions](#calling-conventions) (`"odin"` and `"contextless"`) which would not be possible with the original C tradition of always passing a copy of the thing that has been passed. +By default, Odin procedures use the `"odin"` calling convention. This calling convention is the same as C, however it differs in a couple of ways: + - It promotes values to a pointer if that's more efficient on the target system, and + - It includes a pointer to the current context as an implicit additional argument. -Passing a pointer value makes a copy of the pointer, not the data it points to. Slices, dynamic arrays, and maps behave like pointers in this case (Internally they are structures that contain values, which include pointers, and the "structure" is passed by value). +The promotion is enabled by the fact that all parameters are immutable in Odin, and its rules are consistent for a given type and platform and can be relied on since they are part of the calling convention. + +Passing a pointer value makes a copy of the pointer, not the data it points to. Slices, dynamic arrays, and maps have no special considerations here; they are normal structures with pointer fields, and are passed as such. Their elements will not be copied. To mutate the procedure parameter (like in C), an explicit copy is required. This can be done through shadowing the variable declaration: