-
Notifications
You must be signed in to change notification settings - Fork 58
Implement Type Alias codegen #645
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 14 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
6261ee2
Implement Type Alias codegen
Kronos3 53ab5b6
Update array test ref
Kronos3 b92273d
Add extern, handle builtin types
Kronos3 54d7170
Compare against string type properly
Kronos3 bc0854e
Fix getAutocodeFiles()
Kronos3 bcaf7e9
Clean up other tools
Kronos3 f24fe08
Merge remote-tracking branch 'origin/main' into issue-622-type-alias-…
Kronos3 07b23f7
Update check-cpp
bocchino a0e69ff
Move extern C to HPP
Kronos3 10c9c59
Merge remote-tracking branch 'origin/main' into issue-622-type-alias-…
Kronos3 7ea5712
Fix string type writer and add some tests
Kronos3 3dae6db
Add some test cases in component code
Kronos3 d2f28d1
Fix alias type finalization & make CPP files build
Kronos3 46cc26c
Fix clean
Kronos3 0804409
Use FpConfig instead of FppConfig, update tests to generate .cpp file…
Kronos3 e132e4f
Update refs
Kronos3 305bf51
Put A back in place of AA and expand recursive array alias tests
Kronos3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
185 changes: 185 additions & 0 deletions
185
compiler/lib/src/main/scala/codegen/CppWriter/AliasCppWriter.scala
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| package fpp.compiler.codegen | ||
|
|
||
| import fpp.compiler.analysis._ | ||
| import fpp.compiler.ast._ | ||
| import fpp.compiler.util._ | ||
|
|
||
| /** Writes out C++/C headers for type alias definitions */ | ||
| case class AliasCppWriter ( | ||
| s: CppWriterState, | ||
| aNode: Ast.Annotated[AstNode[Ast.DefAliasType]] | ||
| ) extends CppWriterUtils { | ||
|
|
||
| private val node = aNode._2 | ||
|
|
||
| private val data = node.data | ||
|
|
||
| private val symbol = Symbol.AliasType(aNode) | ||
|
|
||
| private val name = s.getName(symbol) | ||
|
|
||
| private val fileName = ComputeCppFiles.FileNames.getAliasType(name) | ||
|
|
||
| private val aliasType @ Type.AliasType(_, _) = s.a.typeMap(node.id) | ||
|
|
||
| private val namespaceIdentList = s.getNamespaceIdentList(symbol) | ||
|
|
||
| private val typeCppWriter = TypeCppWriter(s, "Fw::String") | ||
|
|
||
| private val supportedCHeader = s.isTypeSupportedInC(aliasType) | ||
|
|
||
| private def writeHppIncludeDirectives( | ||
| s: CppWriterState, | ||
| aNode: Ast.Annotated[AstNode[Ast.DefAliasType]] | ||
| ): List[String] = { | ||
| val Right(a) = UsedSymbols.defAliasTypeAnnotatedNode(s.a, aNode) | ||
| s.writeIncludeDirectives(a.usedSymbolSet) | ||
| } | ||
|
|
||
| private def writeHIncludeDirectives( | ||
| s: CppWriterState, | ||
| aNode: Ast.Annotated[AstNode[Ast.DefAliasType]] | ||
| ): List[String] = { | ||
| val Right(a) = UsedSymbols.defAliasTypeAnnotatedNode(s.a, aNode) | ||
| def getIncludeFiles(sym: Symbol): Option[String] = { | ||
| val name = s.getName(sym) | ||
| for { | ||
| fileName <- sym match { | ||
| case Symbol.AliasType(_) => Some(ComputeCppFiles.FileNames.getAliasType(name)) | ||
| case Symbol.AbsType(node) => | ||
| if s.isBuiltInType(name) then Some("FppConfig") else None | ||
| case _ => None | ||
| } | ||
| } | ||
| yield s.getIncludePath(sym, fileName, "h") | ||
| } | ||
|
|
||
| a.usedSymbolSet.map(getIncludeFiles).filter(_.isDefined).map(_.get).map(CppWriterState.headerString).toList | ||
| } | ||
|
|
||
| def writeHpp: CppDoc = { | ||
| val includeGuard = s.includeGuardFromQualifiedName(symbol, fileName, "HPP") | ||
| CppWriter.createCppDoc( | ||
| s"$name alias", | ||
| fileName, | ||
| includeGuard, | ||
| getHppMembers, | ||
| s.toolName, | ||
| "hpp" | ||
| ) | ||
| } | ||
|
|
||
| def writeH: Option[CppDoc] = { | ||
| if (!supportedCHeader) { | ||
| return None | ||
| } | ||
|
|
||
| val includeGuard = s.includeGuardFromQualifiedName(symbol, fileName, "H") | ||
| Some(CppWriter.createCppDoc( | ||
| s"$name alias", | ||
| fileName, | ||
| includeGuard, | ||
| getHMembers, | ||
| s.toolName, | ||
| "h" | ||
| )) | ||
| } | ||
|
|
||
| private def getHppMembers: List[CppDoc.Member] = { | ||
| List.concat( | ||
| List(getHppIncludes), | ||
| wrapInNamespaces(namespaceIdentList, List(getHppDefinition)) | ||
| ) | ||
| } | ||
|
|
||
| private def getHMembers: List[CppDoc.Member] = { | ||
| List( | ||
| getHIncludes, | ||
| getHDefinition, | ||
| ) | ||
| } | ||
|
|
||
| private def getHPPCPPGuard: CppDoc.Member = { | ||
| linesMember(addBlankPrefix(lines( | ||
| """extern "C" {""") | ||
| )) | ||
| } | ||
|
|
||
| private def getHPPCloseCPPGuard: CppDoc.Member = { | ||
| linesMember(addBlankPrefix(lines( | ||
| """}""") | ||
| )) | ||
| } | ||
|
|
||
| private def getHIncludes: CppDoc.Member = { | ||
| if (!supportedCHeader) { | ||
| // C header is not supported, we will generate the definition | ||
| // in the C++ header | ||
| return linesMember(List()) | ||
| } | ||
|
|
||
| val standardHeaders = List( | ||
| "Fw/Types/BasicTypes.h", | ||
| ).map(CppWriter.headerString) | ||
| val symbolHeaders = writeHIncludeDirectives(s, aNode) | ||
| val headers = standardHeaders ++ symbolHeaders | ||
| linesMember(addBlankPrefix(headers.distinct.sorted.map(line))) | ||
| } | ||
|
|
||
| private def getHppIncludes: CppDoc.Member.Lines = { | ||
| val standardHeaders = List( | ||
| aliasType.aliasType match { | ||
| case Type.String(_) => "Fw/Types/String.hpp" | ||
| case Type.AbsType(node) => | ||
| s.isBuiltInType(node._2._1.name) match { | ||
| case true => "FppConfig.hpp" | ||
|
Kronos3 marked this conversation as resolved.
Outdated
|
||
| case false => "Fw/Types/BasicTypes.h" | ||
| } | ||
| case _ => "Fw/Types/BasicTypes.h" | ||
| }, | ||
| ).map(CppWriter.headerString) | ||
| val symbolHeaders = writeHppIncludeDirectives(s, aNode) | ||
| val headers = standardHeaders ++ symbolHeaders | ||
| linesMember(addBlankPrefix(headers.distinct.sorted.map(line))) | ||
| } | ||
|
|
||
| private def getHppDefinition: CppDoc.Member.Lines = { | ||
| val name = s.getName(symbol) | ||
|
|
||
| supportedCHeader match { | ||
| case true => linesMember( | ||
| // Include the C definition of the type alias | ||
| // This is using a `typedef` | ||
| addBlankPrefix(lines( | ||
| s"""|extern "C" { | ||
| |${CppWriterState.headerString(s.getIncludePath(symbol, ComputeCppFiles.FileNames.getAliasType(name), "h"))} | ||
| |}""") | ||
| )) | ||
| case false => linesMember(addBlankPrefix( | ||
| // Define a C++ only | ||
| AnnotationCppWriter.writePreComment(aNode) ++ lines( | ||
| s"using $name = ${typeCppWriter.write(aliasType.aliasType)};" | ||
| ) | ||
| )) | ||
| } | ||
| } | ||
|
|
||
| private def getHDefinition: CppDoc.Member.Lines = { | ||
| val name = s.getName(symbol) | ||
| def getTypePRI(ty: Type): String = { | ||
| ty match { | ||
| case Type.Float(f) => aliasType.aliasType.toString().toLowerCase() | ||
| case Type.PrimitiveInt(i) => aliasType.aliasType.toString().toLowerCase() | ||
| case _ => typeCppWriter.write(ty) | ||
| } | ||
| } | ||
|
|
||
| val fmtSpec = getTypePRI(aliasType.aliasType) | ||
|
|
||
| linesMember(addBlankPrefix( | ||
| AnnotationCppWriter.writePreComment(aNode) ++ lines( | ||
| s"""|typedef ${typeCppWriter.write(aliasType.aliasType)} $name; | ||
| |#define PRI_$name PRI_${fmtSpec}""") | ||
| )) | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.