Shinichiro Hamaji | 1d545aa | 2015-06-23 15:29:13 +0900 | [diff] [blame] | 1 | // Copyright 2015 Google Inc. All rights reserved |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Fumitoshi Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame] | 15 | // +build ignore |
| 16 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 17 | #include "func.h" |
| 18 | |
Shinichiro Hamaji | 62b16e7 | 2015-07-02 01:42:31 +0900 | [diff] [blame] | 19 | #include <errno.h> |
Shinichiro Hamaji | d527145 | 2015-06-17 18:50:03 +0900 | [diff] [blame] | 20 | #include <limits.h> |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 21 | #include <stdio.h> |
Shinichiro Hamaji | d527145 | 2015-06-17 18:50:03 +0900 | [diff] [blame] | 22 | #include <stdlib.h> |
Shinichiro Hamaji | 62b16e7 | 2015-07-02 01:42:31 +0900 | [diff] [blame] | 23 | #include <unistd.h> |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 24 | |
Shinichiro Hamaji | d87e59e | 2015-06-17 18:18:34 +0900 | [diff] [blame] | 25 | #include <algorithm> |
Shinichiro Hamaji | d527145 | 2015-06-17 18:50:03 +0900 | [diff] [blame] | 26 | #include <iterator> |
Shinichiro Hamaji | cf0cd68 | 2015-06-18 16:18:13 +0900 | [diff] [blame] | 27 | #include <memory> |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 28 | #include <unordered_map> |
| 29 | |
Shinichiro Hamaji | 80456fb | 2015-06-18 14:56:10 +0900 | [diff] [blame] | 30 | #include "ast.h" |
Shinichiro Hamaji | 9619b36 | 2015-06-16 16:13:25 +0900 | [diff] [blame] | 31 | #include "eval.h" |
Shinichiro Hamaji | 94d6f2a | 2015-07-05 05:32:25 +0900 | [diff] [blame] | 32 | #include "fileutil.h" |
Shinichiro Hamaji | 5f57a99 | 2015-06-30 19:39:39 +0900 | [diff] [blame] | 33 | #include "find.h" |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 34 | #include "log.h" |
Shinichiro Hamaji | 80456fb | 2015-06-18 14:56:10 +0900 | [diff] [blame] | 35 | #include "parser.h" |
Shinichiro Hamaji | 0d8e79b | 2015-06-30 03:29:35 +0900 | [diff] [blame] | 36 | #include "stats.h" |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 37 | #include "strutil.h" |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 38 | #include "symtab.h" |
Shinichiro Hamaji | cf0cd68 | 2015-06-18 16:18:13 +0900 | [diff] [blame] | 39 | #include "var.h" |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 40 | |
| 41 | namespace { |
| 42 | |
Shinichiro Hamaji | 2e6cbfc | 2015-06-16 18:46:50 +0900 | [diff] [blame] | 43 | void PatsubstFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
Shinichiro Hamaji | a6a17a4 | 2015-06-18 20:11:19 +0900 | [diff] [blame] | 44 | shared_ptr<string> pat_str = args[0]->Eval(ev); |
Shinichiro Hamaji | 2e6cbfc | 2015-06-16 18:46:50 +0900 | [diff] [blame] | 45 | shared_ptr<string> repl = args[1]->Eval(ev); |
| 46 | shared_ptr<string> str = args[2]->Eval(ev); |
Shinichiro Hamaji | 37591ce | 2015-06-16 19:36:05 +0900 | [diff] [blame] | 47 | WordWriter ww(s); |
Shinichiro Hamaji | a6a17a4 | 2015-06-18 20:11:19 +0900 | [diff] [blame] | 48 | Pattern pat(*pat_str); |
Shinichiro Hamaji | 2e6cbfc | 2015-06-16 18:46:50 +0900 | [diff] [blame] | 49 | for (StringPiece tok : WordScanner(*str)) { |
Shinichiro Hamaji | 37591ce | 2015-06-16 19:36:05 +0900 | [diff] [blame] | 50 | ww.MaybeAddWhitespace(); |
Shinichiro Hamaji | a6a17a4 | 2015-06-18 20:11:19 +0900 | [diff] [blame] | 51 | pat.AppendSubst(tok, *repl, s); |
Shinichiro Hamaji | 2e6cbfc | 2015-06-16 18:46:50 +0900 | [diff] [blame] | 52 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 53 | } |
| 54 | |
Shinichiro Hamaji | 37591ce | 2015-06-16 19:36:05 +0900 | [diff] [blame] | 55 | void StripFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 56 | shared_ptr<string> str = args[0]->Eval(ev); |
| 57 | WordWriter ww(s); |
| 58 | for (StringPiece tok : WordScanner(*str)) { |
| 59 | ww.Write(tok); |
| 60 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 61 | } |
| 62 | |
Shinichiro Hamaji | 37591ce | 2015-06-16 19:36:05 +0900 | [diff] [blame] | 63 | void SubstFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 64 | shared_ptr<string> pat = args[0]->Eval(ev); |
| 65 | shared_ptr<string> repl = args[1]->Eval(ev); |
| 66 | shared_ptr<string> str = args[2]->Eval(ev); |
Shinichiro Hamaji | 5af931d | 2015-07-06 15:37:59 +0900 | [diff] [blame] | 67 | if (pat->empty()) { |
| 68 | *s += *str; |
| 69 | *s += *repl; |
| 70 | return; |
| 71 | } |
Shinichiro Hamaji | 37591ce | 2015-06-16 19:36:05 +0900 | [diff] [blame] | 72 | size_t index = 0; |
| 73 | while (index < str->size()) { |
| 74 | size_t found = str->find(*pat, index); |
| 75 | if (found == string::npos) |
| 76 | break; |
| 77 | AppendString(StringPiece(*str).substr(index, found - index), s); |
| 78 | AppendString(*repl, s); |
| 79 | index = found + pat->size(); |
| 80 | } |
| 81 | AppendString(StringPiece(*str).substr(index), s); |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 82 | } |
| 83 | |
Shinichiro Hamaji | 00cc658 | 2015-06-17 18:12:46 +0900 | [diff] [blame] | 84 | void FindstringFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 85 | shared_ptr<string> find = args[0]->Eval(ev); |
| 86 | shared_ptr<string> in = args[1]->Eval(ev); |
| 87 | if (in->find(*find) != string::npos) |
| 88 | AppendString(*find, s); |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 89 | } |
| 90 | |
Shinichiro Hamaji | 00cc658 | 2015-06-17 18:12:46 +0900 | [diff] [blame] | 91 | void FilterFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 92 | shared_ptr<string> pat_buf = args[0]->Eval(ev); |
| 93 | shared_ptr<string> text = args[1]->Eval(ev); |
Shinichiro Hamaji | a6a17a4 | 2015-06-18 20:11:19 +0900 | [diff] [blame] | 94 | vector<Pattern> pats; |
| 95 | for (StringPiece pat : WordScanner(*pat_buf)) { |
| 96 | pats.push_back(Pattern(pat)); |
| 97 | } |
Shinichiro Hamaji | 00cc658 | 2015-06-17 18:12:46 +0900 | [diff] [blame] | 98 | WordWriter ww(s); |
| 99 | for (StringPiece tok : WordScanner(*text)) { |
Shinichiro Hamaji | a6a17a4 | 2015-06-18 20:11:19 +0900 | [diff] [blame] | 100 | for (const Pattern& pat : pats) { |
| 101 | if (pat.Match(tok)) { |
Shinichiro Hamaji | 00cc658 | 2015-06-17 18:12:46 +0900 | [diff] [blame] | 102 | ww.Write(tok); |
| 103 | break; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void FilterOutFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 110 | shared_ptr<string> pat_buf = args[0]->Eval(ev); |
| 111 | shared_ptr<string> text = args[1]->Eval(ev); |
Shinichiro Hamaji | a6a17a4 | 2015-06-18 20:11:19 +0900 | [diff] [blame] | 112 | vector<Pattern> pats; |
| 113 | for (StringPiece pat : WordScanner(*pat_buf)) { |
| 114 | pats.push_back(Pattern(pat)); |
| 115 | } |
Shinichiro Hamaji | 00cc658 | 2015-06-17 18:12:46 +0900 | [diff] [blame] | 116 | WordWriter ww(s); |
| 117 | for (StringPiece tok : WordScanner(*text)) { |
| 118 | bool matched = false; |
Shinichiro Hamaji | a6a17a4 | 2015-06-18 20:11:19 +0900 | [diff] [blame] | 119 | for (const Pattern& pat : pats) { |
| 120 | if (pat.Match(tok)) { |
Shinichiro Hamaji | 00cc658 | 2015-06-17 18:12:46 +0900 | [diff] [blame] | 121 | matched = true; |
| 122 | break; |
| 123 | } |
| 124 | } |
| 125 | if (!matched) |
| 126 | ww.Write(tok); |
| 127 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 128 | } |
| 129 | |
Shinichiro Hamaji | d87e59e | 2015-06-17 18:18:34 +0900 | [diff] [blame] | 130 | void SortFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 131 | shared_ptr<string> list = args[0]->Eval(ev); |
| 132 | vector<StringPiece> toks; |
| 133 | WordScanner(*list).Split(&toks); |
| 134 | sort(toks.begin(), toks.end()); |
| 135 | WordWriter ww(s); |
| 136 | StringPiece prev; |
| 137 | for (StringPiece tok : toks) { |
| 138 | if (prev != tok) { |
| 139 | ww.Write(tok); |
| 140 | prev = tok; |
| 141 | } |
| 142 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 143 | } |
| 144 | |
Shinichiro Hamaji | d527145 | 2015-06-17 18:50:03 +0900 | [diff] [blame] | 145 | static int GetNumericValueForFunc(const string& buf) { |
| 146 | StringPiece s = TrimLeftSpace(buf); |
| 147 | char* end; |
| 148 | long n = strtol(s.data(), &end, 10); |
| 149 | if (n < 0 || n == LONG_MAX || s.data() + s.size() != end) { |
| 150 | return -1; |
| 151 | } |
| 152 | return n; |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 153 | } |
| 154 | |
Shinichiro Hamaji | d527145 | 2015-06-17 18:50:03 +0900 | [diff] [blame] | 155 | void WordFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 156 | shared_ptr<string> n_str = args[0]->Eval(ev); |
| 157 | int n = GetNumericValueForFunc(*n_str); |
| 158 | if (n < 0) { |
| 159 | ev->Error(StringPrintf( |
| 160 | "*** non-numeric first argument to `word' function: '%s'.", |
| 161 | n_str->c_str())); |
| 162 | } |
| 163 | if (n == 0) { |
| 164 | ev->Error("*** first argument to `word' function must be greater than 0."); |
| 165 | } |
| 166 | |
| 167 | shared_ptr<string> text = args[1]->Eval(ev); |
| 168 | for (StringPiece tok : WordScanner(*text)) { |
| 169 | n--; |
| 170 | if (n == 0) { |
| 171 | AppendString(tok, s); |
| 172 | break; |
| 173 | } |
| 174 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 175 | } |
| 176 | |
Shinichiro Hamaji | d527145 | 2015-06-17 18:50:03 +0900 | [diff] [blame] | 177 | void WordlistFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 178 | shared_ptr<string> s_str = args[0]->Eval(ev); |
| 179 | int si = GetNumericValueForFunc(*s_str); |
| 180 | if (si < 0) { |
| 181 | ev->Error(StringPrintf( |
| 182 | "*** non-numeric first argument to `wordlist' function: '%s'.", |
| 183 | s_str->c_str())); |
| 184 | } |
| 185 | if (si == 0) { |
| 186 | ev->Error(StringPrintf( |
| 187 | "*** invalid first argument to `wordlist' function: %s`", |
| 188 | s_str->c_str())); |
| 189 | } |
| 190 | |
| 191 | shared_ptr<string> e_str = args[1]->Eval(ev); |
| 192 | int ei = GetNumericValueForFunc(*e_str); |
| 193 | if (ei < 0) { |
| 194 | ev->Error(StringPrintf( |
| 195 | "*** non-numeric second argument to `wordlist' function: '%s'.", |
| 196 | e_str->c_str())); |
| 197 | } |
| 198 | |
| 199 | shared_ptr<string> text = args[2]->Eval(ev); |
| 200 | int i = 0; |
| 201 | WordWriter ww(s); |
| 202 | for (StringPiece tok : WordScanner(*text)) { |
| 203 | i++; |
| 204 | if (si <= i && i <= ei) { |
| 205 | ww.Write(tok); |
| 206 | } |
| 207 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 208 | } |
| 209 | |
Shinichiro Hamaji | d527145 | 2015-06-17 18:50:03 +0900 | [diff] [blame] | 210 | void WordsFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 211 | shared_ptr<string> text = args[0]->Eval(ev); |
| 212 | WordScanner ws(*text); |
| 213 | int n = 0; |
| 214 | for (auto iter = ws.begin(); iter != ws.end(); ++iter) |
| 215 | n++; |
| 216 | char buf[32]; |
| 217 | sprintf(buf, "%d", n); |
| 218 | *s += buf; |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 219 | } |
| 220 | |
Shinichiro Hamaji | d527145 | 2015-06-17 18:50:03 +0900 | [diff] [blame] | 221 | void FirstwordFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 222 | shared_ptr<string> text = args[0]->Eval(ev); |
| 223 | for (StringPiece tok : WordScanner(*text)) { |
| 224 | AppendString(tok, s); |
| 225 | return; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | void LastwordFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 230 | shared_ptr<string> text = args[0]->Eval(ev); |
| 231 | StringPiece last; |
| 232 | for (StringPiece tok : WordScanner(*text)) { |
| 233 | last = tok; |
| 234 | } |
| 235 | AppendString(last, s); |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 236 | } |
| 237 | |
Shinichiro Hamaji | 30b8e60 | 2015-06-17 19:28:54 +0900 | [diff] [blame] | 238 | void JoinFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 239 | shared_ptr<string> list1 = args[0]->Eval(ev); |
| 240 | shared_ptr<string> list2 = args[1]->Eval(ev); |
| 241 | WordScanner ws1(*list1); |
| 242 | WordScanner ws2(*list2); |
| 243 | WordWriter ww(s); |
| 244 | for (WordScanner::Iterator iter1 = ws1.begin(), iter2 = ws2.begin(); |
| 245 | iter1 != ws1.end() && iter2 != ws2.end(); |
| 246 | ++iter1, ++iter2) { |
| 247 | ww.Write(*iter1); |
| 248 | // Use |AppendString| not to append extra ' '. |
| 249 | AppendString(*iter2, s); |
| 250 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 251 | } |
| 252 | |
Shinichiro Hamaji | fcf1b76 | 2015-06-18 03:43:54 +0900 | [diff] [blame] | 253 | void WildcardFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
Shinichiro Hamaji | 0d8e79b | 2015-06-30 03:29:35 +0900 | [diff] [blame] | 254 | COLLECT_STATS("func wildcard time"); |
Shinichiro Hamaji | 284f3d1 | 2015-06-17 19:29:01 +0900 | [diff] [blame] | 255 | shared_ptr<string> pat = args[0]->Eval(ev); |
Shinichiro Hamaji | 7409aee | 2015-07-28 14:52:37 +0900 | [diff] [blame] | 256 | // Note GNU make does not delay the execution of $(wildcard) so we |
| 257 | // do not need to check avoid_io here. |
Shinichiro Hamaji | fcf1b76 | 2015-06-18 03:43:54 +0900 | [diff] [blame] | 258 | WordWriter ww(s); |
Shinichiro Hamaji | 0e3873a | 2015-07-05 15:48:28 +0900 | [diff] [blame] | 259 | vector<string>* files; |
Shinichiro Hamaji | 284f3d1 | 2015-06-17 19:29:01 +0900 | [diff] [blame] | 260 | for (StringPiece tok : WordScanner(*pat)) { |
Shinichiro Hamaji | 8f68bd3 | 2015-06-18 11:01:51 +0900 | [diff] [blame] | 261 | ScopedTerminator st(tok); |
Shinichiro Hamaji | 0e3873a | 2015-07-05 15:48:28 +0900 | [diff] [blame] | 262 | Glob(tok.data(), &files); |
| 263 | sort(files->begin(), files->end()); |
| 264 | for (const string& file : *files) { |
Shinichiro Hamaji | fe97c41 | 2015-06-29 16:57:57 +0900 | [diff] [blame] | 265 | ww.Write(file); |
| 266 | } |
Shinichiro Hamaji | 284f3d1 | 2015-06-17 19:29:01 +0900 | [diff] [blame] | 267 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 268 | } |
| 269 | |
Shinichiro Hamaji | 67f9a70 | 2015-06-18 06:00:57 +0900 | [diff] [blame] | 270 | void DirFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 271 | shared_ptr<string> text = args[0]->Eval(ev); |
| 272 | WordWriter ww(s); |
| 273 | for (StringPiece tok : WordScanner(*text)) { |
| 274 | ww.Write(Dirname(tok)); |
Shinichiro Hamaji | 5590685 | 2015-06-29 16:40:33 +0900 | [diff] [blame] | 275 | s->push_back('/'); |
Shinichiro Hamaji | 67f9a70 | 2015-06-18 06:00:57 +0900 | [diff] [blame] | 276 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 277 | } |
| 278 | |
Shinichiro Hamaji | 67f9a70 | 2015-06-18 06:00:57 +0900 | [diff] [blame] | 279 | void NotdirFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 280 | shared_ptr<string> text = args[0]->Eval(ev); |
| 281 | WordWriter ww(s); |
| 282 | for (StringPiece tok : WordScanner(*text)) { |
| 283 | if (tok == "/") { |
Shinichiro Hamaji | 388e858 | 2015-07-03 16:51:46 +0900 | [diff] [blame] | 284 | ww.Write(StringPiece("")); |
Shinichiro Hamaji | 67f9a70 | 2015-06-18 06:00:57 +0900 | [diff] [blame] | 285 | } else { |
| 286 | ww.Write(Basename(tok)); |
| 287 | } |
| 288 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 289 | } |
| 290 | |
Shinichiro Hamaji | 67f9a70 | 2015-06-18 06:00:57 +0900 | [diff] [blame] | 291 | void SuffixFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 292 | shared_ptr<string> text = args[0]->Eval(ev); |
| 293 | WordWriter ww(s); |
| 294 | for (StringPiece tok : WordScanner(*text)) { |
| 295 | StringPiece suf = GetExt(tok); |
| 296 | if (!suf.empty()) |
| 297 | ww.Write(suf); |
| 298 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 299 | } |
| 300 | |
Shinichiro Hamaji | 67f9a70 | 2015-06-18 06:00:57 +0900 | [diff] [blame] | 301 | void BasenameFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 302 | shared_ptr<string> text = args[0]->Eval(ev); |
| 303 | WordWriter ww(s); |
| 304 | for (StringPiece tok : WordScanner(*text)) { |
| 305 | ww.Write(StripExt(tok)); |
| 306 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 307 | } |
| 308 | |
Shinichiro Hamaji | 5d694f0 | 2015-06-18 06:05:36 +0900 | [diff] [blame] | 309 | void AddsuffixFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 310 | shared_ptr<string> suf = args[0]->Eval(ev); |
| 311 | shared_ptr<string> text = args[1]->Eval(ev); |
| 312 | WordWriter ww(s); |
| 313 | for (StringPiece tok : WordScanner(*text)) { |
| 314 | ww.Write(tok); |
| 315 | *s += *suf; |
| 316 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 317 | } |
| 318 | |
Shinichiro Hamaji | 5d694f0 | 2015-06-18 06:05:36 +0900 | [diff] [blame] | 319 | void AddprefixFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 320 | shared_ptr<string> pre = args[0]->Eval(ev); |
| 321 | shared_ptr<string> text = args[1]->Eval(ev); |
| 322 | WordWriter ww(s); |
| 323 | for (StringPiece tok : WordScanner(*text)) { |
| 324 | ww.Write(*pre); |
| 325 | AppendString(tok, s); |
| 326 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 327 | } |
| 328 | |
Shinichiro Hamaji | 8a96358 | 2015-06-18 07:05:58 +0900 | [diff] [blame] | 329 | void RealpathFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 330 | shared_ptr<string> text = args[0]->Eval(ev); |
Shinichiro Hamaji | df1fc8b | 2015-06-29 15:45:21 +0900 | [diff] [blame] | 331 | if (ev->avoid_io()) { |
Shinichiro Hamaji | 65dce54 | 2015-07-28 15:39:03 +0900 | [diff] [blame^] | 332 | *s += "$(realpath "; |
| 333 | *s += *text; |
| 334 | *s += " 2> /dev/null)"; |
Shinichiro Hamaji | df1fc8b | 2015-06-29 15:45:21 +0900 | [diff] [blame] | 335 | return; |
| 336 | } |
| 337 | |
Shinichiro Hamaji | 8a96358 | 2015-06-18 07:05:58 +0900 | [diff] [blame] | 338 | WordWriter ww(s); |
| 339 | for (StringPiece tok : WordScanner(*text)) { |
Shinichiro Hamaji | 8f68bd3 | 2015-06-18 11:01:51 +0900 | [diff] [blame] | 340 | ScopedTerminator st(tok); |
Shinichiro Hamaji | 8a96358 | 2015-06-18 07:05:58 +0900 | [diff] [blame] | 341 | char buf[PATH_MAX]; |
| 342 | if (realpath(tok.data(), buf)) |
| 343 | *s += buf; |
Shinichiro Hamaji | 8a96358 | 2015-06-18 07:05:58 +0900 | [diff] [blame] | 344 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 345 | } |
| 346 | |
Shinichiro Hamaji | 8a96358 | 2015-06-18 07:05:58 +0900 | [diff] [blame] | 347 | void AbspathFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 348 | shared_ptr<string> text = args[0]->Eval(ev); |
| 349 | WordWriter ww(s); |
| 350 | string buf; |
| 351 | for (StringPiece tok : WordScanner(*text)) { |
| 352 | AbsPath(tok, &buf); |
| 353 | ww.Write(buf); |
| 354 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 355 | } |
| 356 | |
Shinichiro Hamaji | fead3b7 | 2015-06-18 15:31:15 +0900 | [diff] [blame] | 357 | void IfFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 358 | shared_ptr<string> cond = args[0]->Eval(ev); |
| 359 | if (cond->empty()) { |
| 360 | if (args.size() > 2) |
| 361 | args[2]->Eval(ev, s); |
| 362 | } else { |
| 363 | args[1]->Eval(ev, s); |
| 364 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 365 | } |
| 366 | |
Shinichiro Hamaji | fead3b7 | 2015-06-18 15:31:15 +0900 | [diff] [blame] | 367 | void AndFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 368 | shared_ptr<string> cond; |
| 369 | for (Value* a : args) { |
| 370 | cond = a->Eval(ev); |
| 371 | if (cond->empty()) |
| 372 | return; |
| 373 | } |
| 374 | if (cond.get()) { |
| 375 | *s += *cond; |
| 376 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 377 | } |
| 378 | |
Shinichiro Hamaji | fead3b7 | 2015-06-18 15:31:15 +0900 | [diff] [blame] | 379 | void OrFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 380 | for (Value* a : args) { |
| 381 | shared_ptr<string> cond = a->Eval(ev); |
| 382 | if (!cond->empty()) { |
| 383 | *s += *cond; |
| 384 | return; |
| 385 | } |
| 386 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 387 | } |
| 388 | |
Shinichiro Hamaji | 81699be | 2015-06-22 18:07:38 +0900 | [diff] [blame] | 389 | void ValueFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 390 | shared_ptr<string> var_name = args[0]->Eval(ev); |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 391 | Var* var = ev->LookupVar(Intern(*var_name)); |
Shinichiro Hamaji | 81699be | 2015-06-22 18:07:38 +0900 | [diff] [blame] | 392 | AppendString(var->String().as_string(), s); |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 393 | } |
| 394 | |
Shinichiro Hamaji | fead3b7 | 2015-06-18 15:31:15 +0900 | [diff] [blame] | 395 | void EvalFunc(const vector<Value*>& args, Evaluator* ev, string*) { |
Shinichiro Hamaji | 76ff983 | 2015-06-18 17:11:22 +0900 | [diff] [blame] | 396 | // TODO: eval leaks everything... for now. |
| 397 | //shared_ptr<string> text = args[0]->Eval(ev); |
| 398 | string* text = new string; |
| 399 | args[0]->Eval(ev, text); |
Shinichiro Hamaji | 80456fb | 2015-06-18 14:56:10 +0900 | [diff] [blame] | 400 | vector<AST*> asts; |
| 401 | Parse(*text, ev->loc(), &asts); |
| 402 | for (AST* ast : asts) { |
| 403 | LOG("%s", ast->DebugString().c_str()); |
| 404 | ast->Eval(ev); |
Shinichiro Hamaji | 76ff983 | 2015-06-18 17:11:22 +0900 | [diff] [blame] | 405 | //delete ast; |
Shinichiro Hamaji | 80456fb | 2015-06-18 14:56:10 +0900 | [diff] [blame] | 406 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 407 | } |
| 408 | |
Shinichiro Hamaji | 5f57a99 | 2015-06-30 19:39:39 +0900 | [diff] [blame] | 409 | //#define TEST_FIND_EMULATOR |
| 410 | |
| 411 | #ifdef TEST_FIND_EMULATOR |
| 412 | static string SortWordsInString(StringPiece s) { |
| 413 | vector<string> toks; |
| 414 | for (StringPiece tok : WordScanner(s)) { |
| 415 | toks.push_back(tok.as_string()); |
| 416 | } |
| 417 | sort(toks.begin(), toks.end()); |
| 418 | return JoinStrings(toks, " "); |
| 419 | } |
| 420 | #endif |
| 421 | |
Shinichiro Hamaji | 68e712b | 2015-07-17 06:11:08 +0900 | [diff] [blame] | 422 | |
| 423 | // A hack for Android build. We need to evaluate things like $((3+4)) |
| 424 | // when we emit ninja file, because the result of such expressions |
| 425 | // will be passed to other make functions. |
| 426 | // TODO: Maybe we should introduce a helper binary which evaluate |
| 427 | // make expressions at ninja-time. |
| 428 | static bool HasNoIoInShellScript(const string& cmd) { |
| 429 | if (cmd.empty()) |
| 430 | return true; |
| 431 | if (HasPrefix(cmd, "echo $((") && cmd[cmd.size()-1] == ')') |
| 432 | return true; |
| 433 | return false; |
| 434 | } |
| 435 | |
Shinichiro Hamaji | fcf1b76 | 2015-06-18 03:43:54 +0900 | [diff] [blame] | 436 | void ShellFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 437 | shared_ptr<string> cmd = args[0]->Eval(ev); |
Shinichiro Hamaji | 68e712b | 2015-07-17 06:11:08 +0900 | [diff] [blame] | 438 | if (ev->avoid_io() && !HasNoIoInShellScript(*cmd)) { |
Shinichiro Hamaji | df1fc8b | 2015-06-29 15:45:21 +0900 | [diff] [blame] | 439 | *s += "$("; |
| 440 | *s += *cmd; |
| 441 | *s += ")"; |
| 442 | return; |
| 443 | } |
| 444 | |
Shinichiro Hamaji | fcf1b76 | 2015-06-18 03:43:54 +0900 | [diff] [blame] | 445 | LOG("ShellFunc: %s", cmd->c_str()); |
Shinichiro Hamaji | 5f57a99 | 2015-06-30 19:39:39 +0900 | [diff] [blame] | 446 | |
| 447 | #ifdef TEST_FIND_EMULATOR |
| 448 | bool need_check = false; |
| 449 | string out2; |
Shinichiro Hamaji | 4e950e6 | 2015-07-02 02:59:55 +0900 | [diff] [blame] | 450 | if (FindEmulator::Get() && FindEmulator::Get()->HandleFind(*cmd, &out2)) { |
Shinichiro Hamaji | 5f57a99 | 2015-06-30 19:39:39 +0900 | [diff] [blame] | 451 | need_check = true; |
| 452 | } |
| 453 | #else |
Shinichiro Hamaji | 4e950e6 | 2015-07-02 02:59:55 +0900 | [diff] [blame] | 454 | if (FindEmulator::Get() && FindEmulator::Get()->HandleFind(*cmd, s)) |
Shinichiro Hamaji | 5f57a99 | 2015-06-30 19:39:39 +0900 | [diff] [blame] | 455 | return; |
| 456 | #endif |
| 457 | |
Shinichiro Hamaji | 5f57a99 | 2015-06-30 19:39:39 +0900 | [diff] [blame] | 458 | COLLECT_STATS_WITH_SLOW_REPORT("func shell time", cmd->c_str()); |
Shinichiro Hamaji | fcf1b76 | 2015-06-18 03:43:54 +0900 | [diff] [blame] | 459 | string out; |
Shinichiro Hamaji | 94d6f2a | 2015-07-05 05:32:25 +0900 | [diff] [blame] | 460 | shared_ptr<string> shell = ev->EvalVar(kShellSym); |
| 461 | RunCommand(*shell, *cmd, false, &out); |
Shinichiro Hamaji | fcf1b76 | 2015-06-18 03:43:54 +0900 | [diff] [blame] | 462 | |
| 463 | while (out[out.size()-1] == '\n') |
| 464 | out.pop_back(); |
| 465 | for (size_t i = 0; i < out.size(); i++) { |
| 466 | if (out[i] == '\n') |
| 467 | out[i] = ' '; |
| 468 | } |
Shinichiro Hamaji | 5f57a99 | 2015-06-30 19:39:39 +0900 | [diff] [blame] | 469 | |
| 470 | #ifdef TEST_FIND_EMULATOR |
| 471 | if (need_check) { |
| 472 | string sorted = SortWordsInString(out); |
| 473 | out2 = SortWordsInString(out2); |
| 474 | if (sorted != out2) { |
| 475 | ERROR("FindEmulator is broken: %s\n%s\nvs\n%s", |
| 476 | cmd->c_str(), sorted.c_str(), out2.c_str()); |
| 477 | } |
| 478 | } |
| 479 | #endif |
| 480 | |
Shinichiro Hamaji | fcf1b76 | 2015-06-18 03:43:54 +0900 | [diff] [blame] | 481 | *s += out; |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 482 | } |
| 483 | |
Shinichiro Hamaji | 3064f1f | 2015-06-18 16:32:09 +0900 | [diff] [blame] | 484 | void CallFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
Colin Cross | 580cc1f | 2015-07-27 13:18:18 -0700 | [diff] [blame] | 485 | static const string tmpvar_names[] = { |
Shinichiro Hamaji | 3064f1f | 2015-06-18 16:32:09 +0900 | [diff] [blame] | 486 | "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" |
| 487 | }; |
| 488 | |
| 489 | shared_ptr<string> func_name = args[0]->Eval(ev); |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 490 | Var* func = ev->LookupVar(Intern(*func_name)); |
Shinichiro Hamaji | 3064f1f | 2015-06-18 16:32:09 +0900 | [diff] [blame] | 491 | vector<unique_ptr<SimpleVar>> av; |
Shinichiro Hamaji | 3064f1f | 2015-06-18 16:32:09 +0900 | [diff] [blame] | 492 | for (size_t i = 1; i < args.size(); i++) { |
Shinichiro Hamaji | f62e9a7 | 2015-06-26 04:18:21 +0900 | [diff] [blame] | 493 | unique_ptr<SimpleVar> s( |
| 494 | new SimpleVar(args[i]->Eval(ev), VarOrigin::AUTOMATIC)); |
Shinichiro Hamaji | 3064f1f | 2015-06-18 16:32:09 +0900 | [diff] [blame] | 495 | av.push_back(move(s)); |
| 496 | } |
Shinichiro Hamaji | 5d53bc7 | 2015-06-26 08:31:54 +0900 | [diff] [blame] | 497 | vector<unique_ptr<ScopedVar>> sv; |
| 498 | for (size_t i = 1; i < args.size(); i++) { |
Colin Cross | 580cc1f | 2015-07-27 13:18:18 -0700 | [diff] [blame] | 499 | string s; |
| 500 | StringPiece tmpvar_name; |
| 501 | if (i < sizeof(tmpvar_names)/sizeof(tmpvar_names[0])) { |
| 502 | tmpvar_name = tmpvar_names[i]; |
| 503 | } else { |
| 504 | s = StringPrintf("%d", i); |
| 505 | tmpvar_name = s; |
| 506 | } |
Shinichiro Hamaji | 5d53bc7 | 2015-06-26 08:31:54 +0900 | [diff] [blame] | 507 | sv.push_back(move(unique_ptr<ScopedVar>( |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 508 | new ScopedVar(ev->mutable_vars(), |
Colin Cross | 580cc1f | 2015-07-27 13:18:18 -0700 | [diff] [blame] | 509 | Intern(tmpvar_name), av[i-1].get())))); |
Shinichiro Hamaji | 5d53bc7 | 2015-06-26 08:31:54 +0900 | [diff] [blame] | 510 | } |
Shinichiro Hamaji | 3064f1f | 2015-06-18 16:32:09 +0900 | [diff] [blame] | 511 | func->Eval(ev, s); |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 512 | } |
| 513 | |
Shinichiro Hamaji | cf0cd68 | 2015-06-18 16:18:13 +0900 | [diff] [blame] | 514 | void ForeachFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 515 | shared_ptr<string> varname = args[0]->Eval(ev); |
| 516 | shared_ptr<string> list = args[1]->Eval(ev); |
| 517 | WordWriter ww(s); |
| 518 | for (StringPiece tok : WordScanner(*list)) { |
| 519 | unique_ptr<SimpleVar> v(new SimpleVar( |
Shinichiro Hamaji | f62e9a7 | 2015-06-26 04:18:21 +0900 | [diff] [blame] | 520 | make_shared<string>(tok.data(), tok.size()), VarOrigin::AUTOMATIC)); |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 521 | ScopedVar sv(ev->mutable_vars(), Intern(*varname), v.get()); |
Shinichiro Hamaji | cf0cd68 | 2015-06-18 16:18:13 +0900 | [diff] [blame] | 522 | ww.MaybeAddWhitespace(); |
| 523 | args[2]->Eval(ev, s); |
| 524 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 525 | } |
| 526 | |
Shinichiro Hamaji | c22fdb4 | 2015-06-22 18:10:40 +0900 | [diff] [blame] | 527 | void OriginFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 528 | shared_ptr<string> var_name = args[0]->Eval(ev); |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 529 | Var* var = ev->LookupVar(Intern(*var_name)); |
Shinichiro Hamaji | f62e9a7 | 2015-06-26 04:18:21 +0900 | [diff] [blame] | 530 | *s += GetOriginStr(var->Origin()); |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 531 | } |
| 532 | |
Shinichiro Hamaji | c22fdb4 | 2015-06-22 18:10:40 +0900 | [diff] [blame] | 533 | void FlavorFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 534 | shared_ptr<string> var_name = args[0]->Eval(ev); |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 535 | Var* var = ev->LookupVar(Intern(*var_name)); |
Shinichiro Hamaji | c22fdb4 | 2015-06-22 18:10:40 +0900 | [diff] [blame] | 536 | *s += var->Flavor(); |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 537 | } |
| 538 | |
Shinichiro Hamaji | 86e1133 | 2015-07-28 15:24:17 +0900 | [diff] [blame] | 539 | void InfoFunc(const vector<Value*>& args, Evaluator* ev, string*) { |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 540 | shared_ptr<string> a = args[0]->Eval(ev); |
Shinichiro Hamaji | df1fc8b | 2015-06-29 15:45:21 +0900 | [diff] [blame] | 541 | if (ev->avoid_io()) { |
Shinichiro Hamaji | 86e1133 | 2015-07-28 15:24:17 +0900 | [diff] [blame] | 542 | ev->add_delayed_output_command(StringPrintf("echo '%s'", a->c_str())); |
Shinichiro Hamaji | df1fc8b | 2015-06-29 15:45:21 +0900 | [diff] [blame] | 543 | return; |
| 544 | } |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 545 | printf("%s\n", a->c_str()); |
| 546 | fflush(stdout); |
| 547 | } |
| 548 | |
Shinichiro Hamaji | 86e1133 | 2015-07-28 15:24:17 +0900 | [diff] [blame] | 549 | void WarningFunc(const vector<Value*>& args, Evaluator* ev, string*) { |
Shinichiro Hamaji | 9619b36 | 2015-06-16 16:13:25 +0900 | [diff] [blame] | 550 | shared_ptr<string> a = args[0]->Eval(ev); |
Shinichiro Hamaji | df1fc8b | 2015-06-29 15:45:21 +0900 | [diff] [blame] | 551 | if (ev->avoid_io()) { |
Shinichiro Hamaji | 86e1133 | 2015-07-28 15:24:17 +0900 | [diff] [blame] | 552 | ev->add_delayed_output_command( |
| 553 | StringPrintf("echo '%s:%d: %s' 2>&1", LOCF(ev->loc()), a->c_str())); |
Shinichiro Hamaji | df1fc8b | 2015-06-29 15:45:21 +0900 | [diff] [blame] | 554 | return; |
| 555 | } |
Shinichiro Hamaji | 8ee8c37 | 2015-06-16 16:19:40 +0900 | [diff] [blame] | 556 | printf("%s:%d: %s\n", LOCF(ev->loc()), a->c_str()); |
Shinichiro Hamaji | 9619b36 | 2015-06-16 16:13:25 +0900 | [diff] [blame] | 557 | fflush(stdout); |
| 558 | } |
| 559 | |
Shinichiro Hamaji | 86e1133 | 2015-07-28 15:24:17 +0900 | [diff] [blame] | 560 | void ErrorFunc(const vector<Value*>& args, Evaluator* ev, string*) { |
Shinichiro Hamaji | 9619b36 | 2015-06-16 16:13:25 +0900 | [diff] [blame] | 561 | shared_ptr<string> a = args[0]->Eval(ev); |
Shinichiro Hamaji | df1fc8b | 2015-06-29 15:45:21 +0900 | [diff] [blame] | 562 | if (ev->avoid_io()) { |
Shinichiro Hamaji | 86e1133 | 2015-07-28 15:24:17 +0900 | [diff] [blame] | 563 | ev->add_delayed_output_command( |
| 564 | StringPrintf("echo '%s:%d: *** %s.' 2>&1 && false", |
| 565 | LOCF(ev->loc()), a->c_str())); |
Shinichiro Hamaji | df1fc8b | 2015-06-29 15:45:21 +0900 | [diff] [blame] | 566 | return; |
| 567 | } |
Shinichiro Hamaji | 9619b36 | 2015-06-16 16:13:25 +0900 | [diff] [blame] | 568 | ev->Error(StringPrintf("*** %s.", a->c_str())); |
| 569 | } |
| 570 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 571 | FuncInfo g_func_infos[] = { |
Shinichiro Hamaji | fead3b7 | 2015-06-18 15:31:15 +0900 | [diff] [blame] | 572 | { "patsubst", &PatsubstFunc, 3, 3, false, false }, |
| 573 | { "strip", &StripFunc, 1, 1, false, false }, |
| 574 | { "subst", &SubstFunc, 3, 3, false, false }, |
| 575 | { "findstring", &FindstringFunc, 2, 2, false, false }, |
| 576 | { "filter", &FilterFunc, 2, 2, false, false }, |
| 577 | { "filter-out", &FilterOutFunc, 2, 2, false, false }, |
| 578 | { "sort", &SortFunc, 1, 1, false, false }, |
| 579 | { "word", &WordFunc, 2, 2, false, false }, |
| 580 | { "wordlist", &WordlistFunc, 3, 3, false, false }, |
| 581 | { "words", &WordsFunc, 1, 1, false, false }, |
| 582 | { "firstword", &FirstwordFunc, 1, 1, false, false }, |
| 583 | { "lastword", &LastwordFunc, 1, 1, false, false }, |
Shinichiro Hamaji | 2e6cbfc | 2015-06-16 18:46:50 +0900 | [diff] [blame] | 584 | |
Shinichiro Hamaji | fead3b7 | 2015-06-18 15:31:15 +0900 | [diff] [blame] | 585 | { "join", &JoinFunc, 2, 2, false, false }, |
| 586 | { "wildcard", &WildcardFunc, 1, 1, false, false }, |
| 587 | { "dir", &DirFunc, 1, 1, false, false }, |
| 588 | { "notdir", &NotdirFunc, 1, 1, false, false }, |
| 589 | { "suffix", &SuffixFunc, 1, 1, false, false }, |
| 590 | { "basename", &BasenameFunc, 1, 1, false, false }, |
| 591 | { "addsuffix", &AddsuffixFunc, 2, 2, false, false }, |
| 592 | { "addprefix", &AddprefixFunc, 2, 2, false, false }, |
| 593 | { "realpath", &RealpathFunc, 1, 1, false, false }, |
| 594 | { "abspath", &AbspathFunc, 1, 1, false, false }, |
Shinichiro Hamaji | 30b8e60 | 2015-06-17 19:28:54 +0900 | [diff] [blame] | 595 | |
Shinichiro Hamaji | fead3b7 | 2015-06-18 15:31:15 +0900 | [diff] [blame] | 596 | { "if", &IfFunc, 3, 2, false, true }, |
| 597 | { "and", &AndFunc, 0, 0, true, false }, |
| 598 | { "or", &OrFunc, 0, 0, true, false }, |
Shinichiro Hamaji | e22fe8e | 2015-06-18 07:11:54 +0900 | [diff] [blame] | 599 | |
Shinichiro Hamaji | fead3b7 | 2015-06-18 15:31:15 +0900 | [diff] [blame] | 600 | { "value", &ValueFunc, 1, 1, false, false }, |
| 601 | { "eval", &EvalFunc, 1, 1, false, false }, |
| 602 | { "shell", &ShellFunc, 1, 1, false, false }, |
| 603 | { "call", &CallFunc, 0, 0, false, false }, |
| 604 | { "foreach", &ForeachFunc, 3, 3, false, false }, |
Shinichiro Hamaji | e22fe8e | 2015-06-18 07:11:54 +0900 | [diff] [blame] | 605 | |
Shinichiro Hamaji | fead3b7 | 2015-06-18 15:31:15 +0900 | [diff] [blame] | 606 | { "origin", &OriginFunc, 1, 1, false, false }, |
| 607 | { "flavor", &FlavorFunc, 1, 1, false, false }, |
Shinichiro Hamaji | e22fe8e | 2015-06-18 07:11:54 +0900 | [diff] [blame] | 608 | |
Shinichiro Hamaji | fead3b7 | 2015-06-18 15:31:15 +0900 | [diff] [blame] | 609 | { "info", &InfoFunc, 1, 1, false, false }, |
| 610 | { "warning", &WarningFunc, 1, 1, false, false }, |
| 611 | { "error", &ErrorFunc, 1, 1, false, false }, |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 612 | }; |
| 613 | |
| 614 | unordered_map<StringPiece, FuncInfo*>* g_func_info_map; |
| 615 | |
| 616 | } // namespace |
| 617 | |
| 618 | void InitFuncTable() { |
| 619 | g_func_info_map = new unordered_map<StringPiece, FuncInfo*>; |
| 620 | for (size_t i = 0; i < sizeof(g_func_infos) / sizeof(g_func_infos[0]); i++) { |
| 621 | FuncInfo* fi = &g_func_infos[i]; |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 622 | bool ok = g_func_info_map->emplace(fi->name, fi).second; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 623 | CHECK(ok); |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | void QuitFuncTable() { |
| 628 | delete g_func_info_map; |
| 629 | } |
| 630 | |
| 631 | FuncInfo* GetFuncInfo(StringPiece name) { |
| 632 | auto found = g_func_info_map->find(name); |
| 633 | if (found == g_func_info_map->end()) |
| 634 | return NULL; |
| 635 | return found->second; |
| 636 | } |