190075Sobrien/* Functions dealing with attribute handling, used by most front ends. 290075Sobrien Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 3169689Skan 2002, 2003, 2004, 2005 Free Software Foundation, Inc. 490075Sobrien 590075SobrienThis file is part of GCC. 690075Sobrien 790075SobrienGCC is free software; you can redistribute it and/or modify it under 890075Sobrienthe terms of the GNU General Public License as published by the Free 990075SobrienSoftware Foundation; either version 2, or (at your option) any later 1090075Sobrienversion. 1190075Sobrien 1290075SobrienGCC is distributed in the hope that it will be useful, but WITHOUT ANY 1390075SobrienWARRANTY; without even the implied warranty of MERCHANTABILITY or 1490075SobrienFITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1590075Sobrienfor more details. 1690075Sobrien 1790075SobrienYou should have received a copy of the GNU General Public License 1890075Sobrienalong with GCC; see the file COPYING. If not, write to the Free 19169689SkanSoftware Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 20169689Skan02110-1301, USA. */ 2190075Sobrien 2290075Sobrien#include "config.h" 2390075Sobrien#include "system.h" 24132718Skan#include "coretypes.h" 25132718Skan#include "tm.h" 2690075Sobrien#include "tree.h" 2790075Sobrien#include "flags.h" 2890075Sobrien#include "toplev.h" 2990075Sobrien#include "output.h" 3090075Sobrien#include "rtl.h" 3190075Sobrien#include "ggc.h" 3290075Sobrien#include "tm_p.h" 3390075Sobrien#include "cpplib.h" 3490075Sobrien#include "target.h" 35117395Skan#include "langhooks.h" 3690075Sobrien 37132718Skanstatic void init_attributes (void); 3890075Sobrien 39117395Skan/* Table of the tables of attributes (common, language, format, machine) 4090075Sobrien searched. */ 4190075Sobrienstatic const struct attribute_spec *attribute_tables[4]; 4290075Sobrien 4390075Sobrienstatic bool attributes_initialized = false; 4490075Sobrien 4590075Sobrien/* Default empty table of attributes. */ 4690075Sobrienstatic const struct attribute_spec empty_attribute_table[] = 4790075Sobrien{ 4890075Sobrien { NULL, 0, 0, false, false, false, NULL } 4990075Sobrien}; 5090075Sobrien 5190075Sobrien/* Initialize attribute tables, and make some sanity checks 5290075Sobrien if --enable-checking. */ 5390075Sobrien 5490075Sobrienstatic void 55132718Skaninit_attributes (void) 5690075Sobrien{ 57117395Skan size_t i; 5890075Sobrien 59117395Skan attribute_tables[0] = lang_hooks.common_attribute_table; 60117395Skan attribute_tables[1] = lang_hooks.attribute_table; 61117395Skan attribute_tables[2] = lang_hooks.format_attribute_table; 6290075Sobrien attribute_tables[3] = targetm.attribute_table; 6390075Sobrien 64117395Skan /* Translate NULL pointers to pointers to the empty table. */ 65117395Skan for (i = 0; i < ARRAY_SIZE (attribute_tables); i++) 66117395Skan if (attribute_tables[i] == NULL) 67117395Skan attribute_tables[i] = empty_attribute_table; 68117395Skan 6990075Sobrien#ifdef ENABLE_CHECKING 7090075Sobrien /* Make some sanity checks on the attribute tables. */ 71117395Skan for (i = 0; i < ARRAY_SIZE (attribute_tables); i++) 7290075Sobrien { 7390075Sobrien int j; 7490075Sobrien 7590075Sobrien for (j = 0; attribute_tables[i][j].name != NULL; j++) 7690075Sobrien { 7790075Sobrien /* The name must not begin and end with __. */ 7890075Sobrien const char *name = attribute_tables[i][j].name; 7990075Sobrien int len = strlen (name); 80169689Skan 81169689Skan gcc_assert (!(name[0] == '_' && name[1] == '_' 82169689Skan && name[len - 1] == '_' && name[len - 2] == '_')); 83169689Skan 8490075Sobrien /* The minimum and maximum lengths must be consistent. */ 85169689Skan gcc_assert (attribute_tables[i][j].min_length >= 0); 86169689Skan 87169689Skan gcc_assert (attribute_tables[i][j].max_length == -1 88169689Skan || (attribute_tables[i][j].max_length 89169689Skan >= attribute_tables[i][j].min_length)); 90169689Skan 9190075Sobrien /* An attribute cannot require both a DECL and a TYPE. */ 92169689Skan gcc_assert (!attribute_tables[i][j].decl_required 93169689Skan || !attribute_tables[i][j].type_required); 94169689Skan 9590075Sobrien /* If an attribute requires a function type, in particular 9690075Sobrien it requires a type. */ 97169689Skan gcc_assert (!attribute_tables[i][j].function_type_required 98169689Skan || attribute_tables[i][j].type_required); 9990075Sobrien } 10090075Sobrien } 10190075Sobrien 10290075Sobrien /* Check that each name occurs just once in each table. */ 103117395Skan for (i = 0; i < ARRAY_SIZE (attribute_tables); i++) 10490075Sobrien { 10590075Sobrien int j, k; 10690075Sobrien for (j = 0; attribute_tables[i][j].name != NULL; j++) 10790075Sobrien for (k = j + 1; attribute_tables[i][k].name != NULL; k++) 108169689Skan gcc_assert (strcmp (attribute_tables[i][j].name, 109169689Skan attribute_tables[i][k].name)); 11090075Sobrien } 11190075Sobrien /* Check that no name occurs in more than one table. */ 112117395Skan for (i = 0; i < ARRAY_SIZE (attribute_tables); i++) 11390075Sobrien { 114117395Skan size_t j, k, l; 11590075Sobrien 116117395Skan for (j = i + 1; j < ARRAY_SIZE (attribute_tables); j++) 11790075Sobrien for (k = 0; attribute_tables[i][k].name != NULL; k++) 11890075Sobrien for (l = 0; attribute_tables[j][l].name != NULL; l++) 119169689Skan gcc_assert (strcmp (attribute_tables[i][k].name, 120169689Skan attribute_tables[j][l].name)); 12190075Sobrien } 12290075Sobrien#endif 12390075Sobrien 12490075Sobrien attributes_initialized = true; 12590075Sobrien} 12690075Sobrien 12790075Sobrien/* Process the attributes listed in ATTRIBUTES and install them in *NODE, 12890075Sobrien which is either a DECL (including a TYPE_DECL) or a TYPE. If a DECL, 12990075Sobrien it should be modified in place; if a TYPE, a copy should be created 13090075Sobrien unless ATTR_FLAG_TYPE_IN_PLACE is set in FLAGS. FLAGS gives further 13190075Sobrien information, in the form of a bitwise OR of flags in enum attribute_flags 13290075Sobrien from tree.h. Depending on these flags, some attributes may be 13390075Sobrien returned to be applied at a later stage (for example, to apply 134132718Skan a decl attribute to the declaration rather than to its type). */ 13590075Sobrien 13690075Sobrientree 137132718Skandecl_attributes (tree *node, tree attributes, int flags) 13890075Sobrien{ 13990075Sobrien tree a; 14090075Sobrien tree returned_attrs = NULL_TREE; 14190075Sobrien 14290075Sobrien if (!attributes_initialized) 14390075Sobrien init_attributes (); 14490075Sobrien 145169689Skan targetm.insert_attributes (*node, &attributes); 14690075Sobrien 14790075Sobrien for (a = attributes; a; a = TREE_CHAIN (a)) 14890075Sobrien { 14990075Sobrien tree name = TREE_PURPOSE (a); 15090075Sobrien tree args = TREE_VALUE (a); 15190075Sobrien tree *anode = node; 15290075Sobrien const struct attribute_spec *spec = NULL; 15390075Sobrien bool no_add_attrs = 0; 154132718Skan tree fn_ptr_tmp = NULL_TREE; 155117395Skan size_t i; 15690075Sobrien 157117395Skan for (i = 0; i < ARRAY_SIZE (attribute_tables); i++) 15890075Sobrien { 15990075Sobrien int j; 16090075Sobrien 16190075Sobrien for (j = 0; attribute_tables[i][j].name != NULL; j++) 16290075Sobrien { 16390075Sobrien if (is_attribute_p (attribute_tables[i][j].name, name)) 16490075Sobrien { 16590075Sobrien spec = &attribute_tables[i][j]; 16690075Sobrien break; 16790075Sobrien } 16890075Sobrien } 16990075Sobrien if (spec != NULL) 17090075Sobrien break; 17190075Sobrien } 17290075Sobrien 17390075Sobrien if (spec == NULL) 17490075Sobrien { 175169689Skan warning (OPT_Wattributes, "%qs attribute directive ignored", 17690075Sobrien IDENTIFIER_POINTER (name)); 17790075Sobrien continue; 17890075Sobrien } 17990075Sobrien else if (list_length (args) < spec->min_length 18090075Sobrien || (spec->max_length >= 0 18190075Sobrien && list_length (args) > spec->max_length)) 18290075Sobrien { 183169689Skan error ("wrong number of arguments specified for %qs attribute", 18490075Sobrien IDENTIFIER_POINTER (name)); 18590075Sobrien continue; 18690075Sobrien } 18790075Sobrien 18890075Sobrien if (spec->decl_required && !DECL_P (*anode)) 18990075Sobrien { 19090075Sobrien if (flags & ((int) ATTR_FLAG_DECL_NEXT 19190075Sobrien | (int) ATTR_FLAG_FUNCTION_NEXT 19290075Sobrien | (int) ATTR_FLAG_ARRAY_NEXT)) 19390075Sobrien { 19490075Sobrien /* Pass on this attribute to be tried again. */ 19590075Sobrien returned_attrs = tree_cons (name, args, returned_attrs); 19690075Sobrien continue; 19790075Sobrien } 19890075Sobrien else 19990075Sobrien { 200169689Skan warning (OPT_Wattributes, "%qs attribute does not apply to types", 20190075Sobrien IDENTIFIER_POINTER (name)); 20290075Sobrien continue; 20390075Sobrien } 20490075Sobrien } 20590075Sobrien 20690075Sobrien /* If we require a type, but were passed a decl, set up to make a 20790075Sobrien new type and update the one in the decl. ATTR_FLAG_TYPE_IN_PLACE 20890075Sobrien would have applied if we'd been passed a type, but we cannot modify 20990075Sobrien the decl's type in place here. */ 21090075Sobrien if (spec->type_required && DECL_P (*anode)) 21190075Sobrien { 21290075Sobrien anode = &TREE_TYPE (*anode); 21390075Sobrien flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE; 21490075Sobrien } 21590075Sobrien 21690075Sobrien if (spec->function_type_required && TREE_CODE (*anode) != FUNCTION_TYPE 21790075Sobrien && TREE_CODE (*anode) != METHOD_TYPE) 21890075Sobrien { 219261188Spfg /* APPLE LOCAL radar 6246527 */ 220261188Spfg if ((TREE_CODE (*anode) == POINTER_TYPE || TREE_CODE (*anode) == BLOCK_POINTER_TYPE) 22190075Sobrien && (TREE_CODE (TREE_TYPE (*anode)) == FUNCTION_TYPE 22290075Sobrien || TREE_CODE (TREE_TYPE (*anode)) == METHOD_TYPE)) 22390075Sobrien { 224132718Skan /* OK, this is a bit convoluted. We can't just make a copy 225132718Skan of the pointer type and modify its TREE_TYPE, because if 226132718Skan we change the attributes of the target type the pointer 227132718Skan type needs to have a different TYPE_MAIN_VARIANT. So we 228132718Skan pull out the target type now, frob it as appropriate, and 229132718Skan rebuild the pointer type later. 230132718Skan 231169689Skan This would all be simpler if attributes were part of the 232169689Skan declarator, grumble grumble. */ 233132718Skan fn_ptr_tmp = TREE_TYPE (*anode); 234132718Skan anode = &fn_ptr_tmp; 235132718Skan flags &= ~(int) ATTR_FLAG_TYPE_IN_PLACE; 23690075Sobrien } 23790075Sobrien else if (flags & (int) ATTR_FLAG_FUNCTION_NEXT) 23890075Sobrien { 23990075Sobrien /* Pass on this attribute to be tried again. */ 24090075Sobrien returned_attrs = tree_cons (name, args, returned_attrs); 24190075Sobrien continue; 24290075Sobrien } 24390075Sobrien 24490075Sobrien if (TREE_CODE (*anode) != FUNCTION_TYPE 24590075Sobrien && TREE_CODE (*anode) != METHOD_TYPE) 24690075Sobrien { 247169689Skan warning (OPT_Wattributes, 248169689Skan "%qs attribute only applies to function types", 24990075Sobrien IDENTIFIER_POINTER (name)); 25090075Sobrien continue; 25190075Sobrien } 25290075Sobrien } 25390075Sobrien 254169689Skan if (TYPE_P (*anode) 255169689Skan && (flags & (int) ATTR_FLAG_TYPE_IN_PLACE) 256169689Skan && TYPE_SIZE (*anode) != NULL_TREE) 257169689Skan { 258169689Skan warning (OPT_Wattributes, "type attributes ignored after type is already defined"); 259169689Skan continue; 260169689Skan } 261169689Skan 26290075Sobrien if (spec->handler != NULL) 26390075Sobrien returned_attrs = chainon ((*spec->handler) (anode, name, args, 26490075Sobrien flags, &no_add_attrs), 26590075Sobrien returned_attrs); 26690075Sobrien 26790075Sobrien /* Layout the decl in case anything changed. */ 26890075Sobrien if (spec->type_required && DECL_P (*node) 26996263Sobrien && (TREE_CODE (*node) == VAR_DECL 27096263Sobrien || TREE_CODE (*node) == PARM_DECL 27196263Sobrien || TREE_CODE (*node) == RESULT_DECL)) 272169689Skan relayout_decl (*node); 27390075Sobrien 27490075Sobrien if (!no_add_attrs) 27590075Sobrien { 27690075Sobrien tree old_attrs; 27790075Sobrien tree a; 27890075Sobrien 27990075Sobrien if (DECL_P (*anode)) 28090075Sobrien old_attrs = DECL_ATTRIBUTES (*anode); 28190075Sobrien else 28290075Sobrien old_attrs = TYPE_ATTRIBUTES (*anode); 28390075Sobrien 28490075Sobrien for (a = lookup_attribute (spec->name, old_attrs); 28590075Sobrien a != NULL_TREE; 28690075Sobrien a = lookup_attribute (spec->name, TREE_CHAIN (a))) 28790075Sobrien { 28890075Sobrien if (simple_cst_equal (TREE_VALUE (a), args) == 1) 28990075Sobrien break; 29090075Sobrien } 29190075Sobrien 29290075Sobrien if (a == NULL_TREE) 29390075Sobrien { 29490075Sobrien /* This attribute isn't already in the list. */ 29590075Sobrien if (DECL_P (*anode)) 29690075Sobrien DECL_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs); 29790075Sobrien else if (flags & (int) ATTR_FLAG_TYPE_IN_PLACE) 298169689Skan { 299169689Skan TYPE_ATTRIBUTES (*anode) = tree_cons (name, args, old_attrs); 300169689Skan /* If this is the main variant, also push the attributes 301169689Skan out to the other variants. */ 302169689Skan if (*anode == TYPE_MAIN_VARIANT (*anode)) 303169689Skan { 304169689Skan tree variant; 305169689Skan for (variant = *anode; variant; 306169689Skan variant = TYPE_NEXT_VARIANT (variant)) 307169689Skan { 308169689Skan if (TYPE_ATTRIBUTES (variant) == old_attrs) 309169689Skan TYPE_ATTRIBUTES (variant) 310169689Skan = TYPE_ATTRIBUTES (*anode); 311169689Skan else if (!lookup_attribute 312169689Skan (spec->name, TYPE_ATTRIBUTES (variant))) 313169689Skan TYPE_ATTRIBUTES (variant) = tree_cons 314169689Skan (name, args, TYPE_ATTRIBUTES (variant)); 315169689Skan } 316169689Skan } 317169689Skan } 31890075Sobrien else 31990075Sobrien *anode = build_type_attribute_variant (*anode, 32090075Sobrien tree_cons (name, args, 32190075Sobrien old_attrs)); 32290075Sobrien } 32390075Sobrien } 324132718Skan 325132718Skan if (fn_ptr_tmp) 326132718Skan { 327261188Spfg /* APPLE LOCAL begin radar 6246527 */ 328261188Spfg if (DECL_P (*node) && TREE_TYPE (*node) && 329261188Spfg TREE_CODE (TREE_TYPE (*node)) == BLOCK_POINTER_TYPE) 330261188Spfg /* Rebuild the block pointer type and put it in the 331261188Spfg appropriate place. */ 332261188Spfg fn_ptr_tmp = build_block_pointer_type (fn_ptr_tmp); 333261188Spfg else 334261188Spfg /* APPLE LOCAL end radar 6246527 */ 335132718Skan /* Rebuild the function pointer type and put it in the 336132718Skan appropriate place. */ 337132718Skan fn_ptr_tmp = build_pointer_type (fn_ptr_tmp); 338132718Skan if (DECL_P (*node)) 339132718Skan TREE_TYPE (*node) = fn_ptr_tmp; 340132718Skan else 34190075Sobrien { 342169689Skan gcc_assert (TREE_CODE (*node) == POINTER_TYPE); 343169689Skan *node = fn_ptr_tmp; 34490075Sobrien } 34590075Sobrien } 34690075Sobrien } 34790075Sobrien 348169689Skan return returned_attrs; 34990075Sobrien} 350