Skip to content

Commit

Permalink
feat: Add support for bitwise attribute for strong typedefs.
Browse files Browse the repository at this point in the history
The `sparse` tool can validate these.
  • Loading branch information
iphydf committed Feb 3, 2024
1 parent 61daedb commit f9157a1
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/Language/Cimple/Ast.hs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ data NodeF lexeme a
| Struct lexeme [a]
| Union lexeme [a]
| MemberDecl a (Maybe lexeme)
| TyBitwise a
| TyForce a
| TyConst a
| TyPointer a
| TyStruct lexeme
Expand Down
2 changes: 2 additions & 0 deletions src/Language/Cimple/Lexer.x
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ tokens :-
<0> "#define" { mkL PpDefine `andBegin` ppSC }
<0> "#undef" { mkL PpUndef }
<0> "#include" { mkL PpInclude }
<0,ppSC> "bitwise" { mkL KwBitwise }
<0,ppSC> "break" { mkL KwBreak }
<0,ppSC> "case" { mkL KwCase }
<0,ppSC> "const" { mkL KwConst }
Expand All @@ -161,6 +162,7 @@ tokens :-
<0,ppSC> "enum" { mkL KwEnum }
<0,ppSC> "extern" { mkL KwExtern }
<0,ppSC> "for" { mkL KwFor }
<0,ppSC> "force" { mkL KwForce }
<0,ppSC> "goto" { mkL KwGoto }
<0,ppSC> "if" { mkL KwIf }
<0,ppSC> "non_null" { mkL KwNonNull }
Expand Down
4 changes: 4 additions & 0 deletions src/Language/Cimple/MapAst.hs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,10 @@ instance MapAst itext otext (Node (Lexeme itext)) where
Fix <$> (Union <$> recurse name <*> recurse members)
MemberDecl decl bits ->
Fix <$> (MemberDecl <$> recurse decl <*> recurse bits)
TyBitwise ty ->
Fix <$> (TyBitwise <$> recurse ty)
TyForce ty ->
Fix <$> (TyForce <$> recurse ty)
TyConst ty ->
Fix <$> (TyConst <$> recurse ty)
TyPointer ty ->
Expand Down
6 changes: 5 additions & 1 deletion src/Language/Cimple/Parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import Language.Cimple.Tokens (LexemeClass (..))
ID_STD_TYPE { L _ IdStdType _ }
ID_SUE_TYPE { L _ IdSueType _ }
ID_VAR { L _ IdVar _ }
bitwise { L _ KwBitwise _ }
break { L _ KwBreak _ }
case { L _ KwCase _ }
const { L _ KwConst _ }
Expand All @@ -53,6 +54,7 @@ import Language.Cimple.Tokens (LexemeClass (..))
enum { L _ KwEnum _ }
extern { L _ KwExtern _ }
for { L _ KwFor _ }
force { L _ KwForce _ }
GNU_PRINTF { L _ KwGnuPrintf _ }
goto { L _ KwGoto _ }
if { L _ KwIf _ }
Expand Down Expand Up @@ -666,7 +668,9 @@ TypedefDecl

QualType :: { NonTerm }
QualType
: LeafType { $1 }
: bitwise ID_STD_TYPE { Fix (TyBitwise (Fix (TyStd $2))) }
| force LeafType { Fix (TyForce $2) }
| LeafType { $1 }
| LeafType '*' { tyPointer $1 }
| LeafType '*' '*' { tyPointer (tyPointer $1) }
| LeafType '*' '*' const { tyConst (tyPointer (tyPointer $1)) }
Expand Down
4 changes: 4 additions & 0 deletions src/Language/Cimple/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import Text.PrettyPrint.ANSI.Leijen
indentWidth :: Int
indentWidth = 2

kwBitwise = dullgreen $ text "bitwise"
kwBreak = dullred $ text "break"
kwCase = dullred $ text "case"
kwConst = dullgreen $ text "const"
Expand All @@ -37,6 +38,7 @@ kwElse = dullred $ text "else"
kwEnum = dullgreen $ text "enum"
kwExtern = dullgreen $ text "extern"
kwFor = dullred $ text "for"
kwForce = dullgreen $ text "force"
kwGnuPrintf = dullgreen $ text "GNU_PRINTF"
kwGoto = dullred $ text "goto"
kwIf = dullred $ text "if"
Expand Down Expand Up @@ -402,6 +404,8 @@ ppNode = foldFix go
DeclSpecArray Nothing -> text "[]"
DeclSpecArray (Just dim) -> brackets dim

TyBitwise ty -> kwBitwise <+> ty
TyForce ty -> kwForce <+> ty
TyPointer ty -> ty <> char '*'
TyConst ty -> ty <+> kwConst
TyUserDefined l -> dullgreen $ ppLexeme l
Expand Down
2 changes: 2 additions & 0 deletions src/Language/Cimple/Tokens.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ data LexemeClass
| IdStdType
| IdSueType
| IdVar
| KwBitwise
| KwBreak
| KwCase
| KwConst
Expand All @@ -23,6 +24,7 @@ data LexemeClass
| KwEnum
| KwExtern
| KwFor
| KwForce
| KwGnuPrintf
| KwGoto
| KwIf
Expand Down
4 changes: 4 additions & 0 deletions src/Language/Cimple/TraverseAst.hs
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,10 @@ instance TraverseAst text (Node (Lexeme text)) where
_ <- recurse decl
_ <- recurse bits
pure ()
TyBitwise ty ->
recurse ty
TyForce ty ->
recurse ty
TyConst ty ->
recurse ty
TyPointer ty ->
Expand Down

0 comments on commit f9157a1

Please sign in to comment.