File: Constant.cc

package info (click to toggle)
eclipse-titan 8.2.0-1
  • links: PTS
  • area: main
  • in suites: bookworm, sid
  • size: 103,544 kB
  • sloc: cpp: 271,008; ansic: 33,683; yacc: 23,419; makefile: 15,483; lex: 9,204; java: 4,848; perl: 4,555; sh: 2,242; xml: 1,378; javascript: 85; awk: 48; php: 32; python: 13
file content (226 lines) | stat: -rw-r--r-- 8,340 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/******************************************************************************
 * Copyright (c) 2000-2021 Ericsson Telecom AB
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
 *
 * Contributors:
 *   Szabo, Bence Janos
 *
 ******************************************************************************/
#include "Constant.hh"
#include "ComplexType.hh"


Constant::Constant(SimpleType* p_parent, Mstring p_type, Mstring p_value)
  : RootType(p_parent->getParser(), p_parent->getModule(), p_parent->getConstruct())
{
  parent = p_parent;
  type.upload(p_type, false);
  value = p_value;
  alterego = NULL;
  checked = false;
  unsupported = false;
}

void Constant::nameConversion(const NameConversionMode conversion_mode, const List<NamespaceType> & ns) {
  switch (conversion_mode) {
    case nameMode:
      // Nameconversion of the constants name is postponed to finalFinalModification
      break;
    case typeMode:
      nameConversion_types(ns);
      break;
    case fieldMode:
      break;
  }
}

void Constant::nameConversion_types(const List<NamespaceType> & ns) {

  Mstring prefix = type.convertedValue.getPrefix(':');
  Mstring value_str = type.convertedValue.getValueWithoutPrefix(':');

  Mstring uri;
  for (List<NamespaceType>::iterator namesp = ns.begin(); namesp; namesp = namesp->Next) {
    if (prefix == namesp->Data.prefix) {
      uri = namesp->Data.uri;
      break;
    }
  }

  QualifiedName tmp(uri, value_str);

  QualifiedNames::iterator origTN = TTCN3ModuleInventory::getInstance().getTypenames().begin();
  for (; origTN; origTN = origTN->Next) {
    if (tmp == origTN->Data) {
      QualifiedName tmp_name(module->getTargetNamespace(), name.convertedValue);
      if (tmp_name == origTN->Data)
        continue; // get a new type name
      else
        break;
    }
  }
  if (origTN != NULL) {
    setTypeValue(origTN->Data.name);
    // This      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ is always value_str
    // The only effect here is to remove the "xs:" prefix from type.convertedValue,
    // otherwise the new value is always the same as the old.
  } else {
    Mstring res, var;
    XSDName2TTCN3Name(value_str, empty_string, TTCN3ModuleInventory::getInstance().getTypenames(), type_reference_name, res, var);
    setTypeValue(res);
  }
}

void Constant::finalModification() {
  if (checked) return;
  Mstring tmp_type;
  // Find the buildInType which the type is derived from
  if (isBuiltInType(type.originalValueWoPrefix)) {
    tmp_type = type.originalValueWoPrefix;
  } else if (!parent->getReference().empty() && parent->getReference().get_ref() != NULL) {
    SimpleType * st = (SimpleType*)parent->getReference().get_ref();
    if (isBuiltInType(st->getBuiltInBase())) {
      tmp_type = st->getBuiltInBase();
    } else {
      tmp_type = type.originalValueWoPrefix;
    }
  } else {
    tmp_type = type.originalValueWoPrefix;
  }
  
  // If the element or attribute is an unnamed enumeration and has default
  // or fixed attribute
  if (parent->getEnumeration().modified &&
     (parent->getReference().get_ref() == NULL ||
      !((SimpleType*)(parent->getReference().get_ref()))->getEnumeration().modified)) {
    Mstring newtype = Mstring("");
    // If the unnamed enumeration is a field of a type
    if (parent->getMainType() != NULL) {
      newtype = parent->getMainType()->getName().convertedValue + ".";
      newtype += parent->getPath();
    } else {
      // If the unnamed enumeration is a top level attribute or element
      newtype = parent->getName().convertedValue;
    }
    type.upload(newtype);
  }
  
  // These types do not support enumeration restriction
  if (!(parent->getEnumeration().modified ||
     (parent->getReference().get_ref() != NULL &&
     ((SimpleType*)(parent->getReference().get_ref()))->getEnumeration().modified))
    && (tmp_type == "NMTOKENS" || tmp_type == "IDREFS" || tmp_type == "ENTITIES")) {
    // These are not supported by TITAN.
    // Reset the default value and fixed value
    parent->getValue().default_value = "";
    parent->getValue().fixed_value = "";
    unsupported = true;
    // Set the constant to invisible so it won't be generated into the code.
    setInvisible();
  } else if (parent->getEnumeration().modified ||
     (parent->getReference().get_ref() != NULL &&
     ((SimpleType*)(parent->getReference().get_ref()))->getEnumeration().modified)) {
    
    EnumerationType* enumeration =
      &(parent->getEnumeration().modified ? parent->getEnumeration() :
        ((SimpleType*)(parent->getReference().get_ref()))->getEnumeration());
    
    // float enumeration handled differently
    if (isFloatType(enumeration->parent->getBuiltInBase())) {
      value = xmlFloat2TTCN3FloatStr(value);
    } else {
      // We pushed the converted enumeration values to the converted_facets,
      // here we find the converted value based on the position in the list.
      int i = 0, j = 0;
      for(List<Mstring>::iterator it = enumeration->facets.begin(); it ; it = it->Next) {
        if (it->Data == value) break;
        i++;
      }
      for(List<Mstring>::iterator it = enumeration->converted_facets.begin(); it ; it = it->Next) {
        if (i == j) {
          value = it->Data;
          break;
        }
        j++;
      }
    }
  // String and time types need the quote character around the value
  } else if (isStringType(tmp_type) || isTimeType(tmp_type) || tmp_type == "anySimpleType") {
    value = Mstring("\"") + value + Mstring("\"");
  } else if (isFloatType(tmp_type)) {
    value = xmlFloat2TTCN3FloatStr(value);
  } else if (isBooleanType(tmp_type)) {
    if (value == "1") {
      value = "true";
    } else if (value == "0") {
      value = "false";
    }
  } else if (isQNameType(tmp_type) || isAnyType(tmp_type)) {
    // These are not supported by TITAN.
    // Reset the default value and fixed value
    parent->getValue().default_value = "";
    parent->getValue().fixed_value = "";
    unsupported = true;
    // Set the constant to invisible so it won't be generated into the code.
    setInvisible();
  }
  checked = true;
}

void Constant::finalFinalModification(List<RootType*> constantDefs) {
  // Make the pointers point to the constant which have the same type and value,
  // and set one of them invisible, so only one constant will be generated for
  // each unique type and value pair.
  for (List<RootType*>::iterator it = constantDefs.begin(); it; it = it->Next) {
    if (!it->Data->isVisible()) continue;
    for (List<RootType*>::iterator it2 = it->Next; it2; it2 = it2->Next) {
      if (!it2->Data->isVisible()) continue;
      if (*(Constant*)(it->Data) == *(Constant*)(it2->Data)) {
        ((Constant*)it2->Data)->alterego = (Constant*)(it->Data);
        it2->Data->setInvisible();
      }
    }
  }
  if (!constantDefs.empty()) {
    TTCN3Module* module = constantDefs.begin()->Data->getModule();
    expstring_t tmpname = NULL;
    for (List<RootType*>::iterator it = constantDefs.begin(); it; it = it->Next) {
      if (!it->Data->isVisible()) continue;
      tmpname = mputprintf(tmpname, "c_defaultForEmpty_%u", module->getConstCounter());
      module->increaseConstCounter();
      const_cast<NameType&>(it->Data->getName()).upload(Mstring(tmpname));
      Free(tmpname);
      tmpname = NULL;
    }
  }
}

Mstring Constant::getConstantName(const TTCN3Module* other_mod) const {
  Mstring qname;
  if (alterego != NULL) {
    qname = alterego->getName().convertedValue;
  } else {
    qname = getName().convertedValue;  
  }
  if (!o_flag_used && other_mod->getModulename() != getModule()->getModulename()) {
    qname = getModule()->getModulename() + "." + qname;
  }
  return qname;
}

void Constant::printToFile(FILE * file) {
  if (!visible) return;
  fprintf(file, "const %s %s := %s;\n\n\n",
    type.convertedValue.c_str(),
    name.convertedValue.c_str(),
    value.c_str());
}

void Constant::dump(const unsigned int depth) const {
  fprintf(stderr, "%*s Constant '%s' -> '%s' with value: '%s' at %p\n", depth * 2, "",
    name.originalValueWoPrefix.c_str(), name.convertedValue.c_str(), value.c_str(),
    (const void*) this);
}