From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-yw1-x1129.google.com (mail-yw1-x1129.google.com [IPv6:2607:f8b0:4864:20::1129]) by sourceware.org (Postfix) with ESMTPS id 9636E3858C78 for ; Fri, 12 May 2023 10:05:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9636E3858C78 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-yw1-x1129.google.com with SMTP id 00721157ae682-55a76ed088aso144513087b3.2 for ; Fri, 12 May 2023 03:05:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1683885917; x=1686477917; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=kozVRnxpZX4xQeVHXM4yIlww1g2lGDZ72Roj6QukJ5o=; b=JaiR3tWFvkibd8av6k3Cf4PgoFuYonscWtN9pUPodEld6J/MI+CjIixBRDdMMEYjTt I3c6sB356L9lv7LP0lVZ+t04kdD068KKIuWogH8NonW0KPeuSvmqMZqbtul7f2tfelse kEfqF4FXjLo4+fSzaYJBz48XY3WLBNv+60VO/mI50bF6lyHB1vtJbijAKbJabgLBHE20 dKsWUWB2jv7lfsaFA90SsDIZ7aCAYOmujAnGPunF56B/L5WvOZI6HyHzp8SyyoPI30q7 ssfnvPSr0rb8/lk1cPpTYgqsltYVEef4K+73bCHGTSWlfdELmXg/N5A3QE0wU7OtF7/w xOcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683885917; x=1686477917; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=kozVRnxpZX4xQeVHXM4yIlww1g2lGDZ72Roj6QukJ5o=; b=kH3q5xpIJzn2xR0Gmxw7Yugndk81QI5LUJ5e+v4Yp6z6yFu2ToWBM0Ih/+qY8jde/7 +X5KLZQdGCD+A8AloC3KpZ8aWHwfhxy+iX/ry0O268ouO0KM1p1KMo76NypEX3vjxwXi nj1Xn8S/VcbLQEYPsrKOLwGMFa0sQ+ol2f/5CiQiiZPx2/tm9OcBL+T9wxlYmRpbW6q5 MPo5MC6VSXdWLdZQmm8bnXUrPbaIA2/LGjjkgl7nWrtA0dG0WzWhpHxIbuXUb5Hk/sYG CLqCMQBDJNRYBDlKxQp4EZvvBrUoVXH6UlebkY1pRjV/HO5+IFutnzwdbN5toSDjJxnx Sk7A== X-Gm-Message-State: AC+VfDxC0eTPC21U3Rdz4lj9D3Rk1odyxE2hcdEa5uiiR8gxKuMGS4AE xToFxHqJeX2XAPHfWRTcD3fluKAwb8WYzDLpggpsWiDXXXbpzA== X-Google-Smtp-Source: ACHHUZ4QRUIrR1d1m4PngwnDmcguA7CKEiD1mT0SRjfHoWXI8eKGJtPBWuqiS7CxL6CNSOTn0bYYweFUTk6YO3hlNGg= X-Received: by 2002:a81:4a82:0:b0:54f:bb49:c3a2 with SMTP id x124-20020a814a82000000b0054fbb49c3a2mr15507077ywa.28.1683885917520; Fri, 12 May 2023 03:05:17 -0700 (PDT) MIME-Version: 1.0 References: <91752db5-b9db-b7e8-f1c6-f867aca2f791@gmail.com> In-Reply-To: <91752db5-b9db-b7e8-f1c6-f867aca2f791@gmail.com> From: Yair Lenga Date: Fri, 12 May 2023 06:05:06 -0400 Message-ID: Subject: Re: Wish: scoped enum To: GCC mailing list Cc: =?UTF-8?Q?Arsen_Arsenovi=C4=87?= , Gabriel Ravier Content-Type: multipart/alternative; boundary="000000000000f4c81c05fb7c3d36" X-Spam-Status: No, score=0.5 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: --000000000000f4c81c05fb7c3d36 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable rsen, Gabriel, Thanks for your feedback. I read through the n2347 article, and I believe that your feedback is to leverage the C++ 'enum struct' grammer. As you can tell, I'm not a C++, and and I did not know that the 'SCOPED enum' is already available for using 'enum class' or 'enum struct'. Leveraging the work done in C++, and keeping C/C++ compatible. Given that this is already implemented inside C++, looking for advice on the best path to implement it in gcc C ? I'm not familiar with the internals of C/C++, can the C++ behavior/grammar can be simply "enabled" (with a flag) for "C", or does it require reimplementation for the "C" front end. I have experience in "C" - does anyone know where in the source tree one should look to try to implement myself ? I know gcc is "layered" - shared backends, optimizer, code generation - so that change should be (in theory) only in the "C" front end. Once there is implementation - where should it get posted for feedback/review ? Thanks again for the useful feedback. Yair On Thu, May 11, 2023 at 8:15=E2=80=AFPM Gabriel Ravier wrote: > On 5/12/23 01:58, Yair Lenga via Gcc wrote: > > Hi, > > > > I wonder if it will be possible to add support for "scoped" enum to GCC. > > The current C standard has one name space for all enums, and different > name > > space for the members of each "struct". As a result, possible to say > > > > struct foo { int a } ; > > struct bar { double a }; // This is different 'a', different type > > > > But illegal to to (ignoring the conversion to use all upper for enum). > > > > enum a { u, v } ; > > enum b { v, w } ; // can not do this, as 'v' must be distin= ct > > > > One annoying side effect is that any package/module creating an enum has > to > > worry about namespace collision with everyone else in the world. Common > > practices include distinct prefixes, similar to the way different > libraries > > use distinct prefixes to avoid function name collision. This solution is > > far from perfect and leads to excessive long enum name. > > > > A reasonable wish list - add a magic keyword that will place the enums > into > > a scope, so that the following work: > > > > SCOPED enum shirt_sz { small, medium, large } ; > > SCOPED enum shoe_sz { small, medium, medium_wide, large, xlarge } ; > > > > enum shirt_sz tshift_size =3D shift_sz.medium ; > > enum shoe_siz boot_size =3D shoe_sz.xlarge ; > > > > Not perfect, but not complex, will make enum reusable across many > scenario, > > where they are currently hard to implement - because of namespace > conflict > > - between system header and user headers, between different packages. > > > > A smart compiler can also alert when "types" are mixed (assign value fr= om > > shift_sz to a variable of type shoe_sz). Not critical - as my > understanding > > is that this is not enforced today. For the case that an enum symbol is > > distinct (in the current compilation unit), the compiler can allow using > it > > without the namespace - practically falling back into current behavior. > > > > Feedback ? Anyone know how to get a prototype into gcc ? How one get > > approval for such "extensions". > > > > Yair > > You may wish to learn about > https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2347.pdf - > although of course this isn't available in C right now, but the addition > of a differing, syntactically incompatible while substantially > overlapping feature to C to reproduce the same functionality as `enum > class` seems far more unlikely to occur than the addition of `enum > class` to C. > > --000000000000f4c81c05fb7c3d36--