1818
1919#include " language.h"
2020
21+ #include < util/invariant.h>
22+ #include < util/namespace.h>
23+
2124struct language_entryt
2225{
2326 language_factoryt factory;
@@ -39,15 +42,51 @@ void register_language(language_factoryt factory)
3942
4043std::unique_ptr<languaget> get_language_from_mode (const irep_idt &mode)
4144{
42- for (languagest::const_iterator it=languages.begin ();
43- it!=languages.end ();
44- it++)
45- if (mode==it->mode )
46- return it->factory ();
45+ for (const auto &language : languages)
46+ if (mode == language.mode )
47+ return language.factory ();
4748
4849 return nullptr ;
4950}
5051
52+ // / Get the mode of the given identifier's symbol
53+ // / \param ns: a namespace
54+ // / \param identifier: an identifier
55+ // / \return the mode, e.g. `ID_C`, if the identifier is in the given
56+ // / symbol table, or `ID_unknown` otherwise
57+ const irep_idt &
58+ get_mode_from_identifier (const namespacet &ns, const irep_idt &identifier)
59+ {
60+ if (identifier.empty ())
61+ return ID_unknown;
62+ const symbolt *symbol;
63+ if (ns.lookup (identifier, symbol))
64+ return ID_unknown;
65+ return symbol->mode ;
66+ }
67+
68+ // / Get the language corresponding to the mode of the given identifier's symbol
69+ // / \param ns: a namespace
70+ // / \param identifier: an identifier
71+ // / \return the corresponding language if the mode is not `ID_unknown`, or
72+ // / the default language otherwise;
73+ // / Note: It is assumed as an invariant that languages of symbols in the symbol
74+ // / table have been registered.
75+ std::unique_ptr<languaget>
76+ get_language_from_identifier (const namespacet &ns, const irep_idt &identifier)
77+ {
78+ const irep_idt &mode = get_mode_from_identifier (ns, identifier);
79+ if (mode == ID_unknown)
80+ return get_default_language ();
81+
82+ std::unique_ptr<languaget> language = get_language_from_mode (mode);
83+ INVARIANT (
84+ language,
85+ " symbol `" + id2string (identifier) + " ' has unknown mode '" +
86+ id2string (mode) + " '" );
87+ return language;
88+ }
89+
5190std::unique_ptr<languaget> get_language_from_filename (
5291 const std::string &filename)
5392{
@@ -85,6 +124,6 @@ std::unique_ptr<languaget> get_language_from_filename(
85124
86125std::unique_ptr<languaget> get_default_language ()
87126{
88- assert (!languages.empty ());
127+ PRECONDITION (!languages.empty ());
89128 return languages.front ().factory ();
90129}
0 commit comments