Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions src/source/CGenerator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class CGenerator(spec: Spec) extends Generator(spec) {
private val cppMarshal = new CppMarshal(spec)

private class ResolvedField(val field: Field, val translator: CTypeTranslator)
private class ResolvedConst(val const: ast.Const, val translator: CTypeTranslator)

private class ResolvedMethod(val resolvedName: String, val method: Interface.Method, val parameters: Seq[ResolvedField], val returnType: Option[CTypeTranslator]) {
def retTypename: String = if (returnType.isDefined) returnType.get.typename else "void"
Expand Down Expand Up @@ -99,8 +100,8 @@ class CGenerator(spec: Spec) extends Generator(spec) {
val symbolName = resolveSymbolName(ident.name)
val enumCasePrefix = symbolName + "_"
writeDoc(w, doc)
w.w("enum " + symbolName)
w.bracedSemi {
w.w("typedef enum")
w.bracedEnd(s" ${symbolName};") {
writeEnumOptionNone(w, e, idCpp.enum, "=", enumCasePrefix)
writeEnumOptions(w, e, idCpp.enum, "=", enumCasePrefix)
writeEnumOptionAll(w, e, idCpp.enum, "=", enumCasePrefix)
Expand Down Expand Up @@ -131,6 +132,25 @@ class CGenerator(spec: Spec) extends Generator(spec) {
writeParamList(w, params.map(p => (p.translator.typename, p.field.ident.name)))
}

private def generateConstsHeader(typename: String, consts: Seq[ResolvedConst], w: IndentWriter): Unit = {
for (const <- consts) {
writeDoc(w, const.const.doc)
w.wl(s"""${const.translator.typename} ${typename}_get_${const.const.ident.name}();""")
w.wl
}
}

private def generateConstsImpl(cppTypename: String, typename: String, consts: Seq[ResolvedConst], w: IndentWriter): Unit = {
for (const <- consts) {
w.w(s"""${const.translator.typename} ${typename}_get_${const.const.ident.name}()""")
w.braced {
val cppValue = s"${cppTypename}::${idCpp.const(const.const.ident)}"
w.wl(s"""return ${const.translator.fromCpp(cppValue)};""")
}
w.wl
}
}

override def generateRecord(origin: String, ident: Ident, doc: Doc, params: Seq[TypeParam], r: ast.Record): Unit = {
val selfCpp = cppMarshal.fqTypename(ident, r)

Expand All @@ -139,6 +159,7 @@ class CGenerator(spec: Spec) extends Generator(spec) {
val typeName = resolveRefSymbolTypeName(ident)

val resolvedFields = r.fields.map(f => (new ResolvedField(f, typeResolver.resolve(f.ty.resolved))))
val resolvedConsts = r.consts.map(r => new ResolvedConst(r, typeResolver.resolve(r.ty.resolved)))

writeCFilePair(origin, ident, typeResolver.publicImports.toSeq, typeResolver.privateImports.toSeq)((w: IndentWriter) => {
writeDoc(w, doc)
Expand All @@ -148,6 +169,9 @@ class CGenerator(spec: Spec) extends Generator(spec) {
w.w(s"""${typeName} ${prefix}_new(""")
writeParamListWithResolvedFields(w, resolvedFields)
w.wl(");")
w.wl

generateConstsHeader(prefix, resolvedConsts, w)

for (resolvedField <- resolvedFields) {
val fieldName = resolvedField.field.ident.name
Expand All @@ -174,6 +198,8 @@ class CGenerator(spec: Spec) extends Generator(spec) {

w.wl

generateConstsImpl(selfCpp, prefix, resolvedConsts, w)

val toCppExpr = s"::djinni::c_api::RecordTranslator<${selfCpp}>::toCpp(instance)"
for (resolvedField <- resolvedFields) {
val fieldName = resolvedField.field.ident.name
Expand Down Expand Up @@ -293,6 +319,7 @@ class CGenerator(spec: Spec) extends Generator(spec) {
),
m.ret.map(r => typeResolver.resolve(r.resolved))
))
val resolvedConsts = i.consts.map(r => new ResolvedConst(r, typeResolver.resolve(r.ty.resolved)))

val prefix = resolveSymbolName(ident.name)
val typeName = resolveRefSymbolTypeName(ident)
Expand Down Expand Up @@ -330,6 +357,8 @@ class CGenerator(spec: Spec) extends Generator(spec) {
w.wl
}

generateConstsHeader(prefix, resolvedConsts, w)

for (resolvedMethod <- resolvedMethods) {
writeDoc(w, resolvedMethod.method.doc)
w.w(s"${resolvedMethod.retTypename} ${prefix}_${resolvedMethod.resolvedName}(")
Expand Down Expand Up @@ -361,6 +390,8 @@ class CGenerator(spec: Spec) extends Generator(spec) {
w.wl
}

generateConstsImpl(selfCpp, prefix, resolvedConsts, w)

for (resolvedMethod <- resolvedMethods) {
writeDoc(w, resolvedMethod.method.doc)
w.w(s"${resolvedMethod.retTypename} ${prefix}_${resolvedMethod.resolvedName}(")
Expand Down
1 change: 1 addition & 0 deletions test-suite/generated-src/c/RecordWithEmbeddedCppProto.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ extern "C" {
typedef djinni_record_ref testsuite_RecordWithEmbeddedCppProto_ref;

testsuite_RecordWithEmbeddedCppProto_ref testsuite_RecordWithEmbeddedCppProto_new(djinni_binary_ref state);

djinni_binary_ref testsuite_RecordWithEmbeddedCppProto_get_state(testsuite_RecordWithEmbeddedCppProto_ref instance);
void testsuite_RecordWithEmbeddedCppProto_set_state(testsuite_RecordWithEmbeddedCppProto_ref instance, djinni_binary_ref value);

Expand Down
1 change: 1 addition & 0 deletions test-suite/generated-src/c/RecordWithEmbeddedProto.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ extern "C" {
typedef djinni_record_ref testsuite_RecordWithEmbeddedProto_ref;

testsuite_RecordWithEmbeddedProto_ref testsuite_RecordWithEmbeddedProto_new(djinni_binary_ref person);

djinni_binary_ref testsuite_RecordWithEmbeddedProto_get_person(testsuite_RecordWithEmbeddedProto_ref instance);
void testsuite_RecordWithEmbeddedProto_set_person(testsuite_RecordWithEmbeddedProto_ref instance, djinni_binary_ref value);

Expand Down
1 change: 1 addition & 0 deletions test-suite/generated-src/c/_varname_record_.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ extern "C" {
typedef djinni_record_ref testsuite__varname_record__ref;

testsuite__varname_record__ref testsuite__varname_record__new(int8_t _field_);

int8_t testsuite__varname_record__get__field_(testsuite__varname_record__ref instance);
void testsuite__varname_record__set__field_(testsuite__varname_record__ref instance, int8_t value);

Expand Down
4 changes: 2 additions & 2 deletions test-suite/generated-src/c/access_flags.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
extern "C" {
#endif // __cplusplus

enum testsuite_access_flags {
typedef enum {
testsuite_access_flags_NOBODY = 0,
testsuite_access_flags_OWNER_READ = 1 << 0,
testsuite_access_flags_OWNER_WRITE = 1 << 1,
Expand All @@ -21,7 +21,7 @@ enum testsuite_access_flags {
testsuite_access_flags_SYSTEM_WRITE = 1 << 7,
testsuite_access_flags_SYSTEM_EXECUTE = 1 << 8,
testsuite_access_flags_EVERYBODY = 0 | (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4) | (1 << 5) | (1 << 6) | (1 << 7) | (1 << 8),
};
} testsuite_access_flags;
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
Expand Down
1 change: 1 addition & 0 deletions test-suite/generated-src/c/assorted_primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ extern "C" {
typedef djinni_record_ref testsuite_assorted_primitives_ref;

testsuite_assorted_primitives_ref testsuite_assorted_primitives_new(bool b, int8_t eight, int16_t sixteen, int32_t thirtytwo, int64_t sixtyfour, float fthirtytwo, double fsixtyfour, djinni_optional_bool o_b, djinni_optional_int8 o_eight, djinni_optional_int16 o_sixteen, djinni_optional_int32 o_thirtytwo, djinni_optional_int64 o_sixtyfour, djinni_optional_float o_fthirtytwo, djinni_optional_double o_fsixtyfour);

bool testsuite_assorted_primitives_get_b(testsuite_assorted_primitives_ref instance);
void testsuite_assorted_primitives_set_b(testsuite_assorted_primitives_ref instance, bool value);

Expand Down
1 change: 1 addition & 0 deletions test-suite/generated-src/c/client_returned_record.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ extern "C" {
typedef djinni_record_ref testsuite_client_returned_record_ref;

testsuite_client_returned_record_ref testsuite_client_returned_record_new(int64_t record_id, djinni_string_ref content, djinni_string_ref misc);

int64_t testsuite_client_returned_record_get_record_id(testsuite_client_returned_record_ref instance);
void testsuite_client_returned_record_set_record_id(testsuite_client_returned_record_ref instance, int64_t value);

Expand Down
4 changes: 2 additions & 2 deletions test-suite/generated-src/c/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ extern "C" {
#endif // __cplusplus

/** This is a test */
enum testsuite_color {
typedef enum {
testsuite_color_RED = 0,
testsuite_color_ORANGE = 1,
testsuite_color_YELLOW = 2,
Expand All @@ -23,7 +23,7 @@ enum testsuite_color {
*/
testsuite_color_INDIGO = 5,
testsuite_color_VIOLET = 6,
};
} testsuite_color;
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
Expand Down
4 changes: 2 additions & 2 deletions test-suite/generated-src/c/constant_enum.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ extern "C" {
#endif // __cplusplus

/** enum for use in constants */
enum testsuite_constant_enum {
typedef enum {
testsuite_constant_enum_SOME_VALUE = 0,
testsuite_constant_enum_SOME_OTHER_VALUE = 1,
};
} testsuite_constant_enum;
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
Expand Down
5 changes: 5 additions & 0 deletions test-suite/generated-src/c/constant_interface_with_enum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@

#include "constant_interface_with_enum.h"
#include "djinni_c_translators.hpp"
#include "constant_enum.hpp"
#include "constant_interface_with_enum.hpp"

testsuite_constant_enum testsuite_constant_interface_with_enum_get_const_enum() {
return ::djinni::c_api::EnumTranslator<::testsuite::constant_enum, testsuite_constant_enum>::fromCpp(::testsuite::ConstantInterfaceWithEnum::CONST_ENUM);
}


3 changes: 3 additions & 0 deletions test-suite/generated-src/c/constant_interface_with_enum.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#pragma once

#include "djinni_c.h"
#include "constant_enum.h"

#ifdef __cplusplus
extern "C" {
Expand All @@ -12,6 +13,8 @@ extern "C" {
/** Interface containing enum constant */
typedef djinni_interface_ref testsuite_constant_interface_with_enum_ref;

testsuite_constant_enum testsuite_constant_interface_with_enum_get_const_enum();

#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
Expand Down
1 change: 1 addition & 0 deletions test-suite/generated-src/c/constant_record.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ extern "C" {
typedef djinni_record_ref testsuite_constant_record_ref;

testsuite_constant_record_ref testsuite_constant_record_new(int32_t some_integer, djinni_string_ref some_string);

int32_t testsuite_constant_record_get_some_integer(testsuite_constant_record_ref instance);
void testsuite_constant_record_set_some_integer(testsuite_constant_record_ref instance, int32_t value);

Expand Down
5 changes: 5 additions & 0 deletions test-suite/generated-src/c/constant_with_enum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@

#include "constant_with_enum.h"
#include "djinni_c_translators.hpp"
#include "constant_enum.hpp"
#include "constant_with_enum.hpp"

testsuite_constant_with_enum_ref testsuite_constant_with_enum_new()
{
return ::djinni::c_api::RecordTranslator<::testsuite::ConstantWithEnum>::make();
}

testsuite_constant_enum testsuite_constant_with_enum_get_const_enum() {
return ::djinni::c_api::EnumTranslator<::testsuite::constant_enum, testsuite_constant_enum>::fromCpp(::testsuite::ConstantWithEnum::CONST_ENUM);
}


4 changes: 4 additions & 0 deletions test-suite/generated-src/c/constant_with_enum.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#pragma once

#include "djinni_c.h"
#include "constant_enum.h"

#ifdef __cplusplus
extern "C" {
Expand All @@ -13,6 +14,9 @@ extern "C" {
typedef djinni_record_ref testsuite_constant_with_enum_ref;

testsuite_constant_with_enum_ref testsuite_constant_with_enum_new();

testsuite_constant_enum testsuite_constant_with_enum_get_const_enum();

#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
Expand Down
73 changes: 73 additions & 0 deletions test-suite/generated-src/c/constants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,84 @@

#include "constants.h"
#include "djinni_c_translators.hpp"
#include "constant_record.hpp"
#include "constants.hpp"

testsuite_constants_ref testsuite_constants_new()
{
return ::djinni::c_api::RecordTranslator<::testsuite::Constants>::make();
}

bool testsuite_constants_get_bool_constant() {
return ::testsuite::Constants::BOOL_CONSTANT;
}

int8_t testsuite_constants_get_i8_constant() {
return ::testsuite::Constants::I8_CONSTANT;
}

int16_t testsuite_constants_get_i16_constant() {
return ::testsuite::Constants::I16_CONSTANT;
}

int32_t testsuite_constants_get_i32_constant() {
return ::testsuite::Constants::I32_CONSTANT;
}

int64_t testsuite_constants_get_i64_constant() {
return ::testsuite::Constants::I64_CONSTANT;
}

float testsuite_constants_get_f32_constant() {
return ::testsuite::Constants::F32_CONSTANT;
}

double testsuite_constants_get_f64_constant() {
return ::testsuite::Constants::F64_CONSTANT;
}

djinni_optional_bool testsuite_constants_get_opt_bool_constant() {
return ::djinni::c_api::PrimitiveOptionalTranslator<std::experimental::optional<bool>, djinni_optional_bool>::fromCpp(::testsuite::Constants::OPT_BOOL_CONSTANT);
}

djinni_optional_int8 testsuite_constants_get_opt_i8_constant() {
return ::djinni::c_api::PrimitiveOptionalTranslator<std::experimental::optional<int8_t>, djinni_optional_int8>::fromCpp(::testsuite::Constants::OPT_I8_CONSTANT);
}

djinni_optional_int16 testsuite_constants_get_opt_i16_constant() {
return ::djinni::c_api::PrimitiveOptionalTranslator<std::experimental::optional<int16_t>, djinni_optional_int16>::fromCpp(::testsuite::Constants::OPT_I16_CONSTANT);
}

djinni_optional_int32 testsuite_constants_get_opt_i32_constant() {
return ::djinni::c_api::PrimitiveOptionalTranslator<std::experimental::optional<int32_t>, djinni_optional_int32>::fromCpp(::testsuite::Constants::OPT_I32_CONSTANT);
}

djinni_optional_int64 testsuite_constants_get_opt_i64_constant() {
return ::djinni::c_api::PrimitiveOptionalTranslator<std::experimental::optional<int64_t>, djinni_optional_int64>::fromCpp(::testsuite::Constants::OPT_I64_CONSTANT);
}

djinni_optional_float testsuite_constants_get_opt_f32_constant() {
return ::djinni::c_api::PrimitiveOptionalTranslator<std::experimental::optional<float>, djinni_optional_float>::fromCpp(::testsuite::Constants::OPT_F32_CONSTANT);
}

djinni_optional_double testsuite_constants_get_opt_f64_constant() {
return ::djinni::c_api::PrimitiveOptionalTranslator<std::experimental::optional<double>, djinni_optional_double>::fromCpp(::testsuite::Constants::OPT_F64_CONSTANT);
}

djinni_string_ref testsuite_constants_get_string_constant() {
return ::djinni::c_api::StringTranslator::fromCpp(::testsuite::Constants::STRING_CONSTANT);
}

djinni_string_ref testsuite_constants_get_opt_string_constant() {
return ::djinni::c_api::OptionalTranslator<std::experimental::optional<std::string>, ::djinni::c_api::StringTranslator>::fromCpp(::testsuite::Constants::OPT_STRING_CONSTANT);
}

testsuite_constant_record_ref testsuite_constants_get_object_constant() {
return ::djinni::c_api::RecordTranslator<::testsuite::ConstantRecord>::fromCpp(::testsuite::Constants::OBJECT_CONSTANT);
}

bool testsuite_constants_get_dummy() {
return ::testsuite::Constants::DUMMY;
}


Loading
Loading