GabrielDosReis / ipr

Compiler-neutral Internal Program Representation for C++
BSD 3-Clause "New" or "Revised" License
222 stars 23 forks source link

Attributes on types in a decl-specifier-seq #173

Closed GorNishanov closed 3 years ago

GorNishanov commented 3 years ago

In IPR, attributes can be applied to statements / declarations.

[[some-attr1]] int [[type-attr]] x [[decl-attr2]], y [[decl-attr3]];

When translated into an IPR,

declaration of x gets a merged set of attributes [[some-attr1]] [[decl-attr2]] declaration of y gets a merged set of attributes [[some-attr1]] [[decl-attr3]]

However, we do not have a place to put [[type-attr]] which according to the standard:

[dcl.spec]/1 ... The optional attribute-specifier-seq in a decl-specifier-seq appertains to the type determined by the preceding decl-specifiers (9.3.3). The attribute-specifier-seq affects the type only for the declaration it appears in, not other declarations involving the same type.

Possibly some wrapper type, something like attributed_type(type, attrs) might be added to handle this. [Edited: Terrible, terrible idea!]

GorNishanov commented 3 years ago

Upon further reflection, the distinction between an attribute applied to the type of the declaration of a particular declaration vs to the declaration ifself can be tackled in the attribute space.

An attribute wrapper TypeAttribute(Attribute) that can be applied to distinguish which of the attributes appertain to the type of the declaration as opposed to the declaration itself.

Therefore, in the example above, IPR representation could be

attributes of declaration of x: [[some-attr1]] [[decl-attr2]] TypeAttribute([[type-attr]])

I think I prefer the latter approach to the one mentioned in the initial post

GabrielDosReis commented 3 years ago

A few observations:

  1. CWG has not defined the meaning of "attributes appertaining to a type", and I don't know what that means
  2. One cannot create a new type out of existing ones by just sprinkling attributes on types
  3. How do those attributes behave in face of template argument deduction? In particular, how do they behave in terms of equality, e.g. std::same_as?

As things stand today, reading carefully the current descriptions (including the one quoted above), it sounds like those attributes are supposed to apply to the specific declaration (semantics, not grammar term) obtained from combining a decl-specifier-seq and a particular id-declarator. Consequently, I don't see any additional node or infrastructure needed in IPR to support those curiosities.

However, we do not have a place to put [[type-attr]] which according to the standard

They should go on the particular declaration node they affect.