File: CompField.cc

package info (click to toggle)
eclipse-titan 6.1.0-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 79,084 kB
  • ctags: 29,092
  • sloc: cpp: 210,764; ansic: 44,862; yacc: 21,034; sh: 12,594; makefile: 12,225; lex: 8,972; xml: 5,348; java: 4,849; perl: 3,780; python: 2,834; php: 175
file content (210 lines) | stat: -rw-r--r-- 5,412 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/******************************************************************************
 * Copyright (c) 2000-2016 Ericsson Telecom AB
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Balasko, Jeno
 *   Raduly, Csaba
 *
 ******************************************************************************/
#include "CompField.hh"
#include "Type.hh"
#include "Value.hh"
#include "CompilerError.hh"

namespace Common {

// =================================
// ===== CompField
// =================================

CompField::CompField(Identifier *p_name, Type *p_type, bool p_is_optional,
  Value *p_defval)
  : Node(), Location(), name(p_name), type(p_type),
    is_optional(p_is_optional), defval(p_defval), rawattrib(0)
{
  if(!p_name || !p_type)
    FATAL_ERROR("NULL parameter: Common::CompField::CompField()");
  type->set_ownertype(Type::OT_COMP_FIELD, this);
}

CompField::CompField(const CompField& p)
  : Node(p), Location(p), is_optional(p.is_optional), rawattrib(0)
{
  name=p.name->clone();
  type=p.type->clone();
  type->set_ownertype(Type::OT_COMP_FIELD, this);
  defval=p.defval?p.defval->clone():0;
}

CompField::~CompField()
{
  delete name;
  delete type;
  delete defval;
  delete rawattrib;
}

CompField *CompField::clone() const
{
  return new CompField(*this);
}

void CompField::set_fullname(const string& p_fullname)
{
  string base_name(p_fullname + "." + name->get_dispname());
  Node::set_fullname(base_name);
  type->set_fullname(base_name);
  if (defval) defval->set_fullname(base_name + ".<defval>");
}

void CompField::set_my_scope(Scope *p_scope)
{
  type->set_my_scope(p_scope);
  if (defval) defval->set_my_scope(p_scope);
}

void CompField::set_raw_attrib(RawAST* r_attr)
{
  delete rawattrib;
  rawattrib=r_attr;
}

void CompField::dump(unsigned level) const
{
  name->dump(level);
  type->dump(level + 1);
  if(is_optional)
    DEBUG(level + 1, "optional");
  if(defval) {
    DEBUG(level + 1, "with default value");
    defval->dump(level + 2);
  }
}

// =================================
// ===== CompFieldMap
// =================================

CompFieldMap::CompFieldMap(const CompFieldMap& p)
  : Node(p), my_type(0), checked(false)
{
  size_t nof_comps = p.v.size();
  for (size_t i = 0; i < nof_comps; i++) v.add(p.v[i]->clone());
}

CompFieldMap::~CompFieldMap()
{
  size_t nof_comps = v.size();
  for (size_t i = 0; i < nof_comps; i++) delete v[i];
  v.clear();
  m.clear();
}

CompFieldMap *CompFieldMap::clone() const
{
  return new CompFieldMap(*this);
}

void CompFieldMap::set_fullname(const string& p_fullname)
{
  Node::set_fullname(p_fullname);
  size_t nof_comps = v.size();
  for (size_t i = 0; i < nof_comps; i++) v[i]->set_fullname(p_fullname);
}

void CompFieldMap::set_my_scope(Scope *p_scope)
{
  size_t nof_comps = v.size();
  for (size_t i = 0; i < nof_comps; i++) v[i]->set_my_scope(p_scope);
}

void CompFieldMap::add_comp(CompField *comp)
{
  v.add(comp);
  if (checked) {
    const string& name = comp->get_name().get_name();
    if (m.has_key(name)) FATAL_ERROR("CompFieldMap::add_comp(%s)", name.c_str());
    m.add(name, comp);
  }
}

bool CompFieldMap::has_comp_withName(const Identifier& p_name)
{
  if (!checked) chk_uniq();
  return m.has_key(p_name.get_name());
}

CompField* CompFieldMap::get_comp_byName(const Identifier& p_name)
{
  if (!checked) chk_uniq();
  return m[p_name.get_name()];
}

const char *CompFieldMap::get_typetype_name() const
{
  if (!my_type) FATAL_ERROR("CompFieldMap::get_typetype_name()");
  switch (my_type->get_typetype()) {
  case Type::T_ANYTYPE:
    return "anytype";
  case Type::T_CHOICE_T:
    return "union";
  case Type::T_SEQ_T:
    return "record";
  case Type::T_SET_T:
    return "set";
  case Type::T_OPENTYPE:
    return "open type";
  default:
    return "<unknown>";
  }
}

void CompFieldMap::chk_uniq()
{
  if (checked) return;
  const char *typetype_name = get_typetype_name();
  size_t nof_comps = v.size();
  for (size_t i = 0; i < nof_comps; i++) {
    CompField *comp = v[i];
    const Identifier& id = comp->get_name();
    const string& name = id.get_name();
    if (m.has_key(name)) {
      const char *dispname = id.get_dispname().c_str();
      comp->error("Duplicate %s field name `%s'", typetype_name, dispname);
      m[name]->note("Field `%s' is already defined here", dispname);
    } else m.add(name, comp);
  }
  checked = true;
}

void CompFieldMap::chk()
{
  if (!checked) chk_uniq();
  const char *typetype_name = get_typetype_name();
  size_t nof_comps = v.size();
  for (size_t i = 0; i < nof_comps; i++) {
    CompField *comp = v[i];
    const Identifier& id = comp->get_name();
    Error_Context cntxt(comp, "In %s field `%s'", typetype_name,
      id.get_dispname().c_str());
    Type *t = comp->get_type();
    t->set_genname(my_type->get_genname_own(), id.get_name());
    t->set_parent_type(my_type);
    t->chk();
    t->chk_embedded(true, "embedded into another type");
  }
}

void CompFieldMap::dump(unsigned level) const
{
  size_t nof_comps = v.size();
  DEBUG(level, "component fields: (%lu pcs.) @ %p",
    (unsigned long) nof_comps, (const void*)this);
  for (size_t i = 0; i < nof_comps; i++) v[i]->dump(level + 1);
}

} /* namespace Common */