Menu

#69 support non-standard declaration features like __declspec, __attribute__, verbatim

1.1.0
accepted
None
Functionality
minor
0.4.1
enhancement
2012-09-10
2012-09-01
No

did not think of anything else. But now, that I think of it, I wonder how
vendor-speciffic extensions are (or could be) supported, like Microsoft's
declspec? While they are not C++-standard I must use them when
implementing a DLL.

I can't support all compiler extension--where would I stop?? But I can and probably should support a few that very often used at least for MSVC and G++.

Discussion

  • Lorenzo Caminiti

    • status changed from new to accepted
    • milestone set to 1.1.0
     
  • Lorenzo Caminiti

    • summary changed from consider supporting non-standard declaration feature like MSVC __declspec to support non-standard declaration features like __declspec, __attribute__, verbatim
     
  • Lorenzo Caminiti

    I was thinking more on how to support compiler-specific declaration
    extensions. Supposedly, the lib will not do anything with these but to
    repeat them verbatim when the macros expand the underlining C++ code
    (because these extensions do not affect Contract Programming).
    Therefore, I was thinking that I could support a generic verbatim(...)
    identifier that can appear anywhere within the declarations and will
    specify tokens that the lib will simply repeat "as is" in the actual
    C++ declarations of the expanded macros. For example:

    CONTRACT_CLASS(
    verbatim(declspec(dllimport)) class (x)
    ) {
    CONTRACT_CLASS_INVARIANT( void )
    } varx;

    CONTRACT_CLASS(
    class verbatim(declspec(dllimport)) (x)
    ) {
    CONTRACT_CLASS_INVARIANT( void )
    };

    If possible, this could be used to support a number of different
    (all?) compiler extensions because the lib doesn't need to know
    anything about the content of a verbatim clasule but just that the
    specified tokens have to be repeated "in place and as they are" when
    expanding the macros.

    One difficulty with this is defining
    exactly what "in place" means. You'll
    need to specify how locations in your
    syntax map to locations in the C++
    declaration in all the places where
    the syntax is different. e.g.

    void (f)(int i, verbatim(extension) default 10)

    a) void f(int i extension = 1)
    b) void f(int i = extension 1)
    c) This is illegal

    In contrast if I implement the code below, it will only work for
    declspec and not for other extensions (because the lib needs to be
    aware of declspec and not just of verbatim here):

    CONTRACT_CLASS(
    declspec(dllimport) class (x)
    ) {
    CONTRACT_CLASS_INVARIANT( void )
    } varx;

    CONTRACT_CLASS(
    class declspec(dllimport) (x)
    ) {
    CONTRACT_CLASS_INVARIANT( void )
    };

    What do you think?

    I think both are good. You should implement
    important known compiler extensions like
    declspec and attribute directly and
    also provide verbatim to handle unknown
    or future compiler extensions.

     
  • Lorenzo Caminiti

    Btw, would it be legal for the syntax to use a specifier like
    attribute given that technically specifiers with double
    underscores are reserved? I'd think so because on compilers that
    support attribute it becomes like a keyword const, etc so I can
    use it in the syntax.

    Using attribute at all is an extension.
    In theory, the compiler could define it as
    a macro. I think it's treated as a keyword,
    but you should check to make sure.

    I think it's a keyword and not defined as a macro but that can't be
    guaranteed given that compilers are free to implement it as they wish.
    Maybe I should use declspec and attribute (no ) for those while
    verbatim will accept any as the compiler defines them.

     

Log in to post a comment.