block: don't put spaces around :
[ironout.git] / name.c
blobadfb28afbc6a4834a6c064671854d36620ed76a2
1 #include <ctype.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "hash.h"
5 #include "name.h"
6 #include "utils.h"
8 struct name *name_init(struct node *node, int flags)
10 char *name = node->data;
11 struct name *result = xmalloc(sizeof(struct name));
12 result->name = name;
13 result->flags = flags;
14 return result;
17 void name_free(struct name *name)
19 free(name);
22 static int struct_node(struct node *node)
24 if (!node)
25 return 0;
26 switch (node->type) {
27 case AST_DIRDECL:
28 case AST_DECL:
29 case AST_DECLLIST:
30 case AST_DECLSPEC:
31 case AST_DECLSTMT:
32 return struct_node(node->parent);
33 case AST_STRUCTBITS:
34 case AST_STRUCTDECLLIST:
35 return 1;
36 default:
37 return 0;
41 int node_isfield(struct node *node)
43 struct node *cur = node->parent;
44 if (!cur)
45 return 0;
46 if ((cur->type == AST_GETATTR || cur->type == AST_DEREF)
47 && cur->children[1] == node)
48 return 1;
49 if (struct_node(cur))
50 return 1;
51 return 0;
54 static int node_insidedecl(struct node *node)
56 while (node) {
57 switch (node->type) {
58 case AST_DECL:
59 case AST_DIRDECL:
60 case AST_PARAMLIST:
61 break;
62 default:
63 return node->type != AST_FUNCTION;
65 node = node->parent;
67 return 1;
70 static int node_isparamdecl(struct node *node)
72 struct node *cur = node;
73 while (cur) {
74 switch (cur->type) {
75 case AST_DIRDECL:
76 case AST_DECL:
77 case AST_IDENTIFIER:
78 cur = cur->parent;
79 break;
80 case AST_IDLIST:
81 case AST_PARAMDECL:
82 return node_insidedecl(cur->parent);
83 default:
84 return 0;
87 return 0;
90 int guess_name_flags(struct node *node)
92 int flags = 0;
93 struct node *parent = node->parent;
94 if (parent) {
95 if (parent->type == AST_ENUM)
96 flags |= NAME_ENUM;
97 if (parent->type == AST_STRUCT) {
98 enum nodetype type = parent->children[0]->type;
99 if (type == AST_STRUCTKW)
100 flags |= NAME_STRUCT;
101 else
102 flags |= NAME_UNION;
104 if (parent->type == AST_LABELED ||
105 parent->type == AST_GOTO)
106 flags |= NAME_LABEL;
108 if (node_isfield(node))
109 flags |= NAME_FIELD;
110 if (node_isparamdecl(node))
111 flags |= NAME_PARAMDECL;
112 return flags;
115 int modifiers_match(struct name *name, int flags)
117 return !((name->flags ^ flags) & NAME_MOD_MASK);
120 static struct name *name_at(struct project *project,
121 struct cfile *cfile, long offset)
123 struct node *node;
124 struct block *block;
125 struct name *name;
126 int i;
127 node = node_find(cfile->node, offset);
128 if (!node)
129 return NULL;
130 block = block_find(cfile->block, offset);
131 if (!block)
132 return NULL;
133 name = block_lookup(block, node);
134 if (name)
135 return name;
136 for (i = 0; i < project->count; i++) {
137 struct cfile *cfile = project->files[i];
138 name = block_lookup(cfile->block, node);
139 if (name && !(name->flags & NAME_STATIC))
140 return name;
142 return NULL;
145 struct name *name_find(struct project *project,
146 struct cfile *cfile, char *location)
148 if (isdigit(location[0]))
149 return name_at(project, cfile, atoi(location));
150 return block_find_hier(cfile->block, location);