binpac: Arrays now suport the &transient attribute.

If set, parsed elements won't actually be added to the array, and read
access to the array aren't permitted. This is helpful to save memory
in the case of large arrays for which elements don't need (or can't)
be buffered.
This commit is contained in:
Robin Sommer 2011-09-22 21:19:16 -07:00 committed by Tim Wojtulewicz
parent 5db7ba4050
commit 092d049f8e

View file

@ -92,6 +92,9 @@ Type *ArrayType::ElementDataType() const
string ArrayType::EvalElement(const string &array, const string &index) const string ArrayType::EvalElement(const string &array, const string &index) const
{ {
if ( attr_transient_ )
throw Exception(this, "cannot access element in &transient array");
return strfmt("(*(%s))[%s]", array.c_str(), index.c_str()); return strfmt("(*(%s))[%s]", array.c_str(), index.c_str());
} }
@ -313,6 +316,9 @@ void ArrayType::GenPubDecls(Output *out_h, Env *env)
if ( declared_as_type() ) if ( declared_as_type() )
{ {
if ( attr_transient_ )
throw Exception(this, "cannot access element in &transient array");
out_h->println("int size() const { return %s ? %s->size() : 0; }", out_h->println("int size() const { return %s ? %s->size() : 0; }",
env->RValue(value_var()), env->RValue(value_var()),
env->RValue(value_var())); env->RValue(value_var()));
@ -418,6 +424,13 @@ string ArrayType::GenArrayInit(Output *out_cc, Env *env, bool known_array_length
void ArrayType::GenElementAssignment(Output *out_cc, Env *env, void ArrayType::GenElementAssignment(Output *out_cc, Env *env,
string const &array_str, bool use_vector) string const &array_str, bool use_vector)
{ {
if ( attr_transient_ )
{
// Just discard.
out_cc->println("delete %s;", env->LValue(elem_var()));
return;
}
// Assign the element // Assign the element
if ( ! use_vector ) if ( ! use_vector )
{ {