Skip to content
Adam
C interop

clink and cpplink

How a routine's symbol is named, and how its parameters are passed.

Examples: res\examples\language\linkage holds small, commented programs for this page's features, each listing the output it prints; run one with adam build <file> -r.

The two linkages

clink, the default, gives a routine C linkage: a public routine's symbol is its plain, unmangled name. cpplink names the symbol as the Itanium C++ ABI mangles it, so a C++ compiler targeting x86_64-windows-gnu links an Adam routine as a C++ function.

A routine that is not public has a symbol internal to its module, whatever its linkage, so two modules' private routines of one name never meet. Two units exporting a clink routine of one name is an error at the second: C has one symbol for both.

Overloads

C linkage cannot express overloads. An overloaded routine with no linkage word is cpplink, silently; one written clink is given cpplink with warning SM0323 at each definition, and the program is still built:

linkage_overloads.adam
module exe linkage_overloads;

func clink Show(const AN: int32);
begin
  println("int {}", AN);
end;

func clink Show(const AText: string);
begin
  println("text {}", AText);
end;

begin
  Show(7);
  Show("seven");
end.
warningSM0323…/samples/linkage_overloads.adam(28,1)
'Show' is overloaded: C linkage dropped, it gets C++ linkage
warningSM0323…/samples/linkage_overloads.adam(33,1)
'Show' is overloaded: C linkage dropped, it gets C++ linkage

Parameters

A var parameter is passed by reference under every linkage. A const parameter under C linkage is passed by value whatever its type, as C has no references; under C++ linkage a scalar or pointer goes by value and anything else by const reference.

A routine and its forward agree on the linkage (SM0306), and a routine value fills a routine type of its own linkage only (SM0200): func(..) and func clink(..) are one type, func cpplink(..) another.

Variadics

A clink variadic routine with a body is called as C calls it, with no argument count: it needs a fixed parameter before ..., reads its arguments with varargs.next(T), and varargs.count in it is an error. A cpplink variadic carries a typed pack: varargs.count, next, get, reset and copy, checked at run time.

linkage_variadic.adam
module exe linkage_variadic;

// C's way: the count comes as a fixed parameter
func clink SumC(const ACount: int32; ...): int64;
var
  LSum: int64;
  LI: int32;
begin
  LSum := 0;
  for LI := 1 to ACount do
    LSum := LSum + int64(varargs.next(int32));
  end;
  return LSum;
end;

// Adam's way: the pack knows its count
func cpplink SumPack(...): int64;
var
  LSum: int64;
  LI: int32;
begin
  LSum := 0;
  for LI := 1 to varargs.count do
    LSum := LSum + int64(varargs.next(int32));
  end;
  return LSum;
end;

func cpplink CountOf(...): int32;
begin
  return varargs.count;
end;

begin
  println("{}", SumC(3, 1, 2, 3));
  println("{} {}", CountOf(10, 20, 30), SumPack(10, 20, 30));
end.
output on x86_64-windows-gnu and x86_64-linux-gnu
6
3 60

Calling a C variadic is on C variadics.

Generics

An instance of a generic routine is its own cpplink routine: Instances.

↑ ↓ moveEnter openEsc close