Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1827 lines
49 KiB

  1. /* Copyright (C) Boris Nikolaus, Germany, 1996-1997. All rights reserved. */
  2. /* Copyright (C) Microsoft Corporation, 1997-1998. All rights reserved. */
  3. #include "precomp.h"
  4. #include "error.h"
  5. #include "util.h"
  6. typedef enum {
  7. eNull, eSingle, eMultiple, eString
  8. } RepresentationGroup_e;
  9. void BuildTypeFlags(AssignmentList_t ass, Type_t *type);
  10. Type_t *RebuildTypeWithoutSelectionType(AssignmentList_t ass, Type_t *type);
  11. /* name the sub types of a type */
  12. /* returns 1 if any naming has been performed */
  13. int
  14. NameSubType(AssignmentList_t *ass, char *identifier, Type_t *type, ModuleIdentifier_t *module)
  15. {
  16. char name[256], *n;
  17. char *p;
  18. Component_t *components;
  19. Type_t *subtype;
  20. int ret = 0;
  21. /* build the prefix for the subtypes */
  22. strcpy(name, identifier);
  23. strcat(name, "_");
  24. p = name + strlen(name);
  25. switch (type->Type) {
  26. case eType_Sequence:
  27. case eType_Set:
  28. case eType_Choice:
  29. case eType_InstanceOf:
  30. /* check all components */
  31. for (components = type->U.SSC.Components; components;
  32. components = components->Next) {
  33. switch (components->Type) {
  34. case eComponent_Normal:
  35. case eComponent_Optional:
  36. case eComponent_Default:
  37. /* do not name unstructured types */
  38. if (!IsStructuredType(components->U.NOD.NamedType->Type))
  39. break;
  40. /* name the type of the component and use a type */
  41. /* reference instead */
  42. strcpy(p, components->U.NOD.NamedType->Identifier);
  43. n = Identifier2C(name);
  44. subtype = NewType(eType_Reference);
  45. subtype->U.Reference.Identifier = n;
  46. subtype->U.Reference.Module = module;
  47. AssignType(ass, subtype, components->U.NOD.NamedType->Type);
  48. components->U.NOD.NamedType->Type = subtype;
  49. if (components->Type == eComponent_Default)
  50. components->U.NOD.Value->Type = subtype;
  51. ret = 1;
  52. break;
  53. }
  54. }
  55. break;
  56. case eType_SequenceOf:
  57. case eType_SetOf:
  58. /* already named? */
  59. if (type->U.SS.Type->Type == eType_Reference)
  60. break;
  61. /* name the type of the elements and use a type reference instead */
  62. strcpy(p, type->Type == eType_SequenceOf ? "Seq" : "Set");
  63. n = Identifier2C(name);
  64. subtype = NewType(eType_Reference);
  65. subtype->U.Reference.Identifier = n;
  66. subtype->U.Reference.Module = module;
  67. AssignType(ass, subtype, type->U.SS.Type);
  68. type->U.SS.Type = subtype;
  69. ret = 1;
  70. break;
  71. case eType_Selection:
  72. /* do not name unstructured types */
  73. if (!IsStructuredType(type->U.Selection.Type))
  74. break;
  75. /* name the type of the selected type and use a type reference */
  76. /* instead */
  77. strcpy(p, "Sel");
  78. n = Identifier2C(name);
  79. subtype = NewType(eType_Reference);
  80. subtype->U.Reference.Identifier = n;
  81. subtype->U.Reference.Module = module;
  82. AssignType(ass, subtype, type->U.Selection.Type);
  83. type->U.Selection.Type = subtype;
  84. ret = 1;
  85. break;
  86. }
  87. /* return 1 if any naming has been performed */
  88. return ret;
  89. }
  90. /* name the default value of a type */
  91. /* return 1 if any naming has been performed */
  92. int
  93. NameValueOfType(AssignmentList_t *ass, char *identifier, Type_t *type, ModuleIdentifier_t *module)
  94. {
  95. char name[256], *n;
  96. char *p;
  97. Component_t *components;
  98. Value_t *subvalue;
  99. int ret = 0;
  100. /* build the prefix for the subtypes */
  101. strcpy(name, identifier);
  102. strcat(name, "_");
  103. p = name + strlen(name);
  104. switch (type->Type) {
  105. case eType_Sequence:
  106. case eType_Set:
  107. /* check all components */
  108. for (components = type->U.SSC.Components; components;
  109. components = components->Next) {
  110. switch (components->Type) {
  111. case eComponent_Default:
  112. /* already named? */
  113. if (!components->U.NOD.Value->Type)
  114. break;
  115. /* name the value of the default component and use a value */
  116. /* reference instead */
  117. strcpy(p, components->U.NOD.NamedType->Identifier);
  118. strcat(p, "_default");
  119. n = Identifier2C(name);
  120. subvalue = NewValue(NULL, NULL);
  121. subvalue->U.Reference.Identifier = n;
  122. subvalue->U.Reference.Module = module;
  123. AssignValue(ass, subvalue, components->U.NOD.Value);
  124. components->U.NOD.Value = subvalue;
  125. ret = 1;
  126. break;
  127. }
  128. }
  129. break;
  130. }
  131. /* return 1 if any naming has been performed */
  132. return ret;
  133. }
  134. /* name the types of typefields of the settings of an object */
  135. /* return 1 if any naming has been performed */
  136. int
  137. NameSettings(AssignmentList_t *ass, char *identifier, SettingList_t se, ModuleIdentifier_t *module)
  138. {
  139. int ret = 0;
  140. char name[256], *n;
  141. char *p;
  142. Type_t *subtype;
  143. /* build the prefix for the subtypes */
  144. strcpy(name, identifier);
  145. strcat(name, "_");
  146. p = name + strlen(name);
  147. /* check all settings */
  148. for (; se; se = se->Next) {
  149. strcpy(p, se->Identifier + 1);
  150. switch (se->Type) {
  151. case eSetting_Type:
  152. /* name field type if not already named */
  153. if (se->U.Type.Type->Type != eType_Reference) {
  154. n = Identifier2C(name);
  155. subtype = NewType(eType_Reference);
  156. subtype->U.Reference.Identifier = n;
  157. subtype->U.Reference.Module = module;
  158. ret = AssignType(ass, subtype, se->U.Type.Type);
  159. }
  160. /* mark field type for generation */
  161. se->U.Type.Type->Flags |= eTypeFlags_GenAll;
  162. break;
  163. }
  164. }
  165. /* return 1 if any naming has been performed */
  166. return ret;
  167. }
  168. /* name the default types of typefields of the field specs of an object class */
  169. /* return 1 if any naming has been performed */
  170. int
  171. NameDefaultTypes(AssignmentList_t *ass, char *identifier, ObjectClass_t *oc, SettingList_t se, ModuleIdentifier_t *module)
  172. {
  173. int ret = 0;
  174. char name[256], *n;
  175. char *p;
  176. Type_t *subtype;
  177. FieldSpec_t *fs;
  178. /* build the prefix for the subtypes */
  179. strcpy(name, identifier);
  180. strcat(name, "_");
  181. p = name + strlen(name);
  182. oc = GetObjectClass(*ass, oc);
  183. /* check all field specs */
  184. for (fs = oc->U.ObjectClass.FieldSpec; fs; fs = fs->Next) {
  185. strcpy(p, fs->Identifier + 1);
  186. switch (fs->Type) {
  187. case eFieldSpec_Type:
  188. /* check if typefield has a default type */
  189. if (fs->U.Type.Optionality->Type != eOptionality_Default_Type ||
  190. FindSetting(se, fs->Identifier))
  191. break;
  192. /* name field type if not already named */
  193. if (fs->U.Type.Optionality->U.Type->Type != eType_Reference) {
  194. n = Identifier2C(name);
  195. subtype = NewType(eType_Reference);
  196. subtype->U.Reference.Identifier = n;
  197. subtype->U.Reference.Module = module;
  198. ret = AssignType(ass, subtype, fs->U.Type.Optionality->U.Type);
  199. }
  200. /* mark field type for generation */
  201. fs->U.Type.Optionality->U.Type->Flags |= eTypeFlags_GenAll;
  202. break;
  203. }
  204. }
  205. /* return 1 if any naming has been performed */
  206. return ret;
  207. }
  208. /* name the types of type fields of an object and the default types of */
  209. /* typefields of the field specs of an object class */
  210. /* return 1 if any naming has been performed */
  211. int
  212. NameSettingsOfObject(AssignmentList_t *ass, char *identifier, Object_t *object, ModuleIdentifier_t *module)
  213. {
  214. int ret = 0;
  215. switch (object->Type) {
  216. case eObject_Object:
  217. ret = NameSettings(ass, identifier, object->U.Object.Settings,
  218. module);
  219. ret |= NameDefaultTypes(ass, identifier, object->U.Object.ObjectClass,
  220. object->U.Object.Settings, module);
  221. break;
  222. }
  223. /* return 1 if any naming has been performed */
  224. return ret;
  225. }
  226. /* name the identification value of embedded pdv/character string types */
  227. void
  228. NameIdentificationValueOfType(AssignmentList_t *ass, char *identifier, Type_t *type, ModuleIdentifier_t *module)
  229. {
  230. char name[256], *n;
  231. char *p;
  232. Component_t *components;
  233. NamedValue_t *namedValues;
  234. Value_t *subvalue;
  235. /* build the prefix for the subtypes */
  236. strcpy(name, identifier);
  237. strcat(name, "_");
  238. p = name + strlen(name);
  239. switch (type->Type) {
  240. case eType_Sequence:
  241. case eType_Set:
  242. case eType_Choice:
  243. case eType_InstanceOf:
  244. /* check all components */
  245. for (components = type->U.SSC.Components; components;
  246. components = components->Next) {
  247. switch (components->Type) {
  248. case eComponent_Normal:
  249. case eComponent_Optional:
  250. case eComponent_Default:
  251. strcpy(p, components->U.NOD.NamedType->Identifier);
  252. NameIdentificationValueOfType(ass, name,
  253. components->U.NOD.NamedType->Type, module);
  254. break;
  255. }
  256. }
  257. break;
  258. case eType_SequenceOf:
  259. case eType_SetOf:
  260. /* check the subtype */
  261. strcpy(p, type->Type == eType_SequenceOf ? "Seq" : "Set");
  262. NameIdentificationValueOfType(ass, name, type->U.SS.Type, module);
  263. break;
  264. case eType_EmbeddedPdv:
  265. case eType_CharacterString:
  266. /* check if type has a fixed identification syntaxes constraint */
  267. namedValues = GetFixedIdentification(*ass, type->Constraints);
  268. if (namedValues && !strcmp(namedValues->Identifier, "syntaxes")) {
  269. /* name the identification and use a value reference instead */
  270. for (namedValues = namedValues->Value->U.SSC.NamedValues;
  271. namedValues; namedValues = namedValues->Next) {
  272. strcpy(p, "identification_syntaxes_");
  273. strcat(p, namedValues->Identifier);
  274. n = Identifier2C(name);
  275. subvalue = NewValue(NULL, NULL);
  276. subvalue->U.Reference.Identifier = n;
  277. subvalue->U.Reference.Module = module;
  278. AssignValue(ass, subvalue, namedValues->Value);
  279. }
  280. }
  281. break;
  282. }
  283. }
  284. /* name the type of a value */
  285. /* returns 1 if any naming has been performed */
  286. int
  287. NameTypeOfValue(AssignmentList_t *ass, char *identifier, Value_t *value, ModuleIdentifier_t *module)
  288. {
  289. Type_t *type;
  290. char name[256], *n;
  291. Type_t *subtype;
  292. int ret = 0;
  293. type = value->Type;
  294. /* do not name types of value references or unstructured types */
  295. if (type && IsStructuredType(type)) {
  296. /* build the prefix for the subtype */
  297. strcpy(name, identifier);
  298. strcat(name, "_");
  299. strcat(name, "Type");
  300. n = Identifier2C(name);
  301. /* name the type and use a type reference instead */
  302. subtype = NewType(eType_Reference);
  303. subtype->U.Reference.Identifier = n;
  304. subtype->U.Reference.Module = module;
  305. AssignType(ass, subtype, type);
  306. value->Type = subtype;
  307. ret = 1;
  308. }
  309. return ret;
  310. }
  311. /* replace any components of by the components of the referenced type */
  312. ComponentList_t
  313. RebuildComponentsWithoutComponentsOf(AssignmentList_t ass, ComponentList_t components)
  314. {
  315. Component_t *newcomponents, *subcomponents, **pcomponents;
  316. Type_t *subtype;
  317. int ext;
  318. ext = 0;
  319. pcomponents = &newcomponents;
  320. for (; components; components = components->Next) {
  321. switch (components->Type) {
  322. case eComponent_ComponentsOf:
  323. /* components of should not be used in an extension */
  324. if (ext)
  325. error(E_COMPONENTS_OF_in_extension, NULL);
  326. /* get the components of the referenced type */
  327. subtype = GetType(ass, components->U.ComponentsOf.Type);
  328. switch (subtype->Type) {
  329. case eType_Sequence:
  330. case eType_Set:
  331. case eType_Choice:
  332. case eType_External:
  333. case eType_EmbeddedPdv:
  334. case eType_CharacterString:
  335. case eType_Real:
  336. case eType_InstanceOf:
  337. subcomponents = subtype->U.SSC.Components;
  338. break;
  339. default:
  340. error(E_applied_COMPONENTS_OF_to_bad_type, NULL);
  341. }
  342. /* get the real components of the referenced type */
  343. /*XXX self-referencing components of types will idle forever */
  344. *pcomponents = RebuildComponentsWithoutComponentsOf(ass,
  345. subcomponents);
  346. /* find end of components of referenced type */
  347. while (*pcomponents) {
  348. if ((*pcomponents)->Type == eComponent_ExtensionMarker)
  349. error(E_COMPONENTS_OF_extended_type, NULL);
  350. pcomponents = &(*pcomponents)->Next;
  351. }
  352. break;
  353. case eComponent_ExtensionMarker:
  354. /* copy extension marker */
  355. ext = 1;
  356. *pcomponents = DupComponent(components);
  357. pcomponents = &(*pcomponents)->Next;
  358. break;
  359. default:
  360. /* copy other components */
  361. *pcomponents = DupComponent(components);
  362. pcomponents = &(*pcomponents)->Next;
  363. break;
  364. }
  365. }
  366. /* terminate and return component list */
  367. *pcomponents = NULL;
  368. return newcomponents;
  369. }
  370. /* replace any components of by the components of the referenced type */
  371. Type_t *
  372. RebuildTypeWithoutComponentsOf(AssignmentList_t ass, Type_t *type)
  373. {
  374. switch (type->Type) {
  375. case eType_Sequence:
  376. case eType_Set:
  377. case eType_Choice:
  378. type->U.SSC.Components =
  379. RebuildComponentsWithoutComponentsOf(ass, type->U.SSC.Components);
  380. break;
  381. }
  382. return type;
  383. }
  384. /* replace any selection type by the component of the selected type */
  385. ComponentList_t
  386. RebuildComponentsWithoutSelectionType(AssignmentList_t ass, ComponentList_t components)
  387. {
  388. Component_t *c;
  389. for (c = components; c; c = c->Next) {
  390. switch (c->Type) {
  391. case eComponent_Normal:
  392. case eComponent_Optional:
  393. case eComponent_Default:
  394. c->U.NOD.NamedType->Type = RebuildTypeWithoutSelectionType(
  395. ass, c->U.NOD.NamedType->Type);
  396. break;
  397. }
  398. }
  399. return components;
  400. }
  401. /* replace any selection type by the component of the selected type */
  402. Type_t *RebuildTypeWithoutSelectionType(AssignmentList_t ass, Type_t *type)
  403. {
  404. Type_t *subtype;
  405. Component_t *components;
  406. switch (type->Type) {
  407. case eType_Selection:
  408. subtype = GetType(ass, type->U.Selection.Type);
  409. switch (subtype->Type) {
  410. case eType_Sequence:
  411. case eType_Set:
  412. case eType_Choice:
  413. case eType_External:
  414. case eType_EmbeddedPdv:
  415. case eType_CharacterString:
  416. case eType_Real:
  417. case eType_InstanceOf:
  418. /* get the components of the referenced type */
  419. components = FindComponent(ass, subtype->U.SSC.Components,
  420. type->U.Selection.Identifier);
  421. if (!components)
  422. error(E_bad_component_in_selectiontype, NULL);
  423. /* get the real type of the referenced type */
  424. /*XXX self-referencing selection types will idle forever */
  425. type = RebuildTypeWithoutSelectionType(ass,
  426. components->U.NOD.NamedType->Type);
  427. break;
  428. default:
  429. error(E_selection_of_bad_type, NULL);
  430. }
  431. break;
  432. case eType_Sequence:
  433. case eType_Set:
  434. case eType_Choice:
  435. type->U.SSC.Components =
  436. RebuildComponentsWithoutSelectionType(ass, type->U.SSC.Components);
  437. break;
  438. case eType_SequenceOf:
  439. case eType_SetOf:
  440. type->U.SS.Type = RebuildTypeWithoutSelectionType(ass, type->U.SS.Type);
  441. break;
  442. }
  443. return type;
  444. }
  445. /* mark a type for autotagging */
  446. void
  447. MarkTypeForAutotagging(AssignmentList_t ass, Type_t *type)
  448. {
  449. Component_t *components;
  450. int ext;
  451. switch (type->Type) {
  452. case eType_Sequence:
  453. case eType_Set:
  454. case eType_Choice:
  455. ext = 0;
  456. /* set flags for autotagging */
  457. type->U.SSC.Autotag[0] = 1;
  458. type->U.SSC.Autotag[1] = 1;
  459. /* reset flags for autotagging if a tag has been used */
  460. for (components = type->U.SSC.Components; components;
  461. components = components->Next) {
  462. switch (components->Type) {
  463. case eComponent_Normal:
  464. case eComponent_Optional:
  465. case eComponent_Default:
  466. if (GetTag(ass, components->U.NOD.NamedType->Type))
  467. type->U.SSC.Autotag[ext] = 0;
  468. break;
  469. case eComponent_ExtensionMarker:
  470. ext = 1;
  471. break;
  472. case eComponent_ComponentsOf:
  473. break;
  474. }
  475. }
  476. break;
  477. }
  478. }
  479. /* autotag a marked type */
  480. void
  481. AutotagType(AssignmentList_t ass, Type_t *type)
  482. {
  483. Component_t *components;
  484. Type_t *subtype;
  485. int ext;
  486. int tag;
  487. Tag_t *tags;
  488. switch (type->Type) {
  489. case eType_Sequence:
  490. case eType_Set:
  491. case eType_Choice:
  492. ext = 0;
  493. /* tag number to use */
  494. tag = 0;
  495. for (components = type->U.SSC.Components; components;
  496. components = components->Next) {
  497. switch (components->Type) {
  498. case eComponent_Normal:
  499. case eComponent_Optional:
  500. case eComponent_Default:
  501. subtype = components->U.NOD.NamedType->Type;
  502. tags = subtype->Tags;
  503. /* check if type needs autotagging */
  504. if (!tags &&
  505. type->TagDefault == eTagType_Automatic &&
  506. type->U.SSC.Autotag[ext]) {
  507. /* create a tagged version of the type */
  508. components->U.NOD.NamedType->Type = subtype =
  509. DupType(subtype);
  510. /* use explicit tag for choice components types and */
  511. /* for open type and dummy reference, implicit tag */
  512. /* otherwise */
  513. if (subtype->Type == eType_Choice ||
  514. subtype->Type == eType_Open
  515. /*XXX || DummyReference*/) {
  516. subtype->Tags = NewTag(eTagType_Explicit);
  517. } else {
  518. subtype->Tags = NewTag(eTagType_Implicit);
  519. }
  520. subtype->Tags->Tag = NewValue(NULL, Builtin_Type_Integer);
  521. intx_setuint32(&subtype->Tags->Tag->U.Integer.Value,
  522. tag++);
  523. }
  524. break;
  525. case eComponent_ExtensionMarker:
  526. ext = 1;
  527. break;
  528. }
  529. }
  530. break;
  531. }
  532. }
  533. /* mark constraints extendable */
  534. void
  535. AutoextendConstraints(Constraint_t *constraints)
  536. {
  537. if (!constraints)
  538. return;
  539. if (constraints->Type == eExtension_Unextended)
  540. constraints->Type = eExtension_Extendable;
  541. }
  542. /* autoextend a type if desired */
  543. void
  544. AutoextendType(AssignmentList_t ass, Type_t *type)
  545. {
  546. Component_t *c, **cc;
  547. Type_t *subtype;
  548. int ext;
  549. /* already done? */
  550. if (type->Flags & eTypeFlags_Done)
  551. return;
  552. type->Flags |= eTypeFlags_Done;
  553. /* auto extending wanted? */
  554. if (type->ExtensionDefault != eExtensionType_Automatic)
  555. return;
  556. /* check all sub types */
  557. switch (type->Type) {
  558. case eType_Sequence:
  559. case eType_Set:
  560. case eType_Choice:
  561. /* extend a sequence/set/choice type */
  562. ext = 0;
  563. for (cc = &type->U.SSC.Components, c = *cc; c;
  564. c = c->Next, cc = &(*cc)->Next) {
  565. *cc = DupComponent(c);
  566. switch (c->Type) {
  567. case eComponent_Normal:
  568. case eComponent_Optional:
  569. case eComponent_Default:
  570. subtype = c->U.NOD.NamedType->Type;
  571. AutoextendType(ass, subtype);
  572. break;
  573. case eComponent_ExtensionMarker:
  574. ext = 1;
  575. break;
  576. }
  577. }
  578. if (!ext) {
  579. *cc = NewComponent(eComponent_ExtensionMarker);
  580. cc = &(*cc)->Next;
  581. }
  582. *cc = NULL;
  583. break;
  584. case eType_SequenceOf:
  585. case eType_SetOf:
  586. subtype = type->U.SS.Type;
  587. AutoextendType(ass, subtype);
  588. break;
  589. }
  590. /* mark type as extendable */
  591. AutoextendConstraints(type->Constraints);
  592. }
  593. /* set the tag type of unspecified tags to explicit or implicit, */
  594. /* create list of all tags (including the type's universal tag and the */
  595. /* tags of the referenced type if applicable), */
  596. /* and create list of first tags (esp. for choice types) */
  597. void
  598. BuildTags(AssignmentList_t ass, Type_t *type, TagType_e eParentDefTagType)
  599. {
  600. Tag_t *t, *t2, **tt;
  601. Component_t *components;
  602. Type_t *reftype;
  603. Type_e te;
  604. uint32_t i;
  605. /* already done? */
  606. if (type->Flags & eTypeFlags_Done)
  607. return;
  608. type->Flags |= eTypeFlags_Done;
  609. // update default tag type
  610. if (type->TagDefault == eTagType_Unknown &&
  611. (eParentDefTagType == eTagType_Explicit || eParentDefTagType == eTagType_Implicit))
  612. {
  613. type->TagDefault = eParentDefTagType;
  614. }
  615. /* set tag type of unspecified tags to explicit or implicit */
  616. /* use explicit tags when: */
  617. /* - TagDefault indicates explicit tags, */
  618. /* - Type is choice/open type/dummy reference and no other explicit tag */
  619. /* will follow */
  620. te = GetTypeType(ass, type);
  621. if (type->Tags)
  622. {
  623. for (tt = &type->Tags, t = type->Tags; t; tt = &(*tt)->Next, t = t->Next)
  624. {
  625. *tt = DupTag(t);
  626. if ((*tt)->Type == eTagType_Unknown)
  627. {
  628. for (t2 = t->Next; t2; t2 = t2->Next)
  629. {
  630. if (t2->Type != eTagType_Implicit)
  631. break;
  632. }
  633. if (type->TagDefault == eTagType_Explicit ||
  634. (!t2 && (te == eType_Choice || te == eType_Open /*XXX || DummyReference*/)))
  635. {
  636. (*tt)->Type = eTagType_Explicit;
  637. }
  638. else
  639. {
  640. (*tt)->Type = eTagType_Implicit;
  641. }
  642. }
  643. }
  644. }
  645. /* copy given tags to AllTags list */
  646. for (tt = &type->AllTags, t = type->Tags;
  647. t;
  648. tt = &(*tt)->Next, t = t->Next)
  649. {
  650. *tt = DupTag(t);
  651. }
  652. /* build tags of subtypes and copy tags of reference type */
  653. switch (type->Type)
  654. {
  655. case eType_Sequence:
  656. case eType_Set:
  657. case eType_Choice:
  658. case eType_InstanceOf:
  659. for (components = type->U.SSC.Components;
  660. components;
  661. components = components->Next)
  662. {
  663. switch (components->Type)
  664. {
  665. case eComponent_Normal:
  666. case eComponent_Optional:
  667. case eComponent_Default:
  668. BuildTags(ass, components->U.NOD.NamedType->Type, type->TagDefault);
  669. break;
  670. }
  671. }
  672. break;
  673. case eType_SequenceOf:
  674. case eType_SetOf:
  675. BuildTags(ass, type->U.SS.Type, eTagType_Unknown);
  676. break;
  677. case eType_Reference:
  678. reftype = GetReferencedType(ass, type);
  679. BuildTags(ass, reftype, type->TagDefault);
  680. for (t = reftype->AllTags; t; tt = &(*tt)->Next, t = t->Next)
  681. {
  682. *tt = DupTag(t);
  683. }
  684. break;
  685. }
  686. /* add the type's universal tag to the AllTags list if the type is */
  687. /* not an internal type */
  688. if (!(type->Type & 0x8000))
  689. {
  690. *tt = NewTag(eTagType_Implicit);
  691. (*tt)->Class = eTagClass_Universal;
  692. (*tt)->Tag = NewValue(NULL, Builtin_Type_Integer);
  693. intx_setuint32(&(*tt)->Tag->U.Integer.Value, type->Type & 0x1f);
  694. }
  695. /* build list of FirstTags containing the possible tag values of the type */
  696. tt = &type->FirstTags;
  697. if (type->AllTags)
  698. {
  699. /* if type has any tags, only the first tag is needed */
  700. *tt = DupTag(type->AllTags);
  701. tt = &(*tt)->Next;
  702. }
  703. else
  704. {
  705. /* otherwise we have to examine the type */
  706. switch (type->Type)
  707. {
  708. case eType_Choice:
  709. /* get the first tags of all components of a choice as FirstTags */
  710. for (components = type->U.SSC.Components;
  711. components;
  712. components = components->Next)
  713. {
  714. switch (components->Type)
  715. {
  716. case eComponent_Normal:
  717. case eComponent_Optional:
  718. case eComponent_Default:
  719. for (t = components->U.NOD.NamedType->Type->FirstTags;
  720. t;
  721. t = t->Next)
  722. {
  723. *tt = DupTag(t);
  724. tt = &(*tt)->Next;
  725. }
  726. break;
  727. }
  728. }
  729. break;
  730. case eType_Open:
  731. /* create a list of all tags for open type */
  732. for (i = 1; i < 0x20; i++)
  733. {
  734. *tt = NewTag(eTagType_Unknown);
  735. (*tt)->Class = eTagClass_Unknown;
  736. (*tt)->Tag = NewValue(NULL, Builtin_Type_Integer);
  737. intx_setuint32(&(*tt)->Tag->U.Integer.Value, i);
  738. tt = &(*tt)->Next;
  739. }
  740. break;
  741. case eType_Reference:
  742. /* get the tags of the referenced type */
  743. for (t = reftype->FirstTags; t; t = t->Next)
  744. {
  745. *tt = DupTag(t);
  746. tt = &(*tt)->Next;
  747. }
  748. break;
  749. }
  750. }
  751. *tt = NULL;
  752. }
  753. /* get the smallest tag of a tag list */
  754. Tag_t *
  755. FindSmallestTag(AssignmentList_t ass, TagList_t tags)
  756. {
  757. Tag_t *mintag, *t;
  758. mintag = tags;
  759. for (t = tags->Next; t; t = t->Next) {
  760. if (mintag->Class > t->Class ||
  761. mintag->Class == t->Class && intx_cmp(
  762. &GetValue(ass, mintag->Tag)->U.Integer.Value,
  763. &GetValue(ass, t->Tag)->U.Integer.Value) > 0)
  764. mintag = t;
  765. }
  766. return mintag;
  767. }
  768. /* compare two tags by tag class and tag value */
  769. int
  770. CmpTags(const void *p1, const void *p2, void *ctx)
  771. {
  772. Tag_t *tags1 = (Tag_t *)p1;
  773. Tag_t *tags2 = (Tag_t *)p2;
  774. Assignment_t *ass = (Assignment_t *)ctx;
  775. if (tags1->Class != tags2->Class)
  776. return tags1->Class - tags2->Class;
  777. return intx2uint32(&GetValue(ass, tags1->Tag)->U.Integer.Value) -
  778. intx2uint32(&GetValue(ass, tags2->Tag)->U.Integer.Value);
  779. }
  780. /* compare two components by their smallest tag */
  781. int
  782. CmpComponentsBySmallestTag(const void *p1, const void *p2, void *ctx)
  783. {
  784. Component_t *c1 = (Component_t *)p1;
  785. Component_t *c2 = (Component_t *)p2;
  786. Assignment_t *ass = (Assignment_t *)ctx;
  787. Tag_t *tags1, *tags2;
  788. tags1 = FindSmallestTag(ass, c1->U.NOD.NamedType->Type->FirstTags);
  789. tags2 = FindSmallestTag(ass, c2->U.NOD.NamedType->Type->FirstTags);
  790. if (tags1->Class != tags2->Class)
  791. return tags1->Class - tags2->Class;
  792. return intx2uint32(&tags1->Tag->U.Integer.Value) -
  793. intx2uint32(&tags2->Tag->U.Integer.Value);
  794. }
  795. /* sort the components of a set or choice by their smallest tag */
  796. void
  797. SortTypeTags(AssignmentList_t ass, Type_t *type)
  798. {
  799. Component_t **pcomponents, *extensions;
  800. switch (type->Type) {
  801. case eType_Set:
  802. case eType_Choice:
  803. /* remove extensions */
  804. for (pcomponents = &type->U.SSC.Components; *pcomponents;
  805. pcomponents = &(*pcomponents)->Next) {
  806. if ((*pcomponents)->Type == eComponent_ExtensionMarker)
  807. break;
  808. }
  809. extensions = *pcomponents;
  810. *pcomponents = NULL;
  811. /* sort extension root */
  812. qsortSL((void **)&type->U.SSC.Components, offsetof(Component_t, Next),
  813. CmpComponentsBySmallestTag, ass);
  814. /* sort extensions */
  815. if (extensions && extensions->Next)
  816. qsortSL((void **)&extensions->Next, offsetof(Component_t, Next),
  817. CmpComponentsBySmallestTag, ass);
  818. /* merge extension root and extensions */
  819. for (pcomponents = &type->U.SSC.Components; *pcomponents;
  820. pcomponents = &(*pcomponents)->Next) {}
  821. *pcomponents = extensions;
  822. break;
  823. }
  824. }
  825. /* check if two lists of tags have common tags */
  826. void
  827. CheckCommonTags(AssignmentList_t ass, TagList_t tags1, TagList_t tags2)
  828. {
  829. Tag_t *t1, *t2;
  830. int ret;
  831. qsortSL((void **)&tags1, offsetof(Tag_t, Next), CmpTags, ass);
  832. qsortSL((void **)&tags2, offsetof(Tag_t, Next), CmpTags, ass);
  833. for (t1 = tags1, t2 = tags2; t1 && t2; ) {
  834. ret = CmpTags((const void *)t1, (const void *)t2, (void *)ass);
  835. if (ret == 0) {
  836. error(E_duplicate_tag, NULL);
  837. } else if (ret < 0) {
  838. t1 = t1->Next;
  839. } else {
  840. t2 = t2->Next;
  841. }
  842. }
  843. }
  844. /* check if a list of tags and the first tags of components have common tags */
  845. void
  846. CheckTagsInComponents(AssignmentList_t ass, TagList_t tags, ComponentList_t components, int untilnormal)
  847. {
  848. for (; components; components = components->Next) {
  849. switch (components->Type) {
  850. case eComponent_Normal:
  851. case eComponent_Optional:
  852. case eComponent_Default:
  853. CheckCommonTags(ass, tags,
  854. components->U.NOD.NamedType->Type->FirstTags);
  855. if (untilnormal && components->Type == eComponent_Normal)
  856. return;
  857. break;
  858. }
  859. }
  860. }
  861. /* check for common tags */
  862. void
  863. CheckTags(AssignmentList_t ass, Type_t *type)
  864. {
  865. Component_t *c;
  866. Type_t *subtype;
  867. Tag_t *tag;
  868. switch (type->Type) {
  869. case eType_Sequence:
  870. /* check for common tags in a sequence: */
  871. /* the first tags of an optional/default component and the first */
  872. /* tags of the following components (up to and including the next */
  873. /* non-optional/non-default component) must not have common first */
  874. /* tags */
  875. for (c = type->U.Sequence.Components; c; c = c->Next) {
  876. switch (c->Type) {
  877. case eComponent_Optional:
  878. case eComponent_Default:
  879. subtype = c->U.NOD.NamedType->Type;
  880. tag = subtype->FirstTags;
  881. CheckTagsInComponents(ass, tag, c->Next, 1);
  882. break;
  883. }
  884. }
  885. break;
  886. case eType_Set:
  887. case eType_Choice:
  888. /* check for common tags in a set/choice: */
  889. /* the first tags of all components must be destinct */
  890. for (c = type->U.Sequence.Components; c; c = c->Next) {
  891. switch (c->Type) {
  892. case eComponent_Normal:
  893. case eComponent_Optional:
  894. case eComponent_Default:
  895. subtype = c->U.NOD.NamedType->Type;
  896. tag = subtype->FirstTags;
  897. CheckTagsInComponents(ass, tag, c->Next, 0);
  898. break;
  899. }
  900. }
  901. break;
  902. }
  903. }
  904. /* build the list of PER-visible constraints */
  905. void BuildConstraints(AssignmentList_t ass, Type_t *type)
  906. {
  907. Type_t *reftype;
  908. Constraint_t *cons, *c1, *c2;
  909. Component_t *components;
  910. /* already done? */
  911. if (type->Flags & eTypeFlags_Done)
  912. return;
  913. type->Flags |= eTypeFlags_Done;
  914. switch (type->Type) {
  915. case eType_Reference:
  916. /* create an intersection of the constraints of the reference type */
  917. /* and the constraints of the referenced type */
  918. reftype = GetReferencedType(ass, type);
  919. BuildConstraints(ass, reftype);
  920. c1 = reftype->Constraints;
  921. c2 = type->Constraints;
  922. if (c1) {
  923. if (c2) {
  924. IntersectConstraints(&cons, c1, c2);
  925. } else {
  926. cons = c1;
  927. }
  928. } else {
  929. cons = c2;
  930. }
  931. type->Constraints = cons;
  932. /* get the PER-visible constraints */
  933. GetPERConstraints(ass, cons, &type->PERConstraints);
  934. break;
  935. case eType_Sequence:
  936. case eType_Set:
  937. case eType_Choice:
  938. case eType_InstanceOf:
  939. /* build the constraints of any component */
  940. for (components = type->U.SSC.Components; components;
  941. components = components->Next) {
  942. switch (components->Type) {
  943. case eComponent_Normal:
  944. case eComponent_Optional:
  945. case eComponent_Default:
  946. reftype = components->U.NOD.NamedType->Type;
  947. BuildConstraints(ass, reftype);
  948. break;
  949. }
  950. }
  951. /* get the PER-visible constraints */
  952. GetPERConstraints(ass, type->Constraints, &type->PERConstraints);
  953. break;
  954. case eType_SequenceOf:
  955. case eType_SetOf:
  956. /* build the constraints of the subtype */
  957. reftype = type->U.SS.Type;
  958. BuildConstraints(ass, reftype);
  959. /* get the PER-visible constraints */
  960. GetPERConstraints(ass, type->Constraints, &type->PERConstraints);
  961. break;
  962. default:
  963. /* get the PER-visible constraints */
  964. GetPERConstraints(ass, type->Constraints, &type->PERConstraints);
  965. break;
  966. }
  967. }
  968. /* build type flags from the directives */
  969. void BuildDirectives(AssignmentList_t ass, Type_t *type, int isComponent)
  970. {
  971. int pointer = 0;
  972. TypeRules_e rule = 0;
  973. RepresentationGroup_e grp;
  974. int32_t noctets;
  975. uint32_t zero;
  976. Directive_t *d;
  977. Component_t *components;
  978. Type_t *reftype;
  979. /* already done? */
  980. if (type->Flags & eTypeFlags_Done)
  981. return;
  982. type->Flags |= eTypeFlags_Done;
  983. /* get directive group which may be applied to the type */
  984. switch (type->Type)
  985. {
  986. case eType_Boolean:
  987. case eType_Integer:
  988. case eType_ObjectIdentifier:
  989. case eType_ObjectDescriptor:
  990. case eType_External:
  991. case eType_Real:
  992. case eType_Enumerated:
  993. case eType_EmbeddedPdv:
  994. case eType_Sequence:
  995. case eType_Set:
  996. case eType_InstanceOf:
  997. case eType_UTCTime:
  998. case eType_GeneralizedTime:
  999. case eType_Choice:
  1000. case eType_BitString:
  1001. case eType_OctetString:
  1002. grp = eSingle;
  1003. break;
  1004. case eType_Reference:
  1005. grp = eSingle;
  1006. break;
  1007. case eType_CharacterString:
  1008. case eType_NumericString:
  1009. case eType_PrintableString:
  1010. case eType_TeletexString:
  1011. case eType_T61String:
  1012. case eType_VideotexString:
  1013. case eType_IA5String:
  1014. case eType_GraphicString:
  1015. case eType_VisibleString:
  1016. case eType_ISO646String:
  1017. case eType_GeneralString:
  1018. case eType_UniversalString:
  1019. case eType_BMPString:
  1020. case eType_RestrictedString:
  1021. case eType_UTF8String:
  1022. grp = eString;
  1023. /*XXX rule = zero ? eTypeRules_ZeroTerminated : eTypeRules_FixedArray;
  1024. for upperconstrained size */
  1025. // lonchanc: it was eTypeRules_LengthPointer
  1026. if (type->PrivateDirectives.fLenPtr)
  1027. {
  1028. rule = eTypeRules_LengthPointer;
  1029. }
  1030. else
  1031. if (type->PrivateDirectives.fArray)
  1032. {
  1033. rule = eTypeRules_FixedArray;
  1034. }
  1035. else
  1036. {
  1037. GetStringType(ass, type, &noctets, &zero);
  1038. rule = zero ? eTypeRules_ZeroTerminated : eTypeRules_LengthPointer;
  1039. }
  1040. break;
  1041. case eType_Null:
  1042. grp = eNull;
  1043. break;
  1044. case eType_SequenceOf:
  1045. case eType_SetOf:
  1046. grp = eMultiple;
  1047. // lonchanc: it was eTypeRules_LengthPointer
  1048. if (type->PrivateDirectives.fSLinked)
  1049. {
  1050. rule = eTypeRules_SinglyLinkedList;
  1051. }
  1052. else
  1053. if (type->PrivateDirectives.fLenPtr)
  1054. {
  1055. rule = eTypeRules_LengthPointer;
  1056. }
  1057. else
  1058. if (type->PrivateDirectives.fArray)
  1059. {
  1060. rule = eTypeRules_FixedArray;
  1061. }
  1062. else
  1063. if (type->PrivateDirectives.fPointer)
  1064. {
  1065. rule = eTypeRules_PointerToElement | eTypeRules_FixedArray;
  1066. }
  1067. else
  1068. if (type->PrivateDirectives.fDLinked)
  1069. {
  1070. rule = eTypeRules_DoublyLinkedList;
  1071. }
  1072. else
  1073. {
  1074. if (eExtension_Unconstrained == type->PERConstraints.Size.Type)
  1075. {
  1076. rule = g_eDefTypeRuleSS_NonSized;
  1077. }
  1078. else
  1079. {
  1080. rule = g_eDefTypeRuleSS_Sized;
  1081. }
  1082. }
  1083. break;
  1084. case eType_Selection:
  1085. MyAbort();
  1086. /*NOTREACHED*/
  1087. case eType_Undefined:
  1088. MyAbort();
  1089. /*NOTREACHED*/
  1090. }
  1091. /* parse list of directives */
  1092. for (d = type->Directives; d; d = d->Next) {
  1093. switch (d->Type) {
  1094. case eDirective_LengthPointer:
  1095. if (grp != eString)
  1096. error(E_bad_directive, NULL);
  1097. rule = eTypeRules_LengthPointer;
  1098. break;
  1099. case eDirective_ZeroTerminated:
  1100. if (grp != eString)
  1101. error(E_bad_directive, NULL);
  1102. rule = eTypeRules_ZeroTerminated;
  1103. break;
  1104. case eDirective_Pointer:
  1105. if (!isComponent)
  1106. error(E_bad_directive, NULL);
  1107. pointer = eTypeRules_Pointer;
  1108. break;
  1109. case eDirective_NoPointer:
  1110. if (!isComponent)
  1111. error(E_bad_directive, NULL);
  1112. pointer = 0;
  1113. break;
  1114. }
  1115. }
  1116. /* parse list of size directives of sequence of/set of */
  1117. if (type->Type == eType_SequenceOf || type->Type == eType_SetOf) {
  1118. for (d = type->U.SS.Directives; d; d = d->Next) {
  1119. switch (d->Type) {
  1120. case eDirective_FixedArray:
  1121. rule = eTypeRules_FixedArray;
  1122. break;
  1123. case eDirective_DoublyLinkedList:
  1124. rule = eTypeRules_DoublyLinkedList;
  1125. break;
  1126. case eDirective_SinglyLinkedList:
  1127. rule = eTypeRules_SinglyLinkedList;
  1128. break;
  1129. case eDirective_LengthPointer:
  1130. rule = eTypeRules_LengthPointer;
  1131. break;
  1132. }
  1133. }
  1134. }
  1135. /* lists are always pointered, no additional pointer needed */
  1136. if (rule & (eTypeRules_SinglyLinkedList | eTypeRules_DoublyLinkedList))
  1137. pointer = 0;
  1138. /* set type flags according to directive rule and pointer flag */
  1139. type->Rules = rule | pointer;
  1140. /* build directives of subtypes */
  1141. switch (type->Type) {
  1142. case eType_Sequence:
  1143. case eType_Set:
  1144. case eType_Choice:
  1145. case eType_InstanceOf:
  1146. components = type->U.SSC.Components;
  1147. for (; components; components = components->Next) {
  1148. switch (components->Type) {
  1149. case eComponent_Normal:
  1150. case eComponent_Optional:
  1151. case eComponent_Default:
  1152. reftype = components->U.NOD.NamedType->Type;
  1153. BuildDirectives(ass, reftype, 1);
  1154. break;
  1155. case eComponent_ExtensionMarker:
  1156. break;
  1157. }
  1158. }
  1159. break;
  1160. case eType_SequenceOf:
  1161. case eType_SetOf:
  1162. reftype = type->U.SS.Type;
  1163. BuildDirectives(ass, reftype, 0);
  1164. break;
  1165. }
  1166. }
  1167. /* build type flags and counters for components */
  1168. /* will set eTypeFlags_Null if type has only null components */
  1169. /* will set eTypeFlags_Simple if type has only simple components */
  1170. /* will count optional/default components in the extension root (optionals) */
  1171. /* will count components in the extension root (alternatives) */
  1172. /* will count components in the extension (extensions) */
  1173. void
  1174. BuildComponentsTypeFlags(AssignmentList_t ass, ComponentList_t components, TypeFlags_e *flags, uint32_t *alternatives, uint32_t *optionals, uint32_t *extensions)
  1175. {
  1176. int extended = 0;
  1177. TypeFlags_e f = eTypeFlags_Null | eTypeFlags_Simple;
  1178. while (components) {
  1179. switch (components->Type) {
  1180. case eComponent_Normal:
  1181. case eComponent_Optional:
  1182. case eComponent_Default:
  1183. BuildTypeFlags(ass, components->U.NOD.NamedType->Type);
  1184. if (!(components->U.NOD.NamedType->Type->Flags & eTypeFlags_Null))
  1185. f &= ~eTypeFlags_Null;
  1186. if ((components->U.NOD.NamedType->Type->Rules &
  1187. eTypeRules_Pointer) ||
  1188. !(components->U.NOD.NamedType->Type->Flags & eTypeFlags_Simple))
  1189. f &= ~eTypeFlags_Simple;
  1190. if (extended) {
  1191. if (extensions)
  1192. (*extensions)++;
  1193. } else {
  1194. if (alternatives)
  1195. (*alternatives)++;
  1196. if (optionals && components->Type != eComponent_Normal)
  1197. (*optionals)++;
  1198. }
  1199. break;
  1200. case eComponent_ExtensionMarker:
  1201. f |= eTypeFlags_ExtensionMarker;
  1202. extended = 1;
  1203. break;
  1204. }
  1205. components = components->Next;
  1206. }
  1207. *flags |= f;
  1208. }
  1209. /* build type flags and count components of sequence/set/choice types */
  1210. void
  1211. BuildTypeFlags(AssignmentList_t ass, Type_t *type)
  1212. {
  1213. Assignment_t *a;
  1214. Type_t *subtype;
  1215. char *itype;
  1216. int32_t sign;
  1217. /* already done? */
  1218. if (type->Flags & eTypeFlags_Done)
  1219. return;
  1220. type->Flags |= eTypeFlags_Done;
  1221. switch (type->Type) {
  1222. case eType_Null:
  1223. /* null is null and simple */
  1224. type->Flags |= eTypeFlags_Null | eTypeFlags_Simple;
  1225. break;
  1226. case eType_Boolean:
  1227. case eType_Enumerated:
  1228. /* boolean and enumerated are simple if no pointer is used */
  1229. if (!(type->Rules & eTypeRules_Pointer))
  1230. type->Flags |= eTypeFlags_Simple;
  1231. break;
  1232. case eType_Integer:
  1233. /* integer is simple if no pointer is used and no intx_t is used */
  1234. itype = GetIntegerType(ass, type, &sign);
  1235. if (strcmp(itype, "ASN1intx_t") && !(type->Rules & eTypeRules_Pointer))
  1236. type->Flags |= eTypeFlags_Simple;
  1237. break;
  1238. case eType_Real:
  1239. /* real is simple if no pointer is used and no real_t is used */
  1240. itype = GetRealType(type);
  1241. if (strcmp(itype, "ASN1real_t") && !(type->Rules & eTypeRules_Pointer))
  1242. type->Flags |= eTypeFlags_Simple;
  1243. break;
  1244. case eType_Sequence:
  1245. case eType_Set:
  1246. /* build type flags and counters for the components */
  1247. BuildComponentsTypeFlags(ass, type->U.SSC.Components, &type->Flags,
  1248. NULL, &type->U.SSC.Optionals, &type->U.Sequence.Extensions);
  1249. /* an extended type or a type containing optionals is not null */
  1250. if ((type->Flags & eTypeFlags_ExtensionMarker) || type->U.SSC.Optionals)
  1251. type->Flags &= ~eTypeFlags_Null;
  1252. break;
  1253. case eType_SequenceOf:
  1254. case eType_SetOf:
  1255. /* never null nor simple */
  1256. BuildTypeFlags(ass, type->U.SS.Type);
  1257. break;
  1258. case eType_Choice:
  1259. /* build type flags and counters for the components */
  1260. BuildComponentsTypeFlags(ass, type->U.Choice.Components, &type->Flags,
  1261. &type->U.Choice.Alternatives, NULL, &type->U.Choice.Extensions);
  1262. /* a choice of nulls with more than one alternative or extensions */
  1263. /* is not null because an offset has to be encoded */
  1264. /* set the nullchoice flag instead */
  1265. if ((type->Flags & eTypeFlags_Null) &&
  1266. ((type->Flags & eTypeFlags_ExtensionMarker) ||
  1267. type->U.Choice.Alternatives > 1)) {
  1268. type->Flags &= ~eTypeFlags_Null;
  1269. type->Flags |= eTypeFlags_NullChoice;
  1270. }
  1271. break;
  1272. case eType_Reference:
  1273. /* get the flags of the referenced type */
  1274. a = FindAssignment(ass, eAssignment_Type,
  1275. type->U.Reference.Identifier, type->U.Reference.Module);
  1276. a = GetAssignment(ass, a);
  1277. subtype = a->U.Type.Type;
  1278. BuildTypeFlags(ass, subtype);
  1279. type->Flags = subtype->Flags;
  1280. break;
  1281. }
  1282. }
  1283. /* Mark non-structured types (or all types if wanted) for generation */
  1284. void MarkTypeForGeneration(AssignmentList_t ass, Type_t *type, TypeFlags_e needed)
  1285. {
  1286. Assignment_t *a;
  1287. Component_t *components;
  1288. /* already done? */
  1289. if (type->Flags & eTypeFlags_Done) {
  1290. type->Flags |= needed;
  1291. return;
  1292. }
  1293. type->Flags |= eTypeFlags_Done | needed;
  1294. if (!IsStructuredType(GetType(ass, type)) && !ForceAllTypes) {
  1295. /* generate type only */
  1296. type->Flags |= eTypeFlags_GenType;
  1297. } else {
  1298. if (type->Flags & eTypeFlags_Simple) {
  1299. /* generate encoding/decoding/compare */
  1300. type->Flags |= eTypeFlags_GenSimple;
  1301. } else {
  1302. /* generate encoding/decoding/free/compare */
  1303. type->Flags |= eTypeFlags_GenAll;
  1304. }
  1305. }
  1306. /* mark subtypes for generation */
  1307. switch (type->Type) {
  1308. case eType_Sequence:
  1309. case eType_Set:
  1310. case eType_Choice:
  1311. case eType_InstanceOf:
  1312. for (components = type->U.SSC.Components; components;
  1313. components = components->Next) {
  1314. switch (components->Type) {
  1315. case eComponent_Normal:
  1316. case eComponent_Optional:
  1317. case eComponent_Default:
  1318. MarkTypeForGeneration(ass, components->U.NOD.NamedType->Type,
  1319. needed);
  1320. break;
  1321. }
  1322. }
  1323. break;
  1324. case eType_SequenceOf:
  1325. case eType_SetOf:
  1326. MarkTypeForGeneration(ass, type->U.SS.Type,
  1327. needed | eTypeFlags_GenCompare);
  1328. break;
  1329. case eType_Reference:
  1330. a = FindAssignment(ass, eAssignment_Type,
  1331. type->U.Reference.Identifier, type->U.Reference.Module);
  1332. a = GetAssignment(ass, a);
  1333. MarkTypeForGeneration(ass, a->U.Type.Type, needed);
  1334. break;
  1335. }
  1336. }
  1337. /* mark a value for generation */
  1338. void
  1339. MarkValueForGeneration(AssignmentList_t ass, Value_t *value)
  1340. {
  1341. /* already done? */
  1342. if (value->Flags & eValueFlags_Done)
  1343. return;
  1344. value->Flags |= eValueFlags_GenAll | eValueFlags_Done;
  1345. /* mark type of value for generation */
  1346. if (value->Type)
  1347. MarkTypeForGeneration(ass, value->Type, 0);
  1348. }
  1349. /* mark assignments for generation */
  1350. void
  1351. MarkForGeneration(AssignmentList_t ass, ModuleIdentifier_t *mainmodule, Assignment_t *a)
  1352. {
  1353. /* builtin elements need not to be generated */
  1354. if (!CmpModuleIdentifier(ass, a->Module, Builtin_Module) ||
  1355. !CmpModuleIdentifier(ass, a->Module, Builtin_Character_Module))
  1356. return;
  1357. /* non-main module elements will require long names and are only */
  1358. /* generated if they are referenced by elements of the main module */
  1359. if (CmpModuleIdentifier(ass, a->Module, mainmodule)) {
  1360. a->Flags |= eAssignmentFlags_LongName;
  1361. return;
  1362. }
  1363. /* mark type/value for generation */
  1364. switch (a->Type) {
  1365. case eAssignment_Type:
  1366. MarkTypeForGeneration(ass, a->U.Type.Type, 0);
  1367. break;
  1368. case eAssignment_Value:
  1369. MarkValueForGeneration(ass, a->U.Value.Value);
  1370. break;
  1371. }
  1372. }
  1373. /* clear done flags of types */
  1374. void
  1375. ClearTypeDone(Type_t *type)
  1376. {
  1377. Component_t *components;
  1378. type->Flags &= ~eTypeFlags_Done;
  1379. switch (type->Type) {
  1380. case eType_Sequence:
  1381. case eType_Set:
  1382. case eType_Choice:
  1383. case eType_InstanceOf:
  1384. for (components = type->U.SSC.Components; components;
  1385. components = components->Next) {
  1386. switch (components->Type) {
  1387. case eComponent_Normal:
  1388. case eComponent_Optional:
  1389. case eComponent_Default:
  1390. ClearTypeDone(components->U.NOD.NamedType->Type);
  1391. break;
  1392. }
  1393. }
  1394. break;
  1395. case eType_SequenceOf:
  1396. case eType_SetOf:
  1397. ClearTypeDone(type->U.SS.Type);
  1398. break;
  1399. }
  1400. }
  1401. /* clear done flags of values */
  1402. void
  1403. ClearValueDone(Value_t *value)
  1404. {
  1405. value->Flags &= ~eValueFlags_Done;
  1406. if (value->Type)
  1407. ClearTypeDone(value->Type);
  1408. }
  1409. /* clear done flags of assignments */
  1410. void
  1411. ClearDone(AssignmentList_t ass)
  1412. {
  1413. for (; ass; ass = ass->Next) {
  1414. switch (ass->Type) {
  1415. case eAssignment_Type:
  1416. ClearTypeDone(ass->U.Type.Type);
  1417. break;
  1418. case eAssignment_Value:
  1419. ClearValueDone(ass->U.Value.Value);
  1420. break;
  1421. }
  1422. }
  1423. }
  1424. /* examination of assignments */
  1425. void Examination(AssignmentList_t *ass, ModuleIdentifier_t *mainmodule)
  1426. {
  1427. Assignment_t *a, *nexta, **aa;
  1428. Type_t *subtype;
  1429. Value_t *subvalue;
  1430. ObjectClass_t *subobjclass;
  1431. Object_t *subobject;
  1432. ObjectSet_t *subobjset;
  1433. int redo;
  1434. /* drop results of previous passes */
  1435. for (aa = ass; *aa;) {
  1436. if ((*aa)->Type == eAssignment_NextPass)
  1437. *aa = NULL;
  1438. else
  1439. aa = &(*aa)->Next;
  1440. }
  1441. /* reverse order of assignments to get the original order */
  1442. for (a = *ass, *ass = NULL; a; a = nexta) {
  1443. nexta = a->Next;
  1444. a->Next = *ass;
  1445. *ass = a;
  1446. }
  1447. /* replace references from IMPORT by corresponding type-/value-/...-refs */
  1448. for (a = *ass; a; a = a->Next) {
  1449. if (a->Type == eAssignment_Reference) {
  1450. a->Type = GetAssignmentType(*ass, a);
  1451. switch (a->Type) {
  1452. case eAssignment_Type:
  1453. subtype = NewType(eType_Reference);
  1454. subtype->U.Reference.Identifier = a->U.Reference.Identifier;
  1455. subtype->U.Reference.Module = a->U.Reference.Module;
  1456. a->U.Type.Type = subtype;
  1457. break;
  1458. case eAssignment_Value:
  1459. subvalue = NewValue(NULL, NULL);
  1460. subvalue->U.Reference.Identifier = a->U.Reference.Identifier;
  1461. subvalue->U.Reference.Module = a->U.Reference.Module;
  1462. a->U.Value.Value = subvalue;
  1463. break;
  1464. case eAssignment_ObjectClass:
  1465. subobjclass = NewObjectClass(eObjectClass_Reference);
  1466. subobjclass->U.Reference.Identifier = a->U.Reference.Identifier;
  1467. subobjclass->U.Reference.Module = a->U.Reference.Module;
  1468. a->U.ObjectClass.ObjectClass = subobjclass;
  1469. break;
  1470. case eAssignment_Object:
  1471. subobject = NewObject(eObject_Reference);
  1472. subobject->U.Reference.Identifier = a->U.Reference.Identifier;
  1473. subobject->U.Reference.Module = a->U.Reference.Module;
  1474. a->U.Object.Object = subobject;
  1475. break;
  1476. case eAssignment_ObjectSet:
  1477. subobjset = NewObjectSet(eObjectSet_Reference);
  1478. subobjset->U.Reference.Identifier = a->U.Reference.Identifier;
  1479. subobjset->U.Reference.Module = a->U.Reference.Module;
  1480. a->U.ObjectSet.ObjectSet = subobjset;
  1481. break;
  1482. default:
  1483. MyAbort();
  1484. }
  1485. }
  1486. }
  1487. /* name types in types, values in types, types of values, types of objects*/
  1488. do {
  1489. redo = 0;
  1490. for (a = *ass; a; a = a->Next) {
  1491. switch (a->Type) {
  1492. case eAssignment_Type:
  1493. redo |= NameSubType(ass, Identifier2C(a->Identifier),
  1494. a->U.Type.Type, a->Module);
  1495. redo |= NameValueOfType(ass, Identifier2C(a->Identifier),
  1496. a->U.Type.Type, a->Module);
  1497. break;
  1498. case eAssignment_Value:
  1499. redo |= NameTypeOfValue(ass, Identifier2C(a->Identifier),
  1500. a->U.Value.Value, a->Module);
  1501. break;
  1502. case eAssignment_Object:
  1503. redo |= NameSettingsOfObject(ass,
  1504. Identifier2C(a->Identifier),
  1505. a->U.Object.Object, a->Module);
  1506. break;
  1507. }
  1508. }
  1509. } while (redo);
  1510. /* name identification of embedded pdv/character strings */
  1511. for (a = *ass; a; a = a->Next) {
  1512. if (a->Type == eAssignment_Type)
  1513. NameIdentificationValueOfType(ass, Identifier2C(a->Identifier),
  1514. a->U.Type.Type, a->Module);
  1515. }
  1516. /* mark types that will be automatically tagged */
  1517. for (a = *ass; a; a = a->Next) {
  1518. if (a->Type == eAssignment_Type)
  1519. MarkTypeForAutotagging(*ass, a->U.Type.Type);
  1520. }
  1521. /* replace components of by corresponding components */
  1522. for (a = *ass; a; a = a->Next) {
  1523. if (a->Type == eAssignment_Type)
  1524. a->U.Type.Type = RebuildTypeWithoutComponentsOf(*ass,
  1525. a->U.Type.Type);
  1526. }
  1527. /* replace selection types by corresponding component types */
  1528. for (a = *ass; a; a = a->Next) {
  1529. if (a->Type == eAssignment_Type)
  1530. a->U.Type.Type = RebuildTypeWithoutSelectionType(*ass,
  1531. a->U.Type.Type);
  1532. }
  1533. /* perform automatic tagging */
  1534. for (a = *ass; a; a = a->Next) {
  1535. if (a->Type == eAssignment_Type)
  1536. AutotagType(*ass, a->U.Type.Type);
  1537. }
  1538. /* perform automatic extension */
  1539. ClearDone(*ass);
  1540. for (a = *ass; a; a = a->Next) {
  1541. if (a->Type == eAssignment_Type)
  1542. AutoextendType(*ass, a->U.Type.Type);
  1543. }
  1544. /* build tags of Sequence/Set/Choice/InstanceOf */
  1545. ClearDone(*ass);
  1546. for (a = *ass; a; a = a->Next)
  1547. {
  1548. if (a->Type == eAssignment_Type)
  1549. {
  1550. switch (a->U.Type.Type->Type)
  1551. {
  1552. case eType_Sequence:
  1553. case eType_Set:
  1554. case eType_Choice:
  1555. case eType_InstanceOf:
  1556. BuildTags(*ass, a->U.Type.Type, a->eDefTagType);
  1557. break;
  1558. }
  1559. }
  1560. }
  1561. /* build tags of other types */
  1562. ClearDone(*ass);
  1563. for (a = *ass; a; a = a->Next)
  1564. {
  1565. if (a->Type == eAssignment_Type)
  1566. {
  1567. switch (a->U.Type.Type->Type)
  1568. {
  1569. case eType_Sequence:
  1570. case eType_Set:
  1571. case eType_Choice:
  1572. case eType_InstanceOf:
  1573. break;
  1574. default:
  1575. BuildTags(*ass, a->U.Type.Type, a->eDefTagType);
  1576. break;
  1577. }
  1578. }
  1579. }
  1580. /* sort set and choice types by tag */
  1581. for (a = *ass; a; a = a->Next) {
  1582. if (a->Type == eAssignment_Type)
  1583. SortTypeTags(*ass, a->U.Type.Type);
  1584. }
  1585. /* check for duplicate tags */
  1586. for (a = *ass; a; a = a->Next) {
  1587. if (a->Type == eAssignment_Type)
  1588. CheckTags(*ass, a->U.Type.Type);
  1589. }
  1590. /* derive constraints of referenced types to referencing types */
  1591. ClearDone(*ass);
  1592. for (a = *ass; a; a = a->Next) {
  1593. if (a->Type == eAssignment_Type)
  1594. BuildConstraints(*ass, a->U.Type.Type);
  1595. }
  1596. /* derive constraints of referenced types to referencing types of values */
  1597. for (a = *ass; a; a = a->Next) {
  1598. if (a->Type == eAssignment_Value)
  1599. BuildConstraints(*ass, GetValue(*ass, a->U.Value.Value)->Type);
  1600. }
  1601. /* examine directives of types */
  1602. ClearDone(*ass);
  1603. for (a = *ass; a; a = a->Next) {
  1604. if (a->Type == eAssignment_Type)
  1605. BuildDirectives(*ass, a->U.Type.Type, 0);
  1606. }
  1607. /* examine types to be empty/simple/choice of nulls */
  1608. ClearDone(*ass);
  1609. for (a = *ass; a; a = a->Next) {
  1610. if (a->Type == eAssignment_Type)
  1611. BuildTypeFlags(*ass, a->U.Type.Type);
  1612. }
  1613. /* mark types and values that shall be generated */
  1614. ClearDone(*ass);
  1615. for (a = *ass; a; a = a->Next) {
  1616. MarkForGeneration(*ass, mainmodule, a);
  1617. }
  1618. /* sort assignments so that no forward references will be needed */
  1619. SortAssignedTypes(ass);
  1620. }