cppManifest.cxx 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /**
  2. * PANDA 3D SOFTWARE
  3. * Copyright (c) Carnegie Mellon University. All rights reserved.
  4. *
  5. * All use of this software is subject to the terms of the revised BSD
  6. * license. You should have received a copy of this license along
  7. * with this source code in a file named "LICENSE."
  8. *
  9. * @file cppManifest.cxx
  10. * @author drose
  11. * @date 1999-10-22
  12. */
  13. #include "cppManifest.h"
  14. #include "cppExpression.h"
  15. #include <ctype.h>
  16. /**
  17. *
  18. */
  19. CPPManifest::ExpansionNode::
  20. ExpansionNode(int parm_number, bool stringify, bool paste) :
  21. _parm_number(parm_number), _stringify(stringify), _paste(paste)
  22. {
  23. }
  24. /**
  25. *
  26. */
  27. CPPManifest::ExpansionNode::
  28. ExpansionNode(const string &str, bool paste) :
  29. _parm_number(-1), _stringify(false), _paste(paste), _str(str)
  30. {
  31. }
  32. /**
  33. * Creates a manifest from a preprocessor definition.
  34. */
  35. CPPManifest::
  36. CPPManifest(const string &args, const cppyyltype &loc) :
  37. _variadic_param(-1),
  38. _loc(loc),
  39. _expr((CPPExpression *)NULL),
  40. _vis(V_public)
  41. {
  42. assert(!args.empty());
  43. assert(!isspace(args[0]));
  44. // First, identify the manifest name.
  45. size_t p = 0;
  46. while (p < args.size() && !isspace(args[p]) && args[p] != '(') {
  47. p++;
  48. }
  49. _name = args.substr(0, p);
  50. vector_string parameter_names;
  51. if (args[p] == '(') {
  52. // Hmm, parameters.
  53. _has_parameters = true;
  54. parse_parameters(args, p, parameter_names);
  55. _num_parameters = parameter_names.size();
  56. p++;
  57. } else {
  58. _has_parameters = false;
  59. _num_parameters = 0;
  60. }
  61. // Now identify the expansion. Skip whitespace.
  62. while (p < args.size() && isspace(args[p])) {
  63. p++;
  64. }
  65. save_expansion(args.substr(p), parameter_names);
  66. }
  67. /**
  68. * Creates a custom manifest definition, for example as specified from a
  69. * command-line -D option.
  70. */
  71. CPPManifest::
  72. CPPManifest(const string &macro, const string &definition) :
  73. _variadic_param(-1),
  74. _expr((CPPExpression *)NULL),
  75. _vis(V_public)
  76. {
  77. _loc.first_line = 0;
  78. _loc.first_column = 0;
  79. _loc.last_line = 0;
  80. _loc.last_column = 0;
  81. assert(!macro.empty());
  82. assert(!isspace(macro[0]));
  83. // First, identify the manifest name.
  84. size_t p = 0;
  85. while (p < macro.size() && !isspace(macro[p]) && macro[p] != '(') {
  86. p++;
  87. }
  88. _name = macro.substr(0, p);
  89. vector_string parameter_names;
  90. if (macro[p] == '(') {
  91. // Hmm, parameters.
  92. _has_parameters = true;
  93. parse_parameters(macro, p, parameter_names);
  94. _num_parameters = parameter_names.size();
  95. p++;
  96. } else {
  97. _has_parameters = false;
  98. _num_parameters = 0;
  99. }
  100. save_expansion(definition, parameter_names);
  101. }
  102. /**
  103. *
  104. */
  105. CPPManifest::
  106. ~CPPManifest() {
  107. if (_expr != (CPPExpression *)NULL) {
  108. delete _expr;
  109. }
  110. }
  111. /**
  112. * This implements the stringification operator, #.
  113. */
  114. string CPPManifest::
  115. stringify(const string &source) {
  116. string result("\"");
  117. enum {
  118. S_escaped = 0x01,
  119. S_single_quoted = 0x02,
  120. S_double_quoted = 0x04,
  121. S_quoted = 0x06,
  122. };
  123. int state = 0;
  124. string::const_iterator it;
  125. for (it = source.begin(); it != source.end(); ++it) {
  126. char c = *it;
  127. if ((state & S_escaped) == 0) {
  128. switch (c) {
  129. case '\\':
  130. if (state & S_quoted) {
  131. state |= S_escaped;
  132. result += '\\';
  133. }
  134. break;
  135. case '\'':
  136. state ^= S_single_quoted;
  137. break;
  138. case '"':
  139. state ^= S_double_quoted;
  140. result += '\\';
  141. break;
  142. }
  143. } else {
  144. if (c == '\\' || c == '"') {
  145. result += '\\';
  146. }
  147. state &= ~S_escaped;
  148. }
  149. result += c;
  150. }
  151. result += '"';
  152. return result;
  153. }
  154. /**
  155. *
  156. */
  157. string CPPManifest::
  158. expand(const vector_string &args) const {
  159. string result;
  160. Expansion::const_iterator ei;
  161. for (ei = _expansion.begin(); ei != _expansion.end(); ++ei) {
  162. if ((*ei)._parm_number >= 0) {
  163. int i = (*ei)._parm_number;
  164. string subst;
  165. if (i < (int)args.size()) {
  166. subst = args[i];
  167. if (i == _variadic_param) {
  168. for (++i; i < (int)args.size(); ++i) {
  169. subst += ", " + args[i];
  170. }
  171. }
  172. if ((*ei)._stringify) {
  173. subst = stringify(subst);
  174. }
  175. } else if (i == _variadic_param && (*ei)._paste) {
  176. // Special case GCC behavior: if __VA_ARGS__ is pasted to a comma and
  177. // no arguments are passed, the comma is removed. MSVC does this
  178. // automatically. Not sure if we should allow MSVC behavior as well.
  179. if (!result.empty() && *result.rbegin() == ',') {
  180. result.resize(result.size() - 1);
  181. }
  182. }
  183. if (!subst.empty()) {
  184. if (result.empty() || (*ei)._paste) {
  185. result += subst;
  186. } else {
  187. result += ' ';
  188. result += subst;
  189. }
  190. }
  191. }
  192. if (!(*ei)._str.empty()) {
  193. if (result.empty() || (*ei)._paste) {
  194. result += (*ei)._str;
  195. } else {
  196. result += ' ';
  197. result += (*ei)._str;
  198. }
  199. }
  200. }
  201. return result;
  202. }
  203. /**
  204. * Returns the type of the manifest, if it is known, or NULL if the type
  205. * cannot be determined.
  206. */
  207. CPPType *CPPManifest::
  208. determine_type() const {
  209. if (_expr != (CPPExpression *)NULL) {
  210. return _expr->determine_type();
  211. }
  212. return (CPPType *)NULL;
  213. }
  214. /**
  215. *
  216. */
  217. void CPPManifest::
  218. output(ostream &out) const {
  219. out << _name;
  220. if (_has_parameters) {
  221. out << "(";
  222. if (_num_parameters > 0) {
  223. if (_variadic_param == 0) {
  224. out << "...";
  225. } else {
  226. out << "$1";
  227. }
  228. for (int i = 1; i < _num_parameters; ++i) {
  229. if (_variadic_param == i) {
  230. out << ", ...";
  231. } else {
  232. out << ", $" << i + 1;
  233. }
  234. }
  235. }
  236. out << ")";
  237. }
  238. Expansion::const_iterator ei;
  239. for (ei = _expansion.begin(); ei != _expansion.end(); ++ei) {
  240. if ((*ei)._paste) {
  241. out << " ## ";
  242. } else {
  243. out << " ";
  244. }
  245. if ((*ei)._parm_number >= 0) {
  246. if ((*ei)._stringify) {
  247. out << "#";
  248. }
  249. if ((*ei)._parm_number == _variadic_param) {
  250. out << "__VA_ARGS__";
  251. } else {
  252. out << "$" << (*ei)._parm_number + 1;
  253. }
  254. }
  255. if (!(*ei)._str.empty()) {
  256. out << (*ei)._str;
  257. }
  258. }
  259. }
  260. /**
  261. *
  262. */
  263. void CPPManifest::
  264. parse_parameters(const string &args, size_t &p,
  265. vector_string &parameter_names) {
  266. assert(p < args.size());
  267. assert(args[p] == '(');
  268. p++;
  269. while (p < args.size() && isspace(args[p])) {
  270. p++;
  271. }
  272. while (p < args.size() && args[p] != ')') {
  273. // Here's the beginning of a parm.
  274. size_t q = p;
  275. while (p < args.size() && !isspace(args[p]) &&
  276. args[p] != ')' && args[p] != ',') {
  277. p++;
  278. }
  279. // Check if it's a variadic parameter by checking if it ends with "...".
  280. // This picks up both C99-style variadic macros and GCC-style variadic
  281. // macros.
  282. if (p - q >= 3 && args.compare(p - 3, 3, "...") == 0) {
  283. _variadic_param = parameter_names.size();
  284. parameter_names.push_back(args.substr(q, p - q - 3));
  285. } else {
  286. parameter_names.push_back(args.substr(q, p - q));
  287. }
  288. // Skip whitespace after the parameter name.
  289. while (p < args.size() && isspace(args[p])) {
  290. p++;
  291. }
  292. if (p < args.size() && args[p] == ',') {
  293. p++;
  294. // Skip whitespace after a comma.
  295. while (p < args.size() && isspace(args[p])) {
  296. p++;
  297. }
  298. }
  299. }
  300. }
  301. /**
  302. *
  303. */
  304. void CPPManifest::
  305. save_expansion(const string &exp, const vector_string &parameter_names) {
  306. // Walk through the expansion string. For each substring that is an
  307. // identifier, check it against parameter_names.
  308. size_t p = 0;
  309. size_t last = 0;
  310. bool stringify = false;
  311. bool paste = false;
  312. while (p < exp.size()) {
  313. if (isalpha(exp[p]) || exp[p] == '_') {
  314. // Here's the start of an identifier. Find the end of it.
  315. size_t q = p;
  316. p++;
  317. while (p < exp.size() && (isalnum(exp[p]) || exp[p] == '_')) {
  318. p++;
  319. }
  320. string ident = exp.substr(q, p - q);
  321. // Is this identifier one of our parameters?
  322. int pnum = -1;
  323. if (ident == "__VA_ARGS__") {
  324. // C99-style variadics, ie. #define macro(...) __VA_ARGS__
  325. pnum = _variadic_param;
  326. } else {
  327. for (int i = 0; pnum == -1 && i < (int)parameter_names.size(); ++i) {
  328. const string &pname = parameter_names[i];
  329. if (pname == ident) {
  330. pnum = i;
  331. }
  332. }
  333. }
  334. if (pnum != -1) {
  335. // Yep!
  336. if (last != q) {
  337. _expansion.push_back(ExpansionNode(exp.substr(last, q - last), paste));
  338. paste = false;
  339. }
  340. _expansion.push_back(ExpansionNode(pnum, stringify, paste));
  341. stringify = false;
  342. paste = false;
  343. last = p;
  344. }
  345. } else if (exp[p] == '#') {
  346. // This may be a stringification operator.
  347. if (last != p) {
  348. _expansion.push_back(ExpansionNode(exp.substr(last, p - last), paste));
  349. paste = false;
  350. }
  351. ++p;
  352. if (p < exp.size() && exp[p] == '#') {
  353. // Woah, this is a token-pasting operator.
  354. paste = true;
  355. ++p;
  356. } else {
  357. // Mark that the next argument should be stringified.
  358. stringify = true;
  359. }
  360. last = p;
  361. } else if (isspace(exp[p])) {
  362. if (last != p) {
  363. _expansion.push_back(ExpansionNode(exp.substr(last, p - last), paste));
  364. paste = false;
  365. }
  366. ++p;
  367. last = p;
  368. } else {
  369. ++p;
  370. }
  371. }
  372. if (last != p) {
  373. _expansion.push_back(ExpansionNode(exp.substr(last, p - last), paste));
  374. }
  375. }