18
18
19
19
#include " language.h"
20
20
21
+ #include < util/invariant.h>
22
+ #include < util/namespace.h>
23
+
21
24
struct language_entryt
22
25
{
23
26
language_factoryt factory;
@@ -39,15 +42,51 @@ void register_language(language_factoryt factory)
39
42
40
43
std::unique_ptr<languaget> get_language_from_mode (const irep_idt &mode)
41
44
{
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 ();
47
48
48
49
return nullptr ;
49
50
}
50
51
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
+
51
90
std::unique_ptr<languaget> get_language_from_filename (
52
91
const std::string &filename)
53
92
{
@@ -85,6 +124,6 @@ std::unique_ptr<languaget> get_language_from_filename(
85
124
86
125
std::unique_ptr<languaget> get_default_language ()
87
126
{
88
- assert (!languages.empty ());
127
+ PRECONDITION (!languages.empty ());
89
128
return languages.front ().factory ();
90
129
}
0 commit comments