summaryrefslogtreecommitdiff
path: root/cparser/Cutil.ml
diff options
context:
space:
mode:
authorGravatar xleroy <xleroy@fca1b0fc-160b-0410-b1d3-a4f43f01ea2e>2014-03-28 08:20:14 +0000
committerGravatar xleroy <xleroy@fca1b0fc-160b-0410-b1d3-a4f43f01ea2e>2014-03-28 08:20:14 +0000
commitc677f108ff340c5bca67b428aa6e56b47f62da8c (patch)
treef75acecc7abe80cf06cfe01a938bdc56620137c6 /cparser/Cutil.ml
parentf37a87e35850e57febba0a39ce3cb526e7886c10 (diff)
C: Support array initializers that are too short + default init for remainder.
Elab: Handle C99 designated initializers. C2C, Initializers: more precise intermediate AST for initializers. git-svn-id: https://yquem.inria.fr/compcert/svn/compcert/trunk@2439 fca1b0fc-160b-0410-b1d3-a4f43f01ea2e
Diffstat (limited to 'cparser/Cutil.ml')
-rw-r--r--cparser/Cutil.ml28
1 files changed, 28 insertions, 0 deletions
diff --git a/cparser/Cutil.ml b/cparser/Cutil.ml
index 302c1cf..22ef187 100644
--- a/cparser/Cutil.ml
+++ b/cparser/Cutil.ml
@@ -840,4 +840,32 @@ let printloc oc (filename, lineno) =
let formatloc pp (filename, lineno) =
if filename <> "" then Format.fprintf pp "%s:%d: " filename lineno
+(* Generate the default initializer for the given type *)
+let rec default_init env ty =
+ match unroll env ty with
+ | TInt _ | TEnum _ ->
+ Init_single (intconst 0L IInt)
+ | TFloat(fk, _) ->
+ Init_single floatconst0
+ | TPtr(ty, _) ->
+ Init_single nullconst
+ | TArray(ty, sz, _) ->
+ Init_array []
+ | TStruct(id, _) ->
+ let rec default_init_fields = function
+ | [] -> []
+ | f1 :: fl ->
+ if f1.fld_name = ""
+ then default_init_fields fl
+ else (f1, default_init env f1.fld_typ) :: default_init_fields fl in
+ let ci = Env.find_struct env id in
+ Init_struct(id, default_init_fields ci.ci_members)
+ | TUnion(id, _) ->
+ let ci = Env.find_union env id in
+ begin match ci.ci_members with
+ | [] -> assert false
+ | fld :: _ -> Init_union(id, fld, default_init env fld.fld_typ)
+ end
+ | _ ->
+ assert false