Re: constant expression analyzer and 'sizeof'

Nathaniel Smith <[email protected]>
Newsgroups gmane.comp.documentation.synopsis
Message-ID <[email protected]>
On Mon, Sep 27, 2004 at 09:06:18AM -0400, Stefan Seefeld wrote:
> However, some cases are not as simple, and so
> I'd appreciate feedback: the 'sizeof' operator
> can be used in const expressions, and so I have
> to deal with it. 
> AFAIK each compiler has its own memory
> layout, and thus there's no standard algorithm
> to determine the size of an arbitrary type.

So can 'offsetof', which is even more fun!

> On the other hand, there is no requirement for
> Synopsis / OpenC++ to be binary compatible with
> any other compiler, so may be this is a non-issue.
> 
> Still, I need to provide some numbers when asked
> for the size of an object. Any ideas how to approach
> this ? Any references are highly appreciated !

I guess my question is, what are you going to use these numbers for?
If it's just a question of providing better docs, I'd actually be
tempted to say that docs that said
  const int BUFFER_SIZE = PAGE_SIZE * 4 / sizeof(block_t)
     (= 16384/sizeof(block_t))
would be more useful than
  const int BUFFER_SIZE = PAGE_SIZE * 4 / sizeof(block_t)
     (= 2048)
since the former is actually how I think about things.  I don't really
_care_ how big my objects actually are, though I might be well be
curious how how the arithmetic I wrote involving things I do have
control over works out.  Especially if any expression involving sizeof
is going to end up reducing to a totally untrustworthy number, because
we don't actually know ABI details.  Especially since that would mean
we can never trust the evaluated expressions to have any relation to
reality, since we would never know for sure whether they had any
sizeof() embedded within some macro somewhere...

The only good alternative I see is to somehow parametrize Synopsis on
the ABI; have an object that knows how to compute object size and
layout for a particular compiler version on a particular architecture,
and let people swap in appropriate ones... ew.

So maybe instead of an 'evaluator', there should be a 'simplifier',
that attempts to recursively expressions as far as it can, but at the
end returns an expression rather than a number?

-- Nathaniel

-- 
Eternity is very long, especially towards the end.
  -- Woody Allen
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.