/******************************************************************************
 * Copyright (c) 2000-2025 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:
 *   Balasko, Jeno
 *   Raduly, Csaba
 *
 ******************************************************************************/
#ifndef COMPFIELD_HH_
#define COMPFIELD_HH_

#include "Setting.hh"

class RawAST;

namespace Common {

/**
 * A class to represent a field of a compound type (sequence/record,
 * set and choice/union).
 */
class CompField : public Node, public Location {
private:
  /** The name of the field. Owned by the CompField. */
  Identifier *name;
  /** The type of the field. Owned. */
  Type *type;
  bool is_optional;
  bool is_extension_addition;
  /** Default value or 0 if no default value. Owned. */
  Value *defval;
  /** @default modifier */
  bool default_modifier;
  /** Indicates whether its an embedded field or not.
    * An embedded field is a field with only a type reference and no field name. */
  bool is_embedded;
  /** Raw attributes or 0. Owned */
  RawAST *rawattrib;
  /** Copy constructor not implemented */
  CompField(const CompField& p);
  /** Assignment disabled */
  CompField& operator=(const CompField& p);
public:
  CompField(Identifier *p_name, Type *p_type, bool p_is_optional=false,
    Value *p_defval=0, bool p_default_modifier = false);
  CompField(Type* p_type, bool p_is_optional = false, bool p_default_modifier = false);
  virtual ~CompField();
  virtual CompField *clone() const;
  virtual void set_fullname(const string& p_fullname);
  void set_my_scope(Scope *p_scope);
  void set_is_extension_addition() { is_extension_addition = true; }
  const Identifier& get_name();
  Type *get_type() const { return type; }
  RawAST* get_raw_attrib() const { return rawattrib; }
  void set_raw_attrib(RawAST* r_attr);
  /** Extension additions can be mandatory or optional in ASN.1, but they
    * automatically become optional when imported into TTCN-3 code. */
  bool get_is_optional(bool p_from_ttcn = true) const
    { return is_optional || (p_from_ttcn && is_extension_addition); }
  bool has_default() const { return defval != 0; }
  Value *get_defval() const { return defval; }
  bool has_default_modifier() const { return default_modifier; }
  bool get_is_embedded() const { return is_embedded; }
  virtual void dump(unsigned level) const;
};

/**
 * Class to represent a map of TTCN-3 StructFieldDef (BNF
 * 23). Create a CompFieldMap and use add_comp(CompField *) to add
 * each StructFieldDef (a.k.a. CompRef) into it! Finally, pass
 * CompFieldMap* to constructors of record/set/union types!
 */
class CompFieldMap : public Node {
private:
  /** Maps the name of the component to the actual component. */
  map<string, CompField> m;
  /** Contains pointers to the individual CompField s.
   * The CompFieldMap owns the CompFields and will free them. */
  vector<CompField> v;
  /** Pointer to the union alternative with the '@default' modifier.
   * Null if there is none, or if the structure is not a union. */
  CompField* default_alt;
  /** Points to the owner type, which shall be a TTCN-3 record, set or
   * union or an ASN.1 open type.
   * The CompFieldMap does not own the type. */
  Type *my_type;
  /** Indicates whether the uniqueness of field identifiers has been
   * verified. */
  bool checked;

  CompFieldMap(const CompFieldMap& p);
  /** Assignment disabled */
  CompFieldMap& operator=(const CompFieldMap& p);
public:
  CompFieldMap() : Node(), m(), v(), default_alt(NULL), my_type(0), checked(false) {}
  virtual ~CompFieldMap();
  virtual CompFieldMap *clone() const;
  virtual void set_fullname(const string& p_fullname);
  virtual void set_my_scope(Scope *p_scope);
  void set_my_type(Type *p_my_type) { my_type = p_my_type; }
  void add_comp(CompField *comp);
  size_t get_nof_comps() const { return v.size(); }
  CompField* get_comp_byIndex(size_t n) const { return v[n]; }
  bool has_comp_withName(const Identifier& p_name);
  CompField* get_comp_byName(const Identifier& p_name);
  CompField* get_default();
private:
  const char *get_typetype_name() const;
  /** Check the uniqueness of field identifiers.
   * Builds the map \c m in the process. */
  void chk_uniq();
public:
  void chk();
  virtual void dump(unsigned level) const;
};


} /* namespace Common */
#endif /* COMPFIELD_HH_ */
