Attributes

enum Header

This enum is used by several of the other attributes to specify includes in the generated code.

case System(header : String)

header specifies the name of a system file to be included. The generated code for each system file included is #include <header.h>.

case User(header : String)

header specifies the name of a user file to be included. The generated code for each user file included is #include "header.h".

enum ParameterStyle

This enum specifies the way in which an external type is passed in a call to an external function.

case ConstReference

The parameter should be of type const T&.

case Pointer

The parameter should be of type T*.

case RValueReference

The parameter should be of type T&&.

case Value

The parameter should be of type T.

enum EnumKind

This enum specifies whether an enumeration is scoped or unscoped.

case Scoped

This is a scoped enumeration, and is therefore mapped to enum class in C++.

case Unscoped

This is an unscoped enumeration, and is therefore mapped to enum in C++.

attribute @mapToEnum(name : String, kind : EnumKind, header… : Header)
appliesto:Enumerations

This attribute specifies that Coco should not generate a C++ enum for this declaration, but should instead reuse a pre-existing declaration. The following example specifies how the Coco enum E is mapped to the scoped enum MyEnum in the generated C++ code:

@CPP.mapToEnum("MyEnum", .Scoped, .User("MyEnum.h"))
enum E {
  case A
  case B
}

By default, the code generator will assume that all enum cases have the same name in C++. However, enum cases can also be labelled with the @mapToValue attribute to map them to different C++ names, as illustrated in the following example:

@CPP.mapToEnum("Name::Space::MyNestedEnum", .Unscoped, .User("MyNestedEnum.h"))
enum NestedE {
  case A
  @CPP.mapToValue("Remapped")
  case B
}

For enum declarations that are labelled with @mapToEnum, we advise also labelling them with the @nonExhaustive attribute, in order to make this mapping more robust. This will help cover the case where the Coco version of an enum accidentally omits some cases defined in the corresponding version that it is mapped to. This can arise, for example, if the C++ version is extended with additional cases, and the Coco version is not by mistake.

See also

@nonExhaustive attribute.

attribute @mapToMember(name : String)
Applies to:External functions with at least one argument, and external member functions with any number of arguments.

This attribute specifies that a function should be mapped to a member function of the first argument instead of to a top-level function. For example, given:

@CPP.mapToMember("f")
external function f(x : T, y : Bool) : Nil = {}

then an expression like f(a, b) will be mapped to a.f(b) in the generated C++ code.

attribute @mapToType(name : String, parameterStyle : ParameterStyle, header… : Header)
Applies to:External types

This attribute specifies how an external type should be mapped into the generated C++. The following example shows how the external datatype ChronoDuration is mapped to std::chrono::duration in the generated C++ code:

@CPP.mapToType("std::chrono::duration", CPP.ParameterStyle.Value, CPP.Header.System("chrono"))
external type ChronoDuration
attribute @mapToValue(name : String, header… : Header)
Applies to:External functions and external constants.

This attribute specifies how external functions and external constants are to be mapped into the generated C++. The following example shows how a reference to the standard library std::this_thread::sleep_for function can be accessed:

@CPP.mapToValue("std::this_thread::sleep_for", CPP.Header.System("thread"), CPP.Header.System("chrono"))
external function chronoSleep(t : Duration) : Nil = {}

Similarly, the following example illustrates how an external constant can be mapped to a constant value declared in C++:

@CPP.mapToValue("cocotec::project::kTimeoutPeriod", .User("cocotec/constants.h"))
external val kTimeoutPeriod : Duration = _

Advanced Attributes

attribute @binaryOperator(operator : BinaryOperator)
Applies to:External functions with precisely two arguments, and external member functions with precisely one argument.

This attribute is used to specify that the given Coco function should be mapped to the given operator in C++.

enum BinaryOperator
case ArrayDereference
case Assign
case Divide
case Equals
case GreaterThan
case GreaterThanEquals
case LessThan
case LessThanEquals
case LogicalAnd
case LogicalOr
case Minus
case NotEquals
case Plus
case Remainder
case Times
attribute @defaultHeaders(header… : Header)
Applies to:module

When applied to a Coco module, this attribute will automatically add the specified headers to all occurrences of @mapToType, @mapToValue, and @mapToEnum within the module, thereby reducing duplication. For example:

@CPP.defaultHeaders(.System("thread"), .System("map"), .User("common_cpp.h"))
module

import CPP

@CPP.mapToType("std::vector<{0}>", .ConstReference, .System("vector"))
@derive(Default)
external type Vector<T, val max : Int where 0 <= max>

In this example, the headers listed in the @defaultHeaders attribute on the module will be added to the @mapToType attribute applied to the external type declaration.

In order to apply attributes to Coco modules, the keyword module must be explicitly used and labelled with the corresponding attributes, as illustrated in the example above.

attribute @include(header… : Header)
Applies to:Declarations

When applied to a Coco declaration, this attribute will cause any usage of this declaration to contain the given includes. For example:

@CPP.include(CPP.Header.User("hdr"))
port P {  }

will include #include "hdr" in the generated C++ file that includes P.

attribute @implicitCast
Applies to:External functions with exactly one argument.

This attribute is used to specify that an external function represents an implicit cast in C++, and therefore does not need to be represented in the generated code. For example, given:

@CPP.implicitCast
external function f(x : Int) : IntLike = _

f(y) will appear in the generated code as y.

attribute @unaryOperator(operator : UnaryOperator)
Applies to:External functions with precisely one argument, and external member functions with no arguments.

This attribute is used to specify that the given Coco function should be mapped to the given operator in C++.

enum UnaryOperator
case LogicalNot
case Negate
case PostDecrement
case PostIncrement
case PreDecrement
case PreIncrement