This repository has been archived by the owner on Mar 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 488
Generating constexpr for constants when possible #282
Open
itsff
wants to merge
1
commit into
dropbox:master
Choose a base branch
from
itsff:feature/cpp_constexpr
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -91,11 +91,34 @@ class CppGenerator(spec: Spec) extends Generator(spec) { | |
} | ||
|
||
def generateHppConstants(w: IndentWriter, consts: Seq[Const]) = { | ||
var needsCppGen = false | ||
|
||
for (c <- consts) { | ||
var isConstExpr = true | ||
val constValue = c.value match { | ||
case l: Long => " = { " + l.toString + " };" | ||
case d: Double if marshal.fieldType(c.ty) == "float" => " = { " + d.toString + "f };" | ||
case d: Double => " = { " + d.toString + " };" | ||
case b: Boolean => if (b) " = { true };" else " = { false };" | ||
case e: EnumValue => " = " + marshal.typename(c.ty) + "::" + idCpp.enum(e.ty.name + "_" + e.name) + ";" | ||
case _ => { | ||
needsCppGen = true; | ||
isConstExpr = false; | ||
";" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd suggest making constValue empty here, and applying the semi-colon in the final statement below. The semicolon isn't really part of the const value. |
||
} | ||
} | ||
|
||
w.wl | ||
writeDoc(w, c.doc) | ||
w.wl(s"static ${marshal.fieldType(c.ty)} const ${idCpp.const(c.ident)};") | ||
w.w(s"static ${if (isConstExpr) "constexpr" else "" } const ${marshal.fieldType(c.ty)} ${idCpp.const(c.ident)}${constValue}") | ||
} | ||
|
||
if (!consts.isEmpty) { | ||
w.wl | ||
w.wl | ||
} | ||
|
||
needsCppGen | ||
} | ||
|
||
def generateCppConstants(w: IndentWriter, consts: Seq[Const], selfName: String) = { | ||
|
@@ -127,10 +150,20 @@ class CppGenerator(spec: Spec) extends Generator(spec) { | |
|
||
val skipFirst = SkipFirst() | ||
for (c <- consts) { | ||
skipFirst{ w.wl } | ||
w.w(s"${marshal.fieldType(c.ty)} const $selfName::${idCpp.const(c.ident)} = ") | ||
writeCppConst(w, c.ty, c.value) | ||
w.wl(";") | ||
val isConstExpr = c.value match { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice to unify the cases for this match with the ones above in the header file. One option would be to make a helper function which returns the constValue used in the header file, then have isConstExpr here simply test whether that value is non-empty. |
||
case l: Long => true | ||
case d: Double => true | ||
case b: Boolean => true | ||
case e: EnumValue => true | ||
case _ => false | ||
} | ||
|
||
if (!isConstExpr) { | ||
skipFirst{ w.wl } | ||
w.w(s"${marshal.fieldType(c.ty)} const $selfName::${idCpp.const(c.ident)} = ") | ||
writeCppConst(w, c.ty, c.value) | ||
w.wl(";") | ||
} | ||
} | ||
} | ||
|
||
|
@@ -149,6 +182,8 @@ class CppGenerator(spec: Spec) extends Generator(spec) { | |
refs.cpp.add("#include "+q(spec.cppExtendedRecordIncludePrefix + spec.cppFileIdentStyle(ident) + "." + spec.cppHeaderExt)) | ||
} | ||
|
||
var constantsRequireCpp = false; | ||
|
||
// C++ Header | ||
def writeCppPrototype(w: IndentWriter) { | ||
if (r.ext.cpp) { | ||
|
@@ -159,7 +194,7 @@ class CppGenerator(spec: Spec) extends Generator(spec) { | |
writeDoc(w, doc) | ||
writeCppTypeParams(w, params) | ||
w.w("struct " + actualSelf + cppFinal).bracedSemi { | ||
generateHppConstants(w, r.consts) | ||
constantsRequireCpp = generateHppConstants(w, r.consts) | ||
// Field definitions. | ||
for (f <- r.fields) { | ||
writeDoc(w, f.doc) | ||
|
@@ -210,7 +245,7 @@ class CppGenerator(spec: Spec) extends Generator(spec) { | |
|
||
writeHppFile(cppName, origin, refs.hpp, refs.hppFwds, writeCppPrototype) | ||
|
||
if (r.consts.nonEmpty || r.derivingTypes.nonEmpty) { | ||
if (constantsRequireCpp || r.derivingTypes.nonEmpty) { | ||
writeCppFile(cppName, origin, refs.cpp, w => { | ||
generateCppConstants(w, r.consts, actualSelf) | ||
|
||
|
@@ -274,6 +309,7 @@ class CppGenerator(spec: Spec) extends Generator(spec) { | |
|
||
val self = marshal.typename(ident, i) | ||
val methodNamesInScope = i.methods.map(m => idCpp.method(m.ident)) | ||
var generateCppFile = false; | ||
|
||
writeHppFile(ident, origin, refs.hpp, refs.hppFwds, w => { | ||
writeDoc(w, doc) | ||
|
@@ -283,7 +319,7 @@ class CppGenerator(spec: Spec) extends Generator(spec) { | |
// Destructor | ||
w.wl(s"virtual ~$self() {}") | ||
// Constants | ||
generateHppConstants(w, i.consts) | ||
generateCppFile = generateHppConstants(w, i.consts) | ||
// Methods | ||
for (m <- i.methods) { | ||
w.wl | ||
|
@@ -301,7 +337,7 @@ class CppGenerator(spec: Spec) extends Generator(spec) { | |
}) | ||
|
||
// Cpp only generated in need of Constants | ||
if (i.consts.nonEmpty) { | ||
if (generateCppFile) { | ||
writeCppFile(ident, origin, refs.cpp, w => { | ||
generateCppConstants(w, i.consts, self) | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be clearer to match on c.ty instead. I'm not set on that, since this does make the code simpler, but it took me a while to figure out that this distinction is the reason that optionals are being treated as constexpr-eligible. It's not an explicit case statement anywhere, but simply implicit in the fact that the constant value for an optional is of type T.